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