1 /* util.h
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
4 
5    This file is part of GPGME.
6 
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11 
12    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16 
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21 
22 #ifndef UTIL_H
23 #define UTIL_H
24 
25 #ifdef HAVE_W32_SYSTEM
26 # include "winsock2.h"
27 # include "windows.h"
28 #endif
29 
30 /* For pid_t.  */
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 /* We must see the symbol ttyname_r before a redefinition. */
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 
39 #include "gpgme.h"
40 
41 
42 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
43 
44 
45 
46 /*-- {posix,w32}-util.c --*/
47 int _gpgme_get_conf_int (const char *key, int *value);
48 void _gpgme_allow_set_foreground_window (pid_t pid);
49 
50 /*-- dirinfo.c --*/
51 void _gpgme_dirinfo_disable_gpgconf (void);
52 
53 const char *_gpgme_get_default_homedir (void);
54 const char *_gpgme_get_default_agent_socket (void);
55 const char *_gpgme_get_default_gpg_name (void);
56 const char *_gpgme_get_default_gpgsm_name (void);
57 const char *_gpgme_get_default_g13_name (void);
58 const char *_gpgme_get_default_gpgconf_name (void);
59 const char *_gpgme_get_default_uisrv_socket (void);
60 int _gpgme_in_gpg_one_mode (void);
61 
62 const char *_gpgme_get_basename (const char *name);
63 
64 
65 
66 /*-- replacement functions in <funcname>.c --*/
67 #ifdef HAVE_CONFIG_H
68 
69 #ifndef HAVE_STPCPY
70 static _GPGME_INLINE char *
_gpgme_stpcpy(char * a,const char * b)71 _gpgme_stpcpy (char *a, const char *b)
72 {
73   while (*b)
74     *a++ = *b++;
75   *a = 0;
76   return a;
77 }
78 #define stpcpy(a,b) _gpgme_stpcpy ((a), (b))
79 #endif /*!HAVE_STPCPY*/
80 
81 
82 /* Due to a bug in mingw32's snprintf related to the 'l' modifier and
83    for increased portability we use our snprintf on all systems. */
84 #undef snprintf
85 #define snprintf gpgrt_snprintf
86 
87 
88 #if REPLACE_TTYNAME_R
89 int _gpgme_ttyname_r (int fd, char *buf, size_t buflen);
90 #undef  ttyname_r
91 #define ttyname_r(a,b,c) _gpgme_ttyname_r ((a), (b), (c))
92 #endif
93 
94 #endif /*HAVE_CONFIG_H*/
95 
96 
97 /*-- conversion.c --*/
98 
99 /* Concatenate the string S1 with all the following strings up to a
100    NULL.  Returns a malloced buffer with the new string or NULL on a
101    malloc error or if too many arguments are given.  */
102 char *_gpgme_strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
103 
104 /* Convert two hexadecimal digits from STR to the value they
105    represent.  Returns -1 if one of the characters is not a
106    hexadecimal digit.  */
107 int _gpgme_hextobyte (const char *str);
108 
109 /* Decode the C formatted string SRC and store the result in the
110    buffer *DESTP which is LEN bytes long.  If LEN is zero, then a
111    large enough buffer is allocated with malloc and *DESTP is set to
112    the result.  Currently, LEN is only used to specify if allocation
113    is desired or not, the caller is expected to make sure that *DESTP
114    is large enough if LEN is not zero.  */
115 gpgme_error_t _gpgme_decode_c_string (const char *src, char **destp,
116 				      size_t len);
117 
118 /* Decode the percent escaped string SRC and store the result in the
119    buffer *DESTP which is LEN bytes long.  If LEN is zero, then a
120    large enough buffer is allocated with malloc and *DESTP is set to
121    the result.  Currently, LEN is only used to specify if allocation
122    is desired or not, the caller is expected to make sure that *DESTP
123    is large enough if LEN is not zero.  If BINARY is 1, then '\0'
124    characters are allowed in the output.  */
125 gpgme_error_t _gpgme_decode_percent_string (const char *src, char **destp,
126 					    size_t len, int binary);
127 
128 gpgme_error_t _gpgme_encode_percent_string (const char *src, char **destp,
129 					    size_t len);
130 
131 /* Split a string into space delimited fields and remove leading and
132  * trailing spaces from each field.  A pointer to the each field is
133  * stored in ARRAY.  Stop splitting at ARRAYSIZE fields.  The function
134  * modifies STRING.  The number of parsed fields is returned.  */
135 int _gpgme_split_fields (char *string, char **array, int arraysize);
136 
137 /* Convert the field STRING into an unsigned long value.  Check for
138  * trailing garbage.  */
139 gpgme_error_t _gpgme_strtoul_field (const char *string, unsigned long *result);
140 
141 /* Convert STRING into an offset value similar to atoi().  */
142 gpgme_off_t _gpgme_string_to_off (const char *string);
143 
144 /* Parse the string TIMESTAMP into a time_t.  The string may either be
145    seconds since Epoch or in the ISO 8601 format like
146    "20390815T143012".  Returns 0 for an empty string or seconds since
147    Epoch. Leading spaces are skipped. If ENDP is not NULL, it will
148    point to the next non-parsed character in TIMESTRING. */
149 time_t _gpgme_parse_timestamp (const char *timestamp, char **endp);
150 
151 /* Variant of _gpgme_parse_timestamp to return an unsigned long or 0
152  * on error or missing timestamp.  */
153 unsigned long _gpgme_parse_timestamp_ul (const char *timestamp);
154 
155 int _gpgme_map_pk_algo (int algo, gpgme_protocol_t protocol);
156 
157 const char *_gpgme_cipher_algo_name (int algo, gpgme_protocol_t protocol);
158 const char *_gpgme_cipher_mode_name (int algo, gpgme_protocol_t protocol);
159 
160 
161 /*-- b64dec.c --*/
162 
163 struct b64state
164 {
165   int idx;
166   int quad_count;
167   char *title;
168   unsigned char radbuf[4];
169   int stop_seen:1;
170   int invalid_encoding:1;
171   gpg_error_t lasterr;
172 };
173 
174 gpg_error_t _gpgme_b64dec_start (struct b64state *state, const char *title);
175 gpg_error_t _gpgme_b64dec_proc (struct b64state *state,
176                                 void *buffer, size_t length, size_t *r_nbytes);
177 gpg_error_t _gpgme_b64dec_finish (struct b64state *state);
178 
179 
180 
181 /* Retrieve the environment variable NAME and return a copy of it in a
182    malloc()'ed buffer in *VALUE.  If the environment variable is not
183    set, return NULL in *VALUE.  */
184 gpgme_error_t _gpgme_getenv (const char *name, char **value);
185 
186 
187 /*-- status-table.c --*/
188 /* Convert a status string to a status code.  */
189 void _gpgme_status_init (void);
190 gpgme_status_code_t _gpgme_parse_status (const char *name);
191 const char *_gpgme_status_to_string (gpgme_status_code_t code);
192 
193 
194 #ifdef HAVE_W32_SYSTEM
195 int _gpgme_mkstemp (int *fd, char **name);
196 const char *_gpgme_get_w32spawn_path (void);
197 #endif /*HAVE_W32_SYSTEM*/
198 
199 
200 
201 #include <assuan.h>
202 /* System hooks for assuan integration.  */
203 extern struct assuan_system_hooks _gpgme_assuan_system_hooks;
204 extern struct assuan_malloc_hooks _gpgme_assuan_malloc_hooks;
205 int _gpgme_assuan_log_cb (assuan_context_t ctx, void *hook,
206 			  unsigned int cat, const char *msg);
207 
208 
209 
210 /* Parse the compliance field.  */
211 #define PARSE_COMPLIANCE_FLAGS(flags, result)				\
212   do {									\
213     char *comp_p, *comp_endp;						\
214     unsigned long comp_ul;						\
215 									\
216     for (comp_p = (flags);						\
217 	 comp_p								\
218 	   && (comp_ul = strtoul (comp_p, &comp_endp, 10))		\
219 	   && comp_p != comp_endp;					\
220 	 comp_p = comp_endp)						\
221       {									\
222 	switch (comp_ul)						\
223 	  {								\
224 	  case 23: (result)->is_de_vs = 1; break;			\
225 	  }								\
226       }									\
227   } while (0)
228 
229 
230 #endif /* UTIL_H */
231