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";
40static const char natstr[] =
"sSiIlLjJ";
42static const char endstr[] =
"sSiIlLqQjJ";
44#ifdef HAVE_TRUE_LONG_LONG
46# define NATINT_LEN_Q NATINT_LEN(long long, 8)
48# define NATINT_LEN_Q 8
51#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
62 static int endian_value;
65 if (init)
return endian_value;
68 return endian_value = p[0]?0:1;
70# define BIGENDIAN_P() (is_bigendian())
71#elif defined(WORDS_BIGENDIAN)
72# define BIGENDIAN_P() 1
74# define BIGENDIAN_P() 0
78# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
80# define NATINT_LEN(type,len) ((int)sizeof(type))
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";
121static void encodes(
VALUE,
const char*,
long,
int,
int);
124static unsigned long utf8_to_uv(
const char*,
long*);
126static ID id_associated;
136str_associated(
VALUE str)
138 VALUE associates = rb_ivar_lookup(str, id_associated,
Qfalse);
145associated_pointer(
VALUE associates,
const char *t)
149 for (; p < pend; p++) {
151 if (RB_TYPE_P(tmp,
T_STRING) && RSTRING_PTR(tmp) == t)
return tmp;
158unknown_directive(
const char *mode,
char type,
VALUE fmt)
167 snprintf(unknown,
sizeof(unknown),
"\\x%.2x",
type & 0xff);
169 fmt = rb_str_quote_unprintable(fmt);
170 rb_warn(
"unknown %s directive '%s' in '%"PRIsVALUE
"'",
175VALUE_to_float(
VALUE obj)
183 else if (d < -FLT_MAX) {
186 else if (d <= FLT_MAX) {
197 static const char nul10[] =
"\0\0\0\0\0\0\0\0\0\0";
198 static const char spc10[] =
" ";
199 const char *p, *pend;
200 VALUE res, from, associates = 0;
208 int integer_size, bigendian_p;
212 p = RSTRING_PTR(fmt);
213 pend = p + RSTRING_LEN(fmt);
227#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
228#define MORE_ITEM (idx < RARRAY_LEN(ary))
229#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
230#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
233 int explicit_endian = 0;
234 if (RSTRING_PTR(fmt) + RSTRING_LEN(fmt) != pend) {
244 while ((p < pend) && (*p !=
'\n')) {
255 if (strchr(natstr,
type)) {
268 if (!strchr(endstr,
type)) {
271 if (explicit_endian) {
274 explicit_endian = *p++;
280 len = strchr(
"@Xxu",
type) ? 0
281 : strchr(
"PMm",
type) ? 1
287 len =
STRTOUL(p, (
char**)&p, 10);
299 if (enc_info == 1) enc_info = 2;
301 case 'm':
case 'M':
case 'u':
310 case 'A':
case 'a':
case 'Z':
320 ptr = RSTRING_PTR(from);
321 plen = RSTRING_LEN(from);
333 if (p[-1] ==
'*' &&
type ==
'Z')
347#define castchar(from) (char)((from) & 0xff)
355 j = (len - plen + 1)/2;
358 for (i=0; i++ < len; ptr++) {
364 char c = castchar(
byte);
371 byte >>= 7 - (len & 7);
386 j = (len - plen + 1)/2;
389 for (i=0; i++ < len; ptr++) {
394 char c = castchar(
byte);
401 byte <<= 7 - (len & 7);
416 j = (len + 1) / 2 - (plen + 1) / 2;
419 for (i=0; i++ < len; ptr++) {
421 byte |= (((*ptr & 15) + 9) & 15) << 4;
423 byte |= (*ptr & 15) << 4;
427 char c = castchar(
byte);
433 char c = castchar(
byte);
447 j = (len + 1) / 2 - (plen + 1) / 2;
450 for (i=0; i++ < len; ptr++) {
452 byte |= ((*ptr & 15) + 9) & 15;
458 char c = castchar(
byte);
464 char c = castchar(
byte);
477 bigendian_p = BIGENDIAN_P();
482 integer_size = NATINT_LEN(
short, 2);
483 bigendian_p = BIGENDIAN_P();
488 integer_size = (int)
sizeof(
int);
489 bigendian_p = BIGENDIAN_P();
494 integer_size = NATINT_LEN(
long, 4);
495 bigendian_p = BIGENDIAN_P();
500 integer_size = NATINT_LEN_Q;
501 bigendian_p = BIGENDIAN_P();
505 integer_size =
sizeof(intptr_t);
506 bigendian_p = BIGENDIAN_P();
510 integer_size =
sizeof(uintptr_t);
511 bigendian_p = BIGENDIAN_P();
535 if (explicit_endian) {
536 bigendian_p = explicit_endian ==
'>';
538 if (integer_size > MAX_INTEGER_PACK_SIZE)
539 rb_bug(
"unexpected intger size for pack: %d", integer_size);
541 char intbuf[MAX_INTEGER_PACK_SIZE];
544 rb_integer_pack(from, intbuf, integer_size, 1, 0,
557 f = VALUE_to_float(from);
567 tmp.f = VALUE_to_float(from);
575 DOUBLE_CONVWITH(tmp);
598 tmp.f = VALUE_to_float(from);
606 DOUBLE_CONVWITH(tmp);
626 plen = RSTRING_LEN(res);
633 len -= RSTRING_LEN(res);
634 if (len > 0)
goto grow;
636 if (len > 0)
goto shrink;
664 ptr = RSTRING_PTR(from);
665 plen = RSTRING_LEN(from);
667 if (len == 0 &&
type ==
'm') {
668 encodes(res, ptr, plen,
type, 0);
674 else if (len > 63 &&
type ==
'u')
685 encodes(res, ptr, todo,
type, 1);
695 qpencode(res, from, len);
702 if (RSTRING_LEN(from) < len) {
704 RSTRING_LEN(from), len);
720 associates = rb_ary_new();
722 rb_ary_push(associates, from);
736 numbytes = rb_absint_numwords(from, 7, NULL);
746 rb_bug(
"buffer size problem?");
748 cp = RSTRING_PTR(buf);
749 while (1 < numbytes) {
760 unknown_directive(
"pack",
type, fmt);
767 str_associate(res, associates);
774 rb_enc_set_index(res, rb_utf8_encindex());
783static const char uu_table[] =
784"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
785static const char b64_table[] =
786"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
789encodes(
VALUE str,
const char *s0,
long len,
int type,
int tail_lf)
791 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
792 char buff[buff_size + 1];
794 const char *
const trans =
type ==
'u' ? uu_table : b64_table;
796 const unsigned char *s = (
const unsigned char *)s0;
799 buff[i++] = (char)len +
' ';
805 while (len >= input_unit) {
806 while (len >= input_unit && buff_size-i >= encoded_unit) {
807 buff[i++] = trans[077 & (*s >> 2)];
808 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
809 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
810 buff[i++] = trans[077 & s[2]];
814 if (buff_size-i < encoded_unit) {
821 buff[i++] = trans[077 & (*s >> 2)];
822 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
823 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((
'\0' >> 6) & 03))];
827 buff[i++] = trans[077 & (*s >> 2)];
828 buff[i++] = trans[077 & (((*s << 4) & 060) | ((
'\0' >> 4) & 017))];
832 if (tail_lf) buff[i++] =
'\n';
834 if ((
size_t)i >
sizeof(buff))
rb_bug(
"encodes() buffer overrun");
837static const char hex_table[] =
"0123456789ABCDEF";
843 long i = 0, n = 0, prev = EOF;
844 unsigned char *s = (
unsigned char*)RSTRING_PTR(from);
845 unsigned char *send = s + RSTRING_LEN(from);
849 (*s < 32 && *s !=
'\n' && *s !=
'\t') ||
852 buff[i++] = hex_table[*s >> 4];
853 buff[i++] = hex_table[*s & 0x0f];
857 else if (*s ==
'\n') {
858 if (prev ==
' ' || prev ==
'\t') {
902#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
904 if (len > (long)((send-s)/(sz))) { \
906 tmp_len = len-(send-s)/(sz); \
908 len = (send-s)/(sz); \
912#define PACK_ITEM_ADJUST() do { \
913 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
914 rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
921#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
922# define AVOID_CC_BUG volatile
934pack_unpack_internal(
VALUE str,
VALUE fmt,
enum unpack_mode mode,
long offset)
936#define hexdigits ruby_hexdigits
942 AVOID_CC_BUG
long tmp_len;
947 int signed_p, integer_size, bigendian_p;
948#define UNPACK_PUSH(item) do {\
949 VALUE item_val = (item);\
950 if ((mode) == UNPACK_BLOCK) {\
953 else if ((mode) == UNPACK_ARRAY) {\
954 rb_ary_push(ary, item_val);\
966 len = RSTRING_LEN(str);
969 s = RSTRING_PTR(str);
973 p = RSTRING_PTR(fmt);
974 pend = p + RSTRING_LEN(fmt);
976#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
978 ary = mode == UNPACK_ARRAY ? rb_ary_new() :
Qnil;
980 int explicit_endian = 0;
988 while ((p < pend) && (*p !=
'\n')) {
1001 if (strchr(natstr,
type)) {
1014 if (!strchr(endstr,
type)) {
1017 if (explicit_endian) {
1020 explicit_endian = *p++;
1027 else if (*p ==
'*') {
1034 len =
STRTOUL(p, (
char**)&p, 10);
1035 if (len < 0 || errno) {
1040 len = (
type !=
'@');
1049 if (len > send - s) len = send - s;
1052 char *t = s + len - 1;
1055 if (*t !=
' ' && *t !=
'\0')
break;
1067 if (len > send-s) len = send-s;
1068 while (t < s+len && *t) t++;
1071 s = star ? t : s+len;
1076 if (len > send - s) len = send - s;
1088 if (p[-1] ==
'*' || len > (send - s) * 8)
1089 len = (send - s) * 8;
1092 t = RSTRING_PTR(bitstr);
1093 for (i=0; i<len; i++) {
1094 if (i & 7) bits >>= 1;
1095 else bits = (
unsigned char)*s++;
1096 *t++ = (bits & 1) ?
'1' :
'0';
1098 UNPACK_PUSH(bitstr);
1109 if (p[-1] ==
'*' || len > (send - s) * 8)
1110 len = (send - s) * 8;
1113 t = RSTRING_PTR(bitstr);
1114 for (i=0; i<len; i++) {
1115 if (i & 7) bits <<= 1;
1116 else bits = (
unsigned char)*s++;
1117 *t++ = (bits & 128) ?
'1' :
'0';
1119 UNPACK_PUSH(bitstr);
1130 if (p[-1] ==
'*' || len > (send - s) * 2)
1131 len = (send - s) * 2;
1134 t = RSTRING_PTR(bitstr);
1135 for (i=0; i<len; i++) {
1139 bits = (
unsigned char)*s++;
1140 *t++ = hexdigits[bits & 15];
1142 UNPACK_PUSH(bitstr);
1153 if (p[-1] ==
'*' || len > (send - s) * 2)
1154 len = (send - s) * 2;
1157 t = RSTRING_PTR(bitstr);
1158 for (i=0; i<len; i++) {
1162 bits = (
unsigned char)*s++;
1163 *t++ = hexdigits[(bits >> 4) & 15];
1165 UNPACK_PUSH(bitstr);
1172 bigendian_p = BIGENDIAN_P();
1173 goto unpack_integer;
1178 bigendian_p = BIGENDIAN_P();
1179 goto unpack_integer;
1183 integer_size = NATINT_LEN(
short, 2);
1184 bigendian_p = BIGENDIAN_P();
1185 goto unpack_integer;
1189 integer_size = NATINT_LEN(
short, 2);
1190 bigendian_p = BIGENDIAN_P();
1191 goto unpack_integer;
1195 integer_size = (int)
sizeof(
int);
1196 bigendian_p = BIGENDIAN_P();
1197 goto unpack_integer;
1201 integer_size = (int)
sizeof(
int);
1202 bigendian_p = BIGENDIAN_P();
1203 goto unpack_integer;
1207 integer_size = NATINT_LEN(
long, 4);
1208 bigendian_p = BIGENDIAN_P();
1209 goto unpack_integer;
1213 integer_size = NATINT_LEN(
long, 4);
1214 bigendian_p = BIGENDIAN_P();
1215 goto unpack_integer;
1219 integer_size = NATINT_LEN_Q;
1220 bigendian_p = BIGENDIAN_P();
1221 goto unpack_integer;
1225 integer_size = NATINT_LEN_Q;
1226 bigendian_p = BIGENDIAN_P();
1227 goto unpack_integer;
1231 integer_size =
sizeof(intptr_t);
1232 bigendian_p = BIGENDIAN_P();
1233 goto unpack_integer;
1237 integer_size =
sizeof(uintptr_t);
1238 bigendian_p = BIGENDIAN_P();
1239 goto unpack_integer;
1245 goto unpack_integer;
1251 goto unpack_integer;
1257 goto unpack_integer;
1263 goto unpack_integer;
1266 if (explicit_endian) {
1267 bigendian_p = explicit_endian ==
'>';
1269 PACK_LENGTH_ADJUST_SIZE(integer_size);
1275 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1284 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1287 UNPACK_FETCH(&tmp,
float);
1288 UNPACK_PUSH(
DBL2NUM((
double)tmp));
1294 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1296 FLOAT_CONVWITH(tmp);
1297 UNPACK_FETCH(tmp.buf,
float);
1305 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1307 DOUBLE_CONVWITH(tmp);
1308 UNPACK_FETCH(tmp.buf,
double);
1317 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1320 UNPACK_FETCH(&tmp,
double);
1327 PACK_LENGTH_ADJUST_SIZE(
sizeof(
float));
1329 FLOAT_CONVWITH(tmp);
1330 UNPACK_FETCH(tmp.buf,
float);
1338 PACK_LENGTH_ADJUST_SIZE(
sizeof(
double));
1340 DOUBLE_CONVWITH(tmp);
1341 UNPACK_FETCH(tmp.buf,
double);
1349 if (len > send - s) len = send - s;
1350 while (len > 0 && s < send) {
1351 long alen = send - s;
1354 l = utf8_to_uv(s, &alen);
1363 char *ptr = RSTRING_PTR(buf);
1366 while (s < send && (
unsigned char)*s >
' ' && (
unsigned char)*s <
'a') {
1370 len = ((
unsigned char)*s++ -
' ') & 077;
1373 if (total > RSTRING_LEN(buf)) {
1374 len -= total - RSTRING_LEN(buf);
1375 total = RSTRING_LEN(buf);
1379 long mlen = len > 3 ? 3 : len;
1381 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1382 a = ((
unsigned char)*s++ -
' ') & 077;
1385 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1386 b = ((
unsigned char)*s++ -
' ') & 077;
1389 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1390 c = ((
unsigned char)*s++ -
' ') & 077;
1393 if (s < send && (
unsigned char)*s >=
' ' && (
unsigned char)*s <
'a')
1394 d = ((
unsigned char)*s++ -
' ') & 077;
1397 hunk[0] = (char)(a << 2 | b >> 4);
1398 hunk[1] = (char)(b << 4 | c >> 2);
1399 hunk[2] = (char)(c << 6 | d);
1400 memcpy(ptr, hunk, mlen);
1404 if (s < send && (
unsigned char)*s !=
'\r' && *s !=
'\n')
1406 if (s < send && *s ==
'\r') s++;
1407 if (s < send && *s ==
'\n') s++;
1418 char *ptr = RSTRING_PTR(buf);
1419 int a = -1,b = -1,c = 0,d = 0;
1420 static signed char b64_xtable[256];
1422 if (b64_xtable[
'/'] <= 0) {
1425 for (i = 0; i < 256; i++) {
1428 for (i = 0; i < 64; i++) {
1429 b64_xtable[(
unsigned char)b64_table[i]] = (
char)i;
1435 a = b64_xtable[(
unsigned char)*s++];
1437 b = b64_xtable[(
unsigned char)*s++];
1440 if (s + 2 == send && *(s + 1) ==
'=')
break;
1443 c = b64_xtable[(
unsigned char)*s++];
1445 if (s + 1 == send && *s ==
'=')
break;
1446 d = b64_xtable[(
unsigned char)*s++];
1448 *ptr++ = castchar(a << 2 | b >> 4);
1449 *ptr++ = castchar(b << 4 | c >> 2);
1450 *ptr++ = castchar(c << 6 | d);
1453 *ptr++ = castchar(a << 2 | b >> 4);
1457 *ptr++ = castchar(a << 2 | b >> 4);
1458 *ptr++ = castchar(b << 4 | c >> 2);
1465 while ((a = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1466 if (s >= send)
break;
1468 while ((b = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {s++;}
1469 if (s >= send)
break;
1471 while ((c = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1472 if (*s ==
'=' || s >= send)
break;
1474 while ((d = b64_xtable[(
unsigned char)*s]) == -1 && s < send) {
if (*s ==
'=')
break; s++;}
1475 if (*s ==
'=' || s >= send)
break;
1477 *ptr++ = castchar(a << 2 | b >> 4);
1478 *ptr++ = castchar(b << 4 | c >> 2);
1479 *ptr++ = castchar(c << 6 | d);
1482 if (a != -1 && b != -1) {
1484 *ptr++ = castchar(a << 2 | b >> 4);
1486 *ptr++ = castchar(a << 2 | b >> 4);
1487 *ptr++ = castchar(b << 4 | c >> 2);
1499 char *ptr = RSTRING_PTR(buf), *ss = s;
1505 if (++s == send)
break;
1506 if (s+1 < send && *s ==
'\r' && *(s+1) ==
'\n')
1509 if ((c1 = hex2num(*s)) == -1)
break;
1510 if (++s == send)
break;
1511 if ((c2 = hex2num(*s)) == -1)
break;
1512 csum |= *ptr++ = castchar(c1 << 4 | c2);
1516 csum |= *ptr++ = *s;
1530 if (len > RSTRING_LEN(str))
1532 s = RSTRING_PTR(str) + len;
1536 if (len > s - RSTRING_PTR(str))
1548 if (
sizeof(
char *) <= (
size_t)(send - s)) {
1552 UNPACK_FETCH(&t,
char *);
1554 if (!associates) associates = str_associated(str);
1555 tmp = associated_pointer(associates, t);
1556 if (len < RSTRING_LEN(tmp)) {
1558 str_associate(tmp, associates);
1566 if (len > (
long)((send - s) /
sizeof(
char *)))
1567 len = (send - s) /
sizeof(
char *);
1569 if ((
size_t)(send - s) <
sizeof(
char *))
1575 UNPACK_FETCH(&t,
char *);
1577 if (!associates) associates = str_associated(str);
1578 tmp = associated_pointer(associates, t);
1588 while (len > 0 && s < send) {
1603 unknown_directive(
"unpack",
type, fmt);
1615 return pack_unpack_internal(str, fmt, mode,
RB_NUM2LONG(offset));
1621 return pack_unpack_internal(str, fmt, UNPACK_1,
RB_NUM2LONG(offset));
1632 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1633 buf[1] = castchar((uv&0x3f)|0x80);
1637 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1638 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1639 buf[2] = castchar((uv&0x3f)|0x80);
1642 if (uv <= 0x1fffff) {
1643 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1644 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1645 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1646 buf[3] = castchar((uv&0x3f)|0x80);
1649 if (uv <= 0x3ffffff) {
1650 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1651 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1652 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1653 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1654 buf[4] = castchar((uv&0x3f)|0x80);
1657 if (uv <= 0x7fffffff) {
1658 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1659 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1660 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1661 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1662 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1663 buf[5] = castchar((uv&0x3f)|0x80);
1671static const unsigned long utf8_limits[] = {
1682utf8_to_uv(
const char *p,
long *lenp)
1684 int c = *p++ & 0xff;
1685 unsigned long uv = c;
1697 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1698 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1699 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1700 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1701 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1714 if ((c & 0xc0) != 0x80) {
1725 if (uv < utf8_limits[n]) {
1731#include "pack.rbinc"
1736 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 ISSPACE
Old name of rb_isspace.
#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 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.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eRangeError
RangeError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_eArgError
ArgumentError 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.
#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_modify(VALUE str)
Declares that the string is about to be modified.
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.
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.
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 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*.
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.