44
44
#define TOWN_CRIER_ENCODING_H
45
45
46
46
#include " commons.h"
47
+ #include " debug.h"
47
48
#include " log.h"
48
49
49
50
#include < vector>
50
51
#include < array>
51
52
52
-
53
53
using std::vector;
54
54
using std::invalid_argument;
55
55
using std::string;
56
56
using std::array;
57
57
typedef std::vector<uint8_t > BYTE;
58
58
59
-
60
59
// ! split an integer @code{num} to an byte array
61
60
// ! prepend 0 until reaching @code{width}
62
61
std::vector<uint8_t > itob (uint64_t num, size_t width = 0 );
63
62
63
+ // compute how many (non-zero) bytes there are in _i
64
+ template <typename T>
65
+ static uint8_t byte_length (T _i) {
66
+ uint8_t i = 0 ;
67
+ for (; _i != 0 ; ++i, _i >>= 8 ) {}
68
+ return i;
69
+ }
70
+
71
+ template <typename Iter>
72
+ void rlp_string (Iter begin, Iter end, std::vector<uint8_t > &out) {
73
+ static_assert (std::is_same<typename std::iterator_traits<Iter>::value_type, uint8_t >::value,
74
+ " Iter must point to uint8_t" );
75
+
76
+ long len = std::distance (begin, end);
77
+ if (len < 0 )
78
+ throw std::invalid_argument (" String too long to be encoded." );
79
+
80
+ int32_t len_len;
81
+ if (len == 1 && (*begin) < 0x80 ) {
82
+ out.push_back (*begin);
83
+ return ;
84
+ }
85
+
86
+ // longer than 1
87
+ if (len < 56 ) {
88
+ out.push_back (0x80 + static_cast <uint8_t >(len));
89
+ out.insert (out.end (), begin, end);
90
+ } else {
91
+ len_len = byte_length<size_t >(len);
92
+ if (len_len > 8 ) {
93
+ throw std::invalid_argument (" String too long to be encoded." );
94
+ }
95
+
96
+ out.push_back (0xb7 + static_cast <uint8_t >(len_len));
97
+
98
+ std::vector<uint8_t > b_len = itob (len);
99
+ out.insert (out.end (), b_len.begin (), b_len.end ());
100
+ out.insert (out.end (), begin, end);
101
+ }
102
+ }
103
+
64
104
class RLPSerializable {
65
105
public:
66
- virtual void to_rlp (std::vector<uint8_t >& out) = 0;
106
+ virtual void to_rlp (std::vector<uint8_t > & out) = 0;
67
107
};
68
108
69
- template <typename Iter>
70
- void rlp_string (Iter begin, Iter end, std::vector<uint8_t >& out);
71
-
72
109
class bytes : public std ::vector<uint8_t > {
73
110
public:
74
111
bytes () = default ;
75
- bytes (const bytes& a, const bytes& b) {
112
+ bytes (const bytes & a, const bytes & b) {
76
113
std::vector<uint8_t >::insert (std::vector<uint8_t >::end (), a.begin (), a.end ());
77
114
std::vector<uint8_t >::insert (std::vector<uint8_t >::end (), b.begin (), b.end ());
78
115
}
79
116
explicit bytes (size_t len) : std::vector<uint8_t>(len, static_cast <uint8_t >(0 )) {}
80
- explicit bytes (const std::vector<uint8_t >& data) : std::vector<uint8_t>(data) {}
117
+ explicit bytes (const std::vector<uint8_t > & data) : std::vector<uint8_t>(data) {}
81
118
void replace (const bytes &);
82
119
virtual void from_hex (const char *src);
83
- virtual void parseUInt64 (uint64_t i){
120
+ virtual void parseUInt64 (uint64_t i) {
84
121
this ->clear ();
85
122
vector<uint8_t > b = itob (i);
86
123
this ->insert (this ->begin (), b.begin (), b.end ());
@@ -90,20 +127,18 @@ class bytes : public std::vector<uint8_t> {
90
127
void toString ();
91
128
};
92
129
93
- #include " debug.h"
94
-
95
- class bytes20 : public RLPSerializable {
130
+ class bytes20 : public RLPSerializable {
96
131
private:
97
- static const size_t SIZE= 20 ;
132
+ static const size_t SIZE = 20 ;
98
133
array<uint8_t , SIZE> _b;
99
134
100
135
public:
101
136
bytes20 () = default ;
102
- explicit bytes20 (const char * hex);
103
- void to_rlp (std::vector<uint8_t >& out) override {
137
+ explicit bytes20 (const char * hex);
138
+ void to_rlp (std::vector<uint8_t > & out) override {
104
139
rlp_string (_b.begin (), _b.end (), out);
105
140
}
106
- void dump (const char * title) {
141
+ void dump (const char * title) {
107
142
#ifndef NDEBUG
108
143
hexdump (title, _b.data (), _b.size ());
109
144
#endif
@@ -112,7 +147,7 @@ class bytes20: public RLPSerializable {
112
147
113
148
class bytes32 : public bytes {
114
149
private:
115
- const size_t SIZE= 32 ;
150
+ const size_t SIZE = 32 ;
116
151
public:
117
152
bytes32 () {
118
153
vector<uint8_t >::resize (SIZE);
@@ -132,8 +167,7 @@ class bytes32 : public bytes {
132
167
void replace (const bytes32 &in) { bytes::replace (in); }
133
168
void replace (const BYTE &in);
134
169
135
-
136
- void reset (){}
170
+ void reset () {}
137
171
};
138
172
139
173
uint8_t get_n_th_byte (uint64_t in, int n);
0 commit comments