misc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. int 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. int bufchain_size(bufchain *ch);
  92. void bufchain_add(bufchain *ch, const void *data, int len);
  93. void bufchain_prefix(bufchain *ch, void **data, int *len);
  94. void bufchain_consume(bufchain *ch, int len);
  95. void bufchain_fetch(bufchain *ch, void *data, int len);
  96. void bufchain_fetch_consume(bufchain *ch, void *data, int len);
  97. bool bufchain_try_fetch_consume(bufchain *ch, void *data, int len);
  98. int bufchain_fetch_consume_up_to(bufchain *ch, void *data, int 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, int 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. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  151. * than memset because we don't need the fill char parameter; also
  152. * attempts (by fiddly use of volatile) to inhibit the compiler from
  153. * over-cleverly trying to optimise the memset away because it knows
  154. * the variable is going out of scope. */
  155. void smemclr(void *b, size_t len);
  156. /* Compare two fixed-length chunks of memory for equality, without
  157. * data-dependent control flow (so an attacker with a very accurate
  158. * stopwatch can't try to guess where the first mismatching byte was).
  159. * Returns false for mismatch or true for equality (unlike memcmp),
  160. * hinted at by the 'eq' in the name. */
  161. bool smemeq(const void *av, const void *bv, size_t len);
  162. char *buildinfo(const char *newline);
  163. /*
  164. * A function you can put at points in the code where execution should
  165. * never reach in the first place. Better than assert(false), or even
  166. * assert(false && "some explanatory message"), because some compilers
  167. * don't interpret assert(false) as a declaration of unreachability,
  168. * so they may still warn about pointless things like some variable
  169. * not being initialised on the unreachable code path.
  170. *
  171. * I follow the assertion with a call to abort() just in case someone
  172. * compiles with -DNDEBUG, and I wrap that abort inside my own
  173. * function labelled NORETURN just in case some unusual kind of system
  174. * header wasn't foresighted enough to label abort() itself that way.
  175. */
  176. static inline NORETURN void unreachable_internal(void) {
  177. #ifndef WINSCP_VS
  178. // Not to try to link to VS abort
  179. abort();
  180. #endif
  181. }
  182. #define unreachable(msg) (assert(false && msg), unreachable_internal())
  183. /*
  184. * Debugging functions.
  185. *
  186. * Output goes to debug.log
  187. *
  188. * debug() is like printf().
  189. *
  190. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  191. * is that dmemdumpl() is more suited for when the memory address is
  192. * important (say because you'll be recording pointer values later
  193. * on). dmemdump() is more concise.
  194. */
  195. #ifdef DEBUG
  196. void debug_printf(const char *fmt, ...);
  197. void debug_memdump(const void *buf, int len, bool L);
  198. #define debug(...) (debug_printf(__VA_ARGS__))
  199. #define dmemdump(buf,len) debug_memdump (buf, len, false);
  200. #define dmemdumpl(buf,len) debug_memdump (buf, len, true);
  201. #else
  202. #define debug(...)
  203. #define dmemdump(buf,len)
  204. #define dmemdumpl(buf,len)
  205. #endif
  206. #ifndef lenof
  207. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  208. #endif
  209. #ifndef min
  210. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  211. #endif
  212. #ifndef max
  213. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  214. #endif
  215. #define GET_64BIT_LSB_FIRST(cp) \
  216. (((uint64_t)(unsigned char)(cp)[0]) | \
  217. ((uint64_t)(unsigned char)(cp)[1] << 8) | \
  218. ((uint64_t)(unsigned char)(cp)[2] << 16) | \
  219. ((uint64_t)(unsigned char)(cp)[3] << 24) | \
  220. ((uint64_t)(unsigned char)(cp)[4] << 32) | \
  221. ((uint64_t)(unsigned char)(cp)[5] << 40) | \
  222. ((uint64_t)(unsigned char)(cp)[6] << 48) | \
  223. ((uint64_t)(unsigned char)(cp)[7] << 56))
  224. #define PUT_64BIT_LSB_FIRST(cp, value) ( \
  225. (cp)[0] = (unsigned char)(value), \
  226. (cp)[1] = (unsigned char)((value) >> 8), \
  227. (cp)[2] = (unsigned char)((value) >> 16), \
  228. (cp)[3] = (unsigned char)((value) >> 24), \
  229. (cp)[4] = (unsigned char)((value) >> 32), \
  230. (cp)[5] = (unsigned char)((value) >> 40), \
  231. (cp)[6] = (unsigned char)((value) >> 48), \
  232. (cp)[7] = (unsigned char)((value) >> 56) )
  233. #define GET_32BIT_LSB_FIRST(cp) \
  234. (((uint32_t)(unsigned char)(cp)[0]) | \
  235. ((uint32_t)(unsigned char)(cp)[1] << 8) | \
  236. ((uint32_t)(unsigned char)(cp)[2] << 16) | \
  237. ((uint32_t)(unsigned char)(cp)[3] << 24))
  238. #define PUT_32BIT_LSB_FIRST(cp, value) ( \
  239. (cp)[0] = (unsigned char)(value), \
  240. (cp)[1] = (unsigned char)((value) >> 8), \
  241. (cp)[2] = (unsigned char)((value) >> 16), \
  242. (cp)[3] = (unsigned char)((value) >> 24) )
  243. #define GET_16BIT_LSB_FIRST(cp) \
  244. (((unsigned long)(unsigned char)(cp)[0]) | \
  245. ((unsigned long)(unsigned char)(cp)[1] << 8))
  246. #define PUT_16BIT_LSB_FIRST(cp, value) ( \
  247. (cp)[0] = (unsigned char)(value), \
  248. (cp)[1] = (unsigned char)((value) >> 8) )
  249. #define GET_32BIT_MSB_FIRST(cp) \
  250. (((uint32_t)(unsigned char)(cp)[0] << 24) | \
  251. ((uint32_t)(unsigned char)(cp)[1] << 16) | \
  252. ((uint32_t)(unsigned char)(cp)[2] << 8) | \
  253. ((uint32_t)(unsigned char)(cp)[3]))
  254. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  255. #define PUT_32BIT_MSB_FIRST(cp, value) ( \
  256. (cp)[0] = (unsigned char)((value) >> 24), \
  257. (cp)[1] = (unsigned char)((value) >> 16), \
  258. (cp)[2] = (unsigned char)((value) >> 8), \
  259. (cp)[3] = (unsigned char)(value) )
  260. #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
  261. #define GET_64BIT_MSB_FIRST(cp) \
  262. (((uint64_t)(unsigned char)(cp)[0] << 56) | \
  263. ((uint64_t)(unsigned char)(cp)[1] << 48) | \
  264. ((uint64_t)(unsigned char)(cp)[2] << 40) | \
  265. ((uint64_t)(unsigned char)(cp)[3] << 32) | \
  266. ((uint64_t)(unsigned char)(cp)[4] << 24) | \
  267. ((uint64_t)(unsigned char)(cp)[5] << 16) | \
  268. ((uint64_t)(unsigned char)(cp)[6] << 8) | \
  269. ((uint64_t)(unsigned char)(cp)[7]))
  270. #define PUT_64BIT_MSB_FIRST(cp, value) ( \
  271. (cp)[0] = (unsigned char)((value) >> 56), \
  272. (cp)[1] = (unsigned char)((value) >> 48), \
  273. (cp)[2] = (unsigned char)((value) >> 40), \
  274. (cp)[3] = (unsigned char)((value) >> 32), \
  275. (cp)[4] = (unsigned char)((value) >> 24), \
  276. (cp)[5] = (unsigned char)((value) >> 16), \
  277. (cp)[6] = (unsigned char)((value) >> 8), \
  278. (cp)[7] = (unsigned char)(value) )
  279. #define GET_16BIT_MSB_FIRST(cp) \
  280. (((unsigned long)(unsigned char)(cp)[0] << 8) | \
  281. ((unsigned long)(unsigned char)(cp)[1]))
  282. #define PUT_16BIT_MSB_FIRST(cp, value) ( \
  283. (cp)[0] = (unsigned char)((value) >> 8), \
  284. (cp)[1] = (unsigned char)(value) )
  285. /* Replace NULL with the empty string, permitting an idiom in which we
  286. * get a string (pointer,length) pair that might be NULL,0 and can
  287. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  288. #define NULLTOEMPTY(s) ((s)?(s):"")
  289. #ifdef MPEXT
  290. // Recent PuTTY code uses C99 standard that allows code before initialization.
  291. // Mostly that code are assertions. This assert implementation allows being used before code.
  292. #define pinitassert(P) const int __assert_dummy = 1/((int)(P))
  293. #endif
  294. #endif