Bump to 2.4.3
[platform/upstream/gpg2.git] / common / stringhelp.h
1 /* stringhelp.h
2  * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3  *               2006, 2007, 2009  Free Software Foundation, Inc.
4  *               2015  g10 Code GmbH
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute and/or modify this
9  * part of GnuPG under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * GnuPG is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copies of the GNU General Public License
29  * and the GNU Lesser General Public License along with this program;
30  * if not, see <https://www.gnu.org/licenses/>.
31  */
32
33 #ifndef GNUPG_COMMON_STRINGHELP_H
34 #define GNUPG_COMMON_STRINGHELP_H
35
36 #include <stdint.h>
37 #include "types.h"
38
39 /*-- stringhelp.c --*/
40 char *has_leading_keyword (const char *string, const char *keyword);
41
42 const char *memistr (const void *buf, size_t buflen, const char *sub);
43 char *mem2str( char *, const void *, size_t);
44 char *trim_spaces( char *string );
45 char *ascii_trim_spaces (char *string);
46 char *trim_trailing_spaces( char *string );
47 unsigned int trim_trailing_chars( unsigned char *line, unsigned len,
48                                               const char *trimchars);
49 unsigned int trim_trailing_ws( unsigned char *line, unsigned len );
50 size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
51                                    const char *trimchars );
52 size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
53
54
55 char *make_basename(const char *filepath, const char *inputpath);
56 char *make_dirname(const char *filepath);
57 char *make_filename( const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
58 char *make_filename_try (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
59 char *make_absfilename (const char *first_part, ...) GPGRT_ATTR_SENTINEL(0);
60 char *make_absfilename_try (const char *first_part,
61                             ...) GPGRT_ATTR_SENTINEL(0);
62 int compare_filenames( const char *a, const char *b );
63
64 uint64_t string_to_u64 (const char *string);
65 int hextobyte (const char *s);
66
67 size_t utf8_charcount (const char *s, int len);
68
69
70 #ifdef HAVE_W32_SYSTEM
71 const char *w32_strerror (int ec);
72 #endif
73
74
75 int ascii_isupper (int c);
76 int ascii_islower (int c);
77 int ascii_toupper (int c);
78 int ascii_tolower (int c);
79 char *ascii_strlwr (char *s);
80 char *ascii_strupr (char *s);
81 int ascii_strcasecmp( const char *a, const char *b );
82 int ascii_strncasecmp (const char *a, const char *b, size_t n);
83 int ascii_memcasecmp( const void *a, const void *b, size_t n );
84 const char *ascii_memistr ( const void *buf, size_t buflen, const char *sub);
85 void *ascii_memcasemem (const void *haystack, size_t nhaystack,
86                         const void *needle, size_t nneedle);
87
88
89 #ifndef HAVE_MEMICMP
90 int memicmp( const char *a, const char *b, size_t n );
91 #endif
92 #ifndef HAVE_STPCPY
93 char *stpcpy(char *a,const char *b);
94 #endif
95 #ifndef HAVE_STRPBRK
96 char *strpbrk (const char *s, const char *accept);
97 #endif
98 #ifndef HAVE_STRSEP
99 char *strsep (char **stringp, const char *delim);
100 #endif
101 #ifndef HAVE_STRLWR
102 char *strlwr(char *a);
103 #endif
104 #ifndef HAVE_STRTOUL
105 #  define strtoul(a,b,c)  ((unsigned long)strtol((a),(b),(c)))
106 #endif
107 #ifndef HAVE_MEMMOVE
108 #  define memmove(d, s, n) bcopy((s), (d), (n))
109 #endif
110 #ifndef HAVE_STRICMP
111 #  define stricmp(a,b)   strcasecmp( (a), (b) )
112 #endif
113 #ifndef HAVE_MEMRCHR
114 void *memrchr (const void *buffer, int c, size_t n);
115 #endif
116
117
118 #ifndef HAVE_ISASCII
119 static inline int
120 isascii (int c)
121 {
122   return (((c) & ~0x7f) == 0);
123 }
124 #endif /* !HAVE_ISASCII */
125
126
127 #ifndef STR
128 #  define STR(v) #v
129 #endif
130 #define STR2(v) STR(v)
131
132 /* Percent-escape the string STR by replacing colons with '%3a'.  If
133    EXTRA is not NULL, also replace all characters given in EXTRA.  The
134    "try_" variant fails with NULL if not enough memory can be
135    allocated.  */
136 char *percent_escape (const char *str, const char *extra);
137 char *try_percent_escape (const char *str, const char *extra);
138
139
140 /* Concatenate the string S1 with all the following strings up to a
141    NULL.  Returns a malloced buffer with the new string or NULL on a
142    malloc error or if too many arguments are given.  */
143 char *strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
144 /* Same but taking a va_list.  */
145 char *vstrconcat (const char *s1, va_list arg_ptr);
146 /* Ditto, but die on error.  */
147 char *xstrconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
148
149
150 char **strsplit (char *string, char delim, char replacement, int *count);
151
152 /* Tokenize STRING using the set of delimiters in DELIM.  */
153 char **strtokenize (const char *string, const char *delim);
154 /* Tokenize STRING using the set of delimiters in DELIM but do not
155  * trim the tokens.  */
156 char **strtokenize_nt (const char *string, const char *delim);
157
158 /* Split STRING into space delimited fields and store them in the
159  * provided ARRAY.  */
160 int split_fields (char *string, const char **array, int arraysize);
161
162 /* Split STRING into colon delimited fields and store them in the
163  * provided ARRAY.  */
164 int split_fields_colon (char *string, const char **array, int arraysize);
165
166 /* Return True if MYVERSION is greater or equal than REQ_VERSION.  */
167 int compare_version_strings (const char *my_version, const char *req_version);
168
169 /* Format a string so that it fits within about TARGET_COLS columns.  */
170 char *format_text (const char *text, int target_cols, int max_cols);
171
172 /* Substitute variables in STRING.  */
173 char *substitute_vars (const char *string,
174                        const char *(*getval)(void *cookie, const char *name),
175                        void *cookie);
176 char *substitute_envvars (const char *string);
177
178
179 /*-- mapstrings.c --*/
180 const char *map_static_macro_string (const char *string);
181 const char *map_static_strings (const char *domain, int key1, int key2,
182                                 const char *string1, ...);
183
184 #endif /*GNUPG_COMMON_STRINGHELP_H*/