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";
125static unsigned long utf8_to_uv(
const char*,
rb_len_t*);
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) {
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':
291 if (alignment <= 1)
return 0;
295 if (mod < 0) mod += alignment;
296 return mod ? (
rb_len_t)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);
336pack_pack(rb_execution_context_t *ec,
VALUE ary,
VALUE fmt,
VALUE buffer)
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;
378 const char type = *p++;
379 USING_NATINT(
int natint = 0);
381 if (skip_blank(p,
type))
continue;
384 if ((
type ==
'x' ||
type ==
'@') && p < pend && *p ==
'!') {
386 p = pack_alignment_size(p, pend, fmt, &align);
387 len = pack_align_pad(RSTRING_LEN(res),
type ==
'@' ? 0 : align_base, align);
389 str_expand_fill(res,
'\0',
len);
393 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
398 : strchr(
"PMm",
type) ? 1
416 if (enc_info == 1) enc_info = 2;
418 case 'm':
case 'M':
case 'u':
427 case 'A':
case 'a':
case 'Z':
437 ptr = RSTRING_PTR(from);
438 plen = RSTRING_LEN(from);
452 if (star &&
type ==
'Z')
458 str_expand_fill(res, (
type ==
'A' ?
' ' :
'\0'),
len - plen);
462#define castchar(from) (char)((from) & 0xff)
470 j = (
len - plen + 1)/2;
473 for (i=0; i++ <
len; ptr++) {
479 char c = castchar(
byte);
486 byte >>= 7 - (
len & 7);
501 j = (
len - plen + 1)/2;
504 for (i=0; i++ <
len; ptr++) {
509 char c = castchar(
byte);
516 byte <<= 7 - (
len & 7);
531 j = (
len + 1) / 2 - (plen + 1) / 2;
534 for (i=0; i++ <
len; ptr++) {
536 byte |= (((*ptr & 15) + 9) & 15) << 4;
538 byte |= (*ptr & 15) << 4;
542 char c = castchar(
byte);
548 char c = castchar(
byte);
562 j = (
len + 1) / 2 - (plen + 1) / 2;
565 for (i=0; i++ <
len; ptr++) {
567 byte |= ((*ptr & 15) + 9) & 15;
573 char c = castchar(
byte);
579 char c = castchar(
byte);
592 bigendian_p = BIGENDIAN_P();
597 integer_size = NATINT_LEN(
short, 2);
598 bigendian_p = BIGENDIAN_P();
603 integer_size = (int)
sizeof(
int);
604 bigendian_p = BIGENDIAN_P();
609 integer_size = NATINT_LEN(
long, 4);
610 bigendian_p = BIGENDIAN_P();
615 integer_size = NATINT_LEN_Q;
616 bigendian_p = BIGENDIAN_P();
620 integer_size =
sizeof(intptr_t);
621 bigendian_p = BIGENDIAN_P();
625 integer_size =
sizeof(uintptr_t);
626 bigendian_p = BIGENDIAN_P();
650 if (explicit_endian) {
651 bigendian_p = explicit_endian ==
'>';
653 if (integer_size > MAX_INTEGER_PACK_SIZE)
654 rb_bug(
"unexpected integer size for pack: %d", integer_size);
656 char intbuf[MAX_INTEGER_PACK_SIZE];
659 rb_integer_pack(from, intbuf, integer_size, 1, 0,
672 f = VALUE_to_float(from);
682 tmp.f = VALUE_to_float(from);
690 DOUBLE_CONVWITH(tmp);
713 tmp.f = VALUE_to_float(from);
721 DOUBLE_CONVWITH(tmp);
733 str_expand_fill(res,
'\0',
len);
738 plen = RSTRING_LEN(res);
740 rb_raise(rb_eArgError,
"X outside of string");
745 len -= RSTRING_LEN(res);
746 if (
len > 0)
goto grow;
748 if (
len > 0)
goto shrink;
752 rb_raise(rb_eArgError,
"%% is not supported");
782 size_t numbytes, nlz_bits;
788 if (
type ==
'R' && rb_int_negative_p(from)) {
789 rb_raise(rb_eArgError,
"can't encode negative numbers in ULEB128");
792 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
796 else if (nlz_bits == 0 &&
type ==
'r') {
804 cp = RSTRING_PTR(res) + start;
805 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
809 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
813 while (1 < numbytes) {
825 ptr = RSTRING_PTR(from);
826 plen = RSTRING_LEN(from);
829 encodes(res, ptr, plen,
type, 0);
835 else if (
len > 63 &&
type ==
'u')
846 encodes(res, ptr, todo,
type, 1);
857 qpencode(res, from,
len);
864 if (RSTRING_LEN(from) <
len) {
865 rb_raise(rb_eArgError,
"too short buffer for P(%"PRIdLEN
" for %"PRIdLEN
")",
866 RSTRING_LEN(from),
len);
877 t = RSTRING_PTR(from);
896 numbytes = rb_absint_numwords(from, 7, NULL);
904 rb_raise(rb_eArgError,
"can't compress negative numbers");
906 rb_bug(
"buffer size problem?");
908 cp = RSTRING_PTR(buf);
909 while (1 < numbytes) {
923 unknown_directive(
"pack",
type, fmt);
930 str_associate(res, associates);
947rb_ec_pack_ary(rb_execution_context_t *ec,
VALUE ary,
VALUE fmt,
VALUE buffer)
949 return pack_pack(ec, ary, fmt, buffer);
952static const char uu_table[] =
953"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
954static const char b64_table[] =
955"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
960 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
961 char buff[buff_size + 1];
963 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
965 const unsigned char *s = (
const unsigned char *)s0;
968 buff[i++] = (char)
len +
' ';
974 while (
len >= input_unit) {
975 while (
len >= input_unit && buff_size-i >= encoded_unit) {
976 buff[i++] = trans[077 & (*s >> 2)];
977 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
978 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
979 buff[i++] = trans[077 & s[2]];
983 if (buff_size-i < encoded_unit) {
990 buff[i++] = trans[077 & (*s >> 2)];
991 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
992 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
996 buff[i++] = trans[077 & (*s >> 2)];
997 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
1001 if (tail_lf) buff[i++] =
'\n';
1003 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
1006static const char hex_table[] =
"0123456789ABCDEF";
1013 long n = 0, prev = EOF;
1014 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
1015 unsigned char *send = s + RSTRING_LEN(from);
1019 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
1022 buff[i++] = hex_table[*s >> 4];
1023 buff[i++] = hex_table[*s & 0x0f];
1027 else if (*s ==
'\n') {
1028 if (prev ==
' ' || prev ==
'\t') {
1072#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
1074 if (mode == UNPACK_ARRAY) { \
1075 rb_ary_modify_expand(ary, len); \
1077 if (len > (rb_len_t)((send-s)/(sz))) { \
1079 tmp_len = len-(send-s)/(sz); \
1081 len = (send-s)/(sz); \
1085#define PACK_ITEM_ADJUST() do { \
1086 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
1087 rb_ary_resize(ary, RARRAY_LEN(ary)+tmp_len); \
1094#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
1095# define AVOID_CC_BUG volatile
1097# define AVOID_CC_BUG
1107pack_unpack_internal(
VALUE str,
VALUE fmt,
VALUE ofs,
enum unpack_mode mode)
1109#define hexdigits ruby_hexdigits
1110 const char *s, *send;
1111 const char *p, *pend;
1115 int signed_p, integer_size, bigendian_p;
1121#define UNPACK_PUSH(item) do {\
1122 VALUE item_val = (item);\
1123 if ((mode) == UNPACK_BLOCK) {\
1124 rb_yield(item_val);\
1126 if (RSTRING_PTR(str) != sptr || RSTRING_LEN(str) != slen) {\
1127 rb_raise(rb_eRuntimeError, "string modified");\
1130 if (RSTRING_PTR(fmt) != fptr || RSTRING_LEN(fmt) != flen) {\
1131 rb_raise(rb_eRuntimeError, "format string modified");\
1134 else if ((mode) == UNPACK_ARRAY) {\
1135 rb_ary_push(ary, item_val);\
1147 len = RSTRING_LEN(str);
1148 if (offset < 0 ? (offset +=
len) < 0 : offset >
len) {
1149 rb_raise(rb_eArgError,
"offset outside of string");
1152 s = RSTRING_PTR(str);
1157 align_base = offset;
1159 p = RSTRING_PTR(fmt);
1160 pend = p + RSTRING_LEN(fmt);
1162 flen = RSTRING_LEN(fmt);
1164#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1168 int explicit_endian = 0;
1169 const char type = *p++;
1172 USING_NATINT(
int natint = 0);
1174 if (skip_blank(p,
type))
continue;
1177 if ((
type ==
'x' ||
type ==
'@') && p < pend && *p ==
'!') {
1179 p = pack_alignment_size(p, pend, fmt, &align);
1180 len = pack_align_pad(s - RSTRING_PTR(str),
type ==
'@' ? 0 : align_base, align);
1181 if (
len > send - s) {
1182 rb_raise(rb_eArgError,
type ==
'@' ?
"@ outside of string" :
"x outside of string");
1188 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
1192 else if (*p ==
'*') {
1210 rb_raise(rb_eArgError,
"%% is not supported");
1214 if (
len > send - s)
len = send - s;
1217 const char *t = s +
len - 1;
1220 if (*t !=
' ' && *t !=
'\0')
break;
1232 if (
len > send-s)
len = send-s;
1233 while (t < s+
len && *t) t++;
1236 s = star ? t : s+
len;
1241 if (
len > send - s)
len = send - s;
1253 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1254 len = (send - s) * 8;
1257 t = RSTRING_PTR(bitstr);
1258 for (i=0; i<
len; i++) {
1259 if (i & 7) bits >>= 1;
1260 else bits = (
unsigned char)*s++;
1261 *t++ = (bits & 1) ?
'1' :
'0';
1263 UNPACK_PUSH(bitstr);
1274 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1275 len = (send - s) * 8;
1278 t = RSTRING_PTR(bitstr);
1279 for (i=0; i<
len; i++) {
1280 if (i & 7) bits <<= 1;
1281 else bits = (
unsigned char)*s++;
1282 *t++ = (bits & 128) ?
'1' :
'0';
1284 UNPACK_PUSH(bitstr);
1295 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1296 len = (send - s) * 2;
1299 t = RSTRING_PTR(bitstr);
1300 for (i=0; i<
len; i++) {
1304 bits = (
unsigned char)*s++;
1305 *t++ = hexdigits[bits & 15];
1307 UNPACK_PUSH(bitstr);
1318 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1319 len = (send - s) * 2;
1322 t = RSTRING_PTR(bitstr);
1323 for (i=0; i<
len; i++) {
1327 bits = (
unsigned char)*s++;
1328 *t++ = hexdigits[(bits >> 4) & 15];
1330 UNPACK_PUSH(bitstr);
1337 bigendian_p = BIGENDIAN_P();
1338 goto unpack_integer;
1343 bigendian_p = BIGENDIAN_P();
1344 goto unpack_integer;
1348 integer_size = NATINT_LEN(
short, 2);
1349 bigendian_p = BIGENDIAN_P();
1350 goto unpack_integer;
1354 integer_size = NATINT_LEN(
short, 2);
1355 bigendian_p = BIGENDIAN_P();
1356 goto unpack_integer;
1360 integer_size = (int)
sizeof(
int);
1361 bigendian_p = BIGENDIAN_P();
1362 goto unpack_integer;
1366 integer_size = (int)
sizeof(
int);
1367 bigendian_p = BIGENDIAN_P();
1368 goto unpack_integer;
1372 integer_size = NATINT_LEN(
long, 4);
1373 bigendian_p = BIGENDIAN_P();
1374 goto unpack_integer;
1378 integer_size = NATINT_LEN(
long, 4);
1379 bigendian_p = BIGENDIAN_P();
1380 goto unpack_integer;
1384 integer_size = NATINT_LEN_Q;
1385 bigendian_p = BIGENDIAN_P();
1386 goto unpack_integer;
1390 integer_size = NATINT_LEN_Q;
1391 bigendian_p = BIGENDIAN_P();
1392 goto unpack_integer;
1396 integer_size =
sizeof(intptr_t);
1397 bigendian_p = BIGENDIAN_P();
1398 goto unpack_integer;
1402 integer_size =
sizeof(uintptr_t);
1403 bigendian_p = BIGENDIAN_P();
1404 goto unpack_integer;
1410 goto unpack_integer;
1416 goto unpack_integer;
1422 goto unpack_integer;
1428 goto unpack_integer;
1431 if (explicit_endian) {
1432 bigendian_p = explicit_endian ==
'>';
1434 PACK_LENGTH_ADJUST_SIZE(integer_size);
1440 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1449 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1452 UNPACK_FETCH(&tmp,
float);
1453 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1459 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1461 FLOAT_CONVWITH(tmp);
1462 UNPACK_FETCH(tmp.buf,
float);
1470 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1472 DOUBLE_CONVWITH(tmp);
1473 UNPACK_FETCH(tmp.buf,
double);
1482 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1485 UNPACK_FETCH(&tmp,
double);
1492 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1494 FLOAT_CONVWITH(tmp);
1495 UNPACK_FETCH(tmp.buf,
float);
1503 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1505 DOUBLE_CONVWITH(tmp);
1506 UNPACK_FETCH(tmp.buf,
double);
1514 if (
len > send - s)
len = send - s;
1515 while (
len > 0 && s < send) {
1519 l = utf8_to_uv(s, &alen);
1528 VALUE buf =
rb_str_new(0, (send - s) / 4 * 3 + (send - s) % 4 * 3 / 4);
1529 char *ptr = RSTRING_PTR(buf);
1532 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1536 len = ((
unsigned char)*s++ -
' ') & 077;
1539 if (total > RSTRING_LEN(buf)) {
1540 len -= total - RSTRING_LEN(buf);
1541 total = RSTRING_LEN(buf);
1547 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1548 a = ((
unsigned char)*s++ -
' ') & 077;
1551 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1552 b = ((
unsigned char)*s++ -
' ') & 077;
1555 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1556 c = ((
unsigned char)*s++ -
' ') & 077;
1559 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1560 d = ((
unsigned char)*s++ -
' ') & 077;
1563 hunk[0] = (char)(a << 2 | b >> 4);
1564 hunk[1] = (char)(b << 4 | c >> 2);
1565 hunk[2] = (char)(c << 6 | d);
1566 memcpy(ptr, hunk, mlen);
1570 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1572 if (s < send && *s ==
'\r') s++;
1573 if (s < send && *s ==
'\n') s++;
1584 char *ptr = RSTRING_PTR(buf);
1585 int a = -1,b = -1,c = 0,d = 0;
1586 static signed char b64_xtable[256];
1588 if (b64_xtable[
'/'] <= 0) {
1591 for (i = 0; i < 256; i++) {
1594 for (i = 0; i < 64; i++) {
1595 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1601 a = b64_xtable[(
unsigned char)*s++];
1602 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1603 b = b64_xtable[(
unsigned char)*s++];
1604 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1606 if (s + 2 == send && *(s + 1) ==
'=')
break;
1607 rb_raise(rb_eArgError,
"invalid base64");
1609 c = b64_xtable[(
unsigned char)*s++];
1610 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1611 if (s + 1 == send && *s ==
'=')
break;
1612 d = b64_xtable[(
unsigned char)*s++];
1613 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1614 *ptr++ = castchar(a << 2 | b >> 4);
1615 *ptr++ = castchar(b << 4 | c >> 2);
1616 *ptr++ = castchar(c << 6 | d);
1619 *ptr++ = castchar(a << 2 | b >> 4);
1620 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1623 *ptr++ = castchar(a << 2 | b >> 4);
1624 *ptr++ = castchar(b << 4 | c >> 2);
1625 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1631 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1632 if (s >= send)
break;
1634 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1635 if (s >= send)
break;
1637 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1638 if (*s ==
'=' || s >= send)
break;
1640 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1641 if (*s ==
'=' || s >= send)
break;
1643 *ptr++ = castchar(a << 2 | b >> 4);
1644 *ptr++ = castchar(b << 4 | c >> 2);
1645 *ptr++ = castchar(c << 6 | d);
1648 if (a != -1 && b != -1) {
1650 *ptr++ = castchar(a << 2 | b >> 4);
1652 *ptr++ = castchar(a << 2 | b >> 4);
1653 *ptr++ = castchar(b << 4 | c >> 2);
1665 char *ptr = RSTRING_PTR(buf);
1672 if (++s == send)
break;
1673 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1676 if ((c1 = hex2num(*s)) == -1)
break;
1677 if (++s == send)
break;
1678 if ((c2 = hex2num(*s)) == -1)
break;
1679 csum |= *ptr++ = castchar(c1 << 4 | c2);
1683 csum |= *ptr++ = *s;
1697 if (
len > RSTRING_LEN(str))
1698 rb_raise(rb_eArgError,
"@ outside of string");
1699 s = RSTRING_PTR(str) +
len;
1703 if (
len > s - RSTRING_PTR(str))
1704 rb_raise(rb_eArgError,
"X outside of string");
1710 rb_raise(rb_eArgError,
"x outside of string");
1715 UNPACK_PUSH(
SSIZET2NUM(s - RSTRING_PTR(str)));
1719 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1723 UNPACK_FETCH(&t,
char *);
1725 if (!associates) associates = str_associated(str);
1726 tmp = associated_pointer(associates, t);
1727 if (
len < RSTRING_LEN(tmp)) {
1729 str_associate(tmp, associates);
1737 if (
len > (
rb_len_t)((send - s) /
sizeof(
char *)))
1738 len = (send - s) /
sizeof(
char *);
1740 if ((
size_t)(send - s) <
sizeof(
char *))
1746 UNPACK_FETCH(&t,
char *);
1748 if (!associates) associates = str_associated(str);
1749 tmp = associated_pointer(associates, t);
1765 while (
len > 0 && s < send) {
1771 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1778 if (s0 != s &&
len > 0) {
1792 while (
len > 0 && s < send) {
1807 unknown_directive(
"unpack",
type, fmt);
1816pack_unpack(rb_execution_context_t *ec,
VALUE str,
VALUE fmt,
VALUE offset)
1819 return pack_unpack_internal(str, fmt, offset, mode);
1823pack_unpack1(rb_execution_context_t *ec,
VALUE str,
VALUE fmt,
VALUE offset)
1825 return pack_unpack_internal(str, fmt, offset, UNPACK_1);
1836 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1837 buf[1] = castchar((uv&0x3f)|0x80);
1841 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1842 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1843 buf[2] = castchar((uv&0x3f)|0x80);
1846 if (uv <= 0x1fffff) {
1847 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1848 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1849 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1850 buf[3] = castchar((uv&0x3f)|0x80);
1853 if (uv <= 0x3ffffff) {
1854 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1855 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1856 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1857 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1858 buf[4] = castchar((uv&0x3f)|0x80);
1861 if (uv <= 0x7fffffff) {
1862 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1863 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1864 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1865 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1866 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1867 buf[5] = castchar((uv&0x3f)|0x80);
1875static const unsigned long utf8_limits[] = {
1886utf8_to_uv(
const char *p,
rb_len_t *lenp)
1888 int c = *p++ & 0xff;
1889 unsigned long uv = c;
1898 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1901 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1902 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1903 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1904 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1905 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1908 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1911 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %"PRIdLEN
" bytes, given %"PRIdLEN
" bytes)",
1918 if ((c & 0xc0) != 0x80) {
1920 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1929 if (uv < utf8_limits[n]) {
1930 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1935#include "pack.rbinc"
1940 id_associated = rb_make_internal_id();
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
#define STRTOUL
@old{ruby_strtoul}
#define ISDIGIT
@old{rb_isdigit}
#define ISALPHA
@old{rb_isalpha}
#define ISPRINT
@old{rb_isprint}
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 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 NUM2LONG
Old name of RB_NUM2LONG.
#define NUM2LEN
Old name of RB_NUM2LEN.
#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.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]].
int rb_utf8_encindex(void)
Identical to rb_utf8_encoding(), except it returns the encoding's index instead of the encoding itsel...
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
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.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#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, rb_len_t 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.
VALUE rb_str_buf_new(rb_len_t 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' -> 11 etc.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
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.