strparse.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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. /* Get a word until the first DELIM or end of string. At least one byte long.
  26. return non-zero on error */
  27. int Curl_str_until(char **linep, struct Curl_str *out,
  28. const size_t max, char delim)
  29. {
  30. char *s = *linep;
  31. size_t len = 0;
  32. DEBUGASSERT(linep && *linep && out && max && delim);
  33. out->str = NULL;
  34. out->len = 0;
  35. while(*s && (*s != delim)) {
  36. s++;
  37. if(++len > max) {
  38. return STRE_BIG;
  39. }
  40. }
  41. if(!len)
  42. return STRE_SHORT;
  43. out->str = *linep;
  44. out->len = len;
  45. *linep = s; /* point to the first byte after the word */
  46. return STRE_OK;
  47. }
  48. /* Get a word until the first space or end of string. At least one byte long.
  49. return non-zero on error */
  50. int Curl_str_word(char **linep, struct Curl_str *out,
  51. const size_t max)
  52. {
  53. return Curl_str_until(linep, out, max, ' ');
  54. }
  55. /* Get a "quoted" word. No escaping possible.
  56. return non-zero on error */
  57. int Curl_str_quotedword(char **linep, struct Curl_str *out,
  58. const size_t max)
  59. {
  60. char *s = *linep;
  61. size_t len = 0;
  62. DEBUGASSERT(linep && *linep && out && max);
  63. out->str = NULL;
  64. out->len = 0;
  65. if(*s != '\"')
  66. return STRE_BEGQUOTE;
  67. s++;
  68. while(*s && (*s != '\"')) {
  69. s++;
  70. if(++len > max)
  71. return STRE_BIG;
  72. }
  73. if(*s != '\"')
  74. return STRE_ENDQUOTE;
  75. out->str = (*linep) + 1;
  76. out->len = len;
  77. *linep = s + 1;
  78. return STRE_OK;
  79. }
  80. /* Advance over a single character.
  81. return non-zero on error */
  82. int Curl_str_single(char **linep, char byte)
  83. {
  84. DEBUGASSERT(linep && *linep);
  85. if(**linep != byte)
  86. return STRE_BYTE;
  87. (*linep)++; /* move over it */
  88. return STRE_OK;
  89. }
  90. /* Advance over a single space.
  91. return non-zero on error */
  92. int Curl_str_singlespace(char **linep)
  93. {
  94. return Curl_str_single(linep, ' ');
  95. }
  96. /* Get an unsigned number. Leading zeroes are accepted.
  97. return non-zero on error */
  98. int Curl_str_number(char **linep, size_t *nump, size_t max)
  99. {
  100. size_t num = 0;
  101. DEBUGASSERT(linep && *linep && nump);
  102. *nump = 0;
  103. while(ISDIGIT(**linep)) {
  104. int n = **linep - '0';
  105. if(num > ((SIZE_T_MAX - n) / 10))
  106. return STRE_OVERFLOW;
  107. num = num * 10 + n;
  108. if(num > max)
  109. return STRE_BIG; /** too big */
  110. (*linep)++;
  111. }
  112. *nump = num;
  113. return STRE_OK;
  114. }
  115. /* CR or LF
  116. return non-zero on error */
  117. int Curl_str_newline(char **linep)
  118. {
  119. DEBUGASSERT(linep && *linep);
  120. if(ISNEWLINE(**linep)) {
  121. (*linep)++;
  122. return STRE_OK; /* yessir */
  123. }
  124. return STRE_NEWLINE;
  125. }