1 /* $OpenBSD: bufaux.c,v 1.42 2006/04/18 10:44:28 dtucker Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Auxiliary functions for storing and retrieving various data types to/from
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
16 * SSH2 packet format added by Markus Friedl
17 * Copyright (c) 2000 Markus Friedl. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <openssl/bn.h>
49 * Returns integers from the buffer (msb first).
53 buffer_get_short_ret(u_short *ret, Buffer *buffer)
57 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
64 buffer_get_short(Buffer *buffer)
68 if (buffer_get_short_ret(&ret, buffer) == -1)
69 fatal("buffer_get_short: buffer error");
75 buffer_get_int_ret(u_int *ret, Buffer *buffer)
79 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
86 buffer_get_int(Buffer *buffer)
90 if (buffer_get_int_ret(&ret, buffer) == -1)
91 fatal("buffer_get_int: buffer error");
97 buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
101 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
108 buffer_get_int64(Buffer *buffer)
112 if (buffer_get_int64_ret(&ret, buffer) == -1)
113 fatal("buffer_get_int: buffer error");
119 * Stores integers in the buffer, msb first.
122 buffer_put_short(Buffer *buffer, u_short value)
127 buffer_append(buffer, buf, 2);
131 buffer_put_int(Buffer *buffer, u_int value)
136 buffer_append(buffer, buf, 4);
140 buffer_put_int64(Buffer *buffer, u_int64_t value)
145 buffer_append(buffer, buf, 8);
149 * Returns an arbitrary binary string from the buffer. The string cannot
150 * be longer than 256k. The returned value points to memory allocated
151 * with xmalloc; it is the responsibility of the calling function to free
152 * the data. If length_ptr is non-NULL, the length of the returned data
153 * will be stored there. A null character will be automatically appended
154 * to the returned string, and is not counted in length.
157 buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
162 /* Get the length. */
163 len = buffer_get_int(buffer);
164 if (len > 256 * 1024) {
165 error("buffer_get_string_ret: bad string length %u", len);
168 /* Allocate space for the string. Add one byte for a null character. */
169 value = xmalloc(len + 1);
170 /* Get the string. */
171 if (buffer_get_ret(buffer, value, len) == -1) {
172 error("buffer_get_string_ret: buffer_get failed");
176 /* Append a null character to make processing easier. */
178 /* Optionally return the length of the string. */
185 buffer_get_string(Buffer *buffer, u_int *length_ptr)
189 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
190 fatal("buffer_get_string: buffer error");
195 * Stores and arbitrary binary string in the buffer.
198 buffer_put_string(Buffer *buffer, const void *buf, u_int len)
200 buffer_put_int(buffer, len);
201 buffer_append(buffer, buf, len);
204 buffer_put_cstring(Buffer *buffer, const char *s)
207 fatal("buffer_put_cstring: s == NULL");
208 buffer_put_string(buffer, s, strlen(s));
212 * Returns a character from the buffer (0 - 255).
215 buffer_get_char_ret(char *ret, Buffer *buffer)
217 if (buffer_get_ret(buffer, ret, 1) == -1) {
218 error("buffer_get_char_ret: buffer_get_ret failed");
225 buffer_get_char(Buffer *buffer)
229 if (buffer_get_char_ret(&ch, buffer) == -1)
230 fatal("buffer_get_char: buffer error");
235 * Stores a character in the buffer.
238 buffer_put_char(Buffer *buffer, int value)
242 buffer_append(buffer, &ch, 1);