utils.c 31 KB

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