Home | History | Annotate | Line # | Download | only in dist
      1  1.17   mlelstv /*	$NetBSD: sshbuf.h,v 1.23 2026/04/08 18:58:41 christos Exp $	*/
      2  1.23  christos /*	$OpenBSD: sshbuf.h,v 1.35 2026/03/03 09:57:25 dtucker Exp $	*/
      3  1.21  christos 
      4   1.1  christos /*
      5   1.1  christos  * Copyright (c) 2011 Damien Miller
      6   1.1  christos  *
      7   1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  christos  * copyright notice and this permission notice appear in all copies.
     10   1.1  christos  *
     11   1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  christos  */
     19   1.1  christos 
     20   1.1  christos #ifndef _SSHBUF_H
     21   1.1  christos #define _SSHBUF_H
     22   1.1  christos 
     23   1.1  christos #include <sys/types.h>
     24   1.1  christos #include <stdarg.h>
     25   1.1  christos #include <stdio.h>
     26  1.12  christos 
     27  1.12  christos #ifdef WITH_OPENSSL
     28   1.1  christos #include <openssl/bn.h>
     29   1.1  christos #include <openssl/ec.h>
     30  1.12  christos #include <openssl/ecdsa.h>
     31  1.21  christos #include <openssl/evp.h>
     32  1.12  christos #else /* OPENSSL */
     33  1.12  christos #define BIGNUM		void
     34  1.12  christos #define EC_KEY		void
     35  1.12  christos #define EC_GROUP	void
     36  1.12  christos #define EC_POINT	void
     37  1.21  christos #define EVP_PKEY	void
     38  1.12  christos #endif /* WITH_OPENSSL */
     39   1.1  christos 
     40  1.17   mlelstv #define SSHBUF_SIZE_MAX		0x10000000	/* Hard maximum size 256MB */
     41   1.1  christos #define SSHBUF_REFS_MAX		0x100000	/* Max child buffers */
     42   1.1  christos #define SSHBUF_MAX_BIGNUM	(16384 / 8)	/* Max bignum *bytes* */
     43   1.1  christos #define SSHBUF_MAX_ECPOINT	((528 * 2 / 8) + 1) /* Max EC point *bytes* */
     44   1.1  christos 
     45  1.19  christos struct sshbuf;
     46   1.1  christos 
     47   1.1  christos /*
     48   1.1  christos  * Create a new sshbuf buffer.
     49   1.1  christos  * Returns pointer to buffer on success, or NULL on allocation failure.
     50   1.1  christos  */
     51   1.1  christos struct sshbuf *sshbuf_new(void);
     52   1.1  christos 
     53   1.1  christos /*
     54   1.1  christos  * Create a new, read-only sshbuf buffer from existing data.
     55   1.1  christos  * Returns pointer to buffer on success, or NULL on allocation failure.
     56   1.1  christos  */
     57   1.1  christos struct sshbuf *sshbuf_from(const void *blob, size_t len);
     58   1.1  christos 
     59   1.1  christos /*
     60   1.1  christos  * Create a new, read-only sshbuf buffer from the contents of an existing
     61   1.1  christos  * buffer. The contents of "buf" must not change in the lifetime of the
     62   1.1  christos  * resultant buffer.
     63   1.1  christos  * Returns pointer to buffer on success, or NULL on allocation failure.
     64   1.1  christos  */
     65   1.1  christos struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
     66   1.1  christos 
     67   1.1  christos /*
     68   1.1  christos  * Create a new, read-only sshbuf buffer from the contents of a string in
     69   1.1  christos  * an existing buffer (the string is consumed in the process).
     70   1.1  christos  * The contents of "buf" must not change in the lifetime of the resultant
     71   1.1  christos  * buffer.
     72  1.22  christos  * On success, a pointer to the newly allocated buffer is placed in *bufp.
     73  1.22  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
     74   1.1  christos  */
     75   1.1  christos int	sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
     76   1.1  christos 
     77   1.1  christos /*
     78   1.1  christos  * Clear and free buf
     79   1.1  christos  */
     80   1.1  christos void	sshbuf_free(struct sshbuf *buf);
     81   1.1  christos 
     82   1.1  christos /*
     83   1.1  christos  * Reset buf, clearing its contents. NB. max_size is preserved.
     84   1.1  christos  */
     85   1.1  christos void	sshbuf_reset(struct sshbuf *buf);
     86   1.1  christos 
     87   1.1  christos /*
     88   1.1  christos  * Return the maximum size of buf
     89   1.1  christos  */
     90   1.1  christos size_t	sshbuf_max_size(const struct sshbuf *buf);
     91   1.1  christos 
     92   1.1  christos /*
     93   1.1  christos  * Set the maximum size of buf
     94   1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
     95   1.1  christos  */
     96   1.1  christos int	sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
     97   1.1  christos 
     98   1.1  christos /*
     99   1.1  christos  * Returns the length of data in buf
    100   1.1  christos  */
    101   1.1  christos size_t	sshbuf_len(const struct sshbuf *buf);
    102   1.1  christos 
    103   1.1  christos /*
    104   1.1  christos  * Returns number of bytes left in buffer before hitting max_size.
    105   1.1  christos  */
    106   1.1  christos size_t	sshbuf_avail(const struct sshbuf *buf);
    107   1.1  christos 
    108   1.1  christos /*
    109   1.5  christos  * Returns a read-only pointer to the start of the data in buf
    110   1.1  christos  */
    111   1.1  christos const u_char *sshbuf_ptr(const struct sshbuf *buf);
    112   1.1  christos 
    113   1.1  christos /*
    114   1.5  christos  * Returns a mutable pointer to the start of the data in buf, or
    115   1.1  christos  * NULL if the buffer is read-only.
    116   1.1  christos  */
    117   1.1  christos u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
    118   1.1  christos 
    119   1.1  christos /*
    120   1.1  christos  * Check whether a reservation of size len will succeed in buf
    121   1.1  christos  * Safer to use than direct comparisons again sshbuf_avail as it copes
    122   1.1  christos  * with unsigned overflows correctly.
    123   1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    124   1.1  christos  */
    125   1.1  christos int	sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
    126   1.1  christos 
    127   1.1  christos /*
    128   1.7  christos  * Preallocates len additional bytes in buf.
    129   1.7  christos  * Useful for cases where the caller knows how many bytes will ultimately be
    130   1.7  christos  * required to avoid realloc in the buffer code.
    131   1.7  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    132   1.7  christos  */
    133   1.7  christos int	sshbuf_allocate(struct sshbuf *buf, size_t len);
    134   1.7  christos 
    135   1.7  christos /*
    136   1.1  christos  * Reserve len bytes in buf.
    137   1.1  christos  * Returns 0 on success and a pointer to the first reserved byte via the
    138  1.14  christos  * optional dpp parameter or a negative SSH_ERR_* error code on failure.
    139   1.1  christos  */
    140   1.1  christos int	sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
    141   1.1  christos 
    142   1.1  christos /*
    143   1.1  christos  * Consume len bytes from the start of buf
    144   1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    145   1.1  christos  */
    146   1.1  christos int	sshbuf_consume(struct sshbuf *buf, size_t len);
    147   1.1  christos 
    148   1.1  christos /*
    149   1.1  christos  * Consume len bytes from the end of buf
    150   1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    151   1.1  christos  */
    152   1.1  christos int	sshbuf_consume_end(struct sshbuf *buf, size_t len);
    153   1.1  christos 
    154  1.23  christos /*
    155  1.23  christos  * Consume data from a parent buffer up to that of a child buffer (i.e.
    156  1.23  christos  * one created by sshbuf_fromb()).
    157  1.23  christos  *
    158  1.23  christos  * Intended to be used in a pattern like:
    159  1.23  christos  *
    160  1.23  christos  *     b = sshbuf_fromb(parent);
    161  1.23  christos  *     sshbuf_get_string(b, &foo, &foostr);
    162  1.23  christos  *     sshbuf_get_u32(b, &bar);
    163  1.23  christos  *     sshbuf_consume_upto_child(parent, b);
    164  1.23  christos  *
    165  1.23  christos  * After which, both "b" and "parent" will point to the same data.
    166  1.23  christos  *
    167  1.23  christos  * "child" must be a direct child of "buf" (i.e. neither an unrelated buffer
    168  1.23  christos  * nor a grandchild) which has consumed data past that of "buf".
    169  1.23  christos  */
    170  1.23  christos int	sshbuf_consume_upto_child(struct sshbuf *buf, const struct sshbuf *child);
    171  1.23  christos 
    172   1.1  christos /* Extract or deposit some bytes */
    173   1.1  christos int	sshbuf_get(struct sshbuf *buf, void *v, size_t len);
    174   1.1  christos int	sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
    175   1.1  christos int	sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
    176   1.1  christos 
    177   1.1  christos /* Append using a printf(3) format */
    178   1.1  christos int	sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
    179   1.1  christos 	    __attribute__((format(printf, 2, 3)));
    180   1.3     joerg int	sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
    181   1.3     joerg 	    __printflike(2, 0);
    182   1.1  christos 
    183   1.1  christos /* Functions to extract or store big-endian words of various sizes */
    184  1.23  christos int	sshbuf_get_u64(struct sshbuf *buf, uint64_t *valp);
    185  1.23  christos int	sshbuf_get_u32(struct sshbuf *buf, uint32_t *valp);
    186  1.23  christos int	sshbuf_get_u16(struct sshbuf *buf, uint16_t *valp);
    187   1.1  christos int	sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
    188  1.23  christos int	sshbuf_put_u64(struct sshbuf *buf, uint64_t val);
    189  1.23  christos int	sshbuf_put_u32(struct sshbuf *buf, uint32_t val);
    190  1.23  christos int	sshbuf_put_u16(struct sshbuf *buf, uint16_t val);
    191   1.1  christos int	sshbuf_put_u8(struct sshbuf *buf, u_char val);
    192   1.1  christos 
    193  1.12  christos /* Functions to peek at the contents of a buffer without modifying it. */
    194  1.12  christos int	sshbuf_peek_u64(const struct sshbuf *buf, size_t offset,
    195  1.23  christos     uint64_t *valp);
    196  1.12  christos int	sshbuf_peek_u32(const struct sshbuf *buf, size_t offset,
    197  1.23  christos     uint32_t *valp);
    198  1.12  christos int	sshbuf_peek_u16(const struct sshbuf *buf, size_t offset,
    199  1.23  christos     uint16_t *valp);
    200  1.12  christos int	sshbuf_peek_u8(const struct sshbuf *buf, size_t offset,
    201  1.12  christos     u_char *valp);
    202  1.12  christos 
    203  1.12  christos /*
    204  1.14  christos  * Functions to poke values into an existing buffer (e.g. a length header
    205  1.12  christos  * to a packet). The destination bytes must already exist in the buffer.
    206  1.12  christos  */
    207  1.23  christos int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, uint64_t val);
    208  1.23  christos int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, uint32_t val);
    209  1.23  christos int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, uint16_t val);
    210  1.12  christos int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val);
    211  1.12  christos int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len);
    212  1.12  christos 
    213   1.1  christos /*
    214   1.1  christos  * Functions to extract or store SSH wire encoded strings (u32 len || data)
    215   1.1  christos  * The "cstring" variants admit no \0 characters in the string contents.
    216   1.1  christos  * Caller must free *valp.
    217   1.1  christos  */
    218   1.1  christos int	sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
    219   1.1  christos int	sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
    220   1.1  christos int	sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
    221   1.1  christos int	sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
    222   1.1  christos int	sshbuf_put_cstring(struct sshbuf *buf, const char *v);
    223   1.1  christos int	sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
    224   1.1  christos 
    225   1.1  christos /*
    226   1.1  christos  * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
    227   1.1  christos  * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
    228   1.1  christos  * next sshbuf-modifying function call. Caller does not free.
    229   1.1  christos  */
    230   1.1  christos int	sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
    231   1.1  christos 	    size_t *lenp);
    232   1.1  christos 
    233   1.1  christos /* Skip past a string */
    234   1.1  christos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
    235   1.1  christos 
    236   1.1  christos /* Another variant: "peeks" into the buffer without modifying it */
    237   1.1  christos int	sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
    238   1.1  christos 	    size_t *lenp);
    239   1.1  christos 
    240   1.1  christos /*
    241   1.1  christos  * Functions to extract or store SSH wire encoded bignums and elliptic
    242   1.1  christos  * curve points.
    243   1.1  christos  */
    244  1.11  christos int	sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
    245   1.4  christos int	sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
    246   1.4  christos 	    const u_char **valp, size_t *lenp);
    247   1.1  christos int	sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
    248   1.1  christos int	sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
    249   1.1  christos int	sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
    250   1.1  christos int	sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
    251   1.1  christos int	sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
    252   1.1  christos int	sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
    253  1.21  christos int	sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
    254   1.1  christos 
    255  1.23  christos /* Functions to extract or store various non-SSH wire encoded values */
    256  1.23  christos int	sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen,
    257  1.23  christos 	    char **valp, size_t *lenp);
    258  1.23  christos 
    259   1.1  christos /* Dump the contents of the buffer in a human-readable format */
    260  1.15  christos void	sshbuf_dump(const struct sshbuf *buf, FILE *f);
    261   1.1  christos 
    262   1.1  christos /* Dump specified memory in a human-readable format */
    263   1.1  christos void	sshbuf_dump_data(const void *s, size_t len, FILE *f);
    264   1.1  christos 
    265   1.1  christos /* Return the hexadecimal representation of the contents of the buffer */
    266  1.22  christos char	*sshbuf_dtob16(const struct sshbuf *buf);
    267  1.22  christos /* Make a sshbuf from a hex string */
    268  1.22  christos struct sshbuf *sshbuf_b16tod(const char *b16);
    269   1.1  christos 
    270   1.1  christos /* Encode the contents of the buffer as base64 */
    271  1.12  christos char	*sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
    272  1.12  christos int	sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
    273  1.15  christos /* RFC4648 "base64url" encoding variant */
    274  1.15  christos int	sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
    275   1.1  christos 
    276   1.1  christos /* Decode base64 data and append it to the buffer */
    277   1.1  christos int	sshbuf_b64tod(struct sshbuf *buf, const char *b64);
    278   1.1  christos 
    279   1.6  christos /*
    280  1.12  christos  * Tests whether the buffer contains the specified byte sequence at the
    281  1.12  christos  * specified offset. Returns 0 on successful match, or a ssherr.h code
    282  1.12  christos  * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
    283  1.12  christos  * present but the buffer contents did not match those supplied. Zero-
    284  1.12  christos  * length comparisons are not allowed.
    285  1.12  christos  *
    286  1.12  christos  * If sufficient data is present to make a comparison, then it is
    287  1.12  christos  * performed with timing independent of the value of the data. If
    288  1.12  christos  * insufficient data is present then the comparison is not attempted at
    289  1.12  christos  * all.
    290  1.12  christos  */
    291  1.12  christos int	sshbuf_cmp(const struct sshbuf *b, size_t offset,
    292  1.12  christos     const void *s, size_t len);
    293  1.12  christos 
    294  1.12  christos /*
    295  1.22  christos  * Test whether two buffers have identical contents.
    296  1.22  christos  * SSH_ERR_MESSAGE_INCOMPLETE indicates the buffers had differing size.
    297  1.22  christos  * SSH_ERR_INVALID_FORMAT indicates the buffers were the same size but
    298  1.22  christos  * had differing contents.
    299  1.22  christos  * Returns 0 on successful compare (comparing two empty buffers returns 0).
    300  1.22  christos  */
    301  1.22  christos int sshbuf_equals(const struct sshbuf *a, const struct sshbuf *b);
    302  1.22  christos 
    303  1.22  christos /*
    304  1.12  christos  * Searches the buffer for the specified string. Returns 0 on success
    305  1.12  christos  * and updates *offsetp with the offset of the first match, relative to
    306  1.12  christos  * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h
    307  1.12  christos  * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
    308  1.12  christos  * present in the buffer for a match to be possible but none was found.
    309  1.12  christos  * Searches for zero-length data are not allowed.
    310  1.12  christos  */
    311  1.12  christos int
    312  1.12  christos sshbuf_find(const struct sshbuf *b, size_t start_offset,
    313  1.12  christos     const void *s, size_t len, size_t *offsetp);
    314  1.12  christos 
    315  1.12  christos /*
    316   1.6  christos  * Duplicate the contents of a buffer to a string (caller to free).
    317   1.6  christos  * Returns NULL on buffer error, or if the buffer contains a premature
    318   1.6  christos  * nul character.
    319   1.6  christos  */
    320   1.6  christos char *sshbuf_dup_string(struct sshbuf *buf);
    321   1.6  christos 
    322  1.13  christos /*
    323  1.13  christos  * Fill a buffer from a file descriptor or filename. Both allocate the
    324  1.13  christos  * buffer for the caller.
    325  1.13  christos  */
    326  1.13  christos int sshbuf_load_fd(int, struct sshbuf **)
    327  1.13  christos     __attribute__((__nonnull__ (2)));
    328  1.13  christos int sshbuf_load_file(const char *, struct sshbuf **)
    329  1.13  christos     __attribute__((__nonnull__ (2)));
    330  1.13  christos 
    331  1.13  christos /*
    332  1.13  christos  * Write a buffer to a path, creating/truncating as needed (mode 0644,
    333  1.13  christos  * subject to umask). The buffer contents are not modified.
    334  1.13  christos  */
    335  1.13  christos int sshbuf_write_file(const char *path, struct sshbuf *buf)
    336  1.13  christos     __attribute__((__nonnull__ (2)));
    337  1.13  christos 
    338  1.16  christos /* Read up to maxlen bytes from a fd directly to a buffer */
    339  1.16  christos int sshbuf_read(int, struct sshbuf *, size_t, size_t *)
    340  1.16  christos     __attribute__((__nonnull__ (2)));
    341  1.16  christos 
    342   1.1  christos /* Macros for decoding/encoding integers */
    343   1.1  christos #define PEEK_U64(p) \
    344  1.23  christos 	(((uint64_t)(((const u_char *)(p))[0]) << 56) | \
    345  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[1]) << 48) | \
    346  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[2]) << 40) | \
    347  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[3]) << 32) | \
    348  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[4]) << 24) | \
    349  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[5]) << 16) | \
    350  1.23  christos 	 ((uint64_t)(((const u_char *)(p))[6]) << 8) | \
    351  1.23  christos 	  (uint64_t)(((const u_char *)(p))[7]))
    352   1.1  christos #define PEEK_U32(p) \
    353  1.23  christos 	(((uint32_t)(((const u_char *)(p))[0]) << 24) | \
    354  1.23  christos 	 ((uint32_t)(((const u_char *)(p))[1]) << 16) | \
    355  1.23  christos 	 ((uint32_t)(((const u_char *)(p))[2]) << 8) | \
    356  1.23  christos 	  (uint32_t)(((const u_char *)(p))[3]))
    357   1.1  christos #define PEEK_U16(p) \
    358  1.23  christos 	(((uint16_t)(((const u_char *)(p))[0]) << 8) | \
    359  1.23  christos 	  (uint16_t)(((const u_char *)(p))[1]))
    360   1.1  christos 
    361   1.1  christos #define POKE_U64(p, v) \
    362   1.1  christos 	do { \
    363  1.23  christos 		const uint64_t __v = (v); \
    364   1.5  christos 		((u_char *)(p))[0] = (__v >> 56) & 0xff; \
    365   1.5  christos 		((u_char *)(p))[1] = (__v >> 48) & 0xff; \
    366   1.5  christos 		((u_char *)(p))[2] = (__v >> 40) & 0xff; \
    367   1.5  christos 		((u_char *)(p))[3] = (__v >> 32) & 0xff; \
    368   1.5  christos 		((u_char *)(p))[4] = (__v >> 24) & 0xff; \
    369   1.5  christos 		((u_char *)(p))[5] = (__v >> 16) & 0xff; \
    370   1.5  christos 		((u_char *)(p))[6] = (__v >> 8) & 0xff; \
    371   1.5  christos 		((u_char *)(p))[7] = __v & 0xff; \
    372   1.1  christos 	} while (0)
    373   1.1  christos #define POKE_U32(p, v) \
    374   1.1  christos 	do { \
    375  1.23  christos 		const uint32_t __v = (v); \
    376   1.5  christos 		((u_char *)(p))[0] = (__v >> 24) & 0xff; \
    377   1.5  christos 		((u_char *)(p))[1] = (__v >> 16) & 0xff; \
    378   1.5  christos 		((u_char *)(p))[2] = (__v >> 8) & 0xff; \
    379   1.5  christos 		((u_char *)(p))[3] = __v & 0xff; \
    380   1.1  christos 	} while (0)
    381   1.1  christos #define POKE_U16(p, v) \
    382   1.1  christos 	do { \
    383  1.23  christos 		const uint16_t __v = (v); \
    384   1.5  christos 		((u_char *)(p))[0] = (__v >> 8) & 0xff; \
    385   1.5  christos 		((u_char *)(p))[1] = __v & 0xff; \
    386   1.1  christos 	} while (0)
    387   1.1  christos 
    388   1.1  christos /* Internal definitions follow. Exposed for regress tests */
    389   1.1  christos #ifdef SSHBUF_INTERNAL
    390   1.1  christos 
    391   1.1  christos /*
    392   1.1  christos  * Return the allocation size of buf
    393   1.1  christos  */
    394   1.1  christos size_t	sshbuf_alloc(const struct sshbuf *buf);
    395   1.1  christos 
    396   1.1  christos /*
    397   1.1  christos  * Increment the reference count of buf.
    398   1.1  christos  */
    399   1.1  christos int	sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
    400   1.1  christos 
    401   1.1  christos /*
    402   1.1  christos  * Return the parent buffer of buf, or NULL if it has no parent.
    403   1.1  christos  */
    404   1.1  christos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
    405   1.1  christos 
    406   1.1  christos /*
    407   1.1  christos  * Return the reference count of buf
    408   1.1  christos  */
    409   1.1  christos u_int	sshbuf_refcount(const struct sshbuf *buf);
    410   1.1  christos 
    411   1.1  christos # define SSHBUF_SIZE_INIT	256		/* Initial allocation */
    412   1.1  christos # define SSHBUF_SIZE_INC	256		/* Preferred increment length */
    413  1.16  christos # define SSHBUF_PACK_MIN	8192		/* Minimum packable offset */
    414   1.1  christos 
    415   1.1  christos /* # define SSHBUF_ABORT abort */
    416   1.1  christos /* # define SSHBUF_DEBUG */
    417   1.1  christos 
    418   1.1  christos # ifndef SSHBUF_ABORT
    419   1.1  christos #  define SSHBUF_ABORT()
    420   1.1  christos # endif
    421   1.1  christos 
    422   1.1  christos # ifdef SSHBUF_DEBUG
    423   1.1  christos #  define SSHBUF_DBG(x) do { \
    424   1.1  christos 		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
    425   1.1  christos 		printf x; \
    426   1.1  christos 		printf("\n"); \
    427   1.1  christos 		fflush(stdout); \
    428   1.1  christos 	} while (0)
    429   1.1  christos # else
    430   1.1  christos #  define SSHBUF_DBG(x)
    431   1.1  christos # endif
    432   1.1  christos #endif /* SSHBUF_INTERNAL */
    433   1.1  christos 
    434   1.1  christos #endif /* _SSHBUF_H */
    435