utils.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Platform-independent utility routines used throughout this code base.
  3. *
  4. * This file is linked into stand-alone test utilities which only want
  5. * to include the things they really need, so functions in here should
  6. * avoid depending on any functions outside it. Utility routines that
  7. * are more tightly integrated into the main code should live in
  8. * misc.c.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <limits.h>
  14. #include <ctype.h>
  15. #include <assert.h>
  16. #include "defs.h"
  17. #include "misc.h"
  18. /*
  19. * Parse a string block size specification. This is approximately a
  20. * subset of the block size specs supported by GNU fileutils:
  21. * "nk" = n kilobytes
  22. * "nM" = n megabytes
  23. * "nG" = n gigabytes
  24. * All numbers are decimal, and suffixes refer to powers of two.
  25. * Case-insensitive.
  26. */
  27. unsigned long parse_blocksize(const char *bs)
  28. {
  29. char *suf;
  30. unsigned long r = strtoul(bs, &suf, 10);
  31. if (*suf != '\0') {
  32. while (*suf && isspace((unsigned char)*suf)) suf++;
  33. switch (*suf) {
  34. case 'k': case 'K':
  35. r *= 1024ul;
  36. break;
  37. case 'm': case 'M':
  38. r *= 1024ul * 1024ul;
  39. break;
  40. case 'g': case 'G':
  41. r *= 1024ul * 1024ul * 1024ul;
  42. break;
  43. case '\0':
  44. default:
  45. break;
  46. }
  47. }
  48. return r;
  49. }
  50. /*
  51. * Parse a ^C style character specification.
  52. * Returns NULL in `next' if we didn't recognise it as a control character,
  53. * in which case `c' should be ignored.
  54. * The precise current parsing is an oddity inherited from the terminal
  55. * answerback-string parsing code. All sequences start with ^; all except
  56. * ^<123> are two characters. The ones that are worth keeping are probably:
  57. * ^? 127
  58. * ^@A-Z[\]^_ 0-31
  59. * a-z 1-26
  60. * <num> specified by number (decimal, 0octal, 0xHEX)
  61. * ~ ^ escape
  62. */
  63. char ctrlparse(char *s, char **next)
  64. {
  65. char c = 0;
  66. if (*s != '^') {
  67. *next = NULL;
  68. } else {
  69. s++;
  70. if (*s == '\0') {
  71. *next = NULL;
  72. } else if (*s == '<') {
  73. s++;
  74. c = (char)strtol(s, next, 0);
  75. if ((*next == s) || (**next != '>')) {
  76. c = 0;
  77. *next = NULL;
  78. } else
  79. (*next)++;
  80. } else if (*s >= 'a' && *s <= 'z') {
  81. c = (*s - ('a' - 1));
  82. *next = s+1;
  83. } else if ((*s >= '@' && *s <= '_') || *s == '?' || (*s & 0x80)) {
  84. c = ('@' ^ *s);
  85. *next = s+1;
  86. } else if (*s == '~') {
  87. c = '^';
  88. *next = s+1;
  89. }
  90. }
  91. return c;
  92. }
  93. /*
  94. * Find a character in a string, unless it's a colon contained within
  95. * square brackets. Used for untangling strings of the form
  96. * 'host:port', where host can be an IPv6 literal.
  97. *
  98. * We provide several variants of this function, with semantics like
  99. * various standard string.h functions.
  100. */
  101. static const char *host_strchr_internal(const char *s, const char *set,
  102. bool first)
  103. {
  104. int brackets = 0;
  105. const char *ret = NULL;
  106. while (1) {
  107. if (!*s)
  108. return ret;
  109. if (*s == '[')
  110. brackets++;
  111. else if (*s == ']' && brackets > 0)
  112. brackets--;
  113. else if (brackets && *s == ':')
  114. /* never match */ ;
  115. else if (strchr(set, *s)) {
  116. ret = s;
  117. if (first)
  118. return ret;
  119. }
  120. s++;
  121. }
  122. }
  123. size_t host_strcspn(const char *s, const char *set)
  124. {
  125. const char *answer = host_strchr_internal(s, set, true);
  126. if (answer)
  127. return answer - s;
  128. else
  129. return strlen(s);
  130. }
  131. char *host_strchr(const char *s, int c)
  132. {
  133. char set[2];
  134. set[0] = c;
  135. set[1] = '\0';
  136. return (char *) host_strchr_internal(s, set, true);
  137. }
  138. char *host_strrchr(const char *s, int c)
  139. {
  140. char set[2];
  141. set[0] = c;
  142. set[1] = '\0';
  143. return (char *) host_strchr_internal(s, set, false);
  144. }
  145. #ifdef TEST_HOST_STRFOO
  146. int main(void)
  147. {
  148. int passes = 0, fails = 0;
  149. #define TEST1(func, string, arg2, suffix, result) do \
  150. { \
  151. const char *str = string; \
  152. unsigned ret = func(string, arg2) suffix; \
  153. if (ret == result) { \
  154. passes++; \
  155. } else { \
  156. printf("fail: %s(%s,%s)%s = %u, expected %u\n", \
  157. #func, #string, #arg2, #suffix, ret, \
  158. (unsigned)result); \
  159. fails++; \
  160. } \
  161. } while (0)
  162. TEST1(host_strchr, "[1:2:3]:4:5", ':', -str, 7);
  163. TEST1(host_strrchr, "[1:2:3]:4:5", ':', -str, 9);
  164. TEST1(host_strcspn, "[1:2:3]:4:5", "/:",, 7);
  165. TEST1(host_strchr, "[1:2:3]", ':', == NULL, 1);
  166. TEST1(host_strrchr, "[1:2:3]", ':', == NULL, 1);
  167. TEST1(host_strcspn, "[1:2:3]", "/:",, 7);
  168. TEST1(host_strcspn, "[1:2/3]", "/:",, 4);
  169. TEST1(host_strcspn, "[1:2:3]/", "/:",, 7);
  170. printf("passed %d failed %d total %d\n", passes, fails, passes+fails);
  171. return fails != 0 ? 1 : 0;
  172. }
  173. /* Stubs to stop the rest of this module causing compile failures. */
  174. void modalfatalbox(const char *fmt, ...) {}
  175. int conf_get_int(Conf *conf, int primary) { return 0; }
  176. char *conf_get_str(Conf *conf, int primary) { return NULL; }
  177. #endif /* TEST_HOST_STRFOO */
  178. /*
  179. * Trim square brackets off the outside of an IPv6 address literal.
  180. * Leave all other strings unchanged. Returns a fresh dynamically
  181. * allocated string.
  182. */
  183. char *host_strduptrim(const char *s)
  184. {
  185. if (s[0] == '[') {
  186. const char *p = s+1;
  187. int colons = 0;
  188. while (*p && *p != ']') {
  189. if (isxdigit((unsigned char)*p))
  190. /* OK */;
  191. else if (*p == ':')
  192. colons++;
  193. else
  194. break;
  195. p++;
  196. }
  197. if (*p == '%') {
  198. /*
  199. * This delimiter character introduces an RFC 4007 scope
  200. * id suffix (e.g. suffixing the address literal with
  201. * %eth1 or %2 or some such). There's no syntax
  202. * specification for the scope id, so just accept anything
  203. * except the closing ].
  204. */
  205. p += strcspn(p, "]");
  206. }
  207. if (*p == ']' && !p[1] && colons > 1) {
  208. /*
  209. * This looks like an IPv6 address literal (hex digits and
  210. * at least two colons, plus optional scope id, contained
  211. * in square brackets). Trim off the brackets.
  212. */
  213. return dupprintf("%.*s", (int)(p - (s+1)), s+1);
  214. }
  215. }
  216. /*
  217. * Any other shape of string is simply duplicated.
  218. */
  219. return dupstr(s);
  220. }
  221. /* ----------------------------------------------------------------------
  222. * String handling routines.
  223. */
  224. char *dupstr(const char *s)
  225. {
  226. char *p = NULL;
  227. if (s) {
  228. int len = strlen(s);
  229. p = snewn(len + 1, char);
  230. strcpy(p, s);
  231. }
  232. return p;
  233. }
  234. /* Allocate the concatenation of N strings. Terminate arg list with NULL. */
  235. char *dupcat(const char *s1, ...)
  236. {
  237. int len;
  238. char *p, *q, *sn;
  239. va_list ap;
  240. len = strlen(s1);
  241. va_start(ap, s1);
  242. while (1) {
  243. sn = va_arg(ap, char *);
  244. if (!sn)
  245. break;
  246. len += strlen(sn);
  247. }
  248. va_end(ap);
  249. p = snewn(len + 1, char);
  250. strcpy(p, s1);
  251. q = p + strlen(p);
  252. va_start(ap, s1);
  253. while (1) {
  254. sn = va_arg(ap, char *);
  255. if (!sn)
  256. break;
  257. strcpy(q, sn);
  258. q += strlen(q);
  259. }
  260. va_end(ap);
  261. return p;
  262. }
  263. void burnstr(char *string) /* sfree(str), only clear it first */
  264. {
  265. if (string) {
  266. smemclr(string, strlen(string));
  267. sfree(string);
  268. }
  269. }
  270. int string_length_for_printf(size_t s)
  271. {
  272. /* Truncate absurdly long strings (should one show up) to fit
  273. * within a positive 'int', which is what the "%.*s" format will
  274. * expect. */
  275. if (s > INT_MAX)
  276. return INT_MAX;
  277. return s;
  278. }
  279. /* Work around lack of va_copy in old MSC */
  280. #if defined _MSC_VER && !defined va_copy
  281. #define va_copy(a, b) TYPECHECK( \
  282. (va_list *)0 == &(a) && (va_list *)0 == &(b), \
  283. memcpy(&a, &b, sizeof(va_list)))
  284. #endif
  285. /* Also lack of vsnprintf before VS2015 */
  286. #if defined _WINDOWS && \
  287. !defined __MINGW32__ && \
  288. !defined __WINE__ && \
  289. _MSC_VER < 1900
  290. #define vsnprintf _vsnprintf
  291. #endif
  292. /*
  293. * Do an sprintf(), but into a custom-allocated buffer.
  294. *
  295. * Currently I'm doing this via vsnprintf. This has worked so far,
  296. * but it's not good, because vsnprintf is not available on all
  297. * platforms. There's an ifdef to use `_vsnprintf', which seems
  298. * to be the local name for it on Windows. Other platforms may
  299. * lack it completely, in which case it'll be time to rewrite
  300. * this function in a totally different way.
  301. *
  302. * The only `properly' portable solution I can think of is to
  303. * implement my own format string scanner, which figures out an
  304. * upper bound for the length of each formatting directive,
  305. * allocates the buffer as it goes along, and calls sprintf() to
  306. * actually process each directive. If I ever need to actually do
  307. * this, some caveats:
  308. *
  309. * - It's very hard to find a reliable upper bound for
  310. * floating-point values. %f, in particular, when supplied with
  311. * a number near to the upper or lower limit of representable
  312. * numbers, could easily take several hundred characters. It's
  313. * probably feasible to predict this statically using the
  314. * constants in <float.h>, or even to predict it dynamically by
  315. * looking at the exponent of the specific float provided, but
  316. * it won't be fun.
  317. *
  318. * - Don't forget to _check_, after calling sprintf, that it's
  319. * used at most the amount of space we had available.
  320. *
  321. * - Fault any formatting directive we don't fully understand. The
  322. * aim here is to _guarantee_ that we never overflow the buffer,
  323. * because this is a security-critical function. If we see a
  324. * directive we don't know about, we should panic and die rather
  325. * than run any risk.
  326. */
  327. static char *dupvprintf_inner(char *buf, size_t oldlen, size_t *sizeptr,
  328. const char *fmt, va_list ap)
  329. {
  330. size_t size = *sizeptr;
  331. sgrowarrayn_nm(buf, size, oldlen, 512);
  332. while (1) {
  333. va_list aq;
  334. va_copy(aq, ap);
  335. int len = vsnprintf(buf + oldlen, size - oldlen, fmt, aq);
  336. va_end(aq);
  337. if (len >= 0 && len < size) {
  338. /* This is the C99-specified criterion for snprintf to have
  339. * been completely successful. */
  340. *sizeptr = size;
  341. return buf;
  342. } else if (len > 0) {
  343. /* This is the C99 error condition: the returned length is
  344. * the required buffer size not counting the NUL. */
  345. sgrowarrayn_nm(buf, size, oldlen + 1, len);
  346. } else {
  347. /* This is the pre-C99 glibc error condition: <0 means the
  348. * buffer wasn't big enough, so we enlarge it a bit and hope. */
  349. sgrowarray_nm(buf, size, size);
  350. }
  351. }
  352. }
  353. char *dupvprintf(const char *fmt, va_list ap)
  354. {
  355. size_t size = 0;
  356. return dupvprintf_inner(NULL, 0, &size, fmt, ap);
  357. }
  358. char *dupprintf(const char *fmt, ...)
  359. {
  360. char *ret;
  361. va_list ap;
  362. va_start(ap, fmt);
  363. ret = dupvprintf(fmt, ap);
  364. va_end(ap);
  365. return ret;
  366. }
  367. struct strbuf_impl {
  368. size_t size;
  369. struct strbuf visible;
  370. bool nm; /* true if we insist on non-moving buffer resizes */
  371. };
  372. #define STRBUF_SET_UPTR(buf) \
  373. ((buf)->visible.u = (unsigned char *)(buf)->visible.s)
  374. #define STRBUF_SET_PTR(buf, ptr) \
  375. ((buf)->visible.s = (ptr), STRBUF_SET_UPTR(buf))
  376. void *strbuf_append(strbuf *buf_o, size_t len)
  377. {
  378. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  379. char *toret;
  380. sgrowarray_general(
  381. buf->visible.s, buf->size, buf->visible.len + 1, len, buf->nm);
  382. STRBUF_SET_UPTR(buf);
  383. toret = buf->visible.s + buf->visible.len;
  384. buf->visible.len += len;
  385. buf->visible.s[buf->visible.len] = '\0';
  386. return toret;
  387. }
  388. static void strbuf_BinarySink_write(
  389. BinarySink *bs, const void *data, size_t len)
  390. {
  391. strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
  392. memcpy(strbuf_append(buf_o, len), data, len);
  393. }
  394. static strbuf *strbuf_new_general(bool nm)
  395. {
  396. struct strbuf_impl *buf = snew(struct strbuf_impl);
  397. BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
  398. buf->visible.len = 0;
  399. buf->size = 512;
  400. buf->nm = nm;
  401. STRBUF_SET_PTR(buf, snewn(buf->size, char));
  402. *buf->visible.s = '\0';
  403. return &buf->visible;
  404. }
  405. strbuf *strbuf_new(void) { return strbuf_new_general(false); }
  406. strbuf *strbuf_new_nm(void) { return strbuf_new_general(true); }
  407. void strbuf_free(strbuf *buf_o)
  408. {
  409. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  410. if (buf->visible.s) {
  411. smemclr(buf->visible.s, buf->size);
  412. sfree(buf->visible.s);
  413. }
  414. sfree(buf);
  415. }
  416. char *strbuf_to_str(strbuf *buf_o)
  417. {
  418. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  419. char *ret = buf->visible.s;
  420. sfree(buf);
  421. return ret;
  422. }
  423. void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
  424. {
  425. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  426. STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
  427. &buf->size, fmt, ap));
  428. buf->visible.len += strlen(buf->visible.s + buf->visible.len);
  429. }
  430. void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
  431. {
  432. va_list ap;
  433. va_start(ap, fmt);
  434. strbuf_catfv(buf_o, fmt, ap);
  435. va_end(ap);
  436. }
  437. strbuf *strbuf_new_for_agent_query(void)
  438. {
  439. strbuf *buf = strbuf_new();
  440. strbuf_append(buf, 4);
  441. return buf;
  442. }
  443. void strbuf_finalise_agent_query(strbuf *buf_o)
  444. {
  445. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  446. assert(buf->visible.len >= 5);
  447. PUT_32BIT_MSB_FIRST(buf->visible.u, buf->visible.len - 4);
  448. }
  449. /*
  450. * Read an entire line of text from a file. Return a buffer
  451. * malloced to be as big as necessary (caller must free).
  452. */
  453. char *fgetline(FILE *fp)
  454. {
  455. char *ret = snewn(512, char);
  456. size_t size = 512, len = 0;
  457. while (fgets(ret + len, size - len, fp)) {
  458. len += strlen(ret + len);
  459. if (len > 0 && ret[len-1] == '\n')
  460. break; /* got a newline, we're done */
  461. sgrowarrayn_nm(ret, size, len, 512);
  462. }
  463. if (len == 0) { /* first fgets returned NULL */
  464. sfree(ret);
  465. return NULL;
  466. }
  467. ret[len] = '\0';
  468. return ret;
  469. }
  470. /*
  471. * Read an entire file into a BinarySink.
  472. */
  473. bool read_file_into(BinarySink *bs, FILE *fp)
  474. {
  475. char buf[4096];
  476. while (1) {
  477. size_t retd = fread(buf, 1, sizeof(buf), fp);
  478. if (retd == 0)
  479. return !ferror(fp);
  480. put_data(bs, buf, retd);
  481. }
  482. }
  483. /*
  484. * Perl-style 'chomp', for a line we just read with fgetline. Unlike
  485. * Perl chomp, however, we're deliberately forgiving of strange
  486. * line-ending conventions. Also we forgive NULL on input, so you can
  487. * just write 'line = chomp(fgetline(fp));' and not bother checking
  488. * for NULL until afterwards.
  489. */
  490. char *chomp(char *str)
  491. {
  492. if (str) {
  493. int len = strlen(str);
  494. while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n'))
  495. len--;
  496. str[len] = '\0';
  497. }
  498. return str;
  499. }
  500. /* ----------------------------------------------------------------------
  501. * Core base64 encoding and decoding routines.
  502. */
  503. void base64_encode_atom(const unsigned char *data, int n, char *out)
  504. {
  505. static const char base64_chars[] =
  506. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  507. unsigned word;
  508. word = data[0] << 16;
  509. if (n > 1)
  510. word |= data[1] << 8;
  511. if (n > 2)
  512. word |= data[2];
  513. out[0] = base64_chars[(word >> 18) & 0x3F];
  514. out[1] = base64_chars[(word >> 12) & 0x3F];
  515. if (n > 1)
  516. out[2] = base64_chars[(word >> 6) & 0x3F];
  517. else
  518. out[2] = '=';
  519. if (n > 2)
  520. out[3] = base64_chars[word & 0x3F];
  521. else
  522. out[3] = '=';
  523. }
  524. int base64_decode_atom(const char *atom, unsigned char *out)
  525. {
  526. int vals[4];
  527. int i, v, len;
  528. unsigned word;
  529. char c;
  530. for (i = 0; i < 4; i++) {
  531. c = atom[i];
  532. if (c >= 'A' && c <= 'Z')
  533. v = c - 'A';
  534. else if (c >= 'a' && c <= 'z')
  535. v = c - 'a' + 26;
  536. else if (c >= '0' && c <= '9')
  537. v = c - '0' + 52;
  538. else if (c == '+')
  539. v = 62;
  540. else if (c == '/')
  541. v = 63;
  542. else if (c == '=')
  543. v = -1;
  544. else
  545. return 0; /* invalid atom */
  546. vals[i] = v;
  547. }
  548. if (vals[0] == -1 || vals[1] == -1)
  549. return 0;
  550. if (vals[2] == -1 && vals[3] != -1)
  551. return 0;
  552. if (vals[3] != -1)
  553. len = 3;
  554. else if (vals[2] != -1)
  555. len = 2;
  556. else
  557. len = 1;
  558. word = ((vals[0] << 18) |
  559. (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
  560. out[0] = (word >> 16) & 0xFF;
  561. if (len > 1)
  562. out[1] = (word >> 8) & 0xFF;
  563. if (len > 2)
  564. out[2] = word & 0xFF;
  565. return len;
  566. }
  567. /* ----------------------------------------------------------------------
  568. * Generic routines to deal with send buffers: a linked list of
  569. * smallish blocks, with the operations
  570. *
  571. * - add an arbitrary amount of data to the end of the list
  572. * - remove the first N bytes from the list
  573. * - return a (pointer,length) pair giving some initial data in
  574. * the list, suitable for passing to a send or write system
  575. * call
  576. * - retrieve a larger amount of initial data from the list
  577. * - return the current size of the buffer chain in bytes
  578. */
  579. #define BUFFER_MIN_GRANULE 512
  580. struct bufchain_granule {
  581. struct bufchain_granule *next;
  582. char *bufpos, *bufend, *bufmax;
  583. };
  584. static void uninitialised_queue_idempotent_callback(IdempotentCallback *ic)
  585. {
  586. unreachable("bufchain callback used while uninitialised");
  587. }
  588. void bufchain_init(bufchain *ch)
  589. {
  590. ch->head = ch->tail = NULL;
  591. ch->buffersize = 0;
  592. ch->ic = NULL;
  593. ch->queue_idempotent_callback = uninitialised_queue_idempotent_callback;
  594. }
  595. void bufchain_clear(bufchain *ch)
  596. {
  597. struct bufchain_granule *b;
  598. while (ch->head) {
  599. b = ch->head;
  600. ch->head = ch->head->next;
  601. smemclr(b, sizeof(*b));
  602. sfree(b);
  603. }
  604. ch->tail = NULL;
  605. ch->buffersize = 0;
  606. }
  607. size_t bufchain_size(bufchain *ch)
  608. {
  609. return ch->buffersize;
  610. }
  611. void bufchain_set_callback_inner(
  612. bufchain *ch, IdempotentCallback *ic,
  613. void (*queue_idempotent_callback)(IdempotentCallback *ic))
  614. {
  615. ch->queue_idempotent_callback = queue_idempotent_callback;
  616. ch->ic = ic;
  617. }
  618. void bufchain_add(bufchain *ch, const void *data, size_t len)
  619. {
  620. const char *buf = (const char *)data;
  621. if (len == 0) return;
  622. ch->buffersize += len;
  623. while (len > 0) {
  624. if (ch->tail && ch->tail->bufend < ch->tail->bufmax) {
  625. size_t copylen = min(len, ch->tail->bufmax - ch->tail->bufend);
  626. memcpy(ch->tail->bufend, buf, copylen);
  627. buf += copylen;
  628. len -= copylen;
  629. ch->tail->bufend += copylen;
  630. }
  631. if (len > 0) {
  632. size_t grainlen =
  633. max(sizeof(struct bufchain_granule) + len, BUFFER_MIN_GRANULE);
  634. struct bufchain_granule *newbuf;
  635. newbuf = smalloc(grainlen);
  636. newbuf->bufpos = newbuf->bufend =
  637. (char *)newbuf + sizeof(struct bufchain_granule);
  638. newbuf->bufmax = (char *)newbuf + grainlen;
  639. newbuf->next = NULL;
  640. if (ch->tail)
  641. ch->tail->next = newbuf;
  642. else
  643. ch->head = newbuf;
  644. ch->tail = newbuf;
  645. }
  646. }
  647. if (ch->ic)
  648. ch->queue_idempotent_callback(ch->ic);
  649. }
  650. void bufchain_consume(bufchain *ch, size_t len)
  651. {
  652. struct bufchain_granule *tmp;
  653. assert(ch->buffersize >= len);
  654. while (len > 0) {
  655. int remlen = len;
  656. assert(ch->head != NULL);
  657. if (remlen >= ch->head->bufend - ch->head->bufpos) {
  658. remlen = ch->head->bufend - ch->head->bufpos;
  659. tmp = ch->head;
  660. ch->head = tmp->next;
  661. if (!ch->head)
  662. ch->tail = NULL;
  663. smemclr(tmp, sizeof(*tmp));
  664. sfree(tmp);
  665. } else
  666. ch->head->bufpos += remlen;
  667. ch->buffersize -= remlen;
  668. len -= remlen;
  669. }
  670. }
  671. ptrlen bufchain_prefix(bufchain *ch)
  672. {
  673. return make_ptrlen(ch->head->bufpos, ch->head->bufend - ch->head->bufpos);
  674. }
  675. void bufchain_fetch(bufchain *ch, void *data, size_t len)
  676. {
  677. struct bufchain_granule *tmp;
  678. char *data_c = (char *)data;
  679. tmp = ch->head;
  680. assert(ch->buffersize >= len);
  681. while (len > 0) {
  682. int remlen = len;
  683. assert(tmp != NULL);
  684. if (remlen >= tmp->bufend - tmp->bufpos)
  685. remlen = tmp->bufend - tmp->bufpos;
  686. memcpy(data_c, tmp->bufpos, remlen);
  687. tmp = tmp->next;
  688. len -= remlen;
  689. data_c += remlen;
  690. }
  691. }
  692. void bufchain_fetch_consume(bufchain *ch, void *data, size_t len)
  693. {
  694. bufchain_fetch(ch, data, len);
  695. bufchain_consume(ch, len);
  696. }
  697. bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len)
  698. {
  699. if (ch->buffersize >= len) {
  700. bufchain_fetch_consume(ch, data, len);
  701. return true;
  702. } else {
  703. return false;
  704. }
  705. }
  706. size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len)
  707. {
  708. if (len > ch->buffersize)
  709. len = ch->buffersize;
  710. if (len)
  711. bufchain_fetch_consume(ch, data, len);
  712. return len;
  713. }
  714. /* ----------------------------------------------------------------------
  715. * Debugging routines.
  716. */
  717. #ifdef DEBUG
  718. extern void dputs(const char *); /* defined in per-platform *misc.c */
  719. void debug_printf(const char *fmt, ...)
  720. {
  721. char *buf;
  722. va_list ap;
  723. va_start(ap, fmt);
  724. buf = dupvprintf(fmt, ap);
  725. dputs(buf);
  726. sfree(buf);
  727. va_end(ap);
  728. }
  729. void debug_memdump(const void *buf, int len, bool L)
  730. {
  731. int i;
  732. const unsigned char *p = buf;
  733. char foo[17];
  734. if (L) {
  735. int delta;
  736. debug_printf("\t%d (0x%x) bytes:\n", len, len);
  737. delta = 15 & (uintptr_t)p;
  738. p -= delta;
  739. len += delta;
  740. }
  741. for (; 0 < len; p += 16, len -= 16) {
  742. dputs(" ");
  743. if (L)
  744. debug_printf("%p: ", p);
  745. strcpy(foo, "................"); /* sixteen dots */
  746. for (i = 0; i < 16 && i < len; ++i) {
  747. if (&p[i] < (unsigned char *) buf) {
  748. dputs(" "); /* 3 spaces */
  749. foo[i] = ' ';
  750. } else {
  751. debug_printf("%c%02.2x",
  752. &p[i] != (unsigned char *) buf
  753. && i % 4 ? '.' : ' ', p[i]
  754. );
  755. if (p[i] >= ' ' && p[i] <= '~')
  756. foo[i] = (char) p[i];
  757. }
  758. }
  759. foo[i] = '\0';
  760. debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
  761. }
  762. }
  763. #endif /* def DEBUG */
  764. #ifndef PLATFORM_HAS_SMEMCLR
  765. /*
  766. * Securely wipe memory.
  767. *
  768. * The actual wiping is no different from what memset would do: the
  769. * point of 'securely' is to try to be sure over-clever compilers
  770. * won't optimise away memsets on variables that are about to be freed
  771. * or go out of scope. See
  772. * https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/771-BSI.html
  773. *
  774. * Some platforms (e.g. Windows) may provide their own version of this
  775. * function.
  776. */
  777. void smemclr(void *b, size_t n) {
  778. volatile char *vp;
  779. if (b && n > 0) {
  780. /*
  781. * Zero out the memory.
  782. */
  783. memset(b, 0, n);
  784. /*
  785. * Perform a volatile access to the object, forcing the
  786. * compiler to admit that the previous memset was important.
  787. *
  788. * This while loop should in practice run for zero iterations
  789. * (since we know we just zeroed the object out), but in
  790. * theory (as far as the compiler knows) it might range over
  791. * the whole object. (If we had just written, say, '*vp =
  792. * *vp;', a compiler could in principle have 'helpfully'
  793. * optimised the memset into only zeroing out the first byte.
  794. * This should be robust.)
  795. */
  796. vp = b;
  797. while (*vp) vp++;
  798. }
  799. }
  800. #endif
  801. bool smemeq(const void *av, const void *bv, size_t len)
  802. {
  803. const unsigned char *a = (const unsigned char *)av;
  804. const unsigned char *b = (const unsigned char *)bv;
  805. unsigned val = 0;
  806. while (len-- > 0) {
  807. val |= *a++ ^ *b++;
  808. }
  809. /* Now val is 0 iff we want to return 1, and in the range
  810. * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
  811. * will clear bit 8 iff we want to return 0, and leave it set iff
  812. * we want to return 1, so then we can just shift down. */
  813. return (0x100 - val) >> 8;
  814. }
  815. int nullstrcmp(const char *a, const char *b)
  816. {
  817. if (a == NULL && b == NULL)
  818. return 0;
  819. if (a == NULL)
  820. return -1;
  821. if (b == NULL)
  822. return +1;
  823. return strcmp(a, b);
  824. }
  825. bool ptrlen_eq_string(ptrlen pl, const char *str)
  826. {
  827. size_t len = strlen(str);
  828. return (pl.len == len && !memcmp(pl.ptr, str, len));
  829. }
  830. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
  831. {
  832. return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
  833. }
  834. int ptrlen_strcmp(ptrlen pl1, ptrlen pl2)
  835. {
  836. size_t minlen = pl1.len < pl2.len ? pl1.len : pl2.len;
  837. if (minlen) { /* tolerate plX.ptr==NULL as long as plX.len==0 */
  838. int cmp = memcmp(pl1.ptr, pl2.ptr, minlen);
  839. if (cmp)
  840. return cmp;
  841. }
  842. return pl1.len < pl2.len ? -1 : pl1.len > pl2.len ? +1 : 0;
  843. }
  844. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
  845. {
  846. if (whole.len >= prefix.len &&
  847. !memcmp(whole.ptr, prefix.ptr, prefix.len)) {
  848. if (tail) {
  849. tail->ptr = (const char *)whole.ptr + prefix.len;
  850. tail->len = whole.len - prefix.len;
  851. }
  852. return true;
  853. }
  854. return false;
  855. }
  856. bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
  857. {
  858. if (whole.len >= suffix.len &&
  859. !memcmp((char *)whole.ptr + (whole.len - suffix.len),
  860. suffix.ptr, suffix.len)) {
  861. if (tail) {
  862. tail->ptr = whole.ptr;
  863. tail->len = whole.len - suffix.len;
  864. }
  865. return true;
  866. }
  867. return false;
  868. }
  869. ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
  870. {
  871. const char *p = input->ptr, *end = p + input->len;
  872. ptrlen toret;
  873. while (p < end && strchr(separators, *p))
  874. p++;
  875. toret.ptr = p;
  876. while (p < end && !strchr(separators, *p))
  877. p++;
  878. toret.len = p - (const char *)toret.ptr;
  879. size_t to_consume = p - (const char *)input->ptr;
  880. assert(to_consume <= input->len);
  881. input->ptr = (const char *)input->ptr + to_consume;
  882. input->len -= to_consume;
  883. return toret;
  884. }
  885. char *mkstr(ptrlen pl)
  886. {
  887. char *p = snewn(pl.len + 1, char);
  888. memcpy(p, pl.ptr, pl.len);
  889. p[pl.len] = '\0';
  890. return p;
  891. }
  892. bool strstartswith(const char *s, const char *t)
  893. {
  894. return !memcmp(s, t, strlen(t));
  895. }
  896. bool strendswith(const char *s, const char *t)
  897. {
  898. size_t slen = strlen(s), tlen = strlen(t);
  899. return slen >= tlen && !strcmp(s + (slen - tlen), t);
  900. }
  901. size_t encode_utf8(void *output, unsigned long ch)
  902. {
  903. unsigned char *start = (unsigned char *)output, *p = start;
  904. if (ch < 0x80) {
  905. *p++ = ch;
  906. } else if (ch < 0x800) {
  907. *p++ = 0xC0 | (ch >> 6);
  908. *p++ = 0x80 | (ch & 0x3F);
  909. } else if (ch < 0x10000) {
  910. *p++ = 0xE0 | (ch >> 12);
  911. *p++ = 0x80 | ((ch >> 6) & 0x3F);
  912. *p++ = 0x80 | (ch & 0x3F);
  913. } else {
  914. *p++ = 0xF0 | (ch >> 18);
  915. *p++ = 0x80 | ((ch >> 12) & 0x3F);
  916. *p++ = 0x80 | ((ch >> 6) & 0x3F);
  917. *p++ = 0x80 | (ch & 0x3F);
  918. }
  919. return p - start;
  920. }