LCOV - code coverage report
Current view: top level - common - util.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 2 0.0 %
Date: 2016-09-12 12:29:17 Functions: 0 1 0.0 %

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

Generated by: LCOV version 1.11