-
Notifications
You must be signed in to change notification settings - Fork 12
/
Serialize.cpp
161 lines (120 loc) · 3.66 KB
/
Serialize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <memory>
#include <snarklib/Util.hpp>
#include "snarkfront/Serialize.hpp"
using namespace snarklib;
using namespace std;
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// uint32_t
//
void writeStream(ostream& os, const uint32_t& a) {
const char *ptr = reinterpret_cast<const char*>(addressof(a));
if (is_big_endian<int>()) {
for (int i = sizeof(uint32_t) - 1; i >= 0; --i)
os.put(ptr[i]);
} else {
os.write(ptr, sizeof(uint32_t));
}
}
bool readStream(istream& is, uint32_t& a) {
char *ptr = reinterpret_cast<char*>(addressof(a));
if (is_big_endian<int>()) {
for (int i = sizeof(uint32_t) - 1; i >= 0; --i)
if (! is.get(ptr[i])) return false;
} else {
if (! is.read(ptr, sizeof(uint32_t))) return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// uint64_t
//
void writeStream(ostream& os, const uint64_t& a) {
const char *ptr = reinterpret_cast<const char*>(addressof(a));
if (is_big_endian<int>()) {
for (int i = sizeof(uint64_t) - 1; i >= 0; --i)
os.put(ptr[i]);
} else {
os.write(ptr, sizeof(uint64_t));
}
}
bool readStream(istream& is, uint64_t& a) {
char *ptr = reinterpret_cast<char*>(addressof(a));
if (is_big_endian<int>()) {
for (int i = sizeof(uint64_t) - 1; i >= 0; --i)
if (! is.get(ptr[i])) return false;
} else {
if (! is.read(ptr, sizeof(uint64_t))) return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// string
//
void writeStream(ostream& os, const string& a) {
// number of octets
// a single space
// string text
os << a.size() << ' ' << a;
}
bool readStream(istream& is, string& a) {
// number of octets
size_t len = -1;
if (!(is >> len) || (-1 == len)) return false;
// single space
char c;
if (!is.get(c) || (' ' != c)) return false;
// string text
char buffer[len];
if (!is.read(buffer, len)) return false;
a = string(buffer, len);
// success
return true;
}
////////////////////////////////////////////////////////////////////////////////
// byte vector
//
void writeStream(ostream& os, const vector<uint8_t>& a) {
// number of elements followed by a single space
os << a.size() << ' ';
// vector elements
os.write(reinterpret_cast<const char*>(a.data()), a.size());
}
bool readStream(istream& is, vector<uint8_t>& a) {
// number of elements
size_t len = -1;
if (!(is >> len) || (-1 == len)) return false;
// single space
char c;
if (!is.get(c) || (' ' != c)) return false;
// vector elements
a.resize(len);
if (!is.read(reinterpret_cast<char*>(a.data()), len)) return false;
// success
return true;
}
////////////////////////////////////////////////////////////////////////////////
// read and write types: vector<uint32_t> (commitment vector)
//
void writeStream(ostream& os, const vector<uint32_t>& a) {
// number of elements followed by a single space
os << a.size() << ' ';
// vector elements
for (const auto& b : a)
writeStream(os, b);
}
bool readStream(istream& is, vector<uint32_t>& a) {
// number of elements
size_t len = -1;
if (!(is >> len) || (-1 == len)) return false;
// single space
char c;
if (!is.get(c) || (' ' != c)) return false;
// vector elements
a.resize(len);
for (std::size_t i = 0; i < len; ++i)
if (! readStream(is, a[i])) return false;
// success
return true;
}
} // namespace snarkfront