28#define BITSPERSHORT (2*CHAR_BIT)
29#define SHORTMASK ((1<<BITSPERSHORT)-1)
30#define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
32#if SIZEOF_SHORT == SIZEOF_BDIGIT
33#define SHORTLEN(x) (x)
48#define SHORTLEN(x) shortlen((x),d)
51#define MARSHAL_MAJOR 4
52#define MARSHAL_MINOR 8
57#define TYPE_FIXNUM 'i'
59#define TYPE_EXTENDED 'e'
60#define TYPE_UCLASS 'C'
61#define TYPE_OBJECT 'o'
63#define TYPE_USERDEF 'u'
64#define TYPE_USRMARSHAL 'U'
66#define TYPE_BIGNUM 'l'
67#define TYPE_STRING '"'
68#define TYPE_REGEXP '/'
71#define TYPE_HASH_DEF '}'
72#define TYPE_STRUCT 'S'
73#define TYPE_MODULE_OLD 'M'
75#define TYPE_MODULE 'm'
77#define TYPE_SYMBOL ':'
78#define TYPE_SYMLINK ';'
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;
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"
106 VALUE (*dumper)(VALUE);
107 VALUE (*loader)(VALUE, VALUE);
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);
116mark_marshal_compat_i(st_data_t key, st_data_t value, st_data_t _)
118 marshal_compat_t *p = (marshal_compat_t *)value;
119 rb_gc_mark(p->newclass);
120 rb_gc_mark(p->oldclass);
125mark_marshal_compat_t(void *tbl)
128 st_foreach(tbl, mark_marshal_compat_i, 0);
131static st_table *compat_allocator_table(void);
134rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE))
136 marshal_compat_t *compat;
137 rb_alloc_func_t allocator = rb_get_alloc_func(newclass);
140 rb_raise(rb_eTypeError, "no allocator");
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;
151 st_insert(compat_allocator_table(), (st_data_t)allocator, (st_data_t)compat);
158 st_table *compat_tbl;
162struct dump_call_arg {
164 struct dump_arg *arg;
169check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
172 rb_raise(rb_eRuntimeError, "Marshal.dump reentered at %s",
179check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv,
180 struct dump_arg *arg, const char *name)
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",
188 return check_dump_arg(ret, arg, name);
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)
196static void clear_dump_arg(struct dump_arg *arg);
199mark_dump_arg(void *ptr)
201 struct dump_arg *p = ptr;
204 rb_mark_set(p->symbols);
205 rb_mark_set(p->data);
206 rb_mark_hash(p->compat_tbl);
211free_dump_arg(void *ptr)
218memsize_dump_arg(const void *ptr)
220 return sizeof(struct dump_arg);
223static const rb_data_type_t dump_arg_data = {
225 {mark_dump_arg, free_dump_arg, memsize_dump_arg,},
226 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
230must_not_be_anonymous(const char *type, VALUE path)
232 char *n = RSTRING_PTR(path);
234 if (!rb_enc_asciicompat(rb_enc_get(path))) {
236 rb_raise(rb_eTypeError, "can't dump non-ascii %s name % "PRIsVALUE,
240 rb_raise(rb_eTypeError, "can't dump anonymous %s % "PRIsVALUE,
247class2path(VALUE klass)
249 VALUE path = rb_class_path(klass);
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);
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);
264w_nbyte(const char *s, long n, struct dump_arg *arg)
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);
275w_byte(char c, struct dump_arg *arg)
281w_bytes(const char *s, long n, struct dump_arg *arg)
287#define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
290w_short(int x, struct dump_arg *arg)
292 w_byte((char)((x >> 0) & 0xff), arg);
293 w_byte((char)((x >> 8) & 0xff), arg);
297w_long(long x, struct dump_arg *arg)
299 char buf[sizeof(long)+1];
300 int i = ruby_marshal_write_long(x, buf);
302 rb_raise(rb_eTypeError, "long too big to dump");
304 w_nbyte(buf, i, arg);
308ruby_marshal_write_long(long x, char *buf)
313 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
314 /* big long does not fit in 4 bytes */
323 if (0 < x && x < 123) {
324 buf[0] = (char)(x + 5);
327 if (-124 < x && x < 0) {
328 buf[0] = (char)((x - 5)&0xff);
331 for (i=1;i<(int)sizeof(long)+1;i++) {
332 buf[i] = (char)(x & 0xff);
347#define DECIMAL_MANT (53-16) /* from IEEE754 double precision */
351#elif DBL_MANT_DIG > 24
353#elif DBL_MANT_DIG > 16
360load_mantissa(double d, const char *buf, long len)
363 if (--len > 0 && !*buf++) { /* binary mantissa mark */
364 int e, s = d < 0, dig = 0;
367 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
371 default: m = *buf++ & 0xff; /* fall through */
373 case 3: m = (m << 8) | (*buf++ & 0xff); /* fall through */
376 case 2: m = (m << 8) | (*buf++ & 0xff); /* fall through */
379 case 1: m = (m << 8) | (*buf++ & 0xff);
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);
391#define load_mantissa(d, buf, len) (d)
395#define FLOAT_DIG (DBL_DIG+2)
401w_float(double d, struct dump_arg *arg)
403 char buf[FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
406 if (d < 0) w_cstr("-inf", arg);
407 else w_cstr("inf", arg);
413 if (signbit(d)) w_cstr("-0", arg);
414 else w_cstr("0", arg);
417 int decpt, sign, digs, len = 0;
418 char *e, *p = ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
419 if (sign) buf[len++] = '-';
421 if (decpt < -3 || decpt > digs) {
423 if (--digs > 0) buf[len++] = '.';
424 memcpy(buf + len, p + 1, digs);
426 len += snprintf(buf + len, sizeof(buf) - len, "e%d", decpt - 1);
428 else if (decpt > 0) {
429 memcpy(buf + len, p, decpt);
431 if ((digs -= decpt) > 0) {
433 memcpy(buf + len, p + decpt, digs);
441 memset(buf + len, '0', -decpt);
444 memcpy(buf + len, p, digs);
448 w_bytes(buf, len, arg);
453w_symbol(VALUE sym, struct dump_arg *arg)
458 if (st_lookup(arg->symbols, sym, &num)) {
459 w_byte(TYPE_SYMLINK, arg);
460 w_long((long)num, arg);
463 const VALUE orig_sym = sym;
464 sym = rb_sym2str(sym);
466 rb_raise(rb_eTypeError, "can't dump anonymous ID %"PRIdVALUE, sym);
468 encname = encoding_name(sym, arg);
469 if (NIL_P(encname) ||
470 rb_enc_str_coderange(sym) == ENC_CODERANGE_7BIT) {
474 w_byte(TYPE_IVAR, arg);
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;
484 w_encoding(encname, &c_arg);
490w_unique(VALUE s, struct dump_arg *arg)
492 must_not_be_anonymous("class", s);
493 w_symbol(rb_str_intern(s), arg);
496static void w_object(VALUE,struct dump_arg*,int);
499hash_each(VALUE key, VALUE value, VALUE v)
501 struct dump_call_arg *arg = (void *)v;
502 w_object(key, arg->arg, arg->limit);
503 w_object(value, arg->arg, arg->limit);
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))
512w_extended(VALUE klass, struct dump_arg *arg, int check)
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");
520 klass = RCLASS_SUPER(klass);
522 while (BUILTIN_TYPE(klass) == T_ICLASS) {
523 VALUE path = rb_class_name(RBASIC(klass)->klass);
524 w_byte(TYPE_EXTENDED, arg);
526 klass = RCLASS_SUPER(klass);
531w_class(char type, VALUE obj, struct dump_arg *arg, int check)
537 if (arg->compat_tbl &&
538 st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) {
539 obj = (VALUE)real_obj;
541 klass = CLASS_OF(obj);
542 w_extended(klass, arg, check);
544 path = class2path(rb_class_real(klass));
549w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
551 VALUE klass = CLASS_OF(obj);
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);
561#define to_be_skipped_id(id) (id == rb_id_encoding() || id == s_encoding_short || id == s_ruby2_keywords_flag || !rb_id2str(id))
564 struct dump_call_arg *dump;
569w_obj_each(st_data_t key, st_data_t val, st_data_t a)
572 VALUE value = (VALUE)val;
573 struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
574 struct dump_call_arg *arg = ivarg->dump;
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",
581 if (id == s_ruby2_keywords_flag) {
582 rb_warn("instance variable `"name_s_ruby2_keywords_flag"' on class %"PRIsVALUE" is not dumped",
587 if (!ivarg->num_ivar) {
588 rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
592 w_symbol(ID2SYM(id), arg->arg);
593 w_object(value, arg->arg, arg->limit);
598obj_count_ivars(st_data_t key, st_data_t val, st_data_t a)
601 if (!to_be_skipped_id(id)) ++*(st_index_t *)a;
606encoding_name(VALUE obj, struct dump_arg *arg)
608 if (rb_enc_capable(obj)) {
609 int encidx = rb_enc_get_index(obj);
610 rb_encoding *enc = 0;
613 if (encidx <= 0 || !(enc = rb_enc_from_index(encidx))) {
617 /* special treatment for US-ASCII and UTF-8 */
618 if (encidx == rb_usascii_encindex()) {
621 else if (encidx == rb_utf8_encindex()) {
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);
639w_encoding(VALUE encname, struct dump_call_arg *arg)
641 int limit = arg->limit;
642 if (limit >= 0) ++limit;
646 w_symbol(ID2SYM(s_encoding_short), arg->arg);
647 w_object(encname, arg->arg, limit);
652 w_symbol(ID2SYM(rb_id_encoding()), arg->arg);
653 w_object(encname, arg->arg, limit);
658has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
660 st_index_t enc = !NIL_P(encname);
662 st_index_t ruby2_keywords_flag = 0;
664 if (SPECIAL_CONST_P(obj)) goto generic;
665 switch (BUILTIN_TYPE(obj)) {
669 break; /* counted elsewhere */
671 ruby2_keywords_flag = RHASH(obj)->basic.flags & RHASH_PASS_AS_KEYWORDS ? 1 : 0;
675 rb_ivar_foreach(obj, obj_count_ivars, (st_data_t)&num);
676 if (ruby2_keywords_flag || num) *ivobj = obj;
679 return num + enc + ruby2_keywords_flag;
683w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
685 struct w_ivar_arg ivarg = {arg, num};
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",
695w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
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);
706 if (ivobj != Qundef && num) {
707 w_ivar_each(ivobj, num, arg);
712w_objivar(VALUE obj, struct dump_call_arg *arg)
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);
722w_object(VALUE obj, struct dump_arg *arg, int limit)
724 struct dump_call_arg c_arg;
725 VALUE ivobj = Qundef;
727 st_index_t hasiv = 0;
728 VALUE encname = Qnil;
731 rb_raise(rb_eArgError, "exceed depth limit");
734 if (limit > 0) limit--;
739 if (st_lookup(arg->data, obj, &num)) {
740 w_byte(TYPE_LINK, arg);
741 w_long((long)num, arg);
746 w_byte(TYPE_NIL, arg);
748 else if (obj == Qtrue) {
749 w_byte(TYPE_TRUE, arg);
751 else if (obj == Qfalse) {
752 w_byte(TYPE_FALSE, arg);
754 else if (FIXNUM_P(obj)) {
756 w_byte(TYPE_FIXNUM, arg);
757 w_long(FIX2INT(obj), arg);
759 if (RSHIFT((long)obj, 31) == 0 || RSHIFT((long)obj, 31) == -1) {
760 w_byte(TYPE_FIXNUM, arg);
761 w_long(FIX2LONG(obj), arg);
764 w_object(rb_int2big(FIX2LONG(obj)), arg, limit);
768 else if (SYMBOL_P(obj)) {
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);
779 if (!RBASIC_CLASS(obj)) {
780 rb_raise(rb_eTypeError, "can't dump internal %s",
781 rb_builtin_type_name(BUILTIN_TYPE(obj)));
784 if (rb_obj_respond_to(obj, s_mdump, TRUE)) {
785 st_add_direct(arg->data, obj, arg->data->num_entries);
787 v = dump_funcall(arg, obj, s_mdump, 0, 0);
788 w_class(TYPE_USRMARSHAL, obj, arg, FALSE);
789 w_object(v, arg, limit);
792 if (rb_obj_respond_to(obj, s_dump, TRUE)) {
793 VALUE ivobj2 = Qundef;
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");
802 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
803 hasiv2 = has_ivars(v, (encname2 = encoding_name(v, arg)), &ivobj2);
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);
813 w_ivar(hasiv, ivobj, encname, &c_arg);
815 st_add_direct(arg->data, obj, arg->data->num_entries);
819 st_add_direct(arg->data, obj, arg->data->num_entries);
821 hasiv = has_ivars(obj, (encname = encoding_name(obj, arg)), &ivobj);
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,
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();
834 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
835 if (obj != real_obj && ivobj == Qundef) hasiv = 0;
838 if (hasiv) w_byte(TYPE_IVAR, arg);
840 switch (BUILTIN_TYPE(obj)) {
842 if (FL_TEST(obj, FL_SINGLETON)) {
843 rb_raise(rb_eTypeError, "singleton class can't be dumped");
845 w_byte(TYPE_CLASS, arg);
847 VALUE path = class2path(obj);
848 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
854 w_byte(TYPE_MODULE, arg);
856 VALUE path = class2path(obj);
857 w_bytes(RSTRING_PTR(path), RSTRING_LEN(path), arg);
863 w_byte(TYPE_FLOAT, arg);
864 w_float(RFLOAT_VALUE(obj), arg);
868 w_byte(TYPE_BIGNUM, arg);
870 char sign = BIGNUM_SIGN(obj) ? '+' : '-';
871 size_t len = BIGNUM_LEN(obj);
874 BDIGIT *d = BIGNUM_DIGITS(obj);
876 slen = SHORTLEN(len);
877 if (LONG_MAX < slen) {
878 rb_raise(rb_eTypeError, "too big Bignum can't be dumped");
882 w_long((long)slen, arg);
883 for (j = 0; j < len; j++) {
884#if SIZEOF_BDIGIT > SIZEOF_SHORT
888 for (i=0; i<SIZEOF_BDIGIT; i+=SIZEOF_SHORT) {
889 w_short(num & SHORTMASK, arg);
891 if (j == len - 1 && num == 0) break;
902 w_uclass(obj, rb_cString, arg);
903 w_byte(TYPE_STRING, arg);
904 w_bytes(RSTRING_PTR(obj), RSTRING_LEN(obj), arg);
908 w_uclass(obj, rb_cRegexp, arg);
909 w_byte(TYPE_REGEXP, arg);
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);
918 w_uclass(obj, rb_cArray, arg);
919 w_byte(TYPE_ARRAY, arg);
921 long i, len = RARRAY_LEN(obj);
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");
934 w_uclass(obj, rb_cHash, arg);
935 if (NIL_P(RHASH_IFNONE(obj))) {
936 w_byte(TYPE_HASH, arg);
938 else if (FL_TEST(obj, RHASH_PROC_DEFAULT)) {
939 rb_raise(rb_eTypeError, "can't dump hash with default proc");
942 w_byte(TYPE_HASH_DEF, arg);
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);
952 w_class(TYPE_STRUCT, obj, arg, TRUE);
954 long len = RSTRUCT_LEN(obj);
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);
968 w_class(TYPE_OBJECT, obj, arg, TRUE);
969 w_objivar(obj, &c_arg);
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,
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);
988 rb_raise(rb_eTypeError, "can't dump %"PRIsVALUE,
995 w_ivar(hasiv, ivobj, encname, &c_arg);
1000clear_dump_arg(struct dump_arg *arg)
1002 if (!arg->symbols) return;
1003 st_free_table(arg->symbols);
1005 st_free_table(arg->data);
1007 if (arg->compat_tbl) {
1008 st_free_table(arg->compat_tbl);
1009 arg->compat_tbl = 0;
1011 if (arg->encodings) {
1012 st_free_table(arg->encodings);
1017NORETURN(static inline void io_needed(void));
1021 rb_raise(rb_eTypeError, "instance of IO needed");
1026 * dump( obj [, anIO] , limit=-1 ) -> anIO
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.
1035 * def initialize(str)
1043 * (produces no output)
1045 * o = Klass.new("hello\n")
1046 * data = Marshal.dump(o)
1047 * obj = Marshal.load(data)
1048 * obj.say_hello #=> "hello\n"
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
1054 * * an instance of MatchData, Data, Method, UnboundMethod, Proc, Thread,
1055 * ThreadGroup, Continuation
1056 * * objects which define singleton methods
1059marshal_dump(int argc, VALUE *argv, VALUE _)
1061 VALUE obj, port, a1, a2;
1065 rb_scan_args(argc, argv, "12", &obj, &a1, &a2);
1067 if (!NIL_P(a2)) limit = NUM2INT(a2);
1068 if (NIL_P(a1)) io_needed();
1071 else if (argc == 2) {
1072 if (FIXNUM_P(a1)) limit = FIX2INT(a1);
1073 else if (NIL_P(a1)) io_needed();
1076 return rb_marshal_dump_limited(obj, port, limit);
1080rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
1082 struct dump_arg *arg;
1083 VALUE wrapper; /* used to avoid memory leak in case of exception */
1085 wrapper = TypedData_Make_Struct(0, struct dump_arg, &dump_arg_data, arg);
1087 arg->symbols = st_init_numtable();
1088 arg->data = rb_init_identtable();
1089 arg->compat_tbl = 0;
1091 arg->str = rb_str_buf_new(0);
1093 if (!rb_respond_to(port, s_write)) {
1097 dump_check_funcall(arg, port, s_binmode, 0, 0);
1103 w_byte(MARSHAL_MAJOR, arg);
1104 w_byte(MARSHAL_MINOR, arg);
1106 w_object(obj, arg, limit);
1108 rb_io_write(arg->dest, arg->str);
1109 rb_str_resize(arg->str, 0);
1111 clear_dump_arg(arg);
1112 RB_GC_GUARD(wrapper);
1126 st_table *compat_tbl;
1130check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
1132 if (!arg->symbols) {
1133 rb_raise(rb_eRuntimeError, "Marshal.load reentered at %s",
1138#define load_funcall(arg, obj, sym, argc, argv) \
1139 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym)
1141static void clear_load_arg(struct load_arg *arg);
1144mark_load_arg(void *ptr)
1146 struct load_arg *p = ptr;
1149 rb_mark_tbl(p->symbols);
1150 rb_mark_tbl(p->data);
1151 rb_mark_hash(p->compat_tbl);
1155free_load_arg(void *ptr)
1157 clear_load_arg(ptr);
1162memsize_load_arg(const void *ptr)
1164 return sizeof(struct load_arg);
1167static const rb_data_type_t load_arg_data = {
1169 {mark_load_arg, free_load_arg, memsize_load_arg,},
1170 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
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);
1179NORETURN(static void too_short(void));
1183 rb_raise(rb_eArgError, "marshal data too short");
1187r_prepare(struct load_arg *arg)
1189 st_index_t idx = arg->data->num_entries;
1191 st_insert(arg->data, (st_data_t)idx, (st_data_t)Qundef);
1196r_byte1_buffered(struct load_arg *arg)
1198 if (arg->buflen == 0) {
1199 long readable = arg->readable < BUFSIZ ? arg->readable : BUFSIZ;
1200 VALUE str, n = LONG2NUM(readable);
1202 str = load_funcall(arg, arg->src, s_read, 1, &n);
1203 if (NIL_P(str)) too_short();
1205 memcpy(arg->buf, RSTRING_PTR(str), RSTRING_LEN(str));
1207 arg->buflen = RSTRING_LEN(str);
1210 return arg->buf[arg->offset++];
1214r_byte(struct load_arg *arg)
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++];
1227 if (arg->readable >0 || arg->buflen > 0) {
1228 c = r_byte1_buffered(arg);
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);
1239NORETURN(static void long_toobig(int size));
1242long_toobig(int size)
1244 rb_raise(rb_eTypeError, "long too big for this architecture (size "
1245 STRINGIZE(SIZEOF_LONG)", given %d)", size);
1249r_long(struct load_arg *arg)
1252 int c = (signed char)r_byte(arg);
1255 if (c == 0) return 0;
1257 if (4 < c && c < 128) {
1260 if (c > (int)sizeof(long)) long_toobig(c);
1263 x |= (long)r_byte(arg) << (8*i);
1267 if (-129 < c && c < -4) {
1271 if (c > (int)sizeof(long)) long_toobig(c);
1274 x &= ~((long)0xff << (8*i));
1275 x |= (long)r_byte(arg) << (8*i);
1282ruby_marshal_read_long(const char **buf, long len)
1286 struct load_arg arg;
1287 memset(&arg, 0, sizeof(arg));
1288 arg.src = rb_setup_fake_str(&src, *buf, len, 0);
1295r_bytes1(long len, struct load_arg *arg)
1297 VALUE str, n = LONG2NUM(len);
1299 str = load_funcall(arg, arg->src, s_read, 1, &n);
1300 if (NIL_P(str)) too_short();
1302 if (RSTRING_LEN(str) != len) too_short();
1308r_bytes1_buffered(long len, struct load_arg *arg)
1312 if (len <= arg->buflen) {
1313 str = rb_str_new(arg->buf+arg->offset, len);
1318 long buflen = arg->buflen;
1319 long readable = arg->readable + 1;
1320 long tmp_len, read_len, need_len = len - buflen;
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();
1330 tmp_len = RSTRING_LEN(tmp);
1332 if (tmp_len < need_len) too_short();
1334 str = rb_str_new(arg->buf+arg->offset, buflen);
1335 rb_str_cat(str, RSTRING_PTR(tmp), need_len);
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;
1351#define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1354r_bytes0(long len, struct load_arg *arg)
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);
1369 if (arg->readable > 0 || arg->buflen > 0) {
1370 str = r_bytes1_buffered(len, arg);
1373 str = r_bytes1(len, arg);
1380name_equal(const char *name, size_t nlen, const char *p, long l)
1382 if ((size_t)l != nlen || *p != *name) return 0;
1383 return nlen == 1 || memcmp(p+1, name+1, nlen-1) == 0;
1387sym2encidx(VALUE sym, VALUE val)
1389 static const char name_encoding[8] = "encoding";
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));
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();
1408ruby2_keywords_flag_check(VALUE sym)
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)) {
1421r_symlink(struct load_arg *arg)
1424 long num = r_long(arg);
1426 if (!st_lookup(arg->symbols, num, &sym)) {
1427 rb_raise(rb_eArgError, "bad symbol");
1433r_symreal(struct load_arg *arg, int ivar)
1435 VALUE s = r_bytes(arg);
1438 st_index_t n = arg->symbols->num_entries;
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);
1443 long num = r_long(arg);
1445 sym = r_symbol(arg);
1446 idx = sym2encidx(sym, r_object(arg));
1449 if (idx > 0) rb_enc_associate_index(s, idx);
1455r_symbol(struct load_arg *arg)
1460 switch ((type = r_byte(arg))) {
1462 rb_raise(rb_eArgError, "dump format error for symbol(0x%x)", type);
1467 return r_symreal(arg, ivar);
1470 rb_raise(rb_eArgError, "dump format error (symlink with encoding)");
1472 return r_symlink(arg);
1477r_unique(struct load_arg *arg)
1479 return r_symbol(arg);
1483r_string(struct load_arg *arg)
1485 return r_bytes(arg);
1489r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
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);
1496 st_insert(arg->data, num, (st_data_t)v);
1502r_fixup_compat(VALUE v, struct load_arg *arg)
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);
1519r_post_proc(VALUE v, struct load_arg *arg)
1522 v = load_funcall(arg, arg->proc, s_call, 1, &v);
1528r_leave(VALUE v, struct load_arg *arg)
1530 v = r_fixup_compat(v, arg);
1531 v = r_post_proc(v, arg);
1536copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
1538 VALUE obj = (VALUE)arg, value = (VALUE)val;
1541 if (!rb_ivar_defined(obj, vid))
1542 rb_ivar_set(obj, vid, value);
1547r_copy_ivar(VALUE v, VALUE data)
1549 rb_ivar_foreach(data, copy_ivar_i, (st_data_t)v);
1554r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
1561 VALUE sym = r_symbol(arg);
1562 VALUE val = r_object(arg);
1563 int idx = sym2encidx(sym, val);
1565 if (rb_enc_capable(obj)) {
1566 rb_enc_associate_index(obj, idx);
1569 rb_raise(rb_eArgError, "%"PRIsVALUE" is not enc_capable", obj);
1571 if (has_encoding) *has_encoding = TRUE;
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;
1578 rb_raise(rb_eArgError, "ruby2_keywords flag is given but %"PRIsVALUE" is not a Hash", obj);
1582 rb_ivar_set(obj, rb_intern_str(sym), val);
1584 } while (--len > 0);
1589path2class(VALUE path)
1591 VALUE v = rb_path_to_class(path);
1593 if (!RB_TYPE_P(v, T_CLASS)) {
1594 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to class", path);
1599#define path2module(path) must_be_module(rb_path_to_class(path), path)
1602must_be_module(VALUE v, VALUE path)
1604 if (!RB_TYPE_P(v, T_MODULE)) {
1605 rb_raise(rb_eArgError, "%"PRIsVALUE" does not refer to module", path);
1611obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
1614 rb_alloc_func_t allocator;
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;
1623 if (!arg->compat_tbl) {
1624 arg->compat_tbl = rb_init_identtable();
1626 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
1630 return rb_obj_alloc(klass);
1634obj_alloc_by_path(VALUE path, struct load_arg *arg)
1636 return obj_alloc_by_klass(path2class(path), arg, 0);
1640append_extmod(VALUE obj, VALUE extmod)
1642 long i = RARRAY_LEN(extmod);
1644 VALUE m = RARRAY_AREF(extmod, --i);
1645 rb_extend_object(obj, m);
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"'", \
1658r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
1661 int type = r_byte(arg);
1668 if (!st_lookup(arg->data, (st_data_t)id, &link)) {
1669 rb_raise(rb_eArgError, "dump format error (unlinked)");
1672 v = r_post_proc(v, arg);
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);
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);
1693 if (RB_TYPE_P(m, T_CLASS)) { /* prepended */
1696 v = r_object0(arg, 0, Qnil);
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));
1703 c = rb_singleton_class(v);
1704 while (RARRAY_LEN(extmod) > 0) {
1705 m = rb_ary_pop(extmod);
1706 rb_prepend_module(c, m);
1710 must_be_module(m, path);
1711 rb_ary_push(extmod, m);
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);
1724 VALUE c = path2class(r_unique(arg));
1726 if (FL_TEST(c, FL_SINGLETON)) {
1727 rb_raise(rb_eTypeError, "singleton can't be loaded");
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)) {
1732 rb_raise(rb_eArgError, "dump format error (user class)");
1734 if (RB_TYPE_P(v, T_MODULE) || !RTEST(rb_class_inherited_p(c, RBASIC(v)->klass))) {
1735 VALUE tmp = rb_obj_alloc(c);
1737 if (TYPE(v) != TYPE(tmp)) goto format_error;
1739 RBASIC_SET_CLASS(v, c);
1745 v = r_leave(v, arg);
1750 v = r_leave(v, arg);
1755 v = r_leave(v, arg);
1760 long i = r_long(arg);
1763 v = r_leave(v, arg);
1769 VALUE str = r_bytes(arg);
1770 const char *ptr = RSTRING_PTR(str);
1772 if (strcmp(ptr, "nan") == 0) {
1775 else if (strcmp(ptr, "inf") == 0) {
1778 else if (strcmp(ptr, "-inf") == 0) {
1783 d = strtod(ptr, &e);
1784 d = load_mantissa(d, e, RSTRING_LEN(str) - (e - ptr));
1787 v = r_entry(v, arg);
1788 v = r_leave(v, 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);
1810 v = r_entry(r_string(arg), arg);
1812 v = r_leave(v, arg);
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);
1824 r_ivar(str, &has_encoding, arg);
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);
1832 for (; len-- > 0; *dst++ = *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':
1842 default: bs = 0; break;
1845 rb_str_set_len(str, dst - ptr);
1847 v = r_entry0(rb_reg_new_str(str, options), idx, arg);
1848 v = r_leave(v, arg);
1854 long len = r_long(arg);
1856 v = rb_ary_new2(len);
1857 v = r_entry(v, arg);
1858 arg->readable += len - 1;
1860 rb_ary_push(v, r_object(arg));
1863 v = r_leave(v, arg);
1871 long len = r_long(arg);
1873 v = rb_hash_new_with_size(len);
1874 v = r_entry(v, arg);
1875 arg->readable += (len - 1) * 2;
1877 VALUE key = r_object(arg);
1878 VALUE value = r_object(arg);
1879 rb_hash_aset(v, key, value);
1883 if (type == TYPE_HASH_DEF) {
1884 RHASH_SET_IFNONE(v, r_object(arg));
1886 v = r_leave(v, arg);
1895 st_index_t idx = r_prepare(arg);
1896 VALUE klass = path2class(r_unique(arg));
1897 long len = r_long(arg);
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));
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));
1909 arg->readable += (len - 1) * 2;
1910 v = r_entry0(v, idx, arg);
1911 values = rb_ary_new2(len);
1913 VALUE keywords = Qfalse;
1914 if (RTEST(rb_struct_s_keyword_init(klass))) {
1915 keywords = rb_hash_new();
1916 rb_ary_push(values, keywords);
1919 for (i=0; i<len; i++) {
1920 VALUE n = rb_sym2str(RARRAY_AREF(mem, i));
1921 slot = r_symbol(arg);
1923 if (!rb_str_equal(n, slot)) {
1924 rb_raise(rb_eTypeError, "struct %"PRIsVALUE" not compatible (:%"PRIsVALUE" for :%"PRIsVALUE")",
1925 rb_class_name(klass),
1929 rb_hash_aset(keywords, RARRAY_AREF(mem, i), r_object(arg));
1932 rb_ary_push(values, r_object(arg));
1937 rb_struct_initialize(v, values);
1938 v = r_leave(v, arg);
1945 VALUE name = r_unique(arg);
1946 VALUE klass = path2class(name);
1950 if (!rb_obj_respond_to(klass, s_load, TRUE)) {
1951 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method `_load'",
1954 data = r_string(arg);
1956 r_ivar(data, NULL, arg);
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);
1965 v = r_post_proc(v, arg);
1969 case TYPE_USRMARSHAL:
1971 VALUE name = r_unique(arg);
1972 VALUE klass = path2class(name);
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);
1981 if (!rb_obj_respond_to(v, s_mload, TRUE)) {
1982 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method `marshal_load'",
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);
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");
2005 v = r_entry0(v, idx, arg);
2006 r_ivar(v, NULL, arg);
2007 v = r_leave(v, arg);
2013 VALUE name = r_unique(arg);
2014 VALUE klass = path2class(name);
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");
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'",
2028 r = r_object0(arg, 0, extmod);
2029 load_funcall(arg, v, s_load_data, 1, &r);
2030 v = r_leave(v, arg);
2034 case TYPE_MODULE_OLD:
2036 VALUE str = r_bytes(arg);
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);
2047 VALUE str = r_bytes(arg);
2049 v = path2class(str);
2050 prohibit_ivar("class", str);
2051 v = r_entry(v, arg);
2052 v = r_leave(v, arg);
2058 VALUE str = r_bytes(arg);
2060 v = path2module(str);
2061 prohibit_ivar("module", str);
2062 v = r_entry(v, arg);
2063 v = r_leave(v, arg);
2069 v = r_symreal(arg, *ivp);
2073 v = r_symreal(arg, 0);
2075 v = rb_str_intern(v);
2076 v = r_leave(v, arg);
2080 v = rb_str_intern(r_symlink(arg));
2084 rb_raise(rb_eArgError, "dump format error(0x%x)", type);
2089 rb_raise(rb_eArgError, "dump format error (bad link)");
2096r_object(struct load_arg *arg)
2098 return r_object0(arg, 0, Qnil);
2102clear_load_arg(struct load_arg *arg)
2111 if (!arg->symbols) return;
2112 st_free_table(arg->symbols);
2114 st_free_table(arg->data);
2116 if (arg->compat_tbl) {
2117 st_free_table(arg->compat_tbl);
2118 arg->compat_tbl = 0;
2124 * load( source [, proc] ) -> obj
2125 * restore( source [, proc] ) -> obj
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.
2133 * Never pass untrusted data (including user supplied input) to this method.
2134 * Please see the overview for further details.
2137marshal_load(int argc, VALUE *argv, VALUE _)
2141 rb_check_arity(argc, 1, 2);
2143 proc = argc > 1 ? argv[1] : Qnil;
2144 return rb_marshal_load_with_proc(port, proc);
2148rb_marshal_load_with_proc(VALUE port, VALUE proc)
2152 VALUE wrapper; /* used to avoid memory leak in case of exception */
2153 struct load_arg *arg;
2155 v = rb_check_string_type(port);
2159 else if (rb_respond_to(port, s_getbyte) && rb_respond_to(port, s_read)) {
2160 rb_check_funcall(port, s_binmode, 0, 0);
2165 wrapper = TypedData_Make_Struct(0, struct load_arg, &load_arg_data, arg);
2168 arg->symbols = st_init_numtable();
2169 arg->data = rb_init_identtable();
2170 arg->compat_tbl = 0;
2175 arg->buf = xmalloc(BUFSIZ);
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);
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);
2193 if (!NIL_P(proc)) arg->proc = proc;
2195 clear_load_arg(arg);
2196 RB_GC_GUARD(wrapper);
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.
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.
2216 * str = Marshal.dump("thing")
2217 * RUBY_VERSION #=> "1.9.0"
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.
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.
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.
2235 * == Security considerations
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.
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
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
2250 * == marshal_dump and marshal_load
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.
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.
2263 * def initialize name, version, data
2265 * @version = version
2273 * def marshal_load array
2274 * @name, @version = array
2278 * == _dump and _load
2280 * Use _dump and _load when you need to allocate the object you're restoring
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.
2288 * The class method _load should take a String and use it to return an object
2289 * of the same class.
2294 * def initialize name, version, data
2296 * @version = version
2301 * [@name, @version].join ':'
2304 * def self._load args
2305 * new(*args.split(':'))
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.
2316#define rb_intern(str) rb_intern_const(str)
2318 VALUE rb_mMarshal = rb_define_module("Marshal");
2319#define set_id(sym) sym = rb_intern_const(name_##sym)
2324 set_id(s_dump_data);
2325 set_id(s_load_data);
2332 set_id(s_encoding_short);
2333 set_id(s_ruby2_keywords_flag);
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);
2340 rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
2342 rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
2346compat_allocator_table(void)
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;
2359rb_marshal_dump(VALUE obj, VALUE port)
2361 return rb_marshal_dump_limited(obj, port, -1);
2365rb_marshal_load(VALUE port)
2367 return rb_marshal_load_with_proc(port, Qnil);