12 #if defined __GNUC__ && __GNUC__ < 3 31 #define BITSPERSHORT (2*CHAR_BIT) 32 #define SHORTMASK ((1<<BITSPERSHORT)-1) 33 #define SHORTDN(x) RSHIFT((x),BITSPERSHORT) 35 #if SIZEOF_SHORT == SIZEOF_BDIGIT 36 #define SHORTLEN(x) (x) 51 #define SHORTLEN(x) shortlen((x),d) 54 #define MARSHAL_MAJOR 4 55 #define MARSHAL_MINOR 8 59 #define TYPE_FALSE 'F' 60 #define TYPE_FIXNUM 'i' 62 #define TYPE_EXTENDED 'e' 63 #define TYPE_UCLASS 'C' 64 #define TYPE_OBJECT 'o' 66 #define TYPE_USERDEF 'u' 67 #define TYPE_USRMARSHAL 'U' 68 #define TYPE_FLOAT 'f' 69 #define TYPE_BIGNUM 'l' 70 #define TYPE_STRING '"' 71 #define TYPE_REGEXP '/' 72 #define TYPE_ARRAY '[' 74 #define TYPE_HASH_DEF '}' 75 #define TYPE_STRUCT 'S' 76 #define TYPE_MODULE_OLD 'M' 77 #define TYPE_CLASS 'c' 78 #define TYPE_MODULE 'm' 80 #define TYPE_SYMBOL ':' 81 #define TYPE_SYMLINK ';' 90 #define name_s_dump "_dump" 91 #define name_s_load "_load" 92 #define name_s_mdump "marshal_dump" 93 #define name_s_mload "marshal_load" 94 #define name_s_dump_data "_dump_data" 95 #define name_s_load_data "_load_data" 96 #define name_s_alloc "_alloc" 97 #define name_s_call "call" 98 #define name_s_getbyte "getbyte" 99 #define name_s_read "read" 100 #define name_s_write "write" 101 #define name_s_binmode "binmode" 154 #define MARSHAL_INFECTION FL_TAINT 195 #define dump_funcall(arg, obj, sym, argc, argv) \ 196 check_userdump_arg(obj, sym, argc, argv, arg, name_##sym) 197 #define dump_check_funcall(arg, obj, sym, argc, argv) \ 198 check_dump_arg(rb_check_funcall(obj, sym, argc, argv), arg, name_##sym) 291 #define w_cstr(s, arg) w_bytes((s), strlen(s), (arg)) 296 w_byte((
char)((x >> 0) & 0xff), arg);
297 w_byte((
char)((x >> 8) & 0xff), arg);
303 char buf[
sizeof(long)+1];
307 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
317 if (0 < x && x < 123) {
318 w_byte((
char)(x + 5), arg);
321 if (-124 < x && x < 0) {
322 w_byte((
char)((x - 5)&0xff), arg);
325 for (i=1;i<(int)
sizeof(
long)+1;i++) {
326 buf[i] = (char)(x & 0xff);
341 #define DECIMAL_MANT (53-16) 343 #if DBL_MANT_DIG > 32 345 #elif DBL_MANT_DIG > 24 347 #elif DBL_MANT_DIG > 16 357 if (--len > 0 && !*buf++) {
358 int e, s = d < 0, dig = 0;
361 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
365 default: m = *buf++ & 0xff;
367 case 3: m = (m << 8) | (*buf++ & 0xff);
370 case 2: m = (m << 8) | (*buf++ & 0xff);
373 case 1: m = (m << 8) | (*buf++ & 0xff);
376 dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
377 d += ldexp((
double)m, dig);
378 }
while ((len -= MANT_BITS / 8) > 0);
379 d = ldexp(d, e - DECIMAL_MANT);
385 #define load_mantissa(d, buf, len) (d) 389 #define FLOAT_DIG (DBL_DIG+2) 397 char buf[
FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
400 if (d < 0)
w_cstr(
"-inf", arg);
407 if (1.0/d < 0)
w_cstr(
"-0", arg);
411 int decpt, sign, digs, len = 0;
412 char *e, *p =
ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
413 if (sign) buf[len++] =
'-';
415 if (decpt < -3 || decpt > digs) {
417 if (--digs > 0) buf[len++] =
'.';
418 memcpy(buf + len, p + 1, digs);
420 len +=
snprintf(buf + len,
sizeof(buf) - len,
"e%d", decpt - 1);
422 else if (decpt > 0) {
423 memcpy(buf + len, p, decpt);
425 if ((digs -= decpt) > 0) {
427 memcpy(buf + len, p + decpt, digs);
435 memset(buf + len,
'0', -decpt);
438 memcpy(buf + len, p, digs);
463 if (
NIL_P(encname) ||
473 if (!
NIL_P(encname)) {
500 #define SINGLETON_DUMP_UNABLE_P(klass) \ 501 (rb_id_table_size(RCLASS_M_TBL(klass)) > 0 || \ 502 (RCLASS_IV_TBL(klass) && RCLASS_IV_TBL(klass)->num_entries > 1)) 532 obj = (
VALUE)real_obj;
548 if (klass != super) {
554 #define to_be_skipped_id(id) (id == rb_id_encoding() || id == rb_intern("E") || !rb_id2str(id)) 609 if (limit >= 0) ++
limit;
637 if (num) *ivobj =
obj;
678 if (limit > 0) limit--;
691 else if (obj ==
Qtrue) {
702 if (RSHIFT((
long)obj, 31) == 0 || RSHIFT((
long)obj, 31) == -1) {
758 w_ivar(hasiv, ivobj, encname, &c_arg);
775 obj = compat->
dumper(real_obj);
780 if (obj != real_obj && ivobj ==
Qundef) hasiv = 0;
828 #if SIZEOF_BDIGIT > SIZEOF_SHORT 835 if (len == 0 && num == 0)
break;
904 for (i=0; i<
len; i++) {
922 "no _dump_data is defined for class %"PRIsVALUE,
939 w_ivar(hasiv, ivobj, encname, &c_arg);
1015 else if (argc == 2) {
1084 #define load_funcall(arg, obj, sym, argc, argv) \ 1085 check_load_arg(rb_funcallv(obj, sym, argc, argv), arg, name_##sym) 1116 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1119 #define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg)) 1141 static unsigned char 1180 c = (
unsigned char)
NUM2CHR(v);
1190 STRINGIZE(SIZEOF_LONG)
", given %d)", size);
1197 int c = (
signed char)
r_byte(arg);
1200 if (c == 0)
return 0;
1202 if (4 < c && c < 128) {
1208 x |= (long)
r_byte(arg) << (8*i);
1212 if (-129 < c && c < -4) {
1219 x &= ~((long)0xff << (8*i));
1220 x |= (long)
r_byte(arg) << (8*i);
1245 if (len <= arg->buflen) {
1251 long buflen = arg->
buflen;
1253 long tmp_len, read_len, need_len = len - buflen;
1256 readable = readable < BUFSIZ ?
readable : BUFSIZ;
1257 read_len = need_len > readable ? need_len :
readable;
1271 if (tmp_len > need_len) {
1272 buflen = tmp_len - need_len;
1285 #define r_bytes(arg) r_bytes0(r_long(arg), (arg)) 1316 static const char name_encoding[8] =
"encoding";
1321 if (l <= 0)
return -1;
1322 if (l ==
sizeof(name_encoding) &&
1323 memcmp(p, name_encoding,
sizeof(name_encoding)) == 0) {
1327 else if (l == 1 && *p ==
'E') {
1375 switch ((type =
r_byte(arg))) {
1432 compat->
loader(real_obj, v);
1487 if (has_encoding) *has_encoding =
TRUE;
1492 }
while (--len > 0);
1507 #define path2module(path) must_be_module(rb_path_to_class(path), path) 1529 if (oldclass) *oldclass = compat->
oldclass;
1558 #define prohibit_ivar(type, str) do { \ 1559 if (!ivp || !*ivp) break; \ 1560 rb_raise(rb_eTypeError, \ 1561 "can't override instance variable of "type" `%"PRIsVALUE"'", \ 1642 if (
TYPE(v) !=
TYPE(tmp))
goto format_error;
1677 if (strcmp(ptr,
"nan") == 0) {
1680 else if (strcmp(ptr,
"inf") == 0) {
1683 else if (strcmp(ptr,
"-inf") == 0) {
1723 int has_encoding =
FALSE;
1727 r_ivar(str, &has_encoding, arg);
1730 if (!has_encoding) {
1735 for (; len-- > 0; *dst++ = *src++) {
1737 case '\\': bs++;
break;
1738 case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
1739 case 'm':
case 'o':
case 'p':
case 'q':
case 'u':
case 'y':
1740 case 'E':
case 'F':
case 'H':
case 'I':
case 'J':
case 'K':
1741 case 'L':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
1742 case 'S':
case 'T':
case 'U':
case 'V':
case 'X':
case 'Y':
1744 default: bs = 0;
break;
1814 for (i=0; i<
len; i++) {
1852 v = compat->
loader(klass, v);
1866 if (!
NIL_P(extmod)) {
1880 if (!
NIL_P(extmod)) {
1914 "class %"PRIsVALUE" needs to have instance method `_load_data'",
2032 proc = argc > 1 ? argv[1] :
Qnil;
2039 int major, minor, infection = 0;
2076 \tformat version %d.%d required; %d.%d given",
2080 rb_warn(
"incompatible marshal file format (can be read)\n\ 2081 \tformat version %d.%d required; %d.%d given",
2208 #define rb_intern(str) rb_intern_const(str) 2211 #define set_id(sym) sym = rb_intern_const(name_##sym) 2240 #undef RUBY_UNTYPED_DATA_WARNING 2241 #define RUBY_UNTYPED_DATA_WARNING 0 RUBY_EXTERN VALUE rb_cString
static VALUE marshal_load(int argc, VALUE *argv)
#define ENCINDEX_US_ASCII
static const rb_data_type_t dump_arg_data
static void long_toobig(int size)
int rb_enc_get_index(VALUE obj)
RUBY_EXTERN VALUE rb_cData
static long r_long(struct load_arg *arg)
#define path2module(path)
VALUE rb_ary_pop(VALUE ary)
static VALUE class2path(VALUE klass)
#define RUBY_TYPED_FREE_IMMEDIATELY
#define load_mantissa(d, buf, len)
VALUE rb_str_equal(VALUE str1, VALUE str2)
static unsigned char r_byte1_buffered(struct load_arg *arg)
static size_t memsize_dump_arg(const void *ptr)
static int w_obj_each(st_data_t key, st_data_t val, st_data_t a)
static void w_objivar(VALUE obj, struct dump_call_arg *arg)
VALUE rb_str_cat(VALUE, const char *, long)
VALUE rb_marshal_dump(VALUE obj, VALUE port)
static VALUE r_copy_ivar(VALUE v, VALUE data)
static void w_long(long, struct dump_arg *)
static void w_object(VALUE, struct dump_arg *, int)
const char * rb_builtin_type_name(int t)
static VALUE marshal_dump(int argc, VALUE *argv)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE rb_marshal_dump_limited(VALUE obj, VALUE port, int limit)
static VALUE r_symbol(struct load_arg *arg)
int rb_usascii_encindex(void)
VALUE rb_ary_tmp_new(long capa)
static int r_byte(struct load_arg *arg)
VALUE rb_path_to_class(VALUE)
void rb_str_set_len(VALUE, long)
#define RBASIC_SET_CLASS(obj, cls)
int rb_reg_options(VALUE)
int rb_enc_str_coderange(VALUE)
static void w_short(int x, struct dump_arg *arg)
void rb_raise(VALUE exc, const char *fmt,...)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
VALUE rb_ary_clear(VALUE ary)
static void w_encoding(VALUE encname, struct dump_call_arg *arg)
static VALUE r_symlink(struct load_arg *arg)
static VALUE check_userdump_arg(VALUE obj, ID sym, int argc, const VALUE *argv, struct dump_arg *arg, const char *name)
static st_index_t has_ivars(VALUE obj, VALUE encname, VALUE *ivobj)
void rb_gc_mark(VALUE ptr)
static void w_uclass(VALUE obj, VALUE super, struct dump_arg *arg)
VALUE rb_io_write(VALUE, VALUE)
static int copy_ivar_i(st_data_t key, st_data_t val, st_data_t arg)
STATIC_ASSERT(marshal_infection_is_int, MARSHAL_INFECTION==(int) MARSHAL_INFECTION)
static int sym2encidx(VALUE sym, VALUE val)
static VALUE path2class(VALUE path)
static VALUE r_bytes1_buffered(long len, struct load_arg *arg)
static void w_class(char type, VALUE obj, struct dump_arg *arg, int check)
VALUE rb_ivar_defined(VALUE, ID)
static int hash_each(VALUE key, VALUE value, struct dump_call_arg *arg)
static VALUE obj_alloc_by_path(VALUE path, struct load_arg *arg)
void rb_ivar_foreach(VALUE, int(*)(ANYARGS), st_data_t)
#define ENC_CODERANGE_7BIT
static void w_nbyte(const char *s, long n, struct dump_arg *arg)
#define RHASH_SET_IFNONE(h, ifnone)
VALUE rb_str_buf_cat(VALUE, const char *, long)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
#define Data_Wrap_Struct(klass, mark, free, sval)
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
static VALUE r_string(struct load_arg *arg)
VALUE rb_struct_members(VALUE)
void rb_prepend_module(VALUE klass, VALUE module)
st_table * st_init_strcasetable(void)
VALUE rb_singleton_class(VALUE obj)
Returns the singleton class of obj.
#define RB_TYPE_P(obj, type)
VALUE rb_struct_s_members(VALUE)
#define prohibit_ivar(type, str)
#define rb_intern_str(string)
VALUE rb_class_name(VALUE)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
#define RREGEXP_SRC_PTR(r)
#define SINGLETON_DUMP_UNABLE_P(klass)
static VALUE r_fixup_compat(VALUE v, struct load_arg *arg)
static VALUE check_dump_arg(VALUE ret, struct dump_arg *arg, const char *name)
void rb_define_const(VALUE, const char *, VALUE)
static VALUE r_entry0(VALUE v, st_index_t num, struct load_arg *arg)
static VALUE check_load_arg(VALUE ret, struct load_arg *arg, const char *name)
static VALUE r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
static st_index_t r_prepare(struct load_arg *arg)
static VALUE r_post_proc(VALUE v, struct load_arg *arg)
void rb_gc_register_mark_object(VALUE obj)
rb_alloc_func_t rb_get_alloc_func(VALUE)
RUBY_EXTERN int isinf(double)
VALUE rb_enc_associate_index(VALUE obj, int idx)
VALUE rb_obj_alloc(VALUE)
int link(const char *, const char *)
static VALUE r_symreal(struct load_arg *arg, int ivar)
char * ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
#define HASH_PROC_DEFAULT
static size_t memsize_load_arg(const void *ptr)
static void mark_dump_arg(void *ptr)
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
#define to_be_skipped_id(id)
VALUE rb_str_resize(VALUE, long)
static void too_short(void)
static void w_unique(VALUE s, struct dump_arg *arg)
static VALUE must_be_module(VALUE v, VALUE path)
RUBY_EXTERN VALUE rb_cRegexp
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
st_table * rb_init_identtable(void)
int rb_obj_respond_to(VALUE, ID, int)
static VALUE r_bytes1(long len, struct load_arg *arg)
static void w_symbol(VALUE sym, struct dump_arg *arg)
VALUE rb_class_path(VALUE)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static st_table * compat_allocator_tbl
VALUE rb_ivar_set(VALUE, ID, VALUE)
unsigned char buf[MIME_BUF_SIZE]
static void r_ivar(VALUE obj, int *has_encoding, struct load_arg *arg)
static int options(unsigned char *cp)
static void clear_dump_arg(struct dump_arg *arg)
void rb_extend_object(VALUE obj, VALUE module)
int rb_utf8_encindex(void)
void rb_mark_tbl(st_table *tbl)
static void free_load_arg(void *ptr)
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
#define rb_enc_asciicompat(enc)
VALUE rb_str_new_cstr(const char *)
int memcmp(const void *s1, const void *s2, size_t len)
#define INTEGER_PACK_LITTLE_ENDIAN
static int mark_marshal_compat_i(st_data_t key, st_data_t value)
static void mark_load_arg(void *ptr)
VALUE rb_class_real(VALUE cl)
static VALUE r_bytes0(long len, struct load_arg *arg)
int rb_respond_to(VALUE, ID)
#define MARSHAL_INFECTION
register unsigned int len
static VALUE encoding_name(VALUE obj, struct dump_arg *arg)
#define load_funcall(arg, obj, sym, argc, argv)
#define StringValueCStr(v)
NORETURN(static inline void io_needed(void))
static void w_bytes(const char *s, long n, struct dump_arg *arg)
#define INTEGER_PACK_NEGATIVE
rb_encoding * rb_enc_get(VALUE obj)
static void clear_load_arg(struct load_arg *arg)
#define RSTRUCT_GET(st, idx)
static int obj_count_ivars(st_data_t key, st_data_t val, st_data_t a)
#define RARRAY_AREF(a, i)
#define RBASIC_CLASS(obj)
void rb_mark_set(st_table *tbl)
VALUE rb_check_string_type(VALUE)
static int rb_special_const_p(VALUE obj)
#define dump_check_funcall(arg, obj, sym, argc, argv)
#define dump_funcall(arg, obj, sym, argc, argv)
static VALUE compat_allocator_tbl_wrapper
static VALUE must_not_be_anonymous(const char *type, VALUE path)
static VALUE append_extmod(VALUE obj, VALUE extmod)
static VALUE r_unique(struct load_arg *arg)
#define TypedData_Make_Struct(klass, type, data_type, sval)
static void free_dump_arg(void *ptr)
VALUE rb_int2big(SIGNED_VALUE n)
static VALUE r_object(struct load_arg *arg)
static void mark_marshal_compat_t(void *tbl)
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
#define RREGEXP_SRC_LEN(r)
static struct StringIO * readable(VALUE strio)
static VALUE obj_alloc_by_klass(VALUE klass, struct load_arg *arg, VALUE *oldclass)
static void io_needed(void)
VALUE rb_reg_new_str(VALUE, int)
static st_table * compat_allocator_table(void)
static const rb_data_type_t load_arg_data
int rb_enc_find_index(const char *name)
VALUE rb_str_intern(VALUE)
static void w_ivar(st_index_t num, VALUE ivobj, VALUE encname, struct dump_call_arg *arg)
#define SPECIAL_CONST_P(x)
VALUE rb_define_module(const char *name)
void rb_mark_hash(st_table *tbl)
int rb_enc_str_asciionly_p(VALUE)
VALUE rb_str_buf_new(long)
VALUE(* rb_alloc_func_t)(VALUE)
static void w_extended(VALUE klass, struct dump_arg *arg, int check)
static void w_byte(char c, struct dump_arg *arg)
static void w_float(double d, struct dump_arg *arg)
VALUE rb_marshal_load(VALUE port)
VALUE rb_struct_initialize(VALUE, VALUE)
VALUE(* loader)(VALUE, VALUE)
VALUE rb_class_inherited_p(VALUE mod, VALUE arg)
void rb_warn(const char *fmt,...)
static VALUE rb_marshal_load_with_proc(VALUE port, VALUE proc)
static VALUE r_leave(VALUE v, struct load_arg *arg)
rb_encoding * rb_enc_from_index(int index)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)