12#include "ruby/internal/config.h"
20#include "internal/array.h"
21#include "internal/bits.h"
22#include "internal/string.h"
23#include "internal/symbol.h"
24#include "internal/variable.h"
37#ifdef HAVE_TRUE_LONG_LONG
38static const char natstr[] =
"sSiIlLqQjJ";
41static const char natstr[] =
"sSiIlLjJ";
42static const char endstr[] =
"sSiIlLqQjJ";
45#ifdef HAVE_TRUE_LONG_LONG
47# define NATINT_LEN_Q NATINT_LEN(long long, 8)
49# define NATINT_LEN_Q 8
52#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
63 static int endian_value;
66 if (init)
return endian_value;
69 return endian_value = p[0]?0:1;
71# define BIGENDIAN_P() (is_bigendian())
72#elif defined(WORDS_BIGENDIAN)
73# define BIGENDIAN_P() 1
75# define BIGENDIAN_P() 0
79# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
81# define NATINT_LEN(type,len) ((int)sizeof(type))
94#define swapf(x) swap32(x)
95#define swapd(x) swap64(x)
97#define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
98#define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
99#define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
100#define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
101#define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
102#define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
103#define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
104#define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
106#define FLOAT_CONVWITH(x) FLOAT_SWAPPER x;
107#define HTONF(x) ((x).u = rb_htonf((x).u))
108#define HTOVF(x) ((x).u = rb_htovf((x).u))
109#define NTOHF(x) ((x).u = rb_ntohf((x).u))
110#define VTOHF(x) ((x).u = rb_vtohf((x).u))
112#define DOUBLE_CONVWITH(x) DOUBLE_SWAPPER x;
113#define HTOND(x) ((x).u = rb_htond((x).u))
114#define HTOVD(x) ((x).u = rb_htovd((x).u))
115#define NTOHD(x) ((x).u = rb_ntohd((x).u))
116#define VTOHD(x) ((x).u = rb_vtohd((x).u))
118#define MAX_INTEGER_PACK_SIZE 8
120static const char toofew[] =
"too few arguments";
121static const char intoitself[] =
"cannot pack buffer object into itself";
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++) {
153 if (RB_TYPE_P(tmp,
T_STRING) && RSTRING_PTR(tmp) == t)
return tmp;
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, pe, t, n, e) pack_modifiers(p, pe, t, e)
219pack_modifiers(
const char *p,
const char *pend,
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++;
254pack_pack(rb_execution_context_t *ec,
VALUE ary,
VALUE fmt,
VALUE buffer)
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;
290 if (RSTRING_END(fmt) != pend) {
293 const char type = *p++;
298 if (skip_blank(p,
type))
continue;
299 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
304 : strchr(
"PMm",
type) ? 1
322 if (enc_info == 1) enc_info = 2;
324 case 'm':
case 'M':
case 'u':
333 case 'A':
case 'a':
case 'Z':
343 ptr = RSTRING_PTR(from);
344 plen = RSTRING_LEN(from);
358 if (star &&
type ==
'Z')
364 str_expand_fill(res, (
type ==
'A' ?
' ' :
'\0'),
len - plen);
368#define castchar(from) (char)((from) & 0xff)
376 j = (
len - plen + 1)/2;
379 for (i=0; i++ <
len;
ptr++) {
385 char c = castchar(
byte);
392 byte >>= 7 - (
len & 7);
407 j = (
len - plen + 1)/2;
410 for (i=0; i++ <
len;
ptr++) {
415 char c = castchar(
byte);
422 byte <<= 7 - (
len & 7);
437 j = (
len + 1) / 2 - (plen + 1) / 2;
440 for (i=0; i++ <
len;
ptr++) {
442 byte |= (((*
ptr & 15) + 9) & 15) << 4;
444 byte |= (*
ptr & 15) << 4;
448 char c = castchar(
byte);
454 char c = castchar(
byte);
468 j = (
len + 1) / 2 - (plen + 1) / 2;
471 for (i=0; i++ <
len;
ptr++) {
473 byte |= ((*
ptr & 15) + 9) & 15;
479 char c = castchar(
byte);
485 char c = castchar(
byte);
498 bigendian_p = BIGENDIAN_P();
503 integer_size = NATINT_LEN(
short, 2);
504 bigendian_p = BIGENDIAN_P();
509 integer_size = (int)
sizeof(
int);
510 bigendian_p = BIGENDIAN_P();
515 integer_size = NATINT_LEN(
long, 4);
516 bigendian_p = BIGENDIAN_P();
521 integer_size = NATINT_LEN_Q;
522 bigendian_p = BIGENDIAN_P();
526 integer_size =
sizeof(intptr_t);
527 bigendian_p = BIGENDIAN_P();
531 integer_size =
sizeof(uintptr_t);
532 bigendian_p = BIGENDIAN_P();
556 if (explicit_endian) {
557 bigendian_p = explicit_endian ==
'>';
559 if (integer_size > MAX_INTEGER_PACK_SIZE)
560 rb_bug(
"unexpected integer size for pack: %d", integer_size);
562 char intbuf[MAX_INTEGER_PACK_SIZE];
565 rb_integer_pack(from, intbuf, integer_size, 1, 0,
578 f = VALUE_to_float(from);
588 tmp.f = VALUE_to_float(from);
596 DOUBLE_CONVWITH(tmp);
619 tmp.f = VALUE_to_float(from);
627 DOUBLE_CONVWITH(tmp);
639 str_expand_fill(res,
'\0',
len);
644 plen = RSTRING_LEN(res);
646 rb_raise(rb_eArgError,
"X outside of string");
651 len -= RSTRING_LEN(res);
652 if (
len > 0)
goto grow;
654 if (
len > 0)
goto shrink;
658 rb_raise(rb_eArgError,
"%% is not supported");
682 ptr = RSTRING_PTR(from);
683 plen = RSTRING_LEN(from);
686 encodes(res,
ptr, plen,
type, 0);
692 else if (
len > 63 &&
type ==
'u')
703 encodes(res,
ptr, todo,
type, 1);
714 qpencode(res, from,
len);
721 if (RSTRING_LEN(from) <
len) {
722 rb_raise(rb_eArgError,
"too short buffer for P(%ld for %ld)",
723 RSTRING_LEN(from),
len);
734 t = RSTRING_PTR(from);
753 numbytes = rb_absint_numwords(from, 7, NULL);
761 rb_raise(rb_eArgError,
"can't compress negative numbers");
763 rb_bug(
"buffer size problem?");
765 cp = RSTRING_PTR(buf);
766 while (1 < numbytes) {
777 unknown_directive(
"pack",
type, fmt);
784 str_associate(res, associates);
791 rb_enc_set_index(res, rb_utf8_encindex());
801rb_ec_pack_ary(rb_execution_context_t *ec,
VALUE ary,
VALUE fmt,
VALUE buffer)
803 return pack_pack(ec, ary, fmt, buffer);
806static const char uu_table[] =
807"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
808static const char b64_table[] =
809"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
812encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
814 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
815 char buff[buff_size + 1];
817 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
819 const unsigned char *s = (
const unsigned char *)s0;
822 buff[i++] = (char)
len +
' ';
828 while (
len >= input_unit) {
829 while (
len >= input_unit && buff_size-i >= encoded_unit) {
830 buff[i++] = trans[077 & (*s >> 2)];
831 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
832 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
833 buff[i++] = trans[077 & s[2]];
837 if (buff_size-i < encoded_unit) {
844 buff[i++] = trans[077 & (*s >> 2)];
845 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
846 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
850 buff[i++] = trans[077 & (*s >> 2)];
851 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
855 if (tail_lf) buff[i++] =
'\n';
857 if ((
size_t)i >
sizeof(buff)) rb_bug(
"encodes() buffer overrun");
860static const char hex_table[] =
"0123456789ABCDEF";
866 long i = 0, n = 0, prev = EOF;
867 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
868 unsigned char *send = s + RSTRING_LEN(from);
872 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
875 buff[i++] = hex_table[*s >> 4];
876 buff[i++] = hex_table[*s & 0x0f];
880 else if (*s ==
'\n') {
881 if (prev ==
' ' || prev ==
'\t') {
925#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
927 if (len > (long)((send-s)/(sz))) { \
929 tmp_len = len-(send-s)/(sz); \
931 len = (send-s)/(sz); \
935#define PACK_ITEM_ADJUST() do { \
936 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
937 rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
944#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
945# define AVOID_CC_BUG volatile
957pack_unpack_internal(
VALUE str,
VALUE fmt,
enum unpack_mode mode,
long offset)
959#define hexdigits ruby_hexdigits
964 AVOID_CC_BUG
long tmp_len;
965 int signed_p, integer_size, bigendian_p;
970#define UNPACK_PUSH(item) do {\
971 VALUE item_val = (item);\
972 if ((mode) == UNPACK_BLOCK) {\
975 if (RSTRING_PTR(str) != sptr || RSTRING_LEN(str) != slen) {\
976 rb_raise(rb_eRuntimeError, "string modified");\
979 if (RSTRING_PTR(fmt) != fptr || RSTRING_LEN(fmt) != flen) {\
980 rb_raise(rb_eRuntimeError, "format string modified");\
983 else if ((mode) == UNPACK_ARRAY) {\
984 rb_ary_push(ary, item_val);\
995 if (offset < 0) rb_raise(rb_eArgError,
"offset can't be negative");
996 len = RSTRING_LEN(str);
997 if (offset >
len) rb_raise(rb_eArgError,
"offset outside of string");
999 s = RSTRING_PTR(str);
1005 p = RSTRING_PTR(fmt);
1006 pend = p + RSTRING_LEN(fmt);
1008 flen = RSTRING_LEN(fmt);
1010#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1014 int explicit_endian = 0;
1015 const char type = *p++;
1021 if (skip_blank(p,
type))
continue;
1022 p = pack_modifiers(p, pend,
type, &natint, &explicit_endian);
1026 else if (*p ==
'*') {
1044 rb_raise(rb_eArgError,
"%% is not supported");
1048 if (
len > send - s)
len = send - s;
1051 char *t = s +
len - 1;
1054 if (*t !=
' ' && *t !=
'\0')
break;
1066 if (
len > send-s)
len = send-s;
1067 while (t < s+
len && *t) t++;
1070 s = star ? t : s+
len;
1075 if (
len > send - s)
len = send - s;
1087 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1088 len = (send - s) * 8;
1091 t = RSTRING_PTR(bitstr);
1092 for (i=0; i<
len; i++) {
1093 if (i & 7) bits >>= 1;
1094 else bits = (
unsigned char)*s++;
1095 *t++ = (bits & 1) ?
'1' :
'0';
1097 UNPACK_PUSH(bitstr);
1108 if (p[-1] ==
'*' ||
len > (send - s) * 8)
1109 len = (send - s) * 8;
1112 t = RSTRING_PTR(bitstr);
1113 for (i=0; i<
len; i++) {
1114 if (i & 7) bits <<= 1;
1115 else bits = (
unsigned char)*s++;
1116 *t++ = (bits & 128) ?
'1' :
'0';
1118 UNPACK_PUSH(bitstr);
1129 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1130 len = (send - s) * 2;
1133 t = RSTRING_PTR(bitstr);
1134 for (i=0; i<
len; i++) {
1138 bits = (
unsigned char)*s++;
1139 *t++ = hexdigits[bits & 15];
1141 UNPACK_PUSH(bitstr);
1152 if (p[-1] ==
'*' ||
len > (send - s) * 2)
1153 len = (send - s) * 2;
1156 t = RSTRING_PTR(bitstr);
1157 for (i=0; i<
len; i++) {
1161 bits = (
unsigned char)*s++;
1162 *t++ = hexdigits[(bits >> 4) & 15];
1164 UNPACK_PUSH(bitstr);
1171 bigendian_p = BIGENDIAN_P();
1172 goto unpack_integer;
1177 bigendian_p = BIGENDIAN_P();
1178 goto unpack_integer;
1182 integer_size = NATINT_LEN(
short, 2);
1183 bigendian_p = BIGENDIAN_P();
1184 goto unpack_integer;
1188 integer_size = NATINT_LEN(
short, 2);
1189 bigendian_p = BIGENDIAN_P();
1190 goto unpack_integer;
1194 integer_size = (int)
sizeof(
int);
1195 bigendian_p = BIGENDIAN_P();
1196 goto unpack_integer;
1200 integer_size = (int)
sizeof(
int);
1201 bigendian_p = BIGENDIAN_P();
1202 goto unpack_integer;
1206 integer_size = NATINT_LEN(
long, 4);
1207 bigendian_p = BIGENDIAN_P();
1208 goto unpack_integer;
1212 integer_size = NATINT_LEN(
long, 4);
1213 bigendian_p = BIGENDIAN_P();
1214 goto unpack_integer;
1218 integer_size = NATINT_LEN_Q;
1219 bigendian_p = BIGENDIAN_P();
1220 goto unpack_integer;
1224 integer_size = NATINT_LEN_Q;
1225 bigendian_p = BIGENDIAN_P();
1226 goto unpack_integer;
1230 integer_size =
sizeof(intptr_t);
1231 bigendian_p = BIGENDIAN_P();
1232 goto unpack_integer;
1236 integer_size =
sizeof(uintptr_t);
1237 bigendian_p = BIGENDIAN_P();
1238 goto unpack_integer;
1244 goto unpack_integer;
1250 goto unpack_integer;
1256 goto unpack_integer;
1262 goto unpack_integer;
1265 if (explicit_endian) {
1266 bigendian_p = explicit_endian ==
'>';
1268 PACK_LENGTH_ADJUST_SIZE(integer_size);
1274 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1283 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1286 UNPACK_FETCH(&tmp,
float);
1287 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1293 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1295 FLOAT_CONVWITH(tmp);
1296 UNPACK_FETCH(tmp.buf,
float);
1304 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1306 DOUBLE_CONVWITH(tmp);
1307 UNPACK_FETCH(tmp.buf,
double);
1316 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1319 UNPACK_FETCH(&tmp,
double);
1326 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1328 FLOAT_CONVWITH(tmp);
1329 UNPACK_FETCH(tmp.buf,
float);
1337 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1339 DOUBLE_CONVWITH(tmp);
1340 UNPACK_FETCH(tmp.buf,
double);
1348 if (
len > send - s)
len = send - s;
1349 while (
len > 0 && s < send) {
1350 long alen = send - s;
1353 l = utf8_to_uv(s, &alen);
1362 char *
ptr = RSTRING_PTR(buf);
1365 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1369 len = ((
unsigned char)*s++ -
' ') & 077;
1372 if (total > RSTRING_LEN(buf)) {
1373 len -= total - RSTRING_LEN(buf);
1374 total = RSTRING_LEN(buf);
1378 long mlen =
len > 3 ? 3 :
len;
1380 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1381 a = ((
unsigned char)*s++ -
' ') & 077;
1384 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1385 b = ((
unsigned char)*s++ -
' ') & 077;
1388 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1389 c = ((
unsigned char)*s++ -
' ') & 077;
1392 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1393 d = ((
unsigned char)*s++ -
' ') & 077;
1396 hunk[0] = (char)(a << 2 | b >> 4);
1397 hunk[1] = (char)(b << 4 | c >> 2);
1398 hunk[2] = (char)(c << 6 | d);
1399 memcpy(
ptr, hunk, mlen);
1403 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1405 if (s < send && *s ==
'\r') s++;
1406 if (s < send && *s ==
'\n') s++;
1417 char *
ptr = RSTRING_PTR(buf);
1418 int a = -1,b = -1,c = 0,d = 0;
1419 static signed char b64_xtable[256];
1421 if (b64_xtable[
'/'] <= 0) {
1424 for (i = 0; i < 256; i++) {
1427 for (i = 0; i < 64; i++) {
1428 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1434 a = b64_xtable[(
unsigned char)*s++];
1435 if (s >= send || a == -1) rb_raise(rb_eArgError,
"invalid base64");
1436 b = b64_xtable[(
unsigned char)*s++];
1437 if (s >= send || b == -1) rb_raise(rb_eArgError,
"invalid base64");
1439 if (s + 2 == send && *(s + 1) ==
'=')
break;
1440 rb_raise(rb_eArgError,
"invalid base64");
1442 c = b64_xtable[(
unsigned char)*s++];
1443 if (s >= send || c == -1) rb_raise(rb_eArgError,
"invalid base64");
1444 if (s + 1 == send && *s ==
'=')
break;
1445 d = b64_xtable[(
unsigned char)*s++];
1446 if (d == -1) rb_raise(rb_eArgError,
"invalid base64");
1447 *
ptr++ = castchar(a << 2 | b >> 4);
1448 *
ptr++ = castchar(b << 4 | c >> 2);
1449 *
ptr++ = castchar(c << 6 | d);
1452 *
ptr++ = castchar(a << 2 | b >> 4);
1453 if (b & 0xf) rb_raise(rb_eArgError,
"invalid base64");
1456 *
ptr++ = castchar(a << 2 | b >> 4);
1457 *
ptr++ = castchar(b << 4 | c >> 2);
1458 if (c & 0x3) rb_raise(rb_eArgError,
"invalid base64");
1464 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1465 if (s >= send)
break;
1467 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1468 if (s >= send)
break;
1470 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1471 if (*s ==
'=' || s >= send)
break;
1473 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1474 if (*s ==
'=' || s >= send)
break;
1476 *
ptr++ = castchar(a << 2 | b >> 4);
1477 *
ptr++ = castchar(b << 4 | c >> 2);
1478 *
ptr++ = castchar(c << 6 | d);
1481 if (a != -1 && b != -1) {
1483 *
ptr++ = castchar(a << 2 | b >> 4);
1485 *
ptr++ = castchar(a << 2 | b >> 4);
1486 *
ptr++ = castchar(b << 4 | c >> 2);
1498 char *
ptr = RSTRING_PTR(buf), *ss = s;
1504 if (++s == send)
break;
1505 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1508 if ((c1 = hex2num(*s)) == -1)
break;
1509 if (++s == send)
break;
1510 if ((c2 = hex2num(*s)) == -1)
break;
1511 csum |= *
ptr++ = castchar(c1 << 4 | c2);
1515 csum |= *
ptr++ = *s;
1529 if (
len > RSTRING_LEN(str))
1530 rb_raise(rb_eArgError,
"@ outside of string");
1531 s = RSTRING_PTR(str) +
len;
1535 if (
len > s - RSTRING_PTR(str))
1536 rb_raise(rb_eArgError,
"X outside of string");
1542 rb_raise(rb_eArgError,
"x outside of string");
1547 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1551 UNPACK_FETCH(&t,
char *);
1553 if (!associates) associates = str_associated(str);
1554 tmp = associated_pointer(associates, t);
1555 if (
len < RSTRING_LEN(tmp)) {
1557 str_associate(tmp, associates);
1565 if (
len > (
long)((send - s) /
sizeof(
char *)))
1566 len = (send - s) /
sizeof(
char *);
1568 if ((
size_t)(send - s) <
sizeof(
char *))
1574 UNPACK_FETCH(&t,
char *);
1576 if (!associates) associates = str_associated(str);
1577 tmp = associated_pointer(associates, t);
1587 while (
len > 0 && s < send) {
1602 unknown_directive(
"unpack",
type, fmt);
1611pack_unpack(rb_execution_context_t *ec,
VALUE str,
VALUE fmt,
VALUE offset)
1614 return pack_unpack_internal(str, fmt, mode,
RB_NUM2LONG(offset));
1618pack_unpack1(rb_execution_context_t *ec,
VALUE str,
VALUE fmt,
VALUE offset)
1620 return pack_unpack_internal(str, fmt, UNPACK_1,
RB_NUM2LONG(offset));
1631 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1632 buf[1] = castchar((uv&0x3f)|0x80);
1636 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1637 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1638 buf[2] = castchar((uv&0x3f)|0x80);
1641 if (uv <= 0x1fffff) {
1642 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1643 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1644 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1645 buf[3] = castchar((uv&0x3f)|0x80);
1648 if (uv <= 0x3ffffff) {
1649 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1650 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1651 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1652 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1653 buf[4] = castchar((uv&0x3f)|0x80);
1656 if (uv <= 0x7fffffff) {
1657 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1658 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1659 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1660 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1661 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1662 buf[5] = castchar((uv&0x3f)|0x80);
1670static const unsigned long utf8_limits[] = {
1681utf8_to_uv(
const char *p,
long *lenp)
1683 int c = *p++ & 0xff;
1684 unsigned long uv = c;
1693 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1696 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1697 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1698 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1699 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1700 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1703 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1706 rb_raise(rb_eArgError,
"malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1713 if ((c & 0xc0) != 0x80) {
1715 rb_raise(rb_eArgError,
"malformed UTF-8 character");
1724 if (uv < utf8_limits[n]) {
1725 rb_raise(rb_eArgError,
"redundant UTF-8 sequence");
1730#include "pack.rbinc"
1735 id_associated = rb_make_internal_id();
#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 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 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.
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.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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_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.
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.