1
0

misc.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Header for miscellaneous helper functions, mostly defined in the
  3. * utils subdirectory.
  4. */
  5. #ifndef PUTTY_MISC_H
  6. #define PUTTY_MISC_H
  7. #include "defs.h"
  8. #include "puttymem.h"
  9. #include "marshal.h"
  10. #include <stdio.h> /* for FILE * */
  11. #include <stdarg.h> /* for va_list */
  12. #include <stdlib.h> /* for abort */
  13. #include <time.h> /* for struct tm */
  14. #include <limits.h> /* for INT_MAX/MIN */
  15. #include <assert.h> /* for assert (obviously) */
  16. unsigned long parse_blocksize(const char *bs);
  17. char ctrlparse(char *s, char **next);
  18. size_t host_strcspn(const char *s, const char *set);
  19. char *host_strchr(const char *s, int c);
  20. char *host_strrchr(const char *s, int c);
  21. char *host_strduptrim(const char *s);
  22. char *dupstr(const char *s);
  23. wchar_t *dupwcs(const wchar_t *s);
  24. char *dupcat_fn(const char *s1, ...);
  25. #define dupcat(...) dupcat_fn(__VA_ARGS__, (const char *)NULL)
  26. wchar_t *dupwcscat_fn(const wchar_t *s1, ...);
  27. #define dupwcscat(...) dupwcscat_fn(__VA_ARGS__, (const wchar_t *)NULL)
  28. char *dupprintf(const char *fmt, ...) PRINTF_LIKE(1, 2);
  29. char *dupvprintf(const char *fmt, va_list ap);
  30. void burnstr(char *string);
  31. void burnwcs(wchar_t *string);
  32. /*
  33. * The visible part of a strbuf structure. There's a surrounding
  34. * implementation struct in strbuf.c, which isn't exposed to client
  35. * code.
  36. */
  37. struct strbuf {
  38. char *s;
  39. unsigned char *u;
  40. size_t len;
  41. BinarySink_IMPLEMENTATION;
  42. };
  43. /* strbuf constructors: strbuf_new_nm and strbuf_new differ in that a
  44. * strbuf constructed using the _nm version will resize itself by
  45. * alloc/copy/smemclr/free instead of realloc. Use that version for
  46. * data sensitive enough that it's worth costing performance to
  47. * avoid copies of it lingering in process memory. */
  48. strbuf *strbuf_new(void);
  49. strbuf *strbuf_new_nm(void);
  50. /* Helpers to allocate a strbuf containing an existing string */
  51. strbuf *strbuf_dup(ptrlen string);
  52. strbuf *strbuf_dup_nm(ptrlen string);
  53. void strbuf_free(strbuf *buf);
  54. void *strbuf_append(strbuf *buf, size_t len);
  55. void strbuf_shrink_to(strbuf *buf, size_t new_len);
  56. void strbuf_shrink_by(strbuf *buf, size_t amount_to_remove);
  57. char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
  58. static inline void strbuf_clear(strbuf *buf) { strbuf_shrink_to(buf, 0); }
  59. bool strbuf_chomp(strbuf *buf, char char_to_remove);
  60. strbuf *strbuf_new_for_agent_query(void);
  61. void strbuf_finalise_agent_query(strbuf *buf);
  62. /* String-to-Unicode converters that auto-allocate the destination and
  63. * work around the rather deficient interface of mb_to_wc. */
  64. wchar_t *dup_mb_to_wc_c(int codepage, const char *string,
  65. size_t len, size_t *outlen_p);
  66. wchar_t *dup_mb_to_wc(int codepage, const char *string);
  67. char *dup_wc_to_mb_c(int codepage, const wchar_t *string,
  68. size_t len, const char *defchr, size_t *outlen_p);
  69. char *dup_wc_to_mb(int codepage, const wchar_t *string,
  70. const char *defchr);
  71. static inline int toint(unsigned u)
  72. {
  73. /*
  74. * Convert an unsigned to an int, without running into the
  75. * undefined behaviour which happens by the strict C standard if
  76. * the value overflows. You'd hope that sensible compilers would
  77. * do the sensible thing in response to a cast, but actually I
  78. * don't trust modern compilers not to do silly things like
  79. * assuming that _obviously_ you wouldn't have caused an overflow
  80. * and so they can elide an 'if (i < 0)' test immediately after
  81. * the cast.
  82. *
  83. * Sensible compilers ought of course to optimise this entire
  84. * function into 'just return the input value', and since it's
  85. * also declared inline, elide it completely in their output.
  86. */
  87. if (u <= (unsigned)INT_MAX)
  88. return (int)u;
  89. else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
  90. return INT_MIN + (int)(u - (unsigned)INT_MIN);
  91. else
  92. return INT_MIN; /* fallback; should never occur on binary machines */
  93. }
  94. char *fgetline(FILE *fp);
  95. bool read_file_into(BinarySink *bs, FILE *fp);
  96. char *chomp(char *str);
  97. bool strstartswith(const char *s, const char *t);
  98. bool strendswith(const char *s, const char *t);
  99. void base64_encode_atom(const unsigned char *data, int n, char *out);
  100. int base64_decode_atom(const char *atom, unsigned char *out);
  101. void base64_decode_bs(BinarySink *bs, ptrlen data);
  102. void base64_decode_fp(FILE *fp, ptrlen data);
  103. strbuf *base64_decode_sb(ptrlen data);
  104. void base64_encode_bs(BinarySink *bs, ptrlen data, int cpl);
  105. void base64_encode_fp(FILE *fp, ptrlen data, int cpl);
  106. strbuf *base64_encode_sb(ptrlen data, int cpl);
  107. bool base64_valid(ptrlen data);
  108. void percent_encode_bs(BinarySink *bs, ptrlen data, const char *badchars);
  109. void percent_encode_fp(FILE *fp, ptrlen data, const char *badchars);
  110. strbuf *percent_encode_sb(ptrlen data, const char *badchars);
  111. void percent_decode_bs(BinarySink *bs, ptrlen data);
  112. void percent_decode_fp(FILE *fp, ptrlen data);
  113. strbuf *percent_decode_sb(ptrlen data);
  114. struct bufchain_granule;
  115. struct bufchain_tag {
  116. struct bufchain_granule *head, *tail;
  117. size_t buffersize; /* current amount of buffered data */
  118. void (*queue_idempotent_callback)(IdempotentCallback *ic);
  119. IdempotentCallback *ic;
  120. };
  121. void bufchain_init(bufchain *ch);
  122. void bufchain_clear(bufchain *ch);
  123. size_t bufchain_size(bufchain *ch);
  124. void bufchain_add(bufchain *ch, const void *data, size_t len);
  125. ptrlen bufchain_prefix(bufchain *ch);
  126. void bufchain_consume(bufchain *ch, size_t len);
  127. void bufchain_fetch(bufchain *ch, void *data, size_t len);
  128. void bufchain_fetch_consume(bufchain *ch, void *data, size_t len);
  129. bool bufchain_try_consume(bufchain *ch, size_t len);
  130. bool bufchain_try_fetch(bufchain *ch, void *data, size_t len);
  131. bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len);
  132. size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len);
  133. void bufchain_set_callback_inner(
  134. bufchain *ch, IdempotentCallback *ic,
  135. void (*queue_idempotent_callback)(IdempotentCallback *ic));
  136. static inline void bufchain_set_callback(bufchain *ch, IdempotentCallback *ic)
  137. {
  138. extern void queue_idempotent_callback(struct IdempotentCallback *ic);
  139. /* Wrapper that puts in the standard queue_idempotent_callback
  140. * function. Lives here rather than in bufchain.c so that
  141. * standalone programs can use the bufchain facility without this
  142. * optional callback feature and not need to provide a stub of
  143. * queue_idempotent_callback. */
  144. bufchain_set_callback_inner(ch, ic, queue_idempotent_callback);
  145. }
  146. bool validate_manual_hostkey(char *key);
  147. struct tm ltime(void);
  148. /*
  149. * Special form of strcmp which can cope with NULL inputs. NULL is
  150. * defined to sort before even the empty string.
  151. */
  152. int nullstrcmp(const char *a, const char *b);
  153. static inline ptrlen make_ptrlen(const void *ptr, size_t len)
  154. {
  155. ptrlen pl;
  156. pl.ptr = ptr;
  157. pl.len = len;
  158. return pl;
  159. }
  160. static inline const void *ptrlen_end(ptrlen pl)
  161. {
  162. return (const char *)pl.ptr + pl.len;
  163. }
  164. static inline ptrlen make_ptrlen_startend(const void *startv, const void *endv)
  165. {
  166. const char *start = (const char *)startv, *end = (const char *)endv;
  167. assert(end >= start);
  168. { // WINSCP
  169. ptrlen pl;
  170. pl.ptr = start;
  171. pl.len = end - start;
  172. return pl;
  173. } // WINSCP
  174. }
  175. static inline ptrlen ptrlen_from_asciz(const char *str)
  176. {
  177. return make_ptrlen(str, strlen(str));
  178. }
  179. static inline ptrlen ptrlen_from_strbuf(strbuf *sb)
  180. {
  181. return make_ptrlen(sb->u, sb->len);
  182. }
  183. bool ptrlen_eq_string(ptrlen pl, const char *str);
  184. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
  185. int ptrlen_strcmp(ptrlen pl1, ptrlen pl2);
  186. /* ptrlen_startswith and ptrlen_endswith write through their 'tail'
  187. * argument if and only if it is non-NULL and they return true. Hence
  188. * you can write ptrlen_startswith(thing, prefix, &thing), writing
  189. * back to the same ptrlen it read from, to remove a prefix if present
  190. * and say whether it did so. */
  191. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
  192. bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail);
  193. ptrlen ptrlen_get_word(ptrlen *input, const char *separators);
  194. bool ptrlen_contains(ptrlen input, const char *characters);
  195. bool ptrlen_contains_only(ptrlen input, const char *characters);
  196. char *mkstr(ptrlen pl);
  197. int string_length_for_printf(size_t);
  198. /* Derive two printf arguments from a ptrlen, suitable for "%.*s" */
  199. #define PTRLEN_PRINTF(pl) \
  200. string_length_for_printf((pl).len), (const char *)(pl).ptr
  201. /* Make a ptrlen out of a compile-time string literal. We try to
  202. * enforce that it _is_ a string literal by token-pasting "" on to it,
  203. * which should provoke a compile error if it's any other kind of
  204. * string. */
  205. #define PTRLEN_LITERAL(stringlit) \
  206. TYPECHECK("" stringlit "", make_ptrlen(stringlit, sizeof(stringlit)-1))
  207. /* Make a ptrlen out of a compile-time string literal in a way that
  208. * allows you to declare the ptrlen itself as a compile-time initialiser. */
  209. #define PTRLEN_DECL_LITERAL(stringlit) \
  210. { TYPECHECK("" stringlit "", stringlit), sizeof(stringlit)-1 }
  211. /* Make a ptrlen out of a constant byte array. */
  212. #define PTRLEN_FROM_CONST_BYTES(a) make_ptrlen(a, sizeof(a))
  213. void wordwrap(BinarySink *bs, ptrlen input, size_t maxwid);
  214. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  215. * than memset because we don't need the fill char parameter; also
  216. * attempts (by fiddly use of volatile) to inhibit the compiler from
  217. * over-cleverly trying to optimise the memset away because it knows
  218. * the variable is going out of scope. */
  219. void smemclr(void *b, size_t len);
  220. /* Compare two fixed-length chunks of memory for equality, without
  221. * data-dependent control flow (so an attacker with a very accurate
  222. * stopwatch can't try to guess where the first mismatching byte was).
  223. * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
  224. * by the 'eq' in the name. */
  225. unsigned smemeq(const void *av, const void *bv, size_t len);
  226. /* Encode a wide-character string into UTF-8. Tolerates surrogates if
  227. * sizeof(wchar_t) == 2, assuming that in that case the wide string is
  228. * encoded in UTF-16. */
  229. char *encode_wide_string_as_utf8(const wchar_t *wstr);
  230. /* Decode UTF-8 to a wide-character string, emitting UTF-16 surrogates
  231. * if sizeof(wchar_t) == 2. */
  232. wchar_t *decode_utf8_to_wide_string(const char *ustr);
  233. /* Decode a single UTF-8 character. Returns U+FFFD for any of the
  234. * illegal cases. If the source is empty, returns L'\0' (and sets the
  235. * error indicator on the source, of course). */
  236. #define DECODE_UTF8_FAILURE_LIST(X) \
  237. X(DUTF8_SUCCESS, "success") \
  238. X(DUTF8_SPURIOUS_CONTINUATION, "spurious continuation byte") \
  239. X(DUTF8_ILLEGAL_BYTE, "illegal UTF-8 byte value") \
  240. X(DUTF8_E_OUT_OF_DATA, "unfinished multibyte encoding at end of string") \
  241. X(DUTF8_TRUNCATED_SEQUENCE, "multibyte encoding interrupted by " \
  242. "non-continuation byte") \
  243. X(DUTF8_OVERLONG_ENCODING, "overlong encoding") \
  244. X(DUTF8_ENCODED_SURROGATE, "Unicode surrogate character encoded in " \
  245. "UTF-8") \
  246. X(DUTF8_CODE_POINT_TOO_BIG, "code point outside the Unicode range") \
  247. /* end of list */
  248. typedef enum DecodeUTF8Failure {
  249. #define ENUM_DECL(sym, string) sym,
  250. DECODE_UTF8_FAILURE_LIST(ENUM_DECL)
  251. #undef ENUM_DECL
  252. DUTF8_N_FAILURE_CODES
  253. } DecodeUTF8Failure;
  254. unsigned decode_utf8(BinarySource *src, DecodeUTF8Failure *err);
  255. extern const char *const decode_utf8_error_strings[DUTF8_N_FAILURE_CODES];
  256. /* Decode a single UTF-8 character to an output buffer of the
  257. * platform's wchar_t. May write a pair of surrogates if
  258. * sizeof(wchar_t) == 2, assuming that in that case the wide string is
  259. * encoded in UTF-16. Otherwise, writes one character. Returns the
  260. * number written. */
  261. size_t decode_utf8_to_wchar(BinarySource *src, wchar_t *out,
  262. DecodeUTF8Failure *err);
  263. /* Normalise a UTF-8 string into Normalisation Form C. */
  264. strbuf *utf8_to_nfc(ptrlen input);
  265. /* Determine if a UTF-8 string contains any characters unknown to our
  266. * supported version of Unicode. */
  267. char *utf8_unknown_char(ptrlen input);
  268. /* Write a string out in C string-literal format. */
  269. void write_c_string_literal(FILE *fp, ptrlen str);
  270. char *buildinfo(const char *newline);
  271. /*
  272. * A function you can put at points in the code where execution should
  273. * never reach in the first place. Better than assert(false), or even
  274. * assert(false && "some explanatory message"), because some compilers
  275. * don't interpret assert(false) as a declaration of unreachability,
  276. * so they may still warn about pointless things like some variable
  277. * not being initialised on the unreachable code path.
  278. *
  279. * I follow the assertion with a call to abort() just in case someone
  280. * compiles with -DNDEBUG, and I wrap that abort inside my own
  281. * function labelled NORETURN just in case some unusual kind of system
  282. * header wasn't foresighted enough to label abort() itself that way.
  283. */
  284. static inline NORETURN void unreachable_internal(void) {
  285. #ifndef WINSCP_VS
  286. // Not to try to link to VS abort
  287. abort();
  288. #endif
  289. }
  290. #define unreachable(msg) (assert(false && msg), unreachable_internal())
  291. /*
  292. * Debugging functions.
  293. *
  294. * Output goes to debug.log
  295. *
  296. * debug() is like printf().
  297. *
  298. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  299. * is that dmemdumpl() is more suited for when the memory address is
  300. * important (say because you'll be recording pointer values later
  301. * on). dmemdump() is more concise.
  302. */
  303. #ifdef DEBUG
  304. void debug_printf(const char *fmt, ...) PRINTF_LIKE(1, 2);
  305. void debug_memdump(const void *buf, int len, bool L);
  306. #define debug(...) (debug_printf(__VA_ARGS__))
  307. #define dmemdump(buf,len) (debug_memdump(buf, len, false))
  308. #define dmemdumpl(buf,len) (debug_memdump(buf, len, true))
  309. /* Functions used only for debugging, not declared unless
  310. * defined(DEBUG) to avoid accidentally linking them in production */
  311. const char *conf_id(int key);
  312. #else
  313. #define debug(...) ((void)0)
  314. #define dmemdump(buf,len) ((void)0)
  315. #define dmemdumpl(buf,len) ((void)0)
  316. #endif
  317. #ifndef lenof
  318. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  319. #endif
  320. #ifndef min
  321. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  322. #endif
  323. #ifndef max
  324. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  325. #endif
  326. static inline uint64_t GET_64BIT_LSB_FIRST(const void *vp)
  327. {
  328. const uint8_t *p = (const uint8_t *)vp;
  329. return (((uint64_t)p[0] ) | ((uint64_t)p[1] << 8) |
  330. ((uint64_t)p[2] << 16) | ((uint64_t)p[3] << 24) |
  331. ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40) |
  332. ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56));
  333. }
  334. static inline void PUT_64BIT_LSB_FIRST(void *vp, uint64_t value)
  335. {
  336. uint8_t *p = (uint8_t *)vp;
  337. p[0] = (uint8_t)(value);
  338. p[1] = (uint8_t)(value >> 8);
  339. p[2] = (uint8_t)(value >> 16);
  340. p[3] = (uint8_t)(value >> 24);
  341. p[4] = (uint8_t)(value >> 32);
  342. p[5] = (uint8_t)(value >> 40);
  343. p[6] = (uint8_t)(value >> 48);
  344. p[7] = (uint8_t)(value >> 56);
  345. }
  346. static inline uint32_t GET_32BIT_LSB_FIRST(const void *vp)
  347. {
  348. const uint8_t *p = (const uint8_t *)vp;
  349. return (((uint32_t)p[0] ) | ((uint32_t)p[1] << 8) |
  350. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24));
  351. }
  352. static inline void PUT_32BIT_LSB_FIRST(void *vp, uint32_t value)
  353. {
  354. uint8_t *p = (uint8_t *)vp;
  355. p[0] = (uint8_t)(value);
  356. p[1] = (uint8_t)(value >> 8);
  357. p[2] = (uint8_t)(value >> 16);
  358. p[3] = (uint8_t)(value >> 24);
  359. }
  360. static inline uint16_t GET_16BIT_LSB_FIRST(const void *vp)
  361. {
  362. const uint8_t *p = (const uint8_t *)vp;
  363. return (((uint16_t)p[0] ) | ((uint16_t)p[1] << 8));
  364. }
  365. static inline void PUT_16BIT_LSB_FIRST(void *vp, uint16_t value)
  366. {
  367. uint8_t *p = (uint8_t *)vp;
  368. p[0] = (uint8_t)(value);
  369. p[1] = (uint8_t)(value >> 8);
  370. }
  371. static inline uint64_t GET_64BIT_MSB_FIRST(const void *vp)
  372. {
  373. const uint8_t *p = (const uint8_t *)vp;
  374. return (((uint64_t)p[7] ) | ((uint64_t)p[6] << 8) |
  375. ((uint64_t)p[5] << 16) | ((uint64_t)p[4] << 24) |
  376. ((uint64_t)p[3] << 32) | ((uint64_t)p[2] << 40) |
  377. ((uint64_t)p[1] << 48) | ((uint64_t)p[0] << 56));
  378. }
  379. static inline void PUT_64BIT_MSB_FIRST(void *vp, uint64_t value)
  380. {
  381. uint8_t *p = (uint8_t *)vp;
  382. p[7] = (uint8_t)(value);
  383. p[6] = (uint8_t)(value >> 8);
  384. p[5] = (uint8_t)(value >> 16);
  385. p[4] = (uint8_t)(value >> 24);
  386. p[3] = (uint8_t)(value >> 32);
  387. p[2] = (uint8_t)(value >> 40);
  388. p[1] = (uint8_t)(value >> 48);
  389. p[0] = (uint8_t)(value >> 56);
  390. }
  391. static inline uint32_t GET_32BIT_MSB_FIRST(const void *vp)
  392. {
  393. const uint8_t *p = (const uint8_t *)vp;
  394. return (((uint32_t)p[3] ) | ((uint32_t)p[2] << 8) |
  395. ((uint32_t)p[1] << 16) | ((uint32_t)p[0] << 24));
  396. }
  397. static inline void PUT_32BIT_MSB_FIRST(void *vp, uint32_t value)
  398. {
  399. uint8_t *p = (uint8_t *)vp;
  400. p[3] = (uint8_t)(value);
  401. p[2] = (uint8_t)(value >> 8);
  402. p[1] = (uint8_t)(value >> 16);
  403. p[0] = (uint8_t)(value >> 24);
  404. }
  405. static inline uint16_t GET_16BIT_MSB_FIRST(const void *vp)
  406. {
  407. const uint8_t *p = (const uint8_t *)vp;
  408. return (((uint16_t)p[1] ) | ((uint16_t)p[0] << 8));
  409. }
  410. static inline void PUT_16BIT_MSB_FIRST(void *vp, uint16_t value)
  411. {
  412. uint8_t *p = (uint8_t *)vp;
  413. p[1] = (uint8_t)(value);
  414. p[0] = (uint8_t)(value >> 8);
  415. }
  416. /* For use in X11-related applications, an endianness-variable form of
  417. * {GET,PUT}_16BIT which expects 'endian' to be either 'B' or 'l' */
  418. static inline uint16_t GET_16BIT_X11(char endian, const void *p)
  419. {
  420. return endian == 'B' ? GET_16BIT_MSB_FIRST(p) : GET_16BIT_LSB_FIRST(p);
  421. }
  422. static inline void PUT_16BIT_X11(char endian, void *p, uint16_t value)
  423. {
  424. if (endian == 'B')
  425. PUT_16BIT_MSB_FIRST(p, value);
  426. else
  427. PUT_16BIT_LSB_FIRST(p, value);
  428. }
  429. /* Replace NULL with the empty string, permitting an idiom in which we
  430. * get a string (pointer,length) pair that might be NULL,0 and can
  431. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  432. static inline const char *NULLTOEMPTY(const char *s)
  433. {
  434. return s ? s : "";
  435. }
  436. /* StripCtrlChars, defined in stripctrl.c: an adapter you can put on
  437. * the front of one BinarySink and which functions as one in turn.
  438. * Interprets its input as a stream of multibyte characters in the
  439. * system locale, and removes any that are not either printable
  440. * characters or newlines. */
  441. struct StripCtrlChars {
  442. BinarySink_IMPLEMENTATION;
  443. /* and this is contained in a larger structure */
  444. };
  445. StripCtrlChars *stripctrl_new(
  446. BinarySink *bs_out, bool permit_cr, wchar_t substitution);
  447. StripCtrlChars *stripctrl_new_term_fn(
  448. BinarySink *bs_out, bool permit_cr, wchar_t substitution,
  449. Terminal *term, unsigned long (*translate)(
  450. Terminal *, term_utf8_decode *, unsigned char));
  451. #define stripctrl_new_term(bs, cr, sub, term) \
  452. stripctrl_new_term_fn(bs, cr, sub, term, term_translate)
  453. void stripctrl_retarget(StripCtrlChars *sccpub, BinarySink *new_bs_out);
  454. void stripctrl_reset(StripCtrlChars *sccpub);
  455. void stripctrl_free(StripCtrlChars *sanpub);
  456. void stripctrl_enable_line_limiting(StripCtrlChars *sccpub);
  457. #ifndef WINSCP
  458. char *stripctrl_string_ptrlen(StripCtrlChars *sccpub, ptrlen str);
  459. static inline char *stripctrl_string(StripCtrlChars *sccpub, const char *str)
  460. {
  461. return stripctrl_string_ptrlen(sccpub, ptrlen_from_asciz(str));
  462. }
  463. #endif
  464. #ifdef MPEXT
  465. // Recent PuTTY code uses C99 standard that allows code before initialization.
  466. // Frequently that code are assertions. This assert implementation allows being used before code.
  467. #define pinitassert(P) const int __assert_dummy = 1/((int)(P))
  468. #endif
  469. /*
  470. * A mechanism for loading a file from disk into a memory buffer where
  471. * it can be picked apart as a BinarySource.
  472. */
  473. struct LoadedFile {
  474. char *data;
  475. size_t len, max_size;
  476. BinarySource_IMPLEMENTATION;
  477. };
  478. typedef enum {
  479. LF_OK, /* file loaded successfully */
  480. LF_TOO_BIG, /* file didn't fit in buffer */
  481. LF_ERROR, /* error from stdio layer */
  482. } LoadFileStatus;
  483. LoadedFile *lf_new(size_t max_size);
  484. void lf_free(LoadedFile *lf);
  485. LoadFileStatus lf_load_fp(LoadedFile *lf, FILE *fp);
  486. LoadFileStatus lf_load(LoadedFile *lf, const Filename *filename);
  487. static inline ptrlen ptrlen_from_lf(LoadedFile *lf)
  488. { return make_ptrlen(lf->data, lf->len); }
  489. /* Set the memory block of 'size' bytes at 'out' to the bitwise XOR of
  490. * the two blocks of the same size at 'in1' and 'in2'.
  491. *
  492. * 'out' may point to exactly the same address as one of the inputs,
  493. * but if the input and output blocks overlap in any other way, the
  494. * result of this function is not guaranteed. No memmove-style effort
  495. * is made to handle difficult overlap cases. */
  496. void memxor(uint8_t *out, const uint8_t *in1, const uint8_t *in2, size_t size);
  497. /* Boolean expressions used in OpenSSH certificate configuration */
  498. bool cert_expr_valid(const char *expression,
  499. char **error_msg, ptrlen *error_loc);
  500. bool cert_expr_match_str(const char *expression,
  501. const char *hostname, unsigned port);
  502. /* Build a certificate expression out of hostname wildcards. Required
  503. * to handle legacy configuration from early in development, when
  504. * multiple wildcards were stored separately in config, implicitly
  505. * ORed together. */
  506. CertExprBuilder *cert_expr_builder_new(void);
  507. void cert_expr_builder_free(CertExprBuilder *eb);
  508. void cert_expr_builder_add(CertExprBuilder *eb, const char *wildcard);
  509. char *cert_expr_expression(CertExprBuilder *eb);
  510. #endif