utils.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 WINSCP) && !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. { // WINSCP
  344. #if defined _DEBUG && defined IDE
  345. // CodeGuard hangs in v*printf functions. But while it's possible to disable CodeGuard in vsprintf, it's not possible for vsnprintf.
  346. // We never want to distribute this version of the code, hence the IDE condition.
  347. // Put this into WinSCP.cgi along with WinSCP.exe
  348. // [vsprintf]
  349. // Disable=yes
  350. int len = vsprintf(buf + oldlen, fmt, aq);
  351. #else
  352. int len = vsnprintf(buf + oldlen, size - oldlen, fmt, aq);
  353. #endif
  354. va_end(aq);
  355. if (len >= 0 && len < size) {
  356. /* This is the C99-specified criterion for snprintf to have
  357. * been completely successful. */
  358. *sizeptr = size;
  359. return buf;
  360. } else if (len > 0) {
  361. /* This is the C99 error condition: the returned length is
  362. * the required buffer size not counting the NUL. */
  363. sgrowarrayn_nm(buf, size, oldlen + 1, len);
  364. } else {
  365. /* This is the pre-C99 glibc error condition: <0 means the
  366. * buffer wasn't big enough, so we enlarge it a bit and hope. */
  367. sgrowarray_nm(buf, size, size);
  368. }
  369. } // WINSCP
  370. }
  371. }
  372. char *dupvprintf(const char *fmt, va_list ap)
  373. {
  374. size_t size = 0;
  375. return dupvprintf_inner(NULL, 0, &size, fmt, ap);
  376. }
  377. char *dupprintf(const char *fmt, ...)
  378. {
  379. char *ret;
  380. va_list ap;
  381. va_start(ap, fmt);
  382. ret = dupvprintf(fmt, ap);
  383. va_end(ap);
  384. return ret;
  385. }
  386. struct strbuf_impl {
  387. size_t size;
  388. struct strbuf visible;
  389. bool nm; /* true if we insist on non-moving buffer resizes */
  390. };
  391. #define STRBUF_SET_UPTR(buf) \
  392. ((buf)->visible.u = (unsigned char *)(buf)->visible.s)
  393. #define STRBUF_SET_PTR(buf, ptr) \
  394. ((buf)->visible.s = (ptr), STRBUF_SET_UPTR(buf))
  395. void *strbuf_append(strbuf *buf_o, size_t len)
  396. {
  397. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  398. char *toret;
  399. sgrowarray_general(
  400. buf->visible.s, buf->size, buf->visible.len + 1, len, buf->nm);
  401. STRBUF_SET_UPTR(buf);
  402. toret = buf->visible.s + buf->visible.len;
  403. buf->visible.len += len;
  404. buf->visible.s[buf->visible.len] = '\0';
  405. return toret;
  406. }
  407. void strbuf_shrink_to(strbuf *buf, size_t new_len)
  408. {
  409. assert(new_len <= buf->len);
  410. buf->len = new_len;
  411. buf->s[buf->len] = '\0';
  412. }
  413. void strbuf_shrink_by(strbuf *buf, size_t amount_to_remove)
  414. {
  415. assert(amount_to_remove <= buf->len);
  416. buf->len -= amount_to_remove;
  417. buf->s[buf->len] = '\0';
  418. }
  419. bool strbuf_chomp(strbuf *buf, char char_to_remove)
  420. {
  421. if (buf->len > 0 && buf->s[buf->len-1] == char_to_remove) {
  422. strbuf_shrink_by(buf, 1);
  423. return true;
  424. }
  425. return false;
  426. }
  427. static void strbuf_BinarySink_write(
  428. BinarySink *bs, const void *data, size_t len)
  429. {
  430. strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
  431. memcpy(strbuf_append(buf_o, len), data, len);
  432. }
  433. static strbuf *strbuf_new_general(bool nm)
  434. {
  435. struct strbuf_impl *buf = snew(struct strbuf_impl);
  436. BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
  437. buf->visible.len = 0;
  438. buf->size = 512;
  439. buf->nm = nm;
  440. STRBUF_SET_PTR(buf, snewn(buf->size, char));
  441. *buf->visible.s = '\0';
  442. return &buf->visible;
  443. }
  444. strbuf *strbuf_new(void) { return strbuf_new_general(false); }
  445. strbuf *strbuf_new_nm(void) { return strbuf_new_general(true); }
  446. void strbuf_free(strbuf *buf_o)
  447. {
  448. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  449. if (buf->visible.s) {
  450. smemclr(buf->visible.s, buf->size);
  451. sfree(buf->visible.s);
  452. }
  453. sfree(buf);
  454. }
  455. char *strbuf_to_str(strbuf *buf_o)
  456. {
  457. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  458. char *ret = buf->visible.s;
  459. sfree(buf);
  460. return ret;
  461. }
  462. void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
  463. {
  464. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  465. STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
  466. &buf->size, fmt, ap));
  467. buf->visible.len += strlen(buf->visible.s + buf->visible.len);
  468. }
  469. void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
  470. {
  471. va_list ap;
  472. va_start(ap, fmt);
  473. strbuf_catfv(buf_o, fmt, ap);
  474. va_end(ap);
  475. }
  476. strbuf *strbuf_new_for_agent_query(void)
  477. {
  478. strbuf *buf = strbuf_new();
  479. strbuf_append(buf, 4);
  480. return buf;
  481. }
  482. void strbuf_finalise_agent_query(strbuf *buf_o)
  483. {
  484. struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
  485. assert(buf->visible.len >= 5);
  486. PUT_32BIT_MSB_FIRST(buf->visible.u, buf->visible.len - 4);
  487. }
  488. /*
  489. * Read an entire line of text from a file. Return a buffer
  490. * malloced to be as big as necessary (caller must free).
  491. */
  492. char *fgetline(FILE *fp)
  493. {
  494. char *ret = snewn(512, char);
  495. size_t size = 512, len = 0;
  496. while (fgets(ret + len, size - len, fp)) {
  497. len += strlen(ret + len);
  498. if (len > 0 && ret[len-1] == '\n')
  499. break; /* got a newline, we're done */
  500. sgrowarrayn_nm(ret, size, len, 512);
  501. }
  502. if (len == 0) { /* first fgets returned NULL */
  503. sfree(ret);
  504. return NULL;
  505. }
  506. ret[len] = '\0';
  507. return ret;
  508. }
  509. /*
  510. * Read an entire file into a BinarySink.
  511. */
  512. bool read_file_into(BinarySink *bs, FILE *fp)
  513. {
  514. char buf[4096];
  515. while (1) {
  516. size_t retd = fread(buf, 1, sizeof(buf), fp);
  517. if (retd == 0)
  518. return !ferror(fp);
  519. put_data(bs, buf, retd);
  520. }
  521. }
  522. /*
  523. * Perl-style 'chomp', for a line we just read with fgetline. Unlike
  524. * Perl chomp, however, we're deliberately forgiving of strange
  525. * line-ending conventions. Also we forgive NULL on input, so you can
  526. * just write 'line = chomp(fgetline(fp));' and not bother checking
  527. * for NULL until afterwards.
  528. */
  529. char *chomp(char *str)
  530. {
  531. if (str) {
  532. int len = strlen(str);
  533. while (len > 0 && (str[len-1] == '\r' || str[len-1] == '\n'))
  534. len--;
  535. str[len] = '\0';
  536. }
  537. return str;
  538. }
  539. /* ----------------------------------------------------------------------
  540. * Core base64 encoding and decoding routines.
  541. */
  542. void base64_encode_atom(const unsigned char *data, int n, char *out)
  543. {
  544. static const char base64_chars[] =
  545. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  546. unsigned word;
  547. word = data[0] << 16;
  548. if (n > 1)
  549. word |= data[1] << 8;
  550. if (n > 2)
  551. word |= data[2];
  552. out[0] = base64_chars[(word >> 18) & 0x3F];
  553. out[1] = base64_chars[(word >> 12) & 0x3F];
  554. if (n > 1)
  555. out[2] = base64_chars[(word >> 6) & 0x3F];
  556. else
  557. out[2] = '=';
  558. if (n > 2)
  559. out[3] = base64_chars[word & 0x3F];
  560. else
  561. out[3] = '=';
  562. }
  563. int base64_decode_atom(const char *atom, unsigned char *out)
  564. {
  565. int vals[4];
  566. int i, v, len;
  567. unsigned word;
  568. char c;
  569. for (i = 0; i < 4; i++) {
  570. c = atom[i];
  571. if (c >= 'A' && c <= 'Z')
  572. v = c - 'A';
  573. else if (c >= 'a' && c <= 'z')
  574. v = c - 'a' + 26;
  575. else if (c >= '0' && c <= '9')
  576. v = c - '0' + 52;
  577. else if (c == '+')
  578. v = 62;
  579. else if (c == '/')
  580. v = 63;
  581. else if (c == '=')
  582. v = -1;
  583. else
  584. return 0; /* invalid atom */
  585. vals[i] = v;
  586. }
  587. if (vals[0] == -1 || vals[1] == -1)
  588. return 0;
  589. if (vals[2] == -1 && vals[3] != -1)
  590. return 0;
  591. if (vals[3] != -1)
  592. len = 3;
  593. else if (vals[2] != -1)
  594. len = 2;
  595. else
  596. len = 1;
  597. word = ((vals[0] << 18) |
  598. (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F));
  599. out[0] = (word >> 16) & 0xFF;
  600. if (len > 1)
  601. out[1] = (word >> 8) & 0xFF;
  602. if (len > 2)
  603. out[2] = word & 0xFF;
  604. return len;
  605. }
  606. /* ----------------------------------------------------------------------
  607. * Generic routines to deal with send buffers: a linked list of
  608. * smallish blocks, with the operations
  609. *
  610. * - add an arbitrary amount of data to the end of the list
  611. * - remove the first N bytes from the list
  612. * - return a (pointer,length) pair giving some initial data in
  613. * the list, suitable for passing to a send or write system
  614. * call
  615. * - retrieve a larger amount of initial data from the list
  616. * - return the current size of the buffer chain in bytes
  617. */
  618. /* WINSCP
  619. * Default granule of 512 leads to low performance.
  620. */
  621. #define BUFFER_MIN_GRANULE 512*2*32
  622. struct bufchain_granule {
  623. struct bufchain_granule *next;
  624. char *bufpos, *bufend, *bufmax;
  625. };
  626. static void uninitialised_queue_idempotent_callback(IdempotentCallback *ic)
  627. {
  628. unreachable("bufchain callback used while uninitialised");
  629. }
  630. void bufchain_init(bufchain *ch)
  631. {
  632. ch->head = ch->tail = NULL;
  633. ch->buffersize = 0;
  634. ch->ic = NULL;
  635. ch->queue_idempotent_callback = uninitialised_queue_idempotent_callback;
  636. }
  637. void bufchain_clear(bufchain *ch)
  638. {
  639. struct bufchain_granule *b;
  640. while (ch->head) {
  641. b = ch->head;
  642. ch->head = ch->head->next;
  643. smemclr(b, sizeof(*b));
  644. sfree(b);
  645. }
  646. ch->tail = NULL;
  647. ch->buffersize = 0;
  648. }
  649. size_t bufchain_size(bufchain *ch)
  650. {
  651. return ch->buffersize;
  652. }
  653. void bufchain_set_callback_inner(
  654. bufchain *ch, IdempotentCallback *ic,
  655. void (*queue_idempotent_callback)(IdempotentCallback *ic))
  656. {
  657. ch->queue_idempotent_callback = queue_idempotent_callback;
  658. ch->ic = ic;
  659. }
  660. void bufchain_add(bufchain *ch, const void *data, size_t len)
  661. {
  662. const char *buf = (const char *)data;
  663. if (len == 0) return;
  664. ch->buffersize += len;
  665. while (len > 0) {
  666. if (ch->tail && ch->tail->bufend < ch->tail->bufmax) {
  667. size_t copylen = min(len, ch->tail->bufmax - ch->tail->bufend);
  668. memcpy(ch->tail->bufend, buf, copylen);
  669. buf += copylen;
  670. len -= copylen;
  671. ch->tail->bufend += copylen;
  672. }
  673. if (len > 0) {
  674. size_t grainlen =
  675. max(sizeof(struct bufchain_granule) + len, BUFFER_MIN_GRANULE);
  676. struct bufchain_granule *newbuf;
  677. newbuf = smalloc(grainlen);
  678. newbuf->bufpos = newbuf->bufend =
  679. (char *)newbuf + sizeof(struct bufchain_granule);
  680. newbuf->bufmax = (char *)newbuf + grainlen;
  681. newbuf->next = NULL;
  682. if (ch->tail)
  683. ch->tail->next = newbuf;
  684. else
  685. ch->head = newbuf;
  686. ch->tail = newbuf;
  687. }
  688. }
  689. if (ch->ic)
  690. ch->queue_idempotent_callback(ch->ic);
  691. }
  692. void bufchain_consume(bufchain *ch, size_t len)
  693. {
  694. struct bufchain_granule *tmp;
  695. assert(ch->buffersize >= len);
  696. while (len > 0) {
  697. int remlen = len;
  698. assert(ch->head != NULL);
  699. if (remlen >= ch->head->bufend - ch->head->bufpos) {
  700. remlen = ch->head->bufend - ch->head->bufpos;
  701. tmp = ch->head;
  702. ch->head = tmp->next;
  703. if (!ch->head)
  704. ch->tail = NULL;
  705. smemclr(tmp, sizeof(*tmp));
  706. sfree(tmp);
  707. } else
  708. ch->head->bufpos += remlen;
  709. ch->buffersize -= remlen;
  710. len -= remlen;
  711. }
  712. }
  713. ptrlen bufchain_prefix(bufchain *ch)
  714. {
  715. return make_ptrlen(ch->head->bufpos, ch->head->bufend - ch->head->bufpos);
  716. }
  717. void bufchain_fetch(bufchain *ch, void *data, size_t len)
  718. {
  719. struct bufchain_granule *tmp;
  720. char *data_c = (char *)data;
  721. tmp = ch->head;
  722. assert(ch->buffersize >= len);
  723. while (len > 0) {
  724. int remlen = len;
  725. assert(tmp != NULL);
  726. if (remlen >= tmp->bufend - tmp->bufpos)
  727. remlen = tmp->bufend - tmp->bufpos;
  728. memcpy(data_c, tmp->bufpos, remlen);
  729. tmp = tmp->next;
  730. len -= remlen;
  731. data_c += remlen;
  732. }
  733. }
  734. void bufchain_fetch_consume(bufchain *ch, void *data, size_t len)
  735. {
  736. bufchain_fetch(ch, data, len);
  737. bufchain_consume(ch, len);
  738. }
  739. bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len)
  740. {
  741. if (ch->buffersize >= len) {
  742. bufchain_fetch_consume(ch, data, len);
  743. return true;
  744. } else {
  745. return false;
  746. }
  747. }
  748. size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len)
  749. {
  750. if (len > ch->buffersize)
  751. len = ch->buffersize;
  752. if (len)
  753. bufchain_fetch_consume(ch, data, len);
  754. return len;
  755. }
  756. /* ----------------------------------------------------------------------
  757. * Debugging routines.
  758. */
  759. #ifdef DEBUG
  760. extern void dputs(const char *); /* defined in per-platform *misc.c */
  761. void debug_printf(const char *fmt, ...)
  762. {
  763. char *buf;
  764. va_list ap;
  765. va_start(ap, fmt);
  766. buf = dupvprintf(fmt, ap);
  767. dputs(buf);
  768. sfree(buf);
  769. va_end(ap);
  770. }
  771. void debug_memdump(const void *buf, int len, bool L)
  772. {
  773. int i;
  774. const unsigned char *p = buf;
  775. char foo[17];
  776. if (L) {
  777. int delta;
  778. debug_printf("\t%d (0x%x) bytes:\n", len, len);
  779. delta = 15 & (uintptr_t)p;
  780. p -= delta;
  781. len += delta;
  782. }
  783. for (; 0 < len; p += 16, len -= 16) {
  784. dputs(" ");
  785. if (L)
  786. debug_printf("%p: ", p);
  787. strcpy(foo, "................"); /* sixteen dots */
  788. for (i = 0; i < 16 && i < len; ++i) {
  789. if (&p[i] < (unsigned char *) buf) {
  790. dputs(" "); /* 3 spaces */
  791. foo[i] = ' ';
  792. } else {
  793. debug_printf("%c%02.2x",
  794. &p[i] != (unsigned char *) buf
  795. && i % 4 ? '.' : ' ', p[i]
  796. );
  797. if (p[i] >= ' ' && p[i] <= '~')
  798. foo[i] = (char) p[i];
  799. }
  800. }
  801. foo[i] = '\0';
  802. debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
  803. }
  804. }
  805. #endif /* def DEBUG */
  806. #ifndef PLATFORM_HAS_SMEMCLR
  807. /*
  808. * Securely wipe memory.
  809. *
  810. * The actual wiping is no different from what memset would do: the
  811. * point of 'securely' is to try to be sure over-clever compilers
  812. * won't optimise away memsets on variables that are about to be freed
  813. * or go out of scope. See
  814. * https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/771-BSI.html
  815. *
  816. * Some platforms (e.g. Windows) may provide their own version of this
  817. * function.
  818. */
  819. void smemclr(void *b, size_t n) {
  820. volatile char *vp;
  821. if (b && n > 0) {
  822. /*
  823. * Zero out the memory.
  824. */
  825. memset(b, 0, n);
  826. /*
  827. * Perform a volatile access to the object, forcing the
  828. * compiler to admit that the previous memset was important.
  829. *
  830. * This while loop should in practice run for zero iterations
  831. * (since we know we just zeroed the object out), but in
  832. * theory (as far as the compiler knows) it might range over
  833. * the whole object. (If we had just written, say, '*vp =
  834. * *vp;', a compiler could in principle have 'helpfully'
  835. * optimised the memset into only zeroing out the first byte.
  836. * This should be robust.)
  837. */
  838. vp = b;
  839. while (*vp) vp++;
  840. }
  841. }
  842. #endif
  843. bool smemeq(const void *av, const void *bv, size_t len)
  844. {
  845. const unsigned char *a = (const unsigned char *)av;
  846. const unsigned char *b = (const unsigned char *)bv;
  847. unsigned val = 0;
  848. while (len-- > 0) {
  849. val |= *a++ ^ *b++;
  850. }
  851. /* Now val is 0 iff we want to return 1, and in the range
  852. * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
  853. * will clear bit 8 iff we want to return 0, and leave it set iff
  854. * we want to return 1, so then we can just shift down. */
  855. return (0x100 - val) >> 8;
  856. }
  857. int nullstrcmp(const char *a, const char *b)
  858. {
  859. if (a == NULL && b == NULL)
  860. return 0;
  861. if (a == NULL)
  862. return -1;
  863. if (b == NULL)
  864. return +1;
  865. return strcmp(a, b);
  866. }
  867. bool ptrlen_eq_string(ptrlen pl, const char *str)
  868. {
  869. size_t len = strlen(str);
  870. return (pl.len == len && !memcmp(pl.ptr, str, len));
  871. }
  872. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2)
  873. {
  874. return (pl1.len == pl2.len && !memcmp(pl1.ptr, pl2.ptr, pl1.len));
  875. }
  876. int ptrlen_strcmp(ptrlen pl1, ptrlen pl2)
  877. {
  878. size_t minlen = pl1.len < pl2.len ? pl1.len : pl2.len;
  879. if (minlen) { /* tolerate plX.ptr==NULL as long as plX.len==0 */
  880. int cmp = memcmp(pl1.ptr, pl2.ptr, minlen);
  881. if (cmp)
  882. return cmp;
  883. }
  884. return pl1.len < pl2.len ? -1 : pl1.len > pl2.len ? +1 : 0;
  885. }
  886. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail)
  887. {
  888. if (whole.len >= prefix.len &&
  889. !memcmp(whole.ptr, prefix.ptr, prefix.len)) {
  890. if (tail) {
  891. tail->ptr = (const char *)whole.ptr + prefix.len;
  892. tail->len = whole.len - prefix.len;
  893. }
  894. return true;
  895. }
  896. return false;
  897. }
  898. bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
  899. {
  900. if (whole.len >= suffix.len &&
  901. !memcmp((char *)whole.ptr + (whole.len - suffix.len),
  902. suffix.ptr, suffix.len)) {
  903. if (tail) {
  904. tail->ptr = whole.ptr;
  905. tail->len = whole.len - suffix.len;
  906. }
  907. return true;
  908. }
  909. return false;
  910. }
  911. ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
  912. {
  913. const char *p = input->ptr, *end = p + input->len;
  914. ptrlen toret;
  915. while (p < end && strchr(separators, *p))
  916. p++;
  917. toret.ptr = p;
  918. while (p < end && !strchr(separators, *p))
  919. p++;
  920. toret.len = p - (const char *)toret.ptr;
  921. { // WINSCP
  922. size_t to_consume = p - (const char *)input->ptr;
  923. assert(to_consume <= input->len);
  924. input->ptr = (const char *)input->ptr + to_consume;
  925. input->len -= to_consume;
  926. } // WINSCP
  927. return toret;
  928. }
  929. char *mkstr(ptrlen pl)
  930. {
  931. char *p = snewn(pl.len + 1, char);
  932. memcpy(p, pl.ptr, pl.len);
  933. p[pl.len] = '\0';
  934. return p;
  935. }
  936. bool strstartswith(const char *s, const char *t)
  937. {
  938. return !memcmp(s, t, strlen(t));
  939. }
  940. bool strendswith(const char *s, const char *t)
  941. {
  942. size_t slen = strlen(s), tlen = strlen(t);
  943. return slen >= tlen && !strcmp(s + (slen - tlen), t);
  944. }
  945. size_t encode_utf8(void *output, unsigned long ch)
  946. {
  947. unsigned char *start = (unsigned char *)output, *p = start;
  948. if (ch < 0x80) {
  949. *p++ = ch;
  950. } else if (ch < 0x800) {
  951. *p++ = 0xC0 | (ch >> 6);
  952. *p++ = 0x80 | (ch & 0x3F);
  953. } else if (ch < 0x10000) {
  954. *p++ = 0xE0 | (ch >> 12);
  955. *p++ = 0x80 | ((ch >> 6) & 0x3F);
  956. *p++ = 0x80 | (ch & 0x3F);
  957. } else {
  958. *p++ = 0xF0 | (ch >> 18);
  959. *p++ = 0x80 | ((ch >> 12) & 0x3F);
  960. *p++ = 0x80 | ((ch >> 6) & 0x3F);
  961. *p++ = 0x80 | (ch & 0x3F);
  962. }
  963. return p - start;
  964. }