MISC.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. char *dupstr(const char *s);
  21. char *dupcat(const char *s1, ...);
  22. char *dupprintf(const char *fmt, ...);
  23. char *dupvprintf(const char *fmt, va_list ap);
  24. char *fgetline(FILE *fp);
  25. void base64_encode_atom(unsigned char *data, int n, char *out);
  26. struct bufchain_granule;
  27. typedef struct bufchain_tag {
  28. struct bufchain_granule *head, *tail;
  29. int buffersize; /* current amount of buffered data */
  30. } bufchain;
  31. void bufchain_init(bufchain *ch);
  32. void bufchain_clear(bufchain *ch);
  33. int bufchain_size(bufchain *ch);
  34. void bufchain_add(bufchain *ch, const void *data, int len);
  35. void bufchain_prefix(bufchain *ch, void **data, int *len);
  36. void bufchain_consume(bufchain *ch, int len);
  37. void bufchain_fetch(bufchain *ch, void *data, int len);
  38. struct tm ltime(void);
  39. /*
  40. * Debugging functions.
  41. *
  42. * Output goes to debug.log
  43. *
  44. * debug(()) (note the double brackets) is like printf().
  45. *
  46. * dmemdump() and dmemdumpl() both do memory dumps. The difference
  47. * is that dmemdumpl() is more suited for when the memory address is
  48. * important (say because you'll be recording pointer values later
  49. * on). dmemdump() is more concise.
  50. */
  51. #ifdef DEBUG
  52. void debug_printf(char *fmt, ...);
  53. void debug_memdump(void *buf, int len, int L);
  54. #define debug(x) (debug_printf x)
  55. #define dmemdump(buf,len) debug_memdump (buf, len, 0);
  56. #define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
  57. #else
  58. #define debug(x)
  59. #define dmemdump(buf,len)
  60. #define dmemdumpl(buf,len)
  61. #endif
  62. #ifndef lenof
  63. #define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
  64. #endif
  65. #ifndef min
  66. #define min(x,y) ( (x) < (y) ? (x) : (y) )
  67. #endif
  68. #ifndef max
  69. #define max(x,y) ( (x) > (y) ? (x) : (y) )
  70. #endif
  71. #define GET_32BIT_LSB_FIRST(cp) \
  72. (((unsigned long)(unsigned char)(cp)[0]) | \
  73. ((unsigned long)(unsigned char)(cp)[1] << 8) | \
  74. ((unsigned long)(unsigned char)(cp)[2] << 16) | \
  75. ((unsigned long)(unsigned char)(cp)[3] << 24))
  76. #define PUT_32BIT_LSB_FIRST(cp, value) ( \
  77. (cp)[0] = (unsigned char)(value), \
  78. (cp)[1] = (unsigned char)((value) >> 8), \
  79. (cp)[2] = (unsigned char)((value) >> 16), \
  80. (cp)[3] = (unsigned char)((value) >> 24) )
  81. #define GET_16BIT_LSB_FIRST(cp) \
  82. (((unsigned long)(unsigned char)(cp)[0]) | \
  83. ((unsigned long)(unsigned char)(cp)[1] << 8))
  84. #define PUT_16BIT_LSB_FIRST(cp, value) ( \
  85. (cp)[0] = (unsigned char)(value), \
  86. (cp)[1] = (unsigned char)((value) >> 8) )
  87. #define GET_32BIT_MSB_FIRST(cp) \
  88. (((unsigned long)(unsigned char)(cp)[0] << 24) | \
  89. ((unsigned long)(unsigned char)(cp)[1] << 16) | \
  90. ((unsigned long)(unsigned char)(cp)[2] << 8) | \
  91. ((unsigned long)(unsigned char)(cp)[3]))
  92. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  93. #define PUT_32BIT_MSB_FIRST(cp, value) ( \
  94. (cp)[0] = (unsigned char)((value) >> 24), \
  95. (cp)[1] = (unsigned char)((value) >> 16), \
  96. (cp)[2] = (unsigned char)((value) >> 8), \
  97. (cp)[3] = (unsigned char)(value) )
  98. #define PUT_32BIT(cp, value) PUT_32BIT_MSB_FIRST(cp, value)
  99. #define GET_16BIT_MSB_FIRST(cp) \
  100. (((unsigned long)(unsigned char)(cp)[0] << 8) | \
  101. ((unsigned long)(unsigned char)(cp)[1]))
  102. #define PUT_16BIT_MSB_FIRST(cp, value) ( \
  103. (cp)[0] = (unsigned char)((value) >> 8), \
  104. (cp)[1] = (unsigned char)(value) )
  105. #endif