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)
64 static int endian_value;
67 if (init)
return endian_value;
70 return endian_value = p[0]?0:1;
72# define BIGENDIAN_P() (is_bigendian())
73#elif defined(WORDS_BIGENDIAN)
74# define BIGENDIAN_P() 1
76# define BIGENDIAN_P() 0
80# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
82# define NATINT_LEN(type,len) ((int)sizeof(type))
95#define swapf(x) swap32(x)
96#define swapd(x) swap64(x)
98#define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
99#define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
100#define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
101#define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
102#define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
103#define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
104#define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
105#define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
107#define FLOAT_CONVWITH(x) FLOAT_SWAPPER x;
108#define HTONF(x) ((x).u = rb_htonf((x).u))
109#define HTOVF(x) ((x).u = rb_htovf((x).u))
110#define NTOHF(x) ((x).u = rb_ntohf((x).u))
111#define VTOHF(x) ((x).u = rb_vtohf((x).u))
113#define DOUBLE_CONVWITH(x) DOUBLE_SWAPPER x;
114#define HTOND(x) ((x).u = rb_htond((x).u))
115#define HTOVD(x) ((x).u = rb_htovd((x).u))
116#define NTOHD(x) ((x).u = rb_ntohd((x).u))
117#define VTOHD(x) ((x).u = rb_vtohd((x).u))
119#define MAX_INTEGER_PACK_SIZE 8
121static const char toofew[] =
"too few arguments";
123static void encodes(
VALUE,
const char*,
long,
int,
int);
126static unsigned long utf8_to_uv(
const char*,
long*);
128static ID id_associated;
138str_associated(
VALUE str)
140 VALUE associates = rb_ivar_lookup(str, id_associated,
Qfalse);
142 rb_raise(rb_eArgError,
"no associated pointer");
147associated_pointer(
VALUE associates,
const char *t)
151 for (; p < pend; p++) {
155 rb_raise(rb_eArgError,
"non associated pointer");
161unknown_directive(const
char *mode,
char type,
VALUE fmt)
170 snprintf(unknown,
sizeof(unknown),
"\\x%.2x",
type & 0xff);
172 fmt = rb_str_quote_unprintable(fmt);
173 rb_raise(rb_eArgError,
"unknown %s directive '%s' in '%"PRIsVALUE
"'",
178VALUE_to_float(
VALUE obj)
186 else if (d < -FLT_MAX) {
189 else if (d <= FLT_MAX) {
198str_expand_fill(
VALUE res,
int c,
long len)
200 long olen = RSTRING_LEN(res);
201 memset(RSTRING_PTR(res) + olen, c,
len);
206skip_to_eol(
const char *p,
const char *pend)
208 p = memchr(p,
'\n', pend - p);
209 return (
char *)(p ? p + 1 : pend);
212#define skip_blank(p, type) \
213 (ISSPACE(type) || (type == '#' && (p = skip_to_eol(p, pend), 1)))
216# define pack_modifiers(p, t, n, e) pack_modifiers(p, t, e)
219pack_modifiers(
const char *p,
char type,
int *natint,
int *explicit_endian)
225 if (strchr(natstr,
type)) {
232 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, natstr);
238 if (!strchr(endstr,
type)) {
239 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, endstr);
241 if (*explicit_endian) {
244 *explicit_endian = *p++;
255 const char *p, *pend;
256 VALUE res, from, associates = 0;
260 int integer_size, bigendian_p;
264 p = RSTRING_PTR(fmt);
265 pend = p + RSTRING_LEN(fmt);
273 rb_str_modify(buffer);
279#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
280#define MORE_ITEM (idx < RARRAY_LEN(ary))
281#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
282#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
285 int explicit_endian = 0;
289 const char type = *p++;
294 if (skip_blank(p,
type))
continue;
295 p = pack_modifiers(p,
type, &natint, &explicit_endian);
299 : strchr(
"PMm",
type) ? 1
317 if (enc_info == 1) enc_info = 2;
319 case 'm':
case 'M':
case 'u':
328 case 'A':
case 'a':
case 'Z':
338 ptr = RSTRING_PTR(from);
339 plen = RSTRING_LEN(from);
351 if (p[-1] ==
'*' &&
type ==
'Z')
357 str_expand_fill(res, (
type ==
'A' ?
' ' :
'\0'),
len - plen);
361#define castchar(from) (char)((from) & 0xff)
369 j = (
len - plen + 1)/2;
372 for (i=0; i++ <
len; ptr++) {
378 char c = castchar(
byte);
385 byte >>= 7 - (
len & 7);
400 j = (
len - plen + 1)/2;
403 for (i=0; i++ <
len; ptr++) {
408 char c = castchar(
byte);
415 byte <<= 7 - (
len & 7);
430 j = (
len + 1) / 2 - (plen + 1) / 2;
433 for (i=0; i++ <
len; ptr++) {
435 byte |= (((*ptr & 15) + 9) & 15) << 4;
437 byte |= (*ptr & 15) << 4;
441 char c = castchar(
byte);
447 char c = castchar(
byte);
461 j = (
len + 1) / 2 - (plen + 1) / 2;
464 for (i=0; i++ <
len; ptr++) {
466 byte |= ((*ptr & 15) + 9) & 15;
472 char c = castchar(
byte);
478 char c = castchar(
byte);
491 bigendian_p = BIGENDIAN_P();
496 integer_size = NATINT_LEN(
short, 2);
497 bigendian_p = BIGENDIAN_P();
502 integer_size = (int)
sizeof(
int);
503 bigendian_p = BIGENDIAN_P();
508 integer_size = NATINT_LEN(
long, 4);
509 bigendian_p = BIGENDIAN_P();
514 integer_size = NATINT_LEN_Q;
515 bigendian_p = BIGENDIAN_P();
519 integer_size =
sizeof(intptr_t);
520 bigendian_p = BIGENDIAN_P();
524 integer_size =
sizeof(uintptr_t);
525 bigendian_p = BIGENDIAN_P();
549 if (explicit_endian) {
550 bigendian_p = explicit_endian ==
'>';
552 if (integer_size > MAX_INTEGER_PACK_SIZE)
553 rb_bug(
"unexpected integer size for pack: %d", integer_size);
555 char intbuf[MAX_INTEGER_PACK_SIZE];
558 rb_integer_pack(from, intbuf, integer_size, 1, 0,
571 f = VALUE_to_float(from);
581 tmp.f = VALUE_to_float(from);
589 DOUBLE_CONVWITH(tmp);
612 tmp.f = VALUE_to_float(from);
620 DOUBLE_CONVWITH(tmp);
632 str_expand_fill(res,
'\0',
len);
637 plen = RSTRING_LEN(res);
639 rb_raise(rb_eArgError,
"X outside of string");
644 len -= RSTRING_LEN(res);
645 if (
len > 0)
goto grow;
647 if (
len > 0)
goto shrink;
651 rb_raise(rb_eArgError,
"%% is not supported");
681 size_t numbytes, nlz_bits;
684 const long start = RSTRING_LEN(res);
688 if (
type ==
'R' && rb_int_negative_p(from)) {
689 rb_raise(rb_eArgError,
"can't encode negative numbers in ULEB128");
692 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
696 else if (nlz_bits == 0 &&
type ==
'r') {
702 cp = RSTRING_PTR(res) + start;
703 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
707 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
711 while (1 < numbytes) {
723 ptr = RSTRING_PTR(from);
724 plen = RSTRING_LEN(from);
727 encodes(res, ptr, plen,
type, 0);
733 else if (
len > 63 &&
type ==
'u')
744 encodes(res, ptr, todo,
type, 1);
754 qpencode(res, from,
len);
761 if (RSTRING_LEN(from) <
len) {
762 rb_raise(rb_eArgError,
"too short buffer for P(%ld for %ld)",
763 RSTRING_LEN(from),
len);
795 numbytes = rb_absint_numwords(from, 7, NULL);
803 rb_raise(rb_eArgError,
"can't compress negative numbers");
805 rb_bug(
"buffer size problem?");
807 cp = RSTRING_PTR(buf);
808 while (1 < numbytes) {
819 unknown_directive(
"pack",
type, fmt);
826 str_associate(res, associates);
833 rb_enc_set_index(res, rb_utf8_encindex());
845 return pack_pack(ec, ary, fmt, buffer);
848static const char uu_table[] =
849"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
850static const char b64_table[] =
851"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
854encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
856 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
857 char buff[buff_size + 1];
859 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
861 const unsigned char *s = (
const unsigned char *)s0;
864 buff[i++] = (char)
len +
' ';
870 while (
len >= input_unit) {
871 while (
len >= input_unit && buff_size-i >= encoded_unit) {
872 buff[i++] = trans[077 & (*s >> 2)];
873 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
874 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
875 buff[i++] = trans[077 & s[2]];
879 if (buff_size-i < encoded_unit) {
886 buff[i++] = trans[077 & (*s >> 2)];
887 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
888 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
892 buff[i++] = trans[077 & (*s >> 2)];
893 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
897 if (tail_lf) buff[i++] =
'\n';
899 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
902static const char hex_table[] =
"0123456789ABCDEF";
908 long i = 0, n = 0, prev = EOF;
909 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
910 unsigned char *send = s + RSTRING_LEN(from);
914 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
917 buff[i++] = hex_table[*s >> 4];
918 buff[i++] = hex_table[*s & 0x0f];
922 else if (*s ==
'\n') {
923 if (prev ==
' ' || prev ==
'\t') {
967#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
969 if (len > (long)((send-s)/(sz))) { \
971 tmp_len = len-(send-s)/(sz); \
973 len = (send-s)/(sz); \
977#define PACK_ITEM_ADJUST() do { \
978 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
979 rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
986#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
987# define AVOID_CC_BUG volatile
999pack_unpack_internal(
VALUE str,
VALUE fmt,
enum unpack_mode mode,
long offset)
1001#define hexdigits ruby_hexdigits
1006 AVOID_CC_BUG
long tmp_len;
1007 int signed_p, integer_size, bigendian_p;
1008#define UNPACK_PUSH(item) do {\
1009 VALUE item_val = (item);\
1010 if ((mode) == UNPACK_BLOCK) {\
1011 rb_yield(item_val);\
1013 else if ((mode) == UNPACK_ARRAY) {\
1014 rb_ary_push(ary, item_val);\
1025 if (offset < 0) rb_raise(rb_eArgError,
"offset can't be negative");
1026 len = RSTRING_LEN(str);
1027 if (offset >
len) rb_raise(rb_eArgError,
"offset outside of string");
1029 s = RSTRING_PTR(str);
1033 p = RSTRING_PTR(fmt);
1034 pend = p + RSTRING_LEN(fmt);
1036#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1040 int explicit_endian = 0;
1041 const char type = *p++;
1047 if (skip_blank(p,
type))
continue;
1048 p = pack_modifiers(p,
type, &natint, &explicit_endian);
1052 else if (*p ==
'*') {
1070 rb_raise(rb_eArgError,
"%% is not supported");
1074 if (
len > send - s)
len = send - s;
1077 char *t = s +
len - 1;
1080 if (*t !=
' ' && *t !=
'\0')
break;
1092 if (
len > send-s)
len = send-s;
1093 while (t < s+
len && *t) t++;
1096 s = star ? t : s+
len;
1101 if (
len > send - s)
len = send - s;
1113 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1114 len = (send - s) * 8;
1117 t = RSTRING_PTR(bitstr);
1118 for (i=0; i<
len; i++) {
1119 if (i & 7) bits >>= 1;
1120 else bits = (
unsigned char)*s++;
1121 *t++ = (bits & 1) ?
'1' :
'0';
1123 UNPACK_PUSH(bitstr);
1134 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1135 len = (send - s) * 8;
1138 t = RSTRING_PTR(bitstr);
1139 for (i=0; i<
len; i++) {
1140 if (i & 7) bits <<= 1;
1141 else bits = (
unsigned char)*s++;
1142 *t++ = (bits & 128) ?
'1' :
'0';
1144 UNPACK_PUSH(bitstr);
1155 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1156 len = (send - s) * 2;
1159 t = RSTRING_PTR(bitstr);
1160 for (i=0; i<
len; i++) {
1164 bits = (
unsigned char)*s++;
1165 *t++ = hexdigits[bits & 15];
1167 UNPACK_PUSH(bitstr);
1178 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1179 len = (send - s) * 2;
1182 t = RSTRING_PTR(bitstr);
1183 for (i=0; i<
len; i++) {
1187 bits = (
unsigned char)*s++;
1188 *t++ = hexdigits[(bits >> 4) & 15];
1190 UNPACK_PUSH(bitstr);
1197 bigendian_p = BIGENDIAN_P();
1198 goto unpack_integer;
1203 bigendian_p = BIGENDIAN_P();
1204 goto unpack_integer;
1208 integer_size = NATINT_LEN(
short, 2);
1209 bigendian_p = BIGENDIAN_P();
1210 goto unpack_integer;
1214 integer_size = NATINT_LEN(
short, 2);
1215 bigendian_p = BIGENDIAN_P();
1216 goto unpack_integer;
1220 integer_size = (int)
sizeof(
int);
1221 bigendian_p = BIGENDIAN_P();
1222 goto unpack_integer;
1226 integer_size = (int)
sizeof(
int);
1227 bigendian_p = BIGENDIAN_P();
1228 goto unpack_integer;
1232 integer_size = NATINT_LEN(
long, 4);
1233 bigendian_p = BIGENDIAN_P();
1234 goto unpack_integer;
1238 integer_size = NATINT_LEN(
long, 4);
1239 bigendian_p = BIGENDIAN_P();
1240 goto unpack_integer;
1244 integer_size = NATINT_LEN_Q;
1245 bigendian_p = BIGENDIAN_P();
1246 goto unpack_integer;
1250 integer_size = NATINT_LEN_Q;
1251 bigendian_p = BIGENDIAN_P();
1252 goto unpack_integer;
1256 integer_size =
sizeof(intptr_t);
1257 bigendian_p = BIGENDIAN_P();
1258 goto unpack_integer;
1262 integer_size =
sizeof(uintptr_t);
1263 bigendian_p = BIGENDIAN_P();
1264 goto unpack_integer;
1270 goto unpack_integer;
1276 goto unpack_integer;
1282 goto unpack_integer;
1288 goto unpack_integer;
1291 if (explicit_endian) {
1292 bigendian_p = explicit_endian ==
'>';
1294 PACK_LENGTH_ADJUST_SIZE(integer_size);
1300 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1309 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1312 UNPACK_FETCH(&tmp,
float);
1313 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1319 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1321 FLOAT_CONVWITH(tmp);
1322 UNPACK_FETCH(tmp.buf,
float);
1330 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1332 DOUBLE_CONVWITH(tmp);
1333 UNPACK_FETCH(tmp.buf,
double);
1342 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1345 UNPACK_FETCH(&tmp,
double);
1352 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1354 FLOAT_CONVWITH(tmp);
1355 UNPACK_FETCH(tmp.buf,
float);
1363 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1365 DOUBLE_CONVWITH(tmp);
1366 UNPACK_FETCH(tmp.buf,
double);
1374 if (
len > send - s)
len = send - s;
1375 while (
len > 0 && s < send) {
1376 long alen = send - s;
1379 l = utf8_to_uv(s, &alen);
1388 char *ptr = RSTRING_PTR(buf);
1391 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1395 len = ((
unsigned char)*s++ -
' ') & 077;
1398 if (total > RSTRING_LEN(buf)) {
1399 len -= total - RSTRING_LEN(buf);
1400 total = RSTRING_LEN(buf);
1404 long mlen =
len > 3 ? 3 :
len;
1406 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1407 a = ((
unsigned char)*s++ -
' ') & 077;
1410 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1411 b = ((
unsigned char)*s++ -
' ') & 077;
1414 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1415 c = ((
unsigned char)*s++ -
' ') & 077;
1418 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1419 d = ((
unsigned char)*s++ -
' ') & 077;
1422 hunk[0] = (char)(a << 2 | b >> 4);
1423 hunk[1] = (char)(b << 4 | c >> 2);
1424 hunk[2] = (char)(c << 6 | d);
1425 memcpy(ptr, hunk, mlen);
1429 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1431 if (s < send && *s ==
'\r') s++;
1432 if (s < send && *s ==
'\n') s++;
1443 char *ptr = RSTRING_PTR(buf);
1444 int a = -1,b = -1,c = 0,d = 0;
1445 static signed char b64_xtable[256];
1447 if (b64_xtable[
'/'] <= 0) {
1450 for (i = 0; i < 256; i++) {
1453 for (i = 0; i < 64; i++) {
1454 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1460 a = b64_xtable[(
unsigned char)*s++];
1461 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1462 b = b64_xtable[(
unsigned char)*s++];
1463 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1465 if (s + 2 == send && *(s + 1) ==
'=')
break;
1466 rb_raise(rb_eArgError,
"invalid base64");
1468 c = b64_xtable[(
unsigned char)*s++];
1469 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1470 if (s + 1 == send && *s ==
'=')
break;
1471 d = b64_xtable[(
unsigned char)*s++];
1472 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1473 *ptr++ = castchar(a << 2 | b >> 4);
1474 *ptr++ = castchar(b << 4 | c >> 2);
1475 *ptr++ = castchar(c << 6 | d);
1478 *ptr++ = castchar(a << 2 | b >> 4);
1479 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1482 *ptr++ = castchar(a << 2 | b >> 4);
1483 *ptr++ = castchar(b << 4 | c >> 2);
1484 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1490 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1491 if (s >= send)
break;
1493 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1494 if (s >= send)
break;
1496 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1497 if (*s ==
'=' || s >= send)
break;
1499 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1500 if (*s ==
'=' || s >= send)
break;
1502 *ptr++ = castchar(a << 2 | b >> 4);
1503 *ptr++ = castchar(b << 4 | c >> 2);
1504 *ptr++ = castchar(c << 6 | d);
1507 if (a != -1 && b != -1) {
1509 *ptr++ = castchar(a << 2 | b >> 4);
1511 *ptr++ = castchar(a << 2 | b >> 4);
1512 *ptr++ = castchar(b << 4 | c >> 2);
1524 char *ptr = RSTRING_PTR(buf), *ss = s;
1530 if (++s == send)
break;
1531 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1534 if ((c1 = hex2num(*s)) == -1)
break;
1535 if (++s == send)
break;
1536 if ((c2 = hex2num(*s)) == -1)
break;
1537 csum |= *ptr++ = castchar(c1 << 4 | c2);
1541 csum |= *ptr++ = *s;
1555 if (
len > RSTRING_LEN(str))
1556 rb_raise(rb_eArgError,
"@ outside of string");
1557 s = RSTRING_PTR(str) +
len;
1561 if (
len > s - RSTRING_PTR(str))
1562 rb_raise(rb_eArgError,
"X outside of string");
1568 rb_raise(rb_eArgError,
"x outside of string");
1573 UNPACK_PUSH(
SSIZET2NUM(s - RSTRING_PTR(str)));
1577 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1581 UNPACK_FETCH(&t,
char *);
1583 if (!associates) associates = str_associated(str);
1584 tmp = associated_pointer(associates, t);
1585 if (
len < RSTRING_LEN(tmp)) {
1587 str_associate(tmp, associates);
1595 if (
len > (
long)((send - s) /
sizeof(
char *)))
1596 len = (send - s) /
sizeof(
char *);
1598 if ((
size_t)(send - s) <
sizeof(
char *))
1604 UNPACK_FETCH(&t,
char *);
1606 if (!associates) associates = str_associated(str);
1607 tmp = associated_pointer(associates, t);
1623 while (
len > 0 && s < send) {
1629 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1636 if (s0 != s &&
len > 0) {
1650 while (
len > 0 && s < send) {
1665 unknown_directive(
"unpack",
type, fmt);
1677 return pack_unpack_internal(str, fmt, mode,
RB_NUM2LONG(offset));
1683 return pack_unpack_internal(str, fmt, UNPACK_1,
RB_NUM2LONG(offset));
1694 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1695 buf[1] = castchar((uv&0x3f)|0x80);
1699 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1700 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1701 buf[2] = castchar((uv&0x3f)|0x80);
1704 if (uv <= 0x1fffff) {
1705 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1706 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1707 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1708 buf[3] = castchar((uv&0x3f)|0x80);
1711 if (uv <= 0x3ffffff) {
1712 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1713 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1714 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1715 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1716 buf[4] = castchar((uv&0x3f)|0x80);
1719 if (uv <= 0x7fffffff) {
1720 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1721 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1722 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1723 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1724 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1725 buf[5] = castchar((uv&0x3f)|0x80);
1733static const unsigned long utf8_limits[] = {
1744utf8_to_uv(
const char *p,
long *lenp)
1746 int c = *p++ & 0xff;
1747 unsigned long uv = c;
1756 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1759 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1760 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1761 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1762 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1763 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1766 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1769 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1776 if ((c & 0xc0) != 0x80) {
1778 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1787 if (uv < utf8_limits[n]) {
1788 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1793#include "pack.rbinc"
1798 id_associated = rb_make_internal_id();
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.
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
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.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
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.