1 /* $OpenBSD: bufaux.c,v 1.60 2014/04/30 05:29:56 djm Exp $ */
3 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
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.
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.
18 /* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
22 #include <sys/types.h>
29 buffer_get_short_ret(u_short
*v
, Buffer
*buffer
)
33 if ((ret
= sshbuf_get_u16(buffer
, v
)) != 0) {
34 error("%s: %s", __func__
, ssh_err(ret
));
41 buffer_get_short(Buffer
*buffer
)
45 if (buffer_get_short_ret(&ret
, buffer
) == -1)
46 fatal("%s: buffer error", __func__
);
52 buffer_get_int_ret(u_int
*v
, Buffer
*buffer
)
56 if ((ret
= sshbuf_get_u32(buffer
, v
)) != 0) {
57 error("%s: %s", __func__
, ssh_err(ret
));
64 buffer_get_int(Buffer
*buffer
)
68 if (buffer_get_int_ret(&ret
, buffer
) == -1)
69 fatal("%s: buffer error", __func__
);
75 buffer_get_int64_ret(u_int64_t
*v
, Buffer
*buffer
)
79 if ((ret
= sshbuf_get_u64(buffer
, v
)) != 0) {
80 error("%s: %s", __func__
, ssh_err(ret
));
87 buffer_get_int64(Buffer
*buffer
)
91 if (buffer_get_int64_ret(&ret
, buffer
) == -1)
92 fatal("%s: buffer error", __func__
);
98 buffer_put_short(Buffer
*buffer
, u_short value
)
102 if ((ret
= sshbuf_put_u16(buffer
, value
)) != 0)
103 fatal("%s: %s", __func__
, ssh_err(ret
));
107 buffer_put_int(Buffer
*buffer
, u_int value
)
111 if ((ret
= sshbuf_put_u32(buffer
, value
)) != 0)
112 fatal("%s: %s", __func__
, ssh_err(ret
));
116 buffer_put_int64(Buffer
*buffer
, u_int64_t value
)
120 if ((ret
= sshbuf_put_u64(buffer
, value
)) != 0)
121 fatal("%s: %s", __func__
, ssh_err(ret
));
125 buffer_get_string_ret(Buffer
*buffer
, u_int
*length_ptr
)
131 if ((ret
= sshbuf_get_string(buffer
, &value
, &len
)) != 0) {
132 error("%s: %s", __func__
, ssh_err(ret
));
135 if (length_ptr
!= NULL
)
136 *length_ptr
= len
; /* Safe: sshbuf never stores len > 2^31 */
141 buffer_get_string(Buffer
*buffer
, u_int
*length_ptr
)
145 if ((ret
= buffer_get_string_ret(buffer
, length_ptr
)) == NULL
)
146 fatal("%s: buffer error", __func__
);
151 buffer_get_cstring_ret(Buffer
*buffer
, u_int
*length_ptr
)
157 if ((ret
= sshbuf_get_cstring(buffer
, &value
, &len
)) != 0) {
158 error("%s: %s", __func__
, ssh_err(ret
));
161 if (length_ptr
!= NULL
)
162 *length_ptr
= len
; /* Safe: sshbuf never stores len > 2^31 */
167 buffer_get_cstring(Buffer
*buffer
, u_int
*length_ptr
)
171 if ((ret
= buffer_get_cstring_ret(buffer
, length_ptr
)) == NULL
)
172 fatal("%s: buffer error", __func__
);
177 buffer_get_string_ptr_ret(Buffer
*buffer
, u_int
*length_ptr
)
183 if ((ret
= sshbuf_get_string_direct(buffer
, &value
, &len
)) != 0) {
184 error("%s: %s", __func__
, ssh_err(ret
));
187 if (length_ptr
!= NULL
)
188 *length_ptr
= len
; /* Safe: sshbuf never stores len > 2^31 */
193 buffer_get_string_ptr(Buffer
*buffer
, u_int
*length_ptr
)
197 if ((ret
= buffer_get_string_ptr_ret(buffer
, length_ptr
)) == NULL
)
198 fatal("%s: buffer error", __func__
);
203 buffer_put_string(Buffer
*buffer
, const void *buf
, u_int len
)
207 if ((ret
= sshbuf_put_string(buffer
, buf
, len
)) != 0)
208 fatal("%s: %s", __func__
, ssh_err(ret
));
212 buffer_put_cstring(Buffer
*buffer
, const char *s
)
216 if ((ret
= sshbuf_put_cstring(buffer
, s
)) != 0)
217 fatal("%s: %s", __func__
, ssh_err(ret
));
221 buffer_get_char_ret(char *v
, Buffer
*buffer
)
225 if ((ret
= sshbuf_get_u8(buffer
, (u_char
*)v
)) != 0) {
226 error("%s: %s", __func__
, ssh_err(ret
));
233 buffer_get_char(Buffer
*buffer
)
237 if (buffer_get_char_ret(&ch
, buffer
) == -1)
238 fatal("%s: buffer error", __func__
);
243 buffer_put_char(Buffer
*buffer
, int value
)
247 if ((ret
= sshbuf_put_u8(buffer
, value
)) != 0)
248 fatal("%s: %s", __func__
, ssh_err(ret
));
252 buffer_put_bignum2_from_string(Buffer
*buffer
, const u_char
*s
, u_int l
)
256 if ((ret
= sshbuf_put_bignum2_bytes(buffer
, s
, l
)) != 0)
257 fatal("%s: %s", __func__
, ssh_err(ret
));