summaryrefslogtreecommitdiff
path: root/src/protocol/internal/buffer.c
blob: dc53781bd466676b4fa23f7bc3453cd8dcf47ef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * buffer.c: SSH buffer
 * Copyright (C) 2018  Iru Cai <mytbk920423@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "buffer.h"
#include "ssh_endian.h"

#define INITSIZE 2048

int buffer_init(buffer *b)
{
	b->p = (uint8_t *)malloc(INITSIZE);
	if (b->p) {
		b->alloc = INITSIZE;
		b->offs = 0;
		b->sz = 0;
		return 1;
	} else {
		return 0;
	}
}

static int ensure(buffer *b, size_t len)
{
	if (b->offs + b->sz + len <= b->alloc)
		return 1;

	uint8_t *r = (uint8_t *)realloc(b->p, (b->alloc + len) * 2);
	if (r == NULL)
		return 0;
	b->alloc = (b->alloc + len) * 2;
	b->p = r;
	return 1;
}

int buffer_append(buffer *b, const uint8_t *s, size_t len)
{
	if (ensure(b, len)) {
		if (s != NULL)
			memcpy(b->p + b->offs + b->sz, s, len);
		b->sz += len;
		return 1;
	} else {
		return 0;
	}
}

int buffer_append_string(buffer *b, const char *s, size_t len)
{
	uint32_t beint = htobe32(len);
	if (ensure(b, len + 4)) {
		*(uint32_t *)(b->p + b->offs + b->sz) = beint;
		memcpy(b->p + b->offs + b->sz + 4, s, len);
		b->sz += len + 4;
		return 1;
	} else {
		return 0;
	}
}

int buffer_append_byte(buffer *b, uint8_t x)
{
	if (ensure(b, 1)) {
		b->p[b->offs + b->sz] = x;
		b->sz += 1;
	} else {
		return 0;
	}
}

int buffer_append_be16(buffer *b, uint16_t x)
{
	uint16_t beint = htobe16(x);
	if (ensure(b, 2)) {
		*(uint16_t *)(b->p + b->offs + b->sz) = beint;
		b->sz += 2;
	} else {
		return 0;
	}
}

int buffer_append_be32(buffer *b, uint32_t x)
{
	uint32_t beint = htobe32(x);
	if (ensure(b, 4)) {
		*(uint32_t *)(b->p + b->offs + b->sz) = beint;
		b->sz += 4;
	} else {
		return 0;
	}
}

void buffer_get(buffer *b, uint8_t *s, size_t len)
{
	memcpy(s, buffer_data(b), len);
	buffer_consume(b, len);
}

uint8_t buffer_get_u8(buffer *b)
{
	uint8_t c = *buffer_data(b);
	buffer_consume(b, 1);
	return c;
}

uint16_t buffer_get_u16(buffer *b)
{
	uint16_t u = be16toh(*(uint16_t *)buffer_data(b));
	buffer_consume(b, 2);
	return u;
}

uint32_t buffer_get_u32(buffer *b)
{
	uint32_t u = be32toh(*(uint32_t *)buffer_data(b));
	buffer_consume(b, 4);
	return u;
}