chiark / gitweb /
gnupg2 (2.1.17-3) unstable; urgency=medium
[gnupg2.git] / common / util.h
1 /* util.h - Utility functions for GnuPG
2  * Copyright (C) 2001, 2002, 2003, 2004, 2009 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify it
7  * under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * GnuPG is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copies of the GNU General Public License
27  * and the GNU Lesser General Public License along with this program;
28  * if not, see <https://www.gnu.org/licenses/>.
29  */
30
31 #ifndef GNUPG_COMMON_UTIL_H
32 #define GNUPG_COMMON_UTIL_H
33
34 #include <gcrypt.h> /* We need this for the memory function protos. */
35 #include <errno.h>  /* We need errno.  */
36 #include <gpg-error.h> /* We need gpg_error_t and estream. */
37
38 /* These error codes are used but not defined in the required
39  * libgpg-error version.  Define them here.
40  * Example: (#if GPG_ERROR_VERSION_NUMBER < 0x011500 // 1.21)
41  */
42 #if GPG_ERROR_VERSION_NUMBER < 0x011a00 /* 1.26 */
43 # define GPG_ERR_UNKNOWN_FLAG     309
44 # define GPG_ERR_INV_ORDER        310
45 # define GPG_ERR_ALREADY_FETCHED  311
46 # define GPG_ERR_TRY_LATER        312
47 # define GPG_ERR_SYSTEM_BUG       666
48 # define GPG_ERR_DNS_UNKNOWN      711
49 # define GPG_ERR_DNS_SECTION      712
50 # define GPG_ERR_DNS_ADDRESS      713
51 # define GPG_ERR_DNS_NO_QUERY     714
52 # define GPG_ERR_DNS_NO_ANSWER    715
53 # define GPG_ERR_DNS_CLOSED       716
54 # define GPG_ERR_DNS_VERIFY       717
55 # define GPG_ERR_DNS_TIMEOUT      718
56 #endif
57
58
59 /* Hash function used with libksba. */
60 #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
61
62 /* Get all the stuff from jnlib. */
63 #include "../common/logging.h"
64 #include "../common/argparse.h"
65 #include "../common/stringhelp.h"
66 #include "../common/mischelp.h"
67 #include "../common/strlist.h"
68 #include "../common/dotlock.h"
69 #include "../common/utf8conv.h"
70 #include "../common/dynload.h"
71 #include "../common/fwddecl.h"
72 #include "../common/utilproto.h"
73
74 #include "gettime.h"
75
76 /* Redefine asprintf by our estream version which uses our own memory
77    allocator..  */
78 #define asprintf gpgrt_asprintf
79 #define vasprintf gpgrt_vasprintf
80
81 /* Due to a bug in mingw32's snprintf related to the 'l' modifier and
82    for increased portability we use our snprintf on all systems. */
83 #undef snprintf
84 #define snprintf gpgrt_snprintf
85
86
87 /* Replacements for macros not available with libgpg-error < 1.20.  */
88
89 /* We need this type even if we are not using libreadline and or we
90    did not include libreadline in the current file. */
91 #ifndef GNUPG_LIBREADLINE_H_INCLUDED
92 typedef char **rl_completion_func_t (const char *, int, int);
93 #endif /*!GNUPG_LIBREADLINE_H_INCLUDED*/
94
95
96 /* Handy malloc macros - please use only them. */
97 #define xtrymalloc(a)    gcry_malloc ((a))
98 #define xtrymalloc_secure(a)  gcry_malloc_secure ((a))
99 #define xtrycalloc(a,b)  gcry_calloc ((a),(b))
100 #define xtrycalloc_secure(a,b)  gcry_calloc_secure ((a),(b))
101 #define xtryrealloc(a,b) gcry_realloc ((a),(b))
102 #define xtrystrdup(a)    gcry_strdup ((a))
103 #define xfree(a)         gcry_free ((a))
104 #define xfree_fnc        gcry_free
105
106 #define xmalloc(a)       gcry_xmalloc ((a))
107 #define xmalloc_secure(a)  gcry_xmalloc_secure ((a))
108 #define xcalloc(a,b)     gcry_xcalloc ((a),(b))
109 #define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
110 #define xrealloc(a,b)    gcry_xrealloc ((a),(b))
111 #define xstrdup(a)       gcry_xstrdup ((a))
112
113 /* For compatibility with gpg 1.4 we also define these: */
114 #define xmalloc_clear(a) gcry_xcalloc (1, (a))
115 #define xmalloc_secure_clear(a) gcry_xcalloc_secure (1, (a))
116
117 /* The default error source of the application.  This is different
118    from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the
119    source file and thus is usable in code shared by applications.
120    Defined by init.c.  */
121 extern gpg_err_source_t default_errsource;
122
123 /* Convenience function to return a gpg-error code for memory
124    allocation failures.  This function makes sure that an error will
125    be returned even if accidentally ERRNO is not set.  */
126 static inline gpg_error_t
127 out_of_core (void)
128 {
129   return gpg_error_from_syserror ();
130 }
131
132
133 /*-- yesno.c --*/
134 int answer_is_yes (const char *s);
135 int answer_is_yes_no_default (const char *s, int def_answer);
136 int answer_is_yes_no_quit (const char *s);
137 int answer_is_okay_cancel (const char *s, int def_answer);
138
139 /*-- xreadline.c --*/
140 ssize_t read_line (FILE *fp,
141                    char **addr_of_buffer, size_t *length_of_buffer,
142                    size_t *max_length);
143
144
145 /*-- b64enc.c and b64dec.c --*/
146 struct b64state
147 {
148   unsigned int flags;
149   int idx;
150   int quad_count;
151   FILE *fp;
152   estream_t stream;
153   char *title;
154   unsigned char radbuf[4];
155   u32 crc;
156   int stop_seen:1;
157   int invalid_encoding:1;
158   gpg_error_t lasterr;
159 };
160
161 gpg_error_t b64enc_start (struct b64state *state, FILE *fp, const char *title);
162 gpg_error_t b64enc_start_es (struct b64state *state, estream_t fp,
163                              const char *title);
164 gpg_error_t b64enc_write (struct b64state *state,
165                           const void *buffer, size_t nbytes);
166 gpg_error_t b64enc_finish (struct b64state *state);
167
168 gpg_error_t b64dec_start (struct b64state *state, const char *title);
169 gpg_error_t b64dec_proc (struct b64state *state, void *buffer, size_t length,
170                          size_t *r_nbytes);
171 gpg_error_t b64dec_finish (struct b64state *state);
172
173 /*-- sexputil.c */
174 char *canon_sexp_to_string (const unsigned char *canon, size_t canonlen);
175 void log_printcanon (const char *text,
176                      const unsigned char *sexp, size_t sexplen);
177 void log_printsexp (const char *text, gcry_sexp_t sexp);
178
179 gpg_error_t make_canon_sexp (gcry_sexp_t sexp,
180                              unsigned char **r_buffer, size_t *r_buflen);
181 gpg_error_t make_canon_sexp_pad (gcry_sexp_t sexp, int secure,
182                                  unsigned char **r_buffer, size_t *r_buflen);
183 gpg_error_t keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
184                                      unsigned char *grip);
185 int cmp_simple_canon_sexp (const unsigned char *a, const unsigned char *b);
186 unsigned char *make_simple_sexp_from_hexstr (const char *line,
187                                              size_t *nscanned);
188 int hash_algo_from_sigval (const unsigned char *sigval);
189 unsigned char *make_canon_sexp_from_rsa_pk (const void *m, size_t mlen,
190                                             const void *e, size_t elen,
191                                             size_t *r_len);
192 gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata,
193                                         size_t keydatalen,
194                                         unsigned char const **r_n,
195                                         size_t *r_nlen,
196                                         unsigned char const **r_e,
197                                         size_t *r_elen);
198 gpg_error_t get_pk_algo_from_canon_sexp (const unsigned char *keydata,
199                                          size_t keydatalen,
200                                          const char **r_algo);
201 int get_pk_algo_from_key (gcry_sexp_t key);
202
203 /*-- convert.c --*/
204 int hex2bin (const char *string, void *buffer, size_t length);
205 int hexcolon2bin (const char *string, void *buffer, size_t length);
206 char *bin2hex (const void *buffer, size_t length, char *stringbuf);
207 char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
208 const char *hex2str (const char *hexstring,
209                      char *buffer, size_t bufsize, size_t *buflen);
210 char *hex2str_alloc (const char *hexstring, size_t *r_count);
211
212 /*-- percent.c --*/
213 char *percent_plus_escape (const char *string);
214 char *percent_plus_unescape (const char *string, int nulrepl);
215 char *percent_unescape (const char *string, int nulrepl);
216
217 size_t percent_plus_unescape_inplace (char *string, int nulrepl);
218 size_t percent_unescape_inplace (char *string, int nulrepl);
219
220 /*-- openpgp-oid.c --*/
221 gpg_error_t openpgp_oid_from_str (const char *string, gcry_mpi_t *r_mpi);
222 char *openpgp_oid_to_str (gcry_mpi_t a);
223 int openpgp_oid_is_ed25519 (gcry_mpi_t a);
224 int openpgp_oid_is_cv25519 (gcry_mpi_t a);
225 const char *openpgp_curve_to_oid (const char *name, unsigned int *r_nbits);
226 const char *openpgp_oid_to_curve (const char *oid, int canon);
227 const char *openpgp_enum_curves (int *idxp);
228 const char *openpgp_is_curve_supported (const char *name,
229                                         int *r_algo, unsigned int *r_nbits);
230
231
232 /*-- homedir.c --*/
233 const char *standard_homedir (void);
234 const char *default_homedir (void);
235 void gnupg_set_homedir (const char *newdir);
236 const char *gnupg_homedir (void);
237 int gnupg_default_homedir_p (void);
238 const char *gnupg_socketdir (void);
239 const char *gnupg_sysconfdir (void);
240 const char *gnupg_bindir (void);
241 const char *gnupg_libexecdir (void);
242 const char *gnupg_libdir (void);
243 const char *gnupg_datadir (void);
244 const char *gnupg_localedir (void);
245 const char *gnupg_cachedir (void);
246 const char *dirmngr_socket_name (void);
247
248 char *_gnupg_socketdir_internal (int skip_checks, unsigned *r_info);
249
250 /* All module names.  We also include gpg and gpgsm for the sake for
251    gpgconf. */
252 #define GNUPG_MODULE_NAME_AGENT        1
253 #define GNUPG_MODULE_NAME_PINENTRY     2
254 #define GNUPG_MODULE_NAME_SCDAEMON     3
255 #define GNUPG_MODULE_NAME_DIRMNGR      4
256 #define GNUPG_MODULE_NAME_PROTECT_TOOL 5
257 #define GNUPG_MODULE_NAME_CHECK_PATTERN 6
258 #define GNUPG_MODULE_NAME_GPGSM         7
259 #define GNUPG_MODULE_NAME_GPG           8
260 #define GNUPG_MODULE_NAME_CONNECT_AGENT 9
261 #define GNUPG_MODULE_NAME_GPGCONF       10
262 #define GNUPG_MODULE_NAME_DIRMNGR_LDAP  11
263 #define GNUPG_MODULE_NAME_GPGV          12
264 const char *gnupg_module_name (int which);
265 void gnupg_module_name_flush_some (void);
266 void gnupg_set_builddir (const char *newdir);
267
268
269
270 /*-- gpgrlhelp.c --*/
271 void gnupg_rl_initialize (void);
272
273 /*-- helpfile.c --*/
274 char *gnupg_get_help_string (const char *key, int only_current_locale);
275
276 /*-- localename.c --*/
277 const char *gnupg_messages_locale_name (void);
278
279 /*-- miscellaneous.c --*/
280
281 /* This function is called at startup to tell libgcrypt to use our own
282    logging subsystem. */
283 void setup_libgcrypt_logging (void);
284
285 /* Print an out of core message and die.  */
286 void xoutofcore (void);
287
288 /* Same as estream_asprintf but die on memory failure.  */
289 char *xasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
290 /* This is now an alias to estream_asprintf.  */
291 char *xtryasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
292
293 /* Replacement for gcry_cipher_algo_name.  */
294 const char *gnupg_cipher_algo_name (int algo);
295
296 void obsolete_option (const char *configname, unsigned int configlineno,
297                       const char *name);
298
299 const char *print_fname_stdout (const char *s);
300 const char *print_fname_stdin (const char *s);
301 void print_utf8_buffer3 (estream_t fp, const void *p, size_t n,
302                          const char *delim);
303 void print_utf8_buffer2 (estream_t fp, const void *p, size_t n, int delim);
304 void print_utf8_buffer (estream_t fp, const void *p, size_t n);
305 void print_hexstring (FILE *fp, const void *buffer, size_t length,
306                       int reserved);
307 char *try_make_printable_string (const void *p, size_t n, int delim);
308 char *make_printable_string (const void *p, size_t n, int delim);
309
310 int is_file_compressed (const char *s, int *ret_rc);
311
312 int match_multistr (const char *multistr,const char *match);
313
314 int gnupg_compare_version (const char *a, const char *b);
315
316 struct debug_flags_s
317 {
318   unsigned int flag;
319   const char *name;
320 };
321 int parse_debug_flag (const char *string, unsigned int *debugvar,
322                       const struct debug_flags_s *flags);
323
324
325 /*-- Simple replacement functions. */
326
327 /* We use the gnupg_ttyname macro to be safe not to run into conflicts
328    which an extisting but broken ttyname.  */
329 #if !defined(HAVE_TTYNAME) || defined(HAVE_BROKEN_TTYNAME)
330 # define gnupg_ttyname(n) _gnupg_ttyname ((n))
331 /* Systems without ttyname (W32) will merely return NULL. */
332 static inline char *
333 _gnupg_ttyname (int fd)
334 {
335   (void)fd;
336   return NULL;
337 }
338 #else /*HAVE_TTYNAME*/
339 # define gnupg_ttyname(n) ttyname ((n))
340 #endif /*HAVE_TTYNAME */
341
342 #ifdef HAVE_W32CE_SYSTEM
343 #define getpid() GetCurrentProcessId ()
344 char *_gnupg_getenv (const char *name); /* See sysutils.c */
345 #define getenv(a)  _gnupg_getenv ((a))
346 char *_gnupg_setenv (const char *name); /* See sysutils.c */
347 #define setenv(a,b,c)  _gnupg_setenv ((a),(b),(c))
348 int _gnupg_isatty (int fd);
349 #define gnupg_isatty(a)  _gnupg_isatty ((a))
350 #else
351 #define gnupg_isatty(a)  isatty ((a))
352 #endif
353
354
355
356 /*-- Macros to replace ctype ones to avoid locale problems. --*/
357 #define spacep(p)   (*(p) == ' ' || *(p) == '\t')
358 #define digitp(p)   (*(p) >= '0' && *(p) <= '9')
359 #define alphap(p)   ((*(p) >= 'A' && *(p) <= 'Z')       \
360                      || (*(p) >= 'a' && *(p) <= 'z'))
361 #define alnump(p)   (alphap (p) || digitp (p))
362 #define hexdigitp(a) (digitp (a)                     \
363                       || (*(a) >= 'A' && *(a) <= 'F')  \
364                       || (*(a) >= 'a' && *(a) <= 'f'))
365   /* Note this isn't identical to a C locale isspace() without \f and
366      \v, but works for the purposes used here. */
367 #define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
368
369 /* The atoi macros assume that the buffer has only valid digits. */
370 #define atoi_1(p)   (*(p) - '0' )
371 #define atoi_2(p)   ((atoi_1(p) * 10) + atoi_1((p)+1))
372 #define atoi_4(p)   ((atoi_2(p) * 100) + atoi_2((p)+2))
373 #define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
374                      *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
375 #define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
376 #define xtoi_4(p)   ((xtoi_2(p) * 256) + xtoi_2((p)+2))
377
378 #endif /*GNUPG_COMMON_UTIL_H*/