utils.c 30 KB

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