Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
marshal.c
Go to the documentation of this file.
1/**********************************************************************
2
3 marshal.c -
4
5 $Author$
6 created at: Thu Apr 27 16:30:01 JST 1995
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/ruby.h"
13#include "ruby/io.h"
14#include "internal.h"
15#include "ruby/st.h"
16#include "ruby/util.h"
17#include "encindex.h"
18#include "id_table.h"
19
20#include <math.h>
21#ifdef HAVE_FLOAT_H
22#include <float.h>
23#endif
24#ifdef HAVE_IEEEFP_H
25#include <ieeefp.h>
26#endif
27
28#define BITSPERSHORT (2*CHAR_BIT)
29#define SHORTMASK ((1<<BITSPERSHORT)-1)
30#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
31
32#if SIZEOF_SHORT == SIZEOF_BDIGIT
33#define SHORTLEN(x) (x)
34#else
35static size_t
36shortlen(size_t len, BDIGIT *ds)
37{
38 BDIGIT num;
39 int offset = 0;
40
41 num = ds[len-1];
42 while (num) {
43 num = SHORTDN(num);
44 offset++;
45 }
46 return (len - 1)*SIZEOF_BDIGIT/2 + offset;
47}
48#define SHORTLEN(x) shortlen((x),d)
49#endif
50
51#define MARSHAL_MAJOR 4
52#define MARSHAL_MINOR 8
53
54#define TYPE_NIL '0'
55#define TYPE_TRUE 'T'
56#define TYPE_FALSE 'F'
57#define TYPE_FIXNUM 'i'
58
59#define TYPE_EXTENDED 'e'
60#define TYPE_UCLASS 'C'
61#define TYPE_OBJECT 'o'
62#define TYPE_DATA 'd'
63#define TYPE_USERDEF 'u'
64#define TYPE_USRMARSHAL 'U'
65#define TYPE_FLOAT 'f'
66#define TYPE_BIGNUM 'l'
67#define TYPE_STRING '"'
68#define TYPE_REGEXP '/'
69#define TYPE_ARRAY '['
70#define TYPE_HASH '{'
71#define TYPE_HASH_DEF '}'
72#define TYPE_STRUCT 'S'
73#define TYPE_MODULE_OLD 'M'
74#define TYPE_CLASS 'c'
75#define TYPE_MODULE 'm'
76
77#define TYPE_SYMBOL ':'
78#define TYPE_SYMLINK ';'
79
80#define TYPE_IVAR 'I'
81#define TYPE_LINK '@'
82
83static ID s_dump, s_load, s_mdump, s_mload;
84static ID s_dump_data, s_load_data, s_alloc, s_call;
85static ID s_getbyte, s_read, s_write, s_binmode;
86static ID s_encoding_short, s_ruby2_keywords_flag;
87
88#define name_s_dump "_dump"
89#define name_s_load "_load"
90#define name_s_mdump "marshal_dump"
91#define name_s_mload "marshal_load"
92#define name_s_dump_data "_dump_data"
93#define name_s_load_data "_load_data"
94#define name_s_alloc "_alloc"
95#define name_s_call "call"
96#define name_s_getbyte "getbyte"
97#define name_s_read "read"
98#define name_s_write "write"
99#define name_s_binmode "binmode"
100#define name_s_encoding_short "E"
101#define name_s_ruby2_keywords_flag "K"
102
103typedef struct {
104 VALUE newclass;
105 VALUE oldclass;
106 VALUE (*dumper)(VALUE);
107 VALUE (*loader)(VALUE, VALUE);
108} marshal_compat_t;
109
110static st_table *compat_allocator_tbl;
111static VALUE compat_allocator_tbl_wrapper;
112static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit);
113static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc);
114
115static int
116mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _)
117{
118 marshal_compat_t *p = (marshal_compat_t *)value;
119 rb_gc_mark(p->newclass);
120 rb_gc_mark(p->oldclass);
121 return ST_CONTINUE;
122}
123
124static void
125mark_marshal_compat_t(void *tbl)
126{
127 if (!tbl) return;
128 st_foreach(tbl, mark_marshal_compat_i, 0);
129}
130
131static st_table *compat_allocator_table(void);
132
133void
134rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
135{
136 marshal_compat_t *compat;
137 rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
138
139 if (!allocator) {
140 rb_raise(rb_eTypeError, "no allocator");
141 }
142
143 compat = ALLOC(marshal_compat_t);
144 compat->newclass = Qnil;
145 compat->oldclass = Qnil;
146 compat->newclass = newclass;
147 compat->oldclass = oldclass;
148 compat->dumper = dumper;
149 compat->loader = loader;
150
151 st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
152}
153
154struct dump_arg {
155 VALUE str, dest;
156 st_table *symbols;
157 st_table *data;
158 st_table *compat_tbl;
159 st_table *encodings;
160};
161
162struct dump_call_arg {
163 VALUE obj;
164 struct dump_arg *arg;
165 int limit;
166};
167
168static VALUE
169check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
170{
171 if (!arg->symbols) {
172 rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
173 name);
174 }
175 return ret;
176}
177
178static VALUE
179check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
180 struct dump_arg *arg, const char *name)
181{
182 VALUE ret = rb_funcallv(obj, sym, argc, argv);
183 VALUE klass = CLASS_OF(obj);
184 if (CLASS_OF(ret) == klass) {
185 rb_raise(rb_eRuntimeError, "%"PRIsVALUE"#%s returned same class instance",
186 klass, name);
187 }
188 return check_dump_arg(ret, arg, name);
189}
190
191#define dump_funcall(arg, obj, sym, argc, argv) \
192 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym)
193#define dump_check_funcall(arg, obj, sym, argc, argv) \
194 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym)
195
196static void clear_dump_arg(struct dump_arg *arg);
197
198static void
199mark_dump_arg(void *ptr)
200{
201 struct dump_arg *p = ptr;
202 if (!p->symbols)
203 return;
204 rb_mark_set(p->symbols);
205 rb_mark_set(p->data);
206 rb_mark_hash(p->compat_tbl);
207 rb_gc_mark(p->str);
208}
209
210static void
211free_dump_arg(void *ptr)
212{
213 clear_dump_arg(ptr);
214 xfree(ptr);
215}
216
217static size_t
218memsize_dump_arg(const void *ptr)
219{
220 return sizeof(struct dump_arg);
221}
222
223static const rb_data_type_t dump_arg_data = {
224 "dump_arg",
225 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
226 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
227};
228
229static VALUE
230must_not_be_anonymous(const char *type, VALUE path)
231{
232 char *n = RSTRING_PTR(path);
233
234 if (!rb_enc_asciicompat(rb_enc_get(path))) {
235 /* cannot occur? */
236 rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
237 type, path);
238 }
239 if (n[0] == '#') {
240 rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
241 type, path);
242 }
243 return path;
244}
245
246static VALUE
247class2path(VALUE klass)
248{
249 VALUE path = rb_class_path(klass);
250
251 must_not_be_anonymous((RB_TYPE_P(klass, T_CLASS) ? "class" : "module"), path);
252 if (rb_path_to_class(path) != rb_class_real(klass)) {
253 rb_raise(rb_eTypeError, "% "PRIsVALUE" can't be referred to", path);
254 }
255 return path;
256}
257
258int ruby_marshal_write_long(long x, char *buf);
259static void w_long(long, struct dump_arg*);
260static int w_encoding(VALUE encname, struct dump_call_arg *arg);
261static VALUE encoding_name(VALUE obj, struct dump_arg *arg);
262
263static void
264w_nbyte(const char *s, long n, struct dump_arg *arg)
265{
266 VALUE buf = arg->str;
267 rb_str_buf_cat(buf, s, n);
268 if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
269 rb_io_write(arg->dest, buf);
270 rb_str_resize(buf, 0);
271 }
272}
273
274static void
275w_byte(char c, struct dump_arg *arg)
276{
277 w_nbyte(&c, 1, arg);
278}
279
280static void
281w_bytes(const char *s, long n, struct dump_arg *arg)
282{
283 w_long(n, arg);
284 w_nbyte(s, n, arg);
285}
286
287#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
288
289static void
290w_short(int x, struct dump_arg *arg)
291{
292 w_byte((char)((x >> 0) & 0xff), arg);
293 w_byte((char)((x >> 8) & 0xff), arg);
294}
295
296static void
297w_long(long x, struct dump_arg *arg)
298{
299 char buf[sizeof(long)+1];
300 int i = ruby_marshal_write_long(x, buf);
301 if (i < 0) {
302 rb_raise(rb_eTypeError, "long too big to dump");
303 }
304 w_nbyte(buf, i, arg);
305}
306
307int
308ruby_marshal_write_long(long x, char *buf)
309{
310 int i;
311
312#if SIZEOF_LONG > 4
313 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
314 /* big long does not fit in 4 bytes */
315 return -1;
316 }
317#endif
318
319 if (x == 0) {
320 buf[0] = 0;
321 return 1;
322 }
323 if (0 < x && x < 123) {
324 buf[0] = (char)(x + 5);
325 return 1;
326 }
327 if (-124 < x && x < 0) {
328 buf[0] = (char)((x - 5)&0xff);
329 return 1;
330 }
331 for (i=1;i<(int)sizeof(long)+1;i++) {
332 buf[i] = (char)(x & 0xff);
333 x = RSHIFT(x,8);
334 if (x == 0) {
335 buf[0] = i;
336 break;
337 }
338 if (x == -1) {
339 buf[0] = -i;
340 break;
341 }
342 }
343 return i+1;
344}
345
346#ifdef DBL_MANT_DIG
347#define DECIMAL_MANT (53-16) /* from IEEE754 double precision */
348
349#if DBL_MANT_DIG > 32
350#define MANT_BITS 32
351#elif DBL_MANT_DIG > 24
352#define MANT_BITS 24
353#elif DBL_MANT_DIG > 16
354#define MANT_BITS 16
355#else
356#define MANT_BITS 8
357#endif
358
359static double
360load_mantissa(double d, const char *buf, long len)
361{
362 if (!len) return d;
363 if (--len > 0 && !*buf++) { /* binary mantissa mark */
364 int e, s = d < 0, dig = 0;
365 unsigned long m;
366
367 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
368 do {
369 m = 0;
370 switch (len) {
371 default: m = *buf++ & 0xff; /* fall through */
372#if MANT_BITS > 24
373 case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
374#endif
375#if MANT_BITS > 16
376 case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
377#endif
378#if MANT_BITS > 8
379 case 1: m = (m << 8) | (*buf++ & 0xff);
380#endif
381 }
382 dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
383 d += ldexp((double)m, dig);
384 } while ((len -= MANT_BITS / 8) > 0);
385 d = ldexp(d, e - DECIMAL_MANT);
386 if (s) d = -d;
387 }
388 return d;
389}
390#else
391#define load_mantissa(d, buf, len) (d)
392#endif
393
394#ifdef DBL_DIG
395#define FLOAT_DIG (DBL_DIG+2)
396#else
397#define FLOAT_DIG 17
398#endif
399
400static void
401w_float(double d, struct dump_arg *arg)
402{
403 char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
404
405 if (isinf(d)) {
406 if (d < 0) w_cstr("-inf", arg);
407 else w_cstr("inf", arg);
408 }
409 else if (isnan(d)) {
410 w_cstr("nan", arg);
411 }
412 else if (d == 0.0) {
413 if (signbit(d)) w_cstr("-0", arg);
414 else w_cstr("0", arg);
415 }
416 else {
417 int decpt, sign, digs, len = 0;
418 char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
419 if (sign) buf[len++] = '-';
420 digs = (int)(e - p);
421 if (decpt < -3 || decpt > digs) {
422 buf[len++] = p[0];
423 if (--digs > 0) buf[len++] = '.';
424 memcpy(buf + len, p + 1, digs);
425 len += digs;
426 len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
427 }
428 else if (decpt > 0) {
429 memcpy(buf + len, p, decpt);
430 len += decpt;
431 if ((digs -= decpt) > 0) {
432 buf[len++] = '.';
433 memcpy(buf + len, p + decpt, digs);
434 len += digs;
435 }
436 }
437 else {
438 buf[len++] = '0';
439 buf[len++] = '.';
440 if (decpt) {
441 memset(buf + len, '0', -decpt);
442 len -= decpt;
443 }
444 memcpy(buf + len, p, digs);
445 len += digs;
446 }
447 xfree(p);
448 w_bytes(buf, len, arg);
449 }
450}
451
452static void
453w_symbol(VALUE sym, struct dump_arg *arg)
454{
455 st_data_t num;
456 VALUE encname;
457
458 if (st_lookup(arg->symbols, sym, &num)) {
459 w_byte(TYPE_SYMLINK, arg);
460 w_long((long)num, arg);
461 }
462 else {
463 const VALUE orig_sym = sym;
464 sym = rb_sym2str(sym);
465 if (!sym) {
466 rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
467 }
468 encname = encoding_name(sym, arg);
469 if (NIL_P(encname) ||
470 rb_enc_str_coderange(sym) == ENC_CODERANGE_7BIT) {
471 encname = Qnil;
472 }
473 else {
474 w_byte(TYPE_IVAR, arg);
475 }
476 w_byte(TYPE_SYMBOL, arg);
477 w_bytes(RSTRING_PTR(sym), RSTRING_LEN(sym), arg);
478 st_add_direct(arg->symbols, orig_sym, arg->symbols->num_entries);
479 if (!NIL_P(encname)) {
480 struct dump_call_arg c_arg;
481 c_arg.limit = 1;
482 c_arg.arg = arg;
483 w_long(1L, arg);
484 w_encoding(encname, &c_arg);
485 }
486 }
487}
488
489static void
490w_unique(VALUE s, struct dump_arg *arg)
491{
492 must_not_be_anonymous("class", s);
493 w_symbol(rb_str_intern(s), arg);
494}
495
496static void w_object(VALUE,struct dump_arg*,int);
497
498static int
499hash_each(VALUE key, VALUE value, VALUE v)
500{
501 struct dump_call_arg *arg = (void *)v;
502 w_object(key, arg->arg, arg->limit);
503 w_object(value, arg->arg, arg->limit);
504 return ST_CONTINUE;
505}
506
507#define SINGLETON_DUMP_UNABLE_P(klass) \
508 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \
509 (RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1))
510
511static void
512w_extended(VALUE klass, struct dump_arg *arg, int check)
513{
514 if (check && FL_TEST(klass, FL_SINGLETON)) {
515 VALUE origin = RCLASS_ORIGIN(klass);
516 if (SINGLETON_DUMP_UNABLE_P(klass) ||
517 (origin != klass && SINGLETON_DUMP_UNABLE_P(origin))) {
518 rb_raise(rb_eTypeError, "singleton can't be dumped");
519 }
520 klass = RCLASS_SUPER(klass);
521 }
522 while (BUILTIN_TYPE(klass) == T_ICLASS) {
523 VALUE path = rb_class_name(RBASIC(klass)->klass);
524 w_byte(TYPE_EXTENDED, arg);
525 w_unique(path, arg);
526 klass = RCLASS_SUPER(klass);
527 }
528}
529
530static void
531w_class(char type, VALUE obj, struct dump_arg *arg, int check)
532{
533 VALUE path;
534 st_data_t real_obj;
535 VALUE klass;
536
537 if (arg->compat_tbl &&
538 st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
539 obj = (VALUE)real_obj;
540 }
541 klass = CLASS_OF(obj);
542 w_extended(klass, arg, check);
543 w_byte(type, arg);
544 path = class2path(rb_class_real(klass));
545 w_unique(path, arg);
546}
547
548static void
549w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
550{
551 VALUE klass = CLASS_OF(obj);
552
553 w_extended(klass, arg, TRUE);
554 klass = rb_class_real(klass);
555 if (klass != super) {
556 w_byte(TYPE_UCLASS, arg);
557 w_unique(class2path(klass), arg);
558 }
559}
560
561#define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || id == s_ruby2_keywords_flag || !rb_id2str(id))
562
563struct w_ivar_arg {
564 struct dump_call_arg *dump;
565 st_data_t num_ivar;
566};
567
568static int
569w_obj_each(st_data_t key, st_data_t val, st_data_t a)
570{
571 ID id = (ID)key;
572 VALUE value = (VALUE)val;
573 struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
574 struct dump_call_arg *arg = ivarg->dump;
575
576 if (to_be_skipped_id(id)) {
577 if (id == s_encoding_short) {
578 rb_warn("instance variable `"name_s_encoding_short"' on class %"PRIsVALUE" is not dumped",
579 CLASS_OF(arg->obj));
580 }
581 if (id == s_ruby2_keywords_flag) {
582 rb_warn("instance variable `"name_s_ruby2_keywords_flag"' on class %"PRIsVALUE" is not dumped",
583 CLASS_OF(arg->obj));
584 }
585 return ST_CONTINUE;
586 }
587 if (!ivarg->num_ivar) {
588 rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
589 CLASS_OF(arg->obj));
590 }
591 --ivarg->num_ivar;
592 w_symbol(ID2SYM(id), arg->arg);
593 w_object(value, arg->arg, arg->limit);
594 return ST_CONTINUE;
595}
596
597static int
598obj_count_ivars(st_data_t key, st_data_t val, st_data_t a)
599{
600 ID id = (ID)key;
601 if (!to_be_skipped_id(id)) ++*(st_index_t *)a;
602 return ST_CONTINUE;
603}
604
605static VALUE
606encoding_name(VALUE obj, struct dump_arg *arg)
607{
608 if (rb_enc_capable(obj)) {
609 int encidx = rb_enc_get_index(obj);
610 rb_encoding *enc = 0;
611 st_data_t name;
612
613 if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
614 return Qnil;
615 }
616
617 /* special treatment for US-ASCII and UTF-8 */
618 if (encidx == rb_usascii_encindex()) {
619 return Qfalse;
620 }
621 else if (encidx == rb_utf8_encindex()) {
622 return Qtrue;
623 }
624
625 if (arg->encodings ?
626 !st_lookup(arg->encodings, (st_data_t)rb_enc_name(enc), &name) :
627 (arg->encodings = st_init_strcasetable(), 1)) {
628 name = (st_data_t)rb_str_new_cstr(rb_enc_name(enc));
629 st_insert(arg->encodings, (st_data_t)rb_enc_name(enc), name);
630 }
631 return (VALUE)name;
632 }
633 else {
634 return Qnil;
635 }
636}
637
638static int
639w_encoding(VALUE encname, struct dump_call_arg *arg)
640{
641 int limit = arg->limit;
642 if (limit >= 0) ++limit;
643 switch (encname) {
644 case Qfalse:
645 case Qtrue:
646 w_symbol(ID2SYM(s_encoding_short), arg->arg);
647 w_object(encname, arg->arg, limit);
648 return 1;
649 case Qnil:
650 return 0;
651 }
652 w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
653 w_object(encname, arg->arg, limit);
654 return 1;
655}
656
657static st_index_t
658has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
659{
660 st_index_t enc = !NIL_P(encname);
661 st_index_t num = 0;
662 st_index_t ruby2_keywords_flag = 0;
663
664 if (SPECIAL_CONST_P(obj)) goto generic;
665 switch (BUILTIN_TYPE(obj)) {
666 case T_OBJECT:
667 case T_CLASS:
668 case T_MODULE:
669 break; /* counted elsewhere */
670 case T_HASH:
671 ruby2_keywords_flag = RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS ? 1 : 0;
672 /* fall through */
673 default:
674 generic:
675 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
676 if (ruby2_keywords_flag || num) *ivobj = obj;
677 }
678
679 return num + enc + ruby2_keywords_flag;
680}
681
682static void
683w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
684{
685 struct w_ivar_arg ivarg = {arg, num};
686 if (!num) return;
687 rb_ivar_foreach(obj, w_obj_each, (st_data_t)&ivarg);
688 if (ivarg.num_ivar) {
689 rb_raise(rb_eRuntimeError, "instance variable removed from %"PRIsVALUE" instance",
690 CLASS_OF(arg->obj));
691 }
692}
693
694static void
695w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
696{
697 w_long(num, arg->arg);
698 num -= w_encoding(encname, arg);
699 if (RB_TYPE_P(ivobj, T_HASH) && (RHASH(ivobj)->basic.flags & RHASH_PASS_AS_KEYWORDS)) {
700 int limit = arg->limit;
701 if (limit >= 0) ++limit;
702 w_symbol(ID2SYM(s_ruby2_keywords_flag), arg->arg);
703 w_object(Qtrue, arg->arg, limit);
704 num--;
705 }
706 if (ivobj != Qundef && num) {
707 w_ivar_each(ivobj, num, arg);
708 }
709}
710
711static void
712w_objivar(VALUE obj, struct dump_call_arg *arg)
713{
714 st_data_t num = 0;
715
716 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
717 w_long(num, arg->arg);
718 w_ivar_each(obj, num, arg);
719}
720
721static void
722w_object(VALUE obj, struct dump_arg *arg, int limit)
723{
724 struct dump_call_arg c_arg;
725 VALUE ivobj = Qundef;
726 st_data_t num;
727 st_index_t hasiv = 0;
728 VALUE encname = Qnil;
729
730 if (limit == 0) {
731 rb_raise(rb_eArgError, "exceed depth limit");
732 }
733
734 if (limit > 0) limit--;
735 c_arg.limit = limit;
736 c_arg.arg = arg;
737 c_arg.obj = obj;
738
739 if (st_lookup(arg->data, obj, &num)) {
740 w_byte(TYPE_LINK, arg);
741 w_long((long)num, arg);
742 return;
743 }
744
745 if (obj == Qnil) {
746 w_byte(TYPE_NIL, arg);
747 }
748 else if (obj == Qtrue) {
749 w_byte(TYPE_TRUE, arg);
750 }
751 else if (obj == Qfalse) {
752 w_byte(TYPE_FALSE, arg);
753 }
754 else if (FIXNUM_P(obj)) {
755#if SIZEOF_LONG <= 4
756 w_byte(TYPE_FIXNUM, arg);
757 w_long(FIX2INT(obj), arg);
758#else
759 if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
760 w_byte(TYPE_FIXNUM, arg);
761 w_long(FIX2LONG(obj), arg);
762 }
763 else {
764 w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
765 }
766#endif
767 }
768 else if (SYMBOL_P(obj)) {
769 w_symbol(obj, arg);
770 }
771 else if (FLONUM_P(obj)) {
772 st_add_direct(arg->data, obj, arg->data->num_entries);
773 w_byte(TYPE_FLOAT, arg);
774 w_float(RFLOAT_VALUE(obj), arg);
775 }
776 else {
777 VALUE v;
778
779 if (!RBASIC_CLASS(obj)) {
780 rb_raise(rb_eTypeError, "can't dump internal %s",
781 rb_builtin_type_name(BUILTIN_TYPE(obj)));
782 }
783
784 if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
785 st_add_direct(arg->data, obj, arg->data->num_entries);
786
787 v = dump_funcall(arg, obj, s_mdump, 0, 0);
788 w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
789 w_object(v, arg, limit);
790 return;
791 }
792 if (rb_obj_respond_to(obj, s_dump, TRUE)) {
793 VALUE ivobj2 = Qundef;
794 st_index_t hasiv2;
795 VALUE encname2;
796
797 v = INT2NUM(limit);
798 v = dump_funcall(arg, obj, s_dump, 1, &v);
799 if (!RB_TYPE_P(v, T_STRING)) {
800 rb_raise(rb_eTypeError, "_dump() must return string");
801 }
802 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
803 hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
804 if (hasiv2) {
805 hasiv = hasiv2;
806 ivobj = ivobj2;
807 encname = encname2;
808 }
809 if (hasiv) w_byte(TYPE_IVAR, arg);
810 w_class(TYPE_USERDEF, obj, arg, FALSE);
811 w_bytes(RSTRING_PTR(v), RSTRING_LEN(v), arg);
812 if (hasiv) {
813 w_ivar(hasiv, ivobj, encname, &c_arg);
814 }
815 st_add_direct(arg->data, obj, arg->data->num_entries);
816 return;
817 }
818
819 st_add_direct(arg->data, obj, arg->data->num_entries);
820
821 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
822 {
823 st_data_t compat_data;
824 rb_alloc_func_t allocator = rb_get_alloc_func(RBASIC(obj)->klass);
825 if (st_lookup(compat_allocator_tbl,
826 (st_data_t)allocator,
827 &compat_data)) {
828 marshal_compat_t *compat = (marshal_compat_t*)compat_data;
829 VALUE real_obj = obj;
830 obj = compat->dumper(real_obj);
831 if (!arg->compat_tbl) {
832 arg->compat_tbl = rb_init_identtable();
833 }
834 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
835 if (obj != real_obj && ivobj == Qundef) hasiv = 0;
836 }
837 }
838 if (hasiv) w_byte(TYPE_IVAR, arg);
839
840 switch (BUILTIN_TYPE(obj)) {
841 case T_CLASS:
842 if (FL_TEST(obj, FL_SINGLETON)) {
843 rb_raise(rb_eTypeError, "singleton class can't be dumped");
844 }
845 w_byte(TYPE_CLASS, arg);
846 {
847 VALUE path = class2path(obj);
848 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
849 RB_GC_GUARD(path);
850 }
851 break;
852
853 case T_MODULE:
854 w_byte(TYPE_MODULE, arg);
855 {
856 VALUE path = class2path(obj);
857 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
858 RB_GC_GUARD(path);
859 }
860 break;
861
862 case T_FLOAT:
863 w_byte(TYPE_FLOAT, arg);
864 w_float(RFLOAT_VALUE(obj), arg);
865 break;
866
867 case T_BIGNUM:
868 w_byte(TYPE_BIGNUM, arg);
869 {
870 char sign = BIGNUM_SIGN(obj) ? '+' : '-';
871 size_t len = BIGNUM_LEN(obj);
872 size_t slen;
873 size_t j;
874 BDIGIT *d = BIGNUM_DIGITS(obj);
875
876 slen = SHORTLEN(len);
877 if (LONG_MAX < slen) {
878 rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
879 }
880
881 w_byte(sign, arg);
882 w_long((long)slen, arg);
883 for (j = 0; j < len; j++) {
884#if SIZEOF_BDIGIT > SIZEOF_SHORT
885 BDIGIT num = *d;
886 int i;
887
888 for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
889 w_short(num & SHORTMASK, arg);
890 num = SHORTDN(num);
891 if (j == len - 1 && num == 0) break;
892 }
893#else
894 w_short(*d, arg);
895#endif
896 d++;
897 }
898 }
899 break;
900
901 case T_STRING:
902 w_uclass(obj, rb_cString, arg);
903 w_byte(TYPE_STRING, arg);
904 w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
905 break;
906
907 case T_REGEXP:
908 w_uclass(obj, rb_cRegexp, arg);
909 w_byte(TYPE_REGEXP, arg);
910 {
911 int opts = rb_reg_options(obj);
912 w_bytes(RREGEXP_SRC_PTR(obj), RREGEXP_SRC_LEN(obj), arg);
913 w_byte((char)opts, arg);
914 }
915 break;
916
917 case T_ARRAY:
918 w_uclass(obj, rb_cArray, arg);
919 w_byte(TYPE_ARRAY, arg);
920 {
921 long i, len = RARRAY_LEN(obj);
922
923 w_long(len, arg);
924 for (i=0; i<RARRAY_LEN(obj); i++) {
925 w_object(RARRAY_AREF(obj, i), arg, limit);
926 if (len != RARRAY_LEN(obj)) {
927 rb_raise(rb_eRuntimeError, "array modified during dump");
928 }
929 }
930 }
931 break;
932
933 case T_HASH:
934 w_uclass(obj, rb_cHash, arg);
935 if (NIL_P(RHASH_IFNONE(obj))) {
936 w_byte(TYPE_HASH, arg);
937 }
938 else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
939 rb_raise(rb_eTypeError, "can't dump hash with default proc");
940 }
941 else {
942 w_byte(TYPE_HASH_DEF, arg);
943 }
944 w_long(rb_hash_size_num(obj), arg);
945 rb_hash_foreach(obj, hash_each, (st_data_t)&c_arg);
946 if (!NIL_P(RHASH_IFNONE(obj))) {
947 w_object(RHASH_IFNONE(obj), arg, limit);
948 }
949 break;
950
951 case T_STRUCT:
952 w_class(TYPE_STRUCT, obj, arg, TRUE);
953 {
954 long len = RSTRUCT_LEN(obj);
955 VALUE mem;
956 long i;
957
958 w_long(len, arg);
959 mem = rb_struct_members(obj);
960 for (i=0; i<len; i++) {
961 w_symbol(RARRAY_AREF(mem, i), arg);
962 w_object(RSTRUCT_GET(obj, i), arg, limit);
963 }
964 }
965 break;
966
967 case T_OBJECT:
968 w_class(TYPE_OBJECT, obj, arg, TRUE);
969 w_objivar(obj, &c_arg);
970 break;
971
972 case T_DATA:
973 {
974 VALUE v;
975
976 if (!rb_obj_respond_to(obj, s_dump_data, TRUE)) {
977 rb_raise(rb_eTypeError,
978 "no _dump_data is defined for class %"PRIsVALUE,
979 rb_obj_class(obj));
980 }
981 v = dump_funcall(arg, obj, s_dump_data, 0, 0);
982 w_class(TYPE_DATA, obj, arg, TRUE);
983 w_object(v, arg, limit);
984 }
985 break;
986
987 default:
988 rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
989 rb_obj_class(obj));
990 break;
991 }
992 RB_GC_GUARD(obj);
993 }
994 if (hasiv) {
995 w_ivar(hasiv, ivobj, encname, &c_arg);
996 }
997}
998
999static void
1000clear_dump_arg(struct dump_arg *arg)
1001{
1002 if (!arg->symbols) return;
1003 st_free_table(arg->symbols);
1004 arg->symbols = 0;
1005 st_free_table(arg->data);
1006 arg->data = 0;
1007 if (arg->compat_tbl) {
1008 st_free_table(arg->compat_tbl);
1009 arg->compat_tbl = 0;
1010 }
1011 if (arg->encodings) {
1012 st_free_table(arg->encodings);
1013 arg->encodings = 0;
1014 }
1015}
1016
1017NORETURN(static inline void io_needed(void));
1018static inline void
1019io_needed(void)
1020{
1021 rb_raise(rb_eTypeError, "instance of IO needed");
1022}
1023
1024/*
1025 * call-seq:
1026 * dump( obj [, anIO] , limit=-1 ) -> anIO
1027 *
1028 * Serializes obj and all descendant objects. If anIO is
1029 * specified, the serialized data will be written to it, otherwise the
1030 * data will be returned as a String. If limit is specified, the
1031 * traversal of subobjects will be limited to that depth. If limit is
1032 * negative, no checking of depth will be performed.
1033 *
1034 * class Klass
1035 * def initialize(str)
1036 * @str = str
1037 * end
1038 * def say_hello
1039 * @str
1040 * end
1041 * end
1042 *
1043 * (produces no output)
1044 *
1045 * o = Klass.new("hello\n")
1046 * data = Marshal.dump(o)
1047 * obj = Marshal.load(data)
1048 * obj.say_hello #=> "hello\n"
1049 *
1050 * Marshal can't dump following objects:
1051 * * anonymous Class/Module.
1052 * * objects which are related to system (ex: Dir, File::Stat, IO, File, Socket
1053 * and so on)
1054 * * an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread,
1055 * ThreadGroup, Continuation
1056 * * objects which define singleton methods
1057 */
1058static VALUE
1059marshal_dump(int argc, VALUE *argv, VALUE _)
1060{
1061 VALUE obj, port, a1, a2;
1062 int limit = -1;
1063
1064 port = Qnil;
1065 rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
1066 if (argc == 3) {
1067 if (!NIL_P(a2)) limit = NUM2INT(a2);
1068 if (NIL_P(a1)) io_needed();
1069 port = a1;
1070 }
1071 else if (argc == 2) {
1072 if (FIXNUM_P(a1)) limit = FIX2INT(a1);
1073 else if (NIL_P(a1)) io_needed();
1074 else port = a1;
1075 }
1076 return rb_marshal_dump_limited(obj, port, limit);
1077}
1078
1079VALUE
1080rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
1081{
1082 struct dump_arg *arg;
1083 VALUE wrapper; /* used to avoid memory leak in case of exception */
1084
1085 wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
1086 arg->dest = 0;
1087 arg->symbols = st_init_numtable();
1088 arg->data = rb_init_identtable();
1089 arg->compat_tbl = 0;
1090 arg->encodings = 0;
1091 arg->str = rb_str_buf_new(0);
1092 if (!NIL_P(port)) {
1093 if (!rb_respond_to(port, s_write)) {
1094 io_needed();
1095 }
1096 arg->dest = port;
1097 dump_check_funcall(arg, port, s_binmode, 0, 0);
1098 }
1099 else {
1100 port = arg->str;
1101 }
1102
1103 w_byte(MARSHAL_MAJOR, arg);
1104 w_byte(MARSHAL_MINOR, arg);
1105
1106 w_object(obj, arg, limit);
1107 if (arg->dest) {
1108 rb_io_write(arg->dest, arg->str);
1109 rb_str_resize(arg->str, 0);
1110 }
1111 clear_dump_arg(arg);
1112 RB_GC_GUARD(wrapper);
1113
1114 return port;
1115}
1116
1117struct load_arg {
1118 VALUE src;
1119 char *buf;
1120 long buflen;
1121 long readable;
1122 long offset;
1123 st_table *symbols;
1124 st_table *data;
1125 VALUE proc;
1126 st_table *compat_tbl;
1127};
1128
1129static VALUE
1130check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
1131{
1132 if (!arg->symbols) {
1133 rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
1134 name);
1135 }
1136 return ret;
1137}
1138#define load_funcall(arg, obj, sym, argc, argv) \
1139 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1140
1141static void clear_load_arg(struct load_arg *arg);
1142
1143static void
1144mark_load_arg(void *ptr)
1145{
1146 struct load_arg *p = ptr;
1147 if (!p->symbols)
1148 return;
1149 rb_mark_tbl(p->symbols);
1150 rb_mark_tbl(p->data);
1151 rb_mark_hash(p->compat_tbl);
1152}
1153
1154static void
1155free_load_arg(void *ptr)
1156{
1157 clear_load_arg(ptr);
1158 xfree(ptr);
1159}
1160
1161static size_t
1162memsize_load_arg(const void *ptr)
1163{
1164 return sizeof(struct load_arg);
1165}
1166
1167static const rb_data_type_t load_arg_data = {
1168 "load_arg",
1169 {mark_load_arg, free_load_arg, memsize_load_arg,},
1170 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1171};
1172
1173#define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1174static VALUE r_entry0(VALUE v, st_index_t num, struct load_arg *arg);
1175static VALUE r_object(struct load_arg *arg);
1176static VALUE r_symbol(struct load_arg *arg);
1177static VALUE path2class(VALUE path);
1178
1179NORETURN(static void too_short(void));
1180static void
1181too_short(void)
1182{
1183 rb_raise(rb_eArgError, "marshal data too short");
1184}
1185
1186static st_index_t
1187r_prepare(struct load_arg *arg)
1188{
1189 st_index_t idx = arg->data->num_entries;
1190
1191 st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
1192 return idx;
1193}
1194
1195static unsigned char
1196r_byte1_buffered(struct load_arg *arg)
1197{
1198 if (arg->buflen == 0) {
1199 long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ;
1200 VALUE str, n = LONG2NUM(readable);
1201
1202 str = load_funcall(arg, arg->src, s_read, 1, &n);
1203 if (NIL_P(str)) too_short();
1204 StringValue(str);
1205 memcpy(arg->buf, RSTRING_PTR(str), RSTRING_LEN(str));
1206 arg->offset = 0;
1207 arg->buflen = RSTRING_LEN(str);
1208 }
1209 arg->buflen--;
1210 return arg->buf[arg->offset++];
1211}
1212
1213static int
1214r_byte(struct load_arg *arg)
1215{
1216 int c;
1217
1218 if (RB_TYPE_P(arg->src, T_STRING)) {
1219 if (RSTRING_LEN(arg->src) > arg->offset) {
1220 c = (unsigned char)RSTRING_PTR(arg->src)[arg->offset++];
1221 }
1222 else {
1223 too_short();
1224 }
1225 }
1226 else {
1227 if (arg->readable >0 || arg->buflen > 0) {
1228 c = r_byte1_buffered(arg);
1229 }
1230 else {
1231 VALUE v = load_funcall(arg, arg->src, s_getbyte, 0, 0);
1232 if (NIL_P(v)) rb_eof_error();
1233 c = (unsigned char)NUM2CHR(v);
1234 }
1235 }
1236 return c;
1237}
1238
1239NORETURN(static void long_toobig(int size));
1240
1241static void
1242long_toobig(int size)
1243{
1244 rb_raise(rb_eTypeError, "long too big for this architecture (size "
1245 STRINGIZE(SIZEOF_LONG)", given %d)", size);
1246}
1247
1248static long
1249r_long(struct load_arg *arg)
1250{
1251 register long x;
1252 int c = (signed char)r_byte(arg);
1253 long i;
1254
1255 if (c == 0) return 0;
1256 if (c > 0) {
1257 if (4 < c && c < 128) {
1258 return c - 5;
1259 }
1260 if (c > (int)sizeof(long)) long_toobig(c);
1261 x = 0;
1262 for (i=0;i<c;i++) {
1263 x |= (long)r_byte(arg) << (8*i);
1264 }
1265 }
1266 else {
1267 if (-129 < c && c < -4) {
1268 return c + 5;
1269 }
1270 c = -c;
1271 if (c > (int)sizeof(long)) long_toobig(c);
1272 x = -1;
1273 for (i=0;i<c;i++) {
1274 x &= ~((long)0xff << (8*i));
1275 x |= (long)r_byte(arg) << (8*i);
1276 }
1277 }
1278 return x;
1279}
1280
1281long
1282ruby_marshal_read_long(const char **buf, long len)
1283{
1284 long x;
1285 struct RString src;
1286 struct load_arg arg;
1287 memset(&arg, 0, sizeof(arg));
1288 arg.src = rb_setup_fake_str(&src, *buf, len, 0);
1289 x = r_long(&arg);
1290 *buf += arg.offset;
1291 return x;
1292}
1293
1294static VALUE
1295r_bytes1(long len, struct load_arg *arg)
1296{
1297 VALUE str, n = LONG2NUM(len);
1298
1299 str = load_funcall(arg, arg->src, s_read, 1, &n);
1300 if (NIL_P(str)) too_short();
1301 StringValue(str);
1302 if (RSTRING_LEN(str) != len) too_short();
1303
1304 return str;
1305}
1306
1307static VALUE
1308r_bytes1_buffered(long len, struct load_arg *arg)
1309{
1310 VALUE str;
1311
1312 if (len <= arg->buflen) {
1313 str = rb_str_new(arg->buf+arg->offset, len);
1314 arg->offset += len;
1315 arg->buflen -= len;
1316 }
1317 else {
1318 long buflen = arg->buflen;
1319 long readable = arg->readable + 1;
1320 long tmp_len, read_len, need_len = len - buflen;
1321 VALUE tmp, n;
1322
1323 readable = readable < BUFSIZ ? readable : BUFSIZ;
1324 read_len = need_len > readable ? need_len : readable;
1325 n = LONG2NUM(read_len);
1326 tmp = load_funcall(arg, arg->src, s_read, 1, &n);
1327 if (NIL_P(tmp)) too_short();
1328 StringValue(tmp);
1329
1330 tmp_len = RSTRING_LEN(tmp);
1331
1332 if (tmp_len < need_len) too_short();
1333
1334 str = rb_str_new(arg->buf+arg->offset, buflen);
1335 rb_str_cat(str, RSTRING_PTR(tmp), need_len);
1336
1337 if (tmp_len > need_len) {
1338 buflen = tmp_len - need_len;
1339 memcpy(arg->buf, RSTRING_PTR(tmp)+need_len, buflen);
1340 arg->buflen = buflen;
1341 }
1342 else {
1343 arg->buflen = 0;
1344 }
1345 arg->offset = 0;
1346 }
1347
1348 return str;
1349}
1350
1351#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1352
1353static VALUE
1354r_bytes0(long len, struct load_arg *arg)
1355{
1356 VALUE str;
1357
1358 if (len == 0) return rb_str_new(0, 0);
1359 if (RB_TYPE_P(arg->src, T_STRING)) {
1360 if (RSTRING_LEN(arg->src) - arg->offset >= len) {
1361 str = rb_str_new(RSTRING_PTR(arg->src)+arg->offset, len);
1362 arg->offset += len;
1363 }
1364 else {
1365 too_short();
1366 }
1367 }
1368 else {
1369 if (arg->readable > 0 || arg->buflen > 0) {
1370 str = r_bytes1_buffered(len, arg);
1371 }
1372 else {
1373 str = r_bytes1(len, arg);
1374 }
1375 }
1376 return str;
1377}
1378
1379static inline int
1380name_equal(const char *name, size_t nlen, const char *p, long l)
1381{
1382 if ((size_t)l != nlen || *p != *name) return 0;
1383 return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
1384}
1385
1386static int
1387sym2encidx(VALUE sym, VALUE val)
1388{
1389 static const char name_encoding[8] = "encoding";
1390 const char *p;
1391 long l;
1392 if (rb_enc_get_index(sym) != ENCINDEX_US_ASCII) return -1;
1393 RSTRING_GETMEM(sym, p, l);
1394 if (l <= 0) return -1;
1395 if (name_equal(name_encoding, sizeof(name_encoding), p, l)) {
1396 int idx = rb_enc_find_index(StringValueCStr(val));
1397 return idx;
1398 }
1399 if (name_equal(name_s_encoding_short, rb_strlen_lit(name_s_encoding_short), p, l)) {
1400 if (val == Qfalse) return rb_usascii_encindex();
1401 else if (val == Qtrue) return rb_utf8_encindex();
1402 /* bogus ignore */
1403 }
1404 return -1;
1405}
1406
1407static int
1408ruby2_keywords_flag_check(VALUE sym)
1409{
1410 const char *p;
1411 long l;
1412 RSTRING_GETMEM(sym, p, l);
1413 if (l <= 0) return 0;
1414 if (name_equal(name_s_ruby2_keywords_flag, rb_strlen_lit(name_s_ruby2_keywords_flag), p, 1)) {
1415 return 1;
1416 }
1417 return 0;
1418}
1419
1420static VALUE
1421r_symlink(struct load_arg *arg)
1422{
1423 st_data_t sym;
1424 long num = r_long(arg);
1425
1426 if (!st_lookup(arg->symbols, num, &sym)) {
1427 rb_raise(rb_eArgError, "bad symbol");
1428 }
1429 return (VALUE)sym;
1430}
1431
1432static VALUE
1433r_symreal(struct load_arg *arg, int ivar)
1434{
1435 VALUE s = r_bytes(arg);
1436 VALUE sym;
1437 int idx = -1;
1438 st_index_t n = arg->symbols->num_entries;
1439
1440 if (rb_enc_str_asciionly_p(s)) rb_enc_associate_index(s, ENCINDEX_US_ASCII);
1441 st_insert(arg->symbols, (st_data_t)n, (st_data_t)s);
1442 if (ivar) {
1443 long num = r_long(arg);
1444 while (num-- > 0) {
1445 sym = r_symbol(arg);
1446 idx = sym2encidx(sym, r_object(arg));
1447 }
1448 }
1449 if (idx > 0) rb_enc_associate_index(s, idx);
1450
1451 return s;
1452}
1453
1454static VALUE
1455r_symbol(struct load_arg *arg)
1456{
1457 int type, ivar = 0;
1458
1459 again:
1460 switch ((type = r_byte(arg))) {
1461 default:
1462 rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
1463 case TYPE_IVAR:
1464 ivar = 1;
1465 goto again;
1466 case TYPE_SYMBOL:
1467 return r_symreal(arg, ivar);
1468 case TYPE_SYMLINK:
1469 if (ivar) {
1470 rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
1471 }
1472 return r_symlink(arg);
1473 }
1474}
1475
1476static VALUE
1477r_unique(struct load_arg *arg)
1478{
1479 return r_symbol(arg);
1480}
1481
1482static VALUE
1483r_string(struct load_arg *arg)
1484{
1485 return r_bytes(arg);
1486}
1487
1488static VALUE
1489r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
1490{
1491 st_data_t real_obj = (VALUE)Qundef;
1492 if (arg->compat_tbl && st_lookup(arg->compat_tbl, v, &real_obj)) {
1493 st_insert(arg->data, num, (st_data_t)real_obj);
1494 }
1495 else {
1496 st_insert(arg->data, num, (st_data_t)v);
1497 }
1498 return v;
1499}
1500
1501static VALUE
1502r_fixup_compat(VALUE v, struct load_arg *arg)
1503{
1504 st_data_t data;
1505 st_data_t key = (st_data_t)v;
1506 if (arg->compat_tbl && st_delete(arg->compat_tbl, &key, &data)) {
1507 VALUE real_obj = (VALUE)data;
1508 rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj));
1509 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1510 marshal_compat_t *compat = (marshal_compat_t*)data;
1511 compat->loader(real_obj, v);
1512 }
1513 v = real_obj;
1514 }
1515 return v;
1516}
1517
1518static VALUE
1519r_post_proc(VALUE v, struct load_arg *arg)
1520{
1521 if (arg->proc) {
1522 v = load_funcall(arg, arg->proc, s_call, 1, &v);
1523 }
1524 return v;
1525}
1526
1527static VALUE
1528r_leave(VALUE v, struct load_arg *arg)
1529{
1530 v = r_fixup_compat(v, arg);
1531 v = r_post_proc(v, arg);
1532 return v;
1533}
1534
1535static int
1536copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
1537{
1538 VALUE obj = (VALUE)arg, value = (VALUE)val;
1539 ID vid = (ID)key;
1540
1541 if (!rb_ivar_defined(obj, vid))
1542 rb_ivar_set(obj, vid, value);
1543 return ST_CONTINUE;
1544}
1545
1546static VALUE
1547r_copy_ivar(VALUE v, VALUE data)
1548{
1549 rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
1550 return v;
1551}
1552
1553static void
1554r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
1555{
1556 long len;
1557
1558 len = r_long(arg);
1559 if (len > 0) {
1560 do {
1561 VALUE sym = r_symbol(arg);
1562 VALUE val = r_object(arg);
1563 int idx = sym2encidx(sym, val);
1564 if (idx >= 0) {
1565 if (rb_enc_capable(obj)) {
1566 rb_enc_associate_index(obj, idx);
1567 }
1568 else {
1569 rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
1570 }
1571 if (has_encoding) *has_encoding = TRUE;
1572 }
1573 else if (ruby2_keywords_flag_check(sym)) {
1574 if (RB_TYPE_P(obj, T_HASH)) {
1575 RHASH(obj)->basic.flags |= RHASH_PASS_AS_KEYWORDS;
1576 }
1577 else {
1578 rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
1579 }
1580 }
1581 else {
1582 rb_ivar_set(obj, rb_intern_str(sym), val);
1583 }
1584 } while (--len > 0);
1585 }
1586}
1587
1588static VALUE
1589path2class(VALUE path)
1590{
1591 VALUE v = rb_path_to_class(path);
1592
1593 if (!RB_TYPE_P(v, T_CLASS)) {
1594 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
1595 }
1596 return v;
1597}
1598
1599#define path2module(path) must_be_module(rb_path_to_class(path), path)
1600
1601static VALUE
1602must_be_module(VALUE v, VALUE path)
1603{
1604 if (!RB_TYPE_P(v, T_MODULE)) {
1605 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
1606 }
1607 return v;
1608}
1609
1610static VALUE
1611obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
1612{
1613 st_data_t data;
1614 rb_alloc_func_t allocator;
1615
1616 allocator = rb_get_alloc_func(klass);
1617 if (st_lookup(compat_allocator_tbl, (st_data_t)allocator, &data)) {
1618 marshal_compat_t *compat = (marshal_compat_t*)data;
1619 VALUE real_obj = rb_obj_alloc(klass);
1620 VALUE obj = rb_obj_alloc(compat->oldclass);
1621 if (oldclass) *oldclass = compat->oldclass;
1622
1623 if (!arg->compat_tbl) {
1624 arg->compat_tbl = rb_init_identtable();
1625 }
1626 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
1627 return obj;
1628 }
1629
1630 return rb_obj_alloc(klass);
1631}
1632
1633static VALUE
1634obj_alloc_by_path(VALUE path, struct load_arg *arg)
1635{
1636 return obj_alloc_by_klass(path2class(path), arg, 0);
1637}
1638
1639static VALUE
1640append_extmod(VALUE obj, VALUE extmod)
1641{
1642 long i = RARRAY_LEN(extmod);
1643 while (i > 0) {
1644 VALUE m = RARRAY_AREF(extmod, --i);
1645 rb_extend_object(obj, m);
1646 }
1647 return obj;
1648}
1649
1650#define prohibit_ivar(type, str) do { \
1651 if (!ivp || !*ivp) break; \
1652 rb_raise(rb_eTypeError, \
1653 "can't override instance variable of "type" `%"PRIsVALUE"'", \
1654 (str)); \
1655 } while (0)
1656
1657static VALUE
1658r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
1659{
1660 VALUE v = Qnil;
1661 int type = r_byte(arg);
1662 long id;
1663 st_data_t link;
1664
1665 switch (type) {
1666 case TYPE_LINK:
1667 id = r_long(arg);
1668 if (!st_lookup(arg->data, (st_data_t)id, &link)) {
1669 rb_raise(rb_eArgError, "dump format error (unlinked)");
1670 }
1671 v = (VALUE)link;
1672 v = r_post_proc(v, arg);
1673 break;
1674
1675 case TYPE_IVAR:
1676 {
1677 int ivar = TRUE;
1678
1679 v = r_object0(arg, &ivar, extmod);
1680 if (ivar) r_ivar(v, NULL, arg);
1681 if (RB_TYPE_P(v, T_STRING)) {
1682 v = r_leave(v, arg);
1683 }
1684 }
1685 break;
1686
1687 case TYPE_EXTENDED:
1688 {
1689 VALUE path = r_unique(arg);
1690 VALUE m = rb_path_to_class(path);
1691 if (NIL_P(extmod)) extmod = rb_ary_tmp_new(0);
1692
1693 if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
1694 VALUE c;
1695
1696 v = r_object0(arg, 0, Qnil);
1697 c = CLASS_OF(v);
1698 if (c != m || FL_TEST(c, FL_SINGLETON)) {
1699 rb_raise(rb_eArgError,
1700 "prepended class %"PRIsVALUE" differs from class %"PRIsVALUE,
1701 path, rb_class_name(c));
1702 }
1703 c = rb_singleton_class(v);
1704 while (RARRAY_LEN(extmod) > 0) {
1705 m = rb_ary_pop(extmod);
1706 rb_prepend_module(c, m);
1707 }
1708 }
1709 else {
1710 must_be_module(m, path);
1711 rb_ary_push(extmod, m);
1712
1713 v = r_object0(arg, 0, extmod);
1714 while (RARRAY_LEN(extmod) > 0) {
1715 m = rb_ary_pop(extmod);
1716 rb_extend_object(v, m);
1717 }
1718 }
1719 }
1720 break;
1721
1722 case TYPE_UCLASS:
1723 {
1724 VALUE c = path2class(r_unique(arg));
1725
1726 if (FL_TEST(c, FL_SINGLETON)) {
1727 rb_raise(rb_eTypeError, "singleton can't be loaded");
1728 }
1729 v = r_object0(arg, 0, extmod);
1730 if (rb_special_const_p(v) || RB_TYPE_P(v, T_OBJECT) || RB_TYPE_P(v, T_CLASS)) {
1731 format_error:
1732 rb_raise(rb_eArgError, "dump format error (user class)");
1733 }
1734 if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
1735 VALUE tmp = rb_obj_alloc(c);
1736
1737 if (TYPE(v) != TYPE(tmp)) goto format_error;
1738 }
1739 RBASIC_SET_CLASS(v, c);
1740 }
1741 break;
1742
1743 case TYPE_NIL:
1744 v = Qnil;
1745 v = r_leave(v, arg);
1746 break;
1747
1748 case TYPE_TRUE:
1749 v = Qtrue;
1750 v = r_leave(v, arg);
1751 break;
1752
1753 case TYPE_FALSE:
1754 v = Qfalse;
1755 v = r_leave(v, arg);
1756 break;
1757
1758 case TYPE_FIXNUM:
1759 {
1760 long i = r_long(arg);
1761 v = LONG2FIX(i);
1762 }
1763 v = r_leave(v, arg);
1764 break;
1765
1766 case TYPE_FLOAT:
1767 {
1768 double d;
1769 VALUE str = r_bytes(arg);
1770 const char *ptr = RSTRING_PTR(str);
1771
1772 if (strcmp(ptr, "nan") == 0) {
1773 d = nan("");
1774 }
1775 else if (strcmp(ptr, "inf") == 0) {
1776 d = HUGE_VAL;
1777 }
1778 else if (strcmp(ptr, "-inf") == 0) {
1779 d = -HUGE_VAL;
1780 }
1781 else {
1782 char *e;
1783 d = strtod(ptr, &e);
1784 d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
1785 }
1786 v = DBL2NUM(d);
1787 v = r_entry(v, arg);
1788 v = r_leave(v, arg);
1789 }
1790 break;
1791
1792 case TYPE_BIGNUM:
1793 {
1794 long len;
1795 VALUE data;
1796 int sign;
1797
1798 sign = r_byte(arg);
1799 len = r_long(arg);
1800 data = r_bytes0(len * 2, arg);
1801 v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
1802 INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
1803 rb_str_resize(data, 0L);
1804 v = r_entry(v, arg);
1805 v = r_leave(v, arg);
1806 }
1807 break;
1808
1809 case TYPE_STRING:
1810 v = r_entry(r_string(arg), arg);
1811 if (!ivp) {
1812 v = r_leave(v, arg);
1813 }
1814 break;
1815
1816 case TYPE_REGEXP:
1817 {
1818 VALUE str = r_bytes(arg);
1819 int options = r_byte(arg);
1820 int has_encoding = FALSE;
1821 st_index_t idx = r_prepare(arg);
1822
1823 if (ivp) {
1824 r_ivar(str, &has_encoding, arg);
1825 *ivp = FALSE;
1826 }
1827 if (!has_encoding) {
1828 /* 1.8 compatibility; remove escapes undefined in 1.8 */
1829 char *ptr = RSTRING_PTR(str), *dst = ptr, *src = ptr;
1830 long len = RSTRING_LEN(str);
1831 long bs = 0;
1832 for (; len-- > 0; *dst++ = *src++) {
1833 switch (*src) {
1834 case '\\': bs++; break;
1835 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1836 case 'm': case 'o': case 'p': case 'q': case 'u': case 'y':
1837 case 'E': case 'F': case 'H': case 'I': case 'J': case 'K':
1838 case 'L': case 'N': case 'O': case 'P': case 'Q': case 'R':
1839 case 'S': case 'T': case 'U': case 'V': case 'X': case 'Y':
1840 if (bs & 1) --dst;
1841 /* fall through */
1842 default: bs = 0; break;
1843 }
1844 }
1845 rb_str_set_len(str, dst - ptr);
1846 }
1847 v = r_entry0(rb_reg_new_str(str, options), idx, arg);
1848 v = r_leave(v, arg);
1849 }
1850 break;
1851
1852 case TYPE_ARRAY:
1853 {
1854 long len = r_long(arg);
1855
1856 v = rb_ary_new2(len);
1857 v = r_entry(v, arg);
1858 arg->readable += len - 1;
1859 while (len--) {
1860 rb_ary_push(v, r_object(arg));
1861 arg->readable--;
1862 }
1863 v = r_leave(v, arg);
1864 arg->readable++;
1865 }
1866 break;
1867
1868 case TYPE_HASH:
1869 case TYPE_HASH_DEF:
1870 {
1871 long len = r_long(arg);
1872
1873 v = rb_hash_new_with_size(len);
1874 v = r_entry(v, arg);
1875 arg->readable += (len - 1) * 2;
1876 while (len--) {
1877 VALUE key = r_object(arg);
1878 VALUE value = r_object(arg);
1879 rb_hash_aset(v, key, value);
1880 arg->readable -= 2;
1881 }
1882 arg->readable += 2;
1883 if (type == TYPE_HASH_DEF) {
1884 RHASH_SET_IFNONE(v, r_object(arg));
1885 }
1886 v = r_leave(v, arg);
1887 }
1888 break;
1889
1890 case TYPE_STRUCT:
1891 {
1892 VALUE mem, values;
1893 long i;
1894 VALUE slot;
1895 st_index_t idx = r_prepare(arg);
1896 VALUE klass = path2class(r_unique(arg));
1897 long len = r_long(arg);
1898
1899 v = rb_obj_alloc(klass);
1900 if (!RB_TYPE_P(v, T_STRUCT)) {
1901 rb_raise(rb_eTypeError, "class %"PRIsVALUE" not a struct", rb_class_name(klass));
1902 }
1903 mem = rb_struct_s_members(klass);
1904 if (RARRAY_LEN(mem) != len) {
1905 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (struct size differs)",
1906 rb_class_name(klass));
1907 }
1908
1909 arg->readable += (len - 1) * 2;
1910 v = r_entry0(v, idx, arg);
1911 values = rb_ary_new2(len);
1912 {
1913 VALUE keywords = Qfalse;
1914 if (RTEST(rb_struct_s_keyword_init(klass))) {
1915 keywords = rb_hash_new();
1916 rb_ary_push(values, keywords);
1917 }
1918
1919 for (i=0; i<len; i++) {
1920 VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
1921 slot = r_symbol(arg);
1922
1923 if (!rb_str_equal(n, slot)) {
1924 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
1925 rb_class_name(klass),
1926 slot, n);
1927 }
1928 if (keywords) {
1929 rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
1930 }
1931 else {
1932 rb_ary_push(values, r_object(arg));
1933 }
1934 arg->readable -= 2;
1935 }
1936 }
1937 rb_struct_initialize(v, values);
1938 v = r_leave(v, arg);
1939 arg->readable += 2;
1940 }
1941 break;
1942
1943 case TYPE_USERDEF:
1944 {
1945 VALUE name = r_unique(arg);
1946 VALUE klass = path2class(name);
1947 VALUE data;
1948 st_data_t d;
1949
1950 if (!rb_obj_respond_to(klass, s_load, TRUE)) {
1951 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method `_load'",
1952 name);
1953 }
1954 data = r_string(arg);
1955 if (ivp) {
1956 r_ivar(data, NULL, arg);
1957 *ivp = FALSE;
1958 }
1959 v = load_funcall(arg, klass, s_load, 1, &data);
1960 v = r_entry(v, arg);
1961 if (st_lookup(compat_allocator_tbl, (st_data_t)rb_get_alloc_func(klass), &d)) {
1962 marshal_compat_t *compat = (marshal_compat_t*)d;
1963 v = compat->loader(klass, v);
1964 }
1965 v = r_post_proc(v, arg);
1966 }
1967 break;
1968
1969 case TYPE_USRMARSHAL:
1970 {
1971 VALUE name = r_unique(arg);
1972 VALUE klass = path2class(name);
1973 VALUE oldclass = 0;
1974 VALUE data;
1975
1976 v = obj_alloc_by_klass(klass, arg, &oldclass);
1977 if (!NIL_P(extmod)) {
1978 /* for the case marshal_load is overridden */
1979 append_extmod(v, extmod);
1980 }
1981 if (!rb_obj_respond_to(v, s_mload, TRUE)) {
1982 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method `marshal_load'",
1983 name);
1984 }
1985 v = r_entry(v, arg);
1986 data = r_object(arg);
1987 load_funcall(arg, v, s_mload, 1, &data);
1988 v = r_fixup_compat(v, arg);
1989 v = r_copy_ivar(v, data);
1990 v = r_post_proc(v, arg);
1991 if (!NIL_P(extmod)) {
1992 if (oldclass) append_extmod(v, extmod);
1993 rb_ary_clear(extmod);
1994 }
1995 }
1996 break;
1997
1998 case TYPE_OBJECT:
1999 {
2000 st_index_t idx = r_prepare(arg);
2001 v = obj_alloc_by_path(r_unique(arg), arg);
2002 if (!RB_TYPE_P(v, T_OBJECT)) {
2003 rb_raise(rb_eArgError, "dump format error");
2004 }
2005 v = r_entry0(v, idx, arg);
2006 r_ivar(v, NULL, arg);
2007 v = r_leave(v, arg);
2008 }
2009 break;
2010
2011 case TYPE_DATA:
2012 {
2013 VALUE name = r_unique(arg);
2014 VALUE klass = path2class(name);
2015 VALUE oldclass = 0;
2016 VALUE r;
2017
2018 v = obj_alloc_by_klass(klass, arg, &oldclass);
2019 if (!RB_TYPE_P(v, T_DATA)) {
2020 rb_raise(rb_eArgError, "dump format error");
2021 }
2022 v = r_entry(v, arg);
2023 if (!rb_obj_respond_to(v, s_load_data, TRUE)) {
2024 rb_raise(rb_eTypeError,
2025 "class %"PRIsVALUE" needs to have instance method `_load_data'",
2026 name);
2027 }
2028 r = r_object0(arg, 0, extmod);
2029 load_funcall(arg, v, s_load_data, 1, &r);
2030 v = r_leave(v, arg);
2031 }
2032 break;
2033
2034 case TYPE_MODULE_OLD:
2035 {
2036 VALUE str = r_bytes(arg);
2037
2038 v = rb_path_to_class(str);
2039 prohibit_ivar("class/module", str);
2040 v = r_entry(v, arg);
2041 v = r_leave(v, arg);
2042 }
2043 break;
2044
2045 case TYPE_CLASS:
2046 {
2047 VALUE str = r_bytes(arg);
2048
2049 v = path2class(str);
2050 prohibit_ivar("class", str);
2051 v = r_entry(v, arg);
2052 v = r_leave(v, arg);
2053 }
2054 break;
2055
2056 case TYPE_MODULE:
2057 {
2058 VALUE str = r_bytes(arg);
2059
2060 v = path2module(str);
2061 prohibit_ivar("module", str);
2062 v = r_entry(v, arg);
2063 v = r_leave(v, arg);
2064 }
2065 break;
2066
2067 case TYPE_SYMBOL:
2068 if (ivp) {
2069 v = r_symreal(arg, *ivp);
2070 *ivp = FALSE;
2071 }
2072 else {
2073 v = r_symreal(arg, 0);
2074 }
2075 v = rb_str_intern(v);
2076 v = r_leave(v, arg);
2077 break;
2078
2079 case TYPE_SYMLINK:
2080 v = rb_str_intern(r_symlink(arg));
2081 break;
2082
2083 default:
2084 rb_raise(rb_eArgError, "dump format error(0x%x)", type);
2085 break;
2086 }
2087
2088 if (v == Qundef) {
2089 rb_raise(rb_eArgError, "dump format error (bad link)");
2090 }
2091
2092 return v;
2093}
2094
2095static VALUE
2096r_object(struct load_arg *arg)
2097{
2098 return r_object0(arg, 0, Qnil);
2099}
2100
2101static void
2102clear_load_arg(struct load_arg *arg)
2103{
2104 if (arg->buf) {
2105 xfree(arg->buf);
2106 arg->buf = 0;
2107 }
2108 arg->buflen = 0;
2109 arg->offset = 0;
2110 arg->readable = 0;
2111 if (!arg->symbols) return;
2112 st_free_table(arg->symbols);
2113 arg->symbols = 0;
2114 st_free_table(arg->data);
2115 arg->data = 0;
2116 if (arg->compat_tbl) {
2117 st_free_table(arg->compat_tbl);
2118 arg->compat_tbl = 0;
2119 }
2120}
2121
2122/*
2123 * call-seq:
2124 * load( source [, proc] ) -> obj
2125 * restore( source [, proc] ) -> obj
2126 *
2127 * Returns the result of converting the serialized data in source into a
2128 * Ruby object (possibly with associated subordinate objects). source
2129 * may be either an instance of IO or an object that responds to
2130 * to_str. If proc is specified, each object will be passed to the proc, as the object
2131 * is being deserialized.
2132 *
2133 * Never pass untrusted data (including user supplied input) to this method.
2134 * Please see the overview for further details.
2135 */
2136static VALUE
2137marshal_load(int argc, VALUE *argv, VALUE _)
2138{
2139 VALUE port, proc;
2140
2141 rb_check_arity(argc, 1, 2);
2142 port = argv[0];
2143 proc = argc > 1 ? argv[1] : Qnil;
2144 return rb_marshal_load_with_proc(port, proc);
2145}
2146
2147VALUE
2148rb_marshal_load_with_proc(VALUE port, VALUE proc)
2149{
2150 int major, minor;
2151 VALUE v;
2152 VALUE wrapper; /* used to avoid memory leak in case of exception */
2153 struct load_arg *arg;
2154
2155 v = rb_check_string_type(port);
2156 if (!NIL_P(v)) {
2157 port = v;
2158 }
2159 else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
2160 rb_check_funcall(port, s_binmode, 0, 0);
2161 }
2162 else {
2163 io_needed();
2164 }
2165 wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
2166 arg->src = port;
2167 arg->offset = 0;
2168 arg->symbols = st_init_numtable();
2169 arg->data = rb_init_identtable();
2170 arg->compat_tbl = 0;
2171 arg->proc = 0;
2172 arg->readable = 0;
2173
2174 if (NIL_P(v))
2175 arg->buf = xmalloc(BUFSIZ);
2176 else
2177 arg->buf = 0;
2178
2179 major = r_byte(arg);
2180 minor = r_byte(arg);
2181 if (major != MARSHAL_MAJOR || minor > MARSHAL_MINOR) {
2182 clear_load_arg(arg);
2183 rb_raise(rb_eTypeError, "incompatible marshal file format (can't be read)\n\
2184\tformat version %d.%d required; %d.%d given",
2185 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2186 }
2187 if (RTEST(ruby_verbose) && minor != MARSHAL_MINOR) {
2188 rb_warn("incompatible marshal file format (can be read)\n\
2189\tformat version %d.%d required; %d.%d given",
2190 MARSHAL_MAJOR, MARSHAL_MINOR, major, minor);
2191 }
2192
2193 if (!NIL_P(proc)) arg->proc = proc;
2194 v = r_object(arg);
2195 clear_load_arg(arg);
2196 RB_GC_GUARD(wrapper);
2197
2198 return v;
2199}
2200
2201/*
2202 * The marshaling library converts collections of Ruby objects into a
2203 * byte stream, allowing them to be stored outside the currently
2204 * active script. This data may subsequently be read and the original
2205 * objects reconstituted.
2206 *
2207 * Marshaled data has major and minor version numbers stored along
2208 * with the object information. In normal use, marshaling can only
2209 * load data written with the same major version number and an equal
2210 * or lower minor version number. If Ruby's ``verbose'' flag is set
2211 * (normally using -d, -v, -w, or --verbose) the major and minor
2212 * numbers must match exactly. Marshal versioning is independent of
2213 * Ruby's version numbers. You can extract the version by reading the
2214 * first two bytes of marshaled data.
2215 *
2216 * str = Marshal.dump("thing")
2217 * RUBY_VERSION #=> "1.9.0"
2218 * str[0].ord #=> 4
2219 * str[1].ord #=> 8
2220 *
2221 * Some objects cannot be dumped: if the objects to be dumped include
2222 * bindings, procedure or method objects, instances of class IO, or
2223 * singleton objects, a TypeError will be raised.
2224 *
2225 * If your class has special serialization needs (for example, if you
2226 * want to serialize in some specific format), or if it contains
2227 * objects that would otherwise not be serializable, you can implement
2228 * your own serialization strategy.
2229 *
2230 * There are two methods of doing this, your object can define either
2231 * marshal_dump and marshal_load or _dump and _load. marshal_dump will take
2232 * precedence over _dump if both are defined. marshal_dump may result in
2233 * smaller Marshal strings.
2234 *
2235 * == Security considerations
2236 *
2237 * By design, Marshal.load can deserialize almost any class loaded into the
2238 * Ruby process. In many cases this can lead to remote code execution if the
2239 * Marshal data is loaded from an untrusted source.
2240 *
2241 * As a result, Marshal.load is not suitable as a general purpose serialization
2242 * format and you should never unmarshal user supplied input or other untrusted
2243 * data.
2244 *
2245 * If you need to deserialize untrusted data, use JSON or another serialization
2246 * format that is only able to load simple, 'primitive' types such as String,
2247 * Array, Hash, etc. Never allow user input to specify arbitrary types to
2248 * deserialize into.
2249 *
2250 * == marshal_dump and marshal_load
2251 *
2252 * When dumping an object the method marshal_dump will be called.
2253 * marshal_dump must return a result containing the information necessary for
2254 * marshal_load to reconstitute the object. The result can be any object.
2255 *
2256 * When loading an object dumped using marshal_dump the object is first
2257 * allocated then marshal_load is called with the result from marshal_dump.
2258 * marshal_load must recreate the object from the information in the result.
2259 *
2260 * Example:
2261 *
2262 * class MyObj
2263 * def initialize name, version, data
2264 * @name = name
2265 * @version = version
2266 * @data = data
2267 * end
2268 *
2269 * def marshal_dump
2270 * [@name, @version]
2271 * end
2272 *
2273 * def marshal_load array
2274 * @name, @version = array
2275 * end
2276 * end
2277 *
2278 * == _dump and _load
2279 *
2280 * Use _dump and _load when you need to allocate the object you're restoring
2281 * yourself.
2282 *
2283 * When dumping an object the instance method _dump is called with an Integer
2284 * which indicates the maximum depth of objects to dump (a value of -1 implies
2285 * that you should disable depth checking). _dump must return a String
2286 * containing the information necessary to reconstitute the object.
2287 *
2288 * The class method _load should take a String and use it to return an object
2289 * of the same class.
2290 *
2291 * Example:
2292 *
2293 * class MyObj
2294 * def initialize name, version, data
2295 * @name = name
2296 * @version = version
2297 * @data = data
2298 * end
2299 *
2300 * def _dump level
2301 * [@name, @version].join ':'
2302 * end
2303 *
2304 * def self._load args
2305 * new(*args.split(':'))
2306 * end
2307 * end
2308 *
2309 * Since Marshal.dump outputs a string you can have _dump return a Marshal
2310 * string which is Marshal.loaded in _load for complex objects.
2311 */
2312void
2313Init_marshal(void)
2314{
2315#undef rb_intern
2316#define rb_intern(str) rb_intern_const(str)
2317
2318 VALUE rb_mMarshal = rb_define_module("Marshal");
2319#define set_id(sym) sym = rb_intern_const(name_##sym)
2320 set_id(s_dump);
2321 set_id(s_load);
2322 set_id(s_mdump);
2323 set_id(s_mload);
2324 set_id(s_dump_data);
2325 set_id(s_load_data);
2326 set_id(s_alloc);
2327 set_id(s_call);
2328 set_id(s_getbyte);
2329 set_id(s_read);
2330 set_id(s_write);
2331 set_id(s_binmode);
2332 set_id(s_encoding_short);
2333 set_id(s_ruby2_keywords_flag);
2334
2335 rb_define_module_function(rb_mMarshal, "dump", marshal_dump, -1);
2336 rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);
2337 rb_define_module_function(rb_mMarshal, "restore", marshal_load, -1);
2338
2339 /* major version */
2340 rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
2341 /* minor version */
2342 rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
2343}
2344
2345static st_table *
2346compat_allocator_table(void)
2347{
2348 if (compat_allocator_tbl) return compat_allocator_tbl;
2349 compat_allocator_tbl = st_init_numtable();
2350#undef RUBY_UNTYPED_DATA_WARNING
2351#define RUBY_UNTYPED_DATA_WARNING 0
2352 compat_allocator_tbl_wrapper =
2353 Data_Wrap_Struct(0, mark_marshal_compat_t, 0, compat_allocator_tbl);
2354 rb_gc_register_mark_object(compat_allocator_tbl_wrapper);
2355 return compat_allocator_tbl;
2356}
2357
2358VALUE
2359rb_marshal_dump(VALUE obj, VALUE port)
2360{
2361 return rb_marshal_dump_limited(obj, port, -1);
2362}
2363
2364VALUE
2365rb_marshal_load(VALUE port)
2366{
2367 return rb_marshal_load_with_proc(port, Qnil);
2368}
#define SHORTDN(x)
Definition: marshal.c:30
#define SIZEOF_BDIGIT
__inline__ const void *__restrict__ size_t len
#define BDIGIT