2 /* $OpenBSD: bufaux.c,v 1.46 2008/06/10 23:21:34 dtucker Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Auxiliary functions for storing and retrieving various data types to/from
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
17 * SSH2 packet format added by Markus Friedl
18 * Copyright (c) 2000 Markus Friedl. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 __RCSID("$NetBSD: bufaux.c,v 1.10 2009/02/16 20:53:54 christos Exp $");
43 #include <sys/types.h>
45 #include <openssl/bn.h>
57 * Returns integers from the buffer (msb first).
61 buffer_get_short_ret(u_short
*ret
, Buffer
*buffer
)
65 if (buffer_get_ret(buffer
, (char *) buf
, 2) == -1)
72 buffer_get_short(Buffer
*buffer
)
74 u_short ret
= 0; /* XXX: GCC */
76 if (buffer_get_short_ret(&ret
, buffer
) == -1)
77 fatal("buffer_get_short: buffer error");
83 buffer_get_int_ret(u_int
*ret
, Buffer
*buffer
)
87 if (buffer_get_ret(buffer
, (char *) buf
, 4) == -1)
94 buffer_get_int(Buffer
*buffer
)
96 u_int ret
= 0; /* XXX: GCC */
98 if (buffer_get_int_ret(&ret
, buffer
) == -1)
99 fatal("buffer_get_int: buffer error");
105 buffer_get_int64_ret(u_int64_t
*ret
, Buffer
*buffer
)
109 if (buffer_get_ret(buffer
, (char *) buf
, 8) == -1)
116 buffer_get_int64(Buffer
*buffer
)
118 u_int64_t ret
= 0; /* XXX: GCC */
120 if (buffer_get_int64_ret(&ret
, buffer
) == -1)
121 fatal("buffer_get_int: buffer error");
127 * Stores integers in the buffer, msb first.
130 buffer_put_short(Buffer
*buffer
, u_short value
)
135 buffer_append(buffer
, buf
, 2);
139 buffer_put_int(Buffer
*buffer
, u_int value
)
144 buffer_append(buffer
, buf
, 4);
148 buffer_put_int64(Buffer
*buffer
, u_int64_t value
)
153 buffer_append(buffer
, buf
, 8);
157 * Returns an arbitrary binary string from the buffer. The string cannot
158 * be longer than 256k. The returned value points to memory allocated
159 * with xmalloc; it is the responsibility of the calling function to free
160 * the data. If length_ptr is non-NULL, the length of the returned data
161 * will be stored there. A null character will be automatically appended
162 * to the returned string, and is not counted in length.
165 buffer_get_string_ret(Buffer
*buffer
, u_int
*length_ptr
)
170 /* Get the length. */
171 len
= buffer_get_int(buffer
);
172 if (len
> 256 * 1024) {
173 error("buffer_get_string_ret: bad string length %u", len
);
176 /* Allocate space for the string. Add one byte for a null character. */
177 value
= xmalloc(len
+ 1);
178 /* Get the string. */
179 if (buffer_get_ret(buffer
, value
, len
) == -1) {
180 error("buffer_get_string_ret: buffer_get failed");
184 /* Append a null character to make processing easier. */
186 /* Optionally return the length of the string. */
193 buffer_get_string(Buffer
*buffer
, u_int
*length_ptr
)
197 if ((ret
= buffer_get_string_ret(buffer
, length_ptr
)) == NULL
)
198 fatal("buffer_get_string: buffer error");
203 buffer_get_string_ptr(Buffer
*buffer
, u_int
*length_ptr
)
208 len
= buffer_get_int(buffer
);
209 if (len
> 256 * 1024)
210 fatal("buffer_get_string_ptr: bad string length %u", len
);
211 ptr
= buffer_ptr(buffer
);
212 buffer_consume(buffer
, len
);
219 * Stores and arbitrary binary string in the buffer.
222 buffer_put_string(Buffer
*buffer
, const void *buf
, u_int len
)
224 buffer_put_int(buffer
, len
);
225 buffer_append(buffer
, buf
, len
);
228 buffer_put_cstring(Buffer
*buffer
, const char *s
)
231 fatal("buffer_put_cstring: s == NULL");
232 buffer_put_string(buffer
, s
, strlen(s
));
236 * Returns a character from the buffer (0 - 255).
239 buffer_get_char_ret(char *ret
, Buffer
*buffer
)
241 if (buffer_get_ret(buffer
, ret
, 1) == -1) {
242 error("buffer_get_char_ret: buffer_get_ret failed");
249 buffer_get_char(Buffer
*buffer
)
253 if (buffer_get_char_ret(&ch
, buffer
) == -1)
254 fatal("buffer_get_char: buffer error");
259 * Stores a character in the buffer.
262 buffer_put_char(Buffer
*buffer
, int value
)
266 buffer_append(buffer
, &ch
, 1);