-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.c
1328 lines (1167 loc) · 26.1 KB
/
marshal.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
/**********************************************************************
marshal.c -
$Author$
$Date$
created at: Thu Apr 27 16:30:01 JST 1995
Copyright (C) 1993-2003 Yukihiro Matsumoto
**********************************************************************/
#include "ruby.h"
#include "rubyio.h"
#include "st.h"
#include "util.h"
#include <math.h>
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#define BITSPERSHORT (2*CHAR_BIT)
#define SHORTMASK ((1<<BITSPERSHORT)-1)
#define SHORTDN(x) RSHIFT(x,BITSPERSHORT)
#if SIZEOF_SHORT == SIZEOF_BDIGITS
#define SHORTLEN(x) (x)
#else
static int
shortlen(len, ds)
long len;
BDIGIT *ds;
{
BDIGIT num;
int offset = 0;
num = ds[len-1];
while (num) {
num = SHORTDN(num);
offset++;
}
return (len - 1)*sizeof(BDIGIT)/2 + offset;
}
#define SHORTLEN(x) shortlen((x),d)
#endif
#define MARSHAL_MAJOR 4
#define MARSHAL_MINOR 8
#define TYPE_NIL '0'
#define TYPE_TRUE 'T'
#define TYPE_FALSE 'F'
#define TYPE_FIXNUM 'i'
#define TYPE_EXTENDED 'e'
#define TYPE_UCLASS 'C'
#define TYPE_OBJECT 'o'
#define TYPE_DATA 'd'
#define TYPE_USERDEF 'u'
#define TYPE_USRMARSHAL 'U'
#define TYPE_FLOAT 'f'
#define TYPE_BIGNUM 'l'
#define TYPE_STRING '"'
#define TYPE_REGEXP '/'
#define TYPE_ARRAY '['
#define TYPE_HASH '{'
#define TYPE_HASH_DEF '}'
#define TYPE_STRUCT 'S'
#define TYPE_MODULE_OLD 'M'
#define TYPE_CLASS 'c'
#define TYPE_MODULE 'm'
#define TYPE_SYMBOL ':'
#define TYPE_SYMLINK ';'
#define TYPE_IVAR 'I'
#define TYPE_LINK '@'
static ID s_dump, s_load;
static ID s_dump_data, s_load_data, s_alloc;
static ID s_getc, s_read, s_write, s_binmode;
struct dump_arg {
VALUE obj;
VALUE str, dest;
st_table *symbol;
st_table *data;
int taint;
};
struct dump_call_arg {
VALUE obj;
struct dump_arg *arg;
int limit;
};
static void w_long _((long, struct dump_arg*));
static void
w_nbyte(s, n, arg)
char *s;
int n;
struct dump_arg *arg;
{
VALUE buf = arg->str;
rb_str_buf_cat(buf, s, n);
if (arg->dest && RSTRING(buf)->len >= BUFSIZ) {
if (arg->taint) OBJ_TAINT(buf);
rb_io_write(arg->dest, buf);
rb_str_resize(buf, 0);
}
}
static void
w_byte(c, arg)
char c;
struct dump_arg *arg;
{
w_nbyte(&c, 1, arg);
}
static void
w_bytes(s, n, arg)
char *s;
int n;
struct dump_arg *arg;
{
w_long(n, arg);
w_nbyte(s, n, arg);
}
static void
w_short(x, arg)
int x;
struct dump_arg *arg;
{
w_byte((x >> 0) & 0xff, arg);
w_byte((x >> 8) & 0xff, arg);
}
static void
w_long(x, arg)
long x;
struct dump_arg *arg;
{
char buf[sizeof(long)+1];
int i, len = 0;
#if SIZEOF_LONG > 4
if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
/* big long does not fit in 4 bytes */
rb_raise(rb_eTypeError, "long too big to dump");
}
#endif
if (x == 0) {
w_byte(0, arg);
return;
}
if (0 < x && x < 123) {
w_byte(x + 5, arg);
return;
}
if (-124 < x && x < 0) {
w_byte((x - 5)&0xff, arg);
return;
}
for (i=1;i<sizeof(long)+1;i++) {
buf[i] = x & 0xff;
x = RSHIFT(x,8);
if (x == 0) {
buf[0] = i;
break;
}
if (x == -1) {
buf[0] = -i;
break;
}
}
len = i;
for (i=0;i<=len;i++) {
w_byte(buf[i], arg);
}
}
#ifdef DBL_MANT_DIG
static int
save_mantissa(d, buf)
double d;
char *buf;
{
int e, m;
m = (int)(modf(ldexp(frexp(fabs(d), &e), DBL_MANT_DIG - 16), &d) * 0x10000);
*buf++ = 0;
*buf++ = m >> 8;
*buf++ = m;
return 3;
}
static double
load_mantissa(d, buf, len)
double d;
const char *buf;
int len;
{
if (len > 0) {
int e, s = d < 0, dig = len << 3;
unsigned long m = 0;
modf(ldexp(frexp(fabs(d), &e), DBL_MANT_DIG - dig), &d);
do {m = (m << 8) | (*buf++ & 0xff);} while (--len);
d = ldexp(frexp(d + ldexp((double)m, -dig), &dig), e);
if (s) d = -d;
}
return d;
}
#else
#define load_mantissa(d, buf, len) (d)
#define save_mantissa(d, buf) 0
#endif
#ifdef DBL_DIG
#define FLOAT_DIG (DBL_DIG+1)
#else
#define FLOAT_DIG 17
#endif
static void
w_float(d, arg)
double d;
struct dump_arg *arg;
{
char buf[100];
if (isinf(d)) {
if (d < 0) strcpy(buf, "-inf");
else strcpy(buf, "inf");
}
else if (isnan(d)) {
strcpy(buf, "nan");
}
else if (d == 0.0) {
if (1.0/d < 0) strcpy(buf, "-0");
else strcpy(buf, "0");
}
else {
int len;
/* xxx: should not use system's sprintf(3) */
sprintf(buf, "%.*g", FLOAT_DIG, d);
len = strlen(buf);
w_bytes(buf, len + save_mantissa(d, buf + len), arg);
return;
}
w_bytes(buf, strlen(buf), arg);
}
static void
w_symbol(id, arg)
ID id;
struct dump_arg *arg;
{
char *sym = rb_id2name(id);
long num;
if (st_lookup(arg->symbol, id, &num)) {
w_byte(TYPE_SYMLINK, arg);
w_long(num, arg);
}
else {
w_byte(TYPE_SYMBOL, arg);
w_bytes(sym, strlen(sym), arg);
st_add_direct(arg->symbol, id, arg->symbol->num_entries);
}
}
static void
w_unique(s, arg)
char *s;
struct dump_arg *arg;
{
if (s[0] == '#') {
rb_raise(rb_eArgError, "can't dump anonymous class %s", s);
}
w_symbol(rb_intern(s), arg);
}
static void w_object _((VALUE,struct dump_arg*,int));
static int
hash_each(key, value, arg)
VALUE key, value;
struct dump_call_arg *arg;
{
w_object(key, arg->arg, arg->limit);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
static int
obj_each(id, value, arg)
ID id;
VALUE value;
struct dump_call_arg *arg;
{
w_symbol(id, arg->arg);
w_object(value, arg->arg, arg->limit);
return ST_CONTINUE;
}
static void
w_extended(klass, arg)
VALUE klass;
struct dump_arg *arg;
{
char *path;
if (FL_TEST(klass, FL_SINGLETON)) {
if (RCLASS(klass)->m_tbl->num_entries ||
(RCLASS(klass)->iv_tbl && RCLASS(klass)->iv_tbl->num_entries > 1)) {
rb_raise(rb_eTypeError, "singleton can't be dumped");
}
klass = RCLASS(klass)->super;
}
while (BUILTIN_TYPE(klass) == T_ICLASS) {
path = rb_class2name(RBASIC(klass)->klass);
w_byte(TYPE_EXTENDED, arg);
w_unique(path, arg);
klass = RCLASS(klass)->super;
}
}
static void
w_class(type, obj, arg)
int type;
VALUE obj;
struct dump_arg *arg;
{
char *path;
VALUE klass = CLASS_OF(obj);
w_extended(klass, arg);
w_byte(type, arg);
path = rb_class2name(klass);
w_unique(path, arg);
}
static void
w_uclass(obj, base_klass, arg)
VALUE obj, base_klass;
struct dump_arg *arg;
{
VALUE klass = CLASS_OF(obj);
w_extended(klass, arg);
if (klass != base_klass) {
w_byte(TYPE_UCLASS, arg);
w_unique(rb_obj_classname(obj), arg);
}
}
static void
w_ivar(tbl, arg)
st_table *tbl;
struct dump_call_arg *arg;
{
if (tbl) {
w_long(tbl->num_entries, arg->arg);
st_foreach(tbl, obj_each, (st_data_t)arg);
}
else {
w_long(0, arg->arg);
}
}
static void
w_object(obj, arg, limit)
VALUE obj;
struct dump_arg *arg;
int limit;
{
struct dump_call_arg c_arg;
st_table *ivtbl = 0;
if (limit == 0) {
rb_raise(rb_eArgError, "exceed depth limit");
}
if (obj == Qnil) {
w_byte(TYPE_NIL, arg);
}
else if (obj == Qtrue) {
w_byte(TYPE_TRUE, arg);
}
else if (obj == Qfalse) {
w_byte(TYPE_FALSE, arg);
}
else if (FIXNUM_P(obj)) {
#if SIZEOF_LONG <= 4
w_byte(TYPE_FIXNUM, arg);
w_long(FIX2INT(obj), arg);
#else
if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
w_byte(TYPE_FIXNUM, arg);
w_long(FIX2LONG(obj), arg);
}
else {
w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
return;
}
#endif
}
else if (SYMBOL_P(obj)) {
w_symbol(SYM2ID(obj), arg);
return;
}
else {
long num;
limit--;
c_arg.limit = limit;
c_arg.arg = arg;
if (st_lookup(arg->data, obj, &num)) {
w_byte(TYPE_LINK, arg);
w_long(num, arg);
return;
}
if (OBJ_TAINTED(obj)) arg->taint = Qtrue;
if (ivtbl = rb_generic_ivar_table(obj)) {
w_byte(TYPE_IVAR, arg);
}
st_add_direct(arg->data, obj, arg->data->num_entries);
if (rb_respond_to(obj, s_dump)) {
VALUE v;
w_class(TYPE_USERDEF, obj, arg);
v = rb_funcall(obj, s_dump, 1, INT2NUM(limit));
if (TYPE(v) != T_STRING) {
rb_raise(rb_eTypeError, "_dump() must return String");
}
w_bytes(RSTRING(v)->ptr, RSTRING(v)->len, arg);
if (ivtbl) w_ivar(ivtbl, &c_arg);
return;
}
switch (BUILTIN_TYPE(obj)) {
case T_CLASS:
if (FL_TEST(obj, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "singleton class can't be dumped");
}
w_byte(TYPE_CLASS, arg);
{
VALUE path = rb_class_path(obj);
if (RSTRING(path)->ptr[0] == '#') {
rb_raise(rb_eArgError, "can't dump anonymous class %s",
RSTRING(path)->ptr);
}
w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
}
break;
case T_MODULE:
w_byte(TYPE_MODULE, arg);
{
VALUE path = rb_class_path(obj);
if (RSTRING(path)->ptr[0] == '#') {
rb_raise(rb_eArgError, "can't dump anonymous module %s",
RSTRING(path)->ptr);
}
w_bytes(RSTRING(path)->ptr, RSTRING(path)->len, arg);
}
break;
case T_FLOAT:
w_byte(TYPE_FLOAT, arg);
w_float(RFLOAT(obj)->value, arg);
break;
case T_BIGNUM:
w_byte(TYPE_BIGNUM, arg);
{
char sign = RBIGNUM(obj)->sign ? '+' : '-';
long len = RBIGNUM(obj)->len;
BDIGIT *d = RBIGNUM(obj)->digits;
w_byte(sign, arg);
w_long(SHORTLEN(len), arg); /* w_short? */
while (len--) {
#if SIZEOF_BDIGITS > SIZEOF_SHORT
BDIGIT num = *d;
int i;
for (i=0; i<SIZEOF_BDIGITS; i+=SIZEOF_SHORT) {
w_short(num & SHORTMASK, arg);
num = SHORTDN(num);
if (len == 0 && num == 0) break;
}
#else
w_short(*d, arg);
#endif
d++;
}
}
break;
case T_STRING:
w_uclass(obj, rb_cString, arg);
w_byte(TYPE_STRING, arg);
w_bytes(RSTRING(obj)->ptr, RSTRING(obj)->len, arg);
break;
case T_REGEXP:
w_uclass(obj, rb_cRegexp, arg);
w_byte(TYPE_REGEXP, arg);
w_bytes(RREGEXP(obj)->str, RREGEXP(obj)->len, arg);
w_byte(rb_reg_options(obj), arg);
break;
case T_ARRAY:
w_uclass(obj, rb_cArray, arg);
w_byte(TYPE_ARRAY, arg);
{
long len = RARRAY(obj)->len;
VALUE *ptr = RARRAY(obj)->ptr;
w_long(len, arg);
while (len--) {
w_object(*ptr, arg, limit);
ptr++;
}
}
break;
case T_HASH:
w_uclass(obj, rb_cHash, arg);
if (NIL_P(RHASH(obj)->ifnone)) {
w_byte(TYPE_HASH, arg);
}
else if (FL_TEST(obj, FL_USER2)) {
/* FL_USER2 means HASH_PROC_DEFAULT (see hash.c) */
rb_raise(rb_eArgError, "cannot dump hash with default proc");
}
else {
w_byte(TYPE_HASH_DEF, arg);
}
w_long(RHASH(obj)->tbl->num_entries, arg);
st_foreach(RHASH(obj)->tbl, hash_each, (st_data_t)&c_arg);
if (!NIL_P(RHASH(obj)->ifnone)) {
w_object(RHASH(obj)->ifnone, arg, limit);
}
break;
case T_STRUCT:
w_class(TYPE_STRUCT, obj, arg);
{
long len = RSTRUCT(obj)->len;
VALUE mem;
long i;
w_long(len, arg);
mem = rb_struct_iv_get(rb_obj_class(obj), "__member__");
if (mem == Qnil) {
rb_raise(rb_eTypeError, "uninitialized struct");
}
for (i=0; i<len; i++) {
w_symbol(SYM2ID(RARRAY(mem)->ptr[i]), arg);
w_object(RSTRUCT(obj)->ptr[i], arg, limit);
}
}
break;
case T_OBJECT:
w_class(TYPE_OBJECT, obj, arg);
w_ivar(ROBJECT(obj)->iv_tbl, &c_arg);
break;
case T_DATA:
{
VALUE v;
w_class(TYPE_DATA, obj, arg);
if (!rb_respond_to(obj, s_dump_data)) {
rb_raise(rb_eTypeError,
"class %s needs to have instance method `_dump_data'",
rb_obj_classname(obj));
}
v = rb_funcall(obj, s_dump_data, 0);
w_object(v, arg, limit);
}
break;
default:
rb_raise(rb_eTypeError, "can't dump %s",
rb_obj_classname(obj));
break;
}
}
if (ivtbl) {
w_ivar(ivtbl, &c_arg);
}
}
static VALUE
dump(arg)
struct dump_call_arg *arg;
{
w_object(arg->obj, arg->arg, arg->limit);
if (arg->arg->dest) {
rb_io_write(arg->arg->dest, arg->arg->str);
rb_str_resize(arg->arg->str, 0);
}
return 0;
}
static VALUE
dump_ensure(arg)
struct dump_arg *arg;
{
st_free_table(arg->symbol);
st_free_table(arg->data);
if (arg->taint) {
OBJ_TAINT(arg->str);
}
return 0;
}
static VALUE
marshal_dump(argc, argv)
int argc;
VALUE* argv;
{
VALUE obj, port, a1, a2;
int limit = -1;
struct dump_arg arg;
struct dump_call_arg c_arg;
port = Qnil;
rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
if (argc == 3) {
if (!NIL_P(a2)) limit = NUM2INT(a2);
if (NIL_P(a1)) goto type_error;
port = a1;
}
else if (argc == 2) {
if (FIXNUM_P(a1)) limit = FIX2INT(a1);
else if (NIL_P(a1)) goto type_error;
else port = a1;
}
arg.dest = 0;
if (!NIL_P(port)) {
if (!rb_respond_to(port, s_write)) {
type_error:
rb_raise(rb_eTypeError, "instance of IO needed");
}
arg.str = rb_str_buf_new(0);
arg.dest = port;
if (rb_respond_to(port, s_binmode)) {
rb_funcall2(port, s_binmode, 0, 0);
}
}
else {
port = rb_str_buf_new(0);
arg.str = port;
}
arg.symbol = st_init_numtable();
arg.data = st_init_numtable();
arg.taint = Qfalse;
c_arg.obj = obj;
c_arg.arg = &arg;
c_arg.limit = limit;
w_byte(MARSHAL_MAJOR, &arg);
w_byte(MARSHAL_MINOR, &arg);
rb_ensure(dump, (VALUE)&c_arg, dump_ensure, (VALUE)&arg);
return port;
}
struct load_arg {
char *ptr, *end;
st_table *symbol;
VALUE data;
VALUE proc;
int taint;
};
static VALUE r_object _((struct load_arg *arg));
static int
r_byte(arg)
struct load_arg *arg;
{
int c;
if (!arg->end) {
VALUE src = (VALUE)arg->ptr;
VALUE v = rb_funcall2(src, s_getc, 0, 0);
if (NIL_P(v)) rb_eof_error();
c = (unsigned char)FIX2INT(v);
}
else if (arg->ptr < arg->end) {
c = *(unsigned char*)arg->ptr++;
}
else {
rb_raise(rb_eArgError, "marshal data too short");
}
return c;
}
static void
long_toobig(size)
int size;
{
rb_raise(rb_eTypeError, "long too big for this architecture (size %d, given %d)",
sizeof(long), size);
}
#undef SIGN_EXTEND_CHAR
#if __STDC__
# define SIGN_EXTEND_CHAR(c) ((signed char)(c))
#else /* not __STDC__ */
/* As in Harbison and Steele. */
# define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
#endif
static long
r_long(arg)
struct load_arg *arg;
{
register long x;
int c = SIGN_EXTEND_CHAR(r_byte(arg));
long i;
if (c == 0) return 0;
if (c > 0) {
if (4 < c && c < 128) {
return c - 5;
}
if (c > sizeof(long)) long_toobig(c);
x = 0;
for (i=0;i<c;i++) {
x |= (long)r_byte(arg) << (8*i);
}
}
else {
if (-129 < c && c < -4) {
return c + 5;
}
c = -c;
if (c > sizeof(long)) long_toobig(c);
x = -1;
for (i=0;i<c;i++) {
x &= ~((long)0xff << (8*i));
x |= (long)r_byte(arg) << (8*i);
}
}
return x;
}
#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
static VALUE
r_bytes0(len, arg)
long len;
struct load_arg *arg;
{
VALUE str;
if (!arg->end) {
VALUE src = (VALUE)arg->ptr;
VALUE n = LONG2NUM(len);
str = rb_funcall2(src, s_read, 1, &n);
if (NIL_P(str)) goto too_short;
StringValue(str);
if (RSTRING(str)->len != len) goto too_short;
if (OBJ_TAINTED(str)) arg->taint = Qtrue;
}
else {
if (arg->ptr + len > arg->end) {
too_short:
rb_raise(rb_eArgError, "marshal data too short");
}
str = rb_str_new(arg->ptr, len);
arg->ptr += len;
}
return str;
}
static ID
r_symlink(arg)
struct load_arg *arg;
{
ID id;
long num = r_long(arg);
if (st_lookup(arg->symbol, num, &id)) {
return id;
}
rb_raise(rb_eTypeError, "bad symbol");
}
static ID
r_symreal(arg)
struct load_arg *arg;
{
ID id;
id = rb_intern(RSTRING(r_bytes(arg))->ptr);
st_insert(arg->symbol, arg->symbol->num_entries, id);
return id;
}
static ID
r_symbol(arg)
struct load_arg *arg;
{
if (r_byte(arg) == TYPE_SYMLINK) {
return r_symlink(arg);
}
return r_symreal(arg);
}
static char*
r_unique(arg)
struct load_arg *arg;
{
return rb_id2name(r_symbol(arg));
}
static VALUE
r_string(arg)
struct load_arg *arg;
{
return r_bytes(arg);
}
static VALUE
r_regist(v, arg)
VALUE v;
struct load_arg *arg;
{
rb_hash_aset(arg->data, INT2FIX(RHASH(arg->data)->tbl->num_entries), v);
if (arg->taint) OBJ_TAINT(v);
return v;
}
static void
r_ivar(obj, arg)
VALUE obj;
struct load_arg *arg;
{
long len;
len = r_long(arg);
if (len > 0) {
while (len--) {
ID id = r_symbol(arg);
VALUE val = r_object(arg);
rb_ivar_set(obj, id, val);
}
}
}
static VALUE
path2class(path)
char *path;
{
VALUE v = rb_path2class(path);
if (TYPE(v) != T_CLASS) {
rb_raise(rb_eTypeError, "%s does not refer class", path);
}
return v;
}
static VALUE
path2module(path)
char *path;
{
VALUE v = rb_path2class(path);
if (TYPE(v) != T_MODULE) {
rb_raise(rb_eTypeError, "%s does not refer module", path);
}
return v;
}
static VALUE
r_object0(arg, proc)
struct load_arg *arg;
VALUE proc;
{
VALUE v = Qnil;
int type = r_byte(arg);
long id;
switch (type) {
case TYPE_LINK:
id = r_long(arg);
v = rb_hash_aref(arg->data, LONG2FIX(id));
if (NIL_P(v)) {
rb_raise(rb_eArgError, "dump format error (unlinked)");
}
return v;
case TYPE_IVAR:
v = r_object0(arg, 0);
r_ivar(v, arg);
break;
case TYPE_EXTENDED:
{
VALUE m = path2module(r_unique(arg));
v = r_object0(arg, 0);
rb_extend_object(v, m);
}
break;
case TYPE_UCLASS:
{
VALUE c = path2class(r_unique(arg));
if (FL_TEST(c, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "singleton can't be loaded");
}
v = r_object0(arg, 0);
if (rb_special_const_p(v) || TYPE(v) == T_OBJECT || TYPE(v) == T_CLASS) {
format_error:
rb_raise(rb_eArgError, "dump format error (user class)");
}
if (TYPE(v) == T_MODULE || !RTEST(rb_funcall(c, '<', 1, RBASIC(v)->klass))) {
VALUE tmp = rb_obj_alloc(c);
if (TYPE(v) != TYPE(tmp)) goto format_error;
}
RBASIC(v)->klass = c;
}
break;
case TYPE_NIL:
v = Qnil;
break;
case TYPE_TRUE:
v = Qtrue;
break;
case TYPE_FALSE:
v = Qfalse;
break;
case TYPE_FIXNUM:
{
long i = r_long(arg);
v = LONG2FIX(i);
}
break;
case TYPE_FLOAT:
{
double d, t = 0.0;
VALUE str = r_bytes(arg);
const char *ptr = RSTRING(str)->ptr;
if (strcmp(ptr, "nan") == 0) {
d = t / t;
}
else if (strcmp(ptr, "inf") == 0) {
d = 1.0 / t;
}
else if (strcmp(ptr, "-inf") == 0) {
d = -1.0 / t;
}
else {
char *e;
d = strtod(ptr, &e);
if (!*e++) {
d = load_mantissa(d, e, RSTRING(str)->len - (e - ptr));
}
}
v = rb_float_new(d);
r_regist(v, arg);
}
break;
case TYPE_BIGNUM:
{
long len;
BDIGIT *digits;
VALUE data;