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;
781 const long start = RSTRING_LEN(res);
785 if (
type ==
'R' && rb_int_negative_p(from)) {
786 rb_raise(rb_eArgError,
"can't encode negative numbers in ULEB128");
789 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
793 else if (nlz_bits == 0 &&
type ==
'r') {
799 cp = RSTRING_PTR(res) + start;
800 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
804 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
808 while (1 < numbytes) {
820 ptr = RSTRING_PTR(from);
821 plen = RSTRING_LEN(from);
824 encodes(res, ptr, plen,
type, 0);
830 else if (
len > 63 &&
type ==
'u')
841 encodes(res, ptr, todo,
type, 1);
852 qpencode(res, from,
len);
859 if (RSTRING_LEN(from) <
len) {
860 rb_raise(rb_eArgError,
"too short buffer for P(%ld for %ld)",
861 RSTRING_LEN(from),
len);
872 t = RSTRING_PTR(from);
891 numbytes = rb_absint_numwords(from, 7, NULL);
899 rb_raise(rb_eArgError,
"can't compress negative numbers");
901 rb_bug(
"buffer size problem?");
903 cp = RSTRING_PTR(buf);
904 while (1 < numbytes) {
915 unknown_directive(
"pack",
type, fmt);
922 str_associate(res, associates);
929 rb_enc_set_index(res, rb_utf8_encindex());
941 return pack_pack(ec, ary, fmt, buffer);
944static const char uu_table[] =
945"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
946static const char b64_table[] =
947"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
950encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
952 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
953 char buff[buff_size + 1];
955 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
957 const unsigned char *s = (
const unsigned char *)s0;
960 buff[i++] = (char)
len +
' ';
966 while (
len >= input_unit) {
967 while (
len >= input_unit && buff_size-i >= encoded_unit) {
968 buff[i++] = trans[077 & (*s >> 2)];
969 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
970 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
971 buff[i++] = trans[077 & s[2]];
975 if (buff_size-i < encoded_unit) {
982 buff[i++] = trans[077 & (*s >> 2)];
983 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
984 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
988 buff[i++] = trans[077 & (*s >> 2)];
989 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
993 if (tail_lf) buff[i++] =
'\n';
995 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
998static const char hex_table[] =
"0123456789ABCDEF";
1004 long i = 0, n = 0, prev = EOF;
1005 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
1006 unsigned char *send = s + RSTRING_LEN(from);
1010 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
1013 buff[i++] = hex_table[*s >> 4];
1014 buff[i++] = hex_table[*s & 0x0f];
1018 else if (*s ==
'\n') {
1019 if (prev ==
' ' || prev ==
'\t') {
1063#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
1065 if (mode == UNPACK_ARRAY) { \
1066 rb_ary_modify_expand(ary, len); \
1068 if (len > (long)((send-s)/(sz))) { \
1070 tmp_len = len-(send-s)/(sz); \
1072 len = (send-s)/(sz); \
1076#define PACK_ITEM_ADJUST() do { \
1077 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
1078 rb_ary_resize(ary, RARRAY_LEN(ary)+tmp_len); \
1085#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
1086# define AVOID_CC_BUG volatile
1088# define AVOID_CC_BUG
1098pack_unpack_internal(
VALUE str,
VALUE fmt,
VALUE ofs,
enum unpack_mode mode)
1100#define hexdigits ruby_hexdigits
1101 const char *s, *send;
1102 const char *p, *pend;
1105 AVOID_CC_BUG
long tmp_len;
1106 int signed_p, integer_size, bigendian_p;
1108#define UNPACK_PUSH(item) do {\
1109 VALUE item_val = (item);\
1110 if ((mode) == UNPACK_BLOCK) {\
1111 rb_yield(item_val);\
1113 else if ((mode) == UNPACK_ARRAY) {\
1114 rb_ary_push(ary, item_val);\
1126 len = RSTRING_LEN(str);
1127 if (offset < 0 ? (offset +=
len) < 0 : offset >
len) {
1128 rb_raise(rb_eArgError,
"offset outside of string");
1131 s = RSTRING_PTR(str);
1134 align_base = offset;
1136 p = RSTRING_PTR(fmt);
1137 pend = p + RSTRING_LEN(fmt);
1139#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1143 int explicit_endian = 0;
1144 const char type = *p++;
1147 USING_NATINT(
int natint = 0);
1149 if (skip_blank(p,
type))
continue;
1152 if ((
type ==
'x' ||
type ==
'@') && p < pend && *p ==
'!') {
1154 p = pack_alignment_size(p, pend, fmt, &align);
1155 len = pack_align_pad(s - RSTRING_PTR(str),
type ==
'@' ? 0 : align_base, align);
1156 if (
len > send - s) {
1157 rb_raise(rb_eArgError,
type ==
'@' ?
"@ outside of string" :
"x outside of string");
1163 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
1167 else if (*p ==
'*') {
1185 rb_raise(rb_eArgError,
"%% is not supported");
1189 if (
len > send - s)
len = send - s;
1192 const char *t = s +
len - 1;
1195 if (*t !=
' ' && *t !=
'\0')
break;
1207 if (
len > send-s)
len = send-s;
1208 while (t < s+
len && *t) t++;
1211 s = star ? t : s+
len;
1216 if (
len > send - s)
len = send - s;
1228 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1229 len = (send - s) * 8;
1232 t = RSTRING_PTR(bitstr);
1233 for (i=0; i<
len; i++) {
1234 if (i & 7) bits >>= 1;
1235 else bits = (
unsigned char)*s++;
1236 *t++ = (bits & 1) ?
'1' :
'0';
1238 UNPACK_PUSH(bitstr);
1249 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1250 len = (send - s) * 8;
1253 t = RSTRING_PTR(bitstr);
1254 for (i=0; i<
len; i++) {
1255 if (i & 7) bits <<= 1;
1256 else bits = (
unsigned char)*s++;
1257 *t++ = (bits & 128) ?
'1' :
'0';
1259 UNPACK_PUSH(bitstr);
1270 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1271 len = (send - s) * 2;
1274 t = RSTRING_PTR(bitstr);
1275 for (i=0; i<
len; i++) {
1279 bits = (
unsigned char)*s++;
1280 *t++ = hexdigits[bits & 15];
1282 UNPACK_PUSH(bitstr);
1293 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1294 len = (send - s) * 2;
1297 t = RSTRING_PTR(bitstr);
1298 for (i=0; i<
len; i++) {
1302 bits = (
unsigned char)*s++;
1303 *t++ = hexdigits[(bits >> 4) & 15];
1305 UNPACK_PUSH(bitstr);
1312 bigendian_p = BIGENDIAN_P();
1313 goto unpack_integer;
1318 bigendian_p = BIGENDIAN_P();
1319 goto unpack_integer;
1323 integer_size = NATINT_LEN(
short, 2);
1324 bigendian_p = BIGENDIAN_P();
1325 goto unpack_integer;
1329 integer_size = NATINT_LEN(
short, 2);
1330 bigendian_p = BIGENDIAN_P();
1331 goto unpack_integer;
1335 integer_size = (int)
sizeof(
int);
1336 bigendian_p = BIGENDIAN_P();
1337 goto unpack_integer;
1341 integer_size = (int)
sizeof(
int);
1342 bigendian_p = BIGENDIAN_P();
1343 goto unpack_integer;
1347 integer_size = NATINT_LEN(
long, 4);
1348 bigendian_p = BIGENDIAN_P();
1349 goto unpack_integer;
1353 integer_size = NATINT_LEN(
long, 4);
1354 bigendian_p = BIGENDIAN_P();
1355 goto unpack_integer;
1359 integer_size = NATINT_LEN_Q;
1360 bigendian_p = BIGENDIAN_P();
1361 goto unpack_integer;
1365 integer_size = NATINT_LEN_Q;
1366 bigendian_p = BIGENDIAN_P();
1367 goto unpack_integer;
1371 integer_size =
sizeof(intptr_t);
1372 bigendian_p = BIGENDIAN_P();
1373 goto unpack_integer;
1377 integer_size =
sizeof(uintptr_t);
1378 bigendian_p = BIGENDIAN_P();
1379 goto unpack_integer;
1385 goto unpack_integer;
1391 goto unpack_integer;
1397 goto unpack_integer;
1403 goto unpack_integer;
1406 if (explicit_endian) {
1407 bigendian_p = explicit_endian ==
'>';
1409 PACK_LENGTH_ADJUST_SIZE(integer_size);
1415 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1424 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1427 UNPACK_FETCH(&tmp,
float);
1428 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1434 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1436 FLOAT_CONVWITH(tmp);
1437 UNPACK_FETCH(tmp.buf,
float);
1445 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1447 DOUBLE_CONVWITH(tmp);
1448 UNPACK_FETCH(tmp.buf,
double);
1457 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1460 UNPACK_FETCH(&tmp,
double);
1467 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1469 FLOAT_CONVWITH(tmp);
1470 UNPACK_FETCH(tmp.buf,
float);
1478 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1480 DOUBLE_CONVWITH(tmp);
1481 UNPACK_FETCH(tmp.buf,
double);
1489 if (
len > send - s)
len = send - s;
1490 while (
len > 0 && s < send) {
1491 long alen = send - s;
1494 l = utf8_to_uv(s, &alen);
1503 char *ptr = RSTRING_PTR(buf);
1506 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1510 len = ((
unsigned char)*s++ -
' ') & 077;
1513 if (total > RSTRING_LEN(buf)) {
1514 len -= total - RSTRING_LEN(buf);
1515 total = RSTRING_LEN(buf);
1519 long mlen =
len > 3 ? 3 :
len;
1521 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1522 a = ((
unsigned char)*s++ -
' ') & 077;
1525 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1526 b = ((
unsigned char)*s++ -
' ') & 077;
1529 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1530 c = ((
unsigned char)*s++ -
' ') & 077;
1533 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1534 d = ((
unsigned char)*s++ -
' ') & 077;
1537 hunk[0] = (char)(a << 2 | b >> 4);
1538 hunk[1] = (char)(b << 4 | c >> 2);
1539 hunk[2] = (char)(c << 6 | d);
1540 memcpy(ptr, hunk, mlen);
1544 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1546 if (s < send && *s ==
'\r') s++;
1547 if (s < send && *s ==
'\n') s++;
1558 char *ptr = RSTRING_PTR(buf);
1559 int a = -1,b = -1,c = 0,d = 0;
1560 static signed char b64_xtable[256];
1562 if (b64_xtable[
'/'] <= 0) {
1565 for (i = 0; i < 256; i++) {
1568 for (i = 0; i < 64; i++) {
1569 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1575 a = b64_xtable[(
unsigned char)*s++];
1576 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1577 b = b64_xtable[(
unsigned char)*s++];
1578 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1580 if (s + 2 == send && *(s + 1) ==
'=')
break;
1581 rb_raise(rb_eArgError,
"invalid base64");
1583 c = b64_xtable[(
unsigned char)*s++];
1584 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1585 if (s + 1 == send && *s ==
'=')
break;
1586 d = b64_xtable[(
unsigned char)*s++];
1587 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1588 *ptr++ = castchar(a << 2 | b >> 4);
1589 *ptr++ = castchar(b << 4 | c >> 2);
1590 *ptr++ = castchar(c << 6 | d);
1593 *ptr++ = castchar(a << 2 | b >> 4);
1594 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1597 *ptr++ = castchar(a << 2 | b >> 4);
1598 *ptr++ = castchar(b << 4 | c >> 2);
1599 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1605 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1606 if (s >= send)
break;
1608 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1609 if (s >= send)
break;
1611 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1612 if (*s ==
'=' || s >= send)
break;
1614 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1615 if (*s ==
'=' || s >= send)
break;
1617 *ptr++ = castchar(a << 2 | b >> 4);
1618 *ptr++ = castchar(b << 4 | c >> 2);
1619 *ptr++ = castchar(c << 6 | d);
1622 if (a != -1 && b != -1) {
1624 *ptr++ = castchar(a << 2 | b >> 4);
1626 *ptr++ = castchar(a << 2 | b >> 4);
1627 *ptr++ = castchar(b << 4 | c >> 2);
1639 char *ptr = RSTRING_PTR(buf);
1646 if (++s == send)
break;
1647 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1650 if ((c1 = hex2num(*s)) == -1)
break;
1651 if (++s == send)
break;
1652 if ((c2 = hex2num(*s)) == -1)
break;
1653 csum |= *ptr++ = castchar(c1 << 4 | c2);
1657 csum |= *ptr++ = *s;
1671 if (
len > RSTRING_LEN(str))
1672 rb_raise(rb_eArgError,
"@ outside of string");
1673 s = RSTRING_PTR(str) +
len;
1677 if (
len > s - RSTRING_PTR(str))
1678 rb_raise(rb_eArgError,
"X outside of string");
1684 rb_raise(rb_eArgError,
"x outside of string");
1689 UNPACK_PUSH(
SSIZET2NUM(s - RSTRING_PTR(str)));
1693 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1697 UNPACK_FETCH(&t,
char *);
1699 if (!associates) associates = str_associated(str);
1700 tmp = associated_pointer(associates, t);
1701 if (
len < RSTRING_LEN(tmp)) {
1703 str_associate(tmp, associates);
1711 if (
len > (
long)((send - s) /
sizeof(
char *)))
1712 len = (send - s) /
sizeof(
char *);
1714 if ((
size_t)(send - s) <
sizeof(
char *))
1720 UNPACK_FETCH(&t,
char *);
1722 if (!associates) associates = str_associated(str);
1723 tmp = associated_pointer(associates, t);
1739 while (
len > 0 && s < send) {
1745 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1752 if (s0 != s &&
len > 0) {
1766 while (
len > 0 && s < send) {
1781 unknown_directive(
"unpack",
type, fmt);
1793 return pack_unpack_internal(str, fmt, offset, mode);
1799 return pack_unpack_internal(str, fmt, offset, UNPACK_1);
1810 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1811 buf[1] = castchar((uv&0x3f)|0x80);
1815 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1816 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1817 buf[2] = castchar((uv&0x3f)|0x80);
1820 if (uv <= 0x1fffff) {
1821 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1822 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1823 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1824 buf[3] = castchar((uv&0x3f)|0x80);
1827 if (uv <= 0x3ffffff) {
1828 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1829 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1830 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1831 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1832 buf[4] = castchar((uv&0x3f)|0x80);
1835 if (uv <= 0x7fffffff) {
1836 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1837 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1838 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1839 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1840 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1841 buf[5] = castchar((uv&0x3f)|0x80);
1849static const unsigned long utf8_limits[] = {
1860utf8_to_uv(
const char *p,
long *lenp)
1862 int c = *p++ & 0xff;
1863 unsigned long uv = c;
1872 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1875 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1876 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1877 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1878 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1879 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1882 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1885 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1892 if ((c & 0xc0) != 0x80) {
1894 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1903 if (uv < utf8_limits[n]) {
1904 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1909#include "pack.rbinc"
1914 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.