misc.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Header for misc.c.
  3. */
  4. #ifndef PUTTY_MISC_H
  5. #define PUTTY_MISC_H
  6. #include "puttymem.h"
  7. #include <stdio.h> /* for FILE * */
  8. #include <stdarg.h> /* for va_list */
  9. #include <time.h> /* for struct tm */
  10. #ifndef FALSE
  11. #define FALSE 0
  12. #endif
  13. #ifndef TRUE
  14. #define TRUE 1
  15. #endif
  16. typedef struct Filename Filename;
  17. typedef struct FontSpec FontSpec;
  18. unsigned long parse_blocksize(const char *bs);
  19. char ctrlparse(char *s, char **next);
  20. size_t host_strcspn(const char *s, const char *set);
  21. char *host_strchr(const char *s, int c);
  22. char *host_strrchr(const char *s, int c);
  23. char *host_strduptrim(const char *s);
  24. char *dupstr(const char *s);
  25. char *dupcat(const char *s1, ...);
  26. char *dupprintf(const char *fmt, ...)
  27. #ifdef __GNUC__
  28. __attribute__ ((format (printf, 1, 2)))
  29. #endif
  30. ;
  31. char *dupvprintf(const char *fmt, va_list ap);
  32. void burnstr(char *string);
  33. typedef struct strbuf strbuf;
  34. strbuf *strbuf_new(void);
  35. void strbuf_free(strbuf *buf);
  36. char *strbuf_str(strbuf *buf); /* does not free buf */
  37. char *strbuf_to_str(strbuf *buf); /* does free buf, but you must free result */
  38. void strbuf_catf(strbuf *buf, const char *fmt, ...);
  39. void strbuf_catfv(strbuf *buf, const char *fmt, va_list ap);
  40. /* String-to-Unicode converters that auto-allocate the destination and
  41. * work around the rather deficient interface of mb_to_wc.
  42. *
  43. * These actually live in miscucs.c, not misc.c (the distinction being
  44. * that the former is only linked into tools that also have the main
  45. * Unicode support). */
  46. wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, int len);
  47. wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string);
  48. int toint(unsigned);
  49. char *fgetline(FILE *fp);
  50. char *chomp(char *str);
  51. int strstartswith(const char *s, const char *t);
  52. int strendswith(const char *s, const char *t);
  53. void base64_encode_atom(const unsigned char *data, int n, char *out);
  54. int base64_decode_atom(const char *atom, unsigned char *out);
  55. struct bufchain_granule;
  56. struct bufchain_tag {
  57. struct bufchain_granule *head, *tail;
  58. int buffersize; /* current amount of buffered data */
  59. };
  60. #ifndef BUFCHAIN_TYPEDEF
  61. typedef struct bufchain_tag bufchain; /* rest of declaration in misc.c */
  62. #define BUFCHAIN_TYPEDEF
  63. #endif
  64. void bufchain_init(bufchain *ch);
  65. void bufchain_clear(bufchain *ch);
  66. int bufchain_size(bufchain *ch);
  67. void bufchain_add(bufchain *ch, const void *data, int len);
  68. void bufchain_prefix(bufchain *ch, void **data, int *len);
  69. void bufchain_consume(bufchain *ch, int len);
  70. void bufchain_fetch(bufchain *ch, void *data, int len);
  71. int validate_manual_hostkey(char *key);
  72. struct tm ltime(void);
  73. /* Wipe sensitive data out of memory that's about to be freed. Simpler
  74. * than memset because we don't need the fill char parameter; also
  75. * attempts (by fiddly use of volatile) to inhibit the compiler from
  76. * over-cleverly trying to optimise the memset away because it knows
  77. * the variable is going out of scope. */
  78. void smemclr(void *b, size_t len);
  79. /* Compare two fixed-length chunks of memory for equality, without
  80. * data-dependent control flow (so an attacker with a very accurate
  81. * stopwatch can't try to guess where the first mismatching byte was).
  82. * Returns 0 for mismatch or 1 for equality (unlike memcmp), hinted at
  83. * by the 'eq' in the name. */
  84. int smemeq(const void *av, const void *bv, size_t len);
  85. /* Extracts an SSH-marshalled string from the start of *data. If
  86. * successful (*datalen is not too small), advances data/datalen past
  87. * the string and returns a pointer to the string itself and its
  88. * length in *stringlen. Otherwise does nothing and returns NULL.
  89. *
  90. * Like strchr, this function can discard const from its parameter.
  91. * Treat it as if it was a family of two functions, one returning a
  92. * non-const string given a non-const pointer, and one taking and
  93. * returning const. */
  94. void *get_ssh_string(int *datalen, const void **data, int *stringlen);
  95. /* Extracts an SSH uint32, similarly. Returns TRUE on success, and
  96. * leaves the extracted value in *ret. */
  97. int get_ssh_uint32(int *datalen, const void **data, unsigned *ret);
  98. /* Given a not-necessarily-zero-terminated string in (length,data)
  99. * form, check if it equals an ordinary C zero-terminated string. */
  100. int match_ssh_id(int stringlen, const void *string, const char *id);
  101. char *buildinfo(const char *newline);
  102. /*
  103. * Debugging functions.
  104. *
  105. * Output goes to debug.log
  106. *
  107. * debug(()) (note the double brackets) is like printf().
  108. *
  109. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  110. * is that dmemdumpl() is more suited for when the memory address is
  111. * important (say because you'll be recording pointer values later
  112. * on). dmemdump() is more concise.
  113. */
  114. #ifdef DEBUG
  115. void debug_printf(const char *fmt, ...);
  116. void debug_memdump(const void *buf, int len, int L);
  117. #define debug(x) (debug_printf x)
  118. #define dmemdump(buf,len) debug_memdump (buf, len, 0);
  119. #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
  120. #else
  121. #define debug(x)
  122. #define dmemdump(buf,len)
  123. #define dmemdumpl(buf,len)
  124. #endif
  125. #ifndef lenof
  126. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  127. #endif
  128. #ifndef min
  129. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  130. #endif
  131. #ifndef max
  132. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  133. #endif
  134. #define GET_32BIT_LSB_FIRST(cp) \
  135. (((unsigned long)(unsigned char)(cp)[0]) | \
  136. ((unsigned long)(unsigned char)(cp)[1] << 8) | \
  137. ((unsigned long)(unsigned char)(cp)[2] << 16) | \
  138. ((unsigned long)(unsigned char)(cp)[3] << 24))
  139. #define PUT_32BIT_LSB_FIRST(cp, value) ( \
  140. (cp)[0] = (unsigned char)(value), \
  141. (cp)[1] = (unsigned char)((value) >> 8), \
  142. (cp)[2] = (unsigned char)((value) >> 16), \
  143. (cp)[3] = (unsigned char)((value) >> 24) )
  144. #define GET_16BIT_LSB_FIRST(cp) \
  145. (((unsigned long)(unsigned char)(cp)[0]) | \
  146. ((unsigned long)(unsigned char)(cp)[1] << 8))
  147. #define PUT_16BIT_LSB_FIRST(cp, value) ( \
  148. (cp)[0] = (unsigned char)(value), \
  149. (cp)[1] = (unsigned char)((value) >> 8) )
  150. #define GET_32BIT_MSB_FIRST(cp) \
  151. (((unsigned long)(unsigned char)(cp)[0] << 24) | \
  152. ((unsigned long)(unsigned char)(cp)[1] << 16) | \
  153. ((unsigned long)(unsigned char)(cp)[2] << 8) | \
  154. ((unsigned long)(unsigned char)(cp)[3]))
  155. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  156. #define PUT_32BIT_MSB_FIRST(cp, value) ( \
  157. (cp)[0] = (unsigned char)((value) >> 24), \
  158. (cp)[1] = (unsigned char)((value) >> 16), \
  159. (cp)[2] = (unsigned char)((value) >> 8), \
  160. (cp)[3] = (unsigned char)(value) )
  161. #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
  162. #define GET_16BIT_MSB_FIRST(cp) \
  163. (((unsigned long)(unsigned char)(cp)[0] << 8) | \
  164. ((unsigned long)(unsigned char)(cp)[1]))
  165. #define PUT_16BIT_MSB_FIRST(cp, value) ( \
  166. (cp)[0] = (unsigned char)((value) >> 8), \
  167. (cp)[1] = (unsigned char)(value) )
  168. /* Replace NULL with the empty string, permitting an idiom in which we
  169. * get a string (pointer,length) pair that might be NULL,0 and can
  170. * then safely say things like printf("%.*s", length, NULLTOEMPTY(ptr)) */
  171. #define NULLTOEMPTY(s) ((s)?(s):"")
  172. #endif