blob: 7df8a38fa18d22cb94120c421648e22f9e184e7f [file] [log] [blame]
Damien Miller91b580e2014-01-12 19:21:22 +11001/* $OpenBSD: buffer.h,v 1.23 2014/01/12 08:13:13 djm Exp $ */
Ben Lindstrom05764b92002-03-05 01:53:02 +00002
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11005 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11007 * Code for manipulating FIFO buffers.
Damien Miller5f056372000-04-16 12:31:48 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016#ifndef BUFFER_H
17#define BUFFER_H
18
Damien Miller95def091999-11-25 00:26:21 +110019typedef struct {
Damien Miller5a6b4fe2001-12-21 14:56:54 +110020 u_char *buf; /* Buffer for data. */
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000021 u_int alloc; /* Number of bytes allocated for data. */
22 u_int offset; /* Offset of first byte containing data. */
23 u_int end; /* Offset of last byte containing data. */
Damien Miller95def091999-11-25 00:26:21 +110024} Buffer;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000026void buffer_init(Buffer *);
27void buffer_clear(Buffer *);
28void buffer_free(Buffer *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
Damien Miller89221062010-02-12 09:23:40 +110030u_int buffer_len(const Buffer *);
31void *buffer_ptr(const Buffer *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
Damien Miller5a6b4fe2001-12-21 14:56:54 +110033void buffer_append(Buffer *, const void *, u_int);
34void *buffer_append_space(Buffer *, u_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
Damien Miller499a0d52006-04-23 12:06:03 +100036int buffer_check_alloc(Buffer *, u_int);
37
Damien Miller5a6b4fe2001-12-21 14:56:54 +110038void buffer_get(Buffer *, void *, u_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000040void buffer_consume(Buffer *, u_int);
41void buffer_consume_end(Buffer *, u_int);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Damien Miller89221062010-02-12 09:23:40 +110043void buffer_dump(const Buffer *);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Darren Tucker50dbe832004-11-05 20:41:24 +110045int buffer_get_ret(Buffer *, void *, u_int);
46int buffer_consume_ret(Buffer *, u_int);
47int buffer_consume_end_ret(Buffer *, u_int);
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include <openssl/bn.h>
50
51void buffer_put_bignum(Buffer *, const BIGNUM *);
52void buffer_put_bignum2(Buffer *, const BIGNUM *);
53void buffer_get_bignum(Buffer *, BIGNUM *);
54void buffer_get_bignum2(Buffer *, BIGNUM *);
55
56u_short buffer_get_short(Buffer *);
57void buffer_put_short(Buffer *, u_short);
58
59u_int buffer_get_int(Buffer *);
60void buffer_put_int(Buffer *, u_int);
61
62u_int64_t buffer_get_int64(Buffer *);
63void buffer_put_int64(Buffer *, u_int64_t);
64
65int buffer_get_char(Buffer *);
66void buffer_put_char(Buffer *, int);
67
68void *buffer_get_string(Buffer *, u_int *);
Damien Millerdb255ca2008-05-19 14:59:37 +100069void *buffer_get_string_ptr(Buffer *, u_int *);
Damien Millerd7834352006-08-05 12:39:39 +100070void buffer_put_string(Buffer *, const void *, u_int);
Damien Millerda108ec2010-08-31 22:36:39 +100071char *buffer_get_cstring(Buffer *, u_int *);
Damien Millerd7834352006-08-05 12:39:39 +100072void buffer_put_cstring(Buffer *, const char *);
73
74#define buffer_skip_string(b) \
75 do { u_int l = buffer_get_int(b); buffer_consume(b, l); } while (0)
76
77int buffer_put_bignum_ret(Buffer *, const BIGNUM *);
78int buffer_get_bignum_ret(Buffer *, BIGNUM *);
79int buffer_put_bignum2_ret(Buffer *, const BIGNUM *);
80int buffer_get_bignum2_ret(Buffer *, BIGNUM *);
81int buffer_get_short_ret(u_short *, Buffer *);
82int buffer_get_int_ret(u_int *, Buffer *);
83int buffer_get_int64_ret(u_int64_t *, Buffer *);
84void *buffer_get_string_ret(Buffer *, u_int *);
Damien Millerda108ec2010-08-31 22:36:39 +100085char *buffer_get_cstring_ret(Buffer *, u_int *);
Darren Tuckerebc71d92010-01-12 19:45:59 +110086void *buffer_get_string_ptr_ret(Buffer *, u_int *);
Damien Millerce986542013-07-18 16:12:44 +100087int buffer_get_char_ret(u_char *, Buffer *);
Damien Millerd7834352006-08-05 12:39:39 +100088
Damien Miller91b580e2014-01-12 19:21:22 +110089void *buffer_get_bignum2_as_string_ret(Buffer *, u_int *);
90void *buffer_get_bignum2_as_string(Buffer *, u_int *);
91void buffer_put_bignum2_from_string(Buffer *, const u_char *, u_int);
92
Damien Miller6af914a2010-09-10 11:39:26 +100093#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +100094#include <openssl/ec.h>
95
96int buffer_put_ecpoint_ret(Buffer *, const EC_GROUP *, const EC_POINT *);
97void buffer_put_ecpoint(Buffer *, const EC_GROUP *, const EC_POINT *);
98int buffer_get_ecpoint_ret(Buffer *, const EC_GROUP *, EC_POINT *);
99void buffer_get_ecpoint(Buffer *, const EC_GROUP *, EC_POINT *);
Damien Miller6af914a2010-09-10 11:39:26 +1000100#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102#endif /* BUFFER_H */