]> andersk Git - openssh.git/blob - bufaux.c
*** empty log message ***
[openssh.git] / bufaux.c
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 */
16
17 #include "config.h"
18 #include "includes.h"
19 RCSID("$Id$");
20
21 #include "ssh.h"
22
23 #ifdef HAVE_OPENSSL
24 #include <openssl/bn.h>
25 #endif
26 #ifdef HAVE_SSL
27 #include <ssl/bn.h>
28 #endif
29
30 #include "bufaux.h"
31 #include "xmalloc.h"
32 #include "getput.h"
33
34 /* Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
35    by (bits+7)/8 bytes of binary data, msb first. */
36
37 void
38 buffer_put_bignum(Buffer *buffer, BIGNUM *value)
39 {
40   int bits = BN_num_bits(value);
41   int bin_size = (bits + 7) / 8;
42   char *buf = xmalloc(bin_size);
43   int oi;
44   char msg[2];
45   
46   /* Get the value of in binary */
47   oi = BN_bn2bin(value, buf);
48   assert(oi == bin_size);
49
50   /* Store the number of bits in the buffer in two bytes, msb first. */
51   PUT_16BIT(msg, bits);
52   buffer_append(buffer, msg, 2);
53   /* Store the binary data. */
54   buffer_append(buffer, buf, oi);
55   /* Clear the temporary data. */
56   memset(buf, 0, bin_size);
57   xfree(buf);
58 }
59
60 /* Retrieves an BIGNUM from the buffer. */
61
62 int
63 buffer_get_bignum(Buffer *buffer, BIGNUM *value)
64 {
65   int bits, bytes;
66   unsigned char buf[2], *bin;
67
68   /* Get the number for bits. */
69   buffer_get(buffer, (char *)buf, 2);
70   bits = GET_16BIT(buf);
71   /* Compute the number of binary bytes that follow. */
72   bytes = (bits + 7) / 8;
73   bin = xmalloc(bytes);
74   buffer_get(buffer, bin, bytes);
75   BN_bin2bn(bin, bytes, value);
76   xfree(bin);
77
78   return 2 + bytes;
79 }
80
81 /* Returns an integer from the buffer (4 bytes, msb first). */
82
83 unsigned int buffer_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 /* Stores an integer in the buffer in 4 bytes, msb first. */
91
92 void buffer_put_int(Buffer *buffer, unsigned int value)
93 {
94   char buf[4];
95   PUT_32BIT(buf, value);
96   buffer_append(buffer, buf, 4);
97 }
98
99 /* Returns an arbitrary binary string from the buffer.  The string cannot
100    be longer than 256k.  The returned value points to memory allocated
101    with xmalloc; it is the responsibility of the calling function to free
102    the data.  If length_ptr is non-NULL, the length of the returned data
103    will be stored there.  A null character will be automatically appended
104    to the returned string, and is not counted in length. */
105
106 char *buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
107 {
108   unsigned int len;
109   char *value;
110   /* Get the length. */
111   len = buffer_get_int(buffer);
112   if (len > 256*1024)
113     fatal("Received packet with bad string length %d", len);
114   /* Allocate space for the string.  Add one byte for a null character. */
115   value = xmalloc(len + 1);
116   /* Get the string. */
117   buffer_get(buffer, value, len);
118   /* Append a null character to make processing easier. */
119   value[len] = 0;
120   /* Optionally return the length of the string. */
121   if (length_ptr)
122     *length_ptr = len;
123   return value;
124 }
125
126 /* Stores and arbitrary binary string in the buffer. */
127
128 void buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
129 {
130   buffer_put_int(buffer, len);
131   buffer_append(buffer, buf, len);
132 }
133
134 /* Returns a character from the buffer (0 - 255). */
135
136 int buffer_get_char(Buffer *buffer)
137 {
138   char ch;
139   buffer_get(buffer, &ch, 1);
140   return (unsigned char)ch;
141 }
142
143 /* Stores a character in the buffer. */
144
145 void buffer_put_char(Buffer *buffer, int value)
146 {
147   char ch = value;
148   buffer_append(buffer, &ch, 1);
149 }
This page took 0.119443 seconds and 5 git commands to generate.