misc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <time.h> /* for struct tm */
  12. #include <limits.h> /* for INT_MAX/MIN */
  13. unsigned long parse_blocksize(const char *bs);
  14. char ctrlparse(char *s, char **next);
  15. size_t host_strcspn(const char *s, const char *set);
  16. char *host_strchr(const char *s, int c);
  17. char *host_strrchr(const char *s, int c);
  18. char *host_strduptrim(const char *s);
  19. char *dupstr(const char *s);
  20. char *dupcat(const char *s1, ...);
  21. char *dupprintf(const char *fmt, ...)
  22. #ifdef __GNUC__
  23. __attribute__ ((format (printf, 1, 2)))
  24. #endif
  25. ;
  26. char *dupvprintf(const char *fmt, va_list ap);
  27. void burnstr(char *string);
  28. struct strbuf {
  29. char *s;
  30. unsigned char *u;
  31. int len;
  32. BinarySink_IMPLEMENTATION;
  33. /* (also there's a surrounding implementation struct in misc.c) */
  34. };
  35. strbuf *strbuf_new(void);
  36. void strbuf_free(strbuf *buf);
  37. void *strbuf_append(strbuf *buf, size_t len);
  38. char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
  39. void strbuf_catf(strbuf *buf, const char *fmt, ...);
  40. void strbuf_catfv(strbuf *buf, const char *fmt, va_list ap);
  41. strbuf *strbuf_new_for_agent_query(void);
  42. void strbuf_finalise_agent_query(strbuf *buf);
  43. /* String-to-Unicode converters that auto-allocate the destination and
  44. * work around the rather deficient interface of mb_to_wc.
  45. *
  46. * These actually live in miscucs.c, not misc.c (the distinction being
  47. * that the former is only linked into tools that also have the main
  48. * Unicode support). */
  49. wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
  50. wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
  51. static inline int toint(unsigned u)
  52. {
  53. /*
  54. * Convert an unsigned to an int, without running into the
  55. * undefined behaviour which happens by the strict C standard if
  56. * the value overflows. You'd hope that sensible compilers would
  57. * do the sensible thing in response to a cast, but actually I
  58. * don't trust modern compilers not to do silly things like
  59. * assuming that _obviously_ you wouldn't have caused an overflow
  60. * and so they can elide an 'if (i < 0)' test immediately after
  61. * the cast.
  62. *
  63. * Sensible compilers ought of course to optimise this entire
  64. * function into 'just return the input value', and since it's
  65. * also declared inline, elide it completely in their output.
  66. */
  67. if (u <= (unsigned)INT_MAX)
  68. return (int)u;
  69. else if (u >= (unsigned)INT_MIN) /* wrap in cast _to_ unsigned is OK */
  70. return INT_MIN + (int)(u - (unsigned)INT_MIN);
  71. else
  72. return INT_MIN; /* fallback; should never occur on binary machines */
  73. }
  74. char *fgetline(FILE *fp);
  75. char *chomp(char *str);
  76. bool strstartswith(const char *s, const char *t);
  77. bool strendswith(const char *s, const char *t);
  78. void base64_encode_atom(const unsigned char *data, int n, char *out);
  79. int base64_decode_atom(const char *atom, unsigned char *out);
  80. struct bufchain_granule;
  81. struct bufchain_tag {
  82. struct bufchain_granule *head, *tail;
  83. int buffersize; /* current amount of buffered data */
  84. IdempotentCallback *ic;
  85. };
  86. void bufchain_init(bufchain *ch);
  87. void bufchain_clear(bufchain *ch);
  88. int bufchain_size(bufchain *ch);
  89. void bufchain_add(bufchain *ch, const void *data, int len);
  90. void bufchain_prefix(bufchain *ch, void **data, int *len);
  91. void bufchain_consume(bufchain *ch, int len);
  92. void bufchain_fetch(bufchain *ch, void *data, int len);
  93. void bufchain_fetch_consume(bufchain *ch, void *data, int len);
  94. bool bufchain_try_fetch_consume(bufchain *ch, void *data, int len);
  95. int bufchain_fetch_consume_up_to(bufchain *ch, void *data, int len);
  96. void sanitise_term_data(bufchain *out, const void *vdata, int len);
  97. bool validate_manual_hostkey(char *key);
  98. struct tm ltime(void);
  99. /*
  100. * Special form of strcmp which can cope with NULL inputs. NULL is
  101. * defined to sort before even the empty string.
  102. */
  103. int nullstrcmp(const char *a, const char *b);
  104. static inline ptrlen make_ptrlen(const void *ptr, size_t len)
  105. {
  106. ptrlen pl;
  107. pl.ptr = ptr;
  108. pl.len = len;
  109. return pl;
  110. }
  111. static inline ptrlen ptrlen_from_asciz(const char *str)
  112. {
  113. return make_ptrlen(str, strlen(str));
  114. }
  115. static inline ptrlen ptrlen_from_strbuf(strbuf *sb)
  116. {
  117. return make_ptrlen(sb->u, sb->len);
  118. }
  119. bool ptrlen_eq_string(ptrlen pl, const char *str);
  120. bool ptrlen_eq_ptrlen(ptrlen pl1, ptrlen pl2);
  121. bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
  122. char *mkstr(ptrlen pl);
  123. int string_length_for_printf(size_t);
  124. /* Derive two printf arguments from a ptrlen, suitable for "%.*s" */
  125. #define PTRLEN_PRINTF(pl) \
  126. string_length_for_printf((pl).len), (const char *)(pl).ptr
  127. /* Make a ptrlen out of a compile-time string literal. We try to
  128. * enforce that it _is_ a string literal by token-pasting "" on to it,
  129. * which should provoke a compile error if it's any other kind of
  130. * string. */
  131. #define PTRLEN_LITERAL(stringlit) \
  132. TYPECHECK("" stringlit "", make_ptrlen(stringlit, sizeof(stringlit)-1))
  133. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  134. * than memset because we don't need the fill char parameter; also
  135. * attempts (by fiddly use of volatile) to inhibit the compiler from
  136. * over-cleverly trying to optimise the memset away because it knows
  137. * the variable is going out of scope. */
  138. void smemclr(void *b, size_t len);
  139. /* Compare two fixed-length chunks of memory for equality, without
  140. * data-dependent control flow (so an attacker with a very accurate
  141. * stopwatch can't try to guess where the first mismatching byte was).
  142. * Returns false for mismatch or true for equality (unlike memcmp),
  143. * hinted at by the 'eq' in the name. */
  144. bool smemeq(const void *av, const void *bv, size_t len);
  145. char *buildinfo(const char *newline);
  146. /*
  147. * Debugging functions.
  148. *
  149. * Output goes to debug.log
  150. *
  151. * debug() is like printf().
  152. *
  153. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  154. * is that dmemdumpl() is more suited for when the memory address is
  155. * important (say because you'll be recording pointer values later
  156. * on). dmemdump() is more concise.
  157. */
  158. #ifdef DEBUG
  159. void debug_printf(const char *fmt, ...);
  160. void debug_memdump(const void *buf, int len, bool L);
  161. #define debug(...) (debug_printf(__VA_ARGS__))
  162. #define dmemdump(buf,len) debug_memdump (buf, len, false);
  163. #define dmemdumpl(buf,len) debug_memdump (buf, len, true);
  164. #else
  165. #define debug(...)
  166. #define dmemdump(buf,len)
  167. #define dmemdumpl(buf,len)
  168. #endif
  169. #ifndef lenof
  170. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  171. #endif
  172. #ifndef min
  173. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  174. #endif
  175. #ifndef max
  176. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  177. #endif
  178. #define GET_64BIT_LSB_FIRST(cp) \
  179. (((uint64_t)(unsigned char)(cp)[0]) | \
  180. ((uint64_t)(unsigned char)(cp)[1] << 8) | \
  181. ((uint64_t)(unsigned char)(cp)[2] << 16) | \
  182. ((uint64_t)(unsigned char)(cp)[3] << 24) | \
  183. ((uint64_t)(unsigned char)(cp)[4] << 32) | \
  184. ((uint64_t)(unsigned char)(cp)[5] << 40) | \
  185. ((uint64_t)(unsigned char)(cp)[6] << 48) | \
  186. ((uint64_t)(unsigned char)(cp)[7] << 56))
  187. #define PUT_64BIT_LSB_FIRST(cp, value) ( \
  188. (cp)[0] = (unsigned char)(value), \
  189. (cp)[1] = (unsigned char)((value) >> 8), \
  190. (cp)[2] = (unsigned char)((value) >> 16), \
  191. (cp)[3] = (unsigned char)((value) >> 24), \
  192. (cp)[4] = (unsigned char)((value) >> 32), \
  193. (cp)[5] = (unsigned char)((value) >> 40), \
  194. (cp)[6] = (unsigned char)((value) >> 48), \
  195. (cp)[7] = (unsigned char)((value) >> 56) )
  196. #define GET_32BIT_LSB_FIRST(cp) \
  197. (((uint32_t)(unsigned char)(cp)[0]) | \
  198. ((uint32_t)(unsigned char)(cp)[1] << 8) | \
  199. ((uint32_t)(unsigned char)(cp)[2] << 16) | \
  200. ((uint32_t)(unsigned char)(cp)[3] << 24))
  201. #define PUT_32BIT_LSB_FIRST(cp, value) ( \
  202. (cp)[0] = (unsigned char)(value), \
  203. (cp)[1] = (unsigned char)((value) >> 8), \
  204. (cp)[2] = (unsigned char)((value) >> 16), \
  205. (cp)[3] = (unsigned char)((value) >> 24) )
  206. #define GET_16BIT_LSB_FIRST(cp) \
  207. (((unsigned long)(unsigned char)(cp)[0]) | \
  208. ((unsigned long)(unsigned char)(cp)[1] << 8))
  209. #define PUT_16BIT_LSB_FIRST(cp, value) ( \
  210. (cp)[0] = (unsigned char)(value), \
  211. (cp)[1] = (unsigned char)((value) >> 8) )
  212. #define GET_32BIT_MSB_FIRST(cp) \
  213. (((uint32_t)(unsigned char)(cp)[0] << 24) | \
  214. ((uint32_t)(unsigned char)(cp)[1] << 16) | \
  215. ((uint32_t)(unsigned char)(cp)[2] << 8) | \
  216. ((uint32_t)(unsigned char)(cp)[3]))
  217. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  218. #define PUT_32BIT_MSB_FIRST(cp, value) ( \
  219. (cp)[0] = (unsigned char)((value) >> 24), \
  220. (cp)[1] = (unsigned char)((value) >> 16), \
  221. (cp)[2] = (unsigned char)((value) >> 8), \
  222. (cp)[3] = (unsigned char)(value) )
  223. #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
  224. #define GET_64BIT_MSB_FIRST(cp) \
  225. (((uint64_t)(unsigned char)(cp)[0] << 56) | \
  226. ((uint64_t)(unsigned char)(cp)[1] << 48) | \
  227. ((uint64_t)(unsigned char)(cp)[2] << 40) | \
  228. ((uint64_t)(unsigned char)(cp)[3] << 32) | \
  229. ((uint64_t)(unsigned char)(cp)[4] << 24) | \
  230. ((uint64_t)(unsigned char)(cp)[5] << 16) | \
  231. ((uint64_t)(unsigned char)(cp)[6] << 8) | \
  232. ((uint64_t)(unsigned char)(cp)[7]))
  233. #define PUT_64BIT_MSB_FIRST(cp, value) ( \
  234. (cp)[0] = (unsigned char)((value) >> 56), \
  235. (cp)[1] = (unsigned char)((value) >> 48), \
  236. (cp)[2] = (unsigned char)((value) >> 40), \
  237. (cp)[3] = (unsigned char)((value) >> 32), \
  238. (cp)[4] = (unsigned char)((value) >> 24), \
  239. (cp)[5] = (unsigned char)((value) >> 16), \
  240. (cp)[6] = (unsigned char)((value) >> 8), \
  241. (cp)[7] = (unsigned char)(value) )
  242. #define GET_16BIT_MSB_FIRST(cp) \
  243. (((unsigned long)(unsigned char)(cp)[0] << 8) | \
  244. ((unsigned long)(unsigned char)(cp)[1]))
  245. #define PUT_16BIT_MSB_FIRST(cp, value) ( \
  246. (cp)[0] = (unsigned char)((value) >> 8), \
  247. (cp)[1] = (unsigned char)(value) )
  248. /* Replace NULL with the empty string, permitting an idiom in which we
  249. * get a string (pointer,length) pair that might be NULL,0 and can
  250. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  251. #define NULLTOEMPTY(s) ((s)?(s):"")
  252. #endif