Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
pack.c
1/**********************************************************************
2
3 pack.c -
4
5 $Author$
6 created at: Thu Feb 10 15:17:05 JST 1994
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <ctype.h>
15#include <errno.h>
16#include <float.h>
17#include <sys/types.h>
18
19#include "internal.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"
26
27#include "builtin.h"
28
29/*
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.
36 */
37#ifdef HAVE_TRUE_LONG_LONG
38static const char natstr[] = "sSiIlLqQjJ";
39# define endstr natstr
40#else
41static const char natstr[] = "sSiIlLjJ";
42static const char endstr[] = "sSiIlLqQjJ";
43#endif
44
45#ifdef HAVE_TRUE_LONG_LONG
46/* It is intentional to use long long instead of LONG_LONG. */
47# define NATINT_LEN_Q NATINT_LEN(long long, 8)
48#else
49# define NATINT_LEN_Q 8
50#endif
51
52#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
53# define NATINT_PACK
54#endif
55
56#ifdef DYNAMIC_ENDIAN
57/* for universal binary of NEXTSTEP and MacOS X */
58/* useless since autoconf 2.63? */
59static int
60is_bigendian(void)
61{
62 static int init = 0;
63 static int endian_value;
64 char *p;
65
66 if (init) return endian_value;
67 init = 1;
68 p = (char*)&init;
69 return endian_value = p[0]?0:1;
70}
71# define BIGENDIAN_P() (is_bigendian())
72#elif defined(WORDS_BIGENDIAN)
73# define BIGENDIAN_P() 1
74#else
75# define BIGENDIAN_P() 0
76#endif
77
78#ifdef NATINT_PACK
79# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
80#else
81# define NATINT_LEN(type,len) ((int)sizeof(type))
82#endif
83
84typedef union {
85 float f;
86 uint32_t u;
87 char buf[4];
89typedef union {
90 double d;
91 uint64_t u;
92 char buf[8];
94#define swapf(x) swap32(x)
95#define swapd(x) swap64(x)
96
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))
105
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))
111
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))
117
118#define MAX_INTEGER_PACK_SIZE 8
119
120static const char toofew[] = "too few arguments";
121static const char intoitself[] = "cannot pack buffer object into itself";
122
123static void encodes(VALUE,const char*,long,int,int);
124static void qpencode(VALUE,VALUE,long);
125
126static unsigned long utf8_to_uv(const char*,long*);
127
128static ID id_associated;
129
130static void
131str_associate(VALUE str, VALUE add)
132{
133 /* assert(NIL_P(rb_attr_get(str, id_associated))); */
134 rb_ivar_set(str, id_associated, add);
135}
136
137static VALUE
138str_associated(VALUE str)
139{
140 VALUE associates = rb_ivar_lookup(str, id_associated, Qfalse);
141 if (!associates)
142 rb_raise(rb_eArgError, "no associated pointer");
143 return associates;
144}
145
146static VALUE
147associated_pointer(VALUE associates, const char *t)
148{
149 const VALUE *p = RARRAY_CONST_PTR(associates);
150 const VALUE *pend = p + RARRAY_LEN(associates);
151 for (; p < pend; p++) {
152 VALUE tmp = *p;
153 if (RB_TYPE_P(tmp, T_STRING) && RSTRING_PTR(tmp) == t) return tmp;
154 }
155 rb_raise(rb_eArgError, "non associated pointer");
157}
158
160static void
161unknown_directive(const char *mode, char type, VALUE fmt)
162{
163 char unknown[5];
164
165 if (ISPRINT(type)) {
166 unknown[0] = type;
167 unknown[1] = '\0';
168 }
169 else {
170 snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
171 }
172 fmt = rb_str_quote_unprintable(fmt);
173 rb_raise(rb_eArgError, "unknown %s directive '%s' in '%"PRIsVALUE"'",
174 mode, unknown, fmt);
175}
176
177static float
178VALUE_to_float(VALUE obj)
179{
180 VALUE v = rb_to_float(obj);
181 double d = RFLOAT_VALUE(v);
182
183 if (isnan(d)) {
184 return NAN;
185 }
186 else if (d < -FLT_MAX) {
187 return -INFINITY;
188 }
189 else if (d <= FLT_MAX) {
190 return d;
191 }
192 else {
193 return INFINITY;
194 }
195}
196
197static void
198str_expand_fill(VALUE res, int c, long len)
199{
200 long olen = RSTRING_LEN(res);
201 memset(RSTRING_PTR(res) + olen, c, len);
202 rb_str_set_len(res, olen + len);
203}
204
205static char *
206skip_to_eol(const char *p, const char *pend)
207{
208 p = memchr(p, '\n', pend - p);
209 return (char *)(p ? p + 1 : pend);
210}
211
212#define skip_blank(p, type) \
213 (ISSPACE(type) || (type == '#' && (p = skip_to_eol(p, pend), 1)))
214
215#ifndef NATINT_PACK
216# define pack_modifiers(p, pe, t, n, e) pack_modifiers(p, pe, t, e)
217#endif
218static char *
219pack_modifiers(const char *p, const char *pend, char type, int *natint, int *explicit_endian)
220{
221 while (p < pend) {
222 switch (*p) {
223 case '_':
224 case '!':
225 if (strchr(natstr, type)) {
226#ifdef NATINT_PACK
227 *natint = 1;
228#endif
229 p++;
230 }
231 else {
232 rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
233 }
234 break;
235
236 case '<':
237 case '>':
238 if (!strchr(endstr, type)) {
239 rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
240 }
241 if (*explicit_endian) {
242 rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
243 }
244 *explicit_endian = *p++;
245 break;
246 default:
247 return (char *)p;
248 }
249 }
250 return (char *)p;
251}
252
253static VALUE
254pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
255{
256 const char *p, *pend;
257 VALUE res, from, associates = 0;
258 long len, idx, plen;
259 const char *ptr;
260 int enc_info = 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
261 int integer_size, bigendian_p;
262
263 StringValue(fmt);
265 p = RSTRING_PTR(fmt);
266 pend = p + RSTRING_LEN(fmt);
267
268 if (NIL_P(buffer)) {
269 res = rb_str_buf_new(0);
270 }
271 else {
272 if (!RB_TYPE_P(buffer, T_STRING))
273 rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer));
274 rb_str_modify(buffer);
275 res = buffer;
276 }
277
278 idx = 0;
279
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))
286
287 while (p < pend) {
288 int explicit_endian = 0;
289 bool star = false;
290 if (RSTRING_END(fmt) != pend) {
291 rb_raise(rb_eRuntimeError, "format string modified");
292 }
293 const char type = *p++; /* get data type */
294#ifdef NATINT_PACK
295 int natint = 0; /* native integer */
296#endif
297
298 if (skip_blank(p, type)) continue;
299 p = pack_modifiers(p, pend, type, &natint, &explicit_endian);
300
301 if (*p == '*') { /* set data length */
302 star = true;
303 len = strchr("@Xxu", type) ? 0
304 : strchr("PMm", type) ? 1
305 : RARRAY_LEN(ary) - idx;
306 p++;
307 }
308 else if (ISDIGIT(*p)) {
309 errno = 0;
310 len = STRTOUL(p, (char**)&p, 10);
311 if (len < 0 || errno) {
312 rb_raise(rb_eRangeError, "pack length too big");
313 }
314 }
315 else {
316 len = 1;
317 }
318
319 switch (type) {
320 case 'U':
321 /* if encoding is US-ASCII, upgrade to UTF-8 */
322 if (enc_info == 1) enc_info = 2;
323 break;
324 case 'm': case 'M': case 'u':
325 /* keep US-ASCII (do nothing) */
326 break;
327 default:
328 /* fall back to BINARY */
329 enc_info = 0;
330 break;
331 }
332 switch (type) {
333 case 'A': case 'a': case 'Z':
334 case 'B': case 'b':
335 case 'H': case 'h':
336 from = NEXTFROM;
337 if (NIL_P(from)) {
338 ptr = "";
339 plen = 0;
340 }
341 else {
342 STR_FROM(from);
343 ptr = RSTRING_PTR(from);
344 plen = RSTRING_LEN(from);
345 }
346
347 /* The conversion of from with #to_str may have modified fmt,
348 * so p may be a dangling pointer; use star instead of p[-1]. */
349 if (star)
350 len = plen;
351
352 switch (type) {
353 case 'a': /* arbitrary binary string (null padded) */
354 case 'A': /* arbitrary binary string (ASCII space padded) */
355 case 'Z': /* null terminated string */
356 if (plen >= len) {
357 rb_str_buf_cat(res, ptr, len);
358 if (star && type == 'Z')
359 rb_str_buf_cat(res, "", 1);
360 }
361 else {
363 rb_str_buf_cat(res, ptr, plen);
364 str_expand_fill(res, (type == 'A' ? ' ' : '\0'), len - plen);
365 }
366 break;
367
368#define castchar(from) (char)((from) & 0xff)
369
370 case 'b': /* bit string (ascending) */
371 {
372 int byte = 0;
373 long i, j = 0;
374
375 if (len > plen) {
376 j = (len - plen + 1)/2;
377 len = plen;
378 }
379 for (i=0; i++ < len; ptr++) {
380 if (*ptr & 1)
381 byte |= 128;
382 if (i & 7)
383 byte >>= 1;
384 else {
385 char c = castchar(byte);
386 rb_str_buf_cat(res, &c, 1);
387 byte = 0;
388 }
389 }
390 if (len & 7) {
391 char c;
392 byte >>= 7 - (len & 7);
393 c = castchar(byte);
394 rb_str_buf_cat(res, &c, 1);
395 }
396 len = j;
397 goto grow;
398 }
399 break;
400
401 case 'B': /* bit string (descending) */
402 {
403 int byte = 0;
404 long i, j = 0;
405
406 if (len > plen) {
407 j = (len - plen + 1)/2;
408 len = plen;
409 }
410 for (i=0; i++ < len; ptr++) {
411 byte |= *ptr & 1;
412 if (i & 7)
413 byte <<= 1;
414 else {
415 char c = castchar(byte);
416 rb_str_buf_cat(res, &c, 1);
417 byte = 0;
418 }
419 }
420 if (len & 7) {
421 char c;
422 byte <<= 7 - (len & 7);
423 c = castchar(byte);
424 rb_str_buf_cat(res, &c, 1);
425 }
426 len = j;
427 goto grow;
428 }
429 break;
430
431 case 'h': /* hex string (low nibble first) */
432 {
433 int byte = 0;
434 long i, j = 0;
435
436 if (len > plen) {
437 j = (len + 1) / 2 - (plen + 1) / 2;
438 len = plen;
439 }
440 for (i=0; i++ < len; ptr++) {
441 if (ISALPHA(*ptr))
442 byte |= (((*ptr & 15) + 9) & 15) << 4;
443 else
444 byte |= (*ptr & 15) << 4;
445 if (i & 1)
446 byte >>= 4;
447 else {
448 char c = castchar(byte);
449 rb_str_buf_cat(res, &c, 1);
450 byte = 0;
451 }
452 }
453 if (len & 1) {
454 char c = castchar(byte);
455 rb_str_buf_cat(res, &c, 1);
456 }
457 len = j;
458 goto grow;
459 }
460 break;
461
462 case 'H': /* hex string (high nibble first) */
463 {
464 int byte = 0;
465 long i, j = 0;
466
467 if (len > plen) {
468 j = (len + 1) / 2 - (plen + 1) / 2;
469 len = plen;
470 }
471 for (i=0; i++ < len; ptr++) {
472 if (ISALPHA(*ptr))
473 byte |= ((*ptr & 15) + 9) & 15;
474 else
475 byte |= *ptr & 15;
476 if (i & 1)
477 byte <<= 4;
478 else {
479 char c = castchar(byte);
480 rb_str_buf_cat(res, &c, 1);
481 byte = 0;
482 }
483 }
484 if (len & 1) {
485 char c = castchar(byte);
486 rb_str_buf_cat(res, &c, 1);
487 }
488 len = j;
489 goto grow;
490 }
491 break;
492 }
493 break;
494
495 case 'c': /* signed char */
496 case 'C': /* unsigned char */
497 integer_size = 1;
498 bigendian_p = BIGENDIAN_P(); /* not effective */
499 goto pack_integer;
500
501 case 's': /* s for int16_t, s! for signed short */
502 case 'S': /* S for uint16_t, S! for unsigned short */
503 integer_size = NATINT_LEN(short, 2);
504 bigendian_p = BIGENDIAN_P();
505 goto pack_integer;
506
507 case 'i': /* i and i! for signed int */
508 case 'I': /* I and I! for unsigned int */
509 integer_size = (int)sizeof(int);
510 bigendian_p = BIGENDIAN_P();
511 goto pack_integer;
512
513 case 'l': /* l for int32_t, l! for signed long */
514 case 'L': /* L for uint32_t, L! for unsigned long */
515 integer_size = NATINT_LEN(long, 4);
516 bigendian_p = BIGENDIAN_P();
517 goto pack_integer;
518
519 case 'q': /* q for int64_t, q! for signed long long */
520 case 'Q': /* Q for uint64_t, Q! for unsigned long long */
521 integer_size = NATINT_LEN_Q;
522 bigendian_p = BIGENDIAN_P();
523 goto pack_integer;
524
525 case 'j': /* j for intptr_t */
526 integer_size = sizeof(intptr_t);
527 bigendian_p = BIGENDIAN_P();
528 goto pack_integer;
529
530 case 'J': /* J for uintptr_t */
531 integer_size = sizeof(uintptr_t);
532 bigendian_p = BIGENDIAN_P();
533 goto pack_integer;
534
535 case 'n': /* 16 bit (2 bytes) integer (network byte-order) */
536 integer_size = 2;
537 bigendian_p = 1;
538 goto pack_integer;
539
540 case 'N': /* 32 bit (4 bytes) integer (network byte-order) */
541 integer_size = 4;
542 bigendian_p = 1;
543 goto pack_integer;
544
545 case 'v': /* 16 bit (2 bytes) integer (VAX byte-order) */
546 integer_size = 2;
547 bigendian_p = 0;
548 goto pack_integer;
549
550 case 'V': /* 32 bit (4 bytes) integer (VAX byte-order) */
551 integer_size = 4;
552 bigendian_p = 0;
553 goto pack_integer;
554
555 pack_integer:
556 if (explicit_endian) {
557 bigendian_p = explicit_endian == '>';
558 }
559 if (integer_size > MAX_INTEGER_PACK_SIZE)
560 rb_bug("unexpected integer size for pack: %d", integer_size);
561 while (len-- > 0) {
562 char intbuf[MAX_INTEGER_PACK_SIZE];
563
564 from = NEXTFROM;
565 rb_integer_pack(from, intbuf, integer_size, 1, 0,
568 rb_str_buf_cat(res, intbuf, integer_size);
569 }
570 break;
571
572 case 'f': /* single precision float in native format */
573 case 'F': /* ditto */
574 while (len-- > 0) {
575 float f;
576
577 from = NEXTFROM;
578 f = VALUE_to_float(from);
579 rb_str_buf_cat(res, (char*)&f, sizeof(float));
580 }
581 break;
582
583 case 'e': /* single precision float in VAX byte-order */
584 while (len-- > 0) {
585 FLOAT_CONVWITH(tmp);
586
587 from = NEXTFROM;
588 tmp.f = VALUE_to_float(from);
589 HTOVF(tmp);
590 rb_str_buf_cat(res, tmp.buf, sizeof(float));
591 }
592 break;
593
594 case 'E': /* double precision float in VAX byte-order */
595 while (len-- > 0) {
596 DOUBLE_CONVWITH(tmp);
597 from = NEXTFROM;
598 tmp.d = RFLOAT_VALUE(rb_to_float(from));
599 HTOVD(tmp);
600 rb_str_buf_cat(res, tmp.buf, sizeof(double));
601 }
602 break;
603
604 case 'd': /* double precision float in native format */
605 case 'D': /* ditto */
606 while (len-- > 0) {
607 double d;
608
609 from = NEXTFROM;
610 d = RFLOAT_VALUE(rb_to_float(from));
611 rb_str_buf_cat(res, (char*)&d, sizeof(double));
612 }
613 break;
614
615 case 'g': /* single precision float in network byte-order */
616 while (len-- > 0) {
617 FLOAT_CONVWITH(tmp);
618 from = NEXTFROM;
619 tmp.f = VALUE_to_float(from);
620 HTONF(tmp);
621 rb_str_buf_cat(res, tmp.buf, sizeof(float));
622 }
623 break;
624
625 case 'G': /* double precision float in network byte-order */
626 while (len-- > 0) {
627 DOUBLE_CONVWITH(tmp);
628
629 from = NEXTFROM;
630 tmp.d = RFLOAT_VALUE(rb_to_float(from));
631 HTOND(tmp);
632 rb_str_buf_cat(res, tmp.buf, sizeof(double));
633 }
634 break;
635
636 case 'x': /* null byte */
637 grow:
639 str_expand_fill(res, '\0', len);
640 break;
641
642 case 'X': /* back up byte */
643 shrink:
644 plen = RSTRING_LEN(res);
645 if (plen < len)
646 rb_raise(rb_eArgError, "X outside of string");
647 rb_str_set_len(res, plen - len);
648 break;
649
650 case '@': /* null fill to absolute position */
651 len -= RSTRING_LEN(res);
652 if (len > 0) goto grow;
653 len = -len;
654 if (len > 0) goto shrink;
655 break;
656
657 case '%':
658 rb_raise(rb_eArgError, "%% is not supported");
659 break;
660
661 case 'U': /* Unicode character */
662 while (len-- > 0) {
663 SIGNED_VALUE l;
664 char buf[8];
665 int le;
666
667 from = NEXTFROM;
668 from = rb_to_int(from);
669 l = NUM2LONG(from);
670 if (l < 0) {
671 rb_raise(rb_eRangeError, "pack(U): value out of range");
672 }
673 le = rb_uv_to_utf8(buf, l);
674 rb_str_buf_cat(res, (char*)buf, le);
675 }
676 break;
677
678 case 'u': /* uuencoded string */
679 case 'm': /* base64 encoded string */
680 from = NEXTFROM;
681 STR_FROM(from);
682 ptr = RSTRING_PTR(from);
683 plen = RSTRING_LEN(from);
684
685 if (len == 0 && type == 'm') {
686 encodes(res, ptr, plen, type, 0);
687 ptr += plen;
688 break;
689 }
690 if (len <= 2)
691 len = 45;
692 else if (len > 63 && type == 'u')
693 len = 63;
694 else
695 len = len / 3 * 3;
696 while (plen > 0) {
697 long todo;
698
699 if (plen > len)
700 todo = len;
701 else
702 todo = plen;
703 encodes(res, ptr, todo, type, 1);
704 plen -= todo;
705 ptr += todo;
706 }
707 break;
708
709 case 'M': /* quoted-printable encoded string */
710 from = rb_obj_as_string(NEXTFROM);
711 NOT_BUFFER(from);
712 if (len <= 1)
713 len = 72;
714 qpencode(res, from, len);
715 break;
716
717 case 'P': /* pointer to packed byte string */
718 from = THISFROM;
719 if (!NIL_P(from)) {
720 STR_FROM(from);
721 if (RSTRING_LEN(from) < len) {
722 rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
723 RSTRING_LEN(from), len);
724 }
725 }
726 len = 1;
727 /* FALL THROUGH */
728 case 'p': /* pointer to string */
729 while (len-- > 0) {
730 char *t = 0;
731 from = NEXTFROM;
732 if (!NIL_P(from)) {
733 STR_FROM(from);
734 t = RSTRING_PTR(from);
735 }
736 if (!associates) {
737 associates = rb_ary_new();
738 }
739 rb_ary_push(associates, from);
740 rb_str_buf_cat(res, (char*)&t, sizeof(char*));
741 }
742 break;
743
744 case 'w': /* BER compressed integer */
745 while (len-- > 0) {
746 VALUE buf = rb_str_new(0, 0);
747 size_t numbytes;
748 int sign;
749 char *cp;
750
751 from = NEXTFROM;
752 from = rb_to_int(from);
753 numbytes = rb_absint_numwords(from, 7, NULL);
754 if (numbytes == 0)
755 numbytes = 1;
756 buf = rb_str_new(NULL, numbytes);
757
758 sign = rb_integer_pack(from, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
759
760 if (sign < 0)
761 rb_raise(rb_eArgError, "can't compress negative numbers");
762 if (sign == 2)
763 rb_bug("buffer size problem?");
764
765 cp = RSTRING_PTR(buf);
766 while (1 < numbytes) {
767 *cp |= 0x80;
768 cp++;
769 numbytes--;
770 }
771
772 rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf));
773 }
774 break;
775
776 default: {
777 unknown_directive("pack", type, fmt);
778 break;
779 }
780 }
781 }
782
783 if (associates) {
784 str_associate(res, associates);
785 }
786 switch (enc_info) {
787 case 1:
788 ENCODING_CODERANGE_SET(res, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
789 break;
790 case 2:
791 rb_enc_set_index(res, rb_utf8_encindex());
792 break;
793 default:
794 /* do nothing, keep ASCII-8BIT */
795 break;
796 }
797 return res;
798}
799
800VALUE
801rb_ec_pack_ary(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
802{
803 return pack_pack(ec, ary, fmt, buffer);
804}
805
806static const char uu_table[] =
807"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
808static const char b64_table[] =
809"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
810
811static void
812encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
813{
814 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
815 char buff[buff_size + 1]; /* +1 for tail_lf */
816 long i = 0;
817 const char *const trans = type == 'u' ? uu_table : b64_table;
818 char padding;
819 const unsigned char *s = (const unsigned char *)s0;
820
821 if (type == 'u') {
822 buff[i++] = (char)len + ' ';
823 padding = '`';
824 }
825 else {
826 padding = '=';
827 }
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]];
834 s += input_unit;
835 len -= input_unit;
836 }
837 if (buff_size-i < encoded_unit) {
838 rb_str_buf_cat(str, buff, i);
839 i = 0;
840 }
841 }
842
843 if (len == 2) {
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))];
847 buff[i++] = padding;
848 }
849 else if (len == 1) {
850 buff[i++] = trans[077 & (*s >> 2)];
851 buff[i++] = trans[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
852 buff[i++] = padding;
853 buff[i++] = padding;
854 }
855 if (tail_lf) buff[i++] = '\n';
856 rb_str_buf_cat(str, buff, i);
857 if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
858}
859
860static const char hex_table[] = "0123456789ABCDEF";
861
862static void
863qpencode(VALUE str, VALUE from, long len)
864{
865 char buff[1024];
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);
869
870 while (s < send) {
871 if ((*s > 126) ||
872 (*s < 32 && *s != '\n' && *s != '\t') ||
873 (*s == '=')) {
874 buff[i++] = '=';
875 buff[i++] = hex_table[*s >> 4];
876 buff[i++] = hex_table[*s & 0x0f];
877 n += 3;
878 prev = EOF;
879 }
880 else if (*s == '\n') {
881 if (prev == ' ' || prev == '\t') {
882 buff[i++] = '=';
883 buff[i++] = *s;
884 }
885 buff[i++] = *s;
886 n = 0;
887 prev = *s;
888 }
889 else {
890 buff[i++] = *s;
891 n++;
892 prev = *s;
893 }
894 if (n > len) {
895 buff[i++] = '=';
896 buff[i++] = '\n';
897 n = 0;
898 prev = '\n';
899 }
900 if (i > 1024 - 5) {
901 rb_str_buf_cat(str, buff, i);
902 i = 0;
903 }
904 s++;
905 }
906 if (n > 0) {
907 buff[i++] = '=';
908 buff[i++] = '\n';
909 }
910 if (i > 0) {
911 rb_str_buf_cat(str, buff, i);
912 }
913}
914
915static inline int
916hex2num(char c)
917{
918 int n;
919 n = ruby_digit36_to_number_table[(unsigned char)c];
920 if (16 <= n)
921 n = -1;
922 return n;
923}
924
925#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
926 tmp_len = 0; \
927 if (len > (long)((send-s)/(sz))) { \
928 if (!star) { \
929 tmp_len = len-(send-s)/(sz); \
930 } \
931 len = (send-s)/(sz); \
932 } \
933} while (0)
934
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); \
938} while (0)
939
940/* Workaround for Oracle Developer Studio (Oracle Solaris Studio)
941 * 12.4/12.5/12.6 C compiler optimization bug
942 * with "-xO4" optimization option.
943 */
944#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
945# define AVOID_CC_BUG volatile
946#else
947# define AVOID_CC_BUG
948#endif
949
950enum unpack_mode {
951 UNPACK_ARRAY,
952 UNPACK_BLOCK,
953 UNPACK_1
954};
955
956static VALUE
957pack_unpack_internal(VALUE str, VALUE fmt, enum unpack_mode mode, long offset)
958{
959#define hexdigits ruby_hexdigits
960 char *s, *send;
961 char *p, *pend;
962 VALUE ary, associates = Qfalse;
963 long len;
964 AVOID_CC_BUG long tmp_len;
965 int signed_p, integer_size, bigendian_p;
966 const char *sptr;
967 long slen;
968 const char *fptr;
969 long flen;
970#define UNPACK_PUSH(item) do {\
971 VALUE item_val = (item);\
972 if ((mode) == UNPACK_BLOCK) {\
973 rb_yield(item_val);\
974 /* The block may have modified str and invalidated s */ \
975 if (RSTRING_PTR(str) != sptr || RSTRING_LEN(str) != slen) {\
976 rb_raise(rb_eRuntimeError, "string modified");\
977 }\
978 /* The block may have also modified fmt and invalidated p */ \
979 if (RSTRING_PTR(fmt) != fptr || RSTRING_LEN(fmt) != flen) {\
980 rb_raise(rb_eRuntimeError, "format string modified");\
981 }\
982 }\
983 else if ((mode) == UNPACK_ARRAY) {\
984 rb_ary_push(ary, item_val);\
985 }\
986 else /* if ((mode) == UNPACK_1) { */ {\
987 return item_val; \
988 }\
989 } while (0)
990
991 StringValue(str);
992 StringValue(fmt);
994
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");
998
999 s = RSTRING_PTR(str);
1000 send = s + len;
1001 sptr = s;
1002 slen = len;
1003 s += offset;
1004
1005 p = RSTRING_PTR(fmt);
1006 pend = p + RSTRING_LEN(fmt);
1007 fptr = p;
1008 flen = RSTRING_LEN(fmt);
1009
1010#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1011
1012 ary = mode == UNPACK_ARRAY ? rb_ary_new() : Qnil;
1013 while (p < pend) {
1014 int explicit_endian = 0;
1015 const char type = *p++;
1016#ifdef NATINT_PACK
1017 int natint = 0; /* native integer */
1018#endif
1019 int star = 0;
1020
1021 if (skip_blank(p, type)) continue;
1022 p = pack_modifiers(p, pend, type, &natint, &explicit_endian);
1023
1024 if (p >= pend)
1025 len = 1;
1026 else if (*p == '*') {
1027 star = 1;
1028 len = send - s;
1029 p++;
1030 }
1031 else if (ISDIGIT(*p)) {
1032 errno = 0;
1033 len = STRTOUL(p, (char**)&p, 10);
1034 if (len < 0 || errno) {
1035 rb_raise(rb_eRangeError, "pack length too big");
1036 }
1037 }
1038 else {
1039 len = (type != '@');
1040 }
1041
1042 switch (type) {
1043 case '%':
1044 rb_raise(rb_eArgError, "%% is not supported");
1045 break;
1046
1047 case 'A':
1048 if (len > send - s) len = send - s;
1049 {
1050 long end = len;
1051 char *t = s + len - 1;
1052
1053 while (t >= s) {
1054 if (*t != ' ' && *t != '\0') break;
1055 t--; len--;
1056 }
1057 UNPACK_PUSH(rb_str_new(s, len));
1058 s += end;
1059 }
1060 break;
1061
1062 case 'Z':
1063 {
1064 char *t = s;
1065
1066 if (len > send-s) len = send-s;
1067 while (t < s+len && *t) t++;
1068 UNPACK_PUSH(rb_str_new(s, t-s));
1069 if (t < send) t++;
1070 s = star ? t : s+len;
1071 }
1072 break;
1073
1074 case 'a':
1075 if (len > send - s) len = send - s;
1076 UNPACK_PUSH(rb_str_new(s, len));
1077 s += len;
1078 break;
1079
1080 case 'b':
1081 {
1082 VALUE bitstr;
1083 char *t;
1084 int bits;
1085 long i;
1086
1087 if (p[-1] == '*' || len > (send - s) * 8)
1088 len = (send - s) * 8;
1089 bits = 0;
1090 bitstr = rb_usascii_str_new(0, len);
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';
1096 }
1097 UNPACK_PUSH(bitstr);
1098 }
1099 break;
1100
1101 case 'B':
1102 {
1103 VALUE bitstr;
1104 char *t;
1105 int bits;
1106 long i;
1107
1108 if (p[-1] == '*' || len > (send - s) * 8)
1109 len = (send - s) * 8;
1110 bits = 0;
1111 bitstr = rb_usascii_str_new(0, len);
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';
1117 }
1118 UNPACK_PUSH(bitstr);
1119 }
1120 break;
1121
1122 case 'h':
1123 {
1124 VALUE bitstr;
1125 char *t;
1126 int bits;
1127 long i;
1128
1129 if (p[-1] == '*' || len > (send - s) * 2)
1130 len = (send - s) * 2;
1131 bits = 0;
1132 bitstr = rb_usascii_str_new(0, len);
1133 t = RSTRING_PTR(bitstr);
1134 for (i=0; i<len; i++) {
1135 if (i & 1)
1136 bits >>= 4;
1137 else
1138 bits = (unsigned char)*s++;
1139 *t++ = hexdigits[bits & 15];
1140 }
1141 UNPACK_PUSH(bitstr);
1142 }
1143 break;
1144
1145 case 'H':
1146 {
1147 VALUE bitstr;
1148 char *t;
1149 int bits;
1150 long i;
1151
1152 if (p[-1] == '*' || len > (send - s) * 2)
1153 len = (send - s) * 2;
1154 bits = 0;
1155 bitstr = rb_usascii_str_new(0, len);
1156 t = RSTRING_PTR(bitstr);
1157 for (i=0; i<len; i++) {
1158 if (i & 1)
1159 bits <<= 4;
1160 else
1161 bits = (unsigned char)*s++;
1162 *t++ = hexdigits[(bits >> 4) & 15];
1163 }
1164 UNPACK_PUSH(bitstr);
1165 }
1166 break;
1167
1168 case 'c':
1169 signed_p = 1;
1170 integer_size = 1;
1171 bigendian_p = BIGENDIAN_P(); /* not effective */
1172 goto unpack_integer;
1173
1174 case 'C':
1175 signed_p = 0;
1176 integer_size = 1;
1177 bigendian_p = BIGENDIAN_P(); /* not effective */
1178 goto unpack_integer;
1179
1180 case 's':
1181 signed_p = 1;
1182 integer_size = NATINT_LEN(short, 2);
1183 bigendian_p = BIGENDIAN_P();
1184 goto unpack_integer;
1185
1186 case 'S':
1187 signed_p = 0;
1188 integer_size = NATINT_LEN(short, 2);
1189 bigendian_p = BIGENDIAN_P();
1190 goto unpack_integer;
1191
1192 case 'i':
1193 signed_p = 1;
1194 integer_size = (int)sizeof(int);
1195 bigendian_p = BIGENDIAN_P();
1196 goto unpack_integer;
1197
1198 case 'I':
1199 signed_p = 0;
1200 integer_size = (int)sizeof(int);
1201 bigendian_p = BIGENDIAN_P();
1202 goto unpack_integer;
1203
1204 case 'l':
1205 signed_p = 1;
1206 integer_size = NATINT_LEN(long, 4);
1207 bigendian_p = BIGENDIAN_P();
1208 goto unpack_integer;
1209
1210 case 'L':
1211 signed_p = 0;
1212 integer_size = NATINT_LEN(long, 4);
1213 bigendian_p = BIGENDIAN_P();
1214 goto unpack_integer;
1215
1216 case 'q':
1217 signed_p = 1;
1218 integer_size = NATINT_LEN_Q;
1219 bigendian_p = BIGENDIAN_P();
1220 goto unpack_integer;
1221
1222 case 'Q':
1223 signed_p = 0;
1224 integer_size = NATINT_LEN_Q;
1225 bigendian_p = BIGENDIAN_P();
1226 goto unpack_integer;
1227
1228 case 'j':
1229 signed_p = 1;
1230 integer_size = sizeof(intptr_t);
1231 bigendian_p = BIGENDIAN_P();
1232 goto unpack_integer;
1233
1234 case 'J':
1235 signed_p = 0;
1236 integer_size = sizeof(uintptr_t);
1237 bigendian_p = BIGENDIAN_P();
1238 goto unpack_integer;
1239
1240 case 'n':
1241 signed_p = 0;
1242 integer_size = 2;
1243 bigendian_p = 1;
1244 goto unpack_integer;
1245
1246 case 'N':
1247 signed_p = 0;
1248 integer_size = 4;
1249 bigendian_p = 1;
1250 goto unpack_integer;
1251
1252 case 'v':
1253 signed_p = 0;
1254 integer_size = 2;
1255 bigendian_p = 0;
1256 goto unpack_integer;
1257
1258 case 'V':
1259 signed_p = 0;
1260 integer_size = 4;
1261 bigendian_p = 0;
1262 goto unpack_integer;
1263
1264 unpack_integer:
1265 if (explicit_endian) {
1266 bigendian_p = explicit_endian == '>';
1267 }
1268 PACK_LENGTH_ADJUST_SIZE(integer_size);
1269 while (len-- > 0) {
1270 int flags = bigendian_p ? INTEGER_PACK_BIG_ENDIAN : INTEGER_PACK_LITTLE_ENDIAN;
1271 VALUE val;
1272 if (signed_p)
1273 flags |= INTEGER_PACK_2COMP;
1274 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1275 UNPACK_PUSH(val);
1276 s += integer_size;
1277 }
1278 PACK_ITEM_ADJUST();
1279 break;
1280
1281 case 'f':
1282 case 'F':
1283 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1284 while (len-- > 0) {
1285 float tmp;
1286 UNPACK_FETCH(&tmp, float);
1287 UNPACK_PUSH(DBL2NUM((double)tmp));
1288 }
1289 PACK_ITEM_ADJUST();
1290 break;
1291
1292 case 'e':
1293 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1294 while (len-- > 0) {
1295 FLOAT_CONVWITH(tmp);
1296 UNPACK_FETCH(tmp.buf, float);
1297 VTOHF(tmp);
1298 UNPACK_PUSH(DBL2NUM(tmp.f));
1299 }
1300 PACK_ITEM_ADJUST();
1301 break;
1302
1303 case 'E':
1304 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1305 while (len-- > 0) {
1306 DOUBLE_CONVWITH(tmp);
1307 UNPACK_FETCH(tmp.buf, double);
1308 VTOHD(tmp);
1309 UNPACK_PUSH(DBL2NUM(tmp.d));
1310 }
1311 PACK_ITEM_ADJUST();
1312 break;
1313
1314 case 'D':
1315 case 'd':
1316 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1317 while (len-- > 0) {
1318 double tmp;
1319 UNPACK_FETCH(&tmp, double);
1320 UNPACK_PUSH(DBL2NUM(tmp));
1321 }
1322 PACK_ITEM_ADJUST();
1323 break;
1324
1325 case 'g':
1326 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1327 while (len-- > 0) {
1328 FLOAT_CONVWITH(tmp);
1329 UNPACK_FETCH(tmp.buf, float);
1330 NTOHF(tmp);
1331 UNPACK_PUSH(DBL2NUM(tmp.f));
1332 }
1333 PACK_ITEM_ADJUST();
1334 break;
1335
1336 case 'G':
1337 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1338 while (len-- > 0) {
1339 DOUBLE_CONVWITH(tmp);
1340 UNPACK_FETCH(tmp.buf, double);
1341 NTOHD(tmp);
1342 UNPACK_PUSH(DBL2NUM(tmp.d));
1343 }
1344 PACK_ITEM_ADJUST();
1345 break;
1346
1347 case 'U':
1348 if (len > send - s) len = send - s;
1349 while (len > 0 && s < send) {
1350 long alen = send - s;
1351 unsigned long l;
1352
1353 l = utf8_to_uv(s, &alen);
1354 s += alen; len--;
1355 UNPACK_PUSH(ULONG2NUM(l));
1356 }
1357 break;
1358
1359 case 'u':
1360 {
1361 VALUE buf = rb_str_new(0, (send - s)*3/4);
1362 char *ptr = RSTRING_PTR(buf);
1363 long total = 0;
1364
1365 while (s < send && (unsigned char)*s > ' ' && (unsigned char)*s < 'a') {
1366 long a,b,c,d;
1367 char hunk[3];
1368
1369 len = ((unsigned char)*s++ - ' ') & 077;
1370
1371 total += len;
1372 if (total > RSTRING_LEN(buf)) {
1373 len -= total - RSTRING_LEN(buf);
1374 total = RSTRING_LEN(buf);
1375 }
1376
1377 while (len > 0) {
1378 long mlen = len > 3 ? 3 : len;
1379
1380 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1381 a = ((unsigned char)*s++ - ' ') & 077;
1382 else
1383 a = 0;
1384 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1385 b = ((unsigned char)*s++ - ' ') & 077;
1386 else
1387 b = 0;
1388 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1389 c = ((unsigned char)*s++ - ' ') & 077;
1390 else
1391 c = 0;
1392 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1393 d = ((unsigned char)*s++ - ' ') & 077;
1394 else
1395 d = 0;
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);
1400 ptr += mlen;
1401 len -= mlen;
1402 }
1403 if (s < send && (unsigned char)*s != '\r' && *s != '\n')
1404 s++; /* possible checksum byte */
1405 if (s < send && *s == '\r') s++;
1406 if (s < send && *s == '\n') s++;
1407 }
1408
1409 rb_str_set_len(buf, total);
1410 UNPACK_PUSH(buf);
1411 }
1412 break;
1413
1414 case 'm':
1415 {
1416 VALUE buf = rb_str_new(0, (send - s + 3) / 4 * 3); /* +3 is for skipping paddings */
1417 char *ptr = RSTRING_PTR(buf);
1418 int a = -1,b = -1,c = 0,d = 0;
1419 static signed char b64_xtable[256];
1420
1421 if (b64_xtable['/'] <= 0) {
1422 int i;
1423
1424 for (i = 0; i < 256; i++) {
1425 b64_xtable[i] = -1;
1426 }
1427 for (i = 0; i < 64; i++) {
1428 b64_xtable[(unsigned char)b64_table[i]] = (char)i;
1429 }
1430 }
1431 if (len == 0) {
1432 while (s < send) {
1433 a = b = c = d = -1;
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");
1438 if (*s == '=') {
1439 if (s + 2 == send && *(s + 1) == '=') break;
1440 rb_raise(rb_eArgError, "invalid base64");
1441 }
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);
1450 }
1451 if (c == -1) {
1452 *ptr++ = castchar(a << 2 | b >> 4);
1453 if (b & 0xf) rb_raise(rb_eArgError, "invalid base64");
1454 }
1455 else if (d == -1) {
1456 *ptr++ = castchar(a << 2 | b >> 4);
1457 *ptr++ = castchar(b << 4 | c >> 2);
1458 if (c & 0x3) rb_raise(rb_eArgError, "invalid base64");
1459 }
1460 }
1461 else {
1462 while (s < send) {
1463 a = b = c = d = -1;
1464 while ((a = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1465 if (s >= send) break;
1466 s++;
1467 while ((b = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1468 if (s >= send) break;
1469 s++;
1470 while ((c = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
1471 if (*s == '=' || s >= send) break;
1472 s++;
1473 while ((d = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
1474 if (*s == '=' || s >= send) break;
1475 s++;
1476 *ptr++ = castchar(a << 2 | b >> 4);
1477 *ptr++ = castchar(b << 4 | c >> 2);
1478 *ptr++ = castchar(c << 6 | d);
1479 a = -1;
1480 }
1481 if (a != -1 && b != -1) {
1482 if (c == -1)
1483 *ptr++ = castchar(a << 2 | b >> 4);
1484 else {
1485 *ptr++ = castchar(a << 2 | b >> 4);
1486 *ptr++ = castchar(b << 4 | c >> 2);
1487 }
1488 }
1489 }
1490 rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
1491 UNPACK_PUSH(buf);
1492 }
1493 break;
1494
1495 case 'M':
1496 {
1497 VALUE buf = rb_str_new(0, send - s);
1498 char *ptr = RSTRING_PTR(buf), *ss = s;
1499 int csum = 0;
1500 int c1, c2;
1501
1502 while (s < send) {
1503 if (*s == '=') {
1504 if (++s == send) break;
1505 if (s+1 < send && *s == '\r' && *(s+1) == '\n')
1506 s++;
1507 if (*s != '\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);
1512 }
1513 }
1514 else {
1515 csum |= *ptr++ = *s;
1516 }
1517 s++;
1518 ss = s;
1519 }
1520 rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
1521 rb_str_buf_cat(buf, ss, send-ss);
1522 csum = ISASCII(csum) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
1523 ENCODING_CODERANGE_SET(buf, rb_ascii8bit_encindex(), csum);
1524 UNPACK_PUSH(buf);
1525 }
1526 break;
1527
1528 case '@':
1529 if (len > RSTRING_LEN(str))
1530 rb_raise(rb_eArgError, "@ outside of string");
1531 s = RSTRING_PTR(str) + len;
1532 break;
1533
1534 case 'X':
1535 if (len > s - RSTRING_PTR(str))
1536 rb_raise(rb_eArgError, "X outside of string");
1537 s -= len;
1538 break;
1539
1540 case 'x':
1541 if (len > send - s)
1542 rb_raise(rb_eArgError, "x outside of string");
1543 s += len;
1544 break;
1545
1546 case 'P':
1547 if (sizeof(char *) <= (size_t)(send - s)) {
1548 VALUE tmp = Qnil;
1549 char *t;
1550
1551 UNPACK_FETCH(&t, char *);
1552 if (t) {
1553 if (!associates) associates = str_associated(str);
1554 tmp = associated_pointer(associates, t);
1555 if (len < RSTRING_LEN(tmp)) {
1556 tmp = rb_str_new(t, len);
1557 str_associate(tmp, associates);
1558 }
1559 }
1560 UNPACK_PUSH(tmp);
1561 }
1562 break;
1563
1564 case 'p':
1565 if (len > (long)((send - s) / sizeof(char *)))
1566 len = (send - s) / sizeof(char *);
1567 while (len-- > 0) {
1568 if ((size_t)(send - s) < sizeof(char *))
1569 break;
1570 else {
1571 VALUE tmp = Qnil;
1572 char *t;
1573
1574 UNPACK_FETCH(&t, char *);
1575 if (t) {
1576 if (!associates) associates = str_associated(str);
1577 tmp = associated_pointer(associates, t);
1578 }
1579 UNPACK_PUSH(tmp);
1580 }
1581 }
1582 break;
1583
1584 case 'w':
1585 {
1586 char *s0 = s;
1587 while (len > 0 && s < send) {
1588 if (*s & 0x80) {
1589 s++;
1590 }
1591 else {
1592 s++;
1593 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
1594 len--;
1595 s0 = s;
1596 }
1597 }
1598 }
1599 break;
1600
1601 default:
1602 unknown_directive("unpack", type, fmt);
1603 break;
1604 }
1605 }
1606
1607 return ary;
1608}
1609
1610static VALUE
1611pack_unpack(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
1612{
1613 enum unpack_mode mode = rb_block_given_p() ? UNPACK_BLOCK : UNPACK_ARRAY;
1614 return pack_unpack_internal(str, fmt, mode, RB_NUM2LONG(offset));
1615}
1616
1617static VALUE
1618pack_unpack1(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
1619{
1620 return pack_unpack_internal(str, fmt, UNPACK_1, RB_NUM2LONG(offset));
1621}
1622
1623int
1624rb_uv_to_utf8(char buf[6], unsigned long uv)
1625{
1626 if (uv <= 0x7f) {
1627 buf[0] = (char)uv;
1628 return 1;
1629 }
1630 if (uv <= 0x7ff) {
1631 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1632 buf[1] = castchar((uv&0x3f)|0x80);
1633 return 2;
1634 }
1635 if (uv <= 0xffff) {
1636 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1637 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1638 buf[2] = castchar((uv&0x3f)|0x80);
1639 return 3;
1640 }
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);
1646 return 4;
1647 }
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);
1654 return 5;
1655 }
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);
1663 return 6;
1664 }
1665 rb_raise(rb_eRangeError, "pack(U): value out of range");
1666
1668}
1669
1670static const unsigned long utf8_limits[] = {
1671 0x0, /* 1 */
1672 0x80, /* 2 */
1673 0x800, /* 3 */
1674 0x10000, /* 4 */
1675 0x200000, /* 5 */
1676 0x4000000, /* 6 */
1677 0x80000000, /* 7 */
1678};
1679
1680static unsigned long
1681utf8_to_uv(const char *p, long *lenp)
1682{
1683 int c = *p++ & 0xff;
1684 unsigned long uv = c;
1685 long n;
1686
1687 if (!(uv & 0x80)) {
1688 *lenp = 1;
1689 return uv;
1690 }
1691 if (!(uv & 0x40)) {
1692 *lenp = 1;
1693 rb_raise(rb_eArgError, "malformed UTF-8 character");
1694 }
1695
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; }
1701 else {
1702 *lenp = 1;
1703 rb_raise(rb_eArgError, "malformed UTF-8 character");
1704 }
1705 if (n > *lenp) {
1706 rb_raise(rb_eArgError, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1707 n, *lenp);
1708 }
1709 *lenp = n--;
1710 if (n != 0) {
1711 while (n--) {
1712 c = *p++ & 0xff;
1713 if ((c & 0xc0) != 0x80) {
1714 *lenp -= n + 1;
1715 rb_raise(rb_eArgError, "malformed UTF-8 character");
1716 }
1717 else {
1718 c &= 0x3f;
1719 uv = uv << 6 | c;
1720 }
1721 }
1722 }
1723 n = *lenp - 1;
1724 if (uv < utf8_limits[n]) {
1725 rb_raise(rb_eArgError, "redundant UTF-8 sequence");
1726 }
1727 return uv;
1728}
1729
1730#include "pack.rbinc"
1731
1732void
1733Init_pack(void)
1734{
1735 id_associated = rb_make_internal_id();
1736}
#define STRTOUL
@old{ruby_strtoul}
Definition ctype.h:104
#define ISDIGIT
@old{rb_isdigit}
Definition ctype.h:93
#define ISALPHA
@old{rb_isalpha}
Definition ctype.h:92
#define ISPRINT
@old{rb_isprint}
Definition ctype.h:86
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1027
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
Definition coderange.h:181
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#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.
Definition double.h:29
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define ENCODING_CODERANGE_SET(obj, encindex, cr)
Old name of RB_ENCODING_CODERANGE_SET.
Definition coderange.h:189
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1435
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1431
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1429
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
Definition object.c:3767
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3306
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.
Definition bignum.h:567
#define INTEGER_PACK_BIG_ENDIAN
Big endian combination.
Definition bignum.h:572
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Encodes a Unicode codepoint into its UTF-8 representation.
Definition pack.c:1624
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
Definition bignum.h:549
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1682
#define rb_usascii_str_new(str, len)
Identical to rb_str_new, except it generates a string of "US ASCII" encoding.
Definition string.h:1533
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3418
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2821
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2775
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1735
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1868
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.
Definition variable.c:2030
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
int len
Length of the buffer.
Definition io.h:8
const signed char ruby_digit36_to_number_table[]
Character to number mapping like 'a' -> 10, 'b' -> 11 etc.
Definition util.c:60
#define RB_NUM2LONG
Just another name of rb_num2long_inline.
Definition long.h:57
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]].
Definition noreturn.h:38
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:515
#define errno
Ractor-aware version of errno.
Definition ruby.h:388
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40