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";
122static const char intoitself[] =
"cannot pack buffer object into itself";
124static void encodes(
VALUE,
const char*,
long,
int,
int);
127static unsigned long utf8_to_uv(
const char*,
long*);
129static ID id_associated;
139str_associated(
VALUE str)
141 VALUE associates = rb_ivar_lookup(str, id_associated,
Qfalse);
143 rb_raise(rb_eArgError,
"no associated pointer");
148associated_pointer(
VALUE associates,
const char *t)
152 for (; p < pend; p++) {
156 rb_raise(rb_eArgError,
"non associated pointer");
162unknown_directive(const
char *mode,
char type,
VALUE fmt)
171 snprintf(unknown,
sizeof(unknown),
"\\x%.2x",
type & 0xff);
173 fmt = rb_str_quote_unprintable(fmt);
174 rb_raise(rb_eArgError,
"unknown %s directive '%s' in '%"PRIsVALUE
"'",
179VALUE_to_float(
VALUE obj)
187 else if (d < -FLT_MAX) {
190 else if (d <= FLT_MAX) {
199str_expand_fill(
VALUE res,
int c,
long len)
201 long olen = RSTRING_LEN(res);
202 memset(RSTRING_PTR(res) + olen, c,
len);
207skip_to_eol(
const char *p,
const char *pend)
209 p = memchr(p,
'\n', pend - p);
210 return (
char *)(p ? p + 1 : pend);
213#define skip_blank(p, type) \
214 (ISSPACE(type) || (type == '#' && (p = skip_to_eol(p, pend), 1)))
217# define pack_modifiers(p, t, n, e) pack_modifiers(p, t, e)
220pack_modifiers(
const char *p,
char type,
int *natint,
int *explicit_endian)
226 if (strchr(natstr,
type)) {
233 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, natstr);
239 if (!strchr(endstr,
type)) {
240 rb_raise(rb_eArgError,
"'%c' allowed only after types %s", *p, endstr);
242 if (*explicit_endian) {
245 *explicit_endian = *p++;
256 const char *p, *pend;
257 VALUE res, from, associates = 0;
261 int integer_size, bigendian_p;
265 p = RSTRING_PTR(fmt);
266 pend = p + RSTRING_LEN(fmt);
274 rb_str_modify(buffer);
280#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
281#define MORE_ITEM (idx < RARRAY_LEN(ary))
282#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
283#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
284#define NOT_BUFFER(val) (((val) == res) ? rb_raise(rb_eArgError, intoitself) : (void)0)
285#define STR_FROM(val) NOT_BUFFER(StringValue(val))
288 int explicit_endian = 0;
292 const char type = *p++;
297 if (skip_blank(p,
type))
continue;
298 p = pack_modifiers(p,
type, &natint, &explicit_endian);
302 : strchr(
"PMm",
type) ? 1
320 if (enc_info == 1) enc_info = 2;
322 case 'm':
case 'M':
case 'u':
331 case 'A':
case 'a':
case 'Z':
341 ptr = RSTRING_PTR(from);
342 plen = RSTRING_LEN(from);
354 if (p[-1] ==
'*' &&
type ==
'Z')
360 str_expand_fill(res, (
type ==
'A' ?
' ' :
'\0'),
len - plen);
364#define castchar(from) (char)((from) & 0xff)
372 j = (
len - plen + 1)/2;
375 for (i=0; i++ <
len; ptr++) {
381 char c = castchar(
byte);
388 byte >>= 7 - (
len & 7);
403 j = (
len - plen + 1)/2;
406 for (i=0; i++ <
len; ptr++) {
411 char c = castchar(
byte);
418 byte <<= 7 - (
len & 7);
433 j = (
len + 1) / 2 - (plen + 1) / 2;
436 for (i=0; i++ <
len; ptr++) {
438 byte |= (((*ptr & 15) + 9) & 15) << 4;
440 byte |= (*ptr & 15) << 4;
444 char c = castchar(
byte);
450 char c = castchar(
byte);
464 j = (
len + 1) / 2 - (plen + 1) / 2;
467 for (i=0; i++ <
len; ptr++) {
469 byte |= ((*ptr & 15) + 9) & 15;
475 char c = castchar(
byte);
481 char c = castchar(
byte);
494 bigendian_p = BIGENDIAN_P();
499 integer_size = NATINT_LEN(
short, 2);
500 bigendian_p = BIGENDIAN_P();
505 integer_size = (int)
sizeof(
int);
506 bigendian_p = BIGENDIAN_P();
511 integer_size = NATINT_LEN(
long, 4);
512 bigendian_p = BIGENDIAN_P();
517 integer_size = NATINT_LEN_Q;
518 bigendian_p = BIGENDIAN_P();
522 integer_size =
sizeof(intptr_t);
523 bigendian_p = BIGENDIAN_P();
527 integer_size =
sizeof(uintptr_t);
528 bigendian_p = BIGENDIAN_P();
552 if (explicit_endian) {
553 bigendian_p = explicit_endian ==
'>';
555 if (integer_size > MAX_INTEGER_PACK_SIZE)
556 rb_bug(
"unexpected integer size for pack: %d", integer_size);
558 char intbuf[MAX_INTEGER_PACK_SIZE];
561 rb_integer_pack(from, intbuf, integer_size, 1, 0,
574 f = VALUE_to_float(from);
584 tmp.f = VALUE_to_float(from);
592 DOUBLE_CONVWITH(tmp);
615 tmp.f = VALUE_to_float(from);
623 DOUBLE_CONVWITH(tmp);
635 str_expand_fill(res,
'\0',
len);
640 plen = RSTRING_LEN(res);
642 rb_raise(rb_eArgError,
"X outside of string");
647 len -= RSTRING_LEN(res);
648 if (
len > 0)
goto grow;
650 if (
len > 0)
goto shrink;
654 rb_raise(rb_eArgError,
"%% is not supported");
684 size_t numbytes, nlz_bits;
687 const long start = RSTRING_LEN(res);
691 if (
type ==
'R' && rb_int_negative_p(from)) {
692 rb_raise(rb_eArgError,
"can't encode negative numbers in ULEB128");
695 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
699 else if (nlz_bits == 0 &&
type ==
'r') {
705 cp = RSTRING_PTR(res) + start;
706 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
710 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
714 while (1 < numbytes) {
726 ptr = RSTRING_PTR(from);
727 plen = RSTRING_LEN(from);
730 encodes(res, ptr, plen,
type, 0);
736 else if (
len > 63 &&
type ==
'u')
747 encodes(res, ptr, todo,
type, 1);
758 qpencode(res, from,
len);
765 if (RSTRING_LEN(from) <
len) {
766 rb_raise(rb_eArgError,
"too short buffer for P(%ld for %ld)",
767 RSTRING_LEN(from),
len);
778 t = RSTRING_PTR(from);
797 numbytes = rb_absint_numwords(from, 7, NULL);
805 rb_raise(rb_eArgError,
"can't compress negative numbers");
807 rb_bug(
"buffer size problem?");
809 cp = RSTRING_PTR(buf);
810 while (1 < numbytes) {
821 unknown_directive(
"pack",
type, fmt);
828 str_associate(res, associates);
835 rb_enc_set_index(res, rb_utf8_encindex());
847 return pack_pack(ec, ary, fmt, buffer);
850static const char uu_table[] =
851"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
852static const char b64_table[] =
853"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
856encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
858 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
859 char buff[buff_size + 1];
861 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
863 const unsigned char *s = (
const unsigned char *)s0;
866 buff[i++] = (char)
len +
' ';
872 while (
len >= input_unit) {
873 while (
len >= input_unit && buff_size-i >= encoded_unit) {
874 buff[i++] = trans[077 & (*s >> 2)];
875 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
876 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
877 buff[i++] = trans[077 & s[2]];
881 if (buff_size-i < encoded_unit) {
888 buff[i++] = trans[077 & (*s >> 2)];
889 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
890 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
894 buff[i++] = trans[077 & (*s >> 2)];
895 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
899 if (tail_lf) buff[i++] =
'\n';
901 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
904static const char hex_table[] =
"0123456789ABCDEF";
910 long i = 0, n = 0, prev = EOF;
911 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
912 unsigned char *send = s + RSTRING_LEN(from);
916 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
919 buff[i++] = hex_table[*s >> 4];
920 buff[i++] = hex_table[*s & 0x0f];
924 else if (*s ==
'\n') {
925 if (prev ==
' ' || prev ==
'\t') {
969#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
971 if (len > (long)((send-s)/(sz))) { \
973 tmp_len = len-(send-s)/(sz); \
975 len = (send-s)/(sz); \
979#define PACK_ITEM_ADJUST() do { \
980 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
981 rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
988#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
989# define AVOID_CC_BUG volatile
1001pack_unpack_internal(
VALUE str,
VALUE fmt,
enum unpack_mode mode,
long offset)
1003#define hexdigits ruby_hexdigits
1004 const char *s, *send;
1005 const char *p, *pend;
1008 AVOID_CC_BUG
long tmp_len;
1009 int signed_p, integer_size, bigendian_p;
1010#define UNPACK_PUSH(item) do {\
1011 VALUE item_val = (item);\
1012 if ((mode) == UNPACK_BLOCK) {\
1013 rb_yield(item_val);\
1015 else if ((mode) == UNPACK_ARRAY) {\
1016 rb_ary_push(ary, item_val);\
1027 if (offset < 0) rb_raise(rb_eArgError,
"offset can't be negative");
1028 len = RSTRING_LEN(str);
1029 if (offset >
len) rb_raise(rb_eArgError,
"offset outside of string");
1031 s = RSTRING_PTR(str);
1035 p = RSTRING_PTR(fmt);
1036 pend = p + RSTRING_LEN(fmt);
1038#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1042 int explicit_endian = 0;
1043 const char type = *p++;
1049 if (skip_blank(p,
type))
continue;
1050 p = pack_modifiers(p,
type, &natint, &explicit_endian);
1054 else if (*p ==
'*') {
1072 rb_raise(rb_eArgError,
"%% is not supported");
1076 if (
len > send - s)
len = send - s;
1079 const char *t = s +
len - 1;
1082 if (*t !=
' ' && *t !=
'\0')
break;
1094 if (
len > send-s)
len = send-s;
1095 while (t < s+
len && *t) t++;
1098 s = star ? t : s+
len;
1103 if (
len > send - s)
len = send - s;
1115 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1116 len = (send - s) * 8;
1119 t = RSTRING_PTR(bitstr);
1120 for (i=0; i<
len; i++) {
1121 if (i & 7) bits >>= 1;
1122 else bits = (
unsigned char)*s++;
1123 *t++ = (bits & 1) ?
'1' :
'0';
1125 UNPACK_PUSH(bitstr);
1136 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1137 len = (send - s) * 8;
1140 t = RSTRING_PTR(bitstr);
1141 for (i=0; i<
len; i++) {
1142 if (i & 7) bits <<= 1;
1143 else bits = (
unsigned char)*s++;
1144 *t++ = (bits & 128) ?
'1' :
'0';
1146 UNPACK_PUSH(bitstr);
1157 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1158 len = (send - s) * 2;
1161 t = RSTRING_PTR(bitstr);
1162 for (i=0; i<
len; i++) {
1166 bits = (
unsigned char)*s++;
1167 *t++ = hexdigits[bits & 15];
1169 UNPACK_PUSH(bitstr);
1180 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1181 len = (send - s) * 2;
1184 t = RSTRING_PTR(bitstr);
1185 for (i=0; i<
len; i++) {
1189 bits = (
unsigned char)*s++;
1190 *t++ = hexdigits[(bits >> 4) & 15];
1192 UNPACK_PUSH(bitstr);
1199 bigendian_p = BIGENDIAN_P();
1200 goto unpack_integer;
1205 bigendian_p = BIGENDIAN_P();
1206 goto unpack_integer;
1210 integer_size = NATINT_LEN(
short, 2);
1211 bigendian_p = BIGENDIAN_P();
1212 goto unpack_integer;
1216 integer_size = NATINT_LEN(
short, 2);
1217 bigendian_p = BIGENDIAN_P();
1218 goto unpack_integer;
1222 integer_size = (int)
sizeof(
int);
1223 bigendian_p = BIGENDIAN_P();
1224 goto unpack_integer;
1228 integer_size = (int)
sizeof(
int);
1229 bigendian_p = BIGENDIAN_P();
1230 goto unpack_integer;
1234 integer_size = NATINT_LEN(
long, 4);
1235 bigendian_p = BIGENDIAN_P();
1236 goto unpack_integer;
1240 integer_size = NATINT_LEN(
long, 4);
1241 bigendian_p = BIGENDIAN_P();
1242 goto unpack_integer;
1246 integer_size = NATINT_LEN_Q;
1247 bigendian_p = BIGENDIAN_P();
1248 goto unpack_integer;
1252 integer_size = NATINT_LEN_Q;
1253 bigendian_p = BIGENDIAN_P();
1254 goto unpack_integer;
1258 integer_size =
sizeof(intptr_t);
1259 bigendian_p = BIGENDIAN_P();
1260 goto unpack_integer;
1264 integer_size =
sizeof(uintptr_t);
1265 bigendian_p = BIGENDIAN_P();
1266 goto unpack_integer;
1272 goto unpack_integer;
1278 goto unpack_integer;
1284 goto unpack_integer;
1290 goto unpack_integer;
1293 if (explicit_endian) {
1294 bigendian_p = explicit_endian ==
'>';
1296 PACK_LENGTH_ADJUST_SIZE(integer_size);
1302 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1311 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1314 UNPACK_FETCH(&tmp,
float);
1315 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1321 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1323 FLOAT_CONVWITH(tmp);
1324 UNPACK_FETCH(tmp.buf,
float);
1332 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1334 DOUBLE_CONVWITH(tmp);
1335 UNPACK_FETCH(tmp.buf,
double);
1344 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1347 UNPACK_FETCH(&tmp,
double);
1354 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1356 FLOAT_CONVWITH(tmp);
1357 UNPACK_FETCH(tmp.buf,
float);
1365 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1367 DOUBLE_CONVWITH(tmp);
1368 UNPACK_FETCH(tmp.buf,
double);
1376 if (
len > send - s)
len = send - s;
1377 while (
len > 0 && s < send) {
1378 long alen = send - s;
1381 l = utf8_to_uv(s, &alen);
1390 char *ptr = RSTRING_PTR(buf);
1393 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1397 len = ((
unsigned char)*s++ -
' ') & 077;
1400 if (total > RSTRING_LEN(buf)) {
1401 len -= total - RSTRING_LEN(buf);
1402 total = RSTRING_LEN(buf);
1406 long mlen =
len > 3 ? 3 :
len;
1408 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1409 a = ((
unsigned char)*s++ -
' ') & 077;
1412 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1413 b = ((
unsigned char)*s++ -
' ') & 077;
1416 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1417 c = ((
unsigned char)*s++ -
' ') & 077;
1420 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1421 d = ((
unsigned char)*s++ -
' ') & 077;
1424 hunk[0] = (char)(a << 2 | b >> 4);
1425 hunk[1] = (char)(b << 4 | c >> 2);
1426 hunk[2] = (char)(c << 6 | d);
1427 memcpy(ptr, hunk, mlen);
1431 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1433 if (s < send && *s ==
'\r') s++;
1434 if (s < send && *s ==
'\n') s++;
1445 char *ptr = RSTRING_PTR(buf);
1446 int a = -1,b = -1,c = 0,d = 0;
1447 static signed char b64_xtable[256];
1449 if (b64_xtable[
'/'] <= 0) {
1452 for (i = 0; i < 256; i++) {
1455 for (i = 0; i < 64; i++) {
1456 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1462 a = b64_xtable[(
unsigned char)*s++];
1463 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1464 b = b64_xtable[(
unsigned char)*s++];
1465 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1467 if (s + 2 == send && *(s + 1) ==
'=')
break;
1468 rb_raise(rb_eArgError,
"invalid base64");
1470 c = b64_xtable[(
unsigned char)*s++];
1471 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1472 if (s + 1 == send && *s ==
'=')
break;
1473 d = b64_xtable[(
unsigned char)*s++];
1474 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1475 *ptr++ = castchar(a << 2 | b >> 4);
1476 *ptr++ = castchar(b << 4 | c >> 2);
1477 *ptr++ = castchar(c << 6 | d);
1480 *ptr++ = castchar(a << 2 | b >> 4);
1481 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1484 *ptr++ = castchar(a << 2 | b >> 4);
1485 *ptr++ = castchar(b << 4 | c >> 2);
1486 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1492 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1493 if (s >= send)
break;
1495 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1496 if (s >= send)
break;
1498 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1499 if (*s ==
'=' || s >= send)
break;
1501 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1502 if (*s ==
'=' || s >= send)
break;
1504 *ptr++ = castchar(a << 2 | b >> 4);
1505 *ptr++ = castchar(b << 4 | c >> 2);
1506 *ptr++ = castchar(c << 6 | d);
1509 if (a != -1 && b != -1) {
1511 *ptr++ = castchar(a << 2 | b >> 4);
1513 *ptr++ = castchar(a << 2 | b >> 4);
1514 *ptr++ = castchar(b << 4 | c >> 2);
1526 char *ptr = RSTRING_PTR(buf);
1533 if (++s == send)
break;
1534 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1537 if ((c1 = hex2num(*s)) == -1)
break;
1538 if (++s == send)
break;
1539 if ((c2 = hex2num(*s)) == -1)
break;
1540 csum |= *ptr++ = castchar(c1 << 4 | c2);
1544 csum |= *ptr++ = *s;
1558 if (
len > RSTRING_LEN(str))
1559 rb_raise(rb_eArgError,
"@ outside of string");
1560 s = RSTRING_PTR(str) +
len;
1564 if (
len > s - RSTRING_PTR(str))
1565 rb_raise(rb_eArgError,
"X outside of string");
1571 rb_raise(rb_eArgError,
"x outside of string");
1576 UNPACK_PUSH(
SSIZET2NUM(s - RSTRING_PTR(str)));
1580 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1584 UNPACK_FETCH(&t,
char *);
1586 if (!associates) associates = str_associated(str);
1587 tmp = associated_pointer(associates, t);
1588 if (
len < RSTRING_LEN(tmp)) {
1590 str_associate(tmp, associates);
1598 if (
len > (
long)((send - s) /
sizeof(
char *)))
1599 len = (send - s) /
sizeof(
char *);
1601 if ((
size_t)(send - s) <
sizeof(
char *))
1607 UNPACK_FETCH(&t,
char *);
1609 if (!associates) associates = str_associated(str);
1610 tmp = associated_pointer(associates, t);
1626 while (
len > 0 && s < send) {
1632 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1639 if (s0 != s &&
len > 0) {
1653 while (
len > 0 && s < send) {
1668 unknown_directive(
"unpack",
type, fmt);
1680 return pack_unpack_internal(str, fmt, mode,
RB_NUM2LONG(offset));
1686 return pack_unpack_internal(str, fmt, UNPACK_1,
RB_NUM2LONG(offset));
1697 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1698 buf[1] = castchar((uv&0x3f)|0x80);
1702 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1703 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1704 buf[2] = castchar((uv&0x3f)|0x80);
1707 if (uv <= 0x1fffff) {
1708 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1709 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1710 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1711 buf[3] = castchar((uv&0x3f)|0x80);
1714 if (uv <= 0x3ffffff) {
1715 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1716 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1717 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1718 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1719 buf[4] = castchar((uv&0x3f)|0x80);
1722 if (uv <= 0x7fffffff) {
1723 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1724 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1725 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1726 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1727 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1728 buf[5] = castchar((uv&0x3f)|0x80);
1736static const unsigned long utf8_limits[] = {
1747utf8_to_uv(
const char *p,
long *lenp)
1749 int c = *p++ & 0xff;
1750 unsigned long uv = c;
1759 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1762 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1763 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1764 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1765 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1766 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1769 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1772 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1779 if ((c & 0xc0) != 0x80) {
1781 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1790 if (uv < utf8_limits[n]) {
1791 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1796#include "pack.rbinc"
1801 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.
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.