ssh has moved
[netbsd-mini2440.git] / crypto / dist / ssh / bufaux.c
blob52ff5a95244747cc4b8998f64633b6a69d3503e1
1 /* $NetBSD: bufaux.c,v 1.9 2006/09/28 21:22:14 christos Exp $ */
2 /* $OpenBSD: bufaux.c,v 1.46 2008/06/10 23:21:34 dtucker Exp $ */
3 /*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 * Auxiliary functions for storing and retrieving various data types to/from
8 * Buffers.
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
22 * are met:
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.
41 #include "includes.h"
42 __RCSID("$NetBSD: bufaux.c,v 1.9 2006/09/28 21:22:14 christos Exp $");
43 #include <sys/types.h>
45 #include <openssl/bn.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <time.h>
51 #include "xmalloc.h"
52 #include "buffer.h"
53 #include "log.h"
54 #include "misc.h"
57 * Returns integers from the buffer (msb first).
60 int
61 buffer_get_short_ret(u_short *ret, Buffer *buffer)
63 u_char buf[2];
65 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
66 return (-1);
67 *ret = get_u16(buf);
68 return (0);
71 u_short
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");
79 return (ret);
82 int
83 buffer_get_int_ret(u_int *ret, Buffer *buffer)
85 u_char buf[4];
87 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
88 return (-1);
89 *ret = get_u32(buf);
90 return (0);
93 u_int
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");
101 return (ret);
105 buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
107 u_char buf[8];
109 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
110 return (-1);
111 *ret = get_u64(buf);
112 return (0);
115 u_int64_t
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");
123 return (ret);
127 * Stores integers in the buffer, msb first.
129 void
130 buffer_put_short(Buffer *buffer, u_short value)
132 char buf[2];
134 put_u16(buf, value);
135 buffer_append(buffer, buf, 2);
138 void
139 buffer_put_int(Buffer *buffer, u_int value)
141 char buf[4];
143 put_u32(buf, value);
144 buffer_append(buffer, buf, 4);
147 void
148 buffer_put_int64(Buffer *buffer, u_int64_t value)
150 char buf[8];
152 put_u64(buf, 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.
164 void *
165 buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
167 u_char *value;
168 u_int len;
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);
174 return (NULL);
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");
181 xfree(value);
182 return (NULL);
184 /* Append a null character to make processing easier. */
185 value[len] = '\0';
186 /* Optionally return the length of the string. */
187 if (length_ptr)
188 *length_ptr = len;
189 return (value);
192 void *
193 buffer_get_string(Buffer *buffer, u_int *length_ptr)
195 void *ret;
197 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
198 fatal("buffer_get_string: buffer error");
199 return (ret);
202 void *
203 buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
205 void *ptr;
206 u_int len;
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);
213 if (length_ptr)
214 *length_ptr = len;
215 return (ptr);
219 * Stores and arbitrary binary string in the buffer.
221 void
222 buffer_put_string(Buffer *buffer, const void *buf, u_int len)
224 buffer_put_int(buffer, len);
225 buffer_append(buffer, buf, len);
227 void
228 buffer_put_cstring(Buffer *buffer, const char *s)
230 if (s == NULL)
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");
243 return (-1);
245 return (0);
249 buffer_get_char(Buffer *buffer)
251 char ch;
253 if (buffer_get_char_ret(&ch, buffer) == -1)
254 fatal("buffer_get_char: buffer error");
255 return (u_char) ch;
259 * Stores a character in the buffer.
261 void
262 buffer_put_char(Buffer *buffer, int value)
264 char ch = value;
266 buffer_append(buffer, &ch, 1);