util_string.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/string/util_string.h"
  6. #include "lib/string/compat_ctype.h"
  7. #include "lib/err/torerr.h"
  8. #include "lib/ctime/di_ops.h"
  9. #include "lib/defs/digest_sizes.h"
  10. #include <string.h>
  11. #include <stdlib.h>
  12. /** Given <b>hlen</b> bytes at <b>haystack</b> and <b>nlen</b> bytes at
  13. * <b>needle</b>, return a pointer to the first occurrence of the needle
  14. * within the haystack, or NULL if there is no such occurrence.
  15. *
  16. * This function is <em>not</em> timing-safe.
  17. *
  18. * Requires that <b>nlen</b> be greater than zero.
  19. */
  20. const void *
  21. tor_memmem(const void *_haystack, size_t hlen,
  22. const void *_needle, size_t nlen)
  23. {
  24. #if defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2)
  25. raw_assert(nlen);
  26. return memmem(_haystack, hlen, _needle, nlen);
  27. #else
  28. /* This isn't as fast as the GLIBC implementation, but it doesn't need to
  29. * be. */
  30. const char *p, *last_possible_start;
  31. const char *haystack = (const char*)_haystack;
  32. const char *needle = (const char*)_needle;
  33. char first;
  34. raw_assert(nlen);
  35. if (nlen > hlen)
  36. return NULL;
  37. p = haystack;
  38. /* Last position at which the needle could start. */
  39. last_possible_start = haystack + hlen - nlen;
  40. first = *(const char*)needle;
  41. while ((p = memchr(p, first, last_possible_start + 1 - p))) {
  42. if (fast_memeq(p, needle, nlen))
  43. return p;
  44. if (++p > last_possible_start) {
  45. /* This comparison shouldn't be necessary, since if p was previously
  46. * equal to last_possible_start, the next memchr call would be
  47. * "memchr(p, first, 0)", which will return NULL. But it clarifies the
  48. * logic. */
  49. return NULL;
  50. }
  51. }
  52. return NULL;
  53. #endif /* defined(HAVE_MEMMEM) && (!defined(__GNUC__) || __GNUC__ >= 2) */
  54. }
  55. const void *
  56. tor_memstr(const void *haystack, size_t hlen, const char *needle)
  57. {
  58. return tor_memmem(haystack, hlen, needle, strlen(needle));
  59. }
  60. /** Return true iff the 'len' bytes at 'mem' are all zero. */
  61. int
  62. tor_mem_is_zero(const char *mem, size_t len)
  63. {
  64. static const char ZERO[] = {
  65. 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,0,0, 0,0,0,0,
  66. };
  67. while (len >= sizeof(ZERO)) {
  68. /* It's safe to use fast_memcmp here, since the very worst thing an
  69. * attacker could learn is how many initial bytes of a secret were zero */
  70. if (fast_memcmp(mem, ZERO, sizeof(ZERO)))
  71. return 0;
  72. len -= sizeof(ZERO);
  73. mem += sizeof(ZERO);
  74. }
  75. /* Deal with leftover bytes. */
  76. if (len)
  77. return fast_memeq(mem, ZERO, len);
  78. return 1;
  79. }
  80. /** Return true iff the DIGEST_LEN bytes in digest are all zero. */
  81. int
  82. tor_digest_is_zero(const char *digest)
  83. {
  84. static const uint8_t ZERO_DIGEST[] = {
  85. 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
  86. };
  87. return tor_memeq(digest, ZERO_DIGEST, DIGEST_LEN);
  88. }
  89. /** Return true iff the DIGEST256_LEN bytes in digest are all zero. */
  90. int
  91. tor_digest256_is_zero(const char *digest)
  92. {
  93. return tor_mem_is_zero(digest, DIGEST256_LEN);
  94. }
  95. /** Remove from the string <b>s</b> every character which appears in
  96. * <b>strip</b>. */
  97. void
  98. tor_strstrip(char *s, const char *strip)
  99. {
  100. char *readp = s;
  101. while (*readp) {
  102. if (strchr(strip, *readp)) {
  103. ++readp;
  104. } else {
  105. *s++ = *readp++;
  106. }
  107. }
  108. *s = '\0';
  109. }
  110. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  111. * lowercase. */
  112. void
  113. tor_strlower(char *s)
  114. {
  115. while (*s) {
  116. *s = TOR_TOLOWER(*s);
  117. ++s;
  118. }
  119. }
  120. /** Convert all alphabetic characters in the nul-terminated string <b>s</b> to
  121. * lowercase. */
  122. void
  123. tor_strupper(char *s)
  124. {
  125. while (*s) {
  126. *s = TOR_TOUPPER(*s);
  127. ++s;
  128. }
  129. }
  130. /** Return 1 if every character in <b>s</b> is printable, else return 0.
  131. */
  132. int
  133. tor_strisprint(const char *s)
  134. {
  135. while (*s) {
  136. if (!TOR_ISPRINT(*s))
  137. return 0;
  138. s++;
  139. }
  140. return 1;
  141. }
  142. /** Return 1 if no character in <b>s</b> is uppercase, else return 0.
  143. */
  144. int
  145. tor_strisnonupper(const char *s)
  146. {
  147. while (*s) {
  148. if (TOR_ISUPPER(*s))
  149. return 0;
  150. s++;
  151. }
  152. return 1;
  153. }
  154. /** Return true iff every character in <b>s</b> is whitespace space; else
  155. * return false. */
  156. int
  157. tor_strisspace(const char *s)
  158. {
  159. while (*s) {
  160. if (!TOR_ISSPACE(*s))
  161. return 0;
  162. s++;
  163. }
  164. return 1;
  165. }
  166. /** As strcmp, except that either string may be NULL. The NULL string is
  167. * considered to be before any non-NULL string. */
  168. int
  169. strcmp_opt(const char *s1, const char *s2)
  170. {
  171. if (!s1) {
  172. if (!s2)
  173. return 0;
  174. else
  175. return -1;
  176. } else if (!s2) {
  177. return 1;
  178. } else {
  179. return strcmp(s1, s2);
  180. }
  181. }
  182. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  183. * strcmp.
  184. */
  185. int
  186. strcmpstart(const char *s1, const char *s2)
  187. {
  188. size_t n = strlen(s2);
  189. return strncmp(s1, s2, n);
  190. }
  191. /** Compare the s1_len-byte string <b>s1</b> with <b>s2</b>,
  192. * without depending on a terminating nul in s1. Sorting order is first by
  193. * length, then lexically; return values are as for strcmp.
  194. */
  195. int
  196. strcmp_len(const char *s1, const char *s2, size_t s1_len)
  197. {
  198. size_t s2_len = strlen(s2);
  199. if (s1_len < s2_len)
  200. return -1;
  201. if (s1_len > s2_len)
  202. return 1;
  203. return fast_memcmp(s1, s2, s2_len);
  204. }
  205. /** Compares the first strlen(s2) characters of s1 with s2. Returns as for
  206. * strcasecmp.
  207. */
  208. int
  209. strcasecmpstart(const char *s1, const char *s2)
  210. {
  211. size_t n = strlen(s2);
  212. return strncasecmp(s1, s2, n);
  213. }
  214. /** Compare the value of the string <b>prefix</b> with the start of the
  215. * <b>memlen</b>-byte memory chunk at <b>mem</b>. Return as for strcmp.
  216. *
  217. * [As fast_memcmp(mem, prefix, strlen(prefix)) but returns -1 if memlen is
  218. * less than strlen(prefix).]
  219. */
  220. int
  221. fast_memcmpstart(const void *mem, size_t memlen,
  222. const char *prefix)
  223. {
  224. size_t plen = strlen(prefix);
  225. if (memlen < plen)
  226. return -1;
  227. return fast_memcmp(mem, prefix, plen);
  228. }
  229. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  230. * strcmp.
  231. */
  232. int
  233. strcmpend(const char *s1, const char *s2)
  234. {
  235. size_t n1 = strlen(s1), n2 = strlen(s2);
  236. if (n2>n1)
  237. return strcmp(s1,s2);
  238. else
  239. return strncmp(s1+(n1-n2), s2, n2);
  240. }
  241. /** Compares the last strlen(s2) characters of s1 with s2. Returns as for
  242. * strcasecmp.
  243. */
  244. int
  245. strcasecmpend(const char *s1, const char *s2)
  246. {
  247. size_t n1 = strlen(s1), n2 = strlen(s2);
  248. if (n2>n1) /* then they can't be the same; figure out which is bigger */
  249. return strcasecmp(s1,s2);
  250. else
  251. return strncasecmp(s1+(n1-n2), s2, n2);
  252. }
  253. /** Return a pointer to the first char of s that is not whitespace and
  254. * not a comment, or to the terminating NUL if no such character exists.
  255. */
  256. const char *
  257. eat_whitespace(const char *s)
  258. {
  259. raw_assert(s);
  260. while (1) {
  261. switch (*s) {
  262. case '\0':
  263. default:
  264. return s;
  265. case ' ':
  266. case '\t':
  267. case '\n':
  268. case '\r':
  269. ++s;
  270. break;
  271. case '#':
  272. ++s;
  273. while (*s && *s != '\n')
  274. ++s;
  275. }
  276. }
  277. }
  278. /** Return a pointer to the first char of s that is not whitespace and
  279. * not a comment, or to the terminating NUL if no such character exists.
  280. */
  281. const char *
  282. eat_whitespace_eos(const char *s, const char *eos)
  283. {
  284. raw_assert(s);
  285. raw_assert(eos && s <= eos);
  286. while (s < eos) {
  287. switch (*s) {
  288. case '\0':
  289. default:
  290. return s;
  291. case ' ':
  292. case '\t':
  293. case '\n':
  294. case '\r':
  295. ++s;
  296. break;
  297. case '#':
  298. ++s;
  299. while (s < eos && *s && *s != '\n')
  300. ++s;
  301. }
  302. }
  303. return s;
  304. }
  305. /** Return a pointer to the first char of s that is not a space or a tab
  306. * or a \\r, or to the terminating NUL if no such character exists. */
  307. const char *
  308. eat_whitespace_no_nl(const char *s)
  309. {
  310. while (*s == ' ' || *s == '\t' || *s == '\r')
  311. ++s;
  312. return s;
  313. }
  314. /** As eat_whitespace_no_nl, but stop at <b>eos</b> whether we have
  315. * found a non-whitespace character or not. */
  316. const char *
  317. eat_whitespace_eos_no_nl(const char *s, const char *eos)
  318. {
  319. while (s < eos && (*s == ' ' || *s == '\t' || *s == '\r'))
  320. ++s;
  321. return s;
  322. }
  323. /** Return a pointer to the first char of s that is whitespace or <b>#</b>,
  324. * or to the terminating NUL if no such character exists.
  325. */
  326. const char *
  327. find_whitespace(const char *s)
  328. {
  329. /* tor_assert(s); */
  330. while (1) {
  331. switch (*s)
  332. {
  333. case '\0':
  334. case '#':
  335. case ' ':
  336. case '\r':
  337. case '\n':
  338. case '\t':
  339. return s;
  340. default:
  341. ++s;
  342. }
  343. }
  344. }
  345. /** As find_whitespace, but stop at <b>eos</b> whether we have found a
  346. * whitespace or not. */
  347. const char *
  348. find_whitespace_eos(const char *s, const char *eos)
  349. {
  350. /* tor_assert(s); */
  351. while (s < eos) {
  352. switch (*s)
  353. {
  354. case '\0':
  355. case '#':
  356. case ' ':
  357. case '\r':
  358. case '\n':
  359. case '\t':
  360. return s;
  361. default:
  362. ++s;
  363. }
  364. }
  365. return s;
  366. }
  367. /** Return the first occurrence of <b>needle</b> in <b>haystack</b> that
  368. * occurs at the start of a line (that is, at the beginning of <b>haystack</b>
  369. * or immediately after a newline). Return NULL if no such string is found.
  370. */
  371. const char *
  372. find_str_at_start_of_line(const char *haystack, const char *needle)
  373. {
  374. size_t needle_len = strlen(needle);
  375. do {
  376. if (!strncmp(haystack, needle, needle_len))
  377. return haystack;
  378. haystack = strchr(haystack, '\n');
  379. if (!haystack)
  380. return NULL;
  381. else
  382. ++haystack;
  383. } while (*haystack);
  384. return NULL;
  385. }
  386. /** Returns true if <b>string</b> could be a C identifier.
  387. A C identifier must begin with a letter or an underscore and the
  388. rest of its characters can be letters, numbers or underscores. No
  389. length limit is imposed. */
  390. int
  391. string_is_C_identifier(const char *string)
  392. {
  393. size_t iter;
  394. size_t length = strlen(string);
  395. if (!length)
  396. return 0;
  397. for (iter = 0; iter < length ; iter++) {
  398. if (iter == 0) {
  399. if (!(TOR_ISALPHA(string[iter]) ||
  400. string[iter] == '_'))
  401. return 0;
  402. } else {
  403. if (!(TOR_ISALPHA(string[iter]) ||
  404. TOR_ISDIGIT(string[iter]) ||
  405. string[iter] == '_'))
  406. return 0;
  407. }
  408. }
  409. return 1;
  410. }