diff options
Diffstat (limited to 'src/lib/mhd_str.h')
-rw-r--r-- | src/lib/mhd_str.h | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/src/lib/mhd_str.h b/src/lib/mhd_str.h new file mode 100644 index 00000000..410cc36e --- /dev/null +++ b/src/lib/mhd_str.h | |||
@@ -0,0 +1,266 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2015, 2016 Karlson2k (Evgeny Grin) | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file microhttpd/mhd_str.h | ||
22 | * @brief Header for string manipulating helpers | ||
23 | * @author Karlson2k (Evgeny Grin) | ||
24 | */ | ||
25 | |||
26 | #ifndef MHD_STR_H | ||
27 | #define MHD_STR_H 1 | ||
28 | |||
29 | #include "mhd_options.h" | ||
30 | |||
31 | #include <stdint.h> | ||
32 | #include <stdlib.h> | ||
33 | #ifdef HAVE_STDBOOL_H | ||
34 | #include <stdbool.h> | ||
35 | #endif /* HAVE_STDBOOL_H */ | ||
36 | |||
37 | #ifdef MHD_FAVOR_SMALL_CODE | ||
38 | #include "mhd_limits.h" | ||
39 | #endif /* MHD_FAVOR_SMALL_CODE */ | ||
40 | |||
41 | #ifndef MHD_STATICSTR_LEN_ | ||
42 | /** | ||
43 | * Determine length of static string / macro strings at compile time. | ||
44 | */ | ||
45 | #define MHD_STATICSTR_LEN_(macro) (sizeof(macro)/sizeof(char) - 1) | ||
46 | #endif /* ! MHD_STATICSTR_LEN_ */ | ||
47 | |||
48 | /* | ||
49 | * Block of functions/macros that use US-ASCII charset as required by HTTP | ||
50 | * standards. Not affected by current locale settings. | ||
51 | */ | ||
52 | |||
53 | #ifndef MHD_FAVOR_SMALL_CODE | ||
54 | /** | ||
55 | * Check two string for equality, ignoring case of US-ASCII letters. | ||
56 | * @param str1 first string to compare | ||
57 | * @param str2 second string to compare | ||
58 | * @return non-zero if two strings are equal, zero otherwise. | ||
59 | */ | ||
60 | int | ||
61 | MHD_str_equal_caseless_ (const char * str1, | ||
62 | const char * str2); | ||
63 | #else /* MHD_FAVOR_SMALL_CODE */ | ||
64 | /* Reuse MHD_str_equal_caseless_n_() to reduce size */ | ||
65 | #define MHD_str_equal_caseless_(s1,s2) MHD_str_equal_caseless_n_((s1),(s2), SIZE_MAX) | ||
66 | #endif /* MHD_FAVOR_SMALL_CODE */ | ||
67 | |||
68 | |||
69 | /** | ||
70 | * Check two string for equality, ignoring case of US-ASCII letters and | ||
71 | * checking not more than @a maxlen characters. | ||
72 | * Compares up to first terminating null character, but not more than | ||
73 | * first @a maxlen characters. | ||
74 | * @param str1 first string to compare | ||
75 | * @param str2 second string to compare | ||
76 | * @param maxlen maximum number of characters to compare | ||
77 | * @return non-zero if two strings are equal, zero otherwise. | ||
78 | */ | ||
79 | int | ||
80 | MHD_str_equal_caseless_n_ (const char * const str1, | ||
81 | const char * const str2, | ||
82 | size_t maxlen); | ||
83 | |||
84 | |||
85 | /** | ||
86 | * Check whether @a str has case-insensitive @a token. | ||
87 | * Token could be surrounded by spaces and tabs and delimited by comma. | ||
88 | * Match succeed if substring between start, end of string or comma | ||
89 | * contains only case-insensitive token and optional spaces and tabs. | ||
90 | * @warning token must not contain null-charters except optional | ||
91 | * terminating null-character. | ||
92 | * @param str the string to check | ||
93 | * @param token the token to find | ||
94 | * @param token_len length of token, not including optional terminating | ||
95 | * null-character. | ||
96 | * @return non-zero if two strings are equal, zero otherwise. | ||
97 | */ | ||
98 | bool | ||
99 | MHD_str_has_token_caseless_ (const char * str, | ||
100 | const char * const token, | ||
101 | size_t token_len); | ||
102 | |||
103 | /** | ||
104 | * Check whether @a str has case-insensitive static @a tkn. | ||
105 | * Token could be surrounded by spaces and tabs and delimited by comma. | ||
106 | * Match succeed if substring between start, end of string or comma | ||
107 | * contains only case-insensitive token and optional spaces and tabs. | ||
108 | * @warning tkn must be static string | ||
109 | * @param str the string to check | ||
110 | * @param tkn the static string of token to find | ||
111 | * @return non-zero if two strings are equal, zero otherwise. | ||
112 | */ | ||
113 | #define MHD_str_has_s_token_caseless_(str,tkn) \ | ||
114 | MHD_str_has_token_caseless_((str),(tkn),MHD_STATICSTR_LEN_(tkn)) | ||
115 | |||
116 | #ifndef MHD_FAVOR_SMALL_CODE | ||
117 | /* Use individual function for each case to improve speed */ | ||
118 | |||
119 | /** | ||
120 | * Convert decimal US-ASCII digits in string to number in uint64_t. | ||
121 | * Conversion stopped at first non-digit character. | ||
122 | * @param str string to convert | ||
123 | * @param out_val pointer to uint64_t to store result of conversion | ||
124 | * @return non-zero number of characters processed on succeed, | ||
125 | * zero if no digit is found, resulting value is larger | ||
126 | * then possible to store in uint64_t or @a out_val is NULL | ||
127 | */ | ||
128 | size_t | ||
129 | MHD_str_to_uint64_ (const char * str, | ||
130 | uint64_t * out_val); | ||
131 | |||
132 | /** | ||
133 | * Convert not more then @a maxlen decimal US-ASCII digits in string to | ||
134 | * number in uint64_t. | ||
135 | * Conversion stopped at first non-digit character or after @a maxlen | ||
136 | * digits. | ||
137 | * @param str string to convert | ||
138 | * @param maxlen maximum number of characters to process | ||
139 | * @param out_val pointer to uint64_t to store result of conversion | ||
140 | * @return non-zero number of characters processed on succeed, | ||
141 | * zero if no digit is found, resulting value is larger | ||
142 | * then possible to store in uint64_t or @a out_val is NULL | ||
143 | */ | ||
144 | size_t | ||
145 | MHD_str_to_uint64_n_ (const char * str, | ||
146 | size_t maxlen, | ||
147 | uint64_t * out_val); | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Convert hexadecimal US-ASCII digits in string to number in uint32_t. | ||
152 | * Conversion stopped at first non-digit character. | ||
153 | * @param str string to convert | ||
154 | * @param out_val pointer to uint32_t to store result of conversion | ||
155 | * @return non-zero number of characters processed on succeed, | ||
156 | * zero if no digit is found, resulting value is larger | ||
157 | * then possible to store in uint32_t or @a out_val is NULL | ||
158 | */ | ||
159 | size_t | ||
160 | MHD_strx_to_uint32_ (const char * str, | ||
161 | uint32_t * out_val); | ||
162 | |||
163 | |||
164 | /** | ||
165 | * Convert not more then @a maxlen hexadecimal US-ASCII digits in string | ||
166 | * to number in uint32_t. | ||
167 | * Conversion stopped at first non-digit character or after @a maxlen | ||
168 | * digits. | ||
169 | * @param str string to convert | ||
170 | * @param maxlen maximum number of characters to process | ||
171 | * @param out_val pointer to uint32_t to store result of conversion | ||
172 | * @return non-zero number of characters processed on succeed, | ||
173 | * zero if no digit is found, resulting value is larger | ||
174 | * then possible to store in uint32_t or @a out_val is NULL | ||
175 | */ | ||
176 | size_t | ||
177 | MHD_strx_to_uint32_n_ (const char * str, | ||
178 | size_t maxlen, | ||
179 | uint32_t * out_val); | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Convert hexadecimal US-ASCII digits in string to number in uint64_t. | ||
184 | * Conversion stopped at first non-digit character. | ||
185 | * @param str string to convert | ||
186 | * @param out_val pointer to uint64_t to store result of conversion | ||
187 | * @return non-zero number of characters processed on succeed, | ||
188 | * zero if no digit is found, resulting value is larger | ||
189 | * then possible to store in uint64_t or @a out_val is NULL | ||
190 | */ | ||
191 | size_t | ||
192 | MHD_strx_to_uint64_ (const char * str, | ||
193 | uint64_t * out_val); | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Convert not more then @a maxlen hexadecimal US-ASCII digits in string | ||
198 | * to number in uint64_t. | ||
199 | * Conversion stopped at first non-digit character or after @a maxlen | ||
200 | * digits. | ||
201 | * @param str string to convert | ||
202 | * @param maxlen maximum number of characters to process | ||
203 | * @param out_val pointer to uint64_t to store result of conversion | ||
204 | * @return non-zero number of characters processed on succeed, | ||
205 | * zero if no digit is found, resulting value is larger | ||
206 | * then possible to store in uint64_t or @a out_val is NULL | ||
207 | */ | ||
208 | size_t | ||
209 | MHD_strx_to_uint64_n_ (const char * str, | ||
210 | size_t maxlen, | ||
211 | uint64_t * out_val); | ||
212 | |||
213 | #else /* MHD_FAVOR_SMALL_CODE */ | ||
214 | /* Use one universal function and macros to reduce size */ | ||
215 | |||
216 | /** | ||
217 | * Generic function for converting not more then @a maxlen | ||
218 | * hexadecimal or decimal US-ASCII digits in string to number. | ||
219 | * Conversion stopped at first non-digit character or after @a maxlen | ||
220 | * digits. | ||
221 | * To be used only within macro. | ||
222 | * @param str the string to convert | ||
223 | * @param maxlen the maximum number of characters to process | ||
224 | * @param out_val the pointer to uint64_t to store result of conversion | ||
225 | * @param val_size the size of variable pointed by @a out_val | ||
226 | * @param max_val the maximum decoded number | ||
227 | * @param base the numeric base, 10 or 16 | ||
228 | * @return non-zero number of characters processed on succeed, | ||
229 | * zero if no digit is found, resulting value is larger | ||
230 | * then @ max_val or @a out_val is NULL | ||
231 | */ | ||
232 | size_t | ||
233 | MHD_str_to_uvalue_n_ (const char * str, | ||
234 | size_t maxlen, | ||
235 | void * out_val, | ||
236 | size_t val_size, | ||
237 | uint64_t max_val, | ||
238 | int base); | ||
239 | |||
240 | #define MHD_str_to_uint64_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\ | ||
241 | sizeof(uint64_t),UINT64_MAX,10) | ||
242 | |||
243 | #define MHD_str_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\ | ||
244 | sizeof(uint64_t),UINT64_MAX,10) | ||
245 | |||
246 | #define MHD_strx_to_sizet_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\ | ||
247 | sizeof(size_t),SIZE_MAX,16) | ||
248 | |||
249 | #define MHD_strx_to_sizet_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\ | ||
250 | sizeof(size_t),SIZE_MAX,16) | ||
251 | |||
252 | #define MHD_strx_to_uint32_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\ | ||
253 | sizeof(uint32_t),UINT32_MAX,16) | ||
254 | |||
255 | #define MHD_strx_to_uint32_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\ | ||
256 | sizeof(uint32_t),UINT32_MAX,16) | ||
257 | |||
258 | #define MHD_strx_to_uint64_(s,ov) MHD_str_to_uvalue_n_((s),SIZE_MAX,(ov),\ | ||
259 | sizeof(uint64_t),UINT64_MAX,16) | ||
260 | |||
261 | #define MHD_strx_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_((s),(ml),(ov),\ | ||
262 | sizeof(uint64_t),UINT64_MAX,16) | ||
263 | |||
264 | #endif /* MHD_FAVOR_SMALL_CODE */ | ||
265 | |||
266 | #endif /* MHD_STR_H */ | ||