Ruby 4.1.0dev (2026-09-26 revision 57213d44ce7b1a31fc9648e9cd5eb0c4507f4a49)
pack.c (57213d44ce7b1a31fc9648e9cd5eb0c4507f4a49)
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/numeric.h"
23#include "internal/string.h"
24#include "internal/symbol.h"
25#include "internal/variable.h"
26#include "ruby/util.h"
27
28#include "builtin.h"
29
30/*
31 * It is intentional that the condition for natstr is HAVE_TRUE_LONG_LONG
32 * instead of HAVE_LONG_LONG or LONG_LONG.
33 * This means q! and Q! means always the standard long long type and
34 * causes ArgumentError for platforms which has no long long type,
35 * even if the platform has an implementation specific 64bit type.
36 * This behavior is consistent with the document of pack/unpack.
37 */
38#ifdef HAVE_TRUE_LONG_LONG
39static const char natstr[] = "sSiIlLqQjJ";
40# define endstr natstr
41#else
42static const char natstr[] = "sSiIlLjJ";
43static const char endstr[] = "sSiIlLqQjJ";
44#endif
45
46#ifdef HAVE_TRUE_LONG_LONG
47/* It is intentional to use long long instead of LONG_LONG. */
48# define NATINT_LEN_Q NATINT_LEN(long long, 8)
49#else
50# define NATINT_LEN_Q 8
51#endif
52
53#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
54# define NATINT_PACK
55#endif
56
57#ifdef DYNAMIC_ENDIAN
58/* for universal binary of NEXTSTEP and MacOS X */
59/* useless since autoconf 2.63? */
60static int
61is_bigendian(void)
62{
63 static const union {int i; char b[1];} endian_value = {1};
64 return !endian_value.b[0];
65}
66# define BIGENDIAN_P() (is_bigendian())
67#elif defined(WORDS_BIGENDIAN)
68# define BIGENDIAN_P() 1
69#else
70# define BIGENDIAN_P() 0
71#endif
72
73#ifdef NATINT_PACK
74# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
75# define NATINT_ALIGN(type,len) (natint?(int)RUBY_ALIGNOF(type):(int)(len))
76# define USING_NATINT(expr) expr
77#else
78# define NATINT_LEN(type,len) ((int)sizeof(type))
79# define NATINT_ALIGN(type,len) ((int)RUBY_ALIGNOF(type))
80# define USING_NATINT(expr) /* void */
81#endif
82
83typedef union {
84 float f;
85 uint32_t u;
86 char buf[4];
88typedef union {
89 double d;
90 uint64_t u;
91 char buf[8];
93#define swapf(x) swap32(x)
94#define swapd(x) swap64(x)
95
96#define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
97#define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
98#define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
99#define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
100#define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
101#define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
102#define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
103#define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
104
105#define FLOAT_CONVWITH(x) FLOAT_SWAPPER x;
106#define HTONF(x) ((x).u = rb_htonf((x).u))
107#define HTOVF(x) ((x).u = rb_htovf((x).u))
108#define NTOHF(x) ((x).u = rb_ntohf((x).u))
109#define VTOHF(x) ((x).u = rb_vtohf((x).u))
110
111#define DOUBLE_CONVWITH(x) DOUBLE_SWAPPER x;
112#define HTOND(x) ((x).u = rb_htond((x).u))
113#define HTOVD(x) ((x).u = rb_htovd((x).u))
114#define NTOHD(x) ((x).u = rb_ntohd((x).u))
115#define VTOHD(x) ((x).u = rb_vtohd((x).u))
116
117#define MAX_INTEGER_PACK_SIZE 8
118
119static const char toofew[] = "too few arguments";
120static const char intoitself[] = "cannot pack buffer object into itself";
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, pe, t, n, e) pack_modifiers(p, pe, t, e)
216#endif
217static const char *
218pack_modifiers(const char *p, const char *pend, char type, int *natint, int *explicit_endian)
219{
220 while (p < pend) {
221 switch (*p) {
222 case '_':
223 case '!':
224 if (strchr(natstr, type)) {
225 USING_NATINT(*natint = 1);
226 p++;
227 }
228 else {
229 rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
230 }
231 break;
232
233 case '<':
234 case '>':
235 if (!strchr(endstr, type)) {
236 rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
237 }
238 if (*explicit_endian) {
239 rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
240 }
241 *explicit_endian = *p++;
242 break;
243 default:
244 return (char *)p;
245 }
246 }
247 return p;
248}
249
250#ifndef NATINT_PACK
251# define pack_alignof(t, n) pack_alignof(t)
252#endif
253static size_t
254pack_alignof(char type, int natint)
255{
256 switch (type) {
257 case 'c': case 'C':
258 return RUBY_ALIGNOF(char);
259 case 's': case 'S':
260 return NATINT_ALIGN(short, 2);
261 case 'i': case 'I':
262 return RUBY_ALIGNOF(int);
263 case 'l': case 'L':
264 return NATINT_ALIGN(long, 4);
265 case 'q': case 'Q':
266 return RUBY_ALIGNOF(int64_t);
267 case 'j':
268 return RUBY_ALIGNOF(intptr_t);
269 case 'J':
270 return RUBY_ALIGNOF(uintptr_t);
271 case 'n': case 'v':
272 return RUBY_ALIGNOF(uint16_t);
273 case 'N': case 'V':
274 return RUBY_ALIGNOF(uint32_t);
275 case 'f': case 'F': case 'e': case 'g':
276 return RUBY_ALIGNOF(float);
277 case 'd': case 'D': case 'E': case 'G':
278 return RUBY_ALIGNOF(double);
279 case 'p': case 'P':
280 return RUBY_ALIGNOF(char *);
281 default:
282 return 0;
283 }
284}
285
286static long
287pack_align_pad(long pos, long base, size_t alignment)
288{
289 long offset, mod;
290
291 if (alignment <= 1) return 0;
292 if (alignment > LONG_MAX) rb_raise(rb_eRangeError, "alignment too big");
293 offset = pos - base;
294 mod = offset % (long)alignment;
295 if (mod < 0) mod += alignment;
296 return mod ? (long)alignment - mod : 0;
297}
298
299static char *
300pack_alignment(const char *p, const char *pend, VALUE fmt, size_t *alignment)
301{
302 char type;
303 int explicit_endian = 0;
304 USING_NATINT(int natint = 0);
305
306 if (p >= pend) {
307 rb_raise(rb_eArgError, "missing alignment");
308 }
309 type = *p++;
310 p = pack_modifiers(p, pend, type, &natint, &explicit_endian);
311 if (explicit_endian) {
312 rb_raise(rb_eArgError, "endian modifier is not allowed for alignment");
313 }
314 *alignment = pack_alignof(type, natint);
315 if (!*alignment) {
316 unknown_directive("alignment", type, fmt);
317 }
318 return (char *)p;
319}
320
321static char *
322pack_alignment_size(const char *p, const char *pend, VALUE fmt, size_t *alignment)
323{
324 if (p < pend && ISDIGIT(*p)) {
325 errno = 0;
326 *alignment = STRTOUL(p, (char**)&p, 10);
327 if (*alignment <= 0 || errno) {
328 rb_raise(rb_eRangeError, "invalid alignment");
329 }
330 return (char *)p;
331 }
332 return pack_alignment(p, pend, fmt, alignment);
333}
334
335static VALUE
336pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
337{
338 const char *p, *pend;
339 VALUE res, from, associates = 0;
340 long len, idx, plen;
341 const char *ptr;
342 int enc_info = 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
343 int integer_size, bigendian_p;
344 long align_base;
345
346 StringValue(fmt);
348 p = RSTRING_PTR(fmt);
349 pend = p + RSTRING_LEN(fmt);
350
351 if (NIL_P(buffer)) {
352 res = rb_str_buf_new(0);
353 }
354 else {
355 if (!RB_TYPE_P(buffer, T_STRING))
356 rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer));
357 rb_str_modify(buffer);
358 res = buffer;
359 }
360
361 idx = 0;
362 align_base = RSTRING_LEN(res);
363
364#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
365#define MORE_ITEM (idx < RARRAY_LEN(ary))
366#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
367#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
368#define NOT_BUFFER(val) (((val) == res) ? rb_raise(rb_eArgError, intoitself) : (void)0)
369#define STR_FROM(val) NOT_BUFFER(StringValue(val))
370
371 while (p < pend) {
372 int explicit_endian = 0;
373 size_t align = 0;
374 bool star = false;
375 if (RSTRING_END(fmt) != pend) {
376 rb_raise(rb_eRuntimeError, "format string modified");
377 }
378 const char type = *p++; /* get data type */
379 USING_NATINT(int natint = 0); /* native integer */
380
381 if (skip_blank(p, type)) continue;
382
383 /* Directives that do not take modifiers. */
384 if ((type == 'x' || type == '@') && p < pend && *p == '!') {
385 p++;
386 p = pack_alignment_size(p, pend, fmt, &align);
387 len = pack_align_pad(RSTRING_LEN(res), type == '@' ? 0 : align_base, align);
389 str_expand_fill(res, '\0', len);
390 continue;
391 }
392
393 p = pack_modifiers(p, pend, type, &natint, &explicit_endian);
394
395 if (*p == '*') { /* set data length */
396 star = true;
397 len = strchr("@Xxu", type) ? 0
398 : strchr("PMm", type) ? 1
399 : RARRAY_LEN(ary) - idx;
400 p++;
401 }
402 else if (ISDIGIT(*p)) {
403 errno = 0;
404 len = STRTOUL(p, (char**)&p, 10);
405 if (len < 0 || errno) {
406 rb_raise(rb_eRangeError, "pack length too big");
407 }
408 }
409 else {
410 len = 1;
411 }
412
413 switch (type) {
414 case 'U':
415 /* if encoding is US-ASCII, upgrade to UTF-8 */
416 if (enc_info == 1) enc_info = 2;
417 break;
418 case 'm': case 'M': case 'u':
419 /* keep US-ASCII (do nothing) */
420 break;
421 default:
422 /* fall back to BINARY */
423 enc_info = 0;
424 break;
425 }
426 switch (type) {
427 case 'A': case 'a': case 'Z':
428 case 'B': case 'b':
429 case 'H': case 'h':
430 from = NEXTFROM;
431 if (NIL_P(from)) {
432 ptr = "";
433 plen = 0;
434 }
435 else {
436 STR_FROM(from);
437 ptr = RSTRING_PTR(from);
438 plen = RSTRING_LEN(from);
439 }
440
441 /* The conversion of from with #to_str may have modified fmt,
442 * so p may be a dangling pointer; use star instead of p[-1]. */
443 if (star)
444 len = plen;
445
446 switch (type) {
447 case 'a': /* arbitrary binary string (null padded) */
448 case 'A': /* arbitrary binary string (ASCII space padded) */
449 case 'Z': /* null terminated string */
450 if (plen >= len) {
451 rb_str_buf_cat(res, ptr, len);
452 if (star && type == 'Z')
453 rb_str_buf_cat(res, "", 1);
454 }
455 else {
457 rb_str_buf_cat(res, ptr, plen);
458 str_expand_fill(res, (type == 'A' ? ' ' : '\0'), len - plen);
459 }
460 break;
461
462#define castchar(from) (char)((from) & 0xff)
463
464 case 'b': /* bit string (ascending) */
465 {
466 int byte = 0;
467 long i, j = 0;
468
469 if (len > plen) {
470 j = (len - plen + 1)/2;
471 len = plen;
472 }
473 for (i=0; i++ < len; ptr++) {
474 if (*ptr & 1)
475 byte |= 128;
476 if (i & 7)
477 byte >>= 1;
478 else {
479 char c = castchar(byte);
480 rb_str_buf_cat(res, &c, 1);
481 byte = 0;
482 }
483 }
484 if (len & 7) {
485 char c;
486 byte >>= 7 - (len & 7);
487 c = castchar(byte);
488 rb_str_buf_cat(res, &c, 1);
489 }
490 len = j;
491 goto grow;
492 }
493 break;
494
495 case 'B': /* bit string (descending) */
496 {
497 int byte = 0;
498 long i, j = 0;
499
500 if (len > plen) {
501 j = (len - plen + 1)/2;
502 len = plen;
503 }
504 for (i=0; i++ < len; ptr++) {
505 byte |= *ptr & 1;
506 if (i & 7)
507 byte <<= 1;
508 else {
509 char c = castchar(byte);
510 rb_str_buf_cat(res, &c, 1);
511 byte = 0;
512 }
513 }
514 if (len & 7) {
515 char c;
516 byte <<= 7 - (len & 7);
517 c = castchar(byte);
518 rb_str_buf_cat(res, &c, 1);
519 }
520 len = j;
521 goto grow;
522 }
523 break;
524
525 case 'h': /* hex string (low nibble first) */
526 {
527 int byte = 0;
528 long i, j = 0;
529
530 if (len > plen) {
531 j = (len + 1) / 2 - (plen + 1) / 2;
532 len = plen;
533 }
534 for (i=0; i++ < len; ptr++) {
535 if (ISALPHA(*ptr))
536 byte |= (((*ptr & 15) + 9) & 15) << 4;
537 else
538 byte |= (*ptr & 15) << 4;
539 if (i & 1)
540 byte >>= 4;
541 else {
542 char c = castchar(byte);
543 rb_str_buf_cat(res, &c, 1);
544 byte = 0;
545 }
546 }
547 if (len & 1) {
548 char c = castchar(byte);
549 rb_str_buf_cat(res, &c, 1);
550 }
551 len = j;
552 goto grow;
553 }
554 break;
555
556 case 'H': /* hex string (high nibble first) */
557 {
558 int byte = 0;
559 long i, j = 0;
560
561 if (len > plen) {
562 j = (len + 1) / 2 - (plen + 1) / 2;
563 len = plen;
564 }
565 for (i=0; i++ < len; ptr++) {
566 if (ISALPHA(*ptr))
567 byte |= ((*ptr & 15) + 9) & 15;
568 else
569 byte |= *ptr & 15;
570 if (i & 1)
571 byte <<= 4;
572 else {
573 char c = castchar(byte);
574 rb_str_buf_cat(res, &c, 1);
575 byte = 0;
576 }
577 }
578 if (len & 1) {
579 char c = castchar(byte);
580 rb_str_buf_cat(res, &c, 1);
581 }
582 len = j;
583 goto grow;
584 }
585 break;
586 }
587 break;
588
589 case 'c': /* signed char */
590 case 'C': /* unsigned char */
591 integer_size = 1;
592 bigendian_p = BIGENDIAN_P(); /* not effective */
593 goto pack_integer;
594
595 case 's': /* s for int16_t, s! for signed short */
596 case 'S': /* S for uint16_t, S! for unsigned short */
597 integer_size = NATINT_LEN(short, 2);
598 bigendian_p = BIGENDIAN_P();
599 goto pack_integer;
600
601 case 'i': /* i and i! for signed int */
602 case 'I': /* I and I! for unsigned int */
603 integer_size = (int)sizeof(int);
604 bigendian_p = BIGENDIAN_P();
605 goto pack_integer;
606
607 case 'l': /* l for int32_t, l! for signed long */
608 case 'L': /* L for uint32_t, L! for unsigned long */
609 integer_size = NATINT_LEN(long, 4);
610 bigendian_p = BIGENDIAN_P();
611 goto pack_integer;
612
613 case 'q': /* q for int64_t, q! for signed long long */
614 case 'Q': /* Q for uint64_t, Q! for unsigned long long */
615 integer_size = NATINT_LEN_Q;
616 bigendian_p = BIGENDIAN_P();
617 goto pack_integer;
618
619 case 'j': /* j for intptr_t */
620 integer_size = sizeof(intptr_t);
621 bigendian_p = BIGENDIAN_P();
622 goto pack_integer;
623
624 case 'J': /* J for uintptr_t */
625 integer_size = sizeof(uintptr_t);
626 bigendian_p = BIGENDIAN_P();
627 goto pack_integer;
628
629 case 'n': /* 16 bit (2 bytes) integer (network byte-order) */
630 integer_size = 2;
631 bigendian_p = 1;
632 goto pack_integer;
633
634 case 'N': /* 32 bit (4 bytes) integer (network byte-order) */
635 integer_size = 4;
636 bigendian_p = 1;
637 goto pack_integer;
638
639 case 'v': /* 16 bit (2 bytes) integer (VAX byte-order) */
640 integer_size = 2;
641 bigendian_p = 0;
642 goto pack_integer;
643
644 case 'V': /* 32 bit (4 bytes) integer (VAX byte-order) */
645 integer_size = 4;
646 bigendian_p = 0;
647 goto pack_integer;
648
649 pack_integer:
650 if (explicit_endian) {
651 bigendian_p = explicit_endian == '>';
652 }
653 if (integer_size > MAX_INTEGER_PACK_SIZE)
654 rb_bug("unexpected integer size for pack: %d", integer_size);
655 while (len-- > 0) {
656 char intbuf[MAX_INTEGER_PACK_SIZE];
657
658 from = NEXTFROM;
659 rb_integer_pack(from, intbuf, integer_size, 1, 0,
662 rb_str_buf_cat(res, intbuf, integer_size);
663 }
664 break;
665
666 case 'f': /* single precision float in native format */
667 case 'F': /* ditto */
668 while (len-- > 0) {
669 float f;
670
671 from = NEXTFROM;
672 f = VALUE_to_float(from);
673 rb_str_buf_cat(res, (char*)&f, sizeof(float));
674 }
675 break;
676
677 case 'e': /* single precision float in VAX byte-order */
678 while (len-- > 0) {
679 FLOAT_CONVWITH(tmp);
680
681 from = NEXTFROM;
682 tmp.f = VALUE_to_float(from);
683 HTOVF(tmp);
684 rb_str_buf_cat(res, tmp.buf, sizeof(float));
685 }
686 break;
687
688 case 'E': /* double precision float in VAX byte-order */
689 while (len-- > 0) {
690 DOUBLE_CONVWITH(tmp);
691 from = NEXTFROM;
692 tmp.d = RFLOAT_VALUE(rb_to_float(from));
693 HTOVD(tmp);
694 rb_str_buf_cat(res, tmp.buf, sizeof(double));
695 }
696 break;
697
698 case 'd': /* double precision float in native format */
699 case 'D': /* ditto */
700 while (len-- > 0) {
701 double d;
702
703 from = NEXTFROM;
704 d = RFLOAT_VALUE(rb_to_float(from));
705 rb_str_buf_cat(res, (char*)&d, sizeof(double));
706 }
707 break;
708
709 case 'g': /* single precision float in network byte-order */
710 while (len-- > 0) {
711 FLOAT_CONVWITH(tmp);
712 from = NEXTFROM;
713 tmp.f = VALUE_to_float(from);
714 HTONF(tmp);
715 rb_str_buf_cat(res, tmp.buf, sizeof(float));
716 }
717 break;
718
719 case 'G': /* double precision float in network byte-order */
720 while (len-- > 0) {
721 DOUBLE_CONVWITH(tmp);
722
723 from = NEXTFROM;
724 tmp.d = RFLOAT_VALUE(rb_to_float(from));
725 HTOND(tmp);
726 rb_str_buf_cat(res, tmp.buf, sizeof(double));
727 }
728 break;
729
730 case 'x': /* null byte */
731 grow:
733 str_expand_fill(res, '\0', len);
734 break;
735
736 case 'X': /* back up byte */
737 shrink:
738 plen = RSTRING_LEN(res);
739 if (plen < len)
740 rb_raise(rb_eArgError, "X outside of string");
741 rb_str_set_len(res, plen - len);
742 break;
743
744 case '@': /* null fill to absolute position */
745 len -= RSTRING_LEN(res);
746 if (len > 0) goto grow;
747 len = -len;
748 if (len > 0) goto shrink;
749 break;
750
751 case '%':
752 rb_raise(rb_eArgError, "%% is not supported");
753 break;
754
755 case 'U': /* Unicode character */
756 while (len-- > 0) {
757 SIGNED_VALUE l;
758 char buf[8];
759 int le;
760
761 from = NEXTFROM;
762 from = rb_to_int(from);
763 l = NUM2LONG(from);
764 if (l < 0) {
765 rb_raise(rb_eRangeError, "pack(U): value out of range");
766 }
767 le = rb_uv_to_utf8(buf, l);
768 rb_str_buf_cat(res, (char*)buf, le);
769 }
770 break;
771
772 case 'r': /* r for SLEB128 encoding (signed) */
773 case 'R': /* R for ULEB128 encoding (unsigned) */
774 {
775 int pack_flags = INTEGER_PACK_LITTLE_ENDIAN;
776
777 if (type == 'r') {
778 pack_flags |= INTEGER_PACK_2COMP;
779 }
780
781 while (len-- > 0) {
782 size_t numbytes, nlz_bits;
783 int sign, extra = 0;
784 char *cp;
785
786 from = NEXTFROM;
787 from = rb_to_int(from);
788 if (type == 'R' && rb_int_negative_p(from)) {
789 rb_raise(rb_eArgError, "can't encode negative numbers in ULEB128");
790 }
791
792 numbytes = rb_absint_numwords(from, 7, &nlz_bits);
793 if (numbytes == 0) {
794 numbytes = 1;
795 }
796 else if (nlz_bits == 0 && type == 'r') {
797 /* No leading zero bits, we need an extra byte for sign extension */
798 extra = 1;
799 }
800 rb_str_modify_expand(res, numbytes + extra);
801
802 long start = RSTRING_LEN(res);
803
804 cp = RSTRING_PTR(res) + start;
805 sign = rb_integer_pack(from, cp, numbytes, 1, 1, pack_flags);
806
807 if (extra) {
808 /* Need an extra byte */
809 cp[numbytes++] = sign < 0 ? 0x7f : 0x00;
810 }
811 rb_str_set_len(res, start + numbytes);
812
813 while (1 < numbytes) {
814 *cp |= 0x80;
815 cp++;
816 numbytes--;
817 }
818 }
819 }
820 break;
821 case 'u': /* uuencoded string */
822 case 'm': /* base64 encoded string */
823 from = NEXTFROM;
824 STR_FROM(from);
825 ptr = RSTRING_PTR(from);
826 plen = RSTRING_LEN(from);
827
828 if (len == 0 && type == 'm') {
829 encodes(res, ptr, plen, type, 0);
830 ptr += plen;
831 break;
832 }
833 if (len <= 2)
834 len = 45;
835 else if (len > 63 && type == 'u')
836 len = 63;
837 else
838 len = len / 3 * 3;
839 while (plen > 0) {
840 long todo;
841
842 if (plen > len)
843 todo = len;
844 else
845 todo = plen;
846 encodes(res, ptr, todo, type, 1);
847 plen -= todo;
848 ptr += todo;
849 }
850 break;
851
852 case 'M': /* quoted-printable encoded string */
853 from = rb_obj_as_string(NEXTFROM);
854 NOT_BUFFER(from);
855 if (len <= 1)
856 len = 72;
857 qpencode(res, from, len);
858 break;
859
860 case 'P': /* pointer to packed byte string */
861 from = THISFROM;
862 if (!NIL_P(from)) {
863 STR_FROM(from);
864 if (RSTRING_LEN(from) < len) {
865 rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
866 RSTRING_LEN(from), len);
867 }
868 }
869 len = 1;
870 /* FALL THROUGH */
871 case 'p': /* pointer to string */
872 while (len-- > 0) {
873 const char *t = 0;
874 from = NEXTFROM;
875 if (!NIL_P(from)) {
876 STR_FROM(from);
877 t = RSTRING_PTR(from);
878 }
879 if (!associates) {
880 associates = rb_ary_new();
881 }
882 rb_ary_push(associates, from);
883 rb_str_buf_cat(res, (char*)&t, sizeof(char*));
884 }
885 break;
886
887 case 'w': /* BER compressed integer */
888 while (len-- > 0) {
889 VALUE buf;
890 size_t numbytes;
891 int sign;
892 char *cp;
893
894 from = NEXTFROM;
895 from = rb_to_int(from);
896 numbytes = rb_absint_numwords(from, 7, NULL);
897 if (numbytes == 0)
898 numbytes = 1;
899 buf = rb_str_new(NULL, numbytes);
900
901 sign = rb_integer_pack(from, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
902
903 if (sign < 0)
904 rb_raise(rb_eArgError, "can't compress negative numbers");
905 if (sign == 2)
906 rb_bug("buffer size problem?");
907
908 cp = RSTRING_PTR(buf);
909 while (1 < numbytes) {
910 *cp |= 0x80;
911 cp++;
912 numbytes--;
913 }
914
915 rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf));
916 }
917 break;
918
919 default: {
920 unknown_directive("pack", type, fmt);
921 break;
922 }
923 }
924 }
925
926 if (associates) {
927 str_associate(res, associates);
928 }
929 switch (enc_info) {
930 case 1:
931 ENCODING_CODERANGE_SET(res, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
932 break;
933 case 2:
934 rb_enc_set_index(res, rb_utf8_encindex());
935 break;
936 default:
937 /* do nothing, keep ASCII-8BIT */
938 break;
939 }
940 return res;
941}
942
943VALUE
944rb_ec_pack_ary(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
945{
946 return pack_pack(ec, ary, fmt, buffer);
947}
948
949static const char uu_table[] =
950"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
951static const char b64_table[] =
952"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
953
954static void
955encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
956{
957 enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
958 char buff[buff_size + 1]; /* +1 for tail_lf */
959 long i = 0;
960 const char *const trans = type == 'u' ? uu_table : b64_table;
961 char padding;
962 const unsigned char *s = (const unsigned char *)s0;
963
964 if (type == 'u') {
965 buff[i++] = (char)len + ' ';
966 padding = '`';
967 }
968 else {
969 padding = '=';
970 }
971 while (len >= input_unit) {
972 while (len >= input_unit && buff_size-i >= encoded_unit) {
973 buff[i++] = trans[077 & (*s >> 2)];
974 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
975 buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
976 buff[i++] = trans[077 & s[2]];
977 s += input_unit;
978 len -= input_unit;
979 }
980 if (buff_size-i < encoded_unit) {
981 rb_str_buf_cat(str, buff, i);
982 i = 0;
983 }
984 }
985
986 if (len == 2) {
987 buff[i++] = trans[077 & (*s >> 2)];
988 buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
989 buff[i++] = trans[077 & (((s[1] << 2) & 074) | (('\0' >> 6) & 03))];
990 buff[i++] = padding;
991 }
992 else if (len == 1) {
993 buff[i++] = trans[077 & (*s >> 2)];
994 buff[i++] = trans[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
995 buff[i++] = padding;
996 buff[i++] = padding;
997 }
998 if (tail_lf) buff[i++] = '\n';
999 rb_str_buf_cat(str, buff, i);
1000 if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
1001}
1002
1003static const char hex_table[] = "0123456789ABCDEF";
1004
1005static void
1006qpencode(VALUE str, VALUE from, long len)
1007{
1008 char buff[1024];
1009 long i = 0, n = 0, prev = EOF;
1010 unsigned char *s = (unsigned char*)RSTRING_PTR(from);
1011 unsigned char *send = s + RSTRING_LEN(from);
1012
1013 while (s < send) {
1014 if ((*s > 126) ||
1015 (*s < 32 && *s != '\n' && *s != '\t') ||
1016 (*s == '=')) {
1017 buff[i++] = '=';
1018 buff[i++] = hex_table[*s >> 4];
1019 buff[i++] = hex_table[*s & 0x0f];
1020 n += 3;
1021 prev = EOF;
1022 }
1023 else if (*s == '\n') {
1024 if (prev == ' ' || prev == '\t') {
1025 buff[i++] = '=';
1026 buff[i++] = *s;
1027 }
1028 buff[i++] = *s;
1029 n = 0;
1030 prev = *s;
1031 }
1032 else {
1033 buff[i++] = *s;
1034 n++;
1035 prev = *s;
1036 }
1037 if (n > len) {
1038 buff[i++] = '=';
1039 buff[i++] = '\n';
1040 n = 0;
1041 prev = '\n';
1042 }
1043 if (i > 1024 - 5) {
1044 rb_str_buf_cat(str, buff, i);
1045 i = 0;
1046 }
1047 s++;
1048 }
1049 if (n > 0) {
1050 buff[i++] = '=';
1051 buff[i++] = '\n';
1052 }
1053 if (i > 0) {
1054 rb_str_buf_cat(str, buff, i);
1055 }
1056}
1057
1058static inline int
1059hex2num(char c)
1060{
1061 int n;
1062 n = ruby_digit36_to_number_table[(unsigned char)c];
1063 if (16 <= n)
1064 n = -1;
1065 return n;
1066}
1067
1068#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
1069 tmp_len = 0; \
1070 if (mode == UNPACK_ARRAY) { \
1071 rb_ary_modify_expand(ary, len); \
1072 } \
1073 if (len > (long)((send-s)/(sz))) { \
1074 if (!star) { \
1075 tmp_len = len-(send-s)/(sz); \
1076 } \
1077 len = (send-s)/(sz); \
1078 } \
1079} while (0)
1080
1081#define PACK_ITEM_ADJUST() do { \
1082 if (tmp_len > 0 && mode == UNPACK_ARRAY) \
1083 rb_ary_resize(ary, RARRAY_LEN(ary)+tmp_len); \
1084} while (0)
1085
1086/* Workaround for Oracle Developer Studio (Oracle Solaris Studio)
1087 * 12.4/12.5/12.6 C compiler optimization bug
1088 * with "-xO4" optimization option.
1089 */
1090#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
1091# define AVOID_CC_BUG volatile
1092#else
1093# define AVOID_CC_BUG
1094#endif
1095
1096enum unpack_mode {
1097 UNPACK_ARRAY,
1098 UNPACK_BLOCK,
1099 UNPACK_1
1100};
1101
1102static VALUE
1103pack_unpack_internal(VALUE str, VALUE fmt, VALUE ofs, enum unpack_mode mode)
1104{
1105#define hexdigits ruby_hexdigits
1106 const char *s, *send;
1107 const char *p, *pend;
1108 VALUE ary, associates = Qfalse;
1109 long len;
1110 AVOID_CC_BUG long tmp_len;
1111 int signed_p, integer_size, bigendian_p;
1112 long align_base;
1113 const char *sptr;
1114 long slen;
1115 const char *fptr;
1116 long flen;
1117#define UNPACK_PUSH(item) do {\
1118 VALUE item_val = (item);\
1119 if ((mode) == UNPACK_BLOCK) {\
1120 rb_yield(item_val);\
1121 /* The block may have modified str and invalidated s */ \
1122 if (RSTRING_PTR(str) != sptr || RSTRING_LEN(str) != slen) {\
1123 rb_raise(rb_eRuntimeError, "string modified");\
1124 }\
1125 /* The block may have also modified fmt and invalidated p */ \
1126 if (RSTRING_PTR(fmt) != fptr || RSTRING_LEN(fmt) != flen) {\
1127 rb_raise(rb_eRuntimeError, "format string modified");\
1128 }\
1129 }\
1130 else if ((mode) == UNPACK_ARRAY) {\
1131 rb_ary_push(ary, item_val);\
1132 }\
1133 else /* if ((mode) == UNPACK_1) { */ {\
1134 return item_val; \
1135 }\
1136 } while (0)
1137
1138 StringValue(str);
1139 StringValue(fmt);
1140 long offset = NUM2LONG(ofs);
1142
1143 len = RSTRING_LEN(str);
1144 if (offset < 0 ? (offset += len) < 0 : offset > len) {
1145 rb_raise(rb_eArgError, "offset outside of string");
1146 }
1147
1148 s = RSTRING_PTR(str);
1149 send = s + len;
1150 sptr = s;
1151 slen = len;
1152 s += offset;
1153 align_base = offset;
1154
1155 p = RSTRING_PTR(fmt);
1156 pend = p + RSTRING_LEN(fmt);
1157 fptr = p;
1158 flen = RSTRING_LEN(fmt);
1159
1160#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
1161
1162 ary = mode == UNPACK_ARRAY ? rb_ary_new() : Qnil;
1163 while (p < pend) {
1164 int explicit_endian = 0;
1165 const char type = *p++;
1166 size_t align = 0;
1167 int star = 0;
1168 USING_NATINT(int natint = 0); /* native integer */
1169
1170 if (skip_blank(p, type)) continue;
1171
1172 /* Directives that do not take modifiers. */
1173 if ((type == 'x' || type == '@') && p < pend && *p == '!') {
1174 p++;
1175 p = pack_alignment_size(p, pend, fmt, &align);
1176 len = pack_align_pad(s - RSTRING_PTR(str), type == '@' ? 0 : align_base, align);
1177 if (len > send - s) {
1178 rb_raise(rb_eArgError, type == '@' ? "@ outside of string" : "x outside of string");
1179 }
1180 s += len;
1181 continue;
1182 }
1183
1184 p = pack_modifiers(p, pend, type, &natint, &explicit_endian);
1185
1186 if (p >= pend)
1187 len = 1;
1188 else if (*p == '*') {
1189 star = 1;
1190 len = send - s;
1191 p++;
1192 }
1193 else if (ISDIGIT(*p)) {
1194 errno = 0;
1195 len = STRTOUL(p, (char**)&p, 10);
1196 if (len < 0 || errno) {
1197 rb_raise(rb_eRangeError, "pack length too big");
1198 }
1199 }
1200 else {
1201 len = (type != '@');
1202 }
1203
1204 switch (type) {
1205 case '%':
1206 rb_raise(rb_eArgError, "%% is not supported");
1207 break;
1208
1209 case 'A':
1210 if (len > send - s) len = send - s;
1211 {
1212 long end = len;
1213 const char *t = s + len - 1;
1214
1215 while (t >= s) {
1216 if (*t != ' ' && *t != '\0') break;
1217 t--; len--;
1218 }
1219 UNPACK_PUSH(rb_str_new(s, len));
1220 s += end;
1221 }
1222 break;
1223
1224 case 'Z':
1225 {
1226 const char *t = s;
1227
1228 if (len > send-s) len = send-s;
1229 while (t < s+len && *t) t++;
1230 UNPACK_PUSH(rb_str_new(s, t-s));
1231 if (t < send) t++;
1232 s = star ? t : s+len;
1233 }
1234 break;
1235
1236 case 'a':
1237 if (len > send - s) len = send - s;
1238 UNPACK_PUSH(rb_str_new(s, len));
1239 s += len;
1240 break;
1241
1242 case 'b':
1243 {
1244 VALUE bitstr;
1245 char *t;
1246 int bits;
1247 long i;
1248
1249 if (p[-1] == '*' || len > (send - s) * 8)
1250 len = (send - s) * 8;
1251 bits = 0;
1252 bitstr = rb_usascii_str_new(0, len);
1253 t = RSTRING_PTR(bitstr);
1254 for (i=0; i<len; i++) {
1255 if (i & 7) bits >>= 1;
1256 else bits = (unsigned char)*s++;
1257 *t++ = (bits & 1) ? '1' : '0';
1258 }
1259 UNPACK_PUSH(bitstr);
1260 }
1261 break;
1262
1263 case 'B':
1264 {
1265 VALUE bitstr;
1266 char *t;
1267 int bits;
1268 long i;
1269
1270 if (p[-1] == '*' || len > (send - s) * 8)
1271 len = (send - s) * 8;
1272 bits = 0;
1273 bitstr = rb_usascii_str_new(0, len);
1274 t = RSTRING_PTR(bitstr);
1275 for (i=0; i<len; i++) {
1276 if (i & 7) bits <<= 1;
1277 else bits = (unsigned char)*s++;
1278 *t++ = (bits & 128) ? '1' : '0';
1279 }
1280 UNPACK_PUSH(bitstr);
1281 }
1282 break;
1283
1284 case 'h':
1285 {
1286 VALUE bitstr;
1287 char *t;
1288 int bits;
1289 long i;
1290
1291 if (p[-1] == '*' || len > (send - s) * 2)
1292 len = (send - s) * 2;
1293 bits = 0;
1294 bitstr = rb_usascii_str_new(0, len);
1295 t = RSTRING_PTR(bitstr);
1296 for (i=0; i<len; i++) {
1297 if (i & 1)
1298 bits >>= 4;
1299 else
1300 bits = (unsigned char)*s++;
1301 *t++ = hexdigits[bits & 15];
1302 }
1303 UNPACK_PUSH(bitstr);
1304 }
1305 break;
1306
1307 case 'H':
1308 {
1309 VALUE bitstr;
1310 char *t;
1311 int bits;
1312 long i;
1313
1314 if (p[-1] == '*' || len > (send - s) * 2)
1315 len = (send - s) * 2;
1316 bits = 0;
1317 bitstr = rb_usascii_str_new(0, len);
1318 t = RSTRING_PTR(bitstr);
1319 for (i=0; i<len; i++) {
1320 if (i & 1)
1321 bits <<= 4;
1322 else
1323 bits = (unsigned char)*s++;
1324 *t++ = hexdigits[(bits >> 4) & 15];
1325 }
1326 UNPACK_PUSH(bitstr);
1327 }
1328 break;
1329
1330 case 'c':
1331 signed_p = 1;
1332 integer_size = 1;
1333 bigendian_p = BIGENDIAN_P(); /* not effective */
1334 goto unpack_integer;
1335
1336 case 'C':
1337 signed_p = 0;
1338 integer_size = 1;
1339 bigendian_p = BIGENDIAN_P(); /* not effective */
1340 goto unpack_integer;
1341
1342 case 's':
1343 signed_p = 1;
1344 integer_size = NATINT_LEN(short, 2);
1345 bigendian_p = BIGENDIAN_P();
1346 goto unpack_integer;
1347
1348 case 'S':
1349 signed_p = 0;
1350 integer_size = NATINT_LEN(short, 2);
1351 bigendian_p = BIGENDIAN_P();
1352 goto unpack_integer;
1353
1354 case 'i':
1355 signed_p = 1;
1356 integer_size = (int)sizeof(int);
1357 bigendian_p = BIGENDIAN_P();
1358 goto unpack_integer;
1359
1360 case 'I':
1361 signed_p = 0;
1362 integer_size = (int)sizeof(int);
1363 bigendian_p = BIGENDIAN_P();
1364 goto unpack_integer;
1365
1366 case 'l':
1367 signed_p = 1;
1368 integer_size = NATINT_LEN(long, 4);
1369 bigendian_p = BIGENDIAN_P();
1370 goto unpack_integer;
1371
1372 case 'L':
1373 signed_p = 0;
1374 integer_size = NATINT_LEN(long, 4);
1375 bigendian_p = BIGENDIAN_P();
1376 goto unpack_integer;
1377
1378 case 'q':
1379 signed_p = 1;
1380 integer_size = NATINT_LEN_Q;
1381 bigendian_p = BIGENDIAN_P();
1382 goto unpack_integer;
1383
1384 case 'Q':
1385 signed_p = 0;
1386 integer_size = NATINT_LEN_Q;
1387 bigendian_p = BIGENDIAN_P();
1388 goto unpack_integer;
1389
1390 case 'j':
1391 signed_p = 1;
1392 integer_size = sizeof(intptr_t);
1393 bigendian_p = BIGENDIAN_P();
1394 goto unpack_integer;
1395
1396 case 'J':
1397 signed_p = 0;
1398 integer_size = sizeof(uintptr_t);
1399 bigendian_p = BIGENDIAN_P();
1400 goto unpack_integer;
1401
1402 case 'n':
1403 signed_p = 0;
1404 integer_size = 2;
1405 bigendian_p = 1;
1406 goto unpack_integer;
1407
1408 case 'N':
1409 signed_p = 0;
1410 integer_size = 4;
1411 bigendian_p = 1;
1412 goto unpack_integer;
1413
1414 case 'v':
1415 signed_p = 0;
1416 integer_size = 2;
1417 bigendian_p = 0;
1418 goto unpack_integer;
1419
1420 case 'V':
1421 signed_p = 0;
1422 integer_size = 4;
1423 bigendian_p = 0;
1424 goto unpack_integer;
1425
1426 unpack_integer:
1427 if (explicit_endian) {
1428 bigendian_p = explicit_endian == '>';
1429 }
1430 PACK_LENGTH_ADJUST_SIZE(integer_size);
1431 while (len-- > 0) {
1432 int flags = bigendian_p ? INTEGER_PACK_BIG_ENDIAN : INTEGER_PACK_LITTLE_ENDIAN;
1433 VALUE val;
1434 if (signed_p)
1435 flags |= INTEGER_PACK_2COMP;
1436 val = rb_integer_unpack(s, integer_size, 1, 0, flags);
1437 UNPACK_PUSH(val);
1438 s += integer_size;
1439 }
1440 PACK_ITEM_ADJUST();
1441 break;
1442
1443 case 'f':
1444 case 'F':
1445 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1446 while (len-- > 0) {
1447 float tmp;
1448 UNPACK_FETCH(&tmp, float);
1449 UNPACK_PUSH(DBL2NUM((double)tmp));
1450 }
1451 PACK_ITEM_ADJUST();
1452 break;
1453
1454 case 'e':
1455 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1456 while (len-- > 0) {
1457 FLOAT_CONVWITH(tmp);
1458 UNPACK_FETCH(tmp.buf, float);
1459 VTOHF(tmp);
1460 UNPACK_PUSH(DBL2NUM(tmp.f));
1461 }
1462 PACK_ITEM_ADJUST();
1463 break;
1464
1465 case 'E':
1466 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1467 while (len-- > 0) {
1468 DOUBLE_CONVWITH(tmp);
1469 UNPACK_FETCH(tmp.buf, double);
1470 VTOHD(tmp);
1471 UNPACK_PUSH(DBL2NUM(tmp.d));
1472 }
1473 PACK_ITEM_ADJUST();
1474 break;
1475
1476 case 'D':
1477 case 'd':
1478 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1479 while (len-- > 0) {
1480 double tmp;
1481 UNPACK_FETCH(&tmp, double);
1482 UNPACK_PUSH(DBL2NUM(tmp));
1483 }
1484 PACK_ITEM_ADJUST();
1485 break;
1486
1487 case 'g':
1488 PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1489 while (len-- > 0) {
1490 FLOAT_CONVWITH(tmp);
1491 UNPACK_FETCH(tmp.buf, float);
1492 NTOHF(tmp);
1493 UNPACK_PUSH(DBL2NUM(tmp.f));
1494 }
1495 PACK_ITEM_ADJUST();
1496 break;
1497
1498 case 'G':
1499 PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1500 while (len-- > 0) {
1501 DOUBLE_CONVWITH(tmp);
1502 UNPACK_FETCH(tmp.buf, double);
1503 NTOHD(tmp);
1504 UNPACK_PUSH(DBL2NUM(tmp.d));
1505 }
1506 PACK_ITEM_ADJUST();
1507 break;
1508
1509 case 'U':
1510 if (len > send - s) len = send - s;
1511 while (len > 0 && s < send) {
1512 long alen = send - s;
1513 unsigned long l;
1514
1515 l = utf8_to_uv(s, &alen);
1516 s += alen; len--;
1517 UNPACK_PUSH(ULONG2NUM(l));
1518 }
1519 break;
1520
1521 case 'u':
1522 {
1523 VALUE buf = rb_str_new(0, (send - s)*3/4);
1524 char *ptr = RSTRING_PTR(buf);
1525 long total = 0;
1526
1527 while (s < send && (unsigned char)*s > ' ' && (unsigned char)*s < 'a') {
1528 long a,b,c,d;
1529 char hunk[3];
1530
1531 len = ((unsigned char)*s++ - ' ') & 077;
1532
1533 total += len;
1534 if (total > RSTRING_LEN(buf)) {
1535 len -= total - RSTRING_LEN(buf);
1536 total = RSTRING_LEN(buf);
1537 }
1538
1539 while (len > 0) {
1540 long mlen = len > 3 ? 3 : len;
1541
1542 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1543 a = ((unsigned char)*s++ - ' ') & 077;
1544 else
1545 a = 0;
1546 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1547 b = ((unsigned char)*s++ - ' ') & 077;
1548 else
1549 b = 0;
1550 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1551 c = ((unsigned char)*s++ - ' ') & 077;
1552 else
1553 c = 0;
1554 if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
1555 d = ((unsigned char)*s++ - ' ') & 077;
1556 else
1557 d = 0;
1558 hunk[0] = (char)(a << 2 | b >> 4);
1559 hunk[1] = (char)(b << 4 | c >> 2);
1560 hunk[2] = (char)(c << 6 | d);
1561 memcpy(ptr, hunk, mlen);
1562 ptr += mlen;
1563 len -= mlen;
1564 }
1565 if (s < send && (unsigned char)*s != '\r' && *s != '\n')
1566 s++; /* possible checksum byte */
1567 if (s < send && *s == '\r') s++;
1568 if (s < send && *s == '\n') s++;
1569 }
1570
1571 rb_str_set_len(buf, total);
1572 UNPACK_PUSH(buf);
1573 }
1574 break;
1575
1576 case 'm':
1577 {
1578 VALUE buf = rb_str_new(0, (send - s + 3)*3/4); /* +3 is for skipping paddings */
1579 char *ptr = RSTRING_PTR(buf);
1580 int a = -1,b = -1,c = 0,d = 0;
1581 static signed char b64_xtable[256];
1582
1583 if (b64_xtable['/'] <= 0) {
1584 int i;
1585
1586 for (i = 0; i < 256; i++) {
1587 b64_xtable[i] = -1;
1588 }
1589 for (i = 0; i < 64; i++) {
1590 b64_xtable[(unsigned char)b64_table[i]] = (char)i;
1591 }
1592 }
1593 if (len == 0) {
1594 while (s < send) {
1595 a = b = c = d = -1;
1596 a = b64_xtable[(unsigned char)*s++];
1597 if (s >= send || a == -1) rb_raise(rb_eArgError, "invalid base64");
1598 b = b64_xtable[(unsigned char)*s++];
1599 if (s >= send || b == -1) rb_raise(rb_eArgError, "invalid base64");
1600 if (*s == '=') {
1601 if (s + 2 == send && *(s + 1) == '=') break;
1602 rb_raise(rb_eArgError, "invalid base64");
1603 }
1604 c = b64_xtable[(unsigned char)*s++];
1605 if (s >= send || c == -1) rb_raise(rb_eArgError, "invalid base64");
1606 if (s + 1 == send && *s == '=') break;
1607 d = b64_xtable[(unsigned char)*s++];
1608 if (d == -1) rb_raise(rb_eArgError, "invalid base64");
1609 *ptr++ = castchar(a << 2 | b >> 4);
1610 *ptr++ = castchar(b << 4 | c >> 2);
1611 *ptr++ = castchar(c << 6 | d);
1612 }
1613 if (c == -1) {
1614 *ptr++ = castchar(a << 2 | b >> 4);
1615 if (b & 0xf) rb_raise(rb_eArgError, "invalid base64");
1616 }
1617 else if (d == -1) {
1618 *ptr++ = castchar(a << 2 | b >> 4);
1619 *ptr++ = castchar(b << 4 | c >> 2);
1620 if (c & 0x3) rb_raise(rb_eArgError, "invalid base64");
1621 }
1622 }
1623 else {
1624 while (s < send) {
1625 a = b = c = d = -1;
1626 while ((a = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1627 if (s >= send) break;
1628 s++;
1629 while ((b = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1630 if (s >= send) break;
1631 s++;
1632 while ((c = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
1633 if (*s == '=' || s >= send) break;
1634 s++;
1635 while ((d = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
1636 if (*s == '=' || s >= send) break;
1637 s++;
1638 *ptr++ = castchar(a << 2 | b >> 4);
1639 *ptr++ = castchar(b << 4 | c >> 2);
1640 *ptr++ = castchar(c << 6 | d);
1641 a = -1;
1642 }
1643 if (a != -1 && b != -1) {
1644 if (c == -1)
1645 *ptr++ = castchar(a << 2 | b >> 4);
1646 else {
1647 *ptr++ = castchar(a << 2 | b >> 4);
1648 *ptr++ = castchar(b << 4 | c >> 2);
1649 }
1650 }
1651 }
1652 rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
1653 UNPACK_PUSH(buf);
1654 }
1655 break;
1656
1657 case 'M':
1658 {
1659 VALUE buf = rb_str_new(0, send - s);
1660 char *ptr = RSTRING_PTR(buf);
1661 const char *ss = s;
1662 int csum = 0;
1663 int c1, c2;
1664
1665 while (s < send) {
1666 if (*s == '=') {
1667 if (++s == send) break;
1668 if (s+1 < send && *s == '\r' && *(s+1) == '\n')
1669 s++;
1670 if (*s != '\n') {
1671 if ((c1 = hex2num(*s)) == -1) break;
1672 if (++s == send) break;
1673 if ((c2 = hex2num(*s)) == -1) break;
1674 csum |= *ptr++ = castchar(c1 << 4 | c2);
1675 }
1676 }
1677 else {
1678 csum |= *ptr++ = *s;
1679 }
1680 s++;
1681 ss = s;
1682 }
1683 rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
1684 rb_str_buf_cat(buf, ss, send-ss);
1686 ENCODING_CODERANGE_SET(buf, rb_ascii8bit_encindex(), csum);
1687 UNPACK_PUSH(buf);
1688 }
1689 break;
1690
1691 case '@':
1692 if (len > RSTRING_LEN(str))
1693 rb_raise(rb_eArgError, "@ outside of string");
1694 s = RSTRING_PTR(str) + len;
1695 break;
1696
1697 case 'X':
1698 if (len > s - RSTRING_PTR(str))
1699 rb_raise(rb_eArgError, "X outside of string");
1700 s -= len;
1701 break;
1702
1703 case 'x':
1704 if (len > send - s)
1705 rb_raise(rb_eArgError, "x outside of string");
1706 s += len;
1707 break;
1708
1709 case '^':
1710 UNPACK_PUSH(SSIZET2NUM(s - RSTRING_PTR(str)));
1711 break;
1712
1713 case 'P':
1714 if (sizeof(char *) <= (size_t)(send - s)) {
1715 VALUE tmp = Qnil;
1716 const char *t;
1717
1718 UNPACK_FETCH(&t, char *);
1719 if (t) {
1720 if (!associates) associates = str_associated(str);
1721 tmp = associated_pointer(associates, t);
1722 if (len < RSTRING_LEN(tmp)) {
1723 tmp = rb_str_new(t, len);
1724 str_associate(tmp, associates);
1725 }
1726 }
1727 UNPACK_PUSH(tmp);
1728 }
1729 break;
1730
1731 case 'p':
1732 if (len > (long)((send - s) / sizeof(char *)))
1733 len = (send - s) / sizeof(char *);
1734 while (len-- > 0) {
1735 if ((size_t)(send - s) < sizeof(char *))
1736 break;
1737 else {
1738 VALUE tmp = Qnil;
1739 const char *t;
1740
1741 UNPACK_FETCH(&t, char *);
1742 if (t) {
1743 if (!associates) associates = str_associated(str);
1744 tmp = associated_pointer(associates, t);
1745 }
1746 UNPACK_PUSH(tmp);
1747 }
1748 }
1749 break;
1750
1751 case 'r':
1752 case 'R':
1753 {
1754 int pack_flags = INTEGER_PACK_LITTLE_ENDIAN;
1755
1756 if (type == 'r') {
1757 pack_flags |= INTEGER_PACK_2COMP;
1758 }
1759 const char *s0 = s;
1760 while (len > 0 && s < send) {
1761 if (*s & 0x80) {
1762 s++;
1763 }
1764 else {
1765 s++;
1766 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, pack_flags));
1767 len--;
1768 s0 = s;
1769 }
1770 }
1771 /* Handle incomplete value and remaining expected values with nil (only if not using *) */
1772 if (!star) {
1773 if (s0 != s && len > 0) {
1774 UNPACK_PUSH(Qnil);
1775 len--;
1776 }
1777 while (len-- > 0) {
1778 UNPACK_PUSH(Qnil);
1779 }
1780 }
1781 }
1782 break;
1783
1784 case 'w':
1785 {
1786 const char *s0 = s;
1787 while (len > 0 && s < send) {
1788 if (*s & 0x80) {
1789 s++;
1790 }
1791 else {
1792 s++;
1793 UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
1794 len--;
1795 s0 = s;
1796 }
1797 }
1798 }
1799 break;
1800
1801 default:
1802 unknown_directive("unpack", type, fmt);
1803 break;
1804 }
1805 }
1806
1807 return ary;
1808}
1809
1810static VALUE
1811pack_unpack(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
1812{
1813 enum unpack_mode mode = rb_block_given_p() ? UNPACK_BLOCK : UNPACK_ARRAY;
1814 return pack_unpack_internal(str, fmt, offset, mode);
1815}
1816
1817static VALUE
1818pack_unpack1(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
1819{
1820 return pack_unpack_internal(str, fmt, offset, UNPACK_1);
1821}
1822
1823int
1824rb_uv_to_utf8(char buf[6], unsigned long uv)
1825{
1826 if (uv <= 0x7f) {
1827 buf[0] = (char)uv;
1828 return 1;
1829 }
1830 if (uv <= 0x7ff) {
1831 buf[0] = castchar(((uv>>6)&0xff)|0xc0);
1832 buf[1] = castchar((uv&0x3f)|0x80);
1833 return 2;
1834 }
1835 if (uv <= 0xffff) {
1836 buf[0] = castchar(((uv>>12)&0xff)|0xe0);
1837 buf[1] = castchar(((uv>>6)&0x3f)|0x80);
1838 buf[2] = castchar((uv&0x3f)|0x80);
1839 return 3;
1840 }
1841 if (uv <= 0x1fffff) {
1842 buf[0] = castchar(((uv>>18)&0xff)|0xf0);
1843 buf[1] = castchar(((uv>>12)&0x3f)|0x80);
1844 buf[2] = castchar(((uv>>6)&0x3f)|0x80);
1845 buf[3] = castchar((uv&0x3f)|0x80);
1846 return 4;
1847 }
1848 if (uv <= 0x3ffffff) {
1849 buf[0] = castchar(((uv>>24)&0xff)|0xf8);
1850 buf[1] = castchar(((uv>>18)&0x3f)|0x80);
1851 buf[2] = castchar(((uv>>12)&0x3f)|0x80);
1852 buf[3] = castchar(((uv>>6)&0x3f)|0x80);
1853 buf[4] = castchar((uv&0x3f)|0x80);
1854 return 5;
1855 }
1856 if (uv <= 0x7fffffff) {
1857 buf[0] = castchar(((uv>>30)&0xff)|0xfc);
1858 buf[1] = castchar(((uv>>24)&0x3f)|0x80);
1859 buf[2] = castchar(((uv>>18)&0x3f)|0x80);
1860 buf[3] = castchar(((uv>>12)&0x3f)|0x80);
1861 buf[4] = castchar(((uv>>6)&0x3f)|0x80);
1862 buf[5] = castchar((uv&0x3f)|0x80);
1863 return 6;
1864 }
1865 rb_raise(rb_eRangeError, "pack(U): value out of range");
1866
1868}
1869
1870static const unsigned long utf8_limits[] = {
1871 0x0, /* 1 */
1872 0x80, /* 2 */
1873 0x800, /* 3 */
1874 0x10000, /* 4 */
1875 0x200000, /* 5 */
1876 0x4000000, /* 6 */
1877 0x80000000, /* 7 */
1878};
1879
1880static unsigned long
1881utf8_to_uv(const char *p, long *lenp)
1882{
1883 int c = *p++ & 0xff;
1884 unsigned long uv = c;
1885 long n;
1886
1887 if (!(uv & 0x80)) {
1888 *lenp = 1;
1889 return uv;
1890 }
1891 if (!(uv & 0x40)) {
1892 *lenp = 1;
1893 rb_raise(rb_eArgError, "malformed UTF-8 character");
1894 }
1895
1896 if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
1897 else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
1898 else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
1899 else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
1900 else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
1901 else {
1902 *lenp = 1;
1903 rb_raise(rb_eArgError, "malformed UTF-8 character");
1904 }
1905 if (n > *lenp) {
1906 rb_raise(rb_eArgError, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
1907 n, *lenp);
1908 }
1909 *lenp = n--;
1910 if (n != 0) {
1911 while (n--) {
1912 c = *p++ & 0xff;
1913 if ((c & 0xc0) != 0x80) {
1914 *lenp -= n + 1;
1915 rb_raise(rb_eArgError, "malformed UTF-8 character");
1916 }
1917 else {
1918 c &= 0x3f;
1919 uv = uv << 6 | c;
1920 }
1921 }
1922 }
1923 n = *lenp - 1;
1924 if (uv < utf8_limits[n]) {
1925 rb_raise(rb_eArgError, "redundant UTF-8 sequence");
1926 }
1927 return uv;
1928}
1929
1930#include "pack.rbinc"
1931
1932void
1933Init_pack(void)
1934{
1935 id_associated = rb_make_internal_id();
1936}
#define RUBY_ALIGNOF
Wraps (or simulates) alignof.
Definition stdalign.h:28
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition eval.c:1035
#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 SSIZET2NUM
Old name of RB_SSIZE2NUM.
Definition size_t.h:64
#define STRTOUL
Old name of ruby_strtoul.
Definition ctype.h:104
#define ISDIGIT
Old name of rb_isdigit.
Definition ctype.h:93
#define ISALPHA
Old name of rb_isalpha.
Definition ctype.h:92
#define ISASCII
Old name of rb_isascii.
Definition ctype.h:85
#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 ISPRINT
Old name of rb_isprint.
Definition ctype.h:86
#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:1477
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1473
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1471
VALUE rb_to_float(VALUE val)
Identical to rb_check_to_float(), except it raises on error.
Definition object.c:3773
VALUE rb_to_int(VALUE val)
Identical to rb_check_to_int(), except it raises in case of conversion mismatch.
Definition object.c:3327
Defines RBIMPL_HAS_BUILTIN.
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:571
#define INTEGER_PACK_BIG_ENDIAN
Big endian combination.
Definition bignum.h:576
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Encodes a Unicode codepoint into its UTF-8 representation.
Definition pack.c:1824
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
Definition bignum.h:553
#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:3485
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2847
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:2801
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1755
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:1887
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:2141
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'->11etc.
Definition util.c:60
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:50
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition variable.c:533
#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
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376