]> andersk Git - openssh.git/blame - bufaux.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / bufaux.c
CommitLineData
e0cbb24b 1/* $OpenBSD: bufaux.c,v 1.47 2010/01/12 01:36:08 djm Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * Auxiliary functions for storing and retrieving various data types to/from
7 * Buffers.
8 *
bcbf86ec 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".
14 *
15 *
7368a6c8 16 * SSH2 packet format added by Markus Friedl
bcbf86ec 17 * Copyright (c) 2000 Markus Friedl. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
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.
7368a6c8 27 *
bcbf86ec 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.
5260325f 38 */
8efc0c15 39
40#include "includes.h"
8efc0c15 41
31652869 42#include <sys/types.h>
43
8efc0c15 44#include <openssl/bn.h>
00146caa 45
46#include <string.h>
31652869 47#include <stdarg.h>
00146caa 48
8efc0c15 49#include "xmalloc.h"
31652869 50#include "buffer.h"
42f11eb2 51#include "log.h"
51e7a012 52#include "misc.h"
8efc0c15 53
5260325f 54/*
9b26c596 55 * Returns integers from the buffer (msb first).
5260325f 56 */
9b26c596 57
b82a59f2 58int
59buffer_get_short_ret(u_short *ret, Buffer *buffer)
60{
61 u_char buf[2];
62
63 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
64 return (-1);
51e7a012 65 *ret = get_u16(buf);
b82a59f2 66 return (0);
67}
68
9b26c596 69u_short
70buffer_get_short(Buffer *buffer)
71{
b82a59f2 72 u_short ret;
73
74 if (buffer_get_short_ret(&ret, buffer) == -1)
75 fatal("buffer_get_short: buffer error");
7528d467 76
b82a59f2 77 return (ret);
78}
79
80int
81buffer_get_int_ret(u_int *ret, Buffer *buffer)
82{
83 u_char buf[4];
84
85 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
86 return (-1);
51e7a012 87 *ret = get_u32(buf);
b82a59f2 88 return (0);
9b26c596 89}
90
1e3b8b07 91u_int
5260325f 92buffer_get_int(Buffer *buffer)
8efc0c15 93{
b82a59f2 94 u_int ret;
95
96 if (buffer_get_int_ret(&ret, buffer) == -1)
97 fatal("buffer_get_int: buffer error");
98
99 return (ret);
100}
7528d467 101
b82a59f2 102int
103buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
104{
105 u_char buf[8];
106
107 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
108 return (-1);
51e7a012 109 *ret = get_u64(buf);
b82a59f2 110 return (0);
8efc0c15 111}
112
f546c780 113u_int64_t
114buffer_get_int64(Buffer *buffer)
115{
b82a59f2 116 u_int64_t ret;
7528d467 117
b82a59f2 118 if (buffer_get_int64_ret(&ret, buffer) == -1)
119 fatal("buffer_get_int: buffer error");
120
121 return (ret);
f546c780 122}
123
5260325f 124/*
9b26c596 125 * Stores integers in the buffer, msb first.
5260325f 126 */
9b26c596 127void
128buffer_put_short(Buffer *buffer, u_short value)
129{
130 char buf[2];
7528d467 131
51e7a012 132 put_u16(buf, value);
9b26c596 133 buffer_append(buffer, buf, 2);
134}
135
35484284 136void
1e3b8b07 137buffer_put_int(Buffer *buffer, u_int value)
8efc0c15 138{
5260325f 139 char buf[4];
7528d467 140
51e7a012 141 put_u32(buf, value);
5260325f 142 buffer_append(buffer, buf, 4);
8efc0c15 143}
144
f546c780 145void
146buffer_put_int64(Buffer *buffer, u_int64_t value)
147{
148 char buf[8];
7528d467 149
51e7a012 150 put_u64(buf, value);
f546c780 151 buffer_append(buffer, buf, 8);
152}
153
5260325f 154/*
155 * Returns an arbitrary binary string from the buffer. The string cannot
156 * be longer than 256k. The returned value points to memory allocated
157 * with xmalloc; it is the responsibility of the calling function to free
158 * the data. If length_ptr is non-NULL, the length of the returned data
159 * will be stored there. A null character will be automatically appended
160 * to the returned string, and is not counted in length.
161 */
6c0fa2b1 162void *
b82a59f2 163buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
8efc0c15 164{
6c0fa2b1 165 u_char *value;
7528d467 166 u_int len;
167
5260325f 168 /* Get the length. */
169 len = buffer_get_int(buffer);
b82a59f2 170 if (len > 256 * 1024) {
171 error("buffer_get_string_ret: bad string length %u", len);
172 return (NULL);
173 }
5260325f 174 /* Allocate space for the string. Add one byte for a null character. */
175 value = xmalloc(len + 1);
176 /* Get the string. */
b82a59f2 177 if (buffer_get_ret(buffer, value, len) == -1) {
178 error("buffer_get_string_ret: buffer_get failed");
179 xfree(value);
180 return (NULL);
181 }
5260325f 182 /* Append a null character to make processing easier. */
8fb12ef0 183 value[len] = '\0';
5260325f 184 /* Optionally return the length of the string. */
185 if (length_ptr)
186 *length_ptr = len;
b82a59f2 187 return (value);
188}
189
190void *
191buffer_get_string(Buffer *buffer, u_int *length_ptr)
192{
193 void *ret;
194
195 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
196 fatal("buffer_get_string: buffer error");
197 return (ret);
8efc0c15 198}
199
17f02f0a 200void *
e0cbb24b 201buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
17f02f0a 202{
203 void *ptr;
204 u_int len;
205
e0cbb24b 206 if (buffer_get_int_ret(&len, buffer) != 0)
207 return NULL;
208 if (len > 256 * 1024) {
209 error("buffer_get_string_ptr: bad string length %u", len);
210 return NULL;
211 }
17f02f0a 212 ptr = buffer_ptr(buffer);
213 buffer_consume(buffer, len);
214 if (length_ptr)
215 *length_ptr = len;
216 return (ptr);
217}
218
e0cbb24b 219void *
220buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
221{
222 void *ret;
223
224 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
225 fatal("buffer_get_string_ptr: buffer error");
226 return (ret);
227}
228
5260325f 229/*
230 * Stores and arbitrary binary string in the buffer.
231 */
35484284 232void
1e3b8b07 233buffer_put_string(Buffer *buffer, const void *buf, u_int len)
8efc0c15 234{
5260325f 235 buffer_put_int(buffer, len);
236 buffer_append(buffer, buf, len);
8efc0c15 237}
35484284 238void
7368a6c8 239buffer_put_cstring(Buffer *buffer, const char *s)
240{
b625ad75 241 if (s == NULL)
242 fatal("buffer_put_cstring: s == NULL");
7368a6c8 243 buffer_put_string(buffer, s, strlen(s));
244}
8efc0c15 245
5260325f 246/*
247 * Returns a character from the buffer (0 - 255).
248 */
b82a59f2 249int
250buffer_get_char_ret(char *ret, Buffer *buffer)
251{
252 if (buffer_get_ret(buffer, ret, 1) == -1) {
253 error("buffer_get_char_ret: buffer_get_ret failed");
254 return (-1);
255 }
256 return (0);
257}
258
35484284 259int
5260325f 260buffer_get_char(Buffer *buffer)
8efc0c15 261{
5260325f 262 char ch;
7528d467 263
b82a59f2 264 if (buffer_get_char_ret(&ch, buffer) == -1)
265 fatal("buffer_get_char: buffer error");
1e3b8b07 266 return (u_char) ch;
8efc0c15 267}
268
5260325f 269/*
270 * Stores a character in the buffer.
271 */
35484284 272void
5260325f 273buffer_put_char(Buffer *buffer, int value)
8efc0c15 274{
5260325f 275 char ch = value;
7528d467 276
5260325f 277 buffer_append(buffer, &ch, 1);
8efc0c15 278}
This page took 0.307116 seconds and 5 git commands to generate.