Deleted Added
full compact
bufaux.c (57430) bufaux.c (57464)
1/*
2 *
3 * bufaux.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Wed Mar 29 02:24:47 1995 ylo
11 *
12 * Auxiliary functions for storing and retrieving various data types to/from
13 * Buffers.
14 *
1/*
2 *
3 * bufaux.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Wed Mar 29 02:24:47 1995 ylo
11 *
12 * Auxiliary functions for storing and retrieving various data types to/from
13 * Buffers.
14 *
15 * $FreeBSD: head/crypto/openssh/bufaux.c 57464 2000-02-25 01:53:12Z green $
15 */
16
17#include "includes.h"
18RCSID("$Id: bufaux.c,v 1.7 1999/11/24 19:53:44 markus Exp $");
19
20#include "ssh.h"
16 */
17
18#include "includes.h"
19RCSID("$Id: bufaux.c,v 1.7 1999/11/24 19:53:44 markus Exp $");
20
21#include "ssh.h"
21#include
22#include <openssl/bn.h>
22#include "bufaux.h"
23#include "xmalloc.h"
24#include "getput.h"
25
26/*
27 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
28 * by (bits+7)/8 bytes of binary data, msb first.
29 */
30void
31buffer_put_bignum(Buffer *buffer, BIGNUM *value)
32{
33 int bits = BN_num_bits(value);
34 int bin_size = (bits + 7) / 8;
35 char *buf = xmalloc(bin_size);
36 int oi;
37 char msg[2];
38
39 /* Get the value of in binary */
40 oi = BN_bn2bin(value, buf);
41 if (oi != bin_size)
42 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
43 oi, bin_size);
44
45 /* Store the number of bits in the buffer in two bytes, msb first. */
46 PUT_16BIT(msg, bits);
47 buffer_append(buffer, msg, 2);
48 /* Store the binary data. */
49 buffer_append(buffer, buf, oi);
50
51 memset(buf, 0, bin_size);
52 xfree(buf);
53}
54
55/*
56 * Retrieves an BIGNUM from the buffer.
57 */
58int
59buffer_get_bignum(Buffer *buffer, BIGNUM *value)
60{
61 int bits, bytes;
62 unsigned char buf[2], *bin;
63
64 /* Get the number for bits. */
65 buffer_get(buffer, (char *) buf, 2);
66 bits = GET_16BIT(buf);
67 /* Compute the number of binary bytes that follow. */
68 bytes = (bits + 7) / 8;
69 if (buffer_len(buffer) < bytes)
70 fatal("buffer_get_bignum: input buffer too small");
71 bin = buffer_ptr(buffer);
72 BN_bin2bn(bin, bytes, value);
73 buffer_consume(buffer, bytes);
74
75 return 2 + bytes;
76}
77
78/*
79 * Returns an integer from the buffer (4 bytes, msb first).
80 */
81unsigned int
82buffer_get_int(Buffer *buffer)
83{
84 unsigned char buf[4];
85 buffer_get(buffer, (char *) buf, 4);
86 return GET_32BIT(buf);
87}
88
89/*
90 * Stores an integer in the buffer in 4 bytes, msb first.
91 */
92void
93buffer_put_int(Buffer *buffer, unsigned int value)
94{
95 char buf[4];
96 PUT_32BIT(buf, value);
97 buffer_append(buffer, buf, 4);
98}
99
100/*
101 * Returns an arbitrary binary string from the buffer. The string cannot
102 * be longer than 256k. The returned value points to memory allocated
103 * with xmalloc; it is the responsibility of the calling function to free
104 * the data. If length_ptr is non-NULL, the length of the returned data
105 * will be stored there. A null character will be automatically appended
106 * to the returned string, and is not counted in length.
107 */
108char *
109buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
110{
111 unsigned int len;
112 char *value;
113 /* Get the length. */
114 len = buffer_get_int(buffer);
115 if (len > 256 * 1024)
116 fatal("Received packet with bad string length %d", len);
117 /* Allocate space for the string. Add one byte for a null character. */
118 value = xmalloc(len + 1);
119 /* Get the string. */
120 buffer_get(buffer, value, len);
121 /* Append a null character to make processing easier. */
122 value[len] = 0;
123 /* Optionally return the length of the string. */
124 if (length_ptr)
125 *length_ptr = len;
126 return value;
127}
128
129/*
130 * Stores and arbitrary binary string in the buffer.
131 */
132void
133buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
134{
135 buffer_put_int(buffer, len);
136 buffer_append(buffer, buf, len);
137}
138
139/*
140 * Returns a character from the buffer (0 - 255).
141 */
142int
143buffer_get_char(Buffer *buffer)
144{
145 char ch;
146 buffer_get(buffer, &ch, 1);
147 return (unsigned char) ch;
148}
149
150/*
151 * Stores a character in the buffer.
152 */
153void
154buffer_put_char(Buffer *buffer, int value)
155{
156 char ch = value;
157 buffer_append(buffer, &ch, 1);
158}
23#include "bufaux.h"
24#include "xmalloc.h"
25#include "getput.h"
26
27/*
28 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
29 * by (bits+7)/8 bytes of binary data, msb first.
30 */
31void
32buffer_put_bignum(Buffer *buffer, BIGNUM *value)
33{
34 int bits = BN_num_bits(value);
35 int bin_size = (bits + 7) / 8;
36 char *buf = xmalloc(bin_size);
37 int oi;
38 char msg[2];
39
40 /* Get the value of in binary */
41 oi = BN_bn2bin(value, buf);
42 if (oi != bin_size)
43 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
44 oi, bin_size);
45
46 /* Store the number of bits in the buffer in two bytes, msb first. */
47 PUT_16BIT(msg, bits);
48 buffer_append(buffer, msg, 2);
49 /* Store the binary data. */
50 buffer_append(buffer, buf, oi);
51
52 memset(buf, 0, bin_size);
53 xfree(buf);
54}
55
56/*
57 * Retrieves an BIGNUM from the buffer.
58 */
59int
60buffer_get_bignum(Buffer *buffer, BIGNUM *value)
61{
62 int bits, bytes;
63 unsigned char buf[2], *bin;
64
65 /* Get the number for bits. */
66 buffer_get(buffer, (char *) buf, 2);
67 bits = GET_16BIT(buf);
68 /* Compute the number of binary bytes that follow. */
69 bytes = (bits + 7) / 8;
70 if (buffer_len(buffer) < bytes)
71 fatal("buffer_get_bignum: input buffer too small");
72 bin = buffer_ptr(buffer);
73 BN_bin2bn(bin, bytes, value);
74 buffer_consume(buffer, bytes);
75
76 return 2 + bytes;
77}
78
79/*
80 * Returns an integer from the buffer (4 bytes, msb first).
81 */
82unsigned int
83buffer_get_int(Buffer *buffer)
84{
85 unsigned char buf[4];
86 buffer_get(buffer, (char *) buf, 4);
87 return GET_32BIT(buf);
88}
89
90/*
91 * Stores an integer in the buffer in 4 bytes, msb first.
92 */
93void
94buffer_put_int(Buffer *buffer, unsigned int value)
95{
96 char buf[4];
97 PUT_32BIT(buf, value);
98 buffer_append(buffer, buf, 4);
99}
100
101/*
102 * Returns an arbitrary binary string from the buffer. The string cannot
103 * be longer than 256k. The returned value points to memory allocated
104 * with xmalloc; it is the responsibility of the calling function to free
105 * the data. If length_ptr is non-NULL, the length of the returned data
106 * will be stored there. A null character will be automatically appended
107 * to the returned string, and is not counted in length.
108 */
109char *
110buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
111{
112 unsigned int len;
113 char *value;
114 /* Get the length. */
115 len = buffer_get_int(buffer);
116 if (len > 256 * 1024)
117 fatal("Received packet with bad string length %d", len);
118 /* Allocate space for the string. Add one byte for a null character. */
119 value = xmalloc(len + 1);
120 /* Get the string. */
121 buffer_get(buffer, value, len);
122 /* Append a null character to make processing easier. */
123 value[len] = 0;
124 /* Optionally return the length of the string. */
125 if (length_ptr)
126 *length_ptr = len;
127 return value;
128}
129
130/*
131 * Stores and arbitrary binary string in the buffer.
132 */
133void
134buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
135{
136 buffer_put_int(buffer, len);
137 buffer_append(buffer, buf, len);
138}
139
140/*
141 * Returns a character from the buffer (0 - 255).
142 */
143int
144buffer_get_char(Buffer *buffer)
145{
146 char ch;
147 buffer_get(buffer, &ch, 1);
148 return (unsigned char) ch;
149}
150
151/*
152 * Stores a character in the buffer.
153 */
154void
155buffer_put_char(Buffer *buffer, int value)
156{
157 char ch = value;
158 buffer_append(buffer, &ch, 1);
159}