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