sysconf.3: Fix typo.
[dragonfly.git] / crypto / openssh / bufaux.c
blob3976896a99e1cd0cbc73d5bb22749ade971e0069
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>
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 */
20 #include "includes.h"
22 #include <sys/types.h>
24 #include "buffer.h"
25 #include "log.h"
26 #include "ssherr.h"
28 int
29 buffer_get_short_ret(u_short *v, Buffer *buffer)
31 int ret;
33 if ((ret = sshbuf_get_u16(buffer, v)) != 0) {
34 error("%s: %s", __func__, ssh_err(ret));
35 return -1;
37 return 0;
40 u_short
41 buffer_get_short(Buffer *buffer)
43 u_short ret;
45 if (buffer_get_short_ret(&ret, buffer) == -1)
46 fatal("%s: buffer error", __func__);
48 return (ret);
51 int
52 buffer_get_int_ret(u_int *v, Buffer *buffer)
54 int ret;
56 if ((ret = sshbuf_get_u32(buffer, v)) != 0) {
57 error("%s: %s", __func__, ssh_err(ret));
58 return -1;
60 return 0;
63 u_int
64 buffer_get_int(Buffer *buffer)
66 u_int ret;
68 if (buffer_get_int_ret(&ret, buffer) == -1)
69 fatal("%s: buffer error", __func__);
71 return (ret);
74 int
75 buffer_get_int64_ret(u_int64_t *v, Buffer *buffer)
77 int ret;
79 if ((ret = sshbuf_get_u64(buffer, v)) != 0) {
80 error("%s: %s", __func__, ssh_err(ret));
81 return -1;
83 return 0;
86 u_int64_t
87 buffer_get_int64(Buffer *buffer)
89 u_int64_t ret;
91 if (buffer_get_int64_ret(&ret, buffer) == -1)
92 fatal("%s: buffer error", __func__);
94 return (ret);
97 void
98 buffer_put_short(Buffer *buffer, u_short value)
100 int ret;
102 if ((ret = sshbuf_put_u16(buffer, value)) != 0)
103 fatal("%s: %s", __func__, ssh_err(ret));
106 void
107 buffer_put_int(Buffer *buffer, u_int value)
109 int ret;
111 if ((ret = sshbuf_put_u32(buffer, value)) != 0)
112 fatal("%s: %s", __func__, ssh_err(ret));
115 void
116 buffer_put_int64(Buffer *buffer, u_int64_t value)
118 int ret;
120 if ((ret = sshbuf_put_u64(buffer, value)) != 0)
121 fatal("%s: %s", __func__, ssh_err(ret));
124 void *
125 buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
127 size_t len;
128 int ret;
129 u_char *value;
131 if ((ret = sshbuf_get_string(buffer, &value, &len)) != 0) {
132 error("%s: %s", __func__, ssh_err(ret));
133 return NULL;
135 if (length_ptr != NULL)
136 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
137 return value;
140 void *
141 buffer_get_string(Buffer *buffer, u_int *length_ptr)
143 void *ret;
145 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
146 fatal("%s: buffer error", __func__);
147 return (ret);
150 char *
151 buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
153 size_t len;
154 int ret;
155 char *value;
157 if ((ret = sshbuf_get_cstring(buffer, &value, &len)) != 0) {
158 error("%s: %s", __func__, ssh_err(ret));
159 return NULL;
161 if (length_ptr != NULL)
162 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
163 return value;
166 char *
167 buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
169 char *ret;
171 if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
172 fatal("%s: buffer error", __func__);
173 return ret;
176 const void *
177 buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
179 size_t len;
180 int ret;
181 const u_char *value;
183 if ((ret = sshbuf_get_string_direct(buffer, &value, &len)) != 0) {
184 error("%s: %s", __func__, ssh_err(ret));
185 return NULL;
187 if (length_ptr != NULL)
188 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
189 return value;
192 const void *
193 buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
195 const void *ret;
197 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
198 fatal("%s: buffer error", __func__);
199 return (ret);
202 void
203 buffer_put_string(Buffer *buffer, const void *buf, u_int len)
205 int ret;
207 if ((ret = sshbuf_put_string(buffer, buf, len)) != 0)
208 fatal("%s: %s", __func__, ssh_err(ret));
211 void
212 buffer_put_cstring(Buffer *buffer, const char *s)
214 int ret;
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)
223 int ret;
225 if ((ret = sshbuf_get_u8(buffer, (u_char *)v)) != 0) {
226 error("%s: %s", __func__, ssh_err(ret));
227 return -1;
229 return 0;
233 buffer_get_char(Buffer *buffer)
235 char ch;
237 if (buffer_get_char_ret(&ch, buffer) == -1)
238 fatal("%s: buffer error", __func__);
239 return (u_char) ch;
242 void
243 buffer_put_char(Buffer *buffer, int value)
245 int ret;
247 if ((ret = sshbuf_put_u8(buffer, value)) != 0)
248 fatal("%s: %s", __func__, ssh_err(ret));
251 void
252 buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
254 int ret;
256 if ((ret = sshbuf_put_bignum2_bytes(buffer, s, l)) != 0)
257 fatal("%s: %s", __func__, ssh_err(ret));