-
+ 5B4ADEDB0DDA25D37413FD81037EF2D94722CEE3B58B5A5314DA3816308540588319A280819E272845D154B6F2FDBDCECE3A73C15B2802F33BC6BFE484F0B4A5
mpi/include/util.h
(0 . 0)(1 . 334)
2338 /* util.h
2339 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2340 * 2006 Free Software Foundation, Inc.
2341 *
2342 * This file is part of GNUPG.
2343 *
2344 * GNUPG is free software; you can redistribute it and/or modify
2345 * it under the terms of the GNU General Public License as published by
2346 * the Free Software Foundation; either version 3 of the License, or
2347 * (at your option) any later version.
2348 *
2349 * GNUPG is distributed in the hope that it will be useful,
2350 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2351 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2352 * GNU General Public License for more details.
2353 *
2354 * You should have received a copy of the GNU General Public License
2355 * along with this program; if not, see <http://www.gnu.org/licenses/>.
2356 */
2357 #ifndef G10_UTIL_H
2358 #define G10_UTIL_H
2359
2360 #if defined (_WIN32) || defined (__CYGWIN32__)
2361 #include <stdarg.h>
2362 #endif
2363
2364 #include "types.h"
2365 #include "errors.h"
2366 #include "types.h"
2367 #include "mpi.h"
2368 #include "compat.h"
2369
2370 typedef struct {
2371 int *argc; /* pointer to argc (value subject to change) */
2372 char ***argv; /* pointer to argv (value subject to change) */
2373 unsigned flags; /* Global flags (DO NOT CHANGE) */
2374 int err; /* print error about last option */
2375 /* 1 = warning, 2 = abort */
2376 int r_opt; /* return option */
2377 int r_type; /* type of return value (0 = no argument found)*/
2378 union {
2379 int ret_int;
2380 long ret_long;
2381 ulong ret_ulong;
2382 char *ret_str;
2383 } r; /* Return values */
2384 struct {
2385 int idx;
2386 int inarg;
2387 int stopped;
2388 const char *last;
2389 void *aliases;
2390 const void *cur_alias;
2391 } internal; /* DO NOT CHANGE */
2392 } ARGPARSE_ARGS;
2393
2394 typedef struct {
2395 int short_opt;
2396 const char *long_opt;
2397 unsigned flags;
2398 const char *description; /* optional option description */
2399 } ARGPARSE_OPTS;
2400
2401 /*-- logger.c --*/
2402 void log_set_logfile( const char *name, int fd );
2403 FILE *log_stream(void);
2404 void g10_log_print_prefix(const char *text);
2405 void log_set_name( const char *name );
2406 const char *log_get_name(void);
2407 void log_set_pid( int pid );
2408 int log_get_errorcount( int clear );
2409 void log_inc_errorcount(void);
2410 int log_set_strict(int val);
2411 void g10_log_hexdump( const char *text, const char *buf, size_t len );
2412
2413 #if defined (__riscos__) \
2414 || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
2415 void g10_log_bug( const char *fmt, ... )
2416 __attribute__ ((noreturn, format (printf,1,2)));
2417 void g10_log_bug0( const char *, int, const char * ) __attribute__ ((noreturn));
2418 void g10_log_fatal( const char *fmt, ... )
2419 __attribute__ ((noreturn, format (printf,1,2)));
2420 void g10_log_error( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2421 void g10_log_info( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2422 void g10_log_warning( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2423 void g10_log_debug( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2424 #ifndef __riscos__
2425 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __FUNCTION__ )
2426 #else
2427 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __func__ )
2428 #endif
2429 #else
2430 void g10_log_bug( const char *fmt, ... );
2431 void g10_log_bug0( const char *, int );
2432 void g10_log_fatal( const char *fmt, ... );
2433 void g10_log_error( const char *fmt, ... );
2434 void g10_log_info( const char *fmt, ... );
2435 void g10_log_warning( const char *fmt, ... );
2436 void g10_log_debug( const char *fmt, ... );
2437 #define BUG() g10_log_bug0( __FILE__ , __LINE__ )
2438 #endif
2439
2440 #define log_hexdump g10_log_hexdump
2441 #define log_bug g10_log_bug
2442 #define log_bug0 g10_log_bug0
2443 #define log_fatal g10_log_fatal
2444 #define log_error g10_log_error
2445 #define log_info g10_log_info
2446 #define log_warning g10_log_warning
2447 #define log_debug g10_log_debug
2448
2449
2450 /*-- errors.c --*/
2451 const char * g10_errstr( int no );
2452
2453 /*-- argparse.c --*/
2454 int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
2455 int optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
2456 ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
2457 void usage( int level );
2458 const char *default_strusage( int level );
2459
2460
2461 /*-- (main program) --*/
2462 const char *strusage( int level );
2463
2464
2465 /*-- dotlock.c --*/
2466 struct dotlock_handle;
2467 typedef struct dotlock_handle *DOTLOCK;
2468
2469 void disable_dotlock(void);
2470 DOTLOCK create_dotlock( const char *file_to_lock );
2471 void destroy_dotlock ( DOTLOCK h );
2472 int make_dotlock( DOTLOCK h, long timeout );
2473 int release_dotlock( DOTLOCK h );
2474 void remove_lockfiles (void);
2475
2476 /*-- fileutil.c --*/
2477 char * make_basename(const char *filepath, const char *inputpath);
2478 char * make_dirname(const char *filepath);
2479 char *make_filename( const char *first_part, ... );
2480 int compare_filenames( const char *a, const char *b );
2481 int same_file_p (const char *name1, const char *name2);
2482 const char *print_fname_stdin( const char *s );
2483 const char *print_fname_stdout( const char *s );
2484 int is_file_compressed(const char *s, int *r_status);
2485
2486 /*-- miscutil.c --*/
2487 u32 make_timestamp(void);
2488 u32 scan_isodatestr( const char *string );
2489 u32 isotime2seconds (const char *string);
2490 const char *strtimevalue( u32 stamp );
2491 const char *strtimestamp( u32 stamp ); /* GMT */
2492 const char *isotimestamp( u32 stamp ); /* GMT with hh:mm:ss */
2493 const char *asctimestamp( u32 stamp ); /* localized */
2494 void print_string( FILE *fp, const byte *p, size_t n, int delim );
2495 void print_string2( FILE *fp, const byte *p, size_t n, int delim, int delim2 );
2496 void print_utf8_string( FILE *fp, const byte *p, size_t n );
2497 void print_utf8_string2( FILE *fp, const byte *p, size_t n, int delim);
2498 char *make_printable_string( const byte *p, size_t n, int delim );
2499 int answer_is_yes_no_default( const char *s, int def_answer );
2500 int answer_is_yes( const char *s );
2501 int answer_is_yes_no_quit( const char *s );
2502 int answer_is_okay_cancel (const char *s, int def_answer);
2503 int match_multistr(const char *multistr,const char *match);
2504
2505 /*-- strgutil.c --*/
2506 void free_strlist( STRLIST sl );
2507 #define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0)
2508 STRLIST add_to_strlist( STRLIST *list, const char *string );
2509 STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
2510 STRLIST append_to_strlist( STRLIST *list, const char *string );
2511 STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
2512 STRLIST strlist_prev( STRLIST head, STRLIST node );
2513 STRLIST strlist_last( STRLIST node );
2514 char *pop_strlist( STRLIST *list );
2515 const char *memistr( const char *buf, size_t buflen, const char *sub );
2516 const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
2517 char *mem2str( char *, const void *, size_t);
2518 char *trim_spaces( char *string );
2519 unsigned int trim_trailing_chars( byte *line, unsigned int len,
2520 const char *trimchars);
2521 unsigned int trim_trailing_ws( byte *line, unsigned len );
2522 unsigned int check_trailing_chars( const byte *line, unsigned int len,
2523 const char *trimchars );
2524 unsigned int check_trailing_ws( const byte *line, unsigned int len );
2525 int string_count_chr( const char *string, int c );
2526 int set_native_charset( const char *newset );
2527 const char* get_native_charset(void);
2528 char *native_to_utf8( const char *string );
2529 char *utf8_to_native( const char *string, size_t length, int delim);
2530 char *string_to_utf8 (const char *string);
2531
2532 int ascii_isupper (int c);
2533 int ascii_islower (int c);
2534 int ascii_memcasecmp( const char *a, const char *b, size_t n);
2535
2536 #ifndef HAVE_STPCPY
2537 char *stpcpy(char *a,const char *b);
2538 #endif
2539 #ifndef HAVE_STRLWR
2540 char *strlwr(char *a);
2541 #endif
2542 #ifndef HAVE_STRCASECMP
2543 int strcasecmp( const char *, const char *b);
2544 #endif
2545 #ifndef HAVE_STRNCASECMP
2546 int strncasecmp (const char *, const char *b, size_t n);
2547 #endif
2548 #ifndef HAVE_STRTOUL
2549 #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
2550 #endif
2551 #ifndef HAVE_MEMMOVE
2552 #define memmove(d, s, n) bcopy((s), (d), (n))
2553 #endif
2554
2555 /*-- membuf.c --*/
2556 /* The definition of the structure is private, we only need it here,
2557 so it can be allocated on the stack. */
2558 struct private_membuf_s {
2559 size_t len;
2560 size_t size;
2561 char *buf;
2562 int out_of_core;
2563 };
2564
2565 typedef struct private_membuf_s membuf_t;
2566
2567 void init_membuf (membuf_t *mb, int initiallen);
2568 void put_membuf (membuf_t *mb, const void *buf, size_t len);
2569 void *get_membuf (membuf_t *mb, size_t *len);
2570
2571
2572
2573 #if defined (_WIN32)
2574 /*-- w32reg.c --*/
2575 char *read_w32_registry_string( const char *root,
2576 const char *dir, const char *name );
2577 int write_w32_registry_string(const char *root, const char *dir,
2578 const char *name, const char *value);
2579
2580 #endif /*_WIN32*/
2581
2582 /*-- strgutil.c --*/
2583 char *xasprintf (const char *fmt, ...);
2584 char *xtryasprintf (const char *fmt, ...);
2585
2586
2587 /*-- pka.c --*/
2588 char *get_pka_info (const char *address, unsigned char *fpr);
2589
2590 /*-- cert.c --*/
2591 int get_cert(const char *name,size_t max_size,IOBUF *iobuf,
2592 unsigned char **fpr,size_t *fpr_len,char **url);
2593
2594 /*-- convert.c --*/
2595 int hex2bin (const char *string, void *buffer, size_t length);
2596 int hexcolon2bin (const char *string, void *buffer, size_t length);
2597 char *bin2hex (const void *buffer, size_t length, char *stringbuf);
2598 char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
2599 const char *hex2str (const char *hexstring,
2600 char *buffer, size_t bufsize, size_t *buflen);
2601 char *hex2str_alloc (const char *hexstring, size_t *r_count);
2602
2603
2604 /**** other missing stuff ****/
2605 #ifndef HAVE_ATEXIT /* For SunOS */
2606 #define atexit(a) (on_exit((a),0))
2607 #endif
2608
2609 #ifndef HAVE_RAISE
2610 #define raise(a) kill(getpid(), (a))
2611 #endif
2612
2613 /*-- Replacement functions from funcname.c --*/
2614
2615
2616
2617 /******** some macros ************/
2618 #ifndef STR
2619 #define STR(v) #v
2620 #endif
2621 #define STR2(v) STR(v)
2622 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
2623 #define DIMof(type,member) DIM(((type *)0)->member)
2624
2625 #define wipememory2(_ptr,_set,_len) do { volatile char *_vptr=(volatile char *)(_ptr); size_t _vlen=(_len); while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } } while(0)
2626 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
2627
2628 /*-- macros to replace ctype ones and avoid locale problems --*/
2629 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
2630 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
2631 #define hexdigitp(a) (digitp (a) \
2632 || (*(a) >= 'A' && *(a) <= 'F') \
2633 || (*(a) >= 'a' && *(a) <= 'f'))
2634 /* the atoi macros assume that the buffer has only valid digits */
2635 #define atoi_1(p) (*(p) - '0' )
2636 #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
2637 #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
2638 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
2639 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
2640 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
2641
2642 /******* RISC OS stuff ***********/
2643 #ifdef __riscos__
2644 int riscos_load_module(const char *name, const char * const path[], int fatal);
2645 int riscos_get_filetype_from_string(const char *string, int len);
2646 int riscos_get_filetype(const char *filename);
2647 void riscos_set_filetype_by_number(const char *filename, int type);
2648 void riscos_set_filetype_by_mimetype(const char *filename, const char *mimetype);
2649 pid_t riscos_getpid(void);
2650 int riscos_kill(pid_t pid, int sig);
2651 int riscos_access(const char *path, int amode);
2652 int riscos_getchar(void);
2653 char *riscos_make_basename(const char *filepath, const char *inputpath);
2654 int riscos_check_regexp(const char *exp, const char *string, int debug);
2655 int riscos_fdopenfile(const char *filename, const int allow_write);
2656 void riscos_close_fds(void);
2657 int riscos_renamefile(const char *old, const char *new);
2658 char *riscos_gstrans(const char *old);
2659 void riscos_not_implemented(const char *feature);
2660 #ifdef DEBUG
2661 void riscos_dump_fdlist(void);
2662 void riscos_list_openfiles(void);
2663 #endif
2664 #ifndef __RISCOS__C__
2665 #define getpid riscos_getpid
2666 #define kill(a,b) riscos_kill((a),(b))
2667 #define access(a,b) riscos_access((a),(b))
2668 #endif /* !__RISCOS__C__ */
2669 #endif /* __riscos__ */
2670
2671 #endif /*G10_UTIL_H*/