1
/* util.h - Utility functions for GnuPG
2
* Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21
#ifndef GNUPG_COMMON_UTIL_H
22
#define GNUPG_COMMON_UTIL_H
24
#include <gcrypt.h> /* We need this for the memory function protos. */
25
#include <time.h> /* We need time_t. */
26
#include <gpg-error.h> /* we need gpg-error_t. */
28
/* to pass hash functions to libksba we need to cast it */
29
#define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
31
/* get all the stuff from jnlib */
32
#include "../jnlib/logging.h"
33
#include "../jnlib/argparse.h"
34
#include "../jnlib/stringhelp.h"
35
#include "../jnlib/mischelp.h"
36
#include "../jnlib/strlist.h"
37
#include "../jnlib/dotlock.h"
38
#include "../jnlib/utf8conv.h"
40
/* Handy malloc macros - please use only them. */
41
#define xtrymalloc(a) gcry_malloc ((a))
42
#define xtrymalloc_secure(a) gcry_malloc_secure ((a))
43
#define xtrycalloc(a,b) gcry_calloc ((a),(b))
44
#define xtrycalloc_secure(a,b) gcry_calloc_secure ((a),(b))
45
#define xtryrealloc(a,b) gcry_realloc ((a),(b))
46
#define xtrystrdup(a) gcry_strdup ((a))
47
#define xfree(a) gcry_free ((a))
49
#define xmalloc(a) gcry_xmalloc ((a))
50
#define xmalloc_secure(a) gcry_xmalloc_secure ((a))
51
#define xcalloc(a,b) gcry_xcalloc ((a),(b))
52
#define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
53
#define xrealloc(a,b) gcry_xrealloc ((a),(b))
54
#define xstrdup(a) gcry_xstrdup ((a))
57
/* A type to hold the ISO time. Note that this this is the same as
58
the the KSBA type ksba_isotime_t. */
59
typedef char gnupg_isotime_t[16];
63
int map_kbx_err (int err);
64
gpg_error_t map_assuan_err_with_source (int source, int err);
65
int map_to_assuan_status (int rc);
68
time_t gnupg_get_time (void);
69
void gnupg_get_isotime (gnupg_isotime_t timebuf);
70
void gnupg_set_time (time_t newtime, int freeze);
71
int gnupg_faked_time_p (void);
72
u32 make_timestamp (void);
73
u32 scan_isodatestr (const char *string);
74
u32 add_days_to_timestamp (u32 stamp, u16 days);
75
const char *strtimevalue (u32 stamp);
76
const char *strtimestamp (u32 stamp); /* GMT */
77
const char *asctimestamp (u32 stamp); /* localized */
80
/* Copy one iso ddate to another, this is inline so that we can do a
83
gnupg_copy_time (gnupg_isotime_t d, const gnupg_isotime_t s)
85
if (*s && (strlen (s) != 15 || s[8] != 'T'))
92
void gnupg_init_signals (int mode, void (*fast_cleanup)(void));
93
void gnupg_pause_on_sigusr (int which);
94
void gnupg_block_all_signals (void);
95
void gnupg_unblock_all_signals (void);
98
int answer_is_yes (const char *s);
99
int answer_is_yes_no_default (const char *s, int def_answer);
100
int answer_is_yes_no_quit (const char *s);
102
/*-- xreadline.c --*/
103
ssize_t read_line (FILE *fp,
104
char **addr_of_buffer, size_t *length_of_buffer,
116
unsigned char radbuf[4];
118
gpg_error_t b64enc_start (struct b64state *state, FILE *fp, const char *title);
119
gpg_error_t b64enc_write (struct b64state *state,
120
const void *buffer, size_t nbytes);
121
gpg_error_t b64enc_finish (struct b64state *state);
125
const char *default_homedir (void);
128
/*-- miscellaneous.c --*/
130
/* Same as asprintf but return an allocated buffer suitable to be
131
freed using xfree. This function simply dies on memory failure,
132
thus no extra check is required. */
133
char *xasprintf (const char *fmt, ...) JNLIB_GCC_A_PRINTF(1,2);
135
const char *print_fname_stdout (const char *s);
136
const char *print_fname_stdin (const char *s);
137
void print_string (FILE *fp, const byte *p, size_t n, int delim);
138
void print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim);
139
void print_utf8_string (FILE *fp, const byte *p, size_t n);
140
char *make_printable_string (const byte *p, size_t n, int delim);
142
int is_file_compressed (const char *s, int *ret_rc);
145
/*-- replacement functions from funcname.c --*/
148
int vasprintf (char **result, const char *format, va_list args);
149
int asprintf (char **result, const char *format, ...) JNLIB_GCC_A_PRINTF(2,3);
152
char *strsep (char **stringp, const char *delim);
155
char *ttyname (int fd);
158
char *mkdtemp (char *template);
162
/*-- some macros to replace ctype ones and avoid locale problems --*/
163
#define spacep(p) (*(p) == ' ' || *(p) == '\t')
164
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
165
#define hexdigitp(a) (digitp (a) \
166
|| (*(a) >= 'A' && *(a) <= 'F') \
167
|| (*(a) >= 'a' && *(a) <= 'f'))
168
/* Note this isn't identical to a C locale isspace() without \f and
169
\v, but works for the purposes used here. */
170
#define ascii_isspace(a) ((a)==' ' || (a)=='\n' || (a)=='\r' || (a)=='\t')
172
/* The atoi macros assume that the buffer has only valid digits. */
173
#define atoi_1(p) (*(p) - '0' )
174
#define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1))
175
#define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2))
176
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
177
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
178
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
179
#define xtoi_4(p) ((xtoi_2(p) * 256) + xtoi_2((p)+2))
183
#endif /*GNUPG_COMMON_UTIL_H*/