Coverage Report

Created: 2026-03-03 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/util.h
Line
Count
Source
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 and/or modify this
7
 * part of GnuPG 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
43
44
#ifndef EXTERN_UNLESS_MAIN_MODULE
45
# if !defined (INCLUDED_BY_MAIN_MODULE)
46
#  define EXTERN_UNLESS_MAIN_MODULE extern
47
# else
48
#  define EXTERN_UNLESS_MAIN_MODULE
49
# endif
50
#endif
51
52
/* Hash function used with libksba. */
53
#define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
54
55
/* The length of the keygrip.  This is a SHA-1 hash of the key
56
 * parameters as generated by gcry_pk_get_keygrip.  */
57
0
#define KEYGRIP_LEN 20
58
59
/* The length of the unique blob identifier as used by the keyboxd.
60
 * This is the possible truncated fingerprint of the primary key.  */
61
0
#define UBID_LEN    20
62
63
64
/* Get all the stuff from jnlib. */
65
#include "../common/logging.h"
66
#include "../common/stringhelp.h"
67
#include "../common/mischelp.h"
68
#include "../common/strlist.h"
69
#include "../common/dotlock.h"
70
#include "../common/utf8conv.h"
71
#include "../common/dynload.h"
72
#include "../common/fwddecl.h"
73
#include "../common/utilproto.h"
74
75
#include "gettime.h"
76
77
/* Redefine asprintf by our estream version which uses our own memory
78
   allocator..  */
79
0
#define asprintf gpgrt_asprintf
80
#define vasprintf gpgrt_vasprintf
81
82
/* Due to a bug in mingw32's snprintf related to the 'l' modifier and
83
   for increased portability we use our snprintf on all systems. */
84
#undef snprintf
85
9
#define snprintf gpgrt_snprintf
86
87
88
/* Replacements for macros not available with libgpg-error < 1.20.  */
89
90
/* We need this type even if we are not using libreadline and or we
91
   did not include libreadline in the current file. */
92
#ifndef GNUPG_LIBREADLINE_H_INCLUDED
93
typedef char **rl_completion_func_t (const char *, int, int);
94
#endif /*!GNUPG_LIBREADLINE_H_INCLUDED*/
95
96
97
/* Handy malloc macros - please use only them. */
98
15
#define xtrymalloc(a)    gcry_malloc ((a))
99
0
#define xtrymalloc_secure(a)  gcry_malloc_secure ((a))
100
9
#define xtrycalloc(a,b)  gcry_calloc ((a),(b))
101
0
#define xtrycalloc_secure(a,b)  gcry_calloc_secure ((a),(b))
102
0
#define xtryrealloc(a,b) gcry_realloc ((a),(b))
103
#define xtryreallocarray(a,b,c,d) gpgrt_reallocarray ((a),(b),(c),(d))
104
0
#define xtrystrdup(a)    gcry_strdup ((a))
105
140
#define xfree(a)         gcry_free ((a))
106
#define xfree_fnc        gcry_free
107
108
70
#define xmalloc(a)       gcry_xmalloc ((a))
109
0
#define xmalloc_secure(a)  gcry_xmalloc_secure ((a))
110
15
#define xcalloc(a,b)     gcry_xcalloc ((a),(b))
111
#define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
112
138
#define xrealloc(a,b)    gcry_xrealloc ((a),(b))
113
17
#define xstrdup(a)       gcry_xstrdup ((a))
114
/* See also the xreallocarray prototype below.  */
115
116
/* For compatibility with gpg 1.4 we also define these: */
117
6
#define xmalloc_clear(a) gcry_xcalloc (1, (a))
118
0
#define xmalloc_secure_clear(a) gcry_xcalloc_secure (1, (a))
119
120
/* The default error source of the application.  This is different
121
   from GPG_ERR_SOURCE_DEFAULT in that it does not depend on the
122
   source file and thus is usable in code shared by applications.
123
   Defined by init.c.  */
124
extern gpg_err_source_t default_errsource;
125
126
/* Convenience function to return a gpg-error code for memory
127
   allocation failures.  This function makes sure that an error will
128
   be returned even if accidentally ERRNO is not set.  */
129
static inline gpg_error_t
130
out_of_core (void)
131
0
{
132
0
  return gpg_error_from_syserror ();
133
0
}
Unexecuted instantiation: fuzzer_stubs.c:out_of_core
Unexecuted instantiation: fuzz_decrypt.c:out_of_core
Unexecuted instantiation: trustdb.c:out_of_core
Unexecuted instantiation: pkclist.c:out_of_core
Unexecuted instantiation: trust.c:out_of_core
Unexecuted instantiation: cpr.c:out_of_core
Unexecuted instantiation: key-clean.c:out_of_core
Unexecuted instantiation: tdbio.c:out_of_core
Unexecuted instantiation: sig-check.c:out_of_core
Unexecuted instantiation: misc.c:out_of_core
Unexecuted instantiation: armor.c:out_of_core
Unexecuted instantiation: call-dirmngr.c:out_of_core
Unexecuted instantiation: getkey.c:out_of_core
Unexecuted instantiation: seskey.c:out_of_core
Unexecuted instantiation: expand-group.c:out_of_core
Unexecuted instantiation: call-agent.c:out_of_core
Unexecuted instantiation: call-keyboxd.c:out_of_core
Unexecuted instantiation: keyid.c:out_of_core
Unexecuted instantiation: tdbdump.c:out_of_core
Unexecuted instantiation: server.c:out_of_core
Unexecuted instantiation: keydb.c:out_of_core
Unexecuted instantiation: kbnode.c:out_of_core
Unexecuted instantiation: pkglue.c:out_of_core
Unexecuted instantiation: import.c:out_of_core
Unexecuted instantiation: key-check.c:out_of_core
Unexecuted instantiation: free-packet.c:out_of_core
Unexecuted instantiation: keylist.c:out_of_core
Unexecuted instantiation: helptext.c:out_of_core
Unexecuted instantiation: parse-packet.c:out_of_core
Unexecuted instantiation: decrypt.c:out_of_core
Unexecuted instantiation: compress.c:out_of_core
Unexecuted instantiation: openfile.c:out_of_core
Unexecuted instantiation: mainproc.c:out_of_core
Unexecuted instantiation: decrypt-data.c:out_of_core
Unexecuted instantiation: keyring.c:out_of_core
Unexecuted instantiation: pubkey-enc.c:out_of_core
Unexecuted instantiation: keyserver.c:out_of_core
Unexecuted instantiation: photoid.c:out_of_core
Unexecuted instantiation: export.c:out_of_core
Unexecuted instantiation: skclist.c:out_of_core
Unexecuted instantiation: keyedit.c:out_of_core
Unexecuted instantiation: objcache.c:out_of_core
Unexecuted instantiation: encrypt.c:out_of_core
Unexecuted instantiation: passphrase.c:out_of_core
Unexecuted instantiation: verify.c:out_of_core
Unexecuted instantiation: sign.c:out_of_core
Unexecuted instantiation: build-packet.c:out_of_core
Unexecuted instantiation: progress.c:out_of_core
Unexecuted instantiation: plaintext.c:out_of_core
Unexecuted instantiation: cipher-cfb.c:out_of_core
Unexecuted instantiation: cipher-aead.c:out_of_core
Unexecuted instantiation: ecdh.c:out_of_core
Unexecuted instantiation: keygen.c:out_of_core
Unexecuted instantiation: mdfilter.c:out_of_core
Unexecuted instantiation: textfilter.c:out_of_core
Unexecuted instantiation: revoke.c:out_of_core
Unexecuted instantiation: keybox-util.c:out_of_core
Unexecuted instantiation: keybox-init.c:out_of_core
Unexecuted instantiation: keybox-blob.c:out_of_core
Unexecuted instantiation: keybox-file.c:out_of_core
Unexecuted instantiation: keybox-search.c:out_of_core
Unexecuted instantiation: keybox-update.c:out_of_core
Unexecuted instantiation: keybox-openpgp.c:out_of_core
Unexecuted instantiation: kbx-client-util.c:out_of_core
Unexecuted instantiation: i18n.c:out_of_core
Unexecuted instantiation: mapstrings.c:out_of_core
Unexecuted instantiation: stringhelp.c:out_of_core
Unexecuted instantiation: strlist.c:out_of_core
Unexecuted instantiation: utf8conv.c:out_of_core
Unexecuted instantiation: dotlock.c:out_of_core
Unexecuted instantiation: mischelp.c:out_of_core
Unexecuted instantiation: status.c:out_of_core
Unexecuted instantiation: init.c:out_of_core
Unexecuted instantiation: sexputil.c:out_of_core
Unexecuted instantiation: sysutils.c:out_of_core
Unexecuted instantiation: homedir.c:out_of_core
Unexecuted instantiation: gettime.c:out_of_core
Unexecuted instantiation: yesno.c:out_of_core
Unexecuted instantiation: zb32.c:out_of_core
Unexecuted instantiation: convert.c:out_of_core
Unexecuted instantiation: percent.c:out_of_core
Unexecuted instantiation: mbox-util.c:out_of_core
Unexecuted instantiation: miscellaneous.c:out_of_core
Unexecuted instantiation: xasprintf.c:out_of_core
Unexecuted instantiation: membuf.c:out_of_core
Unexecuted instantiation: iobuf.c:out_of_core
Unexecuted instantiation: ttyio.c:out_of_core
Unexecuted instantiation: asshelp.c:out_of_core
Unexecuted instantiation: signal.c:out_of_core
Unexecuted instantiation: session-env.c:out_of_core
Unexecuted instantiation: userids.c:out_of_core
Unexecuted instantiation: openpgp-oid.c:out_of_core
Unexecuted instantiation: openpgp-s2k.c:out_of_core
Unexecuted instantiation: helpfile.c:out_of_core
Unexecuted instantiation: server-help.c:out_of_core
Unexecuted instantiation: recsel.c:out_of_core
Unexecuted instantiation: comopt.c:out_of_core
Unexecuted instantiation: compliance.c:out_of_core
Unexecuted instantiation: pkscreening.c:out_of_core
Unexecuted instantiation: kem.c:out_of_core
Unexecuted instantiation: exechelp-posix.c:out_of_core
Unexecuted instantiation: tlv.c:out_of_core
134
135
136
/*-- yesno.c --*/
137
int answer_is_yes (const char *s);
138
int answer_is_yes_no_default (const char *s, int def_answer);
139
int answer_is_yes_no_quit (const char *s);
140
int answer_is_okay_cancel (const char *s, int def_answer);
141
142
/*-- xreadline.c --*/
143
ssize_t read_line (FILE *fp,
144
                   char **addr_of_buffer, size_t *length_of_buffer,
145
                   size_t *max_length);
146
147
148
/*-- sexputil.c */
149
char *canon_sexp_to_string (const unsigned char *canon, size_t canonlen);
150
void log_printcanon (const char *text,
151
                     const unsigned char *sexp, size_t sexplen);
152
void log_printsexp (const char *text, gcry_sexp_t sexp);
153
154
gpg_error_t make_canon_sexp (gcry_sexp_t sexp,
155
                             unsigned char **r_buffer, size_t *r_buflen);
156
gpg_error_t make_canon_sexp_pad (gcry_sexp_t sexp, int secure,
157
                                 unsigned char **r_buffer, size_t *r_buflen);
158
gpg_error_t keygrip_from_canon_sexp (const unsigned char *key, size_t keylen,
159
                                     unsigned char *grip);
160
int cmp_simple_canon_sexp (const unsigned char *a, const unsigned char *b);
161
int cmp_canon_sexp (const unsigned char *a, size_t alen,
162
                    const unsigned char *b, size_t blen,
163
                    int (*tcmp)(void *ctx, int depth,
164
                                const unsigned char *aval, size_t avallen,
165
                                const unsigned char *bval, size_t bvallen),
166
                    void *tcmpctx);
167
unsigned char *make_simple_sexp_from_hexstr (const char *line,
168
                                             size_t *nscanned);
169
int hash_algo_from_sigval (const unsigned char *sigval);
170
unsigned char *make_canon_sexp_from_rsa_pk (const void *m, size_t mlen,
171
                                            const void *e, size_t elen,
172
                                            size_t *r_len);
173
gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata,
174
                                        size_t keydatalen,
175
                                        unsigned char const **r_n,
176
                                        size_t *r_nlen,
177
                                        unsigned char const **r_e,
178
                                        size_t *r_elen);
179
gpg_error_t get_ecc_q_from_canon_sexp (const unsigned char *keydata,
180
                                       size_t keydatalen,
181
                                       unsigned char const **r_q,
182
                                       size_t *r_qlen);
183
gpg_error_t uncompress_ecc_q_in_canon_sexp (const unsigned char *keydata,
184
                                            size_t keydatalen,
185
                                            unsigned char **r_newkeydata,
186
                                            size_t *r_newkeydatalen);
187
188
int get_pk_algo_from_key (gcry_sexp_t key);
189
int get_pk_algo_from_canon_sexp (const unsigned char *keydata,
190
                                 size_t keydatalen);
191
char *pubkey_algo_string (gcry_sexp_t s_pkey, enum gcry_pk_algos *r_algoid);
192
const char *pubkey_algo_to_string (int algo);
193
const char *hash_algo_to_string (int algo);
194
const char *cipher_mode_to_string (int mode);
195
const char *get_ecc_curve_from_key (gcry_sexp_t key);
196
197
/*-- convert.c --*/
198
int hex2bin (const char *string, void *buffer, size_t length);
199
int hexcolon2bin (const char *string, void *buffer, size_t length);
200
char *bin2hex (const void *buffer, size_t length, char *stringbuf);
201
char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
202
const char *hex2str (const char *hexstring,
203
                     char *buffer, size_t bufsize, size_t *buflen);
204
char *hex2str_alloc (const char *hexstring, size_t *r_count);
205
unsigned int hex2fixedbuf (const char *hexstr, void *buffer, size_t bufsize);
206
207
/*-- percent.c --*/
208
char *percent_plus_escape (const char *string);
209
char *percent_data_escape (int plus, const char *prefix,
210
                           const void *data, size_t datalen);
211
char *percent_plus_unescape (const char *string, int nulrepl);
212
char *percent_unescape (const char *string, int nulrepl);
213
214
size_t percent_plus_unescape_inplace (char *string, int nulrepl);
215
size_t percent_unescape_inplace (char *string, int nulrepl);
216
217
/*-- openpgp-oid.c --*/
218
gpg_error_t openpgp_oid_from_str (const char *string, gcry_mpi_t *r_mpi);
219
char *openpgp_oidbuf_to_str (const unsigned char *buf, size_t len);
220
char *openpgp_oid_to_str (gcry_mpi_t a);
221
int openpgp_oidbuf_is_ed25519 (const void *buf, size_t len);
222
int openpgp_oid_is_ed25519 (gcry_mpi_t a);
223
int openpgp_oidbuf_is_cv25519 (const void *buf, size_t len);
224
int openpgp_oid_is_cv25519 (gcry_mpi_t a);
225
int openpgp_oid_is_cv448 (gcry_mpi_t a);
226
int openpgp_oid_is_ed448 (gcry_mpi_t a);
227
const char *openpgp_curve_to_oid (const char *name,
228
                                  unsigned int *r_nbits, int *r_algo,
229
                                  int selector);
230
const char *openpgp_oid_to_curve (const char *oid, int mode);
231
const char *openpgp_oid_or_name_to_curve (const char *oidname, int canon);
232
const char *openpgp_enum_curves (int *idxp);
233
const char *openpgp_is_curve_supported (const char *name,
234
                                        int *r_algo, unsigned int *r_nbits);
235
const char *get_keyalgo_string (enum gcry_pk_algos algo,
236
                                unsigned int nbits, const char *curve);
237
238
239
/*-- homedir.c --*/
240
#ifdef HAVE_W32_SYSTEM
241
int gnupg_isatty (int fd);
242
extern int windows_semihosted_by_wine;
243
#else
244
0
#define gnupg_isatty(a)  isatty ((a))
245
#endif
246
247
const char *standard_homedir (void);
248
void gnupg_set_homedir (const char *newdir);
249
void gnupg_maybe_make_homedir (const char *fname, int quiet);
250
char *gnupg_myproc_self (void);
251
const char *gnupg_homedir (void);
252
int gnupg_default_homedir_p (void);
253
const char *gnupg_registry_dir (void);
254
const char *gnupg_daemon_rootdir (void);
255
const char *gnupg_socketdir (void);
256
const char *gnupg_sysconfdir (void);
257
const char *gnupg_bindir (void);
258
const char *gnupg_libexecdir (void);
259
const char *gnupg_libdir (void);
260
const char *gnupg_datadir (void);
261
const char *gnupg_localedir (void);
262
const char *gpg_agent_socket_name (void);
263
const char *dirmngr_socket_name (void);
264
const char *keyboxd_socket_name (void);
265
266
char *_gnupg_socketdir_internal (int skip_checks, unsigned *r_info);
267
268
/* All module names.  We also include gpg and gpgsm for the sake for
269
   gpgconf. */
270
0
#define GNUPG_MODULE_NAME_AGENT        1
271
0
#define GNUPG_MODULE_NAME_PINENTRY     2
272
0
#define GNUPG_MODULE_NAME_SCDAEMON     3
273
0
#define GNUPG_MODULE_NAME_DIRMNGR      4
274
0
#define GNUPG_MODULE_NAME_PROTECT_TOOL 5
275
0
#define GNUPG_MODULE_NAME_CHECK_PATTERN 6
276
0
#define GNUPG_MODULE_NAME_GPGSM         7
277
0
#define GNUPG_MODULE_NAME_GPG           8
278
0
#define GNUPG_MODULE_NAME_CONNECT_AGENT 9
279
0
#define GNUPG_MODULE_NAME_GPGCONF       10
280
0
#define GNUPG_MODULE_NAME_DIRMNGR_LDAP  11
281
0
#define GNUPG_MODULE_NAME_GPGV          12
282
0
#define GNUPG_MODULE_NAME_KEYBOXD       13
283
0
#define GNUPG_MODULE_NAME_TPM2DAEMON    14
284
0
#define GNUPG_MODULE_NAME_CARD          15
285
0
#define GNUPG_MODULE_NAME_GPGTAR        16
286
const char *gnupg_module_name (int which);
287
void gnupg_module_name_flush_some (void);
288
void gnupg_set_builddir (const char *newdir);
289
290
291
/* A list of constants to identify protocols.  This is used by tools
292
 * which need to distinguish between the different protocols
293
 * implemented by GnuPG.  May be used as bit flags.  */
294
#define GNUPG_PROTOCOL_OPENPGP    1   /* The one and only (gpg).      */
295
#define GNUPG_PROTOCOL_CMS        2   /* The core of S/MIME (gpgsm)   */
296
#define GNUPG_PROTOCOL_SSH_AGENT  4   /* Out ssh-agent implementation */
297
298
299
/*-- gpgrlhelp.c --*/
300
void gnupg_rl_initialize (void);
301
302
/*-- helpfile.c --*/
303
304
/* Bit flags for gnupg_get_template.  */
305
0
#define GET_TEMPLATE_CURRENT_LOCALE 1 /* Use only the current locale.       */
306
0
#define GET_TEMPLATE_SUBST_ENVVARS  2 /* Substitute environment variables.  */
307
0
#define GET_TEMPLATE_CRLF           4 /* Use CR+LF.                         */
308
309
char *gnupg_get_template (const char *domain, const char *key,
310
                          unsigned int flags, const char *override_locale);
311
char *gnupg_get_help_string (const char *key, int only_current_locale);
312
313
/*-- localename.c --*/
314
const char *gnupg_messages_locale_name (void);
315
316
/*-- kem.c --*/
317
gpg_error_t
318
gpgsm_ecc_kem_kdf (void *kek, size_t kek_len,
319
                   int hashalgo, const void *ecdh, size_t ecdh_len,
320
                   const unsigned char *wrap, size_t wrap_len,
321
                   const unsigned char *ukm, size_t ukm_len);
322
323
gpg_error_t gnupg_ecc_kem_kdf (void *kek, size_t kek_len, int is_pgp,
324
                               int hashalgo, const void *ecdh, size_t ecdh_len,
325
                               const unsigned char *kdf_params,
326
                               size_t kdf_params_len);
327
328
gpg_error_t gnupg_ecc_kem_simple_kdf (void *kek, size_t kek_len, int hashalgo,
329
                                      const void *ecdh, size_t ecdh_len,
330
                                      const void *ecc_ct, size_t ecc_ct_len,
331
                                      const void *ecc_pk, size_t ecc_pk_len);
332
333
gpg_error_t gnupg_kem_combiner  (void *kek, size_t kek_len,
334
                                 const void *ecc_ss, size_t ecc_ss_len,
335
                                 const void *ecc_ct, size_t ecc_ct_len,
336
                                 const void *mlkem_ss, size_t mlkem_ss_len,
337
                                 const void *mlkem_ct, size_t mlkem_ct_len,
338
                                 const void *fixedinfo, size_t fixedinfo_len);
339
340
/* ECC parameters for KEM encryption/decryption.  */
341
struct gnupg_ecc_params
342
{
343
  const char *curve;      /* Canonical name of the curve.  */
344
  size_t pubkey_len;      /* Pubkey length in the SEXP representation.  */
345
  size_t scalar_len;
346
  size_t point_len;
347
  int hash_algo;          /* Hash algo when it's used for composite KEM.  */
348
  int kem_algo;
349
  int scalar_reverse;     /* Byte-oder is reverse.  */
350
  int may_have_prefix;    /* Point representation may have prefix.  */
351
  int is_weierstrauss;    /* True if it is Weierstrass curve.  */
352
};
353
354
const struct gnupg_ecc_params *gnupg_get_ecc_params (const char *curve);
355
356
/* Maximum buffer sizes required for ECC KEM.  */
357
#define ECC_SCALAR_LEN_MAX 66
358
#define ECC_POINT_LEN_MAX (1+2*ECC_SCALAR_LEN_MAX)
359
#define ECC_HASH_LEN_MAX 64
360
361
362
/*-- miscellaneous.c --*/
363
364
/* This function is called at startup to tell libgcrypt to use our own
365
   logging subsystem. */
366
void setup_libgcrypt_logging (void);
367
368
/* Print an out of core message and die.  */
369
void xoutofcore (void);
370
371
/* Wrapper around gpgrt_reallocarray.  Uses the gpgrt alloc function
372
 * which redirects to the Libgcrypt versions via
373
 * init_common_subsystems.  Thus this can be used interchangeable with
374
 * the other alloc functions. */
375
void *xreallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size);
376
377
/* Same as estream_asprintf but die on memory failure.  */
378
char *xasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
379
/* This is now an alias to estream_asprintf.  */
380
char *xtryasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
381
382
/* Replacement for gcry_cipher_algo_name.  */
383
const char *gnupg_cipher_algo_name (int algo);
384
385
void obsolete_option (const char *configname, unsigned int configlineno,
386
                      const char *name);
387
388
const char *print_fname_stdout (const char *s);
389
const char *print_fname_stdin (const char *s);
390
void print_utf8_buffer3 (estream_t fp, const void *p, size_t n,
391
                         const char *delim);
392
void print_utf8_buffer2 (estream_t fp, const void *p, size_t n, int delim);
393
void print_utf8_buffer (estream_t fp, const void *p, size_t n);
394
void print_utf8_string (estream_t stream, const char *p);
395
void print_hexstring (FILE *fp, const void *buffer, size_t length,
396
                      int reserved);
397
char *try_make_printable_string (const void *p, size_t n, int delim);
398
char *make_printable_string (const void *p, size_t n, int delim);
399
char *decode_c_string (const char *src);
400
401
int match_multistr (const char *multistr,const char *match);
402
403
int gnupg_compare_version (const char *a, const char *b);
404
405
struct debug_flags_s
406
{
407
  unsigned int flag;
408
  const char *name;
409
};
410
int parse_debug_flag (const char *string, unsigned int *debugvar,
411
                      const struct debug_flags_s *flags);
412
413
struct compatibility_flags_s
414
{
415
  unsigned int flag;
416
  const char *name;
417
  const char *desc;
418
};
419
int parse_compatibility_flags (const char *string, unsigned int *flagvar,
420
                               const struct compatibility_flags_s *flags);
421
422
gpg_error_t b64decode (const char *string, const char *title,
423
                       void **r_buffer, size_t *r_buflen);
424
425
426
427
/*-- Simple replacement functions. */
428
429
/* We use the gnupg_ttyname macro to be safe not to run into conflicts
430
   with an existing but broken ttyname.  */
431
#if !defined(HAVE_TTYNAME) || defined(HAVE_BROKEN_TTYNAME)
432
# define gnupg_ttyname(n) _gnupg_ttyname ((n))
433
/* Systems without ttyname (W32) will merely return NULL. */
434
static inline char *
435
_gnupg_ttyname (int fd)
436
{
437
  (void)fd;
438
  return NULL;
439
}
440
#else /*HAVE_TTYNAME*/
441
0
# define gnupg_ttyname(n) ttyname ((n))
442
#endif /*HAVE_TTYNAME */
443
444
445
/*-- Macros to replace ctype ones to avoid locale problems. --*/
446
0
#define spacep(p)   (*(p) == ' ' || *(p) == '\t')
447
0
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
448
0
#define alphap(p)   ((*(p) >= 'A' && *(p) <= 'Z')       \
449
0
                     || (*(p) >= 'a' && *(p) <= 'z'))
450
0
#define alnump(p)   (alphap (p) || digitp (p))
451
0
#define hexdigitp(a) (digitp (a)                     \
452
0
                      || (*(a) >= 'A' && *(a) <= 'F')  \
453
0
                      || (*(a) >= 'a' && *(a) <= 'f'))
454
  /* Note this isn't identical to a C locale isspace() without \f and
455
     \v, but works for the purposes used here. */
456
0
#define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
457
458
/* The atoi macros assume that the buffer has only valid digits. */
459
0
#define atoi_1(p)   (*(p) - '0' )
460
0
#define atoi_2(p)   ((atoi_1(p) * 10) + atoi_1((p)+1))
461
0
#define atoi_4(p)   ((atoi_2(p) * 100) + atoi_2((p)+2))
462
0
#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
463
0
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
464
0
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
465
#define xtoi_4(p)   ((xtoi_2(p) * 256) + xtoi_2((p)+2))
466
467
#endif /*GNUPG_COMMON_UTIL_H*/