12#include "ruby/internal/config.h"
20#include "internal/array.h"
21#include "internal/bits.h"
22#include "internal/numeric.h"
23#include "internal/string.h"
24#include "internal/symbol.h"
25#include "internal/variable.h"
38#ifdef HAVE_TRUE_LONG_LONG
39static const char natstr[] =
"sSiIlLqQjJ";
42static const char natstr[] =
"sSiIlLjJ";
43static const char endstr[] =
"sSiIlLqQjJ";
46#ifdef HAVE_TRUE_LONG_LONG
48# define NATINT_LEN_Q NATINT_LEN(long long, 8)
50# define NATINT_LEN_Q 8
53#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
63 static const union {
int i;
char b[1];} endian_value = {1};
64 return !endian_value.b[0];
66# define BIGENDIAN_P() (is_bigendian())
67#elif defined(WORDS_BIGENDIAN)
68# define BIGENDIAN_P() 1
70# define BIGENDIAN_P() 0
74# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
75# define NATINT_ALIGN(type,len) (natint?(int)RUBY_ALIGNOF(type):(int)(len))
76# define USING_NATINT(expr) expr
78# define NATINT_LEN(type,len) ((int)sizeof(type))
79# define NATINT_ALIGN(type,len) ((int)RUBY_ALIGNOF(type))
80# define USING_NATINT(expr)
93#define swapf(x) swap32(x)
94#define swapd(x) swap64(x)
96#define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
97#define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
98#define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
99#define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
100#define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
101#define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
102#define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
103#define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
105#define FLOAT_CONVWITH(x) FLOAT_SWAPPER x;
106#define HTONF(x) ((x).u = rb_htonf((x).u))
107#define HTOVF(x) ((x).u = rb_htovf((x).u))
108#define NTOHF(x) ((x).u = rb_ntohf((x).u))
109#define VTOHF(x) ((x).u = rb_vtohf((x).u))
111#define DOUBLE_CONVWITH(x) DOUBLE_SWAPPER x;
112#define HTOND(x) ((x).u = rb_htond((x).u))
113#define HTOVD(x) ((x).u = rb_htovd((x).u))
114#define NTOHD(x) ((x).u = rb_ntohd((x).u))
115#define VTOHD(x) ((x).u = rb_vtohd((x).u))
117#define MAX_INTEGER_PACK_SIZE 8
119static const char toofew[] =
"too few arguments";
120static const char intoitself[] =
"cannot pack buffer object into itself";
122static void encodes(
VALUE,
const char*,
long,
int,
int);
125static unsigned long utf8_to_uv(
const char*,
long*);
127static ID id_associated;
137str_associated(
VALUE str)
139 VALUE associates = rb_ivar_lookup(str, id_associated,
Qfalse);
141 rb_raise(rb_eArgError,
"no associated pointer");
146associated_pointer(
VALUE associates,
const char *t)
150 for (; p < pend; p++) {
154 rb_raise(rb_eArgError,
"non associated pointer");
160unknown_directive(const
char *mode,
char type,
VALUE fmt)
169 snprintf(unknown,
sizeof(unknown),
"\\x%.2x",
type & 0xff);
171 fmt = rb_str_quote_unprintable(fmt);
172 rb_raise(rb_eArgError,
"unknown %s directive '%s' in '%"PRIsVALUE
"'",
177VALUE_to_float(
VALUE obj)
185 else if (d < -FLT_MAX) {
188 else if (d <= FLT_MAX) {
197str_expand_fill(
VALUE res,
int c,
long len)
199 long olen = RSTRING_LEN(res);
200 memset(RSTRING_PTR(res) + olen, c,
len);
205skip_to_eol(
const char *p,
const char *pend)
207 p = memchr(p,
'\n', pend - p);
208 return (
char *)(p ? p + 1 : pend);
211#define skip_blank(p, type) \
212 (ISSPACE(type) || (type == '#' && (p = skip_to_eol(p, pend), 1)))
215# define pack_modifiers(p, pe, t, n, e) pack_modifiers(p, pe, t, e)
218pack_modifiers(
const char *p,
const char *pend,
char type,
int *natint,
int *explicit_endian)
224 if (strchr(natstr,
type)) {
225 USING_NATINT(*natint = 1);
229 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, natstr);
235 if (!strchr(endstr,
type)) {
236 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, endstr);
238 if (*explicit_endian) {
241 *explicit_endian = *p++;
251# define pack_alignof(t, n) pack_alignof(t)
254pack_alignof(
char type,
int natint)
260 return NATINT_ALIGN(
short, 2);
264 return NATINT_ALIGN(
long, 4);
275 case 'f':
case 'F':
case 'e':
case 'g':
277 case 'd':
case 'D':
case 'E':
case 'G':
287pack_align_pad(
long pos,
long base,
size_t alignment)
291 if (alignment <= 1)
return 0;
292 if (alignment > LONG_MAX) rb_raise(
rb_eRangeError,
"alignment too big");
294 mod = offset % (long)alignment;
295 if (mod < 0) mod += alignment;
296 return mod ? (long)alignment - mod : 0;
300pack_alignment(
const char *p,
const char *pend,
VALUE fmt,
size_t *alignment)
303 int explicit_endian = 0;
304 USING_NATINT(
int natint = 0);
307 rb_raise(rb_eArgError,
"missing alignment");
310 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
311 if (explicit_endian) {
312 rb_raise(rb_eArgError,
"endian modifier is not allowed for alignment");
314 *alignment = pack_alignof(
type, natint);
316 unknown_directive(
"alignment",
type, fmt);
322pack_alignment_size(
const char *p,
const char *pend,
VALUE fmt,
size_t *alignment)
326 *alignment =
STRTOUL(p, (
char**)&p, 10);
327 if (*alignment <= 0 ||
errno) {
332 return pack_alignment(p, pend, fmt, alignment);
338 const char *p, *pend;
339 VALUE res, from, associates = 0;
343 int integer_size, bigendian_p;
348 p = RSTRING_PTR(fmt);
349 pend = p + RSTRING_LEN(fmt);
357 rb_str_modify(buffer);
362 align_base = RSTRING_LEN(res);
364#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
365#define MORE_ITEM (idx < RARRAY_LEN(ary))
366#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
367#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
368#define NOT_BUFFER(val) (((val) == res) ? rb_raise(rb_eArgError, intoitself) : (void)0)
369#define STR_FROM(val) NOT_BUFFER(StringValue(val))
372 int explicit_endian = 0;
377 const char type = *p++;
378 USING_NATINT(
int natint = 0);
380 if (skip_blank(p,
type))
continue;
383 if ((
type ==
'x' ||
type ==
'@') && p < pend && *p ==
'!') {
385 p = pack_alignment_size(p, pend, fmt, &align);
386 len = pack_align_pad(RSTRING_LEN(res),
type ==
'@' ? 0 : align_base, align);
388 str_expand_fill(res,
'\0',
len);
392 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
396 : strchr(
"PMm",
type) ? 1
414 if (enc_info == 1) enc_info = 2;
416 case 'm':
case 'M':
case 'u':
425 case 'A':
case 'a':
case 'Z':
435 ptr = RSTRING_PTR(from);
436 plen = RSTRING_LEN(from);
448 if (p[-1] ==
'*' &&
type ==
'Z')
454 str_expand_fill(res, (
type ==
'A' ?
' ' :
'\0'),
len - plen);
458#define castchar(from) (char)((from) & 0xff)
466 j = (
len - plen + 1)/2;
469 for (i=0; i++ <
len; ptr++) {
475 char c = castchar(
byte);
482 byte >>= 7 - (
len & 7);
497 j = (
len - plen + 1)/2;
500 for (i=0; i++ <
len; ptr++) {
505 char c = castchar(
byte);
512 byte <<= 7 - (
len & 7);
527 j = (
len + 1) / 2 - (plen + 1) / 2;
530 for (i=0; i++ <
len; ptr++) {
532 byte |= (((*ptr & 15) + 9) & 15) << 4;
534 byte |= (*ptr & 15) << 4;
538 char c = castchar(
byte);
544 char c = castchar(
byte);
558 j = (
len + 1) / 2 - (plen + 1) / 2;
561 for (i=0; i++ <
len; ptr++) {
563 byte |= ((*ptr & 15) + 9) & 15;
569 char c = castchar(
byte);
575 char c = castchar(
byte);
588 bigendian_p = BIGENDIAN_P();
593 integer_size = NATINT_LEN(
short, 2);
594 bigendian_p = BIGENDIAN_P();
599 integer_size = (int)
sizeof(
int);
600 bigendian_p = BIGENDIAN_P();
605 integer_size = NATINT_LEN(
long, 4);
606 bigendian_p = BIGENDIAN_P();
611 integer_size = NATINT_LEN_Q;
612 bigendian_p = BIGENDIAN_P();
616 integer_size =
sizeof(intptr_t);
617 bigendian_p = BIGENDIAN_P();
621 integer_size =
sizeof(uintptr_t);
622 bigendian_p = BIGENDIAN_P();
646 if (explicit_endian) {
647 bigendian_p = explicit_endian ==
'>';
649 if (integer_size > MAX_INTEGER_PACK_SIZE)
650 rb_bug(
"unexpected integer size for pack: %d", integer_size);
652 char intbuf[MAX_INTEGER_PACK_SIZE];
655 rb_integer_pack(from, intbuf, integer_size, 1, 0,
668 f = VALUE_to_float(from);
678 tmp.f = VALUE_to_float(from);
686 DOUBLE_CONVWITH(tmp);
709 tmp.f = VALUE_to_float(from);
717 DOUBLE_CONVWITH(tmp);
729 str_expand_fill(res,
'\0',
len);
734 plen = RSTRING_LEN(res);
736 rb_raise(rb_eArgError,
"X outside of string");
741 len -= RSTRING_LEN(res);
742 if (
len > 0)
goto grow;
744 if (
len > 0)
goto shrink;
748 rb_raise(rb_eArgError,
"%% is not supported");
778 size_t numbytes, nlz_bits;
784 if (
type ==
'R' && rb_int_negative_p(from)) {
785 rb_raise(rb_eArgError,
"can't encode negative numbers in ULEB128");
788 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
792 else if (nlz_bits == 0 &&
type ==
'r') {
798 long start = RSTRING_LEN(res);
800 cp = RSTRING_PTR(res) + start;
801 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
805 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
809 while (1 < numbytes) {
821 ptr = RSTRING_PTR(from);
822 plen = RSTRING_LEN(from);
825 encodes(res, ptr, plen,
type, 0);
831 else if (
len > 63 &&
type ==
'u')
842 encodes(res, ptr, todo,
type, 1);
853 qpencode(res, from,
len);
860 if (RSTRING_LEN(from) <
len) {
861 rb_raise(rb_eArgError,
"too short buffer for P(%ld for %ld)",
862 RSTRING_LEN(from),
len);
873 t = RSTRING_PTR(from);
892 numbytes = rb_absint_numwords(from, 7, NULL);
900 rb_raise(rb_eArgError,
"can't compress negative numbers");
902 rb_bug(
"buffer size problem?");
904 cp = RSTRING_PTR(buf);
905 while (1 < numbytes) {
916 unknown_directive(
"pack",
type, fmt);
923 str_associate(res, associates);
930 rb_enc_set_index(res, rb_utf8_encindex());
942 return pack_pack(ec, ary, fmt, buffer);
945static const char uu_table[] =
946"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
947static const char b64_table[] =
948"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
951encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
953 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
954 char buff[buff_size + 1];
956 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
958 const unsigned char *s = (
const unsigned char *)s0;
961 buff[i++] = (char)
len +
' ';
967 while (
len >= input_unit) {
968 while (
len >= input_unit && buff_size-i >= encoded_unit) {
969 buff[i++] = trans[077 & (*s >> 2)];
970 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
971 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
972 buff[i++] = trans[077 & s[2]];
976 if (buff_size-i < encoded_unit) {
983 buff[i++] = trans[077 & (*s >> 2)];
984 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
985 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
989 buff[i++] = trans[077 & (*s >> 2)];
990 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
994 if (tail_lf) buff[i++] =
'\n';
996 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
999static const char hex_table[] =
"0123456789ABCDEF";
1005 long i = 0, n = 0, prev = EOF;
1006 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
1007 unsigned char *send = s + RSTRING_LEN(from);
1011 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
1014 buff[i++] = hex_table[*s >> 4];
1015 buff[i++] = hex_table[*s & 0x0f];
1019 else if (*s ==
'\n') {
1020 if (prev ==
' ' || prev ==
'\t') {
1064#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
1066 if (mode == UNPACK_ARRAY) { \
1067 rb_ary_modify_expand(ary, len); \
1069 if (len > (long)((send-s)/(sz))) { \
1071 tmp_len = len-(send-s)/(sz); \
1073 len = (send-s)/(sz); \
1077#define PACK_ITEM_ADJUST() do { \
1078 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
1079 rb_ary_resize(ary, RARRAY_LEN(ary)+tmp_len); \
1086#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
1087# define AVOID_CC_BUG volatile
1089# define AVOID_CC_BUG
1099pack_unpack_internal(
VALUE str,
VALUE fmt,
VALUE ofs,
enum unpack_mode mode)
1101#define hexdigits ruby_hexdigits
1102 const char *s, *send;
1103 const char *p, *pend;
1106 AVOID_CC_BUG
long tmp_len;
1107 int signed_p, integer_size, bigendian_p;
1111#define UNPACK_PUSH(item) do {\
1112 VALUE item_val = (item);\
1113 if ((mode) == UNPACK_BLOCK) {\
1114 rb_yield(item_val);\
1116 if (RSTRING_PTR(str) != sptr || RSTRING_LEN(str) != slen) {\
1117 rb_raise(rb_eRuntimeError, "string modified");\
1120 else if ((mode) == UNPACK_ARRAY) {\
1121 rb_ary_push(ary, item_val);\
1133 len = RSTRING_LEN(str);
1134 if (offset < 0 ? (offset +=
len) < 0 : offset >
len) {
1135 rb_raise(rb_eArgError,
"offset outside of string");
1138 s = RSTRING_PTR(str);
1143 align_base = offset;
1145 p = RSTRING_PTR(fmt);
1146 pend = p + RSTRING_LEN(fmt);
1148#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1152 int explicit_endian = 0;
1153 const char type = *p++;
1156 USING_NATINT(
int natint = 0);
1158 if (skip_blank(p,
type))
continue;
1161 if ((
type ==
'x' ||
type ==
'@') && p < pend && *p ==
'!') {
1163 p = pack_alignment_size(p, pend, fmt, &align);
1164 len = pack_align_pad(s - RSTRING_PTR(str),
type ==
'@' ? 0 : align_base, align);
1165 if (
len > send - s) {
1166 rb_raise(rb_eArgError,
type ==
'@' ?
"@ outside of string" :
"x outside of string");
1172 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
1176 else if (*p ==
'*') {
1194 rb_raise(rb_eArgError,
"%% is not supported");
1198 if (
len > send - s)
len = send - s;
1201 const char *t = s +
len - 1;
1204 if (*t !=
' ' && *t !=
'\0')
break;
1216 if (
len > send-s)
len = send-s;
1217 while (t < s+
len && *t) t++;
1220 s = star ? t : s+
len;
1225 if (
len > send - s)
len = send - s;
1237 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1238 len = (send - s) * 8;
1241 t = RSTRING_PTR(bitstr);
1242 for (i=0; i<
len; i++) {
1243 if (i & 7) bits >>= 1;
1244 else bits = (
unsigned char)*s++;
1245 *t++ = (bits & 1) ?
'1' :
'0';
1247 UNPACK_PUSH(bitstr);
1258 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1259 len = (send - s) * 8;
1262 t = RSTRING_PTR(bitstr);
1263 for (i=0; i<
len; i++) {
1264 if (i & 7) bits <<= 1;
1265 else bits = (
unsigned char)*s++;
1266 *t++ = (bits & 128) ?
'1' :
'0';
1268 UNPACK_PUSH(bitstr);
1279 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1280 len = (send - s) * 2;
1283 t = RSTRING_PTR(bitstr);
1284 for (i=0; i<
len; i++) {
1288 bits = (
unsigned char)*s++;
1289 *t++ = hexdigits[bits & 15];
1291 UNPACK_PUSH(bitstr);
1302 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1303 len = (send - s) * 2;
1306 t = RSTRING_PTR(bitstr);
1307 for (i=0; i<
len; i++) {
1311 bits = (
unsigned char)*s++;
1312 *t++ = hexdigits[(bits >> 4) & 15];
1314 UNPACK_PUSH(bitstr);
1321 bigendian_p = BIGENDIAN_P();
1322 goto unpack_integer;
1327 bigendian_p = BIGENDIAN_P();
1328 goto unpack_integer;
1332 integer_size = NATINT_LEN(
short, 2);
1333 bigendian_p = BIGENDIAN_P();
1334 goto unpack_integer;
1338 integer_size = NATINT_LEN(
short, 2);
1339 bigendian_p = BIGENDIAN_P();
1340 goto unpack_integer;
1344 integer_size = (int)
sizeof(
int);
1345 bigendian_p = BIGENDIAN_P();
1346 goto unpack_integer;
1350 integer_size = (int)
sizeof(
int);
1351 bigendian_p = BIGENDIAN_P();
1352 goto unpack_integer;
1356 integer_size = NATINT_LEN(
long, 4);
1357 bigendian_p = BIGENDIAN_P();
1358 goto unpack_integer;
1362 integer_size = NATINT_LEN(
long, 4);
1363 bigendian_p = BIGENDIAN_P();
1364 goto unpack_integer;
1368 integer_size = NATINT_LEN_Q;
1369 bigendian_p = BIGENDIAN_P();
1370 goto unpack_integer;
1374 integer_size = NATINT_LEN_Q;
1375 bigendian_p = BIGENDIAN_P();
1376 goto unpack_integer;
1380 integer_size =
sizeof(intptr_t);
1381 bigendian_p = BIGENDIAN_P();
1382 goto unpack_integer;
1386 integer_size =
sizeof(uintptr_t);
1387 bigendian_p = BIGENDIAN_P();
1388 goto unpack_integer;
1394 goto unpack_integer;
1400 goto unpack_integer;
1406 goto unpack_integer;
1412 goto unpack_integer;
1415 if (explicit_endian) {
1416 bigendian_p = explicit_endian ==
'>';
1418 PACK_LENGTH_ADJUST_SIZE(integer_size);
1424 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1433 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1436 UNPACK_FETCH(&tmp,
float);
1437 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1443 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1445 FLOAT_CONVWITH(tmp);
1446 UNPACK_FETCH(tmp.buf,
float);
1454 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1456 DOUBLE_CONVWITH(tmp);
1457 UNPACK_FETCH(tmp.buf,
double);
1466 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1469 UNPACK_FETCH(&tmp,
double);
1476 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1478 FLOAT_CONVWITH(tmp);
1479 UNPACK_FETCH(tmp.buf,
float);
1487 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1489 DOUBLE_CONVWITH(tmp);
1490 UNPACK_FETCH(tmp.buf,
double);
1498 if (
len > send - s)
len = send - s;
1499 while (
len > 0 && s < send) {
1500 long alen = send - s;
1503 l = utf8_to_uv(s, &alen);
1512 char *ptr = RSTRING_PTR(buf);
1515 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1519 len = ((
unsigned char)*s++ -
' ') & 077;
1522 if (total > RSTRING_LEN(buf)) {
1523 len -= total - RSTRING_LEN(buf);
1524 total = RSTRING_LEN(buf);
1528 long mlen =
len > 3 ? 3 :
len;
1530 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1531 a = ((
unsigned char)*s++ -
' ') & 077;
1534 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1535 b = ((
unsigned char)*s++ -
' ') & 077;
1538 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1539 c = ((
unsigned char)*s++ -
' ') & 077;
1542 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1543 d = ((
unsigned char)*s++ -
' ') & 077;
1546 hunk[0] = (char)(a << 2 | b >> 4);
1547 hunk[1] = (char)(b << 4 | c >> 2);
1548 hunk[2] = (char)(c << 6 | d);
1549 memcpy(ptr, hunk, mlen);
1553 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1555 if (s < send && *s ==
'\r') s++;
1556 if (s < send && *s ==
'\n') s++;
1567 char *ptr = RSTRING_PTR(buf);
1568 int a = -1,b = -1,c = 0,d = 0;
1569 static signed char b64_xtable[256];
1571 if (b64_xtable[
'/'] <= 0) {
1574 for (i = 0; i < 256; i++) {
1577 for (i = 0; i < 64; i++) {
1578 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1584 a = b64_xtable[(
unsigned char)*s++];
1585 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1586 b = b64_xtable[(
unsigned char)*s++];
1587 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1589 if (s + 2 == send && *(s + 1) ==
'=')
break;
1590 rb_raise(rb_eArgError,
"invalid base64");
1592 c = b64_xtable[(
unsigned char)*s++];
1593 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1594 if (s + 1 == send && *s ==
'=')
break;
1595 d = b64_xtable[(
unsigned char)*s++];
1596 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1597 *ptr++ = castchar(a << 2 | b >> 4);
1598 *ptr++ = castchar(b << 4 | c >> 2);
1599 *ptr++ = castchar(c << 6 | d);
1602 *ptr++ = castchar(a << 2 | b >> 4);
1603 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1606 *ptr++ = castchar(a << 2 | b >> 4);
1607 *ptr++ = castchar(b << 4 | c >> 2);
1608 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1614 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1615 if (s >= send)
break;
1617 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1618 if (s >= send)
break;
1620 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1621 if (*s ==
'=' || s >= send)
break;
1623 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1624 if (*s ==
'=' || s >= send)
break;
1626 *ptr++ = castchar(a << 2 | b >> 4);
1627 *ptr++ = castchar(b << 4 | c >> 2);
1628 *ptr++ = castchar(c << 6 | d);
1631 if (a != -1 && b != -1) {
1633 *ptr++ = castchar(a << 2 | b >> 4);
1635 *ptr++ = castchar(a << 2 | b >> 4);
1636 *ptr++ = castchar(b << 4 | c >> 2);
1648 char *ptr = RSTRING_PTR(buf);
1655 if (++s == send)
break;
1656 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1659 if ((c1 = hex2num(*s)) == -1)
break;
1660 if (++s == send)
break;
1661 if ((c2 = hex2num(*s)) == -1)
break;
1662 csum |= *ptr++ = castchar(c1 << 4 | c2);
1666 csum |= *ptr++ = *s;
1680 if (
len > RSTRING_LEN(str))
1681 rb_raise(rb_eArgError,
"@ outside of string");
1682 s = RSTRING_PTR(str) +
len;
1686 if (
len > s - RSTRING_PTR(str))
1687 rb_raise(rb_eArgError,
"X outside of string");
1693 rb_raise(rb_eArgError,
"x outside of string");
1698 UNPACK_PUSH(
SSIZET2NUM(s - RSTRING_PTR(str)));
1702 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1706 UNPACK_FETCH(&t,
char *);
1708 if (!associates) associates = str_associated(str);
1709 tmp = associated_pointer(associates, t);
1710 if (
len < RSTRING_LEN(tmp)) {
1712 str_associate(tmp, associates);
1720 if (
len > (
long)((send - s) /
sizeof(
char *)))
1721 len = (send - s) /
sizeof(
char *);
1723 if ((
size_t)(send - s) <
sizeof(
char *))
1729 UNPACK_FETCH(&t,
char *);
1731 if (!associates) associates = str_associated(str);
1732 tmp = associated_pointer(associates, t);
1748 while (
len > 0 && s < send) {
1754 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1761 if (s0 != s &&
len > 0) {
1775 while (
len > 0 && s < send) {
1790 unknown_directive(
"unpack",
type, fmt);
1802 return pack_unpack_internal(str, fmt, offset, mode);
1808 return pack_unpack_internal(str, fmt, offset, UNPACK_1);
1819 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1820 buf[1] = castchar((uv&0x3f)|0x80);
1824 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1825 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1826 buf[2] = castchar((uv&0x3f)|0x80);
1829 if (uv <= 0x1fffff) {
1830 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1831 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1832 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1833 buf[3] = castchar((uv&0x3f)|0x80);
1836 if (uv <= 0x3ffffff) {
1837 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1838 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1839 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1840 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1841 buf[4] = castchar((uv&0x3f)|0x80);
1844 if (uv <= 0x7fffffff) {
1845 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1846 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1847 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1848 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1849 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1850 buf[5] = castchar((uv&0x3f)|0x80);
1858static const unsigned long utf8_limits[] = {
1869utf8_to_uv(
const char *p,
long *lenp)
1871 int c = *p++ & 0xff;
1872 unsigned long uv = c;
1881 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1884 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1885 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1886 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1887 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1888 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1891 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1894 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1901 if ((c & 0xc0) != 0x80) {
1903 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1912 if (uv < utf8_limits[n]) {
1913 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1918#include "pack.rbinc"
1923 id_associated = rb_make_internal_id();
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define SSIZET2NUM
Old name of RB_SSIZE2NUM.
#define STRTOUL
Old name of ruby_strtoul.
#define ISDIGIT
Old name of rb_isdigit.
#define ISALPHA
Old name of rb_isalpha.
#define ISASCII
Old name of rb_isascii.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define DBL2NUM
Old name of rb_float_new.
#define ISPRINT
Old name of rb_isprint.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define ENCODING_CODERANGE_SET(obj, encindex, cr)
Old name of RB_ENCODING_CODERANGE_SET.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
#define INTEGER_PACK_LITTLE_ENDIAN
Little endian combination.
#define INTEGER_PACK_BIG_ENDIAN
Big endian combination.
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Encodes a Unicode codepoint into its UTF-8 representation.
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_str_buf_cat
Just another name of rb_str_cat.
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
int len
Length of the buffer.
const signed char ruby_digit36_to_number_table[]
Character to number mapping like ‘'a’->10,'b'->11etc.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define errno
Ractor-aware version of errno.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.