GNU libmicrohttpd  1.0.1
mhd_str.h
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2015-2023 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 
26 #ifndef MHD_STR_H
27 #define MHD_STR_H 1
28 
29 #include "mhd_options.h"
30 #include <stdint.h>
31 #ifdef HAVE_STDDEF_H
32 #include <stddef.h>
33 #endif /* HAVE_STDDEF_H */
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif /* HAVE_SYS_TYPES_H */
37 #ifdef HAVE_STDBOOL_H
38 #include <stdbool.h>
39 #endif /* HAVE_STDBOOL_H */
40 
41 #include "mhd_str_types.h"
42 
43 #if defined(_MSC_FULL_VER) && ! defined(_SSIZE_T_DEFINED)
44 #define _SSIZE_T_DEFINED
45 typedef intptr_t ssize_t;
46 #endif /* !_SSIZE_T_DEFINED */
47 
48 #ifdef MHD_FAVOR_SMALL_CODE
49 #include "mhd_limits.h"
50 #endif /* MHD_FAVOR_SMALL_CODE */
51 
52 /*
53  * Block of functions/macros that use US-ASCII charset as required by HTTP
54  * standards. Not affected by current locale settings.
55  */
56 
57 #ifndef MHD_FAVOR_SMALL_CODE
65 int
66 MHD_str_equal_caseless_ (const char *str1,
67  const char *str2);
68 
69 #else /* MHD_FAVOR_SMALL_CODE */
70 /* Reuse MHD_str_equal_caseless_n_() to reduce size */
71 #define MHD_str_equal_caseless_(s1,s2) MHD_str_equal_caseless_n_ ((s1),(s2), \
72  SIZE_MAX)
73 #endif /* MHD_FAVOR_SMALL_CODE */
74 
75 
86 int
87 MHD_str_equal_caseless_n_ (const char *const str1,
88  const char *const str2,
89  size_t maxlen);
90 
91 
102 bool
103 MHD_str_equal_caseless_bin_n_ (const char *const str1,
104  const char *const str2,
105  size_t len);
106 
107 
122 #define MHD_str_equal_caseless_s_bin_n_(a,s,l) \
123  ((MHD_STATICSTR_LEN_(a) == (l)) \
124  && MHD_str_equal_caseless_bin_n_(a,s,l))
125 
139 bool
140 MHD_str_has_token_caseless_ (const char *str,
141  const char *const token,
142  size_t token_len);
143 
154 #define MHD_str_has_s_token_caseless_(str,tkn) \
155  MHD_str_has_token_caseless_ ((str),(tkn),MHD_STATICSTR_LEN_ (tkn))
156 
157 
186 bool
187 MHD_str_remove_token_caseless_ (const char *str,
188  size_t str_len,
189  const char *const token,
190  const size_t token_len,
191  char *buf,
192  ssize_t *buf_size);
193 
194 
218 bool
220  size_t *str_len,
221  const char *const tokens,
222  const size_t tokens_len);
223 
224 
225 #ifndef MHD_FAVOR_SMALL_CODE
226 /* Use individual function for each case to improve speed */
227 
238 size_t
239 MHD_str_to_uint64_ (const char *str,
240  uint64_t *out_val);
241 
255 size_t
256 MHD_str_to_uint64_n_ (const char *str,
257  size_t maxlen,
258  uint64_t *out_val);
259 
260 
271 size_t
272 MHD_strx_to_uint32_ (const char *str,
273  uint32_t *out_val);
274 
275 
289 size_t
290 MHD_strx_to_uint32_n_ (const char *str,
291  size_t maxlen,
292  uint32_t *out_val);
293 
294 
305 size_t
306 MHD_strx_to_uint64_ (const char *str,
307  uint64_t *out_val);
308 
309 
323 size_t
324 MHD_strx_to_uint64_n_ (const char *str,
325  size_t maxlen,
326  uint64_t *out_val);
327 
328 #else /* MHD_FAVOR_SMALL_CODE */
329 /* Use one universal function and macros to reduce size */
330 
348 size_t
349 MHD_str_to_uvalue_n_ (const char *str,
350  size_t maxlen,
351  void *out_val,
352  size_t val_size,
353  uint64_t max_val,
354  unsigned int base);
355 
356 #define MHD_str_to_uint64_(s,ov) MHD_str_to_uvalue_n_ ((s),SIZE_MAX,(ov), \
357  sizeof(uint64_t), \
358  UINT64_MAX,10)
359 
360 #define MHD_str_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_ ((s),(ml),(ov), \
361  sizeof(uint64_t), \
362  UINT64_MAX,10)
363 
364 #define MHD_strx_to_sizet_(s,ov) MHD_str_to_uvalue_n_ ((s),SIZE_MAX,(ov), \
365  sizeof(size_t),SIZE_MAX, \
366  16)
367 
368 #define MHD_strx_to_sizet_n_(s,ml,ov) MHD_str_to_uvalue_n_ ((s),(ml),(ov), \
369  sizeof(size_t), \
370  SIZE_MAX,16)
371 
372 #define MHD_strx_to_uint32_(s,ov) MHD_str_to_uvalue_n_ ((s),SIZE_MAX,(ov), \
373  sizeof(uint32_t), \
374  UINT32_MAX,16)
375 
376 #define MHD_strx_to_uint32_n_(s,ml,ov) MHD_str_to_uvalue_n_ ((s),(ml),(ov), \
377  sizeof(uint32_t), \
378  UINT32_MAX,16)
379 
380 #define MHD_strx_to_uint64_(s,ov) MHD_str_to_uvalue_n_ ((s),SIZE_MAX,(ov), \
381  sizeof(uint64_t), \
382  UINT64_MAX,16)
383 
384 #define MHD_strx_to_uint64_n_(s,ml,ov) MHD_str_to_uvalue_n_ ((s),(ml),(ov), \
385  sizeof(uint64_t), \
386  UINT64_MAX,16)
387 
388 #endif /* MHD_FAVOR_SMALL_CODE */
389 
390 
400 size_t
401 MHD_uint32_to_strx (uint32_t val,
402  char *buf,
403  size_t buf_size);
404 
405 
406 #ifndef MHD_FAVOR_SMALL_CODE
416 size_t
417 MHD_uint16_to_str (uint16_t val,
418  char *buf,
419  size_t buf_size);
420 
421 #else /* MHD_FAVOR_SMALL_CODE */
422 #define MHD_uint16_to_str(v,b,s) MHD_uint64_to_str(v,b,s)
423 #endif /* MHD_FAVOR_SMALL_CODE */
424 
425 
435 size_t
436 MHD_uint64_to_str (uint64_t val,
437  char *buf,
438  size_t buf_size);
439 
440 
456 size_t
457 MHD_uint8_to_str_pad (uint8_t val,
458  uint8_t min_digits,
459  char *buf,
460  size_t buf_size);
461 
462 
472 size_t
473 MHD_bin_to_hex (const void *bin,
474  size_t size,
475  char *hex);
476 
486 size_t
487 MHD_bin_to_hex_z (const void *bin,
488  size_t size,
489  char *hex);
490 
504 size_t
505 MHD_hex_to_bin (const char *hex,
506  size_t len,
507  void *bin);
508 
527 size_t
528 MHD_str_pct_decode_strict_n_ (const char *pct_encoded,
529  size_t pct_encoded_len,
530  char *decoded,
531  size_t buf_size);
532 
555 size_t
556 MHD_str_pct_decode_lenient_n_ (const char *pct_encoded,
557  size_t pct_encoded_len,
558  char *decoded,
559  size_t buf_size,
560  bool *broken_encoding);
561 
562 
576 size_t
578 
579 
598 size_t
600  bool *broken_encoding);
601 
602 #ifdef DAUTH_SUPPORT
622 bool
623 MHD_str_equal_quoted_bin_n (const char *quoted,
624  size_t quoted_len,
625  const char *unquoted,
626  size_t unquoted_len);
627 
644 #define MHD_str_equal_quoted_s_bin_n(q,l,u) \
645  MHD_str_equal_quoted_bin_n(q,l,u,MHD_STATICSTR_LEN_(u))
646 
667 bool
668 MHD_str_equal_caseless_quoted_bin_n (const char *quoted,
669  size_t quoted_len,
670  const char *unquoted,
671  size_t unquoted_len);
672 
690 #define MHD_str_equal_caseless_quoted_s_bin_n(q,l,u) \
691  MHD_str_equal_caseless_quoted_bin_n(q,l,u,MHD_STATICSTR_LEN_(u))
692 
708 size_t
709 MHD_str_unquote (const char *quoted,
710  size_t quoted_len,
711  char *result);
712 
713 #endif /* DAUTH_SUPPORT */
714 
715 #if defined(DAUTH_SUPPORT) || defined(BAUTH_SUPPORT)
716 
732 size_t
733 MHD_str_quote (const char *unquoted,
734  size_t unquoted_len,
735  char *result,
736  size_t buf_size);
737 
738 #endif /* DAUTH_SUPPORT || BAUTH_SUPPORT */
739 
740 #ifdef BAUTH_SUPPORT
741 
750 #define MHD_base64_max_dec_size_(enc_size) (((enc_size) / 4) * 3)
751 
769 size_t
770 MHD_base64_to_bin_n (const char *base64,
771  size_t base64_len,
772  void *bin,
773  size_t bin_size);
774 
775 #endif /* BAUTH_SUPPORT */
776 
777 #endif /* MHD_STR_H */
int MHD_str_equal_caseless_(const char *str1, const char *str2)
Definition: mhd_str.c:346
size_t MHD_strx_to_uint32_(const char *str, uint32_t *out_val)
Definition: mhd_str.c:558
size_t MHD_str_to_uint64_n_(const char *str, size_t maxlen, uint64_t *out_val)
Definition: mhd_str.c:515
size_t MHD_strx_to_uint64_n_(const char *str, size_t maxlen, uint64_t *out_val)
Definition: mhd_str.c:692
int MHD_str_equal_caseless_n_(const char *const str1, const char *const str2, size_t maxlen)
Definition: mhd_str.c:378
size_t MHD_str_to_uint64_(const char *str, uint64_t *out_val)
Definition: mhd_str.c:473
bool MHD_str_has_token_caseless_(const char *str, const char *const token, size_t token_len)
Definition: mhd_str.c:412
size_t MHD_strx_to_uint64_(const char *str, uint64_t *out_val)
Definition: mhd_str.c:646
size_t MHD_strx_to_uint32_n_(const char *str, size_t maxlen, uint32_t *out_val)
Definition: mhd_str.c:605
additional automatic macros for MHD_config.h
Header for string manipulating helpers types.
size_t MHD_bin_to_hex(const void *bin, size_t size, char *hex)
Definition: mhd_str.c:1676
size_t MHD_str_pct_decode_strict_n_(const char *pct_encoded, size_t pct_encoded_len, char *decoded, size_t buf_size)
Definition: mhd_str.c:1746
size_t MHD_bin_to_hex_z(const void *bin, size_t size, char *hex)
Definition: mhd_str.c:1696
size_t MHD_uint8_to_str_pad(uint8_t val, uint8_t min_digits, char *buf, size_t buf_size)
Definition: mhd_str.c:1629
bool MHD_str_remove_tokens_caseless_(char *str, size_t *str_len, const char *const tokens, const size_t tokens_len)
Definition: mhd_str.c:1033
size_t MHD_str_pct_decode_lenient_n_(const char *pct_encoded, size_t pct_encoded_len, char *decoded, size_t buf_size, bool *broken_encoding)
Definition: mhd_str.c:1829
size_t MHD_uint16_to_str(uint16_t val, char *buf, size_t buf_size)
Definition: mhd_str.c:1550
size_t MHD_str_pct_decode_in_place_lenient_(char *str, bool *broken_encoding)
Definition: mhd_str.c:1984
size_t MHD_uint64_to_str(uint64_t val, char *buf, size_t buf_size)
Definition: mhd_str.c:1591
size_t MHD_str_pct_decode_in_place_strict_(char *str)
Definition: mhd_str.c:1928
bool MHD_str_remove_token_caseless_(const char *str, size_t str_len, const char *const token, const size_t token_len, char *buf, ssize_t *buf_size)
Definition: mhd_str.c:857
bool MHD_str_equal_caseless_bin_n_(const char *const str1, const char *const str2, size_t len)
Definition: mhd_str.c:749
size_t MHD_uint32_to_strx(uint32_t val, char *buf, size_t buf_size)
Definition: mhd_str.c:1516
size_t MHD_hex_to_bin(const char *hex, size_t len, void *bin)
Definition: mhd_str.c:1710