libbtc
bitcoinclibrary
serialize.h
Go to the documentation of this file.
1 /*
2 
3  The MIT License (MIT)
4 
5  Copyright (c) 2012 exMULTI, Inc.
6  Copyright (c) 2015 Jonas Schnelli
7 
8  Permission is hereby granted, free of charge, to any person obtaining
9  a copy of this software and associated documentation files (the "Software"),
10  to deal in the Software without restriction, including without limitation
11  the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  and/or sell copies of the Software, and to permit persons to whom the
13  Software is furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included
16  in all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
22  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  OTHER DEALINGS IN THE SOFTWARE.
25 
26  */
27 
28 #ifndef LIBBTC_VECTOR_H__
29 #define LIBBTC_VECTOR_H__
30 
31 #include <stdint.h>
32 
33 #include "buffer.h"
34 #include "btc/cstr.h"
35 #include "btc/vector.h"
36 
37 #include "portable_endian.h"
38 
39 extern void ser_bytes(cstring* s, const void* p, size_t len);
40 extern void ser_u16(cstring* s, uint16_t v_);
41 extern void ser_u32(cstring* s, uint32_t v_);
42 extern void ser_u64(cstring* s, uint64_t v_);
43 
44 static inline void ser_u256(cstring* s, const unsigned char* v_)
45 {
46  ser_bytes(s, v_, 32);
47 }
48 
49 extern void ser_varlen(cstring* s, uint32_t vlen);
50 extern void ser_str(cstring* s, const char* s_in, size_t maxlen);
51 extern void ser_varstr(cstring* s, cstring* s_in);
52 
53 static inline void ser_s32(cstring* s, int32_t v_)
54 {
55  ser_u32(s, (uint32_t)v_);
56 }
57 
58 static inline void ser_s64(cstring* s, int64_t v_)
59 {
60  ser_u64(s, (uint64_t)v_);
61 }
62 
63 extern void ser_u256_vector(cstring* s, vector* vec);
64 
65 extern btc_bool deser_skip(struct const_buffer* buf, size_t len);
66 extern btc_bool deser_bytes(void* po, struct const_buffer* buf, size_t len);
67 extern btc_bool deser_u16(uint16_t* vo, struct const_buffer* buf);
68 extern btc_bool deser_u32(uint32_t* vo, struct const_buffer* buf);
69 extern btc_bool deser_u64(uint64_t* vo, struct const_buffer* buf);
70 
71 static inline btc_bool deser_u256(uint8_t* vo, struct const_buffer* buf)
72 {
73  return deser_bytes(vo, buf, 32);
74 }
75 
76 extern btc_bool deser_varlen(uint32_t* lo, struct const_buffer* buf);
77 extern btc_bool deser_str(char* so, struct const_buffer* buf, size_t maxlen);
78 extern btc_bool deser_varstr(cstring** so, struct const_buffer* buf);
79 
80 static inline btc_bool deser_s64(int64_t* vo, struct const_buffer* buf)
81 {
82  return deser_u64((uint64_t*)vo, buf);
83 }
84 
85 extern btc_bool deser_u256_vector(vector** vo, struct const_buffer* buf);
86 
87 //extern void u256_from_compact(BIGNUM *vo, uint32_t c);
88 
89 #endif /* LIBBTC_VECTOR_H__ */
uint8_t btc_bool
Definition: btc.h:34
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
size_t len
Definition: buffer.h:22
Definition: vector.h:39
btc_bool deser_varstr(cstring **so, struct const_buffer *buf)
Definition: serialize.c:205
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_varstr(cstring *s, cstring *s_in)
Definition: serialize.c:68
static void ser_s32(cstring *s, int32_t v_)
Definition: serialize.h:53
void ser_str(cstring *s, const char *s_in, size_t maxlen)
Definition: serialize.c:60
static void ser_s64(cstring *s, int64_t v_)
Definition: serialize.h:58
btc_bool deser_bytes(void *po, struct const_buffer *buf, size_t len)
Definition: serialize.c:103
void ser_u256_vector(cstring *s, vector *vec)
Definition: serialize.c:79
static void ser_u256(cstring *s, const unsigned char *v_)
Definition: serialize.h:44
const void * p
Definition: buffer.h:21
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
btc_bool deser_u256_vector(vector **vo, struct const_buffer *buf)
Definition: serialize.c:230
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
void ser_u16(cstring *s, uint16_t v_)
Definition: serialize.c:18
static btc_bool deser_s64(int64_t *vo, struct const_buffer *buf)
Definition: serialize.h:80
void ser_u64(cstring *s, uint64_t v_)
Definition: serialize.c:30
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
Definition: cstr.h:41
static btc_bool deser_u256(uint8_t *vo, struct const_buffer *buf)
Definition: serialize.h:71
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