GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/../bufaux.c Lines: 0 79 0.0 %
Date: 2017-11-07 Branches: 0 48 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: bufaux.c,v 1.60 2014/04/30 05:29:56 djm Exp $ */
2
/*
3
 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
19
20
#include <sys/types.h>
21
22
#include "buffer.h"
23
#include "log.h"
24
#include "ssherr.h"
25
26
int
27
buffer_get_short_ret(u_short *v, Buffer *buffer)
28
{
29
	int ret;
30
31
	if ((ret = sshbuf_get_u16(buffer, v)) != 0) {
32
		error("%s: %s", __func__, ssh_err(ret));
33
		return -1;
34
	}
35
	return 0;
36
}
37
38
u_short
39
buffer_get_short(Buffer *buffer)
40
{
41
	u_short ret;
42
43
	if (buffer_get_short_ret(&ret, buffer) == -1)
44
		fatal("%s: buffer error", __func__);
45
46
	return (ret);
47
}
48
49
int
50
buffer_get_int_ret(u_int *v, Buffer *buffer)
51
{
52
	int ret;
53
54
	if ((ret = sshbuf_get_u32(buffer, v)) != 0) {
55
		error("%s: %s", __func__, ssh_err(ret));
56
		return -1;
57
	}
58
	return 0;
59
}
60
61
u_int
62
buffer_get_int(Buffer *buffer)
63
{
64
	u_int ret;
65
66
	if (buffer_get_int_ret(&ret, buffer) == -1)
67
		fatal("%s: buffer error", __func__);
68
69
	return (ret);
70
}
71
72
int
73
buffer_get_int64_ret(u_int64_t *v, Buffer *buffer)
74
{
75
	int ret;
76
77
	if ((ret = sshbuf_get_u64(buffer, v)) != 0) {
78
		error("%s: %s", __func__, ssh_err(ret));
79
		return -1;
80
	}
81
	return 0;
82
}
83
84
u_int64_t
85
buffer_get_int64(Buffer *buffer)
86
{
87
	u_int64_t ret;
88
89
	if (buffer_get_int64_ret(&ret, buffer) == -1)
90
		fatal("%s: buffer error", __func__);
91
92
	return (ret);
93
}
94
95
void
96
buffer_put_short(Buffer *buffer, u_short value)
97
{
98
	int ret;
99
100
	if ((ret = sshbuf_put_u16(buffer, value)) != 0)
101
		fatal("%s: %s", __func__, ssh_err(ret));
102
}
103
104
void
105
buffer_put_int(Buffer *buffer, u_int value)
106
{
107
	int ret;
108
109
	if ((ret = sshbuf_put_u32(buffer, value)) != 0)
110
		fatal("%s: %s", __func__, ssh_err(ret));
111
}
112
113
void
114
buffer_put_int64(Buffer *buffer, u_int64_t value)
115
{
116
	int ret;
117
118
	if ((ret = sshbuf_put_u64(buffer, value)) != 0)
119
		fatal("%s: %s", __func__, ssh_err(ret));
120
}
121
122
void *
123
buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
124
{
125
	size_t len;
126
	int ret;
127
	u_char *value;
128
129
	if ((ret = sshbuf_get_string(buffer, &value, &len)) != 0) {
130
		error("%s: %s", __func__, ssh_err(ret));
131
		return NULL;
132
	}
133
	if (length_ptr != NULL)
134
		*length_ptr = len;  /* Safe: sshbuf never stores len > 2^31 */
135
	return value;
136
}
137
138
void *
139
buffer_get_string(Buffer *buffer, u_int *length_ptr)
140
{
141
	void *ret;
142
143
	if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
144
		fatal("%s: buffer error", __func__);
145
	return (ret);
146
}
147
148
char *
149
buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
150
{
151
	size_t len;
152
	int ret;
153
	char *value;
154
155
	if ((ret = sshbuf_get_cstring(buffer, &value, &len)) != 0) {
156
		error("%s: %s", __func__, ssh_err(ret));
157
		return NULL;
158
	}
159
	if (length_ptr != NULL)
160
		*length_ptr = len;  /* Safe: sshbuf never stores len > 2^31 */
161
	return value;
162
}
163
164
char *
165
buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
166
{
167
	char *ret;
168
169
	if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
170
		fatal("%s: buffer error", __func__);
171
	return ret;
172
}
173
174
const void *
175
buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
176
{
177
	size_t len;
178
	int ret;
179
	const u_char *value;
180
181
	if ((ret = sshbuf_get_string_direct(buffer, &value, &len)) != 0) {
182
		error("%s: %s", __func__, ssh_err(ret));
183
		return NULL;
184
	}
185
	if (length_ptr != NULL)
186
		*length_ptr = len;  /* Safe: sshbuf never stores len > 2^31 */
187
	return value;
188
}
189
190
const void *
191
buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
192
{
193
	const void *ret;
194
195
	if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
196
		fatal("%s: buffer error", __func__);
197
	return (ret);
198
}
199
200
void
201
buffer_put_string(Buffer *buffer, const void *buf, u_int len)
202
{
203
	int ret;
204
205
	if ((ret = sshbuf_put_string(buffer, buf, len)) != 0)
206
		fatal("%s: %s", __func__, ssh_err(ret));
207
}
208
209
void
210
buffer_put_cstring(Buffer *buffer, const char *s)
211
{
212
	int ret;
213
214
	if ((ret = sshbuf_put_cstring(buffer, s)) != 0)
215
		fatal("%s: %s", __func__, ssh_err(ret));
216
}
217
218
int
219
buffer_get_char_ret(char *v, Buffer *buffer)
220
{
221
	int ret;
222
223
	if ((ret = sshbuf_get_u8(buffer, (u_char *)v)) != 0) {
224
		error("%s: %s", __func__, ssh_err(ret));
225
		return -1;
226
	}
227
	return 0;
228
}
229
230
int
231
buffer_get_char(Buffer *buffer)
232
{
233
	char ch;
234
235
	if (buffer_get_char_ret(&ch, buffer) == -1)
236
		fatal("%s: buffer error", __func__);
237
	return (u_char) ch;
238
}
239
240
void
241
buffer_put_char(Buffer *buffer, int value)
242
{
243
	int ret;
244
245
	if ((ret = sshbuf_put_u8(buffer, value)) != 0)
246
		fatal("%s: %s", __func__, ssh_err(ret));
247
}
248
249
void
250
buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
251
{
252
	int ret;
253
254
	if ((ret = sshbuf_put_bignum2_bytes(buffer, s, l)) != 0)
255
		fatal("%s: %s", __func__, ssh_err(ret));
256
}
257