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