-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathwords.cpp
3433 lines (3105 loc) · 108 KB
/
words.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 "words.h"
#include "Dictionary.h"
#include "IntCtx.h"
#include "SourceLookup.h"
#include "HashMap.h"
#include "common/refcnt.hpp"
#include "common/bigint.hpp"
#include "common/refint.h"
#include "common/bitstring.h"
#include "common/util.h"
#include "openssl/digest.hpp"
#include "Ed25519.h"
#include "vm/cells.h"
#include "vm/cellslice.h"
#include "vm/vm.h"
#include "vm/cp0.h"
#include "vm/dict.h"
#include "vm/boc.h"
#include "vm/box.hpp"
#include "vm/atom.h"
#include "block/block.h"
#include "common/global-version.h"
#include "td/utils/filesystem.h"
#include "td/utils/misc.h"
#include "td/utils/optional.h"
#include "td/utils/PathView.h"
#include "td/utils/port/thread.h"
#include "td/utils/port/Stat.h"
#include "td/utils/Timer.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/crypto.h"
#include <ctime>
namespace fift {
const Ref<FiftCont> nop_word_def = Ref<NopWord>{true};
//
// functions for wordef
//
Ref<FiftCont> pop_exec_token(vm::Stack& stack) {
auto wd_ref = stack.pop_chk().as_object<FiftCont>();
if (wd_ref.is_null()) {
throw IntError{"execution token expected"};
}
return wd_ref;
}
Ref<WordList> pop_word_list(vm::Stack& stack) {
auto wl_ref = stack.pop_chk().as_object<WordList>();
if (wl_ref.is_null()) {
throw IntError{"word list expected"};
}
return wl_ref;
}
void push_argcount(vm::Stack& stack, int args) {
stack.push_smallint(args);
stack.push_object(nop_word_def);
}
void interpret_dot(IntCtx& ctx, bool space_after) {
*ctx.output_stream << dec_string2(ctx.stack.pop_int()) << (space_after ? " " : "");
}
void interpret_dothex(IntCtx& ctx, bool upcase, bool space_after) {
*ctx.output_stream << hex_string(ctx.stack.pop_int(), upcase) << (space_after ? " " : "");
}
void interpret_dotbinary(IntCtx& ctx, bool space_after) {
*ctx.output_stream << binary_string(ctx.stack.pop_int()) << (space_after ? " " : "");
}
void interpret_dot_cellslice_rec(IntCtx& ctx) {
auto cs = ctx.stack.pop_cellslice();
cs->print_rec(*ctx.output_stream);
}
void interpret_dotstack(IntCtx& ctx) {
for (int i = ctx.stack.depth(); i > 0; i--) {
ctx.stack[i - 1].dump(*ctx.output_stream);
*ctx.output_stream << ' ';
}
*ctx.output_stream << std::endl;
}
void interpret_dotstack_list(IntCtx& ctx) {
for (int i = ctx.stack.depth(); i > 0; i--) {
ctx.stack[i - 1].print_list(*ctx.output_stream);
*ctx.output_stream << ' ';
}
*ctx.output_stream << std::endl;
}
void interpret_dotstack_list_dump(IntCtx& ctx) {
ctx.stack.dump(*ctx.output_stream, 3);
}
void interpret_dump(IntCtx& ctx) {
ctx.stack.pop_chk().dump(*ctx.output_stream);
*ctx.output_stream << ' ';
}
void interpret_dump_internal(vm::Stack& stack) {
stack.push_string(stack.pop_chk().to_string());
}
void interpret_list_dump_internal(vm::Stack& stack) {
stack.push_string(stack.pop_chk().to_lisp_string());
}
void interpret_print_list(IntCtx& ctx) {
ctx.stack.pop_chk().print_list(*ctx.output_stream);
*ctx.output_stream << ' ';
}
void interpret_dottc(IntCtx& ctx) {
*ctx.output_stream << "total cells = " << vm::DataCell::get_total_data_cells() << std::endl;
}
void interpret_dot_internal(vm::Stack& stack) {
stack.push_string(dec_string2(stack.pop_int()));
}
void interpret_dothex_internal(vm::Stack& stack, bool upcase) {
stack.push_string(hex_string(stack.pop_int(), upcase));
}
void interpret_dotbinary_internal(vm::Stack& stack) {
stack.push_string(binary_string(stack.pop_int()));
}
void interpret_plus(vm::Stack& stack) {
stack.push_int(stack.pop_int() + stack.pop_int());
}
void interpret_cond_dup(vm::Stack& stack) {
auto x = stack.pop_int();
if (x->sgn()) {
stack.push_int(x);
}
stack.push_int(std::move(x));
}
void interpret_plus_tiny(vm::Stack& stack, long long y) {
stack.push_int(stack.pop_int() + y);
}
void interpret_minus(vm::Stack& stack) {
auto y = stack.pop_int();
stack.push_int(stack.pop_int() - y);
}
void interpret_times(vm::Stack& stack) {
stack.push_int(stack.pop_int() * stack.pop_int());
}
void interpret_div(vm::Stack& stack, int round_mode) {
auto y = stack.pop_int();
stack.push_int(td::div(stack.pop_int(), y, round_mode));
}
void interpret_mod(vm::Stack& stack, int round_mode) {
auto y = stack.pop_int();
stack.push_int(td::mod(stack.pop_int(), y, round_mode));
}
void interpret_divmod(vm::Stack& stack, int round_mode) {
auto y = stack.pop_int();
auto dm = td::divmod(stack.pop_int(), std::move(y), round_mode);
stack.push_int(std::move(dm.first));
stack.push_int(std::move(dm.second));
}
void interpret_times_div(vm::Stack& stack, int round_mode) {
auto z = stack.pop_int(), y = stack.pop_int(), x = stack.pop_int();
stack.push_int(muldiv(std::move(x), std::move(y), std::move(z), round_mode));
}
void interpret_times_divmod(vm::Stack& stack, int round_mode) {
auto z = stack.pop_int(), y = stack.pop_int(), x = stack.pop_int();
auto dm = muldivmod(std::move(x), std::move(y), std::move(z));
stack.push_int(std::move(dm.first));
stack.push_int(std::move(dm.second));
}
void interpret_times_mod(vm::Stack& stack, int round_mode) {
auto z = stack.pop_int();
auto y = stack.pop_int();
auto x = stack.pop_int();
typename td::BigInt256::DoubleInt tmp{0}, q;
tmp.add_mul(*x, *y);
tmp.mod_div(*z, q, round_mode);
stack.push_int(td::make_refint(tmp));
}
void interpret_negate(vm::Stack& stack) {
stack.push_int(-stack.pop_int());
}
void interpret_cmp(vm::Stack& stack, const char opt[3]) {
auto y = stack.pop_int();
auto x = stack.pop_int();
int r = x->cmp(*y);
assert((unsigned)(r + 1) <= 2);
stack.push_smallint(((const signed char*)opt)[r + 1]);
}
void interpret_sgn(vm::Stack& stack, const char opt[3]) {
auto x = stack.pop_int_finite();
int r = x->sgn();
assert((unsigned)(r + 1) <= 2);
stack.push_smallint(((const signed char*)opt)[r + 1]);
}
void interpret_fits(vm::Stack& stack, bool sgnd) {
int n = stack.pop_smallint_range(1023);
auto x = stack.pop_int();
stack.push_bool(x->fits_bits(n, sgnd));
}
void interpret_pow2(vm::Stack& stack) {
int x = stack.pop_smallint_range(255);
auto r = td::make_refint();
r.unique_write().set_pow2(x);
stack.push_int(r);
}
void interpret_neg_pow2(vm::Stack& stack) {
int x = stack.pop_smallint_range(256);
auto r = td::make_refint();
r.unique_write().set_pow2(x).negate().normalize();
stack.push_int(r);
}
void interpret_pow2_minus1(vm::Stack& stack) {
int x = stack.pop_smallint_range(256);
auto r = td::make_refint();
r.unique_write().set_pow2(x).add_tiny(-1).normalize();
stack.push_int(r);
}
void interpret_mod_pow2(vm::Stack& stack) {
int y = stack.pop_smallint_range(256);
auto x = stack.pop_int();
x.write().mod_pow2(y).normalize();
stack.push_int(x);
}
void interpret_lshift(vm::Stack& stack) {
int y = stack.pop_smallint_range(256);
stack.push_int(stack.pop_int() << y);
}
void interpret_rshift(vm::Stack& stack, int round_mode) {
int y = stack.pop_smallint_range(256);
stack.push_int(rshift(stack.pop_int(), y, round_mode));
}
void interpret_lshift_const(vm::Stack& stack, int y) {
stack.push_int(stack.pop_int() << y);
}
void interpret_rshift_const(vm::Stack& stack, int y) {
stack.push_int(stack.pop_int() >> y);
}
void interpret_times_rshift(vm::Stack& stack, int round_mode) {
int z = stack.pop_smallint_range(256);
auto y = stack.pop_int();
auto x = stack.pop_int();
typename td::BigInt256::DoubleInt tmp{0};
tmp.add_mul(*x, *y).rshift(z, round_mode).normalize();
stack.push_int(td::make_refint(tmp));
}
void interpret_lshift_div(vm::Stack& stack, int round_mode) {
int z = stack.pop_smallint_range(256);
auto y = stack.pop_int();
auto x = stack.pop_int();
typename td::BigInt256::DoubleInt tmp{*x};
tmp <<= z;
auto q = td::make_refint();
tmp.mod_div(*y, q.unique_write(), round_mode);
q.unique_write().normalize();
stack.push_int(std::move(q));
}
void interpret_not(vm::Stack& stack) {
stack.push_int(~stack.pop_int());
}
void interpret_and(vm::Stack& stack) {
stack.push_int(stack.pop_int() & stack.pop_int());
}
void interpret_or(vm::Stack& stack) {
stack.push_int(stack.pop_int() | stack.pop_int());
}
void interpret_xor(vm::Stack& stack) {
stack.push_int(stack.pop_int() ^ stack.pop_int());
}
void interpret_has_type(vm::Stack& stack, int t) {
stack.push_bool(stack.pop_chk().type() == t);
}
void interpret_drop(vm::Stack& stack) {
stack.check_underflow(1);
stack.pop();
}
void interpret_2drop(vm::Stack& stack) {
stack.check_underflow(2);
stack.pop();
stack.pop();
}
void interpret_dup(vm::Stack& stack) {
stack.check_underflow(1);
stack.push(stack.fetch(0));
}
void interpret_2dup(vm::Stack& stack) {
stack.check_underflow(2);
stack.push(stack.fetch(1));
stack.push(stack.fetch(1));
}
void interpret_over(vm::Stack& stack) {
stack.check_underflow(2);
stack.push(stack.fetch(1));
}
void interpret_2over(vm::Stack& stack) {
stack.check_underflow(4);
stack.push(stack.fetch(3));
stack.push(stack.fetch(3));
}
void interpret_swap(vm::Stack& stack) {
stack.check_underflow(2);
swap(stack[0], stack[1]);
}
void interpret_2swap(vm::Stack& stack) {
stack.check_underflow(4);
swap(stack[0], stack[2]);
swap(stack[1], stack[3]);
}
void interpret_tuck(vm::Stack& stack) {
stack.check_underflow(2);
swap(stack[0], stack[1]);
stack.push(stack.fetch(1));
}
void interpret_nip(vm::Stack& stack) {
stack.check_underflow(2);
stack.pop(stack[1]);
}
void interpret_rot(vm::Stack& stack) {
stack.check_underflow(3);
swap(stack[1], stack[2]);
swap(stack[0], stack[1]);
}
void interpret_rot_rev(vm::Stack& stack) {
stack.check_underflow(3);
swap(stack[0], stack[1]);
swap(stack[1], stack[2]);
}
void interpret_pick(vm::Stack& stack) {
int n = stack.pop_smallint_range(255);
stack.check_underflow(n + 1);
stack.push(stack.fetch(n));
}
void interpret_roll(vm::Stack& stack) {
int n = stack.pop_smallint_range(255);
stack.check_underflow(n + 1);
for (int i = n; i > 0; i--) {
swap(stack[i], stack[i - 1]);
}
}
void interpret_roll_rev(vm::Stack& stack) {
int n = stack.pop_smallint_range(255);
stack.check_underflow(n + 1);
for (int i = 0; i < n; i++) {
swap(stack[i], stack[i + 1]);
}
}
void interpret_reverse(vm::Stack& stack) {
int m = stack.pop_smallint_range(255);
int n = stack.pop_smallint_range(255);
stack.check_underflow(n + m);
int s = 2 * m + n - 1;
for (int i = ((s - 1) >> 1); i >= m; i--) {
swap(stack[i], stack[s - i]);
}
}
void interpret_exch(vm::Stack& stack) {
int n = stack.pop_smallint_range(255);
stack.check_underflow(n + 1);
swap(stack[0], stack[n]);
}
void interpret_exch2(vm::Stack& stack) {
int n = stack.pop_smallint_range(255);
int m = stack.pop_smallint_range(255);
stack.check_underflow(std::max(m, n) + 1);
swap(stack[n], stack[m]);
}
void interpret_depth(vm::Stack& stack) {
stack.push_smallint(stack.depth());
}
void interpret_xchg0(vm::Stack& stack, int x) {
stack.check_underflow_p(x);
std::swap(stack.tos(), stack.at(x));
}
void interpret_xchg(vm::Stack& stack, int x, int y) {
stack.check_underflow_p(x, y);
std::swap(stack.at(x), stack.at(y));
}
void interpret_push(vm::Stack& stack, int x) {
stack.check_underflow_p(x);
stack.push(stack.fetch(x));
}
void interpret_pop(vm::Stack& stack, int x) {
stack.check_underflow_p(x);
std::swap(stack.tos(), stack.at(x));
stack.pop();
}
Ref<StackWord> dup_word_def{true, interpret_dup}, over_word_def{true, interpret_over},
drop_word_def{true, interpret_drop}, nip_word_def{true, interpret_nip}, swap_word_def{true, interpret_swap};
void interpret_make_xchg(vm::Stack& stack) {
using namespace std::placeholders;
int y = stack.pop_smallint_range(255), x = stack.pop_smallint_range(255);
if (x > y) {
std::swap(x, y);
}
if (x) {
stack.push_object(td::Ref<StackWord>{true, std::bind(interpret_xchg, _1, x, y)});
} else if (y <= 1) {
stack.push_object(y ? swap_word_def : nop_word_def);
} else {
stack.push_object(td::Ref<StackWord>{true, std::bind(interpret_xchg0, _1, y)});
}
}
void interpret_make_push(vm::Stack& stack) {
int x = stack.pop_smallint_range(255);
if (x <= 1) {
stack.push_object(x ? over_word_def : dup_word_def);
} else {
stack.push_object(td::Ref<StackWord>{true, std::bind(interpret_push, std::placeholders::_1, x)});
}
}
void interpret_make_pop(vm::Stack& stack) {
int x = stack.pop_smallint_range(255);
if (x <= 1) {
stack.push_object(x ? nip_word_def : drop_word_def);
} else {
stack.push_object(td::Ref<StackWord>{true, std::bind(interpret_pop, std::placeholders::_1, x)});
}
}
void interpret_is_string(vm::Stack& stack) {
stack.push_bool(stack.pop_chk().type() == vm::StackEntry::t_string);
}
int make_utf8_char(char buffer[4], int x) {
if (x < -0x80) {
return 0;
} else if (x < 0x80) {
buffer[0] = (char)x;
return 1;
} else if (x < 0x800) {
buffer[0] = (char)(0xc0 + (x >> 6));
buffer[1] = (char)(0x80 + (x & 0x3f));
return 2;
} else if (x < 0x10000) {
buffer[0] = (char)(0xe0 + (x >> 12));
buffer[1] = (char)(0x80 + ((x >> 6) & 0x3f));
buffer[2] = (char)(0x80 + (x & 0x3f));
return 3;
} else if (x < 0x200000) {
buffer[0] = (char)(0xf0 + (x >> 18));
buffer[1] = (char)(0x80 + ((x >> 12) & 0x3f));
buffer[2] = (char)(0x80 + ((x >> 6) & 0x3f));
buffer[3] = (char)(0x80 + (x & 0x3f));
return 4;
} else {
return 0;
}
}
void interpret_chr(vm::Stack& stack) {
char buffer[8];
unsigned len = make_utf8_char(buffer, stack.pop_smallint_range(0x10ffff, -128));
stack.push_string(std::string{buffer, len});
}
void interpret_hold(vm::Stack& stack) {
stack.check_underflow(2);
char buffer[8];
unsigned len = make_utf8_char(buffer, stack.pop_smallint_range(0x10ffff, -128));
std::string s = stack.pop_string();
s.append(buffer, len);
stack.push_string(std::move(s));
}
void interpret_emit(IntCtx& ctx) {
char buffer[8];
buffer[make_utf8_char(buffer, ctx.stack.pop_smallint_range(0x10ffff, -128))] = 0;
*ctx.output_stream << buffer;
}
void interpret_emit_const(IntCtx& ctx, char c) {
*ctx.output_stream << c;
}
void interpret_type(IntCtx& ctx) {
std::string s = ctx.stack.pop_string();
*ctx.output_stream << s;
}
void interpret_str_concat(vm::Stack& stack) {
std::string t = stack.pop_string();
stack.push_string(stack.pop_string() + t);
}
void interpret_str_equal(vm::Stack& stack) {
stack.check_underflow(2);
std::string t = stack.pop_string(), s = stack.pop_string();
stack.push_bool(s == t);
}
void interpret_str_cmp(vm::Stack& stack) {
stack.check_underflow(2);
std::string t = stack.pop_string(), s = stack.pop_string();
int res = s.compare(std::move(t));
stack.push_smallint((res > 0) - (res < 0));
}
void interpret_str_len(vm::Stack& stack) {
stack.push_smallint((long long)stack.pop_string().size());
}
void interpret_str_split(vm::Stack& stack) {
stack.check_underflow(2);
unsigned sz = stack.pop_smallint_range(0x7fffffff);
std::string str = stack.pop_string();
if (sz > str.size()) {
throw IntError{"not enough bytes for cutting"};
}
stack.push_string(std::string{str, 0, sz});
stack.push_string(std::string{str, sz});
}
void interpret_str_pos(vm::Stack& stack) {
auto s2 = stack.pop_string(), s1 = stack.pop_string();
auto pos = s1.find(s2);
stack.push_smallint(pos == std::string::npos ? -1 : static_cast<long long>(pos));
}
void interpret_str_reverse(vm::Stack& stack) {
std::string s = stack.pop_string();
auto it = s.begin();
while (it < s.end()) {
if ((*it & 0xc0) != 0xc0) {
++it;
} else {
auto it0 = it++;
while (it < s.end() && (*it & 0xc0) == 0x80) {
++it;
}
std::reverse(it0, it);
}
}
std::reverse(s.begin(), s.end());
stack.push_string(std::move(s));
}
void interpret_utf8_str_len(vm::Stack& stack) {
std::string s = stack.pop_string();
long long cnt = 0;
for (char c : s) {
if ((c & 0xc0) != 0x80) {
cnt++;
}
}
stack.push_smallint(cnt);
}
void interpret_utf8_str_split(vm::Stack& stack) {
stack.check_underflow(2);
unsigned c = stack.pop_smallint_range(0xffff);
std::string s = stack.pop_string();
if (c > s.size()) {
throw IntError{"not enough utf8 characters for cutting"};
}
auto it = s.begin();
for (; it < s.end(); ++it) {
if ((*it & 0xc0) != 0x80) {
if (!c) {
stack.push_string(std::string{s.begin(), it});
stack.push_string(std::string{it, s.end()});
return;
}
--c;
}
}
if (!c) {
stack.push_string(std::move(s));
stack.push_string(std::string{});
} else {
throw IntError{"not enough utf8 characters for cutting"};
}
}
void interpret_utf8_str_pos(vm::Stack& stack) {
auto s2 = stack.pop_string(), s1 = stack.pop_string();
auto pos = s1.find(s2);
if (pos == std::string::npos) {
stack.push_smallint(-1);
return;
}
int cnt = 0;
for (std::size_t i = 0; i < pos; i++) {
cnt += ((s1[i] & 0xc0) != 0x80);
}
stack.push_smallint(cnt);
}
void interpret_str_remove_trailing_int(vm::Stack& stack, int arg) {
char x = (char)(arg ? arg : stack.pop_long_range(127));
std::string s = stack.pop_string();
s.resize(s.find_last_not_of(x) + 1); // if not found, this expression will be 0
stack.push_string(std::move(s));
}
void interpret_bytes_len(vm::Stack& stack) {
stack.push_smallint((long long)stack.pop_bytes().size());
}
const char hex_digits[] = "0123456789abcdef";
const char HEX_digits[] = "0123456789ABCDEF";
static inline const char* hex_digits_table(bool upcase) {
return upcase ? HEX_digits : hex_digits;
}
void interpret_bytes_hex_print_raw(IntCtx& ctx, bool upcase) {
auto hex_digits = hex_digits_table(upcase);
std::string str = ctx.stack.pop_bytes();
for (unsigned c : str) {
*ctx.output_stream << hex_digits[(c >> 4) & 15] << hex_digits[c & 15];
}
}
void interpret_bytes_to_hex(vm::Stack& stack, bool upcase) {
auto hex_digits = hex_digits_table(upcase);
std::string str = stack.pop_bytes();
std::string t(str.size() * 2, 0);
for (std::size_t i = 0; i < str.size(); i++) {
unsigned c = str[i];
t[2 * i] = hex_digits[(c >> 4) & 15];
t[2 * i + 1] = hex_digits[c & 15];
}
stack.push_string(std::move(t));
}
void interpret_hex_to_bytes(vm::Stack& stack, bool partial) {
std::string str = stack.pop_string(), t;
if (!partial) {
if (str.size() & 1) {
throw IntError{"not a hex string"};
}
t.reserve(str.size() >> 1);
}
std::size_t i;
unsigned f = 0;
for (i = 0; i < str.size(); i++) {
int c = str[i];
if (c >= '0' && c <= '9') {
c -= '0';
} else {
c |= 0x20;
if (c >= 'a' && c <= 'f') {
c -= 'a' - 10;
} else {
if (!partial) {
throw IntError{"not a hex string"};
}
break;
}
}
f = (f << 4) + c;
if (i & 1) {
t += (char)(f & 0xff);
}
}
stack.push_bytes(t);
if (partial) {
stack.push_smallint(i & -2);
}
}
void interpret_bytes_split(vm::Stack& stack) {
stack.check_underflow(2);
unsigned sz = stack.pop_smallint_range(0x7fffffff);
std::string str = stack.pop_bytes();
if (sz > str.size()) {
throw IntError{"not enough bytes for cutting"};
}
stack.push_bytes(std::string{str, 0, sz});
stack.push_bytes(std::string{str, sz});
}
void interpret_bytes_concat(vm::Stack& stack) {
std::string t = stack.pop_bytes();
stack.push_bytes(stack.pop_bytes() + t);
}
void interpret_bytes_equal(vm::Stack& stack) {
stack.check_underflow(2);
std::string t = stack.pop_bytes(), s = stack.pop_bytes();
stack.push_bool(s == t);
}
void interpret_bytes_cmp(vm::Stack& stack) {
stack.check_underflow(2);
std::string t = stack.pop_bytes(), s = stack.pop_bytes();
int res = s.compare(std::move(t));
stack.push_smallint((res > 0) - (res < 0));
}
void interpret_bytes_fetch_int(vm::Stack& stack, int mode) {
stack.check_underflow(2);
unsigned bits = (unsigned)stack.pop_smallint_range(256 + (mode & 1));
std::string str = stack.pop_bytes();
if ((bits & 7)) {
throw IntError{"can load only an integer number of bytes"};
}
unsigned sz = bits >> 3;
if (str.size() < sz) {
throw IntError{"not enough bytes in the source"};
}
td::RefInt256 x{true};
bool ok;
const unsigned char* ptr = (const unsigned char*)(str.data());
if (!(mode & 0x10)) {
ok = x.write().import_bytes(ptr, sz, mode & 1);
} else {
ok = x.write().import_bytes_lsb(ptr, sz, mode & 1);
}
if (!ok) {
throw IntError{"cannot load integer"};
}
if (mode & 2) {
stack.push_bytes(std::string{str, sz});
}
stack.push_int(std::move(x));
}
void interpret_int_to_bytes(vm::Stack& stack, bool sgnd, bool lsb) {
stack.check_underflow(2);
unsigned bits = (unsigned)stack.pop_smallint_range(sgnd ? 264 : 256, 1);
td::RefInt256 x = stack.pop_int();
if ((bits & 7)) {
throw IntError{"can store only an integer number of bytes"};
}
unsigned sz = bits >> 3;
unsigned char buffer[33];
if (!(lsb ? x->export_bytes_lsb(buffer, sz, sgnd) : x->export_bytes(buffer, sz, sgnd))) {
throw IntError{"cannot store integer"};
}
stack.push_bytes(std::string{(char*)buffer, sz});
}
void interpret_string_to_bytes(vm::Stack& stack) {
stack.push_bytes(stack.pop_string());
}
void interpret_bytes_to_string(vm::Stack& stack) {
stack.push_string(stack.pop_bytes());
}
void interpret_bytes_hash(vm::Stack& stack, bool as_uint) {
std::string str = stack.pop_bytes();
unsigned char buffer[32];
digest::hash_str<digest::SHA256>(buffer, str.c_str(), str.size());
if (as_uint) {
td::RefInt256 x{true};
x.write().import_bytes(buffer, 32, false);
stack.push_int(std::move(x));
} else {
stack.push_bytes(std::string{(char*)buffer, 32});
}
}
void interpret_empty(vm::Stack& stack) {
stack.push(td::Ref<vm::CellBuilder>{true});
}
void interpret_store(vm::Stack& stack, bool sgnd) {
stack.check_underflow(3);
int bits = stack.pop_smallint_range(1023);
auto x = stack.pop_int();
auto cell = stack.pop_builder();
if (!cell.write().store_int256_bool(*x, bits, sgnd)) {
throw IntError{"integer does not fit into cell"};
}
stack.push(cell);
}
void interpret_store_str(vm::Stack& stack) {
stack.check_underflow(2);
auto str = stack.pop_string();
auto cell = stack.pop_builder();
if (!cell.write().store_bytes_bool(str)) {
throw IntError{"string does not fit into cell"};
}
stack.push(cell);
}
void interpret_store_bytes(vm::Stack& stack) {
stack.check_underflow(2);
auto str = stack.pop_bytes();
auto cell = stack.pop_builder();
if (!cell.write().store_bytes_bool(str)) {
throw IntError{"byte string does not fit into cell"};
}
stack.push(cell);
}
void interpret_string_to_cellslice(vm::Stack& stack) {
auto str = stack.pop_string();
vm::CellBuilder cb;
if (!cb.store_bytes_bool(str)) {
throw IntError{"string does not fit into cell"};
}
stack.push_cellslice(td::Ref<vm::CellSlice>{true, cb.finalize()});
}
void interpret_store_cellslice(vm::Stack& stack) {
stack.check_underflow(2);
auto cs = stack.pop_cellslice();
auto cb = stack.pop_builder();
if (!vm::cell_builder_add_slice_bool(cb.write(), *cs)) {
throw IntError{"slice does not fit into cell"};
}
stack.push(std::move(cb));
}
void interpret_store_cellslice_ref(vm::Stack& stack) {
stack.check_underflow(2);
auto cs = stack.pop_cellslice();
vm::CellBuilder cs_cell_builder;
vm::cell_builder_add_slice(cs_cell_builder, *cs);
auto cb = stack.pop_builder();
if (!cb.write().store_ref_bool(cs_cell_builder.finalize())) {
throw IntError{"cell reference list overflow"};
}
stack.push(std::move(cb));
}
void interpret_concat_cellslice(vm::Stack& stack) {
stack.check_underflow(2);
auto cs2 = stack.pop_cellslice();
auto cs1 = stack.pop_cellslice();
vm::CellBuilder cb;
if (vm::cell_builder_add_slice_bool(cb, *cs1) && vm::cell_builder_add_slice_bool(cb, *cs2)) {
stack.push_cellslice(td::Ref<vm::CellSlice>{true, cb.finalize()});
} else {
throw IntError{"concatenation of two slices does not fit into a cell"};
}
}
void interpret_concat_cellslice_ref(vm::Stack& stack) {
stack.check_underflow(2);
auto cs2 = stack.pop_cellslice();
auto cs1 = stack.pop_cellslice();
vm::CellBuilder builder1, builder2;
vm::cell_builder_add_slice(builder1, *cs1);
vm::cell_builder_add_slice(builder2, *cs2);
if (!builder1.store_ref_bool(builder2.finalize())) {
throw IntError{"cell reference list overflow"};
}
stack.push_cellslice(td::Ref<vm::CellSlice>{true, builder1.finalize()});
}
void interpret_concat_builders(vm::Stack& stack) {
stack.check_underflow(2);
auto cb2 = stack.pop_builder();
auto cb1 = stack.pop_builder();
if (!cb1.write().append_builder_bool(std::move(cb2))) {
throw IntError{"cannot concatenate two builders"};
}
stack.push_builder(std::move(cb1));
}
void interpret_cell_datasize(vm::Stack& stack, int mode) {
auto bound = (mode & 4 ? stack.pop_int() : td::make_refint(1 << 22));
Ref<vm::Cell> cell;
Ref<vm::CellSlice> cs;
if (mode & 2) {
cs = stack.pop_cellslice();
} else {
cell = stack.pop_maybe_cell();
}
if (!bound->is_valid() || bound->sgn() < 0) {
throw IntError{"finite non-negative integer expected"};
}
vm::VmStorageStat stat{bound->unsigned_fits_bits(63) ? bound->to_long() : (1ULL << 63) - 1};
bool ok = (mode & 2 ? stat.add_storage(cs.write()) : stat.add_storage(std::move(cell)));
if (ok) {
stack.push_smallint(stat.cells);
stack.push_smallint(stat.bits);
stack.push_smallint(stat.refs);
} else if (!(mode & 1)) {
throw IntError{"scanned too many cells"};
}
if (mode & 1) {
stack.push_bool(ok);
}
}
void interpret_slice_bitrefs(vm::Stack& stack, int mode) {
auto cs = stack.pop_cellslice();
if (mode & 1) {
stack.push_smallint(cs->size());
}
if (mode & 2) {
stack.push_smallint(cs->size_refs());
}
}
void interpret_builder_bitrefs(vm::Stack& stack, int mode) {
auto cb = stack.pop_builder();
if (mode & 1) {
stack.push_smallint(cb->size());
}
if (mode & 2) {
stack.push_smallint(cb->size_refs());
}
}
void interpret_builder_remaining_bitrefs(vm::Stack& stack, int mode) {
auto cb = stack.pop_builder();
if (mode & 1) {
stack.push_smallint(cb->remaining_bits());
}
if (mode & 2) {
stack.push_smallint(cb->remaining_refs());
}
}