1 /**********************************************************************
6 created at: Thu Feb 10 15:17:05 JST 1994
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/internal/config.h"
17 #include <sys/types.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"
25 #include "ruby/util.h"
30 * It is intentional that the condition for natstr is HAVE_TRUE_LONG_LONG
31 * instead of HAVE_LONG_LONG or LONG_LONG.
32 * This means q! and Q! means always the standard long long type and
33 * causes ArgumentError for platforms which has no long long type,
34 * even if the platform has an implementation specific 64bit type.
35 * This behavior is consistent with the document of pack/unpack.
37 #ifdef HAVE_TRUE_LONG_LONG
38 static const char natstr
[] = "sSiIlLqQjJ";
40 static const char natstr
[] = "sSiIlLjJ";
42 static const char endstr
[] = "sSiIlLqQjJ";
44 #ifdef HAVE_TRUE_LONG_LONG
45 /* It is intentional to use long long instead of 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)
56 /* for universal binary of NEXTSTEP and MacOS X */
57 /* useless since autoconf 2.63? */
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
119 static const char toofew
[] = "too few arguments";
121 static void encodes(VALUE
,const char*,long,int,int);
122 static void qpencode(VALUE
,VALUE
,long);
124 static unsigned long utf8_to_uv(const char*,long*);
126 static ID id_associated
;
129 str_associate(VALUE str
, VALUE add
)
131 /* assert(NIL_P(rb_attr_get(str, id_associated))); */
132 rb_ivar_set(str
, id_associated
, add
);
136 str_associated(VALUE str
)
138 VALUE associates
= rb_ivar_lookup(str
, id_associated
, Qfalse
);
140 rb_raise(rb_eArgError
, "no associated pointer");
145 associated_pointer(VALUE associates
, const char *t
)
147 const VALUE
*p
= RARRAY_CONST_PTR(associates
);
148 const VALUE
*pend
= p
+ RARRAY_LEN(associates
);
149 for (; p
< pend
; p
++) {
151 if (RB_TYPE_P(tmp
, T_STRING
) && RSTRING_PTR(tmp
) == t
) return tmp
;
153 rb_raise(rb_eArgError
, "non associated pointer");
154 UNREACHABLE_RETURN(Qnil
);
158 unknown_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_warning("unknown %s directive '%s' in '%"PRIsVALUE
"'",
175 VALUE_to_float(VALUE obj
)
177 VALUE v
= rb_to_float(obj
);
178 double d
= RFLOAT_VALUE(v
);
183 else if (d
< -FLT_MAX
) {
186 else if (d
<= FLT_MAX
) {
195 pack_pack(rb_execution_context_t
*ec
, VALUE ary
, VALUE fmt
, VALUE buffer
)
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;
204 int enc_info
= 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
206 int natint
; /* native integer */
208 int integer_size
, bigendian_p
;
211 p
= RSTRING_PTR(fmt
);
212 pend
= p
+ RSTRING_LEN(fmt
);
215 res
= rb_str_buf_new(0);
218 if (!RB_TYPE_P(buffer
, T_STRING
))
219 rb_raise(rb_eTypeError
, "buffer must be String, not %s", rb_obj_classname(buffer
));
225 #define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
226 #define MORE_ITEM (idx < RARRAY_LEN(ary))
227 #define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
228 #define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
231 int explicit_endian
= 0;
232 if (RSTRING_PTR(fmt
) + RSTRING_LEN(fmt
) != pend
) {
233 rb_raise(rb_eRuntimeError
, "format string modified");
235 type
= *p
++; /* get data type */
240 if (ISSPACE(type
)) continue;
242 while ((p
< pend
) && (*p
!= '\n')) {
253 if (strchr(natstr
, type
)) {
260 rb_raise(rb_eArgError
, "'%c' allowed only after types %s", *p
, natstr
);
266 if (!strchr(endstr
, type
)) {
267 rb_raise(rb_eArgError
, "'%c' allowed only after types %s", *p
, endstr
);
269 if (explicit_endian
) {
270 rb_raise(rb_eRangeError
, "Can't use both '<' and '>'");
272 explicit_endian
= *p
++;
277 if (*p
== '*') { /* set data length */
278 len
= strchr("@Xxu", type
) ? 0
279 : strchr("PMm", type
) ? 1
280 : RARRAY_LEN(ary
) - idx
;
283 else if (ISDIGIT(*p
)) {
285 len
= STRTOUL(p
, (char**)&p
, 10);
287 rb_raise(rb_eRangeError
, "pack length too big");
296 /* if encoding is US-ASCII, upgrade to UTF-8 */
297 if (enc_info
== 1) enc_info
= 2;
299 case 'm': case 'M': case 'u':
300 /* keep US-ASCII (do nothing) */
303 /* fall back to BINARY */
308 case 'A': case 'a': case 'Z':
318 ptr
= RSTRING_PTR(from
);
319 plen
= RSTRING_LEN(from
);
326 case 'a': /* arbitrary binary string (null padded) */
327 case 'A': /* arbitrary binary string (ASCII space padded) */
328 case 'Z': /* null terminated string */
330 rb_str_buf_cat(res
, ptr
, len
);
331 if (p
[-1] == '*' && type
== 'Z')
332 rb_str_buf_cat(res
, nul10
, 1);
335 rb_str_buf_cat(res
, ptr
, plen
);
338 rb_str_buf_cat(res
, (type
== 'A')?spc10
:nul10
, 10);
341 rb_str_buf_cat(res
, (type
== 'A')?spc10
:nul10
, len
);
345 #define castchar(from) (char)((from) & 0xff)
347 case 'b': /* bit string (ascending) */
353 j
= (len
- plen
+ 1)/2;
356 for (i
=0; i
++ < len
; ptr
++) {
362 char c
= castchar(byte
);
363 rb_str_buf_cat(res
, &c
, 1);
369 byte
>>= 7 - (len
& 7);
371 rb_str_buf_cat(res
, &c
, 1);
378 case 'B': /* bit string (descending) */
384 j
= (len
- plen
+ 1)/2;
387 for (i
=0; i
++ < len
; ptr
++) {
392 char c
= castchar(byte
);
393 rb_str_buf_cat(res
, &c
, 1);
399 byte
<<= 7 - (len
& 7);
401 rb_str_buf_cat(res
, &c
, 1);
408 case 'h': /* hex string (low nibble first) */
414 j
= (len
+ 1) / 2 - (plen
+ 1) / 2;
417 for (i
=0; i
++ < len
; ptr
++) {
419 byte
|= (((*ptr
& 15) + 9) & 15) << 4;
421 byte
|= (*ptr
& 15) << 4;
425 char c
= castchar(byte
);
426 rb_str_buf_cat(res
, &c
, 1);
431 char c
= castchar(byte
);
432 rb_str_buf_cat(res
, &c
, 1);
439 case 'H': /* hex string (high nibble first) */
445 j
= (len
+ 1) / 2 - (plen
+ 1) / 2;
448 for (i
=0; i
++ < len
; ptr
++) {
450 byte
|= ((*ptr
& 15) + 9) & 15;
456 char c
= castchar(byte
);
457 rb_str_buf_cat(res
, &c
, 1);
462 char c
= castchar(byte
);
463 rb_str_buf_cat(res
, &c
, 1);
472 case 'c': /* signed char */
473 case 'C': /* unsigned char */
475 bigendian_p
= BIGENDIAN_P(); /* not effective */
478 case 's': /* s for int16_t, s! for signed short */
479 integer_size
= NATINT_LEN(short, 2);
480 bigendian_p
= BIGENDIAN_P();
483 case 'S': /* S for uint16_t, S! for unsigned short */
484 integer_size
= NATINT_LEN(short, 2);
485 bigendian_p
= BIGENDIAN_P();
488 case 'i': /* i and i! for signed int */
489 integer_size
= (int)sizeof(int);
490 bigendian_p
= BIGENDIAN_P();
493 case 'I': /* I and I! for unsigned int */
494 integer_size
= (int)sizeof(int);
495 bigendian_p
= BIGENDIAN_P();
498 case 'l': /* l for int32_t, l! for signed long */
499 integer_size
= NATINT_LEN(long, 4);
500 bigendian_p
= BIGENDIAN_P();
503 case 'L': /* L for uint32_t, L! for unsigned long */
504 integer_size
= NATINT_LEN(long, 4);
505 bigendian_p
= BIGENDIAN_P();
508 case 'q': /* q for int64_t, q! for signed long long */
509 integer_size
= NATINT_LEN_Q
;
510 bigendian_p
= BIGENDIAN_P();
513 case 'Q': /* Q for uint64_t, Q! for unsigned long long */
514 integer_size
= NATINT_LEN_Q
;
515 bigendian_p
= BIGENDIAN_P();
518 case 'j': /* j for intptr_t */
519 integer_size
= sizeof(intptr_t);
520 bigendian_p
= BIGENDIAN_P();
523 case 'J': /* J for uintptr_t */
524 integer_size
= sizeof(uintptr_t);
525 bigendian_p
= BIGENDIAN_P();
528 case 'n': /* 16 bit (2 bytes) integer (network byte-order) */
533 case 'N': /* 32 bit (4 bytes) integer (network byte-order) */
538 case 'v': /* 16 bit (2 bytes) integer (VAX byte-order) */
543 case 'V': /* 32 bit (4 bytes) integer (VAX byte-order) */
549 if (explicit_endian
) {
550 bigendian_p
= explicit_endian
== '>';
552 if (integer_size
> MAX_INTEGER_PACK_SIZE
)
553 rb_bug("unexpected intger size for pack: %d", integer_size
);
555 char intbuf
[MAX_INTEGER_PACK_SIZE
];
558 rb_integer_pack(from
, intbuf
, integer_size
, 1, 0,
560 (bigendian_p
? INTEGER_PACK_BIG_ENDIAN
: INTEGER_PACK_LITTLE_ENDIAN
));
561 rb_str_buf_cat(res
, intbuf
, integer_size
);
565 case 'f': /* single precision float in native format */
566 case 'F': /* ditto */
571 f
= VALUE_to_float(from
);
572 rb_str_buf_cat(res
, (char*)&f
, sizeof(float));
576 case 'e': /* single precision float in VAX byte-order */
581 tmp
.f
= VALUE_to_float(from
);
583 rb_str_buf_cat(res
, tmp
.buf
, sizeof(float));
587 case 'E': /* double precision float in VAX byte-order */
589 DOUBLE_CONVWITH(tmp
);
591 tmp
.d
= RFLOAT_VALUE(rb_to_float(from
));
593 rb_str_buf_cat(res
, tmp
.buf
, sizeof(double));
597 case 'd': /* double precision float in native format */
598 case 'D': /* ditto */
603 d
= RFLOAT_VALUE(rb_to_float(from
));
604 rb_str_buf_cat(res
, (char*)&d
, sizeof(double));
608 case 'g': /* single precision float in network byte-order */
612 tmp
.f
= VALUE_to_float(from
);
614 rb_str_buf_cat(res
, tmp
.buf
, sizeof(float));
618 case 'G': /* double precision float in network byte-order */
620 DOUBLE_CONVWITH(tmp
);
623 tmp
.d
= RFLOAT_VALUE(rb_to_float(from
));
625 rb_str_buf_cat(res
, tmp
.buf
, sizeof(double));
629 case 'x': /* null byte */
632 rb_str_buf_cat(res
, nul10
, 10);
635 rb_str_buf_cat(res
, nul10
, len
);
638 case 'X': /* back up byte */
640 plen
= RSTRING_LEN(res
);
642 rb_raise(rb_eArgError
, "X outside of string");
643 rb_str_set_len(res
, plen
- len
);
646 case '@': /* null fill to absolute position */
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");
657 case 'U': /* Unicode character */
664 from
= rb_to_int(from
);
667 rb_raise(rb_eRangeError
, "pack(U): value out of range");
669 le
= rb_uv_to_utf8(buf
, l
);
670 rb_str_buf_cat(res
, (char*)buf
, le
);
674 case 'u': /* uuencoded string */
675 case 'm': /* base64 encoded string */
678 ptr
= RSTRING_PTR(from
);
679 plen
= RSTRING_LEN(from
);
681 if (len
== 0 && type
== 'm') {
682 encodes(res
, ptr
, plen
, type
, 0);
688 else if (len
> 63 && type
== 'u')
699 encodes(res
, ptr
, todo
, type
, 1);
705 case 'M': /* quoted-printable encoded string */
706 from
= rb_obj_as_string(NEXTFROM
);
709 qpencode(res
, from
, len
);
712 case 'P': /* pointer to packed byte string */
716 if (RSTRING_LEN(from
) < len
) {
717 rb_raise(rb_eArgError
, "too short buffer for P(%ld for %ld)",
718 RSTRING_LEN(from
), len
);
723 case 'p': /* pointer to string */
731 t
= StringValuePtr(from
);
734 associates
= rb_ary_new();
736 rb_ary_push(associates
, from
);
737 rb_str_buf_cat(res
, (char*)&t
, sizeof(char*));
741 case 'w': /* BER compressed integer */
743 VALUE buf
= rb_str_new(0, 0);
749 from
= rb_to_int(from
);
750 numbytes
= rb_absint_numwords(from
, 7, NULL
);
753 buf
= rb_str_new(NULL
, numbytes
);
755 sign
= rb_integer_pack(from
, RSTRING_PTR(buf
), RSTRING_LEN(buf
), 1, 1, INTEGER_PACK_BIG_ENDIAN
);
758 rb_raise(rb_eArgError
, "can't compress negative numbers");
760 rb_bug("buffer size problem?");
762 cp
= RSTRING_PTR(buf
);
763 while (1 < numbytes
) {
769 rb_str_buf_cat(res
, RSTRING_PTR(buf
), RSTRING_LEN(buf
));
774 unknown_directive("pack", type
, fmt
);
781 str_associate(res
, associates
);
785 ENCODING_CODERANGE_SET(res
, rb_usascii_encindex(), ENC_CODERANGE_7BIT
);
788 rb_enc_set_index(res
, rb_utf8_encindex());
791 /* do nothing, keep ASCII-8BIT */
797 static const char uu_table
[] =
798 "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
799 static const char b64_table
[] =
800 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
803 encodes(VALUE str
, const char *s0
, long len
, int type
, int tail_lf
)
805 enum {buff_size
= 4096, encoded_unit
= 4, input_unit
= 3};
806 char buff
[buff_size
+ 1]; /* +1 for tail_lf */
808 const char *const trans
= type
== 'u' ? uu_table
: b64_table
;
810 const unsigned char *s
= (const unsigned char *)s0
;
813 buff
[i
++] = (char)len
+ ' ';
819 while (len
>= input_unit
) {
820 while (len
>= input_unit
&& 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) | ((s
[2] >> 6) & 03))];
824 buff
[i
++] = trans
[077 & s
[2]];
828 if (buff_size
-i
< encoded_unit
) {
829 rb_str_buf_cat(str
, buff
, i
);
835 buff
[i
++] = trans
[077 & (*s
>> 2)];
836 buff
[i
++] = trans
[077 & (((*s
<< 4) & 060) | ((s
[1] >> 4) & 017))];
837 buff
[i
++] = trans
[077 & (((s
[1] << 2) & 074) | (('\0' >> 6) & 03))];
841 buff
[i
++] = trans
[077 & (*s
>> 2)];
842 buff
[i
++] = trans
[077 & (((*s
<< 4) & 060) | (('\0' >> 4) & 017))];
846 if (tail_lf
) buff
[i
++] = '\n';
847 rb_str_buf_cat(str
, buff
, i
);
848 if ((size_t)i
> sizeof(buff
)) rb_bug("encodes() buffer overrun");
851 static const char hex_table
[] = "0123456789ABCDEF";
854 qpencode(VALUE str
, VALUE from
, long len
)
857 long i
= 0, n
= 0, prev
= EOF
;
858 unsigned char *s
= (unsigned char*)RSTRING_PTR(from
);
859 unsigned char *send
= s
+ RSTRING_LEN(from
);
863 (*s
< 32 && *s
!= '\n' && *s
!= '\t') ||
866 buff
[i
++] = hex_table
[*s
>> 4];
867 buff
[i
++] = hex_table
[*s
& 0x0f];
871 else if (*s
== '\n') {
872 if (prev
== ' ' || prev
== '\t') {
892 rb_str_buf_cat(str
, buff
, i
);
902 rb_str_buf_cat(str
, buff
, i
);
910 n
= ruby_digit36_to_number_table
[(unsigned char)c
];
916 #define PACK_LENGTH_ADJUST_SIZE(sz) do { \
918 if (len > (long)((send-s)/(sz))) { \
920 tmp_len = len-(send-s)/(sz); \
922 len = (send-s)/(sz); \
926 #define PACK_ITEM_ADJUST() do { \
927 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
928 rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
931 /* Workaround for Oracle Developer Studio (Oracle Solaris Studio)
932 * 12.4/12.5/12.6 C compiler optimization bug
933 * with "-xO4" optimization option.
935 #if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
936 # define AVOID_CC_BUG volatile
938 # define AVOID_CC_BUG
942 #define UNPACK_ARRAY 0
943 #define UNPACK_BLOCK 1
947 pack_unpack_internal(VALUE str
, VALUE fmt
, int mode
, long offset
)
949 #define hexdigits ruby_hexdigits
952 VALUE ary
, associates
= Qfalse
;
955 AVOID_CC_BUG
long tmp_len
;
958 int natint
; /* native integer */
960 int signed_p
, integer_size
, bigendian_p
;
961 #define UNPACK_PUSH(item) do {\
962 VALUE item_val = (item);\
963 if ((mode) == UNPACK_BLOCK) {\
966 else if ((mode) == UNPACK_ARRAY) {\
967 rb_ary_push(ary, item_val);\
969 else /* if ((mode) == UNPACK_1) { */ {\
977 if (offset
< 0) rb_raise(rb_eArgError
, "offset can't be negative");
978 len
= RSTRING_LEN(str
);
979 if (offset
> len
) rb_raise(rb_eArgError
, "offset outside of string");
981 s
= RSTRING_PTR(str
);
985 p
= RSTRING_PTR(fmt
);
986 pend
= p
+ RSTRING_LEN(fmt
);
988 #define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
990 ary
= mode
== UNPACK_ARRAY
? rb_ary_new() : Qnil
;
992 int explicit_endian
= 0;
998 if (ISSPACE(type
)) continue;
1000 while ((p
< pend
) && (*p
!= '\n')) {
1013 if (strchr(natstr
, type
)) {
1020 rb_raise(rb_eArgError
, "'%c' allowed only after types %s", *p
, natstr
);
1026 if (!strchr(endstr
, type
)) {
1027 rb_raise(rb_eArgError
, "'%c' allowed only after types %s", *p
, endstr
);
1029 if (explicit_endian
) {
1030 rb_raise(rb_eRangeError
, "Can't use both '<' and '>'");
1032 explicit_endian
= *p
++;
1039 else if (*p
== '*') {
1044 else if (ISDIGIT(*p
)) {
1046 len
= STRTOUL(p
, (char**)&p
, 10);
1047 if (len
< 0 || errno
) {
1048 rb_raise(rb_eRangeError
, "pack length too big");
1052 len
= (type
!= '@');
1057 rb_raise(rb_eArgError
, "%% is not supported");
1061 if (len
> send
- s
) len
= send
- s
;
1064 char *t
= s
+ len
- 1;
1067 if (*t
!= ' ' && *t
!= '\0') break;
1070 UNPACK_PUSH(rb_str_new(s
, len
));
1079 if (len
> send
-s
) len
= send
-s
;
1080 while (t
< s
+len
&& *t
) t
++;
1081 UNPACK_PUSH(rb_str_new(s
, t
-s
));
1083 s
= star
? t
: s
+len
;
1088 if (len
> send
- s
) len
= send
- s
;
1089 UNPACK_PUSH(rb_str_new(s
, len
));
1100 if (p
[-1] == '*' || len
> (send
- s
) * 8)
1101 len
= (send
- s
) * 8;
1103 bitstr
= rb_usascii_str_new(0, len
);
1104 t
= RSTRING_PTR(bitstr
);
1105 for (i
=0; i
<len
; i
++) {
1106 if (i
& 7) bits
>>= 1;
1107 else bits
= (unsigned char)*s
++;
1108 *t
++ = (bits
& 1) ? '1' : '0';
1110 UNPACK_PUSH(bitstr
);
1121 if (p
[-1] == '*' || len
> (send
- s
) * 8)
1122 len
= (send
- s
) * 8;
1124 bitstr
= rb_usascii_str_new(0, len
);
1125 t
= RSTRING_PTR(bitstr
);
1126 for (i
=0; i
<len
; i
++) {
1127 if (i
& 7) bits
<<= 1;
1128 else bits
= (unsigned char)*s
++;
1129 *t
++ = (bits
& 128) ? '1' : '0';
1131 UNPACK_PUSH(bitstr
);
1142 if (p
[-1] == '*' || len
> (send
- s
) * 2)
1143 len
= (send
- s
) * 2;
1145 bitstr
= rb_usascii_str_new(0, len
);
1146 t
= RSTRING_PTR(bitstr
);
1147 for (i
=0; i
<len
; i
++) {
1151 bits
= (unsigned char)*s
++;
1152 *t
++ = hexdigits
[bits
& 15];
1154 UNPACK_PUSH(bitstr
);
1165 if (p
[-1] == '*' || len
> (send
- s
) * 2)
1166 len
= (send
- s
) * 2;
1168 bitstr
= rb_usascii_str_new(0, len
);
1169 t
= RSTRING_PTR(bitstr
);
1170 for (i
=0; i
<len
; i
++) {
1174 bits
= (unsigned char)*s
++;
1175 *t
++ = hexdigits
[(bits
>> 4) & 15];
1177 UNPACK_PUSH(bitstr
);
1184 bigendian_p
= BIGENDIAN_P(); /* not effective */
1185 goto unpack_integer
;
1190 bigendian_p
= BIGENDIAN_P(); /* not effective */
1191 goto unpack_integer
;
1195 integer_size
= NATINT_LEN(short, 2);
1196 bigendian_p
= BIGENDIAN_P();
1197 goto unpack_integer
;
1201 integer_size
= NATINT_LEN(short, 2);
1202 bigendian_p
= BIGENDIAN_P();
1203 goto unpack_integer
;
1207 integer_size
= (int)sizeof(int);
1208 bigendian_p
= BIGENDIAN_P();
1209 goto unpack_integer
;
1213 integer_size
= (int)sizeof(int);
1214 bigendian_p
= BIGENDIAN_P();
1215 goto unpack_integer
;
1219 integer_size
= NATINT_LEN(long, 4);
1220 bigendian_p
= BIGENDIAN_P();
1221 goto unpack_integer
;
1225 integer_size
= NATINT_LEN(long, 4);
1226 bigendian_p
= BIGENDIAN_P();
1227 goto unpack_integer
;
1231 integer_size
= NATINT_LEN_Q
;
1232 bigendian_p
= BIGENDIAN_P();
1233 goto unpack_integer
;
1237 integer_size
= NATINT_LEN_Q
;
1238 bigendian_p
= BIGENDIAN_P();
1239 goto unpack_integer
;
1243 integer_size
= sizeof(intptr_t);
1244 bigendian_p
= BIGENDIAN_P();
1245 goto unpack_integer
;
1249 integer_size
= sizeof(uintptr_t);
1250 bigendian_p
= BIGENDIAN_P();
1251 goto unpack_integer
;
1257 goto unpack_integer
;
1263 goto unpack_integer
;
1269 goto unpack_integer
;
1275 goto unpack_integer
;
1278 if (explicit_endian
) {
1279 bigendian_p
= explicit_endian
== '>';
1281 PACK_LENGTH_ADJUST_SIZE(integer_size
);
1283 int flags
= bigendian_p
? INTEGER_PACK_BIG_ENDIAN
: INTEGER_PACK_LITTLE_ENDIAN
;
1286 flags
|= INTEGER_PACK_2COMP
;
1287 val
= rb_integer_unpack(s
, integer_size
, 1, 0, flags
);
1296 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1299 UNPACK_FETCH(&tmp
, float);
1300 UNPACK_PUSH(DBL2NUM((double)tmp
));
1306 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1308 FLOAT_CONVWITH(tmp
);
1309 UNPACK_FETCH(tmp
.buf
, float);
1311 UNPACK_PUSH(DBL2NUM(tmp
.f
));
1317 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1319 DOUBLE_CONVWITH(tmp
);
1320 UNPACK_FETCH(tmp
.buf
, double);
1322 UNPACK_PUSH(DBL2NUM(tmp
.d
));
1329 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1332 UNPACK_FETCH(&tmp
, double);
1333 UNPACK_PUSH(DBL2NUM(tmp
));
1339 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1341 FLOAT_CONVWITH(tmp
);
1342 UNPACK_FETCH(tmp
.buf
, float);
1344 UNPACK_PUSH(DBL2NUM(tmp
.f
));
1350 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1352 DOUBLE_CONVWITH(tmp
);
1353 UNPACK_FETCH(tmp
.buf
, double);
1355 UNPACK_PUSH(DBL2NUM(tmp
.d
));
1361 if (len
> send
- s
) len
= send
- s
;
1362 while (len
> 0 && s
< send
) {
1363 long alen
= send
- s
;
1366 l
= utf8_to_uv(s
, &alen
);
1368 UNPACK_PUSH(ULONG2NUM(l
));
1374 VALUE buf
= rb_str_new(0, (send
- s
)*3/4);
1375 char *ptr
= RSTRING_PTR(buf
);
1378 while (s
< send
&& (unsigned char)*s
> ' ' && (unsigned char)*s
< 'a') {
1382 len
= ((unsigned char)*s
++ - ' ') & 077;
1385 if (total
> RSTRING_LEN(buf
)) {
1386 len
-= total
- RSTRING_LEN(buf
);
1387 total
= RSTRING_LEN(buf
);
1391 long mlen
= len
> 3 ? 3 : len
;
1393 if (s
< send
&& (unsigned char)*s
>= ' ' && (unsigned char)*s
< 'a')
1394 a
= ((unsigned char)*s
++ - ' ') & 077;
1397 if (s
< send
&& (unsigned char)*s
>= ' ' && (unsigned char)*s
< 'a')
1398 b
= ((unsigned char)*s
++ - ' ') & 077;
1401 if (s
< send
&& (unsigned char)*s
>= ' ' && (unsigned char)*s
< 'a')
1402 c
= ((unsigned char)*s
++ - ' ') & 077;
1405 if (s
< send
&& (unsigned char)*s
>= ' ' && (unsigned char)*s
< 'a')
1406 d
= ((unsigned char)*s
++ - ' ') & 077;
1409 hunk
[0] = (char)(a
<< 2 | b
>> 4);
1410 hunk
[1] = (char)(b
<< 4 | c
>> 2);
1411 hunk
[2] = (char)(c
<< 6 | d
);
1412 memcpy(ptr
, hunk
, mlen
);
1416 if (s
< send
&& (unsigned char)*s
!= '\r' && *s
!= '\n')
1417 s
++; /* possible checksum byte */
1418 if (s
< send
&& *s
== '\r') s
++;
1419 if (s
< send
&& *s
== '\n') s
++;
1422 rb_str_set_len(buf
, total
);
1429 VALUE buf
= rb_str_new(0, (send
- s
+ 3)*3/4); /* +3 is for skipping paddings */
1430 char *ptr
= RSTRING_PTR(buf
);
1431 int a
= -1,b
= -1,c
= 0,d
= 0;
1432 static signed char b64_xtable
[256];
1434 if (b64_xtable
['/'] <= 0) {
1437 for (i
= 0; i
< 256; i
++) {
1440 for (i
= 0; i
< 64; i
++) {
1441 b64_xtable
[(unsigned char)b64_table
[i
]] = (char)i
;
1447 a
= b64_xtable
[(unsigned char)*s
++];
1448 if (s
>= send
|| a
== -1) rb_raise(rb_eArgError
, "invalid base64");
1449 b
= b64_xtable
[(unsigned char)*s
++];
1450 if (s
>= send
|| b
== -1) rb_raise(rb_eArgError
, "invalid base64");
1452 if (s
+ 2 == send
&& *(s
+ 1) == '=') break;
1453 rb_raise(rb_eArgError
, "invalid base64");
1455 c
= b64_xtable
[(unsigned char)*s
++];
1456 if (s
>= send
|| c
== -1) rb_raise(rb_eArgError
, "invalid base64");
1457 if (s
+ 1 == send
&& *s
== '=') break;
1458 d
= b64_xtable
[(unsigned char)*s
++];
1459 if (d
== -1) rb_raise(rb_eArgError
, "invalid base64");
1460 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1461 *ptr
++ = castchar(b
<< 4 | c
>> 2);
1462 *ptr
++ = castchar(c
<< 6 | d
);
1465 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1466 if (b
& 0xf) rb_raise(rb_eArgError
, "invalid base64");
1469 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1470 *ptr
++ = castchar(b
<< 4 | c
>> 2);
1471 if (c
& 0x3) rb_raise(rb_eArgError
, "invalid base64");
1477 while ((a
= b64_xtable
[(unsigned char)*s
]) == -1 && s
< send
) {s
++;}
1478 if (s
>= send
) break;
1480 while ((b
= b64_xtable
[(unsigned char)*s
]) == -1 && s
< send
) {s
++;}
1481 if (s
>= send
) break;
1483 while ((c
= b64_xtable
[(unsigned char)*s
]) == -1 && s
< send
) {if (*s
== '=') break; s
++;}
1484 if (*s
== '=' || s
>= send
) break;
1486 while ((d
= b64_xtable
[(unsigned char)*s
]) == -1 && s
< send
) {if (*s
== '=') break; s
++;}
1487 if (*s
== '=' || s
>= send
) break;
1489 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1490 *ptr
++ = castchar(b
<< 4 | c
>> 2);
1491 *ptr
++ = castchar(c
<< 6 | d
);
1494 if (a
!= -1 && b
!= -1) {
1496 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1498 *ptr
++ = castchar(a
<< 2 | b
>> 4);
1499 *ptr
++ = castchar(b
<< 4 | c
>> 2);
1503 rb_str_set_len(buf
, ptr
- RSTRING_PTR(buf
));
1510 VALUE buf
= rb_str_new(0, send
- s
);
1511 char *ptr
= RSTRING_PTR(buf
), *ss
= s
;
1517 if (++s
== send
) break;
1518 if (s
+1 < send
&& *s
== '\r' && *(s
+1) == '\n')
1521 if ((c1
= hex2num(*s
)) == -1) break;
1522 if (++s
== send
) break;
1523 if ((c2
= hex2num(*s
)) == -1) break;
1524 csum
|= *ptr
++ = castchar(c1
<< 4 | c2
);
1528 csum
|= *ptr
++ = *s
;
1533 rb_str_set_len(buf
, ptr
- RSTRING_PTR(buf
));
1534 rb_str_buf_cat(buf
, ss
, send
-ss
);
1535 csum
= ISASCII(csum
) ? ENC_CODERANGE_7BIT
: ENC_CODERANGE_VALID
;
1536 ENCODING_CODERANGE_SET(buf
, rb_ascii8bit_encindex(), csum
);
1542 if (len
> RSTRING_LEN(str
))
1543 rb_raise(rb_eArgError
, "@ outside of string");
1544 s
= RSTRING_PTR(str
) + len
;
1548 if (len
> s
- RSTRING_PTR(str
))
1549 rb_raise(rb_eArgError
, "X outside of string");
1555 rb_raise(rb_eArgError
, "x outside of string");
1560 if (sizeof(char *) <= (size_t)(send
- s
)) {
1564 UNPACK_FETCH(&t
, char *);
1566 if (!associates
) associates
= str_associated(str
);
1567 tmp
= associated_pointer(associates
, t
);
1568 if (len
< RSTRING_LEN(tmp
)) {
1569 tmp
= rb_str_new(t
, len
);
1570 str_associate(tmp
, associates
);
1578 if (len
> (long)((send
- s
) / sizeof(char *)))
1579 len
= (send
- s
) / sizeof(char *);
1581 if ((size_t)(send
- s
) < sizeof(char *))
1587 UNPACK_FETCH(&t
, char *);
1589 if (!associates
) associates
= str_associated(str
);
1590 tmp
= associated_pointer(associates
, t
);
1600 while (len
> 0 && s
< send
) {
1606 UNPACK_PUSH(rb_integer_unpack(s0
, s
-s0
, 1, 1, INTEGER_PACK_BIG_ENDIAN
));
1615 unknown_directive("unpack", type
, fmt
);
1624 pack_unpack(rb_execution_context_t
*ec
, VALUE str
, VALUE fmt
, VALUE offset
)
1626 int mode
= rb_block_given_p() ? UNPACK_BLOCK
: UNPACK_ARRAY
;
1627 return pack_unpack_internal(str
, fmt
, mode
, RB_NUM2LONG(offset
));
1631 pack_unpack1(rb_execution_context_t
*ec
, VALUE str
, VALUE fmt
, VALUE offset
)
1633 return pack_unpack_internal(str
, fmt
, UNPACK_1
, RB_NUM2LONG(offset
));
1637 rb_uv_to_utf8(char buf
[6], unsigned long uv
)
1644 buf
[0] = castchar(((uv
>>6)&0xff)|0xc0);
1645 buf
[1] = castchar((uv
&0x3f)|0x80);
1649 buf
[0] = castchar(((uv
>>12)&0xff)|0xe0);
1650 buf
[1] = castchar(((uv
>>6)&0x3f)|0x80);
1651 buf
[2] = castchar((uv
&0x3f)|0x80);
1654 if (uv
<= 0x1fffff) {
1655 buf
[0] = castchar(((uv
>>18)&0xff)|0xf0);
1656 buf
[1] = castchar(((uv
>>12)&0x3f)|0x80);
1657 buf
[2] = castchar(((uv
>>6)&0x3f)|0x80);
1658 buf
[3] = castchar((uv
&0x3f)|0x80);
1661 if (uv
<= 0x3ffffff) {
1662 buf
[0] = castchar(((uv
>>24)&0xff)|0xf8);
1663 buf
[1] = castchar(((uv
>>18)&0x3f)|0x80);
1664 buf
[2] = castchar(((uv
>>12)&0x3f)|0x80);
1665 buf
[3] = castchar(((uv
>>6)&0x3f)|0x80);
1666 buf
[4] = castchar((uv
&0x3f)|0x80);
1669 if (uv
<= 0x7fffffff) {
1670 buf
[0] = castchar(((uv
>>30)&0xff)|0xfc);
1671 buf
[1] = castchar(((uv
>>24)&0x3f)|0x80);
1672 buf
[2] = castchar(((uv
>>18)&0x3f)|0x80);
1673 buf
[3] = castchar(((uv
>>12)&0x3f)|0x80);
1674 buf
[4] = castchar(((uv
>>6)&0x3f)|0x80);
1675 buf
[5] = castchar((uv
&0x3f)|0x80);
1678 rb_raise(rb_eRangeError
, "pack(U): value out of range");
1680 UNREACHABLE_RETURN(Qnil
);
1683 static const unsigned long utf8_limits
[] = {
1693 static unsigned long
1694 utf8_to_uv(const char *p
, long *lenp
)
1696 int c
= *p
++ & 0xff;
1697 unsigned long uv
= c
;
1706 rb_raise(rb_eArgError
, "malformed UTF-8 character");
1709 if (!(uv
& 0x20)) { n
= 2; uv
&= 0x1f; }
1710 else if (!(uv
& 0x10)) { n
= 3; uv
&= 0x0f; }
1711 else if (!(uv
& 0x08)) { n
= 4; uv
&= 0x07; }
1712 else if (!(uv
& 0x04)) { n
= 5; uv
&= 0x03; }
1713 else if (!(uv
& 0x02)) { n
= 6; uv
&= 0x01; }
1716 rb_raise(rb_eArgError
, "malformed UTF-8 character");
1719 rb_raise(rb_eArgError
, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1726 if ((c
& 0xc0) != 0x80) {
1728 rb_raise(rb_eArgError
, "malformed UTF-8 character");
1737 if (uv
< utf8_limits
[n
]) {
1738 rb_raise(rb_eArgError
, "redundant UTF-8 sequence");
1743 #include "pack.rbinc"
1748 id_associated
= rb_make_internal_id();