-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbytesobj.c
1498 lines (1403 loc) · 44.3 KB
/
bytesobj.c
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
/*
$Id: bytesobj.c 3136 2024-05-11 09:05:50Z soci $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "bytesobj.h"
#include <string.h>
#include "math.h"
#include "eval.h"
#include "unicode.h"
#include "variables.h"
#include "arguments.h"
#include "error.h"
#include "boolobj.h"
#include "floatobj.h"
#include "codeobj.h"
#include "intobj.h"
#include "strobj.h"
#include "bitsobj.h"
#include "listobj.h"
#include "typeobj.h"
#include "noneobj.h"
#include "errorobj.h"
#include "addressobj.h"
#include "encobj.h"
static Type obj;
Type *const BYTES_OBJ = &obj;
static Bytes null_bytesval = { { &obj, 1 }, 0, null_bytesval.u.val, { { 0 } } };
static Bytes inv_bytesval = { { &obj, 1 }, ~0, inv_bytesval.u.val, { { 0 } } };
Obj *const null_bytes = &null_bytesval.v;
Obj *const inv_bytes = &inv_bytesval.v;
static Bytes *bytes_value[256];
static inline Bytes *ref_bytes(Bytes *v1) {
v1->v.refcount++; return v1;
}
static inline size_t byteslen(const Bytes *v1) {
ssize_t len = v1->len;
return (size_t)(len < 0 ? ~len : len);
}
static MUST_CHECK Obj *bytes_from_int(const Int *, linepos_t);
static MUST_CHECK Obj *bytes_from_u8(unsigned int i);
MUST_CHECK Obj *bytes_from_obj(Obj *v1, linepos_t epoint) {
Obj *err, *ret;
switch (v1->obj->type) {
case T_NONE:
case T_ERROR:
case T_BYTES: return val_reference(v1);
case T_BOOL: return bytes_from_u8(Bool(v1)->value ? 1 : 0);
case T_BITS: return bytes_from_bits(Bits(v1), epoint);
case T_STR: return bytes_from_str(Str(v1), epoint, BYTES_MODE_TEXT);
case T_INT: return bytes_from_int(Int(v1), epoint);
case T_CODE: return bytes_from_code(Code(v1), epoint);
case T_ADDRESS: return bytes_from_address(Address(v1), epoint);
case T_FLOAT:
err = int_from_float(Float(v1), epoint);
if (err->obj != INT_OBJ) return err;
ret = bytes_from_int(Int(err), epoint);
val_destroy(err);
return ret;
default: break;
}
return new_error_conv(v1, BYTES_OBJ, epoint);
}
static MUST_CHECK Obj *convert(oper_t op) {
return bytes_from_obj(op->v2, op->epoint2);
}
static FAST_CALL NO_INLINE void bytes_destroy(Bytes *v1) {
free(v1->data);
}
static FAST_CALL void destroy(Obj *o1) {
Bytes *v1 = Bytes(o1);
if unlikely(v1->u.val != v1->data) bytes_destroy(v1);
}
MUST_CHECK Bytes *new_bytes(size_t ln) {
Bytes *v = Bytes(val_alloc(BYTES_OBJ));
if (ln > sizeof v->u.val) {
v->u.s.max = ln;
v->u.s.hash = -1;
new_array(&v->data, ln);
} else {
v->data = v->u.val;
}
return v;
}
static MALLOC Bytes *new_bytes2(size_t ln) {
Bytes *v = Bytes(val_alloc(BYTES_OBJ));
if (ln > sizeof v->u.val) {
v->u.s.max = ln;
v->u.s.hash = -1;
v->data = allocate_array(uint8_t, ln);
if (v->data == NULL) {
val_destroy(Obj(v));
v = NULL;
}
} else {
v->data = v->u.val;
}
return v;
}
static uint8_t *extend_bytes(Bytes *v, size_t ln) {
uint8_t *tmp;
if (ln <= sizeof v->u.val) {
return v->u.val;
}
if (v->u.val != v->data) {
if (ln <= v->u.s.max) {
v->u.s.hash = -1;
return v->data;
}
tmp = reallocate_array(v->data, ln);
if (tmp == NULL) return tmp;
} else {
tmp = allocate_array(uint8_t, ln);
if (tmp == NULL) return tmp;
memcpy(tmp, v->u.val, byteslen(v));
}
v->data = tmp;
v->u.s.max = ln;
v->u.s.hash = -1;
return tmp;
}
static MUST_CHECK Obj *convert2(oper_t op) {
Funcargs *v2 = Funcargs(op->v2);
argcount_t args = v2->len;
Error *err;
ival_t ival;
uval_t len2;
size_t blen;
Obj *v;
Bytes *bytes, *bytes2;
bool inplace, bits;
if (args != 2) {
return new_error_argnum(args, 1, 2, op->epoint2);
}
v = v2->val[1].val;
err = v->obj->ival(v, &ival, 8 * sizeof ival - 3, &v2->val[1].epoint);
if (err != 0) return Obj(err);
v = v2->val[0].val;
inplace = (v->obj == BYTES_OBJ);
bits = (v->obj == BITS_OBJ);
if (!inplace) {
v = bytes_from_obj(v, op->epoint2);
if (v->obj != BYTES_OBJ) return v;
}
bytes = Bytes(v);
if (ival >= 0) {
len2 = (uval_t)ival;
if (!inplace && !bits && bytes->len < 0) {
val_destroy(Obj(bytes));
return new_error_obj(ERROR______NOT_UVAL, v2->val[0].val, &v2->val[0].epoint);
}
} else {
len2 = -(uval_t)ival;
}
blen = byteslen(bytes);
if (blen > len2 || (ival < 0 && blen == len2 && bytes->data[len2 - 1] >= 0x80)) {
if (!inplace) val_destroy(Obj(bytes));
err = new_error(ival < 0 ? ERROR____CANT_IVAL2 : ERROR____CANT_UVAL2, &v2->val[0].epoint);
err->u.intconv.bits = len2;
err->u.intconv.val = val_reference(v2->val[0].val);
return Obj(err);
}
if (blen == len2) {
return Obj(inplace ? ref_bytes(bytes) : bytes);
}
if (bytes->v.refcount == 1) {
if (extend_bytes(bytes, len2) == NULL) {
if (!inplace) val_destroy(Obj(bytes));
return new_error_mem(op->epoint3);
}
bytes2 = inplace ? ref_bytes(bytes) : bytes;
inplace = true;
} else {
bytes2 = new_bytes(len2);
if (blen != 0) memcpy(bytes2->data, bytes->data, blen);
}
memset(bytes2->data + blen, 0, len2 - blen);
bytes2->len = (ssize_t)(bytes->len < 0 ? ~(size_t)len2 : len2);
if (!inplace) val_destroy(Obj(bytes));
return Obj(bytes2);
}
static MUST_CHECK Obj *invert(const Bytes *v1, linepos_t epoint) {
size_t sz;
sz = byteslen(v1);
if (sz != 0) {
Bytes *v = new_bytes2(sz);
if (v == NULL) return new_error_mem(epoint);
v->len = ~v1->len;
memcpy(v->data, v1->data, sz);
return Obj(v);
}
return val_reference((v1->len < 0) ? null_bytes : inv_bytes);
}
static MUST_CHECK Obj *negate(Bytes *v1, linepos_t epoint) {
size_t i, sz = byteslen(v1);
Bytes *v;
if (v1->len == 0) return val_reference(Obj(v1));
if (v1->len < 0) {
for (i = 0; i < sz; i++) {
if (v1->data[i] != (uint8_t)~0) break;
}
if (i == sz) return NULL;
v = new_bytes2(sz);
if (v == NULL) goto failed;
v->data[i] = (uint8_t)(v1->data[i] + 1U);
} else {
for (i = 0; i < sz; i++) {
if (v1->data[i] != 0) break;
}
if (i == sz) return val_reference(Obj(v1));
v = new_bytes2(sz);
if (v == NULL) goto failed;
v->data[i] = (uint8_t)(v1->data[i] - 1U);
}
if (i != 0) memset(v->data, (v1->len < 0) ? 0 : ~0, i);
i++;
if (i < sz) memcpy(v->data + i, v1->data + i, sz - i);
v->len = ~v1->len;
return Obj(v);
failed:
return new_error_mem(epoint);
}
static FAST_CALL NO_INLINE bool bytes_same(const Bytes *v1, const Bytes *v2) {
return memcmp(v1->data, v2->data, byteslen(v2)) == 0;
}
static FAST_CALL bool same(const Obj *o1, const Obj *o2) {
const Bytes *v1 = Bytes(o1), *v2 = Bytes(o2);
if (o1->obj != o2->obj || v1->len != v2->len) return false;
switch (v1->len) {
case ~0:
case 0: return true;
case ~1:
case 1: return v1->data[0] == v2->data[0];
default: return bytes_same(v1, v2);
}
}
static bool to_bool(const Bytes *v1) {
size_t i, sz;
if (v1->len < 0) return true;
sz = byteslen(v1);
for (i = 0; i < sz; i++) {
if (v1->data[i] != 0) return true;
}
return false;
}
static MUST_CHECK Obj *truth(Obj *o1, Truth_types type, linepos_t UNUSED(epoint)) {
const Bytes *v1 = Bytes(o1);
size_t i, sz;
uint8_t inv;
switch (type) {
case TRUTH_ALL:
sz = byteslen(v1);
inv = (v1->len < 0) ? (uint8_t)~0 : 0;
for (i = 0; i < sz; i++) {
if (v1->data[i] == inv) return ref_false();
}
return ref_true();
case TRUTH_ANY:
if (v1->len == 0 || v1->len == ~0) return ref_false();
FALL_THROUGH; /* fall through */
default:
return truth_reference(to_bool(v1));
}
}
static uint8_t *z85_encode(uint8_t *dest, const uint8_t *src, size_t len) {
static const char *z85 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#";
size_t i;
for (i = 0; i < len; i += 4) {
const uint8_t *src2 = src + i;
uint32_t tmp;
unsigned int j;
tmp = (uint32_t)src2[3] | ((uint32_t)src2[2] << 8) | ((uint32_t)src2[1] << 16) | ((uint32_t)src2[0] << 24);
for (j = 4; j > 0; j--) {
uint32_t divided = tmp / 85;
dest[j] = (uint8_t)z85[tmp - divided * 85];
tmp = divided;
}
dest[j] = (uint8_t)z85[tmp];
dest += 5;
}
return dest;
}
static const uint8_t z85_dec[93] = {
68, 85, 84, 83, 82, 72, 85, 75, 76, 70, 65, 85, 63, 62, 69,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, 85, 73, 66, 74, 71,
81, 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, 77, 85, 78, 67, 85,
85, 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, 79, 85, 80
};
static const uint8_t *z85_decode(uint8_t *dest, const uint8_t *src, size_t len) {
size_t i;
for (i = 0; i < len; i += 4) {
uint32_t tmp;
unsigned int j;
tmp = z85_dec[src[0] - 33];
for (j = 1; j < 5; j++) {
tmp = tmp * 85 + z85_dec[src[j] - 33];
}
dest[i] = (uint8_t)(tmp >> 24);
dest[i+1] = (uint8_t)(tmp >> 16);
dest[i+2] = (uint8_t)(tmp >> 8);
dest[i+3] = (uint8_t)tmp;
src += 5;
}
return src;
}
static MUST_CHECK Obj *str(Obj *o1, linepos_t UNUSED(epoint), size_t maxsize) {
Bytes *v1 = Bytes(o1);
static const char *hex = "0123456789abcdef";
size_t i, len, len2, sz;
uint8_t *s, b;
Str *v;
sz = byteslen(v1);
if (add_overflow(sz, sz, &len2)) return NULL;
len = (v1->len < 0) ? 4 : 3;
if (inc_overflow(&len, len2)) return NULL;
if (len > maxsize) return NULL;
v = new_str2(len);
if (v == NULL) return NULL;
v->chars = len;
s = v->data;
if (v1->len < 0) *s++ = '~';
*s++ = 'x';
*s++ = '\'';
for (i = 0;i < sz; i++) {
b = v1->data[i];
*s++ = (uint8_t)hex[b >> 4];
*s++ = (uint8_t)hex[b & 0xf];
}
*s = '\'';
return Obj(v);
}
static MUST_CHECK Obj *repr(Obj *o1, linepos_t epoint, size_t maxsize) {
Bytes *v1 = Bytes(o1);
size_t len, len2, sz;
uint8_t *s;
Str *v;
sz = byteslen(v1);
if (sz <= 1) return str(o1, epoint, maxsize);
len2 = sz / 4 * 5;
if ((sz & 3) != 0) len2 += (sz & 3) + 1;
len = (v1->len < 0) ? 4 : 3;
if (inc_overflow(&len, len2) || sz > SIZE_MAX / 2) return NULL; /* overflow */
if (len > maxsize) return NULL;
v = new_str2(len);
if (v == NULL) return NULL;
v->chars = len;
s = v->data;
if (v1->len < 0) *s++ = '~';
*s++ = 'z';
*s++ = '\'';
s = z85_encode(s, v1->data, sz & ~3U);
if ((sz & 3) != 0) {
uint8_t tmp2[5], tmp[4] = {0, 0, 0, 0};
memcpy(tmp + 4 - (sz & 3), v1->data + (sz & ~3U), sz & 3);
sz &= 3;
z85_encode(tmp2, tmp, sz);
sz++;
memcpy(s, tmp2 + 5 - sz, sz);
s += sz;
}
*s = '\'';
return Obj(v);
}
static MUST_CHECK Obj *hash(Obj *o1, int *hs, linepos_t UNUSED(epoint)) {
Bytes *v1 = Bytes(o1);
size_t l = byteslen(v1);
const uint8_t *s2 = v1->data;
unsigned int h;
if (s2 != v1->u.val && v1->u.s.hash >= 0) {
*hs = v1->u.s.hash;
return NULL;
}
if (l == 0) {
*hs = 0;
return NULL;
}
h = (unsigned int)*s2 << 7;
while ((l--) != 0) h = (1000003 * h) ^ *s2++;
h ^= (unsigned int)v1->len;
h &= ((~0U) >> 1);
if (v1->data != v1->u.val) v1->u.s.hash = (int)h;
*hs = (int)h;
return NULL;
}
MUST_CHECK Obj *bytes_from_hexstr(const uint8_t *s, linecpos_t *ln, linepos_t epoint) {
Bytes *v;
linecpos_t i, j, spc;
uint8_t ch2, ch = s[0];
i = 1; j = (s[1] == 0x20) ? 2 : 0; spc = 0;
for (;;) {
if ((ch2 = s[i]) == 0) {
*ln = i;
return ref_none();
}
i++;
if (ch2 == ch) {
break; /* end of string; */
}
ch2 ^= 0x30;
if (ch2 < 10) continue;
ch2 = (uint8_t)((ch2 | 0x20) - 0x71);
if (ch2 >= 6 && j == 0) {
if (ch2 == 0xbf && ((i - spc) & 1) == 0) {
spc++;
continue;
}
j = i;
}
}
if (j == 0 && s[i - 2] == 0x20) j = i - 1;
*ln = i;
if (j != 0) {
struct linepos_s epoint2;
epoint2 = *epoint;
epoint2.pos += j;
err_msg2(ERROR______EXPECTED, "hex digit", &epoint2);
return ref_none();
}
i -= spc;
if ((i & 1) != 0) err_msg2(ERROR______EXPECTED, "even number of hex digits", epoint);
j = (i - 2) / 2;
v = new_bytes2(j);
if (v == NULL) return new_error_mem(epoint);
v->len = (ssize_t)j;
for (i = 0; i < j; i++) {
unsigned int c1, c2;
s++;
c1 = s[i] ^ 0x30;
if (c1 >= 10) {
if (c1 == 0x10) {i--; continue;}
c1 = (c1 | 0x20) - 0x67;
}
c2 = s[i+1] ^ 0x30;
if (c2 >= 10) c2 = (c2 | 0x20) - 0x67;
v->data[i] = (uint8_t)((c1 << 4) | c2);
}
return Obj(v);
}
MUST_CHECK Obj *bytes_from_z85str(const uint8_t *s, linecpos_t *ln, linepos_t epoint) {
Bytes *v;
linecpos_t i, j, sz;
uint8_t ch2, ch = s[0];
i = 1; j = 0;
for (;;) {
if ((ch2 = s[i]) == 0) {
*ln = i;
return ref_none();
}
i++;
if (ch2 == ch) {
break; /* end of string; */
}
if (j == 0 && (ch2 <= ' ' || ch2 >= '~' || z85_dec[ch2 - 33] == 85)) j = i;
}
*ln = i;
if (j != 0) {
struct linepos_s epoint2;
epoint2 = *epoint;
epoint2.pos += j;
err_msg2(ERROR______EXPECTED, "z85 character", &epoint2);
return ref_none();
}
i = (i > 1) ? (i - 2) : 0;
j = i / 5;
i -= j * 5;
j *= 4;
if (i == 1) {
err_msg2(ERROR______EXPECTED, "valid z85 string", epoint);
return ref_none();
}
sz = (i > 1) ? (j + i - 1) : j;
v = new_bytes2(sz);
if (v == NULL) return new_error_mem(epoint);
v->len = (ssize_t)sz;
s = z85_decode(v->data, s + 1, j);
if (i > 1) {
uint8_t tmp2[4], tmp[5] = {'0', '0', '0', '0', '0'};
memcpy(tmp + 5 - i, s, i);
i--;
z85_decode(tmp2, tmp, i);
memcpy(v->data + j, tmp2 + 4 - i, i);
if (memcmp(tmp2, "\x0\x0\x0\x0", 4 - i) != 0) {
err_msg2(ERROR______EXPECTED, "valid z85 string", epoint);
}
}
return Obj(v);
}
MUST_CHECK Obj *bytes_from_str(Str *v1, linepos_t epoint, Textconv_types mode) {
size_t len = v1->len, len2 = (mode == BYTES_MODE_PTEXT || mode == BYTES_MODE_NULL) ? 1 : 0;
uint8_t *s;
Bytes *v;
if (len != 0 || len2 != 0) {
struct encoder_s *encoder;
int ch;
if (actual_encoding->updating) {
if (v1->chars == 1) {
unichar_t ch2 = v1->data[0];
if ((ch2 & 0x80) != 0) utf8in(v1->data, &ch2);
return bytes_from_uval(ch2, ch2 < 256 ? 1 : ch2 < 65536 ? 2 : 3);
}
if (v1->chars != 0) return new_error_obj(ERROR__NOT_ONE_CHAR, Obj(v1), epoint);
return Obj(new_error(ERROR__EMPTY_STRING, epoint));
}
if (len < sizeof v->u.val) len = sizeof v->u.val;
if (len == 0) {
return val_reference(null_bytes);
}
v = new_bytes2(len);
if (v == NULL) goto failed;
s = v->data;
encoder = enc_string_init(actual_encoding, v1, epoint);
while ((ch = enc_string(encoder)) != EOF) {
if (len2 >= len) {
if (v->u.val == s) {
len = 32;
s = allocate_array(uint8_t, 32);
if (s == NULL) goto failed2;
v->data = s;
memcpy(s, v->u.val, len2);
v->u.s.hash = -1;
} else {
if (inc_overflow(&len, 1024)) goto failed2;
s = reallocate_array(s, len);
if (s == NULL) goto failed2;
v->data = s;
}
v->u.s.max = len;
}
switch (mode) {
case BYTES_MODE_SHIFT_CHECK:
case BYTES_MODE_SHIFT: if ((ch & 0x80) != 0) enc_error(encoder, ERROR___NO_HIGH_BIT); s[len2] = ch & 0x7f; break;
case BYTES_MODE_SHIFTL: if ((ch & 0x80) != 0) enc_error(encoder, ERROR___NO_HIGH_BIT); s[len2] = (uint8_t)(ch << 1); break;
case BYTES_MODE_NULL_CHECK:if (ch == 0) {enc_error(encoder, ERROR_NO_ZERO_VALUE); ch = 0xff;} s[len2] = (uint8_t)ch; break;
case BYTES_MODE_NULL: if (ch == 0) enc_error(encoder, ERROR_NO_ZERO_VALUE); s[len2 - 1] = (uint8_t)ch; break;
case BYTES_MODE_PTEXT:
case BYTES_MODE_TEXT: s[len2] = (uint8_t)ch; break;
}
len2++;
}
switch (mode) {
case BYTES_MODE_SHIFT: if (len2 != 0) s[len2 - 1] |= 0x80; else err_msg2(ERROR__BYTES_NEEDED, NULL, epoint); break;
case BYTES_MODE_SHIFTL: if (len2 != 0) s[len2 - 1] |= 0x01; else err_msg2(ERROR__BYTES_NEEDED, NULL, epoint);break;
case BYTES_MODE_NULL: s[len2 - 1] = 0x00; break;
case BYTES_MODE_PTEXT: s[0] = (uint8_t)(len2 - 1); if (len2 > 256) err_msg2(ERROR____PTEXT_LONG, &len2, epoint); break;
case BYTES_MODE_SHIFT_CHECK:
case BYTES_MODE_NULL_CHECK:
case BYTES_MODE_TEXT: break;
}
if (v->u.val != s) {
if (len2 <= sizeof v->u.val) {
if (len2 != 0) memcpy(v->u.val, s, len2);
free(s);
v->data = v->u.val;
} else if (len2 < len) {
uint8_t *s2 = reallocate_array(s, len2);
v->data = (s2 != NULL) ? s2 : s;
v->u.s.max = len2;
}
}
if (len2 > SSIZE_MAX) goto failed2; /* overflow */
v->len = (ssize_t)len2;
return Obj(v);
}
if (mode == BYTES_MODE_SHIFT || mode == BYTES_MODE_SHIFTL) err_msg2(ERROR__EMPTY_STRING, NULL, epoint);
return val_reference(null_bytes);
failed2:
val_destroy(Obj(v));
failed:
return new_error_mem(epoint);
}
static MUST_CHECK Obj *bytes_from_u8(unsigned int i) {
Bytes *v;
i &= 0xff;
v = bytes_value[i];
if (v == NULL) {
v = new_bytes(1);
v->len = 1;
v->data[0] = (uint8_t)i;
bytes_value[i] = v;
}
return Obj(ref_bytes(v));
}
MUST_CHECK Obj *bytes_from_uval(uval_t i, unsigned int bytes) {
Bytes *v = new_bytes(bytes);
v->len = (ssize_t)bytes;
switch (bytes) {
default: v->data[3] = (uint8_t)(i >> 24); FALL_THROUGH; /* fall through */
case 3: v->data[2] = (uint8_t)(i >> 16); FALL_THROUGH; /* fall through */
case 2: v->data[1] = (uint8_t)(i >> 8); FALL_THROUGH; /* fall through */
case 1: v->data[0] = (uint8_t)i; FALL_THROUGH; /* fall through */
case 0: break;
}
return Obj(v);
}
MUST_CHECK Obj *bytes_from_bits(const Bits *v1, linepos_t epoint) {
size_t i, sz, len1;
uint8_t *d;
Bytes *v;
bool inv = (v1->len < 0);
len1 = v1->bits;
if (len1 == 0) {
return val_reference(inv ? inv_bytes : null_bytes);
}
if (len1 <= 8 && !inv) {
return bytes_from_u8(v1->data[0]);
}
sz = len1 / 8;
if ((len1 % 8) != 0) sz++;
if (sz > SSIZE_MAX) goto failed; /* overflow */
v = new_bytes2(sz);
if (v == NULL) goto failed;
v->len = (ssize_t)(inv ? ~sz : sz);
d = v->data;
len1 = (size_t)(inv ? ~v1->len : v1->len);
i = 0;
if (len1 != 0) {
bdigit_t b = v1->data[0];
int bits = 0;
size_t j = 0;
while (sz > i) {
d[i++] = (uint8_t)(b >> bits);
if (bits == (8 * sizeof b) - 8) {
bits = 0;
j++;
if (j >= len1) break;
b = v1->data[j];
} else bits += 8;
}
}
if (sz > i) memset(d + i , 0, sz - i);
return Obj(v);
failed:
return new_error_mem(epoint);
}
static MUST_CHECK Obj *bytes_from_int(const Int *v1, linepos_t epoint) {
bool inv, c;
size_t i, sz;
uint8_t *d;
digit_t b2;
const digit_t *b;
Bytes *v;
switch (v1->len) {
case -1:
if (v1->data[0] == 1) return val_reference(inv_bytes);
break;
case 0:
return val_reference(null_bytes);
case 1:
if (v1->data[0] < 256) return bytes_from_u8(v1->data[0]);
break;
default:
break;
}
inv = (v1->len < 0);
sz = (size_t)(inv ? -v1->len : v1->len);
if (sz > SSIZE_MAX / sizeof *b) goto failed; /* overflow */
sz *= sizeof *b;
v = new_bytes2(sz);
if (v == NULL) goto failed;
d = v->data;
b = v1->data;
c = inv;
b2 = 0;
for (i = 0; i < sz; i++) {
if (i % sizeof *b == 0) {
b2 = b[i / sizeof *b];
if (c) {
c = (b2 == 0);
b2--;
}
} else b2 >>= 8;
d[i] = (uint8_t)b2;
}
while (sz != 0 && d[sz - 1] == 0) sz--;
if (sz > SSIZE_MAX) goto failed2; /* overflow */
v->len = (ssize_t)(inv ? ~sz : sz);
if (v->u.val != d) {
if (sz <= sizeof v->u.val) {
if (sz != 0) memcpy(v->u.val, d, sz);
free(d);
v->data = v->u.val;
} else if (sz < i) {
uint8_t *d2 = reallocate_array(d, sz);
v->data = (d2 != NULL) ? d2 : d;
v->u.s.max = sz;
}
}
return Obj(v);
failed2:
val_destroy(Obj(v));
failed:
return new_error_mem(epoint);
}
static bool uvalx(Obj *o1, uval_t *uv, unsigned int bits) {
Bytes *v1 = Bytes(o1);
size_t ln = byteslen(v1);
uval_t u;
if (ln > bits / 8) return false;
u = 0;
while (ln != 0) u = (u << 8) | v1->data[--ln];
*uv = (v1->len < 0) ? ~u : u;
return true;
}
static MUST_CHECK Error *ival(Obj *o1, ival_t *iv, unsigned int bits, linepos_t epoint) {
Error *v;
if (uvalx(o1, (uval_t *)iv, bits)) return NULL;
v = new_error(ERROR_____CANT_IVAL, epoint);
v->u.intconv.bits = bits;
v->u.intconv.val = val_reference(o1);
return v;
}
static MUST_CHECK Error *uval(Obj *o1, uval_t *uv, unsigned int bits, linepos_t epoint) {
Error *v;
if (uvalx(o1, uv, bits)) return NULL;
v = new_error(ERROR_____CANT_UVAL, epoint);
v->u.intconv.bits = bits;
v->u.intconv.val = val_reference(o1);
return v;
}
static MUST_CHECK Error *uval2(Obj *o1, uval_t *uv, unsigned int bits, linepos_t epoint) {
if (Bytes(o1)->len < 0) return Error(new_error_obj(ERROR______NOT_UVAL, o1, epoint));
return uval(o1, uv, bits, epoint);
}
MUST_CHECK Obj *float_from_bytes(const Bytes *v1, linepos_t epoint) {
double d;
size_t i, len1;
switch (v1->len) {
case ~3: d = -1.0 - (double)(v1->data[0] + v1->data[1] * 256 + v1->data[2] * 65536); break;
case ~2: d = -1.0 - (double)(v1->data[0] + v1->data[1] * 256); break;
case ~1: d = -1.0 - v1->data[0]; break;
case ~0: d = -1.0; break;
case 0: d = 0.0; break;
case 1: d = v1->data[0]; break;
case 2: d = (v1->data[0] + v1->data[1] * 256); break;
case 3: d = (v1->data[0] + v1->data[1] * 256 + v1->data[2] * 65536); break;
default:
d = ((v1->len < 0) ? 1 : 0) + v1->data[0];
len1 = byteslen(v1);
for (i = 1; i < len1; i++) {
d += ldexp(v1->data[i], (int)(i * 8));
}
if (v1->len < 0) d = -d;
return float_from_double(d, epoint);
}
return new_float(d);
}
static MUST_CHECK Obj *sign(Obj *o1, linepos_t UNUSED(epoint)) {
Bytes *v1 = Bytes(o1);
size_t i, sz;
if (v1->len < 0) return val_reference(minus1_value);
sz = byteslen(v1);
for (i = 0; i < sz; i++) {
if (v1->data[i] != 0) return val_reference(int_value[1]);
}
return val_reference(int_value[0]);
}
static MUST_CHECK Obj *function(oper_t op) {
Bytes *v1 = Bytes(op->v2);
Obj *tmp, *ret;
op->v2 = tmp = int_from_bytes(v1, op->epoint2);
op->inplace = tmp->refcount == 1 ? tmp : NULL;
ret = tmp->obj->function(op);
val_destroy(tmp);
return ret;
}
static MUST_CHECK Obj *len(oper_t op) {
Bytes *v1 = Bytes(op->v2);
return int_from_size(byteslen(v1));
}
static FAST_CALL MUST_CHECK Obj *iter_element(struct iter_s *v1, size_t i) {
Bytes *iter = Bytes(v1->iter);
const Bytes *vv1 = Bytes(v1->data);
if (iter->v.refcount != 1) {
iter->v.refcount--;
iter = new_bytes(1);
v1->iter = Obj(iter);
iter->len = 1;
}
iter->data[0] = (vv1->len < 0) ? (uint8_t)~vv1->data[i] : vv1->data[i];
return Obj(iter);
}
static FAST_CALL MUST_CHECK Obj *iter_forward(struct iter_s *v1) {
if (v1->val >= v1->len) return NULL;
return iter_element(v1, v1->val++);
}
static void getiter(struct iter_s *v) {
v->iter = val_reference(v->data);
v->val = 0;
v->data = val_reference(v->data);
v->next = iter_forward;
v->len = byteslen(Bytes(v->data));
}
static FAST_CALL MUST_CHECK Obj *iter_reverse(struct iter_s *v1) {
if (v1->val >= v1->len) return NULL;
return iter_element(v1, v1->len - ++v1->val);
}
static void getriter(struct iter_s *v) {
v->iter = val_reference(v->data);
v->val = 0;
v->data = val_reference(v->data);
v->next = iter_reverse;
v->len = byteslen(Bytes(v->data));
}
static inline MUST_CHECK Obj *binary(oper_t op) {
Bytes *vv1 = Bytes(op->v1), *vv2 = Bytes(op->v2);
size_t i, len1, len2, sz;
bool neg1, neg2, neg;
const uint8_t *v1, *v2;
uint8_t *v;
Bytes *vv;
len1 = byteslen(vv1); len2 = byteslen(vv2);
if (len1 < len2) {
Bytes *tmp = vv1; vv1 = vv2; vv2 = tmp;
sz = len1; len1 = len2; len2 = sz;
}
neg1 = vv1->len < 0; neg2 = vv2->len < 0;
switch (op->op) {
case O_AND:
neg = neg1 && neg2;
sz = neg2 ? len1 : len2;
break;
case O_OR:
neg = neg1 || neg2;
sz = neg2 ? len2 : len1;
break;
default:
neg = neg1 != neg2;
sz = len1;
break;
}
if (sz == 0) return val_reference(neg ? inv_bytes : null_bytes);
if (op->inplace == Obj(vv1)) {
vv = ref_bytes(vv1);
if (vv->data != vv->u.val) vv->u.s.hash = -1;
} else if (op->inplace == Obj(vv2) && sz <= len2) {
vv = ref_bytes(vv2);
if (vv->data != vv->u.val) vv->u.s.hash = -1;
} else {
vv = new_bytes2(sz);
if (vv == NULL) return new_error_mem(op->epoint3);
}
v = vv->data;
v1 = vv1->data; v2 = vv2->data;
i = 0;
switch (op->op) {
case O_AND:
if (neg1) {
if (neg2) {
for (; i < len2; i++) v[i] = v1[i] | v2[i];
} else {
for (; i < len2; i++) v[i] = (uint8_t)(~v1[i] & v2[i]);
}
} else {
if (neg2) {
for (; i < len2; i++) v[i] = (uint8_t)(v1[i] & ~v2[i]);
} else {
for (; i < len2; i++) v[i] = v1[i] & v2[i];
}
}
break;
case O_OR:
if (neg1) {
if (neg2) {
for (; i < len2; i++) v[i] = v1[i] & v2[i];
} else {
for (; i < len2; i++) v[i] = (uint8_t)(v1[i] & ~v2[i]);
}
} else {
if (neg2) {
for (; i < len2; i++) v[i] = (uint8_t)(~v1[i] & v2[i]);
} else {
for (; i < len2; i++) v[i] = v1[i] | v2[i];
}
}
break;
default:
for (; i < len2; i++) v[i] = v1[i] ^ v2[i];
break;
}
for (; i < sz; i++) v[i] = v1[i];
/*if (i > SSIZE_MAX) err_msg_out_of_memory();*/ /* overflow */
vv->len = (ssize_t)(neg ? ~i : i);
return Obj(vv);
}
static MUST_CHECK Obj *concat(oper_t op) {
Bytes *v1 = Bytes(op->v1), *v2 = Bytes(op->v2), *v;
uint8_t *s;
bool inv;
size_t ln, i, len1, len2;
if (v2->len == 0 || v2->len == ~(ssize_t)0) {
return Obj(ref_bytes(v1));
}
inv = (v1->len ^ v2->len) < 0;
if ((v1->len == 0 || v1->len == ~(ssize_t)0) && !inv) {
return Obj(ref_bytes(v2));
}
len1 = byteslen(v1);
len2 = byteslen(v2);
if (add_overflow(len1, len2, &ln) || ln > SSIZE_MAX) goto failed; /* overflow */
if (op->inplace == Obj(v1)) {
size_t ln2;
if (ln > sizeof v1->u.val && v1->u.val != v1->data && ln > v1->u.s.max) {
ln2 = ln + (ln < 1024 ? ln : 1024);
if (ln2 < ln) ln2 = ln;
} else ln2 = ln;
s = extend_bytes(v1, ln2);
if (s == NULL) goto failed;
v = ref_bytes(v1);
} else if (op->inplace == Obj(v2)) {
size_t ln2;
if (ln > sizeof v2->u.val && v2->u.val != v2->data && ln > v2->u.s.max) {
ln2 = ln + (ln < 1024 ? ln : 1024);
if (ln2 < ln) ln2 = ln;
} else ln2 = ln;
s = extend_bytes(v2, ln2);