-
+ 5B4ADEDB0DDA25D37413FD81037EF2D94722CEE3B58B5A5314DA3816308540588319A280819E272845D154B6F2FDBDCECE3A73C15B2802F33BC6BFE484F0B4A5mpi/include/util.h(0 . 0)(1 . 334)
2442 /* util.h
2443 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2444 * 2006 Free Software Foundation, Inc.
2445 *
2446 * This file is part of GNUPG.
2447 *
2448 * GNUPG is free software; you can redistribute it and/or modify
2449 * it under the terms of the GNU General Public License as published by
2450 * the Free Software Foundation; either version 3 of the License, or
2451 * (at your option) any later version.
2452 *
2453 * GNUPG is distributed in the hope that it will be useful,
2454 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2455 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2456 * GNU General Public License for more details.
2457 *
2458 * You should have received a copy of the GNU General Public License
2459 * along with this program; if not, see <http://www.gnu.org/licenses/>.
2460 */
2461 #ifndef G10_UTIL_H
2462 #define G10_UTIL_H
2463
2464 #if defined (_WIN32) || defined (__CYGWIN32__)
2465 #include <stdarg.h>
2466 #endif
2467
2468 #include "types.h"
2469 #include "errors.h"
2470 #include "types.h"
2471 #include "mpi.h"
2472 #include "compat.h"
2473
2474 typedef struct {
2475 int *argc; /* pointer to argc (value subject to change) */
2476 char ***argv; /* pointer to argv (value subject to change) */
2477 unsigned flags; /* Global flags (DO NOT CHANGE) */
2478 int err; /* print error about last option */
2479 /* 1 = warning, 2 = abort */
2480 int r_opt; /* return option */
2481 int r_type; /* type of return value (0 = no argument found)*/
2482 union {
2483 int ret_int;
2484 long ret_long;
2485 ulong ret_ulong;
2486 char *ret_str;
2487 } r; /* Return values */
2488 struct {
2489 int idx;
2490 int inarg;
2491 int stopped;
2492 const char *last;
2493 void *aliases;
2494 const void *cur_alias;
2495 } internal; /* DO NOT CHANGE */
2496 } ARGPARSE_ARGS;
2497
2498 typedef struct {
2499 int short_opt;
2500 const char *long_opt;
2501 unsigned flags;
2502 const char *description; /* optional option description */
2503 } ARGPARSE_OPTS;
2504
2505 /*-- logger.c --*/
2506 void log_set_logfile( const char *name, int fd );
2507 FILE *log_stream(void);
2508 void g10_log_print_prefix(const char *text);
2509 void log_set_name( const char *name );
2510 const char *log_get_name(void);
2511 void log_set_pid( int pid );
2512 int log_get_errorcount( int clear );
2513 void log_inc_errorcount(void);
2514 int log_set_strict(int val);
2515 void g10_log_hexdump( const char *text, const char *buf, size_t len );
2516
2517 #if defined (__riscos__) \
2518 || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
2519 void g10_log_bug( const char *fmt, ... )
2520 __attribute__ ((noreturn, format (printf,1,2)));
2521 void g10_log_bug0( const char *, int, const char * ) __attribute__ ((noreturn));
2522 void g10_log_fatal( const char *fmt, ... )
2523 __attribute__ ((noreturn, format (printf,1,2)));
2524 void g10_log_error( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2525 void g10_log_info( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2526 void g10_log_warning( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2527 void g10_log_debug( const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
2528 #ifndef __riscos__
2529 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __FUNCTION__ )
2530 #else
2531 #define BUG() g10_log_bug0( __FILE__ , __LINE__, __func__ )
2532 #endif
2533 #else
2534 void g10_log_bug( const char *fmt, ... );
2535 void g10_log_bug0( const char *, int );
2536 void g10_log_fatal( const char *fmt, ... );
2537 void g10_log_error( const char *fmt, ... );
2538 void g10_log_info( const char *fmt, ... );
2539 void g10_log_warning( const char *fmt, ... );
2540 void g10_log_debug( const char *fmt, ... );
2541 #define BUG() g10_log_bug0( __FILE__ , __LINE__ )
2542 #endif
2543
2544 #define log_hexdump g10_log_hexdump
2545 #define log_bug g10_log_bug
2546 #define log_bug0 g10_log_bug0
2547 #define log_fatal g10_log_fatal
2548 #define log_error g10_log_error
2549 #define log_info g10_log_info
2550 #define log_warning g10_log_warning
2551 #define log_debug g10_log_debug
2552
2553
2554 /*-- errors.c --*/
2555 const char * g10_errstr( int no );
2556
2557 /*-- argparse.c --*/
2558 int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
2559 int optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
2560 ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
2561 void usage( int level );
2562 const char *default_strusage( int level );
2563
2564
2565 /*-- (main program) --*/
2566 const char *strusage( int level );
2567
2568
2569 /*-- dotlock.c --*/
2570 struct dotlock_handle;
2571 typedef struct dotlock_handle *DOTLOCK;
2572
2573 void disable_dotlock(void);
2574 DOTLOCK create_dotlock( const char *file_to_lock );
2575 void destroy_dotlock ( DOTLOCK h );
2576 int make_dotlock( DOTLOCK h, long timeout );
2577 int release_dotlock( DOTLOCK h );
2578 void remove_lockfiles (void);
2579
2580 /*-- fileutil.c --*/
2581 char * make_basename(const char *filepath, const char *inputpath);
2582 char * make_dirname(const char *filepath);
2583 char *make_filename( const char *first_part, ... );
2584 int compare_filenames( const char *a, const char *b );
2585 int same_file_p (const char *name1, const char *name2);
2586 const char *print_fname_stdin( const char *s );
2587 const char *print_fname_stdout( const char *s );
2588 int is_file_compressed(const char *s, int *r_status);
2589
2590 /*-- miscutil.c --*/
2591 u32 make_timestamp(void);
2592 u32 scan_isodatestr( const char *string );
2593 u32 isotime2seconds (const char *string);
2594 const char *strtimevalue( u32 stamp );
2595 const char *strtimestamp( u32 stamp ); /* GMT */
2596 const char *isotimestamp( u32 stamp ); /* GMT with hh:mm:ss */
2597 const char *asctimestamp( u32 stamp ); /* localized */
2598 void print_string( FILE *fp, const byte *p, size_t n, int delim );
2599 void print_string2( FILE *fp, const byte *p, size_t n, int delim, int delim2 );
2600 void print_utf8_string( FILE *fp, const byte *p, size_t n );
2601 void print_utf8_string2( FILE *fp, const byte *p, size_t n, int delim);
2602 char *make_printable_string( const byte *p, size_t n, int delim );
2603 int answer_is_yes_no_default( const char *s, int def_answer );
2604 int answer_is_yes( const char *s );
2605 int answer_is_yes_no_quit( const char *s );
2606 int answer_is_okay_cancel (const char *s, int def_answer);
2607 int match_multistr(const char *multistr,const char *match);
2608
2609 /*-- strgutil.c --*/
2610 void free_strlist( STRLIST sl );
2611 #define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0)
2612 STRLIST add_to_strlist( STRLIST *list, const char *string );
2613 STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
2614 STRLIST append_to_strlist( STRLIST *list, const char *string );
2615 STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
2616 STRLIST strlist_prev( STRLIST head, STRLIST node );
2617 STRLIST strlist_last( STRLIST node );
2618 char *pop_strlist( STRLIST *list );
2619 const char *memistr( const char *buf, size_t buflen, const char *sub );
2620 const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
2621 char *mem2str( char *, const void *, size_t);
2622 char *trim_spaces( char *string );
2623 unsigned int trim_trailing_chars( byte *line, unsigned int len,
2624 const char *trimchars);
2625 unsigned int trim_trailing_ws( byte *line, unsigned len );
2626 unsigned int check_trailing_chars( const byte *line, unsigned int len,
2627 const char *trimchars );
2628 unsigned int check_trailing_ws( const byte *line, unsigned int len );
2629 int string_count_chr( const char *string, int c );
2630 int set_native_charset( const char *newset );
2631 const char* get_native_charset(void);
2632 char *native_to_utf8( const char *string );
2633 char *utf8_to_native( const char *string, size_t length, int delim);
2634 char *string_to_utf8 (const char *string);
2635
2636 int ascii_isupper (int c);
2637 int ascii_islower (int c);
2638 int ascii_memcasecmp( const char *a, const char *b, size_t n);
2639
2640 #ifndef HAVE_STPCPY
2641 char *stpcpy(char *a,const char *b);
2642 #endif
2643 #ifndef HAVE_STRLWR
2644 char *strlwr(char *a);
2645 #endif
2646 #ifndef HAVE_STRCASECMP
2647 int strcasecmp( const char *, const char *b);
2648 #endif
2649 #ifndef HAVE_STRNCASECMP
2650 int strncasecmp (const char *, const char *b, size_t n);
2651 #endif
2652 #ifndef HAVE_STRTOUL
2653 #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c)))
2654 #endif
2655 #ifndef HAVE_MEMMOVE
2656 #define memmove(d, s, n) bcopy((s), (d), (n))
2657 #endif
2658
2659 /*-- membuf.c --*/
2660 /* The definition of the structure is private, we only need it here,
2661 so it can be allocated on the stack. */
2662 struct private_membuf_s {
2663 size_t len;
2664 size_t size;
2665 char *buf;
2666 int out_of_core;
2667 };
2668
2669 typedef struct private_membuf_s membuf_t;
2670
2671 void init_membuf (membuf_t *mb, int initiallen);
2672 void put_membuf (membuf_t *mb, const void *buf, size_t len);
2673 void *get_membuf (membuf_t *mb, size_t *len);
2674
2675
2676
2677 #if defined (_WIN32)
2678 /*-- w32reg.c --*/
2679 char *read_w32_registry_string( const char *root,
2680 const char *dir, const char *name );
2681 int write_w32_registry_string(const char *root, const char *dir,
2682 const char *name, const char *value);
2683
2684 #endif /*_WIN32*/
2685
2686 /*-- strgutil.c --*/
2687 char *xasprintf (const char *fmt, ...);
2688 char *xtryasprintf (const char *fmt, ...);
2689
2690
2691 /*-- pka.c --*/
2692 char *get_pka_info (const char *address, unsigned char *fpr);
2693
2694 /*-- cert.c --*/
2695 int get_cert(const char *name,size_t max_size,IOBUF *iobuf,
2696 unsigned char **fpr,size_t *fpr_len,char **url);
2697
2698 /*-- convert.c --*/
2699 int hex2bin (const char *string, void *buffer, size_t length);
2700 int hexcolon2bin (const char *string, void *buffer, size_t length);
2701 char *bin2hex (const void *buffer, size_t length, char *stringbuf);
2702 char *bin2hexcolon (const void *buffer, size_t length, char *stringbuf);
2703 const char *hex2str (const char *hexstring,
2704 char *buffer, size_t bufsize, size_t *buflen);
2705 char *hex2str_alloc (const char *hexstring, size_t *r_count);
2706
2707
2708 /**** other missing stuff ****/
2709 #ifndef HAVE_ATEXIT /* For SunOS */
2710 #define atexit(a) (on_exit((a),0))
2711 #endif
2712
2713 #ifndef HAVE_RAISE
2714 #define raise(a) kill(getpid(), (a))
2715 #endif
2716
2717 /*-- Replacement functions from funcname.c --*/
2718
2719
2720
2721 /******** some macros ************/
2722 #ifndef STR
2723 #define STR(v) #v
2724 #endif
2725 #define STR2(v) STR(v)
2726 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
2727 #define DIMof(type,member) DIM(((type *)0)->member)
2728
2729 #define wipememory2(_ptr,_set,_len) do { volatile char *_vptr=(volatile char *)(_ptr); size_t _vlen=(_len); while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } } while(0)
2730 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
2731
2732 /*-- macros to replace ctype ones and avoid locale problems --*/
2733 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
2734 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
2735 #define hexdigitp(a) (digitp (a) \
2736 || (*(a) >= 'A' && *(a) <= 'F') \
2737 || (*(a) >= 'a' && *(a) <= 'f'))
2738 /* the atoi macros assume that the buffer has only valid digits */
2739 #define atoi_1(p) (*(p) - '0' )
2740 #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
2741 #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
2742 #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
2743 *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
2744 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
2745
2746 /******* RISC OS stuff ***********/
2747 #ifdef __riscos__
2748 int riscos_load_module(const char *name, const char * const path[], int fatal);
2749 int riscos_get_filetype_from_string(const char *string, int len);
2750 int riscos_get_filetype(const char *filename);
2751 void riscos_set_filetype_by_number(const char *filename, int type);
2752 void riscos_set_filetype_by_mimetype(const char *filename, const char *mimetype);
2753 pid_t riscos_getpid(void);
2754 int riscos_kill(pid_t pid, int sig);
2755 int riscos_access(const char *path, int amode);
2756 int riscos_getchar(void);
2757 char *riscos_make_basename(const char *filepath, const char *inputpath);
2758 int riscos_check_regexp(const char *exp, const char *string, int debug);
2759 int riscos_fdopenfile(const char *filename, const int allow_write);
2760 void riscos_close_fds(void);
2761 int riscos_renamefile(const char *old, const char *new);
2762 char *riscos_gstrans(const char *old);
2763 void riscos_not_implemented(const char *feature);
2764 #ifdef DEBUG
2765 void riscos_dump_fdlist(void);
2766 void riscos_list_openfiles(void);
2767 #endif
2768 #ifndef __RISCOS__C__
2769 #define getpid riscos_getpid
2770 #define kill(a,b) riscos_kill((a),(b))
2771 #define access(a,b) riscos_access((a),(b))
2772 #endif /* !__RISCOS__C__ */
2773 #endif /* __riscos__ */
2774
2775 #endif /*G10_UTIL_H*/