scanf.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2000-2002 Opsycon AB (www.opsycon.se)
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. All advertising materials mentioning features or use of this software
  13. * must display the following acknowledgement:
  14. * This product includes software developed by Opsycon AB.
  15. * 4. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. */
  31. #include <AK/Assertions.h>
  32. #include <ctype.h>
  33. #include <stdarg.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. static const char* determine_base(const char* p, int& base)
  38. {
  39. if (p[0] == '0') {
  40. switch (p[1]) {
  41. case 'x':
  42. base = 16;
  43. break;
  44. case 't':
  45. case 'n':
  46. base = 10;
  47. break;
  48. case 'o':
  49. base = 8;
  50. break;
  51. default:
  52. base = 10;
  53. return p;
  54. }
  55. return p + 2;
  56. }
  57. base = 10;
  58. return p;
  59. }
  60. static int _atob(unsigned long* vp, const char* p, int base)
  61. {
  62. unsigned long value, v1, v2;
  63. const char* q;
  64. char tmp[20];
  65. int digit;
  66. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
  67. base = 16;
  68. p += 2;
  69. }
  70. if (base == 16 && (q = strchr(p, '.')) != 0) {
  71. if (q - p > (ssize_t)sizeof(tmp) - 1)
  72. return 0;
  73. memcpy(tmp, p, q - p);
  74. tmp[q - p] = '\0';
  75. if (!_atob(&v1, tmp, 16))
  76. return 0;
  77. ++q;
  78. if (strchr(q, '.'))
  79. return 0;
  80. if (!_atob(&v2, q, 16))
  81. return 0;
  82. *vp = (v1 << 16) + v2;
  83. return 1;
  84. }
  85. value = *vp = 0;
  86. for (; *p; p++) {
  87. if (*p >= '0' && *p <= '9')
  88. digit = *p - '0';
  89. else if (*p >= 'a' && *p <= 'f')
  90. digit = *p - 'a' + 10;
  91. else if (*p >= 'A' && *p <= 'F')
  92. digit = *p - 'A' + 10;
  93. else
  94. return 0;
  95. if (digit >= base)
  96. return 0;
  97. value *= base;
  98. value += digit;
  99. }
  100. *vp = value;
  101. return 1;
  102. }
  103. static int atob(unsigned int* vp, const char* p, int base)
  104. {
  105. unsigned long v;
  106. if (base == 0)
  107. p = determine_base(p, base);
  108. if (_atob(&v, p, base)) {
  109. *vp = v;
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. #define ISSPACE " \t\n\r\f\v"
  115. int vsscanf(const char* buf, const char* s, va_list ap)
  116. {
  117. int base = 10;
  118. char* t;
  119. char tmp[BUFSIZ];
  120. bool noassign = false;
  121. int count = 0;
  122. int width = 0;
  123. // FIXME: This doesn't work quite right. For example, it fails to match 'SSH-2.0-OpenSSH_8.2p1 Ubuntu-4ubuntu0.1\r\n'
  124. // with 'SSH-%d.%d-%[^\n]\n'
  125. while (*s && *buf) {
  126. while (isspace(*s))
  127. s++;
  128. if (*s == '%') {
  129. s++;
  130. for (; *s; s++) {
  131. if (strchr("dibouxcsefg%", *s))
  132. break;
  133. if (*s == '*')
  134. noassign = true;
  135. else if (*s >= '1' && *s <= '9') {
  136. const char* tc;
  137. for (tc = s; isdigit(*s); s++)
  138. ;
  139. ASSERT((ssize_t)sizeof(tmp) >= s - tc + 1);
  140. memcpy(tmp, tc, s - tc);
  141. tmp[s - tc] = '\0';
  142. atob((uint32_t*)&width, tmp, 10);
  143. s--;
  144. }
  145. }
  146. if (*s == 's') {
  147. while (isspace(*buf))
  148. buf++;
  149. if (!width)
  150. width = strcspn(buf, ISSPACE);
  151. if (!noassign) {
  152. // In this case, we have no way to ensure the user buffer is not overflown :(
  153. memcpy(t = va_arg(ap, char*), buf, width);
  154. t[width] = '\0';
  155. }
  156. buf += width;
  157. } else if (*s == 'c') {
  158. if (!width)
  159. width = 1;
  160. if (!noassign) {
  161. memcpy(t = va_arg(ap, char*), buf, width);
  162. // No null terminator!
  163. }
  164. buf += width;
  165. } else if (strchr("dobxu", *s)) {
  166. while (isspace(*buf))
  167. buf++;
  168. if (*s == 'd' || *s == 'u')
  169. base = 10;
  170. else if (*s == 'x')
  171. base = 16;
  172. else if (*s == 'o')
  173. base = 8;
  174. else if (*s == 'b')
  175. base = 2;
  176. if (!width) {
  177. if (isspace(*(s + 1)) || *(s + 1) == 0) {
  178. width = strcspn(buf, ISSPACE);
  179. } else {
  180. auto* p = strchr(buf, *(s + 1));
  181. if (p)
  182. width = p - buf;
  183. else {
  184. noassign = true;
  185. width = 0;
  186. }
  187. }
  188. }
  189. memcpy(tmp, buf, width);
  190. tmp[width] = '\0';
  191. buf += width;
  192. if (!noassign) {
  193. if (!atob(va_arg(ap, uint32_t*), tmp, base))
  194. noassign = true;
  195. }
  196. }
  197. if (!noassign)
  198. ++count;
  199. width = 0;
  200. noassign = false;
  201. ++s;
  202. } else {
  203. while (isspace(*buf))
  204. buf++;
  205. if (*s != *buf)
  206. break;
  207. else {
  208. ++s;
  209. ++buf;
  210. }
  211. }
  212. }
  213. return count;
  214. }