2006-09-06 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / util.h
blob10828d7e54907aa63b56d1106310087c4d184984
1 /* util.h - Utility functions for GnuPG
2 * Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #ifndef GNUPG_COMMON_UTIL_H
23 #define GNUPG_COMMON_UTIL_H
25 #include <gcrypt.h> /* We need this for the memory function protos. */
26 #include <time.h> /* We need time_t. */
27 #include <errno.h> /* We need errno. */
28 #include <gpg-error.h> /* We need gpg_error_t. */
30 /* Common GNUlib includes (-I ../gl/). */
31 #include "strpbrk.h"
32 #include "strsep.h"
33 #include "vasprintf.h"
36 /* Hash function used with libksba. */
37 #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
39 /* get all the stuff from jnlib */
40 #include "../jnlib/logging.h"
41 #include "../jnlib/argparse.h"
42 #include "../jnlib/stringhelp.h"
43 #include "../jnlib/mischelp.h"
44 #include "../jnlib/strlist.h"
45 #include "../jnlib/dotlock.h"
46 #include "../jnlib/utf8conv.h"
48 /* Handy malloc macros - please use only them. */
49 #define xtrymalloc(a) gcry_malloc ((a))
50 #define xtrymalloc_secure(a) gcry_malloc_secure ((a))
51 #define xtrycalloc(a,b) gcry_calloc ((a),(b))
52 #define xtrycalloc_secure(a,b) gcry_calloc_secure ((a),(b))
53 #define xtryrealloc(a,b) gcry_realloc ((a),(b))
54 #define xtrystrdup(a) gcry_strdup ((a))
55 #define xfree(a) gcry_free ((a))
57 #define xmalloc(a) gcry_xmalloc ((a))
58 #define xmalloc_secure(a) gcry_xmalloc_secure ((a))
59 #define xcalloc(a,b) gcry_xcalloc ((a),(b))
60 #define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
61 #define xrealloc(a,b) gcry_xrealloc ((a),(b))
62 #define xstrdup(a) gcry_xstrdup ((a))
64 /* For compatibility with gpg 1.4 we also define these: */
65 #define xmalloc_clear(a) gcry_xcalloc (1, (a))
66 #define xmalloc_secure_clear(a) gcry_xcalloc_secure (1, (a))
68 /* Convenience function to return a gpg-error code for memory
69 allocation failures. This function makes sure that an error will
70 be returned even if accidently ERRNO is not set. */
71 static inline gpg_error_t
72 out_of_core (void)
74 return gpg_error (errno
75 ? gpg_err_code_from_errno(errno)
76 : GPG_ERR_MISSING_ERRNO);
79 /* A type to hold the ISO time. Note that this this is the same as
80 the the KSBA type ksba_isotime_t. */
81 typedef char gnupg_isotime_t[16];
84 /*-- gettime.c --*/
85 time_t gnupg_get_time (void);
86 void gnupg_get_isotime (gnupg_isotime_t timebuf);
87 void gnupg_set_time (time_t newtime, int freeze);
88 int gnupg_faked_time_p (void);
89 u32 make_timestamp (void);
90 u32 scan_isodatestr (const char *string);
91 u32 add_days_to_timestamp (u32 stamp, u16 days);
92 const char *strtimevalue (u32 stamp);
93 const char *strtimestamp (u32 stamp); /* GMT */
94 const char *isotimestamp (u32 stamp); /* GMT */
95 const char *asctimestamp (u32 stamp); /* localized */
98 /* Copy one iso ddate to another, this is inline so that we can do a
99 sanity check. */
100 static inline void
101 gnupg_copy_time (gnupg_isotime_t d, const gnupg_isotime_t s)
103 if (*s && (strlen (s) != 15 || s[8] != 'T'))
104 BUG();
105 strcpy (d, s);
109 /*-- signal.c --*/
110 void gnupg_init_signals (int mode, void (*fast_cleanup)(void));
111 void gnupg_pause_on_sigusr (int which);
112 void gnupg_block_all_signals (void);
113 void gnupg_unblock_all_signals (void);
115 /*-- yesno.c --*/
116 int answer_is_yes (const char *s);
117 int answer_is_yes_no_default (const char *s, int def_answer);
118 int answer_is_yes_no_quit (const char *s);
119 int answer_is_okay_cancel (const char *s, int def_answer);
121 /*-- xreadline.c --*/
122 ssize_t read_line (FILE *fp,
123 char **addr_of_buffer, size_t *length_of_buffer,
124 size_t *max_length);
127 /*-- b64enc.c --*/
128 struct b64state
130 unsigned int flags;
131 int idx;
132 int quad_count;
133 FILE *fp;
134 char *title;
135 unsigned char radbuf[4];
137 gpg_error_t b64enc_start (struct b64state *state, FILE *fp, const char *title);
138 gpg_error_t b64enc_write (struct b64state *state,
139 const void *buffer, size_t nbytes);
140 gpg_error_t b64enc_finish (struct b64state *state);
142 /*-- sexputil.c */
143 gpg_error_t keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
144 unsigned char *grip);
145 int cmp_simple_canon_sexp (const unsigned char *a, const unsigned char *b);
146 unsigned char *make_simple_sexp_from_hexstr (const char *line,
147 size_t *nscanned);
149 /*-- homedir.c --*/
150 const char *default_homedir (void);
153 /*-- miscellaneous.c --*/
155 /* Same as asprintf but return an allocated buffer suitable to be
156 freed using xfree. This function simply dies on memory failure,
157 thus no extra check is required. */
158 char *xasprintf (const char *fmt, ...) JNLIB_GCC_A_PRINTF(1,2);
159 /* Same as asprintf but return an allocated buffer suitable to be
160 freed using xfree. This function returns NULL on memory failure and
161 sets errno. */
162 char *xtryasprintf (const char *fmt, ...) JNLIB_GCC_A_PRINTF(1,2);
164 const char *print_fname_stdout (const char *s);
165 const char *print_fname_stdin (const char *s);
166 void print_string (FILE *fp, const byte *p, size_t n, int delim);
167 void print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim);
168 void print_utf8_string (FILE *fp, const byte *p, size_t n);
169 char *make_printable_string (const void *p, size_t n, int delim);
171 int is_file_compressed (const char *s, int *ret_rc);
173 int match_multistr (const char *multistr,const char *match);
176 /*-- Simple replacement functions. */
177 #ifndef HAVE_TTYNAME
178 /* Systems without ttyname (W32) will merely return NULL. */
179 static inline char *
180 ttyname (int fd)
182 return NULL
184 #endif /* !HAVE_TTYNAME */
186 #ifndef HAVE_ISASCII
187 static inline int
188 isascii (int c)
190 return (((c) & ~0x7f) == 0);
192 #endif /* !HAVE_ISASCII */
194 /*-- Macros to replace ctype ones to avoid locale problems. --*/
195 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
196 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
197 #define hexdigitp(a) (digitp (a) \
198 || (*(a) >= 'A' && *(a) <= 'F') \
199 || (*(a) >= 'a' && *(a) <= 'f'))
200 /* Note this isn't identical to a C locale isspace() without \f and
201 \v, but works for the purposes used here. */
202 #define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
204 /* The atoi macros assume that the buffer has only valid digits. */
205 #define atoi_1(p) (*(p) - '0' )
206 #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
207 #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
208 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
209 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
210 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
211 #define xtoi_4(p) ((xtoi_2(p) * 256) + xtoi_2((p)+2))
215 #endif /*GNUPG_COMMON_UTIL_H*/