misc.h 6.7 KB

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