strparse.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "strparse.h"
  25. #include "../strcase.h"
  26. void curlx_str_init(struct Curl_str *out)
  27. {
  28. out->str = NULL;
  29. out->len = 0;
  30. }
  31. void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
  32. {
  33. out->str = str;
  34. out->len = len;
  35. }
  36. /* Get a word until the first DELIM or end of string. At least one byte long.
  37. return non-zero on error */
  38. int curlx_str_until(const char **linep, struct Curl_str *out,
  39. const size_t max, char delim)
  40. {
  41. const char *s = *linep;
  42. size_t len = 0;
  43. DEBUGASSERT(linep && *linep && out && max && delim);
  44. curlx_str_init(out);
  45. while(*s && (*s != delim)) {
  46. s++;
  47. if(++len > max) {
  48. return STRE_BIG;
  49. }
  50. }
  51. if(!len)
  52. return STRE_SHORT;
  53. out->str = *linep;
  54. out->len = len;
  55. *linep = s; /* point to the first byte after the word */
  56. return STRE_OK;
  57. }
  58. /* Get a word until the first space or end of string. At least one byte long.
  59. return non-zero on error */
  60. int curlx_str_word(const char **linep, struct Curl_str *out,
  61. const size_t max)
  62. {
  63. return curlx_str_until(linep, out, max, ' ');
  64. }
  65. /* Get a word until a newline byte or end of string. At least one byte long.
  66. return non-zero on error */
  67. int curlx_str_untilnl(const char **linep, struct Curl_str *out,
  68. const size_t max)
  69. {
  70. const char *s = *linep;
  71. size_t len = 0;
  72. DEBUGASSERT(linep && *linep && out && max);
  73. curlx_str_init(out);
  74. while(*s && !ISNEWLINE(*s)) {
  75. s++;
  76. if(++len > max)
  77. return STRE_BIG;
  78. }
  79. if(!len)
  80. return STRE_SHORT;
  81. out->str = *linep;
  82. out->len = len;
  83. *linep = s; /* point to the first byte after the word */
  84. return STRE_OK;
  85. }
  86. /* Get a "quoted" word. No escaping possible.
  87. return non-zero on error */
  88. int curlx_str_quotedword(const char **linep, struct Curl_str *out,
  89. const size_t max)
  90. {
  91. const char *s = *linep;
  92. size_t len = 0;
  93. DEBUGASSERT(linep && *linep && out && max);
  94. curlx_str_init(out);
  95. if(*s != '\"')
  96. return STRE_BEGQUOTE;
  97. s++;
  98. while(*s && (*s != '\"')) {
  99. s++;
  100. if(++len > max)
  101. return STRE_BIG;
  102. }
  103. if(*s != '\"')
  104. return STRE_ENDQUOTE;
  105. out->str = (*linep) + 1;
  106. out->len = len;
  107. *linep = s + 1;
  108. return STRE_OK;
  109. }
  110. /* Advance over a single character.
  111. return non-zero on error */
  112. int curlx_str_single(const char **linep, char byte)
  113. {
  114. DEBUGASSERT(linep && *linep);
  115. if(**linep != byte)
  116. return STRE_BYTE;
  117. (*linep)++; /* move over it */
  118. return STRE_OK;
  119. }
  120. /* Advance over a single space.
  121. return non-zero on error */
  122. int curlx_str_singlespace(const char **linep)
  123. {
  124. return curlx_str_single(linep, ' ');
  125. }
  126. /* given an ASCII character and max ascii, return TRUE if valid */
  127. #define valid_digit(x,m) \
  128. (((x) >= '0') && ((x) <= m) && Curl_hexasciitable[(x)-'0'])
  129. /* We use 16 for the zero index (and the necessary bitwise AND in the loop)
  130. to be able to have a non-zero value there to make valid_digit() able to
  131. use the info */
  132. const unsigned char Curl_hexasciitable[] = {
  133. 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 0x30: 0 - 9 */
  134. 0, 0, 0, 0, 0, 0, 0,
  135. 10, 11, 12, 13, 14, 15, /* 0x41: A - F */
  136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137. 10, 11, 12, 13, 14, 15 /* 0x61: a - f */
  138. };
  139. /* no support for 0x prefix nor leading spaces */
  140. static int str_num_base(const char **linep, curl_off_t *nump, curl_off_t max,
  141. int base) /* 8, 10 or 16, nothing else */
  142. {
  143. curl_off_t num = 0;
  144. const char *p;
  145. int m = (base == 10) ? '9' : /* the largest digit possible */
  146. (base == 16) ? 'f' : '7';
  147. DEBUGASSERT(linep && *linep && nump);
  148. DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
  149. DEBUGASSERT(max >= 0); /* mostly to catch SIZE_T_MAX, which is too large */
  150. *nump = 0;
  151. p = *linep;
  152. if(!valid_digit(*p, m))
  153. return STRE_NO_NUM;
  154. if(max < base) {
  155. /* special-case low max scenario because check needs to be different */
  156. do {
  157. int n = Curl_hexval(*p++);
  158. num = num * base + n;
  159. if(num > max)
  160. return STRE_OVERFLOW;
  161. } while(valid_digit(*p, m));
  162. }
  163. else {
  164. do {
  165. int n = Curl_hexval(*p++);
  166. if(num > ((max - n) / base))
  167. return STRE_OVERFLOW;
  168. num = num * base + n;
  169. } while(valid_digit(*p, m));
  170. }
  171. *nump = num;
  172. *linep = p;
  173. return STRE_OK;
  174. }
  175. /* Get an unsigned decimal number with no leading space or minus. Leading
  176. zeroes are accepted. return non-zero on error */
  177. int curlx_str_number(const char **linep, curl_off_t *nump, curl_off_t max)
  178. {
  179. return str_num_base(linep, nump, max, 10);
  180. }
  181. /* Get an unsigned hexadecimal number with no leading space or minus and no
  182. "0x" support. Leading zeroes are accepted. return non-zero on error */
  183. int curlx_str_hex(const char **linep, curl_off_t *nump, curl_off_t max)
  184. {
  185. return str_num_base(linep, nump, max, 16);
  186. }
  187. /* Get an unsigned octal number with no leading space or minus and no "0"
  188. prefix support. Leading zeroes are accepted. return non-zero on error */
  189. int curlx_str_octal(const char **linep, curl_off_t *nump, curl_off_t max)
  190. {
  191. return str_num_base(linep, nump, max, 8);
  192. }
  193. /*
  194. * Parse a positive number up to 63-bit number written in ASCII. Skip leading
  195. * blanks. No support for prefixes.
  196. */
  197. int curlx_str_numblanks(const char **str, curl_off_t *num)
  198. {
  199. curlx_str_passblanks(str);
  200. return curlx_str_number(str, num, CURL_OFF_T_MAX);
  201. }
  202. /* CR or LF
  203. return non-zero on error */
  204. int curlx_str_newline(const char **linep)
  205. {
  206. DEBUGASSERT(linep && *linep);
  207. if(ISNEWLINE(**linep)) {
  208. (*linep)++;
  209. return STRE_OK; /* yessir */
  210. }
  211. return STRE_NEWLINE;
  212. }
  213. #ifndef WITHOUT_LIBCURL
  214. /* case insensitive compare that the parsed string matches the given string.
  215. Returns non-zero on match. */
  216. int curlx_str_casecompare(struct Curl_str *str, const char *check)
  217. {
  218. size_t clen = check ? strlen(check) : 0;
  219. return ((str->len == clen) && strncasecompare(str->str, check, clen));
  220. }
  221. #endif
  222. /* case sensitive string compare. Returns non-zero on match. */
  223. int curlx_str_cmp(struct Curl_str *str, const char *check)
  224. {
  225. if(check) {
  226. size_t clen = strlen(check);
  227. return ((str->len == clen) && !strncmp(str->str, check, clen));
  228. }
  229. return !!(str->len);
  230. }
  231. /* Trim off 'num' number of bytes from the beginning (left side) of the
  232. string. If 'num' is larger than the string, return error. */
  233. int curlx_str_nudge(struct Curl_str *str, size_t num)
  234. {
  235. if(num <= str->len) {
  236. str->str += num;
  237. str->len -= num;
  238. return STRE_OK;
  239. }
  240. return STRE_OVERFLOW;
  241. }
  242. /* Get the following character sequence that consists only of bytes not
  243. present in the 'reject' string. Like strcspn(). */
  244. int curlx_str_cspn(const char **linep, struct Curl_str *out,
  245. const char *reject)
  246. {
  247. const char *s = *linep;
  248. size_t len;
  249. DEBUGASSERT(linep && *linep);
  250. len = strcspn(s, reject);
  251. if(len) {
  252. out->str = s;
  253. out->len = len;
  254. *linep = &s[len];
  255. return STRE_OK;
  256. }
  257. curlx_str_init(out);
  258. return STRE_SHORT;
  259. }
  260. /* remove ISBLANK()s from both ends of the string */
  261. void curlx_str_trimblanks(struct Curl_str *out)
  262. {
  263. while(out->len && ISBLANK(*out->str))
  264. curlx_str_nudge(out, 1);
  265. /* trim trailing spaces and tabs */
  266. while(out->len && ISBLANK(out->str[out->len - 1]))
  267. out->len--;
  268. }
  269. /* increase the pointer until it has moved over all blanks */
  270. void curlx_str_passblanks(const char **linep)
  271. {
  272. while(ISBLANK(**linep))
  273. (*linep)++; /* move over it */
  274. }