xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshbuf.h (revision 9469f4f13c84743995b7d51c506f9c9849ba30de)
1*9469f4f1Schristos /*	$NetBSD: sshbuf.h,v 1.21 2024/09/24 21:32:19 christos Exp $	*/
2*9469f4f1Schristos /*	$OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $	*/
3*9469f4f1Schristos 
45484a5efSchristos /*
55484a5efSchristos  * Copyright (c) 2011 Damien Miller
65484a5efSchristos  *
75484a5efSchristos  * Permission to use, copy, modify, and distribute this software for any
85484a5efSchristos  * purpose with or without fee is hereby granted, provided that the above
95484a5efSchristos  * copyright notice and this permission notice appear in all copies.
105484a5efSchristos  *
115484a5efSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
125484a5efSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
135484a5efSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
145484a5efSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
155484a5efSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
165484a5efSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
175484a5efSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
185484a5efSchristos  */
195484a5efSchristos 
205484a5efSchristos #ifndef _SSHBUF_H
215484a5efSchristos #define _SSHBUF_H
225484a5efSchristos 
235484a5efSchristos #include <sys/types.h>
245484a5efSchristos #include <stdarg.h>
255484a5efSchristos #include <stdio.h>
26cd4ada6aSchristos 
27cd4ada6aSchristos #ifdef WITH_OPENSSL
285484a5efSchristos #include <openssl/bn.h>
295484a5efSchristos #include <openssl/ec.h>
30cd4ada6aSchristos #include <openssl/ecdsa.h>
31*9469f4f1Schristos #include <openssl/evp.h>
32cd4ada6aSchristos #else /* OPENSSL */
33cd4ada6aSchristos #define BIGNUM		void
34cd4ada6aSchristos #define EC_KEY		void
35cd4ada6aSchristos #define EC_GROUP	void
36cd4ada6aSchristos #define EC_POINT	void
37*9469f4f1Schristos #define EVP_PKEY	void
38cd4ada6aSchristos #endif /* WITH_OPENSSL */
395484a5efSchristos 
407425a2c2Smlelstv #define SSHBUF_SIZE_MAX		0x10000000	/* Hard maximum size 256MB */
415484a5efSchristos #define SSHBUF_REFS_MAX		0x100000	/* Max child buffers */
425484a5efSchristos #define SSHBUF_MAX_BIGNUM	(16384 / 8)	/* Max bignum *bytes* */
435484a5efSchristos #define SSHBUF_MAX_ECPOINT	((528 * 2 / 8) + 1) /* Max EC point *bytes* */
445484a5efSchristos 
45b1066cf3Schristos struct sshbuf;
465484a5efSchristos 
475484a5efSchristos /*
485484a5efSchristos  * Create a new sshbuf buffer.
495484a5efSchristos  * Returns pointer to buffer on success, or NULL on allocation failure.
505484a5efSchristos  */
515484a5efSchristos struct sshbuf *sshbuf_new(void);
525484a5efSchristos 
535484a5efSchristos /*
545484a5efSchristos  * Create a new, read-only sshbuf buffer from existing data.
555484a5efSchristos  * Returns pointer to buffer on success, or NULL on allocation failure.
565484a5efSchristos  */
575484a5efSchristos struct sshbuf *sshbuf_from(const void *blob, size_t len);
585484a5efSchristos 
595484a5efSchristos /*
605484a5efSchristos  * Create a new, read-only sshbuf buffer from the contents of an existing
615484a5efSchristos  * buffer. The contents of "buf" must not change in the lifetime of the
625484a5efSchristos  * resultant buffer.
635484a5efSchristos  * Returns pointer to buffer on success, or NULL on allocation failure.
645484a5efSchristos  */
655484a5efSchristos struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
665484a5efSchristos 
675484a5efSchristos /*
685484a5efSchristos  * Create a new, read-only sshbuf buffer from the contents of a string in
695484a5efSchristos  * an existing buffer (the string is consumed in the process).
705484a5efSchristos  * The contents of "buf" must not change in the lifetime of the resultant
715484a5efSchristos  * buffer.
725484a5efSchristos  * Returns pointer to buffer on success, or NULL on allocation failure.
735484a5efSchristos  */
745484a5efSchristos int	sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
755484a5efSchristos 
765484a5efSchristos /*
775484a5efSchristos  * Clear and free buf
785484a5efSchristos  */
795484a5efSchristos void	sshbuf_free(struct sshbuf *buf);
805484a5efSchristos 
815484a5efSchristos /*
825484a5efSchristos  * Reset buf, clearing its contents. NB. max_size is preserved.
835484a5efSchristos  */
845484a5efSchristos void	sshbuf_reset(struct sshbuf *buf);
855484a5efSchristos 
865484a5efSchristos /*
875484a5efSchristos  * Return the maximum size of buf
885484a5efSchristos  */
895484a5efSchristos size_t	sshbuf_max_size(const struct sshbuf *buf);
905484a5efSchristos 
915484a5efSchristos /*
925484a5efSchristos  * Set the maximum size of buf
935484a5efSchristos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
945484a5efSchristos  */
955484a5efSchristos int	sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
965484a5efSchristos 
975484a5efSchristos /*
985484a5efSchristos  * Returns the length of data in buf
995484a5efSchristos  */
1005484a5efSchristos size_t	sshbuf_len(const struct sshbuf *buf);
1015484a5efSchristos 
1025484a5efSchristos /*
1035484a5efSchristos  * Returns number of bytes left in buffer before hitting max_size.
1045484a5efSchristos  */
1055484a5efSchristos size_t	sshbuf_avail(const struct sshbuf *buf);
1065484a5efSchristos 
1075484a5efSchristos /*
10879976551Schristos  * Returns a read-only pointer to the start of the data in buf
1095484a5efSchristos  */
1105484a5efSchristos const u_char *sshbuf_ptr(const struct sshbuf *buf);
1115484a5efSchristos 
1125484a5efSchristos /*
11379976551Schristos  * Returns a mutable pointer to the start of the data in buf, or
1145484a5efSchristos  * NULL if the buffer is read-only.
1155484a5efSchristos  */
1165484a5efSchristos u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
1175484a5efSchristos 
1185484a5efSchristos /*
1195484a5efSchristos  * Check whether a reservation of size len will succeed in buf
1205484a5efSchristos  * Safer to use than direct comparisons again sshbuf_avail as it copes
1215484a5efSchristos  * with unsigned overflows correctly.
1225484a5efSchristos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
1235484a5efSchristos  */
1245484a5efSchristos int	sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
1255484a5efSchristos 
1265484a5efSchristos /*
127ee85abc4Schristos  * Preallocates len additional bytes in buf.
128ee85abc4Schristos  * Useful for cases where the caller knows how many bytes will ultimately be
129ee85abc4Schristos  * required to avoid realloc in the buffer code.
130ee85abc4Schristos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
131ee85abc4Schristos  */
132ee85abc4Schristos int	sshbuf_allocate(struct sshbuf *buf, size_t len);
133ee85abc4Schristos 
134ee85abc4Schristos /*
1355484a5efSchristos  * Reserve len bytes in buf.
1365484a5efSchristos  * Returns 0 on success and a pointer to the first reserved byte via the
1378db691beSchristos  * optional dpp parameter or a negative SSH_ERR_* error code on failure.
1385484a5efSchristos  */
1395484a5efSchristos int	sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
1405484a5efSchristos 
1415484a5efSchristos /*
1425484a5efSchristos  * Consume len bytes from the start of buf
1435484a5efSchristos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
1445484a5efSchristos  */
1455484a5efSchristos int	sshbuf_consume(struct sshbuf *buf, size_t len);
1465484a5efSchristos 
1475484a5efSchristos /*
1485484a5efSchristos  * Consume len bytes from the end of buf
1495484a5efSchristos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
1505484a5efSchristos  */
1515484a5efSchristos int	sshbuf_consume_end(struct sshbuf *buf, size_t len);
1525484a5efSchristos 
1535484a5efSchristos /* Extract or deposit some bytes */
1545484a5efSchristos int	sshbuf_get(struct sshbuf *buf, void *v, size_t len);
1555484a5efSchristos int	sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
1565484a5efSchristos int	sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
1575484a5efSchristos 
1585484a5efSchristos /* Append using a printf(3) format */
1595484a5efSchristos int	sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
1605484a5efSchristos 	    __attribute__((format(printf, 2, 3)));
16105c84359Sjoerg int	sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
16205c84359Sjoerg 	    __printflike(2, 0);
1635484a5efSchristos 
1645484a5efSchristos /* Functions to extract or store big-endian words of various sizes */
1655484a5efSchristos int	sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
1665484a5efSchristos int	sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
1675484a5efSchristos int	sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
1685484a5efSchristos int	sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
1695484a5efSchristos int	sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
1705484a5efSchristos int	sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
1715484a5efSchristos int	sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
1725484a5efSchristos int	sshbuf_put_u8(struct sshbuf *buf, u_char val);
1735484a5efSchristos 
174cd4ada6aSchristos /* Functions to peek at the contents of a buffer without modifying it. */
175cd4ada6aSchristos int	sshbuf_peek_u64(const struct sshbuf *buf, size_t offset,
176cd4ada6aSchristos     u_int64_t *valp);
177cd4ada6aSchristos int	sshbuf_peek_u32(const struct sshbuf *buf, size_t offset,
178cd4ada6aSchristos     u_int32_t *valp);
179cd4ada6aSchristos int	sshbuf_peek_u16(const struct sshbuf *buf, size_t offset,
180cd4ada6aSchristos     u_int16_t *valp);
181cd4ada6aSchristos int	sshbuf_peek_u8(const struct sshbuf *buf, size_t offset,
182cd4ada6aSchristos     u_char *valp);
183cd4ada6aSchristos 
184cd4ada6aSchristos /*
1858db691beSchristos  * Functions to poke values into an existing buffer (e.g. a length header
186cd4ada6aSchristos  * to a packet). The destination bytes must already exist in the buffer.
187cd4ada6aSchristos  */
188cd4ada6aSchristos int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val);
189cd4ada6aSchristos int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val);
190cd4ada6aSchristos int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val);
191cd4ada6aSchristos int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val);
192cd4ada6aSchristos int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len);
193cd4ada6aSchristos 
1945484a5efSchristos /*
1955484a5efSchristos  * Functions to extract or store SSH wire encoded strings (u32 len || data)
1965484a5efSchristos  * The "cstring" variants admit no \0 characters in the string contents.
1975484a5efSchristos  * Caller must free *valp.
1985484a5efSchristos  */
1995484a5efSchristos int	sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
2005484a5efSchristos int	sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
2015484a5efSchristos int	sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
2025484a5efSchristos int	sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
2035484a5efSchristos int	sshbuf_put_cstring(struct sshbuf *buf, const char *v);
2045484a5efSchristos int	sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
2055484a5efSchristos 
2065484a5efSchristos /*
2075484a5efSchristos  * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
2085484a5efSchristos  * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
2095484a5efSchristos  * next sshbuf-modifying function call. Caller does not free.
2105484a5efSchristos  */
2115484a5efSchristos int	sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
2125484a5efSchristos 	    size_t *lenp);
2135484a5efSchristos 
2145484a5efSchristos /* Skip past a string */
2155484a5efSchristos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
2165484a5efSchristos 
2175484a5efSchristos /* Another variant: "peeks" into the buffer without modifying it */
2185484a5efSchristos int	sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
2195484a5efSchristos 	    size_t *lenp);
2205484a5efSchristos 
2215484a5efSchristos /*
2225484a5efSchristos  * Functions to extract or store SSH wire encoded bignums and elliptic
2235484a5efSchristos  * curve points.
2245484a5efSchristos  */
225aa36fcacSchristos int	sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
226e4d43b82Schristos int	sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
227e4d43b82Schristos 	    const u_char **valp, size_t *lenp);
2285484a5efSchristos int	sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
2295484a5efSchristos int	sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
2305484a5efSchristos int	sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
2315484a5efSchristos int	sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
2325484a5efSchristos int	sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
2335484a5efSchristos int	sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
234*9469f4f1Schristos int	sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
2355484a5efSchristos 
2365484a5efSchristos /* Dump the contents of the buffer in a human-readable format */
2372d3b0f52Schristos void	sshbuf_dump(const struct sshbuf *buf, FILE *f);
2385484a5efSchristos 
2395484a5efSchristos /* Dump specified memory in a human-readable format */
2405484a5efSchristos void	sshbuf_dump_data(const void *s, size_t len, FILE *f);
2415484a5efSchristos 
2425484a5efSchristos /* Return the hexadecimal representation of the contents of the buffer */
2435484a5efSchristos char	*sshbuf_dtob16(struct sshbuf *buf);
2445484a5efSchristos 
2455484a5efSchristos /* Encode the contents of the buffer as base64 */
246cd4ada6aSchristos char	*sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
247cd4ada6aSchristos int	sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
2482d3b0f52Schristos /* RFC4648 "base64url" encoding variant */
2492d3b0f52Schristos int	sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
2505484a5efSchristos 
2515484a5efSchristos /* Decode base64 data and append it to the buffer */
2525484a5efSchristos int	sshbuf_b64tod(struct sshbuf *buf, const char *b64);
2535484a5efSchristos 
2545101d403Schristos /*
255cd4ada6aSchristos  * Tests whether the buffer contains the specified byte sequence at the
256cd4ada6aSchristos  * specified offset. Returns 0 on successful match, or a ssherr.h code
257cd4ada6aSchristos  * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
258cd4ada6aSchristos  * present but the buffer contents did not match those supplied. Zero-
259cd4ada6aSchristos  * length comparisons are not allowed.
260cd4ada6aSchristos  *
261cd4ada6aSchristos  * If sufficient data is present to make a comparison, then it is
262cd4ada6aSchristos  * performed with timing independent of the value of the data. If
263cd4ada6aSchristos  * insufficient data is present then the comparison is not attempted at
264cd4ada6aSchristos  * all.
265cd4ada6aSchristos  */
266cd4ada6aSchristos int	sshbuf_cmp(const struct sshbuf *b, size_t offset,
267cd4ada6aSchristos     const void *s, size_t len);
268cd4ada6aSchristos 
269cd4ada6aSchristos /*
270cd4ada6aSchristos  * Searches the buffer for the specified string. Returns 0 on success
271cd4ada6aSchristos  * and updates *offsetp with the offset of the first match, relative to
272cd4ada6aSchristos  * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h
273cd4ada6aSchristos  * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
274cd4ada6aSchristos  * present in the buffer for a match to be possible but none was found.
275cd4ada6aSchristos  * Searches for zero-length data are not allowed.
276cd4ada6aSchristos  */
277cd4ada6aSchristos int
278cd4ada6aSchristos sshbuf_find(const struct sshbuf *b, size_t start_offset,
279cd4ada6aSchristos     const void *s, size_t len, size_t *offsetp);
280cd4ada6aSchristos 
281cd4ada6aSchristos /*
2825101d403Schristos  * Duplicate the contents of a buffer to a string (caller to free).
2835101d403Schristos  * Returns NULL on buffer error, or if the buffer contains a premature
2845101d403Schristos  * nul character.
2855101d403Schristos  */
2865101d403Schristos char *sshbuf_dup_string(struct sshbuf *buf);
2875101d403Schristos 
288ed75d7a8Schristos /*
289ed75d7a8Schristos  * Fill a buffer from a file descriptor or filename. Both allocate the
290ed75d7a8Schristos  * buffer for the caller.
291ed75d7a8Schristos  */
292ed75d7a8Schristos int sshbuf_load_fd(int, struct sshbuf **)
293ed75d7a8Schristos     __attribute__((__nonnull__ (2)));
294ed75d7a8Schristos int sshbuf_load_file(const char *, struct sshbuf **)
295ed75d7a8Schristos     __attribute__((__nonnull__ (2)));
296ed75d7a8Schristos 
297ed75d7a8Schristos /*
298ed75d7a8Schristos  * Write a buffer to a path, creating/truncating as needed (mode 0644,
299ed75d7a8Schristos  * subject to umask). The buffer contents are not modified.
300ed75d7a8Schristos  */
301ed75d7a8Schristos int sshbuf_write_file(const char *path, struct sshbuf *buf)
302ed75d7a8Schristos     __attribute__((__nonnull__ (2)));
303ed75d7a8Schristos 
304a03ec00cSchristos /* Read up to maxlen bytes from a fd directly to a buffer */
305a03ec00cSchristos int sshbuf_read(int, struct sshbuf *, size_t, size_t *)
306a03ec00cSchristos     __attribute__((__nonnull__ (2)));
307a03ec00cSchristos 
3085484a5efSchristos /* Macros for decoding/encoding integers */
3095484a5efSchristos #define PEEK_U64(p) \
3108a4530f9Schristos 	(((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
3118a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
3128a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
3138a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
3148a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
3158a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
3168a4530f9Schristos 	 ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
3178a4530f9Schristos 	  (u_int64_t)(((const u_char *)(p))[7]))
3185484a5efSchristos #define PEEK_U32(p) \
3198a4530f9Schristos 	(((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
3208a4530f9Schristos 	 ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
3218a4530f9Schristos 	 ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
3228a4530f9Schristos 	  (u_int32_t)(((const u_char *)(p))[3]))
3235484a5efSchristos #define PEEK_U16(p) \
3248a4530f9Schristos 	(((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
3258a4530f9Schristos 	  (u_int16_t)(((const u_char *)(p))[1]))
3265484a5efSchristos 
3275484a5efSchristos #define POKE_U64(p, v) \
3285484a5efSchristos 	do { \
32979976551Schristos 		const u_int64_t __v = (v); \
33079976551Schristos 		((u_char *)(p))[0] = (__v >> 56) & 0xff; \
33179976551Schristos 		((u_char *)(p))[1] = (__v >> 48) & 0xff; \
33279976551Schristos 		((u_char *)(p))[2] = (__v >> 40) & 0xff; \
33379976551Schristos 		((u_char *)(p))[3] = (__v >> 32) & 0xff; \
33479976551Schristos 		((u_char *)(p))[4] = (__v >> 24) & 0xff; \
33579976551Schristos 		((u_char *)(p))[5] = (__v >> 16) & 0xff; \
33679976551Schristos 		((u_char *)(p))[6] = (__v >> 8) & 0xff; \
33779976551Schristos 		((u_char *)(p))[7] = __v & 0xff; \
3385484a5efSchristos 	} while (0)
3395484a5efSchristos #define POKE_U32(p, v) \
3405484a5efSchristos 	do { \
34179976551Schristos 		const u_int32_t __v = (v); \
34279976551Schristos 		((u_char *)(p))[0] = (__v >> 24) & 0xff; \
34379976551Schristos 		((u_char *)(p))[1] = (__v >> 16) & 0xff; \
34479976551Schristos 		((u_char *)(p))[2] = (__v >> 8) & 0xff; \
34579976551Schristos 		((u_char *)(p))[3] = __v & 0xff; \
3465484a5efSchristos 	} while (0)
3475484a5efSchristos #define POKE_U16(p, v) \
3485484a5efSchristos 	do { \
34979976551Schristos 		const u_int16_t __v = (v); \
35079976551Schristos 		((u_char *)(p))[0] = (__v >> 8) & 0xff; \
35179976551Schristos 		((u_char *)(p))[1] = __v & 0xff; \
3525484a5efSchristos 	} while (0)
3535484a5efSchristos 
3545484a5efSchristos /* Internal definitions follow. Exposed for regress tests */
3555484a5efSchristos #ifdef SSHBUF_INTERNAL
3565484a5efSchristos 
3575484a5efSchristos /*
3585484a5efSchristos  * Return the allocation size of buf
3595484a5efSchristos  */
3605484a5efSchristos size_t	sshbuf_alloc(const struct sshbuf *buf);
3615484a5efSchristos 
3625484a5efSchristos /*
3635484a5efSchristos  * Increment the reference count of buf.
3645484a5efSchristos  */
3655484a5efSchristos int	sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
3665484a5efSchristos 
3675484a5efSchristos /*
3685484a5efSchristos  * Return the parent buffer of buf, or NULL if it has no parent.
3695484a5efSchristos  */
3705484a5efSchristos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
3715484a5efSchristos 
3725484a5efSchristos /*
3735484a5efSchristos  * Return the reference count of buf
3745484a5efSchristos  */
3755484a5efSchristos u_int	sshbuf_refcount(const struct sshbuf *buf);
3765484a5efSchristos 
3775484a5efSchristos # define SSHBUF_SIZE_INIT	256		/* Initial allocation */
3785484a5efSchristos # define SSHBUF_SIZE_INC	256		/* Preferred increment length */
379a03ec00cSchristos # define SSHBUF_PACK_MIN	8192		/* Minimum packable offset */
3805484a5efSchristos 
3815484a5efSchristos /* # define SSHBUF_ABORT abort */
3825484a5efSchristos /* # define SSHBUF_DEBUG */
3835484a5efSchristos 
3845484a5efSchristos # ifndef SSHBUF_ABORT
3855484a5efSchristos #  define SSHBUF_ABORT()
3865484a5efSchristos # endif
3875484a5efSchristos 
3885484a5efSchristos # ifdef SSHBUF_DEBUG
3895484a5efSchristos #  define SSHBUF_DBG(x) do { \
3905484a5efSchristos 		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
3915484a5efSchristos 		printf x; \
3925484a5efSchristos 		printf("\n"); \
3935484a5efSchristos 		fflush(stdout); \
3945484a5efSchristos 	} while (0)
3955484a5efSchristos # else
3965484a5efSchristos #  define SSHBUF_DBG(x)
3975484a5efSchristos # endif
3985484a5efSchristos #endif /* SSHBUF_INTERNAL */
3995484a5efSchristos 
4005484a5efSchristos #endif /* _SSHBUF_H */
401