5 Author: Tatu Ylonen <ylo@cs.hut.fi>
7 Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 Created: Wed Mar 29 02:24:47 1995 ylo
12 Auxiliary functions for storing and retrieving various data types to/from
23 #include <openssl/bn.h>
33 /* Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
34 by (bits+7)/8 bytes of binary data, msb first. */
37 buffer_put_bignum(Buffer *buffer, BIGNUM *value)
39 int bits = BN_num_bits(value);
40 int bin_size = (bits + 7) / 8;
41 char *buf = xmalloc(bin_size);
45 /* Get the value of in binary */
46 oi = BN_bn2bin(value, buf);
48 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
51 /* Store the number of bits in the buffer in two bytes, msb first. */
53 buffer_append(buffer, msg, 2);
54 /* Store the binary data. */
55 buffer_append(buffer, buf, oi);
56 /* Clear the temporary data. */
57 memset(buf, 0, bin_size);
61 /* Retrieves an BIGNUM from the buffer. */
64 buffer_get_bignum(Buffer *buffer, BIGNUM *value)
67 unsigned char buf[2], *bin;
69 /* Get the number for bits. */
70 buffer_get(buffer, (char *)buf, 2);
71 bits = GET_16BIT(buf);
72 /* Compute the number of binary bytes that follow. */
73 bytes = (bits + 7) / 8;
75 buffer_get(buffer, bin, bytes);
76 BN_bin2bn(bin, bytes, value);
82 /* Returns an integer from the buffer (4 bytes, msb first). */
84 unsigned int buffer_get_int(Buffer *buffer)
87 buffer_get(buffer, (char *)buf, 4);
88 return GET_32BIT(buf);
91 /* Stores an integer in the buffer in 4 bytes, msb first. */
93 void buffer_put_int(Buffer *buffer, unsigned int value)
96 PUT_32BIT(buf, value);
97 buffer_append(buffer, buf, 4);
100 /* Returns an arbitrary binary string from the buffer. The string cannot
101 be longer than 256k. The returned value points to memory allocated
102 with xmalloc; it is the responsibility of the calling function to free
103 the data. If length_ptr is non-NULL, the length of the returned data
104 will be stored there. A null character will be automatically appended
105 to the returned string, and is not counted in length. */
107 char *buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
111 /* Get the length. */
112 len = buffer_get_int(buffer);
114 fatal("Received packet with bad string length %d", len);
115 /* Allocate space for the string. Add one byte for a null character. */
116 value = xmalloc(len + 1);
117 /* Get the string. */
118 buffer_get(buffer, value, len);
119 /* Append a null character to make processing easier. */
121 /* Optionally return the length of the string. */
127 /* Stores and arbitrary binary string in the buffer. */
129 void buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
131 buffer_put_int(buffer, len);
132 buffer_append(buffer, buf, len);
135 /* Returns a character from the buffer (0 - 255). */
137 int buffer_get_char(Buffer *buffer)
140 buffer_get(buffer, &ch, 1);
141 return (unsigned char)ch;
144 /* Stores a character in the buffer. */
146 void buffer_put_char(Buffer *buffer, int value)
149 buffer_append(buffer, &ch, 1);