marshal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifndef PUTTY_MARSHAL_H
  2. #define PUTTY_MARSHAL_H
  3. #include "defs.h"
  4. /*
  5. * A sort of 'abstract base class' or 'interface' or 'trait' which is
  6. * the common feature of all types that want to accept data formatted
  7. * using the SSH binary conventions of uint32, string, mpint etc.
  8. */
  9. struct BinarySink {
  10. void (*write)(BinarySink *sink, const void *data, size_t len);
  11. BinarySink *binarysink_;
  12. };
  13. /*
  14. * To define a structure type as a valid target for binary formatted
  15. * data, put 'BinarySink_IMPLEMENTATION' in its declaration, and when
  16. * an instance is set up, use 'BinarySink_INIT' to initialise the
  17. * 'base class' state, providing a function pointer to be the
  18. * implementation of the write() call above.
  19. */
  20. #define BinarySink_IMPLEMENTATION BinarySink binarysink_[1]
  21. #define BinarySink_INIT(obj, writefn) \
  22. ((obj)->binarysink_->write = (writefn), \
  23. (obj)->binarysink_->binarysink_ = (obj)->binarysink_)
  24. /*
  25. * To define a larger structure type as a valid BinarySink in such a
  26. * way that it will delegate the write method to some other object,
  27. * put 'BinarySink_DELEGATE_IMPLEMENTATION' in its declaration, and
  28. * when an instance is set up, use 'BinarySink_DELEGATE_INIT' to point
  29. * at the object it wants to delegate to.
  30. */
  31. #define BinarySink_DELEGATE_IMPLEMENTATION BinarySink *binarysink_
  32. #define BinarySink_DELEGATE_INIT(obj, othersink) \
  33. ((obj)->binarysink_ = BinarySink_UPCAST(othersink))
  34. /*
  35. * The implementing type's write function will want to downcast its
  36. * 'BinarySink *' parameter back to the more specific type. Also,
  37. * sometimes you'll want to upcast a pointer to a particular
  38. * implementing type into an abstract 'BinarySink *' to pass to
  39. * generic subroutines not defined in this file. These macros do that
  40. * job.
  41. *
  42. * Importantly, BinarySink_UPCAST can also be applied to a BinarySink
  43. * * itself (and leaves it unchanged). That's achieved by a small
  44. * piece of C trickery: implementing structures and the BinarySink
  45. * structure itself both contain a field called binarysink_, but in
  46. * implementing objects it's a BinarySink[1] whereas in the abstract
  47. * type it's a 'BinarySink *' pointing back to the same structure,
  48. * meaning that you can say 'foo->binarysink_' in either case and get
  49. * a pointer type by different methods.
  50. */
  51. #define BinarySink_DOWNCAST(object, type) \
  52. TYPECHECK((object) == ((type *)0)->binarysink_, \
  53. ((type *)(((char *)(object)) - offsetof(type, binarysink_))))
  54. #define BinarySink_UPCAST(object) \
  55. TYPECHECK((object)->binarysink_ == (BinarySink *)0, \
  56. (object)->binarysink_)
  57. /*
  58. * If you structure-copy an object that's implementing BinarySink,
  59. * then that tricky self-pointer in its trait subobject will point to
  60. * the wrong place. You could call BinarySink_INIT again, but this
  61. * macro is terser and does all that's needed to fix up the copied
  62. * object.
  63. */
  64. #define BinarySink_COPIED(obj) \
  65. ((obj)->binarysink_->binarysink_ = (obj)->binarysink_)
  66. /*
  67. * The put_* macros are the main client to this system. Any structure
  68. * which implements the BinarySink 'trait' is valid for use as the
  69. * first parameter of any of these put_* macros.
  70. */
  71. /* Basic big-endian integer types. */
  72. #define put_byte(bs, val) \
  73. BinarySink_put_byte(BinarySink_UPCAST(bs), val)
  74. #define put_uint16(bs, val) \
  75. BinarySink_put_uint16(BinarySink_UPCAST(bs), val)
  76. #define put_uint32(bs, val) \
  77. BinarySink_put_uint32(BinarySink_UPCAST(bs), val)
  78. #define put_uint64(bs, val) \
  79. BinarySink_put_uint64(BinarySink_UPCAST(bs), val)
  80. /* SSH booleans, encoded as a single byte storing either 0 or 1. */
  81. #define put_bool(bs, val) \
  82. BinarySink_put_bool(BinarySink_UPCAST(bs), val)
  83. /* SSH strings, with a leading uint32 length field. 'stringz' is a
  84. * convenience function that takes an ordinary C zero-terminated
  85. * string as input. 'stringsb' takes a strbuf * as input, and
  86. * finalises it as a side effect (handy for multi-level marshalling in
  87. * which you use these same functions to format an inner blob of data
  88. * that then gets wrapped into a string container in an outer one). */
  89. #define put_string(bs, val, len) \
  90. BinarySink_put_string(BinarySink_UPCAST(bs),val,len)
  91. #define put_stringpl(bs, ptrlen) \
  92. BinarySink_put_stringpl(BinarySink_UPCAST(bs),ptrlen)
  93. #define put_stringz(bs, val) \
  94. BinarySink_put_stringz(BinarySink_UPCAST(bs), val)
  95. #define put_stringsb(bs, val) \
  96. BinarySink_put_stringsb(BinarySink_UPCAST(bs), val)
  97. /* Other string outputs: 'asciz' emits the string data directly into
  98. * the output including the terminating \0, and 'pstring' emits the
  99. * string in Pascal style with a leading _one_-byte length field.
  100. * pstring can fail if the string is too long. */
  101. #define put_asciz(bs, val) \
  102. BinarySink_put_asciz(BinarySink_UPCAST(bs), val)
  103. #define put_pstring(bs, val) \
  104. BinarySink_put_pstring(BinarySink_UPCAST(bs), val)
  105. /* Multiprecision integers, in both the SSH-1 and SSH-2 formats. */
  106. #define put_mp_ssh1(bs, val) \
  107. BinarySink_put_mp_ssh1(BinarySink_UPCAST(bs), val)
  108. #define put_mp_ssh2(bs, val) \
  109. BinarySink_put_mp_ssh2(BinarySink_UPCAST(bs), val)
  110. /* Padding with a specified byte. */
  111. #define put_padding(bs, len, padbyte) \
  112. BinarySink_put_padding(BinarySink_UPCAST(bs), len, padbyte)
  113. /* Fallback: just emit raw data bytes, using a syntax that matches the
  114. * rest of these macros. */
  115. #define put_data(bs, val, len) \
  116. BinarySink_put_data(BinarySink_UPCAST(bs), val, len)
  117. /*
  118. * The underlying real C functions that implement most of those
  119. * macros. Generally you won't want to call these directly, because
  120. * they have such cumbersome names; you call the wrapper macros above
  121. * instead.
  122. *
  123. * A few functions whose wrapper macros are defined above are actually
  124. * declared in other headers, so as to guarantee that the
  125. * declaration(s) of their other parameter type(s) are in scope.
  126. */
  127. void BinarySink_put_data(BinarySink *, const void *data, size_t len);
  128. void BinarySink_put_padding(BinarySink *, size_t len, unsigned char padbyte);
  129. void BinarySink_put_byte(BinarySink *, unsigned char);
  130. void BinarySink_put_bool(BinarySink *, bool);
  131. void BinarySink_put_uint16(BinarySink *, unsigned long);
  132. void BinarySink_put_uint32(BinarySink *, unsigned long);
  133. void BinarySink_put_uint64(BinarySink *, uint64_t);
  134. void BinarySink_put_string(BinarySink *, const void *data, size_t len);
  135. void BinarySink_put_stringpl(BinarySink *, ptrlen);
  136. void BinarySink_put_stringz(BinarySink *, const char *str);
  137. struct strbuf;
  138. void BinarySink_put_stringsb(BinarySink *, struct strbuf *);
  139. void BinarySink_put_asciz(BinarySink *, const char *str);
  140. bool BinarySink_put_pstring(BinarySink *, const char *str);
  141. /* ---------------------------------------------------------------------- */
  142. /*
  143. * A complementary trait structure for _un_-marshalling.
  144. *
  145. * This structure contains client-visible data fields rather than
  146. * methods, because that seemed more useful than leaving it totally
  147. * opaque. But it's still got the self-pointer system that will allow
  148. * the set of get_* macros to target one of these itself or any other
  149. * type that 'derives' from it. So, for example, an SSH packet
  150. * structure can act as a BinarySource while also having additional
  151. * fields like the packet type.
  152. */
  153. typedef enum BinarySourceError {
  154. BSE_NO_ERROR,
  155. BSE_OUT_OF_DATA,
  156. BSE_INVALID
  157. } BinarySourceError;
  158. struct BinarySource {
  159. /*
  160. * (data, len) is the data block being decoded. pos is the current
  161. * position within the block.
  162. */
  163. const void *data;
  164. size_t pos, len;
  165. /*
  166. * 'err' indicates whether a decoding error has happened at any
  167. * point. Once this has been set to something other than
  168. * BSE_NO_ERROR, it shouldn't be changed by any unmarshalling
  169. * function. So you can safely do a long sequence of get_foo()
  170. * operations and then test err just once at the end, rather than
  171. * having to conditionalise every single get.
  172. *
  173. * The unmarshalling functions should always return some value,
  174. * even if a decoding error occurs. Generally on error they'll
  175. * return zero (if numeric) or the empty string (if string-based),
  176. * or some other appropriate default value for more complicated
  177. * types.
  178. *
  179. * If the usual return value is dynamically allocated (e.g. a
  180. * Bignum, or a normal C 'char *' string), then the error value is
  181. * also dynamic in the same way. So you have to free exactly the
  182. * same set of things whether or not there was a decoding error,
  183. * which simplifies exit paths - for example, you could call a big
  184. * pile of get_foo functions, then put the actual handling of the
  185. * results under 'if (!get_err(src))', and then free everything
  186. * outside that if.
  187. */
  188. BinarySourceError err;
  189. /*
  190. * Self-pointer for the implicit derivation trick, same as
  191. * BinarySink above.
  192. */
  193. BinarySource *binarysource_;
  194. };
  195. /*
  196. * Implementation macros, similar to BinarySink.
  197. */
  198. #define BinarySource_IMPLEMENTATION BinarySource binarysource_[1]
  199. #define BinarySource_INIT__(obj, data_, len_) \
  200. ((obj)->data = (data_), \
  201. (obj)->len = (len_), \
  202. (obj)->pos = 0, \
  203. (obj)->err = BSE_NO_ERROR, \
  204. (obj)->binarysource_ = (obj))
  205. #define BinarySource_BARE_INIT(obj, data_, len_) \
  206. TYPECHECK(&(obj)->binarysource_ == (BinarySource **)0, \
  207. BinarySource_INIT__(obj, data_, len_))
  208. #define BinarySource_INIT(obj, data_, len_) \
  209. TYPECHECK(&(obj)->binarysource_ == (BinarySource (*)[1])0, \
  210. BinarySource_INIT__(BinarySource_UPCAST(obj), data_, len_))
  211. #define BinarySource_DOWNCAST(object, type) \
  212. TYPECHECK((object) == ((type *)0)->binarysource_, \
  213. ((type *)(((char *)(object)) - offsetof(type, binarysource_))))
  214. #define BinarySource_UPCAST(object) \
  215. TYPECHECK((object)->binarysource_ == (BinarySource *)0, \
  216. (object)->binarysource_)
  217. #define BinarySource_COPIED(obj) \
  218. ((obj)->binarysource_->binarysource_ = (obj)->binarysource_)
  219. #define get_data(src, len) \
  220. BinarySource_get_data(BinarySource_UPCAST(src), len)
  221. #define get_byte(src) \
  222. BinarySource_get_byte(BinarySource_UPCAST(src))
  223. #define get_bool(src) \
  224. BinarySource_get_bool(BinarySource_UPCAST(src))
  225. #define get_uint16(src) \
  226. BinarySource_get_uint16(BinarySource_UPCAST(src))
  227. #define get_uint32(src) \
  228. BinarySource_get_uint32(BinarySource_UPCAST(src))
  229. #define get_uint64(src) \
  230. BinarySource_get_uint64(BinarySource_UPCAST(src))
  231. #define get_string(src) \
  232. BinarySource_get_string(BinarySource_UPCAST(src))
  233. #define get_asciz(src) \
  234. BinarySource_get_asciz(BinarySource_UPCAST(src))
  235. #define get_pstring(src) \
  236. BinarySource_get_pstring(BinarySource_UPCAST(src))
  237. #define get_mp_ssh1(src) \
  238. BinarySource_get_mp_ssh1(BinarySource_UPCAST(src))
  239. #define get_mp_ssh2(src) \
  240. BinarySource_get_mp_ssh2(BinarySource_UPCAST(src))
  241. #define get_rsa_ssh1_pub(src, rsa, order) \
  242. BinarySource_get_rsa_ssh1_pub(BinarySource_UPCAST(src), rsa, order)
  243. #define get_rsa_ssh1_priv(src, rsa) \
  244. BinarySource_get_rsa_ssh1_priv(BinarySource_UPCAST(src), rsa)
  245. #define get_err(src) (BinarySource_UPCAST(src)->err)
  246. #define get_avail(src) (BinarySource_UPCAST(src)->len - \
  247. BinarySource_UPCAST(src)->pos)
  248. #define get_ptr(src) \
  249. ((const void *)( \
  250. (const unsigned char *)(BinarySource_UPCAST(src)->data) + \
  251. BinarySource_UPCAST(src)->pos))
  252. ptrlen BinarySource_get_data(BinarySource *, size_t);
  253. unsigned char BinarySource_get_byte(BinarySource *);
  254. bool BinarySource_get_bool(BinarySource *);
  255. unsigned BinarySource_get_uint16(BinarySource *);
  256. unsigned long BinarySource_get_uint32(BinarySource *);
  257. uint64_t BinarySource_get_uint64(BinarySource *);
  258. ptrlen BinarySource_get_string(BinarySource *);
  259. const char *BinarySource_get_asciz(BinarySource *);
  260. ptrlen BinarySource_get_pstring(BinarySource *);
  261. #endif /* PUTTY_MARSHAL_H */