libbtc
bitcoinclibrary
serialize.c
Go to the documentation of this file.
1 /* Copyright 2012 exMULTI, Inc.
2  * Distributed under the MIT/X11 software license, see the accompanying
3  * file COPYING or http://www.opensource.org/licenses/mit-license.php.
4  */
5 
6 #include "serialize.h"
7 
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include "btc/cstr.h"
12 
13 void ser_bytes(cstring* s, const void* p, size_t len)
14 {
15  cstr_append_buf(s, p, len);
16 }
17 
18 void ser_u16(cstring* s, uint16_t v_)
19 {
20  uint16_t v = htole16(v_);
21  cstr_append_buf(s, &v, sizeof(v));
22 }
23 
24 void ser_u32(cstring* s, uint32_t v_)
25 {
26  uint32_t v = htole32(v_);
27  cstr_append_buf(s, &v, sizeof(v));
28 }
29 
30 void ser_u64(cstring* s, uint64_t v_)
31 {
32  uint64_t v = htole64(v_);
33  cstr_append_buf(s, &v, sizeof(v));
34 }
35 
36 void ser_varlen(cstring* s, uint32_t vlen)
37 {
38  unsigned char c;
39 
40  if (vlen < 253) {
41  c = vlen;
42  ser_bytes(s, &c, 1);
43  }
44 
45  else if (vlen < 0x10000) {
46  c = 253;
47  ser_bytes(s, &c, 1);
48  ser_u16(s, (uint16_t)vlen);
49  }
50 
51  else {
52  c = 254;
53  ser_bytes(s, &c, 1);
54  ser_u32(s, vlen);
55  }
56 
57  /* u64 case intentionally not implemented */
58 }
59 
60 void ser_str(cstring* s, const char* s_in, size_t maxlen)
61 {
62  size_t slen = strnlen(s_in, maxlen);
63 
64  ser_varlen(s, slen);
65  ser_bytes(s, s_in, slen);
66 }
67 
68 void ser_varstr(cstring* s, cstring* s_in)
69 {
70  if (!s_in || !s_in->len) {
71  ser_varlen(s, 0);
72  return;
73  }
74 
75  ser_varlen(s, s_in->len);
76  ser_bytes(s, s_in->str, s_in->len);
77 }
78 
80 {
81  unsigned int vec_len = vec ? vec->len : 0;
82 
83  ser_varlen(s, vec_len);
84 
85  unsigned int i;
86  for (i = 0; i < vec_len; i++) {
87  uint8_t* av = vector_idx(vec, i);
88  ser_u256(s, av);
89  }
90 }
91 
92 btc_bool deser_skip(struct const_buffer* buf, size_t len)
93 {
94  if (buf->len < len)
95  return false;
96 
97  buf->p += len;
98  buf->len -= len;
99 
100  return true;
101 }
102 
103 btc_bool deser_bytes(void* po, struct const_buffer* buf, size_t len)
104 {
105  if (buf->len < len)
106  return false;
107 
108  memcpy(po, buf->p, len);
109  buf->p += len;
110  buf->len -= len;
111 
112  return true;
113 }
114 
115 btc_bool deser_u16(uint16_t* vo, struct const_buffer* buf)
116 {
117  uint16_t v;
118 
119  if (!deser_bytes(&v, buf, sizeof(v)))
120  return false;
121 
122  *vo = le16toh(v);
123  return true;
124 }
125 
126 btc_bool deser_u32(uint32_t* vo, struct const_buffer* buf)
127 {
128  uint32_t v;
129 
130  if (!deser_bytes(&v, buf, sizeof(v)))
131  return false;
132 
133  *vo = le32toh(v);
134  return true;
135 }
136 
137 btc_bool deser_u64(uint64_t* vo, struct const_buffer* buf)
138 {
139  uint64_t v;
140 
141  if (!deser_bytes(&v, buf, sizeof(v)))
142  return false;
143 
144  *vo = le64toh(v);
145  return true;
146 }
147 
148 btc_bool deser_varlen(uint32_t* lo, struct const_buffer* buf)
149 {
150  uint32_t len;
151 
152  unsigned char c;
153  if (!deser_bytes(&c, buf, 1))
154  return false;
155 
156  if (c == 253) {
157  uint16_t v16;
158  if (!deser_u16(&v16, buf))
159  return false;
160  len = v16;
161  } else if (c == 254) {
162  uint32_t v32;
163  if (!deser_u32(&v32, buf))
164  return false;
165  len = v32;
166  } else if (c == 255) {
167  uint64_t v64;
168  if (!deser_u64(&v64, buf))
169  return false;
170  len = (uint32_t)v64; /* WARNING: truncate */
171  } else
172  len = c;
173 
174  *lo = len;
175  return true;
176 }
177 
178 btc_bool deser_str(char* so, struct const_buffer* buf, size_t maxlen)
179 {
180  uint32_t len;
181  if (!deser_varlen(&len, buf))
182  return false;
183 
184  /* if input larger than buffer, truncate copy, skip remainder */
185  uint32_t skip_len = 0;
186  if (len > maxlen) {
187  skip_len = len - maxlen;
188  len = maxlen;
189  }
190 
191  if (!deser_bytes(so, buf, len))
192  return false;
193  if (!deser_skip(buf, skip_len))
194  return false;
195 
196  /* add C string null */
197  if (len < maxlen)
198  so[len] = 0;
199  else
200  so[maxlen - 1] = 0;
201 
202  return true;
203 }
204 
206 {
207  if (*so) {
208  cstr_free(*so, true);
209  *so = NULL;
210  }
211 
212  uint32_t len;
213  if (!deser_varlen(&len, buf))
214  return false;
215 
216  if (buf->len < len)
217  return false;
218 
219  cstring* s = cstr_new_sz(len);
220  cstr_append_buf(s, buf->p, len);
221 
222  buf->p += len;
223  buf->len -= len;
224 
225  *so = s;
226 
227  return true;
228 }
229 
231 {
232  vector* vec = *vo;
233  if (vec) {
234  vector_free(vec, true);
235  *vo = vec = NULL;
236  }
237 
238  uint32_t vlen;
239  if (!deser_varlen(&vlen, buf))
240  return false;
241 
242  if (!vlen)
243  return true;
244 
245  vec = vector_new(vlen, free);
246  if (!vec)
247  return false;
248 
249  unsigned int i;
250  for (i = 0; i < vlen; i++) {
251  uint8_t* n = malloc(32);
252 
253  if (!deser_u256(n, buf)) {
254  free(n);
255  goto err_out;
256  }
257 
258  vector_add(vec, n);
259  }
260 
261  *vo = vec;
262  return true;
263 
264 err_out:
265  vector_free(vec, true);
266  return false;
267 }
vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
uint8_t btc_bool
Definition: btc.h:34
char * str
Definition: cstr.h:43
size_t len
Definition: buffer.h:22
Definition: vector.h:39
void ser_u256_vector(cstring *s, vector *vec)
Definition: serialize.c:79
btc_bool deser_str(char *so, struct const_buffer *buf, size_t maxlen)
Definition: serialize.c:178
btc_bool deser_skip(struct const_buffer *buf, size_t len)
Definition: serialize.c:92
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
static void ser_u256(cstring *s, const unsigned char *v_)
Definition: serialize.h:44
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
void ser_u64(cstring *s, uint64_t v_)
Definition: serialize.c:30
void ser_bytes(cstring *s, const void *p, size_t len)
Definition: serialize.c:13
const void * p
Definition: buffer.h:21
void ser_varlen(cstring *s, uint32_t vlen)
Definition: serialize.c:36
void ser_varstr(cstring *s, cstring *s_in)
Definition: serialize.c:68
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
void ser_str(cstring *s, const char *s_in, size_t maxlen)
Definition: serialize.c:60
void ser_u16(cstring *s, uint16_t v_)
Definition: serialize.c:18
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
btc_bool deser_u16(uint16_t *vo, struct const_buffer *buf)
Definition: serialize.c:115
#define vector_idx(vec, idx)
Definition: vector.h:59
size_t len
Definition: cstr.h:44
btc_bool deser_varstr(cstring **so, struct const_buffer *buf)
Definition: serialize.c:205
btc_bool deser_u64(uint64_t *vo, struct const_buffer *buf)
Definition: serialize.c:137
size_t len
Definition: vector.h:42
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
btc_bool deser_u256_vector(vector **vo, struct const_buffer *buf)
Definition: serialize.c:230
void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
Definition: cstr.h:41
static btc_bool deser_u256(uint8_t *vo, struct const_buffer *buf)
Definition: serialize.h:71