misc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Header for misc.c.
  3. */
  4. #ifndef PUTTY_MISC_H
  5. #define PUTTY_MISC_H
  6. #include "defs.h"
  7. #include "puttymem.h"
  8. #include "marshal.h"
  9. #include <stdio.h> /* for FILE * */
  10. #include <stdarg.h> /* for va_list */
  11. #include <stdlib.h> /* for abort */
  12. #include <time.h> /* for struct tm */
  13. #include <limits.h> /* for INT_MAX/MIN */
  14. #include <assert.h> /* for assert (obviously) */
  15. unsigned long parse_blocksize(const char *bs);
  16. char ctrlparse(char *s, char **next);
  17. size_t host_strcspn(const char *s, const char *set);
  18. char *host_strchr(const char *s, int c);
  19. char *host_strrchr(const char *s, int c);
  20. char *host_strduptrim(const char *s);
  21. char *dupstr(const char *s);
  22. char *dupcat(const char *s1, ...);
  23. char *dupprintf(const char *fmt, ...)
  24. #ifdef __GNUC__
  25. __attribute__ ((format (printf, 1, 2)))
  26. #endif
  27. ;
  28. char *dupvprintf(const char *fmt, va_list ap);
  29. void burnstr(char *string);
  30. struct strbuf {
  31. char *s;
  32. unsigned char *u;
  33. int len;
  34. BinarySink_IMPLEMENTATION;
  35. /* (also there's a surrounding implementation struct in misc.c) */
  36. };
  37. strbuf *strbuf_new(void);
  38. void strbuf_free(strbuf *buf);
  39. void *strbuf_append(strbuf *buf, size_t len);
  40. char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
  41. void strbuf_catf(strbuf *buf, const char *fmt, ...);
  42. void strbuf_catfv(strbuf *buf, const char *fmt, va_list ap);
  43. strbuf *strbuf_new_for_agent_query(void);
  44. void strbuf_finalise_agent_query(strbuf *buf);
  45. /* String-to-Unicode converters that auto-allocate the destination and
  46. * work around the rather deficient interface of mb_to_wc.
  47. *
  48. * These actually live in miscucs.c, not misc.c (the distinction being
  49. * that the former is only linked into tools that also have the main
  50. * Unicode support). */
  51. wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
  52. wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
  53. static inline int toint(unsigned u)
  54. {
  55. /*
  56. * Convert an unsigned to an int, without running into the
  57. * undefined behaviour which happens by the strict C standard if
  58. * the value overflows. You'd hope that sensible compilers would
  59. * do the sensible thing in response to a cast, but actually I
  60. * don't trust modern compilers not to do silly things like
  61. * assuming that _obviously_ you wouldn't have caused an overflow
  62. * and so they can elide an 'if (i < 0)' test immediately after
  63. * the cast.
  64. *
  65. * Sensible compilers ought of course to optimise this entire
  66. * function into 'just return the input value', and since it's
  67. * also declared inline, elide it completely in their output.
  68. */
  69. if (u <= (unsigned)INT_MAX)
  70. return (int)u;
  71. else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
  72. return INT_MIN + (int)(u - (unsigned)INT_MIN);
  73. else
  74. return INT_MIN; /* fallback; should never occur on binary machines */
  75. }
  76. char *fgetline(FILE *fp);
  77. char *chomp(char *str);
  78. bool strstartswith(const char *s, const char *t);
  79. bool strendswith(const char *s, const char *t);
  80. void base64_encode_atom(const unsigned char *data, int n, char *out);
  81. int base64_decode_atom(const char *atom, unsigned char *out);
  82. struct bufchain_granule;
  83. struct bufchain_tag {
  84. struct bufchain_granule *head, *tail;
  85. size_t buffersize; /* current amount of buffered data */
  86. void (*queue_idempotent_callback)(IdempotentCallback *ic);
  87. IdempotentCallback *ic;
  88. };
  89. void bufchain_init(bufchain *ch);
  90. void bufchain_clear(bufchain *ch);
  91. size_t bufchain_size(bufchain *ch);
  92. void bufchain_add(bufchain *ch, const void *data, size_t len);
  93. ptrlen bufchain_prefix(bufchain *ch);
  94. void bufchain_consume(bufchain *ch, size_t len);
  95. void bufchain_fetch(bufchain *ch, void *data, size_t len);
  96. void bufchain_fetch_consume(bufchain *ch, void *data, size_t len);
  97. bool bufchain_try_fetch_consume(bufchain *ch, void *data, size_t len);
  98. size_t bufchain_fetch_consume_up_to(bufchain *ch, void *data, size_t len);
  99. void bufchain_set_callback_inner(
  100. bufchain *ch, IdempotentCallback *ic,
  101. void (*queue_idempotent_callback)(IdempotentCallback *ic));
  102. static inline void bufchain_set_callback(bufchain *ch, IdempotentCallback *ic)
  103. {
  104. extern void queue_idempotent_callback(struct IdempotentCallback *ic);
  105. /* Wrapper that puts in the standard queue_idempotent_callback
  106. * function. Lives here rather than in utils.c so that standalone
  107. * programs can use the bufchain facility without this optional
  108. * callback feature and not need to provide a stub of
  109. * queue_idempotent_callback. */
  110. bufchain_set_callback_inner(ch, ic, queue_idempotent_callback);
  111. }
  112. void sanitise_term_data(bufchain *out, const void *vdata, size_t len);
  113. bool validate_manual_hostkey(char *key);
  114. struct tm ltime(void);
  115. /*
  116. * Special form of strcmp which can cope with NULL inputs. NULL is
  117. * defined to sort before even the empty string.
  118. */
  119. int nullstrcmp(const char *a, const char *b);
  120. static inline ptrlen make_ptrlen(const void *ptr, size_t len)
  121. {
  122. ptrlen pl;
  123. pl.ptr = ptr;
  124. pl.len = len;
  125. return pl;
  126. }
  127. static inline ptrlen ptrlen_from_asciz(const char *str)
  128. {
  129. return make_ptrlen(str, strlen(str));
  130. }
  131. static inline ptrlen ptrlen_from_strbuf(strbuf *sb)
  132. {
  133. return make_ptrlen(sb->u, sb->len);
  134. }
  135. bool ptrlen_eq_string(ptrlen pl, const char *str);
  136. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
  137. int ptrlen_strcmp(ptrlen pl1, ptrlen pl2);
  138. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
  139. char *mkstr(ptrlen pl);
  140. int string_length_for_printf(size_t);
  141. /* Derive two printf arguments from a ptrlen, suitable for "%.*s" */
  142. #define PTRLEN_PRINTF(pl) \
  143. string_length_for_printf((pl).len), (const char *)(pl).ptr
  144. /* Make a ptrlen out of a compile-time string literal. We try to
  145. * enforce that it _is_ a string literal by token-pasting "" on to it,
  146. * which should provoke a compile error if it's any other kind of
  147. * string. */
  148. #define PTRLEN_LITERAL(stringlit) \
  149. TYPECHECK("" stringlit "", make_ptrlen(stringlit, sizeof(stringlit)-1))
  150. /* Make a ptrlen out of a constant byte array. */
  151. #define PTRLEN_FROM_CONST_BYTES(a) make_ptrlen(a, sizeof(a))
  152. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  153. * than memset because we don't need the fill char parameter; also
  154. * attempts (by fiddly use of volatile) to inhibit the compiler from
  155. * over-cleverly trying to optimise the memset away because it knows
  156. * the variable is going out of scope. */
  157. void smemclr(void *b, size_t len);
  158. /* Compare two fixed-length chunks of memory for equality, without
  159. * data-dependent control flow (so an attacker with a very accurate
  160. * stopwatch can't try to guess where the first mismatching byte was).
  161. * Returns false for mismatch or true for equality (unlike memcmp),
  162. * hinted at by the 'eq' in the name. */
  163. bool smemeq(const void *av, const void *bv, size_t len);
  164. char *buildinfo(const char *newline);
  165. /*
  166. * A function you can put at points in the code where execution should
  167. * never reach in the first place. Better than assert(false), or even
  168. * assert(false && "some explanatory message"), because some compilers
  169. * don't interpret assert(false) as a declaration of unreachability,
  170. * so they may still warn about pointless things like some variable
  171. * not being initialised on the unreachable code path.
  172. *
  173. * I follow the assertion with a call to abort() just in case someone
  174. * compiles with -DNDEBUG, and I wrap that abort inside my own
  175. * function labelled NORETURN just in case some unusual kind of system
  176. * header wasn't foresighted enough to label abort() itself that way.
  177. */
  178. static inline NORETURN void unreachable_internal(void) {
  179. #ifndef WINSCP_VS
  180. // Not to try to link to VS abort
  181. abort();
  182. #endif
  183. }
  184. #define unreachable(msg) (assert(false && msg), unreachable_internal())
  185. /*
  186. * Debugging functions.
  187. *
  188. * Output goes to debug.log
  189. *
  190. * debug() is like printf().
  191. *
  192. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  193. * is that dmemdumpl() is more suited for when the memory address is
  194. * important (say because you'll be recording pointer values later
  195. * on). dmemdump() is more concise.
  196. */
  197. #ifdef DEBUG
  198. void debug_printf(const char *fmt, ...);
  199. void debug_memdump(const void *buf, int len, bool L);
  200. #define debug(...) (debug_printf(__VA_ARGS__))
  201. #define dmemdump(buf,len) debug_memdump (buf, len, false);
  202. #define dmemdumpl(buf,len) debug_memdump (buf, len, true);
  203. #else
  204. #define debug(...)
  205. #define dmemdump(buf,len)
  206. #define dmemdumpl(buf,len)
  207. #endif
  208. #ifndef lenof
  209. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  210. #endif
  211. #ifndef min
  212. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  213. #endif
  214. #ifndef max
  215. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  216. #endif
  217. static inline uint64_t GET_64BIT_LSB_FIRST(const void *vp)
  218. {
  219. const uint8_t *p = (const uint8_t *)vp;
  220. return (((uint64_t)p[0] ) | ((uint64_t)p[1] << 8) |
  221. ((uint64_t)p[2] << 16) | ((uint64_t)p[3] << 24) |
  222. ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40) |
  223. ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56));
  224. }
  225. static inline void PUT_64BIT_LSB_FIRST(void *vp, uint64_t value)
  226. {
  227. uint8_t *p = (uint8_t *)vp;
  228. // WINSCP cast to uint8_t
  229. p[0] = (uint8_t)value;
  230. p[1] = (uint8_t)((value) >> 8);
  231. p[2] = (uint8_t)((value) >> 16);
  232. p[3] = (uint8_t)((value) >> 24);
  233. p[4] = (uint8_t)((value) >> 32);
  234. p[5] = (uint8_t)((value) >> 40);
  235. p[6] = (uint8_t)((value) >> 48);
  236. p[7] = (uint8_t)((value) >> 56);
  237. }
  238. static inline uint32_t GET_32BIT_LSB_FIRST(const void *vp)
  239. {
  240. const uint8_t *p = (const uint8_t *)vp;
  241. return (((uint32_t)p[0] ) | ((uint32_t)p[1] << 8) |
  242. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24));
  243. }
  244. static inline void PUT_32BIT_LSB_FIRST(void *vp, uint32_t value)
  245. {
  246. uint8_t *p = (uint8_t *)vp;
  247. // WINSCP cast to uint8_t
  248. p[0] = (uint8_t)value;
  249. p[1] = (uint8_t)((value) >> 8);
  250. p[2] = (uint8_t)((value) >> 16);
  251. p[3] = (uint8_t)((value) >> 24);
  252. }
  253. static inline uint16_t GET_16BIT_LSB_FIRST(const void *vp)
  254. {
  255. const uint8_t *p = (const uint8_t *)vp;
  256. return (((uint16_t)p[0] ) | ((uint16_t)p[1] << 8));
  257. }
  258. static inline void PUT_16BIT_LSB_FIRST(void *vp, uint16_t value)
  259. {
  260. uint8_t *p = (uint8_t *)vp;
  261. // WINSCP cast to uint8_t
  262. p[0] = (uint8_t)value;
  263. p[1] = (uint8_t)((value) >> 8);
  264. }
  265. static inline uint64_t GET_64BIT_MSB_FIRST(const void *vp)
  266. {
  267. const uint8_t *p = (const uint8_t *)vp;
  268. return (((uint64_t)p[7] ) | ((uint64_t)p[6] << 8) |
  269. ((uint64_t)p[5] << 16) | ((uint64_t)p[4] << 24) |
  270. ((uint64_t)p[3] << 32) | ((uint64_t)p[2] << 40) |
  271. ((uint64_t)p[1] << 48) | ((uint64_t)p[0] << 56));
  272. }
  273. static inline void PUT_64BIT_MSB_FIRST(void *vp, uint64_t value)
  274. {
  275. uint8_t *p = (uint8_t *)vp;
  276. // WINSCP cast to uint8_t
  277. p[7] = (uint8_t)value;
  278. p[6] = (uint8_t)((value) >> 8);
  279. p[5] = (uint8_t)((value) >> 16);
  280. p[4] = (uint8_t)((value) >> 24);
  281. p[3] = (uint8_t)((value) >> 32);
  282. p[2] = (uint8_t)((value) >> 40);
  283. p[1] = (uint8_t)((value) >> 48);
  284. p[0] = (uint8_t)((value) >> 56);
  285. }
  286. static inline uint32_t GET_32BIT_MSB_FIRST(const void *vp)
  287. {
  288. const uint8_t *p = (const uint8_t *)vp;
  289. return (((uint32_t)p[3] ) | ((uint32_t)p[2] << 8) |
  290. ((uint32_t)p[1] << 16) | ((uint32_t)p[0] << 24));
  291. }
  292. static inline void PUT_32BIT_MSB_FIRST(void *vp, uint32_t value)
  293. {
  294. uint8_t *p = (uint8_t *)vp;
  295. // WINSCP cast to uint8_t
  296. p[3] = (uint8_t)value;
  297. p[2] = (uint8_t)((value) >> 8);
  298. p[1] = (uint8_t)((value) >> 16);
  299. p[0] = (uint8_t)((value) >> 24);
  300. }
  301. static inline uint16_t GET_16BIT_MSB_FIRST(const void *vp)
  302. {
  303. const uint8_t *p = (const uint8_t *)vp;
  304. return (((uint16_t)p[1] ) | ((uint16_t)p[0] << 8));
  305. }
  306. static inline void PUT_16BIT_MSB_FIRST(void *vp, uint16_t value)
  307. {
  308. uint8_t *p = (uint8_t *)vp;
  309. p[1] = (uint8_t)value;
  310. p[0] = (value) >> 8;
  311. }
  312. /* Replace NULL with the empty string, permitting an idiom in which we
  313. * get a string (pointer,length) pair that might be NULL,0 and can
  314. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  315. static inline const char *NULLTOEMPTY(const char *s)
  316. {
  317. return s ? s : "";
  318. }
  319. #ifdef MPEXT
  320. // Recent PuTTY code uses C99 standard that allows code before initialization.
  321. // Mostly that code are assertions. This assert implementation allows being used before code.
  322. #define pinitassert(P) const int __assert_dummy = 1/((int)(P))
  323. #endif
  324. #endif