raw
mpi-genesis~            1 /* util.h
mpi-genesis~ 2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
mpi-genesis~ 3 * 2006 Free Software Foundation, Inc.
mpi-genesis~ 4 *
mpi-genesis~ 5 * This file is part of GNUPG.
mpi-genesis~ 6 *
mpi-genesis~ 7 * GNUPG is free software; you can redistribute it and/or modify
mpi-genesis~ 8 * it under the terms of the GNU General Public License as published by
mpi-genesis~ 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis~ 10 * (at your option) any later version.
mpi-genesis~ 11 *
mpi-genesis~ 12 * GNUPG is distributed in the hope that it will be useful,
mpi-genesis~ 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis~ 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis~ 15 * GNU General Public License for more details.
mpi-genesis~ 16 *
mpi-genesis~ 17 * You should have received a copy of the GNU General Public License
mpi-genesis~ 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis~ 19 */
mpi-genesis~ 20 #ifndef G10_UTIL_H
mpi-genesis~ 21 #define G10_UTIL_H
mpi-genesis~ 22
mpi-genesis~ 23 #if defined (_WIN32) || defined (__CYGWIN32__)
mpi-genesis~ 24 #include <stdarg.h>
mpi-genesis~ 25 #endif
mpi-genesis~ 26
mpi-genesis~ 27 #include "types.h"
mpi-genesis~ 28 #include "errors.h"
mpi-genesis~ 29 #include "types.h"
mpi-genesis~ 30 #include "mpi.h"
mpi-genesis~ 31 #include "compat.h"
mpi-genesis~ 32
mpi-genesis~ 33 typedef struct {
mpi-genesis~ 34 int *argc; /* pointer to argc (value subject to change) */
mpi-genesis~ 35 char ***argv; /* pointer to argv (value subject to change) */
mpi-genesis~ 36 unsigned flags; /* Global flags (DO NOT CHANGE) */
mpi-genesis~ 37 int err; /* print error about last option */
mpi-genesis~ 38 /* 1 = warning, 2 = abort */
mpi-genesis~ 39 int r_opt; /* return option */
mpi-genesis~ 40 int r_type; /* type of return value (0 = no argument found)*/
mpi-genesis~ 41 union {
mpi-genesis~ 42 int ret_int;
mpi-genesis~ 43 long ret_long;
mpi-genesis~ 44 ulong ret_ulong;
mpi-genesis~ 45 char *ret_str;
mpi-genesis~ 46 } r; /* Return values */
mpi-genesis~ 47 struct {
mpi-genesis~ 48 int idx;
mpi-genesis~ 49 int inarg;
mpi-genesis~ 50 int stopped;
mpi-genesis~ 51 const char *last;
mpi-genesis~ 52 void *aliases;
mpi-genesis~ 53 const void *cur_alias;
mpi-genesis~ 54 } internal; /* DO NOT CHANGE */
mpi-genesis~ 55 } ARGPARSE_ARGS;
mpi-genesis~ 56
mpi-genesis~ 57 typedef struct {
mpi-genesis~ 58 int short_opt;
mpi-genesis~ 59 const char *long_opt;
mpi-genesis~ 60 unsigned flags;
mpi-genesis~ 61 const char *description; /* optional option description */
mpi-genesis~ 62 } ARGPARSE_OPTS;
mpi-genesis~ 63
mpi-genesis~ 64 /*-- logger.c --*/
mpi-genesis~ 65 void log_set_logfile( const char *name, int fd );
mpi-genesis~ 66 FILE *log_stream(void);
mpi-genesis~ 67 void g10_log_print_prefix(const char *text);
mpi-genesis~ 68 void log_set_name( const char *name );
mpi-genesis~ 69 const char *log_get_name(void);
mpi-genesis~ 70 void log_set_pid( int pid );
mpi-genesis~ 71 int log_get_errorcount( int clear );
mpi-genesis~ 72 void log_inc_errorcount(void);
mpi-genesis~ 73 int log_set_strict(int val);
mpi-genesis~ 74 void g10_log_hexdump( const char *text, const char *buf, size_t len );
mpi-genesis~ 75
mpi-genesis~ 76 #if defined (__riscos__) \
mpi-genesis~ 77 || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
mpi-genesis~ 78 void g10_log_bug( const char *fmt, ... )
mpi-genesis~ 79 __attribute__ ((noreturn, format (printf,1,2)));
mpi-genesis~ 80 void g10_log_bug0( const char *, int, const char * ) __attribute__ ((noreturn));
mpi-genesis~ 81 void g10_log_fatal( const char *fmt, ... )
mpi-genesis~ 82 __attribute__ ((noreturn, format (printf,1,2)));
mpi-genesis~ 83 void g10_log_error( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
mpi-genesis~ 84 void g10_log_info( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
mpi-genesis~ 85 void g10_log_warning( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
mpi-genesis~ 86 void g10_log_debug( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
mpi-genesis~ 87 #ifndef __riscos__
mpi-genesis~ 88 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __FUNCTION__ )
mpi-genesis~ 89 #else
mpi-genesis~ 90 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __func__ )
mpi-genesis~ 91 #endif
mpi-genesis~ 92 #else
mpi-genesis~ 93 void g10_log_bug( const char *fmt, ... );
mpi-genesis~ 94 void g10_log_bug0( const char *, int );
mpi-genesis~ 95 void g10_log_fatal( const char *fmt, ... );
mpi-genesis~ 96 void g10_log_error( const char *fmt, ... );
mpi-genesis~ 97 void g10_log_info( const char *fmt, ... );
mpi-genesis~ 98 void g10_log_warning( const char *fmt, ... );
mpi-genesis~ 99 void g10_log_debug( const char *fmt, ... );
mpi-genesis~ 100 #define BUG() g10_log_bug0( __FILE__ , __LINE__ )
mpi-genesis~ 101 #endif
mpi-genesis~ 102
mpi-genesis~ 103 #define log_hexdump g10_log_hexdump
mpi-genesis~ 104 #define log_bug g10_log_bug
mpi-genesis~ 105 #define log_bug0 g10_log_bug0
mpi-genesis~ 106 #define log_fatal g10_log_fatal
mpi-genesis~ 107 #define log_error g10_log_error
mpi-genesis~ 108 #define log_info g10_log_info
mpi-genesis~ 109 #define log_warning g10_log_warning
mpi-genesis~ 110 #define log_debug g10_log_debug
mpi-genesis~ 111
mpi-genesis~ 112
mpi-genesis~ 113 /*-- errors.c --*/
mpi-genesis~ 114 const char * g10_errstr( int no );
mpi-genesis~ 115
mpi-genesis~ 116 /*-- argparse.c --*/
mpi-genesis~ 117 int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
mpi-genesis~ 118 int optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
mpi-genesis~ 119 ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
mpi-genesis~ 120 void usage( int level );
mpi-genesis~ 121 const char *default_strusage( int level );
mpi-genesis~ 122
mpi-genesis~ 123
mpi-genesis~ 124 /*-- (main program) --*/
mpi-genesis~ 125 const char *strusage( int level );
mpi-genesis~ 126
mpi-genesis~ 127
mpi-genesis~ 128 /*-- dotlock.c --*/
mpi-genesis~ 129 struct dotlock_handle;
mpi-genesis~ 130 typedef struct dotlock_handle *DOTLOCK;
mpi-genesis~ 131
mpi-genesis~ 132 void disable_dotlock(void);
mpi-genesis~ 133 DOTLOCK create_dotlock( const char *file_to_lock );
mpi-genesis~ 134 void destroy_dotlock ( DOTLOCK h );
mpi-genesis~ 135 int make_dotlock( DOTLOCK h, long timeout );
mpi-genesis~ 136 int release_dotlock( DOTLOCK h );
mpi-genesis~ 137 void remove_lockfiles (void);
mpi-genesis~ 138
mpi-genesis~ 139 /*-- fileutil.c --*/
mpi-genesis~ 140 char * make_basename(const char *filepath, const char *inputpath);
mpi-genesis~ 141 char * make_dirname(const char *filepath);
mpi-genesis~ 142 char *make_filename( const char *first_part, ... );
mpi-genesis~ 143 int compare_filenames( const char *a, const char *b );
mpi-genesis~ 144 int same_file_p (const char *name1, const char *name2);
mpi-genesis~ 145 const char *print_fname_stdin( const char *s );
mpi-genesis~ 146 const char *print_fname_stdout( const char *s );
mpi-genesis~ 147 int is_file_compressed(const char *s, int *r_status);
mpi-genesis~ 148
mpi-genesis~ 149 /*-- miscutil.c --*/
mpi-genesis~ 150 u32 make_timestamp(void);
mpi-genesis~ 151 u32 scan_isodatestr( const char *string );
mpi-genesis~ 152 u32 isotime2seconds (const char *string);
mpi-genesis~ 153 const char *strtimevalue( u32 stamp );
mpi-genesis~ 154 const char *strtimestamp( u32 stamp ); /* GMT */
mpi-genesis~ 155 const char *isotimestamp( u32 stamp ); /* GMT with hh:mm:ss */
mpi-genesis~ 156 const char *asctimestamp( u32 stamp ); /* localized */
mpi-genesis~ 157 void print_string( FILE *fp, const byte *p, size_t n, int delim );
mpi-genesis~ 158 void print_string2( FILE *fp, const byte *p, size_t n, int delim, int delim2 );
mpi-genesis~ 159 void print_utf8_string( FILE *fp, const byte *p, size_t n );
mpi-genesis~ 160 void print_utf8_string2( FILE *fp, const byte *p, size_t n, int delim);
mpi-genesis~ 161 char *make_printable_string( const byte *p, size_t n, int delim );
mpi-genesis~ 162 int answer_is_yes_no_default( const char *s, int def_answer );
mpi-genesis~ 163 int answer_is_yes( const char *s );
mpi-genesis~ 164 int answer_is_yes_no_quit( const char *s );
mpi-genesis~ 165 int answer_is_okay_cancel (const char *s, int def_answer);
mpi-genesis~ 166 int match_multistr(const char *multistr,const char *match);
mpi-genesis~ 167
mpi-genesis~ 168 /*-- strgutil.c --*/
mpi-genesis~ 169 void free_strlist( STRLIST sl );
mpi-genesis~ 170 #define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0)
mpi-genesis~ 171 STRLIST add_to_strlist( STRLIST *list, const char *string );
mpi-genesis~ 172 STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
mpi-genesis~ 173 STRLIST append_to_strlist( STRLIST *list, const char *string );
mpi-genesis~ 174 STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
mpi-genesis~ 175 STRLIST strlist_prev( STRLIST head, STRLIST node );
mpi-genesis~ 176 STRLIST strlist_last( STRLIST node );
mpi-genesis~ 177 char *pop_strlist( STRLIST *list );
mpi-genesis~ 178 const char *memistr( const char *buf, size_t buflen, const char *sub );
mpi-genesis~ 179 const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
mpi-genesis~ 180 char *mem2str( char *, const void *, size_t);
mpi-genesis~ 181 char *trim_spaces( char *string );
mpi-genesis~ 182 unsigned int trim_trailing_chars( byte *line, unsigned int len,
mpi-genesis~ 183 const char *trimchars);
mpi-genesis~ 184 unsigned int trim_trailing_ws( byte *line, unsigned len );
mpi-genesis~ 185 unsigned int check_trailing_chars( const byte *line, unsigned int len,
mpi-genesis~ 186 const char *trimchars );
mpi-genesis~ 187 unsigned int check_trailing_ws( const byte *line, unsigned int len );
mpi-genesis~ 188 int string_count_chr( const char *string, int c );
mpi-genesis~ 189 int set_native_charset( const char *newset );
mpi-genesis~ 190 const char* get_native_charset(void);
mpi-genesis~ 191 char *native_to_utf8( const char *string );
mpi-genesis~ 192 char *utf8_to_native( const char *string, size_t length, int delim);
mpi-genesis~ 193 char *string_to_utf8 (const char *string);
mpi-genesis~ 194
mpi-genesis~ 195 int ascii_isupper (int c);
mpi-genesis~ 196 int ascii_islower (int c);
mpi-genesis~ 197 int ascii_memcasecmp( const char *a, const char *b, size_t n);
mpi-genesis~ 198
mpi-genesis~ 199 #ifndef HAVE_STPCPY
mpi-genesis~ 200 char *stpcpy(char *a,const char *b);
mpi-genesis~ 201 #endif
mpi-genesis~ 202 #ifndef HAVE_STRLWR
mpi-genesis~ 203 char *strlwr(char *a);
mpi-genesis~ 204 #endif
mpi-genesis~ 205 #ifndef HAVE_STRCASECMP
mpi-genesis~ 206 int strcasecmp( const char *, const char *b);
mpi-genesis~ 207 #endif
mpi-genesis~ 208 #ifndef HAVE_STRNCASECMP
mpi-genesis~ 209 int strncasecmp (const char *, const char *b, size_t n);
mpi-genesis~ 210 #endif
mpi-genesis~ 211 #ifndef HAVE_STRTOUL
mpi-genesis~ 212 #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
mpi-genesis~ 213 #endif
mpi-genesis~ 214 #ifndef HAVE_MEMMOVE
mpi-genesis~ 215 #define memmove(d, s, n) bcopy((s), (d), (n))
mpi-genesis~ 216 #endif
mpi-genesis~ 217
mpi-genesis~ 218 /*-- membuf.c --*/
mpi-genesis~ 219 /* The definition of the structure is private, we only need it here,
mpi-genesis~ 220 so it can be allocated on the stack. */
mpi-genesis~ 221 struct private_membuf_s {
mpi-genesis~ 222 size_t len;
mpi-genesis~ 223 size_t size;
mpi-genesis~ 224 char *buf;
mpi-genesis~ 225 int out_of_core;
mpi-genesis~ 226 };
mpi-genesis~ 227
mpi-genesis~ 228 typedef struct private_membuf_s membuf_t;
mpi-genesis~ 229
mpi-genesis~ 230 void init_membuf (membuf_t *mb, int initiallen);
mpi-genesis~ 231 void put_membuf (membuf_t *mb, const void *buf, size_t len);
mpi-genesis~ 232 void *get_membuf (membuf_t *mb, size_t *len);
mpi-genesis~ 233
mpi-genesis~ 234
mpi-genesis~ 235
mpi-genesis~ 236 #if defined (_WIN32)
mpi-genesis~ 237 /*-- w32reg.c --*/
mpi-genesis~ 238 char *read_w32_registry_string( const char *root,
mpi-genesis~ 239 const char *dir, const char *name );
mpi-genesis~ 240 int write_w32_registry_string(const char *root, const char *dir,
mpi-genesis~ 241 const char *name, const char *value);
mpi-genesis~ 242
mpi-genesis~ 243 #endif /*_WIN32*/
mpi-genesis~ 244
mpi-genesis~ 245 /*-- strgutil.c --*/
mpi-genesis~ 246 char *xasprintf (const char *fmt, ...);
mpi-genesis~ 247 char *xtryasprintf (const char *fmt, ...);
mpi-genesis~ 248
mpi-genesis~ 249
mpi-genesis~ 250 /*-- pka.c --*/
mpi-genesis~ 251 char *get_pka_info (const char *address, unsigned char *fpr);
mpi-genesis~ 252
mpi-genesis~ 253 /*-- cert.c --*/
mpi-genesis~ 254 int get_cert(const char *name,size_t max_size,IOBUF *iobuf,
mpi-genesis~ 255 unsigned char **fpr,size_t *fpr_len,char **url);
mpi-genesis~ 256
mpi-genesis~ 257 /*-- convert.c --*/
mpi-genesis~ 258 int hex2bin (const char *string, void *buffer, size_t length);
mpi-genesis~ 259 int hexcolon2bin (const char *string, void *buffer, size_t length);
mpi-genesis~ 260 char *bin2hex (const void *buffer, size_t length, char *stringbuf);
mpi-genesis~ 261 char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
mpi-genesis~ 262 const char *hex2str (const char *hexstring,
mpi-genesis~ 263 char *buffer, size_t bufsize, size_t *buflen);
mpi-genesis~ 264 char *hex2str_alloc (const char *hexstring, size_t *r_count);
mpi-genesis~ 265
mpi-genesis~ 266
mpi-genesis~ 267 /**** other missing stuff ****/
mpi-genesis~ 268 #ifndef HAVE_ATEXIT /* For SunOS */
mpi-genesis~ 269 #define atexit(a) (on_exit((a),0))
mpi-genesis~ 270 #endif
mpi-genesis~ 271
mpi-genesis~ 272 #ifndef HAVE_RAISE
mpi-genesis~ 273 #define raise(a) kill(getpid(), (a))
mpi-genesis~ 274 #endif
mpi-genesis~ 275
mpi-genesis~ 276 /*-- Replacement functions from funcname.c --*/
mpi-genesis~ 277
mpi-genesis~ 278
mpi-genesis~ 279
mpi-genesis~ 280 /******** some macros ************/
mpi-genesis~ 281 #ifndef STR
mpi-genesis~ 282 #define STR(v) #v
mpi-genesis~ 283 #endif
mpi-genesis~ 284 #define STR2(v) STR(v)
mpi-genesis~ 285 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
mpi-genesis~ 286 #define DIMof(type,member) DIM(((type *)0)->member)
mpi-genesis~ 287
mpi-genesis~ 288 #define wipememory2(_ptr,_set,_len) do { volatile char *_vptr=(volatile char *)(_ptr); size_t _vlen=(_len); while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } } while(0)
mpi-genesis~ 289 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
mpi-genesis~ 290
mpi-genesis~ 291 /*-- macros to replace ctype ones and avoid locale problems --*/
mpi-genesis~ 292 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
mpi-genesis~ 293 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
mpi-genesis~ 294 #define hexdigitp(a) (digitp (a) \
mpi-genesis~ 295 || (*(a) >= 'A' && *(a) <= 'F') \
mpi-genesis~ 296 || (*(a) >= 'a' && *(a) <= 'f'))
mpi-genesis~ 297 /* the atoi macros assume that the buffer has only valid digits */
mpi-genesis~ 298 #define atoi_1(p) (*(p) - '0' )
mpi-genesis~ 299 #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
mpi-genesis~ 300 #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
mpi-genesis~ 301 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
mpi-genesis~ 302 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
mpi-genesis~ 303 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
mpi-genesis~ 304
mpi-genesis~ 305 /******* RISC OS stuff ***********/
mpi-genesis~ 306 #ifdef __riscos__
mpi-genesis~ 307 int riscos_load_module(const char *name, const char * const path[], int fatal);
mpi-genesis~ 308 int riscos_get_filetype_from_string(const char *string, int len);
mpi-genesis~ 309 int riscos_get_filetype(const char *filename);
mpi-genesis~ 310 void riscos_set_filetype_by_number(const char *filename, int type);
mpi-genesis~ 311 void riscos_set_filetype_by_mimetype(const char *filename, const char *mimetype);
mpi-genesis~ 312 pid_t riscos_getpid(void);
mpi-genesis~ 313 int riscos_kill(pid_t pid, int sig);
mpi-genesis~ 314 int riscos_access(const char *path, int amode);
mpi-genesis~ 315 int riscos_getchar(void);
mpi-genesis~ 316 char *riscos_make_basename(const char *filepath, const char *inputpath);
mpi-genesis~ 317 int riscos_check_regexp(const char *exp, const char *string, int debug);
mpi-genesis~ 318 int riscos_fdopenfile(const char *filename, const int allow_write);
mpi-genesis~ 319 void riscos_close_fds(void);
mpi-genesis~ 320 int riscos_renamefile(const char *old, const char *new);
mpi-genesis~ 321 char *riscos_gstrans(const char *old);
mpi-genesis~ 322 void riscos_not_implemented(const char *feature);
mpi-genesis~ 323 #ifdef DEBUG
mpi-genesis~ 324 void riscos_dump_fdlist(void);
mpi-genesis~ 325 void riscos_list_openfiles(void);
mpi-genesis~ 326 #endif
mpi-genesis~ 327 #ifndef __RISCOS__C__
mpi-genesis~ 328 #define getpid riscos_getpid
mpi-genesis~ 329 #define kill(a,b) riscos_kill((a),(b))
mpi-genesis~ 330 #define access(a,b) riscos_access((a),(b))
mpi-genesis~ 331 #endif /* !__RISCOS__C__ */
mpi-genesis~ 332 #endif /* __riscos__ */
mpi-genesis~ 333
mpi-genesis~ 334 #endif /*G10_UTIL_H*/