Coverage Report

Created: 2026-01-10 07:05

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
94.9k
#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
23.7M
#define xtrymalloc(a)    gcry_malloc ((a))
99
0
#define xtrymalloc_secure(a)  gcry_malloc_secure ((a))
100
961k
#define xtrycalloc(a,b)  gcry_calloc ((a),(b))
101
0
#define xtrycalloc_secure(a,b)  gcry_calloc_secure ((a),(b))
102
34.5k
#define xtryrealloc(a,b) gcry_realloc ((a),(b))
103
#define xtryreallocarray(a,b,c,d) gpgrt_reallocarray ((a),(b),(c),(d))
104
34
#define xtrystrdup(a)    gcry_strdup ((a))
105
167M
#define xfree(a)         gcry_free ((a))
106
#define xfree_fnc        gcry_free
107
108
47.3M
#define xmalloc(a)       gcry_xmalloc ((a))
109
0
#define xmalloc_secure(a)  gcry_xmalloc_secure ((a))
110
6.71M
#define xcalloc(a,b)     gcry_xcalloc ((a),(b))
111
#define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
112
24.9k
#define xrealloc(a,b)    gcry_xrealloc ((a),(b))
113
20.5k
#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
20.9M
#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_import.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
const char *gnupg_homedir (void);
251
int gnupg_default_homedir_p (void);
252
const char *gnupg_registry_dir (void);
253
const char *gnupg_daemon_rootdir (void);
254
const char *gnupg_socketdir (void);
255
const char *gnupg_sysconfdir (void);
256
const char *gnupg_bindir (void);
257
const char *gnupg_libexecdir (void);
258
const char *gnupg_libdir (void);
259
const char *gnupg_datadir (void);
260
const char *gnupg_localedir (void);
261
const char *gpg_agent_socket_name (void);
262
const char *dirmngr_socket_name (void);
263
const char *keyboxd_socket_name (void);
264
265
char *_gnupg_socketdir_internal (int skip_checks, unsigned *r_info);
266
267
/* All module names.  We also include gpg and gpgsm for the sake for
268
   gpgconf. */
269
6.47k
#define GNUPG_MODULE_NAME_AGENT        1
270
0
#define GNUPG_MODULE_NAME_PINENTRY     2
271
0
#define GNUPG_MODULE_NAME_SCDAEMON     3
272
0
#define GNUPG_MODULE_NAME_DIRMNGR      4
273
0
#define GNUPG_MODULE_NAME_PROTECT_TOOL 5
274
0
#define GNUPG_MODULE_NAME_CHECK_PATTERN 6
275
0
#define GNUPG_MODULE_NAME_GPGSM         7
276
0
#define GNUPG_MODULE_NAME_GPG           8
277
0
#define GNUPG_MODULE_NAME_CONNECT_AGENT 9
278
0
#define GNUPG_MODULE_NAME_GPGCONF       10
279
0
#define GNUPG_MODULE_NAME_DIRMNGR_LDAP  11
280
0
#define GNUPG_MODULE_NAME_GPGV          12
281
0
#define GNUPG_MODULE_NAME_KEYBOXD       13
282
0
#define GNUPG_MODULE_NAME_TPM2DAEMON    14
283
0
#define GNUPG_MODULE_NAME_CARD          15
284
0
#define GNUPG_MODULE_NAME_GPGTAR        16
285
const char *gnupg_module_name (int which);
286
void gnupg_module_name_flush_some (void);
287
void gnupg_set_builddir (const char *newdir);
288
289
290
/* A list of constants to identify protocols.  This is used by tools
291
 * which need to distinguish between the different protocols
292
 * implemented by GnuPG.  May be used as bit flags.  */
293
#define GNUPG_PROTOCOL_OPENPGP    1   /* The one and only (gpg).      */
294
#define GNUPG_PROTOCOL_CMS        2   /* The core of S/MIME (gpgsm)   */
295
#define GNUPG_PROTOCOL_SSH_AGENT  4   /* Out ssh-agent implementation */
296
297
298
/*-- gpgrlhelp.c --*/
299
void gnupg_rl_initialize (void);
300
301
/*-- helpfile.c --*/
302
303
/* Bit flags for gnupg_get_template.  */
304
0
#define GET_TEMPLATE_CURRENT_LOCALE 1 /* Use only the current locale.       */
305
0
#define GET_TEMPLATE_SUBST_ENVVARS  2 /* Substitute environment variables.  */
306
0
#define GET_TEMPLATE_CRLF           4 /* Use CR+LF.                         */
307
308
char *gnupg_get_template (const char *domain, const char *key,
309
                          unsigned int flags, const char *override_locale);
310
char *gnupg_get_help_string (const char *key, int only_current_locale);
311
312
/*-- localename.c --*/
313
const char *gnupg_messages_locale_name (void);
314
315
/*-- kem.c --*/
316
gpg_error_t
317
gpgsm_ecc_kem_kdf (void *kek, size_t kek_len,
318
                   int hashalgo, const void *ecdh, size_t ecdh_len,
319
                   const unsigned char *wrap, size_t wrap_len,
320
                   const unsigned char *ukm, size_t ukm_len);
321
322
gpg_error_t gnupg_ecc_kem_kdf (void *kek, size_t kek_len, int is_pgp,
323
                               int hashalgo, const void *ecdh, size_t ecdh_len,
324
                               const unsigned char *kdf_params,
325
                               size_t kdf_params_len);
326
327
gpg_error_t gnupg_ecc_kem_simple_kdf (void *kek, size_t kek_len, int hashalgo,
328
                                      const void *ecdh, size_t ecdh_len,
329
                                      const void *ecc_ct, size_t ecc_ct_len,
330
                                      const void *ecc_pk, size_t ecc_pk_len);
331
332
gpg_error_t gnupg_kem_combiner  (void *kek, size_t kek_len,
333
                                 const void *ecc_ss, size_t ecc_ss_len,
334
                                 const void *ecc_ct, size_t ecc_ct_len,
335
                                 const void *mlkem_ss, size_t mlkem_ss_len,
336
                                 const void *mlkem_ct, size_t mlkem_ct_len,
337
                                 const void *fixedinfo, size_t fixedinfo_len);
338
339
/* ECC parameters for KEM encryption/decryption.  */
340
struct gnupg_ecc_params
341
{
342
  const char *curve;      /* Canonical name of the curve.  */
343
  size_t pubkey_len;      /* Pubkey length in the SEXP representation.  */
344
  size_t scalar_len;
345
  size_t point_len;
346
  int hash_algo;          /* Hash algo when it's used for composite KEM.  */
347
  int kem_algo;
348
  int scalar_reverse;     /* Byte-oder is reverse.  */
349
  int may_have_prefix;    /* Point representation may have prefix.  */
350
  int is_weierstrauss;    /* True if it is Weierstrass curve.  */
351
};
352
353
const struct gnupg_ecc_params *gnupg_get_ecc_params (const char *curve);
354
355
/* Maximum buffer sizes required for ECC KEM.  */
356
#define ECC_SCALAR_LEN_MAX 66
357
#define ECC_POINT_LEN_MAX (1+2*ECC_SCALAR_LEN_MAX)
358
#define ECC_HASH_LEN_MAX 64
359
360
361
/*-- miscellaneous.c --*/
362
363
/* This function is called at startup to tell libgcrypt to use our own
364
   logging subsystem. */
365
void setup_libgcrypt_logging (void);
366
367
/* Print an out of core message and die.  */
368
void xoutofcore (void);
369
370
/* Wrapper around gpgrt_reallocarray.  Uses the gpgrt alloc function
371
 * which redirects to the Libgcrypt versions via
372
 * init_common_subsystems.  Thus this can be used interchangeable with
373
 * the other alloc functions. */
374
void *xreallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size);
375
376
/* Same as estream_asprintf but die on memory failure.  */
377
char *xasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
378
/* This is now an alias to estream_asprintf.  */
379
char *xtryasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
380
381
/* Replacement for gcry_cipher_algo_name.  */
382
const char *gnupg_cipher_algo_name (int algo);
383
384
void obsolete_option (const char *configname, unsigned int configlineno,
385
                      const char *name);
386
387
const char *print_fname_stdout (const char *s);
388
const char *print_fname_stdin (const char *s);
389
void print_utf8_buffer3 (estream_t fp, const void *p, size_t n,
390
                         const char *delim);
391
void print_utf8_buffer2 (estream_t fp, const void *p, size_t n, int delim);
392
void print_utf8_buffer (estream_t fp, const void *p, size_t n);
393
void print_utf8_string (estream_t stream, const char *p);
394
void print_hexstring (FILE *fp, const void *buffer, size_t length,
395
                      int reserved);
396
char *try_make_printable_string (const void *p, size_t n, int delim);
397
char *make_printable_string (const void *p, size_t n, int delim);
398
char *decode_c_string (const char *src);
399
400
int match_multistr (const char *multistr,const char *match);
401
402
int gnupg_compare_version (const char *a, const char *b);
403
404
struct debug_flags_s
405
{
406
  unsigned int flag;
407
  const char *name;
408
};
409
int parse_debug_flag (const char *string, unsigned int *debugvar,
410
                      const struct debug_flags_s *flags);
411
412
struct compatibility_flags_s
413
{
414
  unsigned int flag;
415
  const char *name;
416
  const char *desc;
417
};
418
int parse_compatibility_flags (const char *string, unsigned int *flagvar,
419
                               const struct compatibility_flags_s *flags);
420
421
gpg_error_t b64decode (const char *string, const char *title,
422
                       void **r_buffer, size_t *r_buflen);
423
424
425
426
/*-- Simple replacement functions. */
427
428
/* We use the gnupg_ttyname macro to be safe not to run into conflicts
429
   with an existing but broken ttyname.  */
430
#if !defined(HAVE_TTYNAME) || defined(HAVE_BROKEN_TTYNAME)
431
# define gnupg_ttyname(n) _gnupg_ttyname ((n))
432
/* Systems without ttyname (W32) will merely return NULL. */
433
static inline char *
434
_gnupg_ttyname (int fd)
435
{
436
  (void)fd;
437
  return NULL;
438
}
439
#else /*HAVE_TTYNAME*/
440
0
# define gnupg_ttyname(n) ttyname ((n))
441
#endif /*HAVE_TTYNAME */
442
443
444
/*-- Macros to replace ctype ones to avoid locale problems. --*/
445
0
#define spacep(p)   (*(p) == ' ' || *(p) == '\t')
446
0
#define digitp(p)   (*(p) >= '0' && *(p) <= '9')
447
0
#define alphap(p)   ((*(p) >= 'A' && *(p) <= 'Z')       \
448
0
                     || (*(p) >= 'a' && *(p) <= 'z'))
449
0
#define alnump(p)   (alphap (p) || digitp (p))
450
0
#define hexdigitp(a) (digitp (a)                     \
451
0
                      || (*(a) >= 'A' && *(a) <= 'F')  \
452
0
                      || (*(a) >= 'a' && *(a) <= 'f'))
453
  /* Note this isn't identical to a C locale isspace() without \f and
454
     \v, but works for the purposes used here. */
455
0
#define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
456
457
/* The atoi macros assume that the buffer has only valid digits. */
458
0
#define atoi_1(p)   (*(p) - '0' )
459
0
#define atoi_2(p)   ((atoi_1(p) * 10) + atoi_1((p)+1))
460
0
#define atoi_4(p)   ((atoi_2(p) * 100) + atoi_2((p)+2))
461
0
#define xtoi_1(p)   (*(p) <= '9'? (*(p)- '0'): \
462
0
                     *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
463
0
#define xtoi_2(p)   ((xtoi_1(p) * 16) + xtoi_1((p)+1))
464
#define xtoi_4(p)   ((xtoi_2(p) * 256) + xtoi_2((p)+2))
465
466
#endif /*GNUPG_COMMON_UTIL_H*/