libbtc
bitcoinclibrary
Functions
serialize.h File Reference
#include <stdint.h>
#include "buffer.h"
#include "btc/cstr.h"
#include "btc/vector.h"
#include "portable_endian.h"

Go to the source code of this file.

Functions

void ser_bytes (cstring *s, const void *p, size_t len)
 
void ser_u16 (cstring *s, uint16_t v_)
 
void ser_u32 (cstring *s, uint32_t v_)
 
void ser_u64 (cstring *s, uint64_t v_)
 
static void ser_u256 (cstring *s, const unsigned char *v_)
 
void ser_varlen (cstring *s, uint32_t vlen)
 
void ser_str (cstring *s, const char *s_in, size_t maxlen)
 
void ser_varstr (cstring *s, cstring *s_in)
 
static void ser_s32 (cstring *s, int32_t v_)
 
static void ser_s64 (cstring *s, int64_t v_)
 
void ser_u256_vector (cstring *s, vector *vec)
 
btc_bool deser_skip (struct const_buffer *buf, size_t len)
 
btc_bool deser_bytes (void *po, struct const_buffer *buf, size_t len)
 
btc_bool deser_u16 (uint16_t *vo, struct const_buffer *buf)
 
btc_bool deser_u32 (uint32_t *vo, struct const_buffer *buf)
 
btc_bool deser_u64 (uint64_t *vo, struct const_buffer *buf)
 
static btc_bool deser_u256 (uint8_t *vo, struct const_buffer *buf)
 
btc_bool deser_varlen (uint32_t *lo, struct const_buffer *buf)
 
btc_bool deser_str (char *so, struct const_buffer *buf, size_t maxlen)
 
btc_bool deser_varstr (cstring **so, struct const_buffer *buf)
 
static btc_bool deser_s64 (int64_t *vo, struct const_buffer *buf)
 
btc_bool deser_u256_vector (vector **vo, struct const_buffer *buf)
 

Function Documentation

btc_bool deser_bytes ( void *  po,
struct const_buffer buf,
size_t  len 
)

Definition at line 103 of file serialize.c.

References const_buffer::len, and const_buffer::p.

Referenced by btc_script_copy_without_op_codeseperator(), btc_script_get_ops(), deser_str(), deser_u16(), deser_u256(), deser_u32(), deser_u64(), and deser_varlen().

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 }
size_t len
Definition: buffer.h:22
const void * p
Definition: buffer.h:21
static btc_bool deser_s64 ( int64_t *  vo,
struct const_buffer buf 
)
inlinestatic

Definition at line 80 of file serialize.h.

References deser_u64().

Referenced by btc_tx_out_deserialize().

81 {
82  return deser_u64((uint64_t*)vo, buf);
83 }
btc_bool deser_u64(uint64_t *vo, struct const_buffer *buf)
Definition: serialize.c:137
btc_bool deser_skip ( struct const_buffer buf,
size_t  len 
)

Definition at line 92 of file serialize.c.

References const_buffer::len, and const_buffer::p.

Referenced by btc_script_copy_without_op_codeseperator(), btc_script_get_ops(), and deser_str().

93 {
94  if (buf->len < len)
95  return false;
96 
97  buf->p += len;
98  buf->len -= len;
99 
100  return true;
101 }
size_t len
Definition: buffer.h:22
const void * p
Definition: buffer.h:21
btc_bool deser_str ( char *  so,
struct const_buffer buf,
size_t  maxlen 
)

Definition at line 178 of file serialize.c.

References deser_bytes(), deser_skip(), deser_varlen(), and const_buffer::len.

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 }
size_t len
Definition: buffer.h:22
btc_bool deser_skip(struct const_buffer *buf, size_t len)
Definition: serialize.c:92
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_u16 ( uint16_t *  vo,
struct const_buffer buf 
)

Definition at line 115 of file serialize.c.

References deser_bytes().

Referenced by btc_script_copy_without_op_codeseperator(), btc_script_get_ops(), and deser_varlen().

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 }
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
static btc_bool deser_u256 ( uint8_t *  vo,
struct const_buffer buf 
)
inlinestatic

Definition at line 71 of file serialize.h.

References deser_bytes().

Referenced by btc_tx_in_deserialize(), and deser_u256_vector().

72 {
73  return deser_bytes(vo, buf, 32);
74 }
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_u256_vector ( vector **  vo,
struct const_buffer buf 
)

Definition at line 230 of file serialize.c.

References deser_u256(), deser_varlen(), vector_add(), vector_free(), and vector_new().

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
Definition: vector.h:39
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
static btc_bool deser_u256(uint8_t *vo, struct const_buffer *buf)
Definition: serialize.h:71
btc_bool deser_u32 ( uint32_t *  vo,
struct const_buffer buf 
)

Definition at line 126 of file serialize.c.

References deser_bytes().

Referenced by btc_script_copy_without_op_codeseperator(), btc_script_get_ops(), btc_tx_deserialize(), btc_tx_in_deserialize(), and deser_varlen().

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 }
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_u64 ( uint64_t *  vo,
struct const_buffer buf 
)

Definition at line 137 of file serialize.c.

References deser_bytes().

Referenced by deser_s64(), and deser_varlen().

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 }
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_varlen ( uint32_t *  lo,
struct const_buffer buf 
)

Definition at line 148 of file serialize.c.

References deser_bytes(), deser_u16(), deser_u32(), deser_u64(), and const_buffer::len.

Referenced by btc_tx_deserialize(), deser_str(), deser_u256_vector(), and deser_varstr().

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 }
size_t len
Definition: buffer.h:22
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
btc_bool deser_u16(uint16_t *vo, struct const_buffer *buf)
Definition: serialize.c:115
btc_bool deser_u64(uint64_t *vo, struct const_buffer *buf)
Definition: serialize.c:137
btc_bool deser_varstr ( cstring **  so,
struct const_buffer buf 
)

Definition at line 205 of file serialize.c.

References cstr_append_buf(), cstr_free(), cstr_new_sz(), deser_varlen(), const_buffer::len, and const_buffer::p.

Referenced by btc_tx_in_deserialize(), and btc_tx_out_deserialize().

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 }
size_t len
Definition: buffer.h:22
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
const void * p
Definition: buffer.h:21
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
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
void ser_bytes ( cstring s,
const void *  p,
size_t  len 
)

Definition at line 13 of file serialize.c.

References cstr_append_buf().

Referenced by ser_str(), ser_u256(), ser_varlen(), and ser_varstr().

14 {
15  cstr_append_buf(s, p, len);
16 }
size_t len
Definition: buffer.h:22
const void * p
Definition: buffer.h:21
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
static void ser_s32 ( cstring s,
int32_t  v_ 
)
inlinestatic

Definition at line 53 of file serialize.h.

References ser_u32().

Referenced by btc_tx_sighash().

54 {
55  ser_u32(s, (uint32_t)v_);
56 }
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
static void ser_s64 ( cstring s,
int64_t  v_ 
)
inlinestatic

Definition at line 58 of file serialize.h.

References ser_u64().

Referenced by btc_tx_out_serialize().

59 {
60  ser_u64(s, (uint64_t)v_);
61 }
void ser_u64(cstring *s, uint64_t v_)
Definition: serialize.c:30
void ser_str ( cstring s,
const char *  s_in,
size_t  maxlen 
)

Definition at line 60 of file serialize.c.

References ser_bytes(), and ser_varlen().

61 {
62  size_t slen = strnlen(s_in, maxlen);
63 
64  ser_varlen(s, slen);
65  ser_bytes(s, s_in, slen);
66 }
void ser_bytes(cstring *s, const void *p, size_t len)
Definition: serialize.c:13
void ser_varlen(cstring *s, uint32_t vlen)
Definition: serialize.c:36
void ser_u16 ( cstring s,
uint16_t  v_ 
)

Definition at line 18 of file serialize.c.

References cstr_append_buf().

Referenced by ser_varlen().

19 {
20  uint16_t v = htole16(v_);
21  cstr_append_buf(s, &v, sizeof(v));
22 }
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
static void ser_u256 ( cstring s,
const unsigned char *  v_ 
)
inlinestatic

Definition at line 44 of file serialize.h.

References ser_bytes().

Referenced by btc_tx_in_serialize(), and ser_u256_vector().

45 {
46  ser_bytes(s, v_, 32);
47 }
void ser_bytes(cstring *s, const void *p, size_t len)
Definition: serialize.c:13
void ser_u256_vector ( cstring s,
vector vec 
)

Definition at line 79 of file serialize.c.

References vector::len, ser_u256(), ser_varlen(), and vector_idx.

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 }
static void ser_u256(cstring *s, const unsigned char *v_)
Definition: serialize.h:44
void ser_varlen(cstring *s, uint32_t vlen)
Definition: serialize.c:36
#define vector_idx(vec, idx)
Definition: vector.h:59
size_t len
Definition: vector.h:42
void ser_u32 ( cstring s,
uint32_t  v_ 
)

Definition at line 24 of file serialize.c.

References cstr_append_buf().

Referenced by btc_tx_in_serialize(), btc_tx_serialize(), ser_s32(), and ser_varlen().

25 {
26  uint32_t v = htole32(v_);
27  cstr_append_buf(s, &v, sizeof(v));
28 }
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
void ser_u64 ( cstring s,
uint64_t  v_ 
)

Definition at line 30 of file serialize.c.

References cstr_append_buf().

Referenced by ser_s64().

31 {
32  uint64_t v = htole64(v_);
33  cstr_append_buf(s, &v, sizeof(v));
34 }
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
void ser_varlen ( cstring s,
uint32_t  vlen 
)

Definition at line 36 of file serialize.c.

References ser_bytes(), ser_u16(), and ser_u32().

Referenced by btc_tx_serialize(), ser_str(), ser_u256_vector(), and ser_varstr().

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 }
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
void ser_bytes(cstring *s, const void *p, size_t len)
Definition: serialize.c:13
void ser_u16(cstring *s, uint16_t v_)
Definition: serialize.c:18
void ser_varstr ( cstring s,
cstring s_in 
)

Definition at line 68 of file serialize.c.

References cstring::len, ser_bytes(), ser_varlen(), and cstring::str.

Referenced by btc_tx_in_serialize(), and btc_tx_out_serialize().

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 }
char * str
Definition: cstr.h:43
void ser_bytes(cstring *s, const void *p, size_t len)
Definition: serialize.c:13
void ser_varlen(cstring *s, uint32_t vlen)
Definition: serialize.c:36
size_t len
Definition: cstr.h:44