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