27 #define BITSPERSHORT (2*CHAR_BIT)
28 #define SHORTMASK ((1<<BITSPERSHORT)-1)
29 #define SHORTDN(x) RSHIFT((x),BITSPERSHORT)
31 #if SIZEOF_SHORT == SIZEOF_BDIGITS
32 #define SHORTLEN(x) (x)
45 return (len - 1)*
sizeof(
BDIGIT)/2 + offset;
47 #define SHORTLEN(x) shortlen((x),d)
50 #define MARSHAL_MAJOR 4
51 #define MARSHAL_MINOR 8
55 #define TYPE_FALSE 'F'
56 #define TYPE_FIXNUM 'i'
58 #define TYPE_EXTENDED 'e'
59 #define TYPE_UCLASS 'C'
60 #define TYPE_OBJECT 'o'
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 '['
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'
76 #define TYPE_SYMBOL ':'
77 #define TYPE_SYMLINK ';'
133 #define MARSHAL_INFECTION (FL_TAINT|FL_UNTRUSTED)
183 return ptr ?
sizeof(
struct dump_arg) : 0;
248 #define w_cstr(s, arg) w_bytes((s), strlen(s), (arg))
253 w_byte((
char)((x >> 0) & 0xff), arg);
254 w_byte((
char)((x >> 8) & 0xff), arg);
264 if (!(RSHIFT(x, 31) == 0 || RSHIFT(x, 31) == -1)) {
274 if (0 < x && x < 123) {
275 w_byte((
char)(x + 5), arg);
278 if (-124 < x && x < 0) {
279 w_byte((
char)((x - 5)&0xff), arg);
282 for (i=1;i<(int)
sizeof(
long)+1;i++) {
283 buf[
i] = (char)(x & 0xff);
295 for (i=0;i<=
len;i++) {
301 #define DECIMAL_MANT (53-16)
303 #if DBL_MANT_DIG > 32
305 #elif DBL_MANT_DIG > 24
307 #elif DBL_MANT_DIG > 16
317 if (--len > 0 && !*buf++) {
318 int e, s = d < 0, dig = 0;
321 modf(ldexp(frexp(fabs(d), &e), DECIMAL_MANT), &d);
325 default: m = *buf++ & 0xff;
327 case 3: m = (m << 8) | (*buf++ & 0xff);
330 case 2: m = (m << 8) | (*buf++ & 0xff);
333 case 1: m = (m << 8) | (*buf++ & 0xff);
336 dig -= len < MANT_BITS / 8 ? 8 * (unsigned)len : MANT_BITS;
337 d += ldexp((
double)m, dig);
338 }
while ((len -= MANT_BITS / 8) > 0);
339 d = ldexp(d, e - DECIMAL_MANT);
345 #define load_mantissa(d, buf, len) (d)
349 #define FLOAT_DIG (DBL_DIG+2)
357 char *
ruby_dtoa(
double d_,
int mode,
int ndigits,
int *decpt,
int *sign,
char **rve);
358 char buf[
FLOAT_DIG + (DECIMAL_MANT + 7) / 8 + 10];
361 if (d < 0)
w_cstr(
"-inf", arg);
368 if (1.0/d < 0)
w_cstr(
"-0", arg);
372 int decpt, sign, digs, len = 0;
373 char *e, *
p =
ruby_dtoa(d, 0, 0, &decpt, &sign, &e);
374 if (sign) buf[len++] =
'-';
376 if (decpt < -3 || decpt > digs) {
378 if (--digs > 0) buf[len++] =
'.';
379 memcpy(buf + len, p + 1, digs);
381 len +=
snprintf(buf + len,
sizeof(buf) - len,
"e%d", decpt - 1);
383 else if (decpt > 0) {
384 memcpy(buf + len, p, decpt);
386 if ((digs -= decpt) > 0) {
388 memcpy(buf + len, p + decpt, digs);
396 memset(buf + len,
'0', -decpt);
399 memcpy(buf + len, p, digs);
486 obj = (
VALUE)real_obj;
502 if (klass != super) {
575 for (i = 0; i <
len; i++)
592 #define has_ivars(obj, ivtbl) (((ivtbl) = rb_generic_ivar_table(obj)) != 0 || \
593 (!SPECIAL_CONST_P(obj) && !ENCODING_IS_ASCII8BIT(obj)))
612 else if (obj ==
Qtrue) {
623 if (RSHIFT((
long)obj, 31) == 0 || RSHIFT((
long)obj, 31) == -1) {
649 if (hasiv)
w_ivar(obj, ivtbl, &c_arg);
664 if ((hasiv2 =
has_ivars(v, ivtbl2)) != 0 && !hasiv) {
670 w_ivar(v, ivtbl2, &c_arg);
673 w_ivar(obj, ivtbl, &c_arg);
690 obj = compat->
dumper(real_obj);
692 if (obj != real_obj && !ivtbl) hasiv = 0;
732 #if SIZEOF_BDIGITS > SIZEOF_SHORT
739 if (len == 0 && num == 0)
break;
809 for (i=0; i<
len; i++) {
827 "no _dump_data is defined for class %s",
844 w_ivar(obj, ivtbl, &c_arg);
904 volatile VALUE wrapper;
910 if (
NIL_P(a1))
goto type_error;
913 else if (argc == 2) {
915 else if (
NIL_P(a1))
goto type_error;
996 return ptr ?
sizeof(
struct load_arg) : 0;
1004 #define r_entry(v, arg) r_entry0((v), (arg)->data->num_entries, (arg))
1037 c = (
unsigned char)
NUM2CHR(v);
1046 STRINGIZE(SIZEOF_LONG)
", given %d)", size);
1049 #undef SIGN_EXTEND_CHAR
1051 # define SIGN_EXTEND_CHAR(c) ((signed char)(c))
1054 # define SIGN_EXTEND_CHAR(c) ((((unsigned char)(c)) ^ 128) - 128)
1064 if (c == 0)
return 0;
1066 if (4 < c && c < 128) {
1076 if (-129 < c && c < -4) {
1083 x &= ~((
long)0xff << (8*i));
1090 #define r_bytes(arg) r_bytes0(r_long(arg), (arg))
1113 if (
NIL_P(str))
goto too_short;
1178 switch ((type =
r_byte(arg))) {
1236 compat->
loader(real_obj, v);
1261 if (has_encoding) *has_encoding =
TRUE;
1266 }
while (--len > 0);
1375 if (
TYPE(v) !=
TYPE(tmp))
goto format_error;
1410 if (strcmp(ptr,
"nan") == 0) {
1413 else if (strcmp(ptr,
"inf") == 0) {
1416 else if (strcmp(ptr,
"-inf") == 0) {
1434 volatile VALUE data;
1441 #if SIZEOF_BDIGITS == SIZEOF_SHORT
1448 #if SIZEOF_BDIGITS > SIZEOF_SHORT
1449 MEMZERO((
char *)digits + len * 2,
char,
1454 unsigned char *
p = (
unsigned char *)digits;
1456 #if SIZEOF_BDIGITS > SIZEOF_SHORT
1461 num |= (int)p[i] << shift;
1465 num = p[0] | (p[1] << 8);
1485 int has_encoding =
FALSE;
1489 r_ivar(str, &has_encoding, arg);
1492 if (!has_encoding) {
1494 char *ptr =
RSTRING_PTR(str), *dst = ptr, *src = ptr;
1497 for (; len-- > 0; *dst++ = *src++) {
1499 case '\\': bs++;
break;
1500 case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
1501 case 'm':
case 'o':
case 'p':
case 'q':
case 'u':
case 'y':
1502 case 'E':
case 'F':
case 'H':
case 'I':
case 'J':
case 'K':
1503 case 'L':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
1504 case 'S':
case 'T':
case 'U':
case 'V':
case 'X':
case 'Y':
1506 default: bs = 0;
break;
1518 volatile long len =
r_long(arg);
1569 for (i=0; i<
len; i++) {
1612 if (!
NIL_P(extmod)) {
1647 static int warn =
TRUE;
1649 rb_warn(
"define `allocate' instead of `_alloc'");
1664 "class %s needs to have instance method `_load_data'",
1760 volatile VALUE wrapper;
1766 infection = (int)
FL_TEST(port, MARSHAL_INFECTION);
1792 \tformat version %d.%d required; %d.%d given",
1796 rb_warn(
"incompatible marshal file format (can be read)\n\
1797 \tformat version %d.%d required; %d.%d given",
1909 #define rb_intern(str) rb_intern_const(str)
1947 if (!
NIL_P(port)) argc = 2;