misc.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. unsigned long parse_blocksize(const char *bs);
  13. char ctrlparse(char *s, char **next);
  14. size_t host_strcspn(const char *s, const char *set);
  15. char *host_strchr(const char *s, int c);
  16. char *host_strrchr(const char *s, int c);
  17. char *host_strduptrim(const char *s);
  18. char *dupstr(const char *s);
  19. char *dupcat(const char *s1, ...);
  20. char *dupprintf(const char *fmt, ...)
  21. #ifdef __GNUC__
  22. __attribute__ ((format (printf, 1, 2)))
  23. #endif
  24. ;
  25. char *dupvprintf(const char *fmt, va_list ap);
  26. void burnstr(char *string);
  27. /*
  28. * Pass a dynamically allocated string to logevent and immediately
  29. * free it. Intended for use by wrapper macros which pass the return
  30. * value of dupprintf straight to this.
  31. */
  32. void logevent_and_free(Frontend *frontend, char *msg);
  33. struct strbuf {
  34. char *s;
  35. unsigned char *u;
  36. int len;
  37. BinarySink_IMPLEMENTATION;
  38. /* (also there's a surrounding implementation struct in misc.c) */
  39. };
  40. strbuf *strbuf_new(void);
  41. void strbuf_free(strbuf *buf);
  42. void *strbuf_append(strbuf *buf, size_t len);
  43. char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
  44. void strbuf_catf(strbuf *buf, const char *fmt, ...);
  45. void strbuf_catfv(strbuf *buf, const char *fmt, va_list ap);
  46. strbuf *strbuf_new_for_agent_query(void);
  47. void strbuf_finalise_agent_query(strbuf *buf);
  48. /* String-to-Unicode converters that auto-allocate the destination and
  49. * work around the rather deficient interface of mb_to_wc.
  50. *
  51. * These actually live in miscucs.c, not misc.c (the distinction being
  52. * that the former is only linked into tools that also have the main
  53. * Unicode support). */
  54. wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
  55. wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
  56. int toint(unsigned);
  57. char *fgetline(FILE *fp);
  58. char *chomp(char *str);
  59. int strstartswith(const char *s, const char *t);
  60. int strendswith(const char *s, const char *t);
  61. void base64_encode_atom(const unsigned char *data, int n, char *out);
  62. int base64_decode_atom(const char *atom, unsigned char *out);
  63. struct bufchain_granule;
  64. struct bufchain_tag {
  65. struct bufchain_granule *head, *tail;
  66. int buffersize; /* current amount of buffered data */
  67. IdempotentCallback *ic;
  68. };
  69. void bufchain_init(bufchain *ch);
  70. void bufchain_clear(bufchain *ch);
  71. int bufchain_size(bufchain *ch);
  72. void bufchain_add(bufchain *ch, const void *data, int len);
  73. void bufchain_prefix(bufchain *ch, void **data, int *len);
  74. void bufchain_consume(bufchain *ch, int len);
  75. void bufchain_fetch(bufchain *ch, void *data, int len);
  76. void bufchain_fetch_consume(bufchain *ch, void *data, int len);
  77. int bufchain_try_fetch_consume(bufchain *ch, void *data, int len);
  78. void sanitise_term_data(bufchain *out, const void *vdata, int len);
  79. int validate_manual_hostkey(char *key);
  80. struct tm ltime(void);
  81. /*
  82. * Special form of strcmp which can cope with NULL inputs. NULL is
  83. * defined to sort before even the empty string.
  84. */
  85. int nullstrcmp(const char *a, const char *b);
  86. ptrlen make_ptrlen(const void *ptr, size_t len);
  87. int ptrlen_eq_string(ptrlen pl, const char *str);
  88. char *mkstr(ptrlen pl);
  89. int string_length_for_printf(size_t);
  90. /* Derive two printf arguments from a ptrlen, suitable for "%.*s" */
  91. #define PTRLEN_PRINTF(pl) \
  92. string_length_for_printf((pl).len), (const char *)(pl).ptr
  93. /* Make a ptrlen out of a compile-time string literal. We try to
  94. * enforce that it _is_ a string literal by token-pasting "" on to it,
  95. * which should provoke a compile error if it's any other kind of
  96. * string. */
  97. #define PTRLEN_LITERAL(stringlit) \
  98. TYPECHECK("" stringlit "", make_ptrlen(stringlit, sizeof(stringlit)-1))
  99. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  100. * than memset because we don't need the fill char parameter; also
  101. * attempts (by fiddly use of volatile) to inhibit the compiler from
  102. * over-cleverly trying to optimise the memset away because it knows
  103. * the variable is going out of scope. */
  104. void smemclr(void *b, size_t len);
  105. /* Compare two fixed-length chunks of memory for equality, without
  106. * data-dependent control flow (so an attacker with a very accurate
  107. * stopwatch can't try to guess where the first mismatching byte was).
  108. * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
  109. * by the 'eq' in the name. */
  110. int smemeq(const void *av, const void *bv, size_t len);
  111. char *buildinfo(const char *newline);
  112. /*
  113. * Debugging functions.
  114. *
  115. * Output goes to debug.log
  116. *
  117. * debug(()) (note the double brackets) is like printf().
  118. *
  119. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  120. * is that dmemdumpl() is more suited for when the memory address is
  121. * important (say because you'll be recording pointer values later
  122. * on). dmemdump() is more concise.
  123. */
  124. #ifdef DEBUG
  125. void debug_printf(const char *fmt, ...);
  126. void debug_memdump(const void *buf, int len, int L);
  127. #define debug(x) (debug_printf x)
  128. #define dmemdump(buf,len) debug_memdump (buf, len, 0);
  129. #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
  130. #else
  131. #define debug(x)
  132. #define dmemdump(buf,len)
  133. #define dmemdumpl(buf,len)
  134. #endif
  135. #ifndef lenof
  136. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  137. #endif
  138. #ifndef min
  139. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  140. #endif
  141. #ifndef max
  142. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  143. #endif
  144. #define GET_32BIT_LSB_FIRST(cp) \
  145. (((unsigned long)(unsigned char)(cp)[0]) | \
  146. ((unsigned long)(unsigned char)(cp)[1] << 8) | \
  147. ((unsigned long)(unsigned char)(cp)[2] << 16) | \
  148. ((unsigned long)(unsigned char)(cp)[3] << 24))
  149. #define PUT_32BIT_LSB_FIRST(cp, value) ( \
  150. (cp)[0] = (unsigned char)(value), \
  151. (cp)[1] = (unsigned char)((value) >> 8), \
  152. (cp)[2] = (unsigned char)((value) >> 16), \
  153. (cp)[3] = (unsigned char)((value) >> 24) )
  154. #define GET_16BIT_LSB_FIRST(cp) \
  155. (((unsigned long)(unsigned char)(cp)[0]) | \
  156. ((unsigned long)(unsigned char)(cp)[1] << 8))
  157. #define PUT_16BIT_LSB_FIRST(cp, value) ( \
  158. (cp)[0] = (unsigned char)(value), \
  159. (cp)[1] = (unsigned char)((value) >> 8) )
  160. #define GET_32BIT_MSB_FIRST(cp) \
  161. (((unsigned long)(unsigned char)(cp)[0] << 24) | \
  162. ((unsigned long)(unsigned char)(cp)[1] << 16) | \
  163. ((unsigned long)(unsigned char)(cp)[2] << 8) | \
  164. ((unsigned long)(unsigned char)(cp)[3]))
  165. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  166. #define PUT_32BIT_MSB_FIRST(cp, value) ( \
  167. (cp)[0] = (unsigned char)((value) >> 24), \
  168. (cp)[1] = (unsigned char)((value) >> 16), \
  169. (cp)[2] = (unsigned char)((value) >> 8), \
  170. (cp)[3] = (unsigned char)(value) )
  171. #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
  172. #define GET_16BIT_MSB_FIRST(cp) \
  173. (((unsigned long)(unsigned char)(cp)[0] << 8) | \
  174. ((unsigned long)(unsigned char)(cp)[1]))
  175. #define PUT_16BIT_MSB_FIRST(cp, value) ( \
  176. (cp)[0] = (unsigned char)((value) >> 8), \
  177. (cp)[1] = (unsigned char)(value) )
  178. /* Replace NULL with the empty string, permitting an idiom in which we
  179. * get a string (pointer,length) pair that might be NULL,0 and can
  180. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  181. #define NULLTOEMPTY(s) ((s)?(s):"")
  182. #endif