xref: /openbsd-src/usr.bin/ssh/sshbuf.h (revision 5411e7691687441b2e28282ca3a1c531c6b949c4)
1*5411e769Sdjm /*	$OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $	*/
215b55daeSdjm /*
315b55daeSdjm  * Copyright (c) 2011 Damien Miller
415b55daeSdjm  *
515b55daeSdjm  * Permission to use, copy, modify, and distribute this software for any
615b55daeSdjm  * purpose with or without fee is hereby granted, provided that the above
715b55daeSdjm  * copyright notice and this permission notice appear in all copies.
815b55daeSdjm  *
915b55daeSdjm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1015b55daeSdjm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1115b55daeSdjm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1215b55daeSdjm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1315b55daeSdjm  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1415b55daeSdjm  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1515b55daeSdjm  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1615b55daeSdjm  */
1715b55daeSdjm 
1815b55daeSdjm #ifndef _SSHBUF_H
1915b55daeSdjm #define _SSHBUF_H
2015b55daeSdjm 
2115b55daeSdjm #include <sys/types.h>
2215b55daeSdjm #include <stdarg.h>
2315b55daeSdjm #include <stdio.h>
241f96526fSdjm 
251f96526fSdjm #ifdef WITH_OPENSSL
2615b55daeSdjm #include <openssl/bn.h>
2715b55daeSdjm #include <openssl/ec.h>
281f96526fSdjm #include <openssl/ecdsa.h>
29*5411e769Sdjm #include <openssl/evp.h>
301f96526fSdjm #else /* OPENSSL */
311f96526fSdjm #define BIGNUM		void
321f96526fSdjm #define EC_KEY		void
331f96526fSdjm #define EC_GROUP	void
341f96526fSdjm #define EC_POINT	void
35*5411e769Sdjm #define EVP_PKEY	void
361f96526fSdjm #endif /* WITH_OPENSSL */
3715b55daeSdjm 
3815b55daeSdjm #define SSHBUF_SIZE_MAX		0x8000000	/* Hard maximum size */
3915b55daeSdjm #define SSHBUF_REFS_MAX		0x100000	/* Max child buffers */
4015b55daeSdjm #define SSHBUF_MAX_BIGNUM	(16384 / 8)	/* Max bignum *bytes* */
4115b55daeSdjm #define SSHBUF_MAX_ECPOINT	((528 * 2 / 8) + 1) /* Max EC point *bytes* */
4215b55daeSdjm 
4360dd6e7eSdjm struct sshbuf;
4415b55daeSdjm 
4515b55daeSdjm /*
4615b55daeSdjm  * Create a new sshbuf buffer.
4715b55daeSdjm  * Returns pointer to buffer on success, or NULL on allocation failure.
4815b55daeSdjm  */
4915b55daeSdjm struct sshbuf *sshbuf_new(void);
5015b55daeSdjm 
5115b55daeSdjm /*
5215b55daeSdjm  * Create a new, read-only sshbuf buffer from existing data.
5315b55daeSdjm  * Returns pointer to buffer on success, or NULL on allocation failure.
5415b55daeSdjm  */
5515b55daeSdjm struct sshbuf *sshbuf_from(const void *blob, size_t len);
5615b55daeSdjm 
5715b55daeSdjm /*
5815b55daeSdjm  * Create a new, read-only sshbuf buffer from the contents of an existing
5915b55daeSdjm  * buffer. The contents of "buf" must not change in the lifetime of the
6015b55daeSdjm  * resultant buffer.
6115b55daeSdjm  * Returns pointer to buffer on success, or NULL on allocation failure.
6215b55daeSdjm  */
6315b55daeSdjm struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
6415b55daeSdjm 
6515b55daeSdjm /*
6615b55daeSdjm  * Create a new, read-only sshbuf buffer from the contents of a string in
6715b55daeSdjm  * an existing buffer (the string is consumed in the process).
6815b55daeSdjm  * The contents of "buf" must not change in the lifetime of the resultant
6915b55daeSdjm  * buffer.
7015b55daeSdjm  * Returns pointer to buffer on success, or NULL on allocation failure.
7115b55daeSdjm  */
7215b55daeSdjm int	sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
7315b55daeSdjm 
7415b55daeSdjm /*
7515b55daeSdjm  * Clear and free buf
7615b55daeSdjm  */
7715b55daeSdjm void	sshbuf_free(struct sshbuf *buf);
7815b55daeSdjm 
7915b55daeSdjm /*
8015b55daeSdjm  * Reset buf, clearing its contents. NB. max_size is preserved.
8115b55daeSdjm  */
8215b55daeSdjm void	sshbuf_reset(struct sshbuf *buf);
8315b55daeSdjm 
8415b55daeSdjm /*
8515b55daeSdjm  * Return the maximum size of buf
8615b55daeSdjm  */
8715b55daeSdjm size_t	sshbuf_max_size(const struct sshbuf *buf);
8815b55daeSdjm 
8915b55daeSdjm /*
9015b55daeSdjm  * Set the maximum size of buf
9115b55daeSdjm  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
9215b55daeSdjm  */
9315b55daeSdjm int	sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
9415b55daeSdjm 
9515b55daeSdjm /*
9615b55daeSdjm  * Returns the length of data in buf
9715b55daeSdjm  */
9815b55daeSdjm size_t	sshbuf_len(const struct sshbuf *buf);
9915b55daeSdjm 
10015b55daeSdjm /*
10115b55daeSdjm  * Returns number of bytes left in buffer before hitting max_size.
10215b55daeSdjm  */
10315b55daeSdjm size_t	sshbuf_avail(const struct sshbuf *buf);
10415b55daeSdjm 
10515b55daeSdjm /*
106ea9d9e1eSmmcc  * Returns a read-only pointer to the start of the data in buf
10715b55daeSdjm  */
10815b55daeSdjm const u_char *sshbuf_ptr(const struct sshbuf *buf);
10915b55daeSdjm 
11015b55daeSdjm /*
111ea9d9e1eSmmcc  * Returns a mutable pointer to the start of the data in buf, or
11215b55daeSdjm  * NULL if the buffer is read-only.
11315b55daeSdjm  */
11415b55daeSdjm u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
11515b55daeSdjm 
11615b55daeSdjm /*
11715b55daeSdjm  * Check whether a reservation of size len will succeed in buf
11815b55daeSdjm  * Safer to use than direct comparisons again sshbuf_avail as it copes
11915b55daeSdjm  * with unsigned overflows correctly.
12015b55daeSdjm  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
12115b55daeSdjm  */
12215b55daeSdjm int	sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
12315b55daeSdjm 
12415b55daeSdjm /*
12566d9ceccSdjm  * Preallocates len additional bytes in buf.
12666d9ceccSdjm  * Useful for cases where the caller knows how many bytes will ultimately be
12766d9ceccSdjm  * required to avoid realloc in the buffer code.
12866d9ceccSdjm  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
12966d9ceccSdjm  */
13066d9ceccSdjm int	sshbuf_allocate(struct sshbuf *buf, size_t len);
13166d9ceccSdjm 
13266d9ceccSdjm /*
13315b55daeSdjm  * Reserve len bytes in buf.
13415b55daeSdjm  * Returns 0 on success and a pointer to the first reserved byte via the
135aa383ffdSdtucker  * optional dpp parameter or a negative SSH_ERR_* error code on failure.
13615b55daeSdjm  */
13715b55daeSdjm int	sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
13815b55daeSdjm 
13915b55daeSdjm /*
14015b55daeSdjm  * Consume len bytes from the start of buf
14115b55daeSdjm  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
14215b55daeSdjm  */
14315b55daeSdjm int	sshbuf_consume(struct sshbuf *buf, size_t len);
14415b55daeSdjm 
14515b55daeSdjm /*
14615b55daeSdjm  * Consume len bytes from the end of buf
14715b55daeSdjm  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
14815b55daeSdjm  */
14915b55daeSdjm int	sshbuf_consume_end(struct sshbuf *buf, size_t len);
15015b55daeSdjm 
15115b55daeSdjm /* Extract or deposit some bytes */
15215b55daeSdjm int	sshbuf_get(struct sshbuf *buf, void *v, size_t len);
15315b55daeSdjm int	sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
15415b55daeSdjm int	sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
15515b55daeSdjm 
15615b55daeSdjm /* Append using a printf(3) format */
15715b55daeSdjm int	sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
15815b55daeSdjm 	    __attribute__((format(printf, 2, 3)));
15915b55daeSdjm int	sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap);
16015b55daeSdjm 
16115b55daeSdjm /* Functions to extract or store big-endian words of various sizes */
16215b55daeSdjm int	sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
16315b55daeSdjm int	sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
16415b55daeSdjm int	sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
16515b55daeSdjm int	sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
16615b55daeSdjm int	sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
16715b55daeSdjm int	sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
16815b55daeSdjm int	sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
16915b55daeSdjm int	sshbuf_put_u8(struct sshbuf *buf, u_char val);
17015b55daeSdjm 
17159567923Sdjm /* Functions to peek at the contents of a buffer without modifying it. */
17259567923Sdjm int	sshbuf_peek_u64(const struct sshbuf *buf, size_t offset,
17359567923Sdjm     u_int64_t *valp);
17459567923Sdjm int	sshbuf_peek_u32(const struct sshbuf *buf, size_t offset,
17559567923Sdjm     u_int32_t *valp);
17659567923Sdjm int	sshbuf_peek_u16(const struct sshbuf *buf, size_t offset,
17759567923Sdjm     u_int16_t *valp);
17859567923Sdjm int	sshbuf_peek_u8(const struct sshbuf *buf, size_t offset,
17959567923Sdjm     u_char *valp);
18059567923Sdjm 
18159567923Sdjm /*
182264cfea2Sdjm  * Functions to poke values into an existing buffer (e.g. a length header
18359567923Sdjm  * to a packet). The destination bytes must already exist in the buffer.
18459567923Sdjm  */
18559567923Sdjm int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val);
18659567923Sdjm int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val);
18759567923Sdjm int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val);
18859567923Sdjm int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val);
18959567923Sdjm int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len);
19059567923Sdjm 
19115b55daeSdjm /*
19215b55daeSdjm  * Functions to extract or store SSH wire encoded strings (u32 len || data)
19315b55daeSdjm  * The "cstring" variants admit no \0 characters in the string contents.
19415b55daeSdjm  * Caller must free *valp.
19515b55daeSdjm  */
19615b55daeSdjm int	sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
19715b55daeSdjm int	sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
19815b55daeSdjm int	sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
19915b55daeSdjm int	sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
20015b55daeSdjm int	sshbuf_put_cstring(struct sshbuf *buf, const char *v);
20115b55daeSdjm int	sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
20215b55daeSdjm 
20315b55daeSdjm /*
20415b55daeSdjm  * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
20515b55daeSdjm  * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
20615b55daeSdjm  * next sshbuf-modifying function call. Caller does not free.
20715b55daeSdjm  */
20815b55daeSdjm int	sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
20915b55daeSdjm 	    size_t *lenp);
21015b55daeSdjm 
21115b55daeSdjm /* Skip past a string */
21215b55daeSdjm #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
21315b55daeSdjm 
21415b55daeSdjm /* Another variant: "peeks" into the buffer without modifying it */
21515b55daeSdjm int	sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
21615b55daeSdjm 	    size_t *lenp);
21715b55daeSdjm 
21815b55daeSdjm /*
21915b55daeSdjm  * Functions to extract or store SSH wire encoded bignums and elliptic
22015b55daeSdjm  * curve points.
22115b55daeSdjm  */
222f37f3ee2Sdjm int	sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
22378e9a853Sdjm int	sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
22478e9a853Sdjm 	    const u_char **valp, size_t *lenp);
22515b55daeSdjm int	sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
2266b35e824Sdtucker int	sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
2276b35e824Sdtucker int	sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
2286b35e824Sdtucker int	sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
22915b55daeSdjm int	sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
23015b55daeSdjm int	sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
231*5411e769Sdjm int	sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
23215b55daeSdjm 
233ea2d8289Sdjm /* Dump the contents of the buffer in a human-readable format */
23419d7d104Sdjm void	sshbuf_dump(const struct sshbuf *buf, FILE *f);
23515b55daeSdjm 
236ea2d8289Sdjm /* Dump specified memory in a human-readable format */
237ea2d8289Sdjm void	sshbuf_dump_data(const void *s, size_t len, FILE *f);
238ea2d8289Sdjm 
23915b55daeSdjm /* Return the hexadecimal representation of the contents of the buffer */
24015b55daeSdjm char	*sshbuf_dtob16(struct sshbuf *buf);
24115b55daeSdjm 
24215b55daeSdjm /* Encode the contents of the buffer as base64 */
243bbb0e5b6Sdjm char	*sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
244bbb0e5b6Sdjm int	sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
245d5e61526Sdjm /* RFC4648 "base64url" encoding variant */
246d5e61526Sdjm int	sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
24715b55daeSdjm 
24815b55daeSdjm /* Decode base64 data and append it to the buffer */
24915b55daeSdjm int	sshbuf_b64tod(struct sshbuf *buf, const char *b64);
25015b55daeSdjm 
2519a1b52afSdjm /*
2529e7a6b9fSdjm  * Tests whether the buffer contains the specified byte sequence at the
2539e7a6b9fSdjm  * specified offset. Returns 0 on successful match, or a ssherr.h code
2549e7a6b9fSdjm  * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
2559e7a6b9fSdjm  * present but the buffer contents did not match those supplied. Zero-
2569e7a6b9fSdjm  * length comparisons are not allowed.
2579e7a6b9fSdjm  *
2589e7a6b9fSdjm  * If sufficient data is present to make a comparison, then it is
2599e7a6b9fSdjm  * performed with timing independent of the value of the data. If
2609e7a6b9fSdjm  * insufficient data is present then the comparison is not attempted at
2619e7a6b9fSdjm  * all.
2629e7a6b9fSdjm  */
2639e7a6b9fSdjm int	sshbuf_cmp(const struct sshbuf *b, size_t offset,
264d8ff1cfbSdjm     const void *s, size_t len);
2659e7a6b9fSdjm 
2669e7a6b9fSdjm /*
2679e7a6b9fSdjm  * Searches the buffer for the specified string. Returns 0 on success
2689e7a6b9fSdjm  * and updates *offsetp with the offset of the first match, relative to
2699e7a6b9fSdjm  * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h
2709e7a6b9fSdjm  * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
2719e7a6b9fSdjm  * present in the buffer for a match to be possible but none was found.
2729e7a6b9fSdjm  * Searches for zero-length data are not allowed.
2739e7a6b9fSdjm  */
2749e7a6b9fSdjm int
2759e7a6b9fSdjm sshbuf_find(const struct sshbuf *b, size_t start_offset,
276d8ff1cfbSdjm     const void *s, size_t len, size_t *offsetp);
2779e7a6b9fSdjm 
2789e7a6b9fSdjm /*
2799a1b52afSdjm  * Duplicate the contents of a buffer to a string (caller to free).
2809a1b52afSdjm  * Returns NULL on buffer error, or if the buffer contains a premature
2819a1b52afSdjm  * nul character.
2829a1b52afSdjm  */
2839a1b52afSdjm char *sshbuf_dup_string(struct sshbuf *buf);
2849a1b52afSdjm 
28562af2284Sdjm /*
28662af2284Sdjm  * Fill a buffer from a file descriptor or filename. Both allocate the
28762af2284Sdjm  * buffer for the caller.
28862af2284Sdjm  */
28962af2284Sdjm int sshbuf_load_fd(int, struct sshbuf **)
29062af2284Sdjm     __attribute__((__nonnull__ (2)));
29162af2284Sdjm int sshbuf_load_file(const char *, struct sshbuf **)
29262af2284Sdjm     __attribute__((__nonnull__ (2)));
29362af2284Sdjm 
29462af2284Sdjm /*
29562af2284Sdjm  * Write a buffer to a path, creating/truncating as needed (mode 0644,
29662af2284Sdjm  * subject to umask). The buffer contents are not modified.
29762af2284Sdjm  */
29862af2284Sdjm int sshbuf_write_file(const char *path, struct sshbuf *buf)
29962af2284Sdjm     __attribute__((__nonnull__ (2)));
30062af2284Sdjm 
30141c2b893Sdjm /* Read up to maxlen bytes from a fd directly to a buffer */
30241c2b893Sdjm int sshbuf_read(int, struct sshbuf *, size_t, size_t *)
30341c2b893Sdjm     __attribute__((__nonnull__ (2)));
30441c2b893Sdjm 
30515b55daeSdjm /* Macros for decoding/encoding integers */
30615b55daeSdjm #define PEEK_U64(p) \
3073cad027eSdjm 	(((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
3083cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
3093cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
3103cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
3113cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
3123cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
3133cad027eSdjm 	 ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
3143cad027eSdjm 	  (u_int64_t)(((const u_char *)(p))[7]))
31515b55daeSdjm #define PEEK_U32(p) \
3163cad027eSdjm 	(((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
3173cad027eSdjm 	 ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
3183cad027eSdjm 	 ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
3193cad027eSdjm 	  (u_int32_t)(((const u_char *)(p))[3]))
32015b55daeSdjm #define PEEK_U16(p) \
3213cad027eSdjm 	(((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
3223cad027eSdjm 	  (u_int16_t)(((const u_char *)(p))[1]))
32315b55daeSdjm 
32415b55daeSdjm #define POKE_U64(p, v) \
32515b55daeSdjm 	do { \
3263cad027eSdjm 		const u_int64_t __v = (v); \
3273cad027eSdjm 		((u_char *)(p))[0] = (__v >> 56) & 0xff; \
3283cad027eSdjm 		((u_char *)(p))[1] = (__v >> 48) & 0xff; \
3293cad027eSdjm 		((u_char *)(p))[2] = (__v >> 40) & 0xff; \
3303cad027eSdjm 		((u_char *)(p))[3] = (__v >> 32) & 0xff; \
3313cad027eSdjm 		((u_char *)(p))[4] = (__v >> 24) & 0xff; \
3323cad027eSdjm 		((u_char *)(p))[5] = (__v >> 16) & 0xff; \
3333cad027eSdjm 		((u_char *)(p))[6] = (__v >> 8) & 0xff; \
3343cad027eSdjm 		((u_char *)(p))[7] = __v & 0xff; \
33515b55daeSdjm 	} while (0)
33615b55daeSdjm #define POKE_U32(p, v) \
33715b55daeSdjm 	do { \
3383cad027eSdjm 		const u_int32_t __v = (v); \
3393cad027eSdjm 		((u_char *)(p))[0] = (__v >> 24) & 0xff; \
3403cad027eSdjm 		((u_char *)(p))[1] = (__v >> 16) & 0xff; \
3413cad027eSdjm 		((u_char *)(p))[2] = (__v >> 8) & 0xff; \
3423cad027eSdjm 		((u_char *)(p))[3] = __v & 0xff; \
34315b55daeSdjm 	} while (0)
34415b55daeSdjm #define POKE_U16(p, v) \
34515b55daeSdjm 	do { \
3463cad027eSdjm 		const u_int16_t __v = (v); \
3473cad027eSdjm 		((u_char *)(p))[0] = (__v >> 8) & 0xff; \
3483cad027eSdjm 		((u_char *)(p))[1] = __v & 0xff; \
34915b55daeSdjm 	} while (0)
35015b55daeSdjm 
35115b55daeSdjm /* Internal definitions follow. Exposed for regress tests */
35215b55daeSdjm #ifdef SSHBUF_INTERNAL
35315b55daeSdjm 
35415b55daeSdjm /*
35515b55daeSdjm  * Return the allocation size of buf
35615b55daeSdjm  */
35715b55daeSdjm size_t	sshbuf_alloc(const struct sshbuf *buf);
35815b55daeSdjm 
35915b55daeSdjm /*
36015b55daeSdjm  * Increment the reference count of buf.
36115b55daeSdjm  */
36215b55daeSdjm int	sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
36315b55daeSdjm 
36415b55daeSdjm /*
36515b55daeSdjm  * Return the parent buffer of buf, or NULL if it has no parent.
36615b55daeSdjm  */
36715b55daeSdjm const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
36815b55daeSdjm 
36915b55daeSdjm /*
37015b55daeSdjm  * Return the reference count of buf
37115b55daeSdjm  */
37215b55daeSdjm u_int	sshbuf_refcount(const struct sshbuf *buf);
37315b55daeSdjm 
37415b55daeSdjm # define SSHBUF_SIZE_INIT	256		/* Initial allocation */
37515b55daeSdjm # define SSHBUF_SIZE_INC	256		/* Preferred increment length */
3762475e070Sjsg # define SSHBUF_PACK_MIN	8192		/* Minimum packable offset */
37715b55daeSdjm 
37815b55daeSdjm /* # define SSHBUF_ABORT abort */
37915b55daeSdjm /* # define SSHBUF_DEBUG */
38015b55daeSdjm 
38115b55daeSdjm # ifndef SSHBUF_ABORT
38215b55daeSdjm #  define SSHBUF_ABORT()
38315b55daeSdjm # endif
38415b55daeSdjm 
38515b55daeSdjm # ifdef SSHBUF_DEBUG
386a8cbed27Sdjm #  define SSHBUF_DBG(x) do { \
387a8cbed27Sdjm 		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
388a8cbed27Sdjm 		printf x; \
389a8cbed27Sdjm 		printf("\n"); \
390a8cbed27Sdjm 		fflush(stdout); \
39115b55daeSdjm 	} while (0)
39215b55daeSdjm # else
39315b55daeSdjm #  define SSHBUF_DBG(x)
39415b55daeSdjm # endif
39515b55daeSdjm #endif /* SSHBUF_INTERNAL */
39615b55daeSdjm 
39715b55daeSdjm #endif /* _SSHBUF_H */
398