forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtlblib.cpp
387 lines (325 loc) · 9.85 KB
/
tlblib.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include <tl/tlblib.hpp>
namespace tlb {
const False t_False;
const True t_True;
const Unit t_Unit;
const Bool t_Bool;
const Int t_int8{8}, t_int16{16}, t_int24{24}, t_int32{32}, t_int64{64}, t_int128{128}, t_int256{256}, t_int257{257};
const UInt t_uint8{8}, t_uint16{16}, t_uint24{24}, t_uint32{32}, t_uint64{64}, t_uint128{128}, t_uint256{256};
const NatWidth t_Nat{32};
const Anything t_Anything;
const RefAnything t_RefCell;
std::string TLB::get_type_name() const {
std::ostringstream os;
print_type(os);
return os.str();
}
bool Bool::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
int t = get_tag(cs);
return cs.advance(1) && pp.out(t ? "bool_true" : "bool_false");
}
bool NatWidth::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
long long value = (long long)cs.fetch_ulong(32);
return value >= 0 && pp.out_int(value);
}
bool NatLeq::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
long long value = (long long)as_uint(cs);
return value >= 0 && skip(cs) && pp.out_int(value);
}
bool NatLess::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
long long value = (long long)as_uint(cs);
return value >= 0 && skip(cs) && pp.out_int(value);
}
bool TupleT::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
pp.open("tuple ");
pp.os << n << " [";
pp.mode_nl();
int i = n;
for (; i > 0; --i) {
if (!X.print_skip(pp, cs)) {
return false;
}
pp.mode_nl();
}
return pp.close("]");
}
bool CondT::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
return (n > 0 ? X.print_skip(pp, cs) : (!n && pp.out("()")));
}
bool Int::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
if (n <= 64) {
long long value;
return cs.fetch_int_to(n, value) && pp.out_int(value);
} else {
return pp.out_integer(cs.fetch_int256(n, true));
}
}
bool UInt::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
if (n <= 64) {
unsigned long long value;
return cs.fetch_uint_to(n, value) && pp.out_uint(value);
} else {
return pp.out_integer(cs.fetch_int256(n, false));
}
}
bool Bits::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
if (cs.have(n)) {
pp.os << 'x' << cs.fetch_bits(n).to_hex();
return true;
} else {
return false;
}
}
bool TupleT::skip(vm::CellSlice& cs) const {
int i = n;
for (; i > 0; --i) {
if (!X.skip(cs)) {
break;
}
}
return !i;
}
bool TupleT::validate_skip(int* ops, vm::CellSlice& cs, bool weak) const {
int i = n;
for (; i > 0; --i) {
if (!X.validate_skip(ops, cs, weak)) {
break;
}
}
return !i;
}
bool TLB::validate_ref_internal(int* ops, Ref<vm::Cell> cell_ref, bool weak) const {
if (ops) {
if (*ops <= 0) {
return false;
}
--*ops;
}
bool is_special;
auto cs = load_cell_slice_special(std::move(cell_ref), is_special);
return always_special() ? is_special : (is_special ? weak : (validate_skip(ops, cs) && cs.empty_ext()));
}
bool TLB::print_skip(PrettyPrinter& pp, vm::CellSlice& cs) const {
pp.open("raw@");
pp << *this << ' ';
vm::CellSlice cs_copy{cs};
int size_limit = pp.limit;
if (!validate_skip(&size_limit, cs) || !cs_copy.cut_tail(cs)) {
return pp.fail("invalid value");
}
pp.raw_nl();
return (cs_copy.print_rec(pp.os, &pp.limit, pp.indent) && pp.mkindent() && pp.close()) ||
pp.fail("raw value too long");
}
bool TLB::print_special(PrettyPrinter& pp, vm::CellSlice& cs) const {
pp.open("raw@");
pp << *this << ' ';
pp.raw_nl();
return (cs.print_rec(pp.os, &pp.limit, pp.indent) && pp.mkindent() && pp.close()) || pp.fail("raw value too long");
}
bool TLB::print_ref(PrettyPrinter& pp, Ref<vm::Cell> cell_ref) const {
if (cell_ref.is_null()) {
return pp.fail("null cell reference");
}
if (!pp.register_recursive_call()) {
return pp.fail("too many recursive calls while printing a TL-B value");
}
bool is_special;
auto cs = load_cell_slice_special(std::move(cell_ref), is_special);
if (is_special) {
return print_special(pp, cs);
} else {
return print_skip(pp, cs) && (cs.empty_ext() || pp.fail("extra data in cell"));
}
}
bool TLB::print_skip(std::ostream& os, vm::CellSlice& cs, int indent, int rec_limit) const {
PrettyPrinter pp{os, indent};
pp.set_limit(rec_limit);
return pp.fail_unless(print_skip(pp, cs));
}
bool TLB::print(std::ostream& os, const vm::CellSlice& cs, int indent, int rec_limit) const {
PrettyPrinter pp{os, indent};
pp.set_limit(rec_limit);
return pp.fail_unless(print(pp, cs));
}
bool TLB::print_ref(std::ostream& os, Ref<vm::Cell> cell_ref, int indent, int rec_limit) const {
PrettyPrinter pp{os, indent};
pp.set_limit(rec_limit);
return pp.fail_unless(print_ref(pp, std::move(cell_ref)));
}
std::string TLB::as_string_skip(vm::CellSlice& cs, int indent) const {
std::ostringstream os;
print_skip(os, cs, indent);
return os.str();
}
std::string TLB::as_string(const vm::CellSlice& cs, int indent) const {
std::ostringstream os;
print(os, cs, indent);
return os.str();
}
std::string TLB::as_string_ref(Ref<vm::Cell> cell_ref, int indent) const {
std::ostringstream os;
print_ref(os, std::move(cell_ref), indent);
return os.str();
}
PrettyPrinter::~PrettyPrinter() {
if (failed || level) {
if (nl_used) {
nl(-2 * level);
}
os << "PRINTING FAILED";
while (level > 0) {
os << ')';
--level;
}
}
if (nl_used) {
os << std::endl;
}
}
bool PrettyPrinter::fail(std::string msg) {
os << "<FATAL: " << msg << ">" << std::endl;
failed = true;
return false;
}
bool PrettyPrinter::mkindent(int delta) {
indent += delta;
for (int i = 0; i < indent; i++) {
os << ' ';
}
nl_used = true;
return true;
}
bool PrettyPrinter::nl(int delta) {
os << std::endl;
return mkindent(delta);
}
bool PrettyPrinter::raw_nl(int delta) {
os << std::endl;
indent += delta;
nl_used = true;
return true;
}
bool PrettyPrinter::open(std::string msg) {
os << "(" << msg;
indent += 2;
level++;
return true;
}
bool PrettyPrinter::close() {
return close("");
}
bool PrettyPrinter::close(std::string msg) {
if (level <= 0) {
return fail("cannot close scope");
}
indent -= 2;
--level;
os << msg << ")";
return true;
}
bool PrettyPrinter::mode_nl() {
if (mode & 1) {
return nl();
} else {
os << ' ';
return true;
}
}
bool PrettyPrinter::field(std::string name) {
mode_nl();
os << name << ':';
return true;
}
bool PrettyPrinter::field() {
mode_nl();
return true;
}
bool PrettyPrinter::field_int(long long x, std::string name) {
os << ' ' << name << ':' << x;
return true;
}
bool PrettyPrinter::field_int(long long x) {
os << ' ' << x;
return true;
}
bool PrettyPrinter::field_uint(unsigned long long x, std::string name) {
os << ' ' << name << ':' << x;
return true;
}
bool PrettyPrinter::field_uint(unsigned long long x) {
os << ' ' << x;
return true;
}
bool PrettyPrinter::fetch_bits_field(vm::CellSlice& cs, int n) {
os << " x";
return cs.have(n) && out(cs.fetch_bits(n).to_hex());
}
bool PrettyPrinter::fetch_bits_field(vm::CellSlice& cs, int n, std::string name) {
os << ' ' << name << ":x";
return cs.have(n) && out(cs.fetch_bits(n).to_hex());
}
bool PrettyPrinter::fetch_int_field(vm::CellSlice& cs, int n) {
return cs.have(n) && field_int(cs.fetch_long(n));
}
bool PrettyPrinter::fetch_int_field(vm::CellSlice& cs, int n, std::string name) {
return cs.have(n) && field_int(cs.fetch_long(n), name);
}
bool PrettyPrinter::fetch_uint_field(vm::CellSlice& cs, int n) {
return cs.have(n) && field_uint(cs.fetch_ulong(n));
}
bool PrettyPrinter::fetch_uint_field(vm::CellSlice& cs, int n, std::string name) {
return cs.have(n) && field_uint(cs.fetch_ulong(n), name);
}
bool PrettyPrinter::fetch_int256_field(vm::CellSlice& cs, int n) {
os << ' ';
return out_integer(cs.fetch_int256(n, true));
}
bool PrettyPrinter::fetch_int256_field(vm::CellSlice& cs, int n, std::string name) {
os << ' ' << name << ':';
return out_integer(cs.fetch_int256(n, true));
}
bool PrettyPrinter::fetch_uint256_field(vm::CellSlice& cs, int n) {
os << ' ';
return out_integer(cs.fetch_int256(n, false));
}
bool PrettyPrinter::fetch_uint256_field(vm::CellSlice& cs, int n, std::string name) {
os << ' ' << name << ':';
return out_integer(cs.fetch_int256(n, false));
}
} // namespace tlb
namespace tlb {
bool TypenameLookup::register_types(typename TypenameLookup::register_func_t func) {
return func([this](const char* name, const TLB* tp) { return register_type(name, tp); });
}
bool TypenameLookup::register_type(const char* name, const TLB* tp) {
if (!name || !tp) {
return false;
}
auto res = types.emplace(name, tp);
return res.second;
}
const TLB* TypenameLookup::lookup(std::string str) const {
auto it = types.find(str);
return it != types.end() ? it->second : nullptr;
}
const TLB* TypenameLookup::lookup(td::Slice str) const {
auto it = std::lower_bound(types.begin(), types.end(), str,
[](const auto& x, const auto& y) { return td::Slice(x.first) < y; });
return it != types.end() && td::Slice(it->first) == str ? it->second : nullptr;
}
} // namespace tlb