3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * He still hopefully carried some of his gear in his pack: a small tinder-box,
13 * two small shallow pans, the smaller fitting into the larger; inside them a
14 * wooden spoon, a short two-pronged fork and some skewers were stowed; and
15 * hidden at the bottom of the pack in a flat wooden box a dwindling treasure,
18 * [p.653 of _The Lord of the Rings_, IV/iv: "Of Herbs and Stewed Rabbit"]
21 /* This file contains pp ("push/pop") functions that
22 * execute the opcodes that make up a perl program. A typical pp function
23 * expects to find its arguments on the stack, and usually pushes its
24 * results onto the stack, hence the 'pp' terminology. Each OP structure
25 * contains a pointer to the relevant pp_foo() function.
27 * This particular file just contains pp_pack() and pp_unpack(). See the
28 * other pp*.c files for the rest of the pp_ functions.
32 #define PERL_IN_PP_PACK_C
35 /* Types used by pack/unpack */
37 e_no_len, /* no length */
38 e_number, /* number, [] */
42 typedef struct tempsym {
43 const char* patptr; /* current template char */
44 const char* patend; /* one after last char */
45 const char* grpbeg; /* 1st char of ()-group */
46 const char* grpend; /* end of ()-group */
47 I32 code; /* template code (!<>) */
48 I32 length; /* length/repeat count */
49 howlen_t howlen; /* how length is given */
50 int level; /* () nesting level */
51 U32 flags; /* /=4, comma=2, pack=1 */
52 /* and group modifiers */
53 STRLEN strbeg; /* offset of group start */
54 struct tempsym *previous; /* previous group */
57 #define TEMPSYM_INIT(symptr, p, e, f) \
59 (symptr)->patptr = (p); \
60 (symptr)->patend = (e); \
61 (symptr)->grpbeg = NULL; \
62 (symptr)->grpend = NULL; \
63 (symptr)->grpend = NULL; \
65 (symptr)->length = 0; \
66 (symptr)->howlen = e_no_len; \
67 (symptr)->level = 0; \
68 (symptr)->flags = (f); \
69 (symptr)->strbeg = 0; \
70 (symptr)->previous = NULL; \
78 #if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
81 U8 bytes[sizeof(long double)];
88 /* Maximum number of bytes to which a byte can grow due to upgrade */
92 * Offset for integer pack/unpack.
94 * On architectures where I16 and I32 aren't really 16 and 32 bits,
95 * which for now are all Crays, pack and unpack have to play games.
99 * These values are required for portability of pack() output.
100 * If they're not right on your machine, then pack() and unpack()
101 * wouldn't work right anyway; you'll need to apply the Cray hack.
102 * (I'd like to check them with #if, but you can't use sizeof() in
103 * the preprocessor.) --???
106 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
107 defines are now in config.h. --Andy Dougherty April 1998
112 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
115 #if U16SIZE > SIZE16 || U32SIZE > SIZE32
116 # if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 /* little-endian */
117 # define OFF16(p) ((char*)(p))
118 # define OFF32(p) ((char*)(p))
120 # if BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 /* big-endian */
121 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
122 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
124 ++++ bad cray byte order
128 # define OFF16(p) ((char *) (p))
129 # define OFF32(p) ((char *) (p))
132 #define PUSH16(utf8, cur, p) PUSH_BYTES(utf8, cur, OFF16(p), SIZE16)
133 #define PUSH32(utf8, cur, p) PUSH_BYTES(utf8, cur, OFF32(p), SIZE32)
135 #if BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 /* big-endian */
136 # define NEEDS_SWAP(d) (TYPE_ENDIANNESS(d) == TYPE_IS_LITTLE_ENDIAN)
137 #elif BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 /* little-endian */
138 # define NEEDS_SWAP(d) (TYPE_ENDIANNESS(d) == TYPE_IS_BIG_ENDIAN)
140 # error "Unsupported byteorder"
141 /* Need to add code here to re-instate mixed endian support.
142 NEEDS_SWAP would need to hold a flag indicating which action to
143 take, and S_reverse_copy and the code in uni_to_bytes would need
144 logic adding to deal with any mixed-endian transformations needed.
148 /* Only to be used inside a loop (see the break) */
149 #define SHIFT_BYTES(utf8, s, strend, buf, len, datumtype) \
152 if (!uni_to_bytes(aTHX_ &s, strend, \
153 (char *) (buf), len, datumtype)) break; \
156 S_reverse_copy(s, (char *) (buf), len); \
158 Copy(s, (char *) (buf), len, char); \
163 #define SHIFT16(utf8, s, strend, p, datumtype) \
164 SHIFT_BYTES(utf8, s, strend, OFF16(p), SIZE16, datumtype)
166 #define SHIFT32(utf8, s, strend, p, datumtype) \
167 SHIFT_BYTES(utf8, s, strend, OFF32(p), SIZE32, datumtype)
169 #define SHIFT_VAR(utf8, s, strend, var, datumtype) \
170 SHIFT_BYTES(utf8, s, strend, &(var), sizeof(var), datumtype)
172 #define PUSH_VAR(utf8, aptr, var) \
173 PUSH_BYTES(utf8, aptr, &(var), sizeof(var))
175 /* Avoid stack overflow due to pathological templates. 100 should be plenty. */
176 #define MAX_SUB_TEMPLATE_LEVEL 100
178 /* flags (note that type modifiers can also be used as flags!) */
179 #define FLAG_WAS_UTF8 0x40
180 #define FLAG_PARSE_UTF8 0x20 /* Parse as utf8 */
181 #define FLAG_UNPACK_ONLY_ONE 0x10
182 #define FLAG_DO_UTF8 0x08 /* The underlying string is utf8 */
183 #define FLAG_SLASH 0x04
184 #define FLAG_COMMA 0x02
185 #define FLAG_PACK 0x01
188 S_mul128(pTHX_ SV *sv, U8 m)
191 char *s = SvPV(sv, len);
194 PERL_ARGS_ASSERT_MUL128;
196 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
197 SV * const tmpNew = newSVpvs("0000000000");
199 sv_catsv(tmpNew, sv);
200 SvREFCNT_dec(sv); /* free old sv */
205 while (!*t) /* trailing '\0'? */
208 const U32 i = ((*t - '0') << 7) + m;
209 *(t--) = '0' + (char)(i % 10);
215 /* Explosives and implosives. */
217 #if 'I' == 73 && 'J' == 74
218 /* On an ASCII/ISO kind of system */
219 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
222 Some other sort of character set - use memchr() so we don't match
225 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
229 #define TYPE_IS_SHRIEKING 0x100
230 #define TYPE_IS_BIG_ENDIAN 0x200
231 #define TYPE_IS_LITTLE_ENDIAN 0x400
232 #define TYPE_IS_PACK 0x800
233 #define TYPE_ENDIANNESS_MASK (TYPE_IS_BIG_ENDIAN|TYPE_IS_LITTLE_ENDIAN)
234 #define TYPE_MODIFIERS(t) ((t) & ~0xFF)
235 #define TYPE_NO_MODIFIERS(t) ((t) & 0xFF)
237 # define TYPE_ENDIANNESS(t) ((t) & TYPE_ENDIANNESS_MASK)
238 # define TYPE_NO_ENDIANNESS(t) ((t) & ~TYPE_ENDIANNESS_MASK)
240 # define ENDIANNESS_ALLOWED_TYPES "sSiIlLqQjJfFdDpP("
242 # define DO_BO_PACK(var) \
245 my_swabn(&var, sizeof(var)); \
249 #define PACK_SIZE_CANNOT_CSUM 0x80
250 #define PACK_SIZE_UNPREDICTABLE 0x40 /* Not a fixed size element */
251 #define PACK_SIZE_MASK 0x3F
253 #include "packsizetables.c"
256 S_reverse_copy(const char *src, char *dest, STRLEN len)
264 uni_to_byte(pTHX_ const char **s, const char *end, I32 datumtype)
267 UV val = utf8n_to_uvchr((U8 *) *s, end-*s, &retlen,
268 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
269 /* We try to process malformed UTF-8 as much as possible (preferably with
270 warnings), but these two mean we make no progress in the string and
271 might enter an infinite loop */
272 if (retlen == (STRLEN) -1 || retlen == 0)
273 Perl_croak(aTHX_ "Malformed UTF-8 string in '%c' format in unpack",
274 (int) TYPE_NO_MODIFIERS(datumtype));
276 Perl_ck_warner(aTHX_ packWARN(WARN_UNPACK),
277 "Character in '%c' format wrapped in unpack",
278 (int) TYPE_NO_MODIFIERS(datumtype));
285 #define SHIFT_BYTE(utf8, s, strend, datumtype) ((utf8) ? \
286 uni_to_byte(aTHX_ &(s), (strend), (datumtype)) : \
290 uni_to_bytes(pTHX_ const char **s, const char *end, const char *buf, int buf_len, I32 datumtype)
294 const char *from = *s;
296 const U32 flags = ckWARN(WARN_UTF8) ?
297 UTF8_CHECK_ONLY : (UTF8_CHECK_ONLY | UTF8_ALLOW_ANY);
298 const bool needs_swap = NEEDS_SWAP(datumtype);
303 for (;buf_len > 0; buf_len--) {
304 if (from >= end) return FALSE;
305 val = utf8n_to_uvchr((U8 *) from, end-from, &retlen, flags);
306 if (retlen == (STRLEN) -1 || retlen == 0) {
307 from += UTF8SKIP(from);
309 } else from += retlen;
315 *(U8 *)--buf = (U8)val;
317 *(U8 *)buf++ = (U8)val;
319 /* We have enough characters for the buffer. Did we have problems ? */
322 /* Rewalk the string fragment while warning */
324 const int flags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
325 for (ptr = *s; ptr < from; ptr += UTF8SKIP(ptr)) {
326 if (ptr >= end) break;
327 utf8n_to_uvuni((U8 *) ptr, end-ptr, &retlen, flags);
329 if (from > end) from = end;
332 Perl_ck_warner(aTHX_ packWARN(datumtype & TYPE_IS_PACK ?
333 WARN_PACK : WARN_UNPACK),
334 "Character(s) in '%c' format wrapped in %s",
335 (int) TYPE_NO_MODIFIERS(datumtype),
336 datumtype & TYPE_IS_PACK ? "pack" : "unpack");
343 next_uni_uu(pTHX_ const char **s, const char *end, I32 *out)
347 const UV val = utf8n_to_uvchr((U8 *) *s, end-*s, &retlen, UTF8_CHECK_ONLY);
348 if (val >= 0x100 || !ISUUCHAR(val) ||
349 retlen == (STRLEN) -1 || retlen == 0) {
353 *out = PL_uudmap[val] & 077;
359 S_bytes_to_uni(const U8 *start, STRLEN len, char *dest) {
360 const U8 * const end = start + len;
362 PERL_ARGS_ASSERT_BYTES_TO_UNI;
364 while (start < end) {
365 const UV uv = NATIVE_TO_ASCII(*start);
366 if (UNI_IS_INVARIANT(uv))
367 *dest++ = (char)(U8)UTF_TO_NATIVE(uv);
369 *dest++ = (char)(U8)UTF8_EIGHT_BIT_HI(uv);
370 *dest++ = (char)(U8)UTF8_EIGHT_BIT_LO(uv);
377 #define PUSH_BYTES(utf8, cur, buf, len) \
380 (cur) = bytes_to_uni((U8 *) buf, len, (cur)); \
382 Copy(buf, cur, len, char); \
387 #define GROWING(utf8, cat, start, cur, in_len) \
389 STRLEN glen = (in_len); \
390 if (utf8) glen *= UTF8_EXPAND; \
391 if ((cur) + glen >= (start) + SvLEN(cat)) { \
392 (start) = sv_exp_grow(cat, glen); \
393 (cur) = (start) + SvCUR(cat); \
397 #define PUSH_GROWING_BYTES(utf8, cat, start, cur, buf, in_len) \
399 const STRLEN glen = (in_len); \
401 if (utf8) gl *= UTF8_EXPAND; \
402 if ((cur) + gl >= (start) + SvLEN(cat)) { \
404 SvCUR_set((cat), (cur) - (start)); \
405 (start) = sv_exp_grow(cat, gl); \
406 (cur) = (start) + SvCUR(cat); \
408 PUSH_BYTES(utf8, cur, buf, glen); \
411 #define PUSH_BYTE(utf8, s, byte) \
414 const U8 au8 = (byte); \
415 (s) = bytes_to_uni(&au8, 1, (s)); \
416 } else *(U8 *)(s)++ = (byte); \
419 /* Only to be used inside a loop (see the break) */
420 #define NEXT_UNI_VAL(val, cur, str, end, utf8_flags) \
423 if (str >= end) break; \
424 val = utf8n_to_uvchr((U8 *) str, end-str, &retlen, utf8_flags); \
425 if (retlen == (STRLEN) -1 || retlen == 0) { \
427 Perl_croak(aTHX_ "Malformed UTF-8 string in pack"); \
432 static const char *_action( const tempsym_t* symptr )
434 return (const char *)(( symptr->flags & FLAG_PACK ) ? "pack" : "unpack");
437 /* Returns the sizeof() struct described by pat */
439 S_measure_struct(pTHX_ tempsym_t* symptr)
443 PERL_ARGS_ASSERT_MEASURE_STRUCT;
445 while (next_symbol(symptr)) {
449 switch (symptr->howlen) {
451 Perl_croak(aTHX_ "Within []-length '*' not allowed in %s",
455 /* e_no_len and e_number */
456 len = symptr->length;
460 size = packprops[TYPE_NO_ENDIANNESS(symptr->code)] & PACK_SIZE_MASK;
463 /* endianness doesn't influence the size of a type */
464 switch(TYPE_NO_ENDIANNESS(symptr->code)) {
466 Perl_croak(aTHX_ "Invalid type '%c' in %s",
467 (int)TYPE_NO_MODIFIERS(symptr->code),
469 case '.' | TYPE_IS_SHRIEKING:
470 case '@' | TYPE_IS_SHRIEKING:
474 case 'U': /* XXXX Is it correct? */
477 Perl_croak(aTHX_ "Within []-length '%c' not allowed in %s",
478 (int) TYPE_NO_MODIFIERS(symptr->code),
485 tempsym_t savsym = *symptr;
486 symptr->patptr = savsym.grpbeg;
487 symptr->patend = savsym.grpend;
488 /* XXXX Theoretically, we need to measure many times at
489 different positions, since the subexpression may contain
490 alignment commands, but be not of aligned length.
491 Need to detect this and croak(). */
492 size = measure_struct(symptr);
496 case 'X' | TYPE_IS_SHRIEKING:
497 /* XXXX Is this useful? Then need to treat MEASURE_BACKWARDS.
499 if (!len) /* Avoid division by 0 */
501 len = total % len; /* Assumed: the start is aligned. */
506 Perl_croak(aTHX_ "'X' outside of string in %s", _action( symptr ) );
508 case 'x' | TYPE_IS_SHRIEKING:
509 if (!len) /* Avoid division by 0 */
511 star = total % len; /* Assumed: the start is aligned. */
512 if (star) /* Other portable ways? */
536 size = sizeof(char*);
546 /* locate matching closing parenthesis or bracket
547 * returns char pointer to char after match, or NULL
550 S_group_end(pTHX_ const char *patptr, const char *patend, char ender)
552 PERL_ARGS_ASSERT_GROUP_END;
554 while (patptr < patend) {
555 const char c = *patptr++;
562 while (patptr < patend && *patptr != '\n')
566 patptr = group_end(patptr, patend, ')') + 1;
568 patptr = group_end(patptr, patend, ']') + 1;
570 Perl_croak(aTHX_ "No group ending character '%c' found in template",
576 /* Convert unsigned decimal number to binary.
577 * Expects a pointer to the first digit and address of length variable
578 * Advances char pointer to 1st non-digit char and returns number
581 S_get_num(pTHX_ const char *patptr, I32 *lenptr )
583 I32 len = *patptr++ - '0';
585 PERL_ARGS_ASSERT_GET_NUM;
587 while (isDIGIT(*patptr)) {
588 if (len >= 0x7FFFFFFF/10)
589 Perl_croak(aTHX_ "pack/unpack repeat count overflow");
590 len = (len * 10) + (*patptr++ - '0');
596 /* The marvellous template parsing routine: Using state stored in *symptr,
597 * locates next template code and count
600 S_next_symbol(pTHX_ tempsym_t* symptr )
602 const char* patptr = symptr->patptr;
603 const char* const patend = symptr->patend;
605 PERL_ARGS_ASSERT_NEXT_SYMBOL;
607 symptr->flags &= ~FLAG_SLASH;
609 while (patptr < patend) {
610 if (isSPACE(*patptr))
612 else if (*patptr == '#') {
614 while (patptr < patend && *patptr != '\n')
619 /* We should have found a template code */
620 I32 code = *patptr++ & 0xFF;
621 U32 inherited_modifiers = 0;
623 if (code == ','){ /* grandfather in commas but with a warning */
624 if (((symptr->flags & FLAG_COMMA) == 0) && ckWARN(WARN_UNPACK)){
625 symptr->flags |= FLAG_COMMA;
626 Perl_warner(aTHX_ packWARN(WARN_UNPACK),
627 "Invalid type ',' in %s", _action( symptr ) );
632 /* for '(', skip to ')' */
634 if( isDIGIT(*patptr) || *patptr == '*' || *patptr == '[' )
635 Perl_croak(aTHX_ "()-group starts with a count in %s",
637 symptr->grpbeg = patptr;
638 patptr = 1 + ( symptr->grpend = group_end(patptr, patend, ')') );
639 if( symptr->level >= MAX_SUB_TEMPLATE_LEVEL )
640 Perl_croak(aTHX_ "Too deeply nested ()-groups in %s",
644 /* look for group modifiers to inherit */
645 if (TYPE_ENDIANNESS(symptr->flags)) {
646 if (strchr(ENDIANNESS_ALLOWED_TYPES, TYPE_NO_MODIFIERS(code)))
647 inherited_modifiers |= TYPE_ENDIANNESS(symptr->flags);
650 /* look for modifiers */
651 while (patptr < patend) {
656 modifier = TYPE_IS_SHRIEKING;
657 allowed = "sSiIlLxXnNvV@.";
660 modifier = TYPE_IS_BIG_ENDIAN;
661 allowed = ENDIANNESS_ALLOWED_TYPES;
664 modifier = TYPE_IS_LITTLE_ENDIAN;
665 allowed = ENDIANNESS_ALLOWED_TYPES;
676 if (!strchr(allowed, TYPE_NO_MODIFIERS(code)))
677 Perl_croak(aTHX_ "'%c' allowed only after types %s in %s", *patptr,
678 allowed, _action( symptr ) );
680 if (TYPE_ENDIANNESS(code | modifier) == TYPE_ENDIANNESS_MASK)
681 Perl_croak(aTHX_ "Can't use both '<' and '>' after type '%c' in %s",
682 (int) TYPE_NO_MODIFIERS(code), _action( symptr ) );
683 else if (TYPE_ENDIANNESS(code | modifier | inherited_modifiers) ==
684 TYPE_ENDIANNESS_MASK)
685 Perl_croak(aTHX_ "Can't use '%c' in a group with different byte-order in %s",
686 *patptr, _action( symptr ) );
688 if ((code & modifier)) {
689 Perl_ck_warner(aTHX_ packWARN(WARN_UNPACK),
690 "Duplicate modifier '%c' after '%c' in %s",
691 *patptr, (int) TYPE_NO_MODIFIERS(code),
699 /* inherit modifiers */
700 code |= inherited_modifiers;
702 /* look for count and/or / */
703 if (patptr < patend) {
704 if (isDIGIT(*patptr)) {
705 patptr = get_num( patptr, &symptr->length );
706 symptr->howlen = e_number;
708 } else if (*patptr == '*') {
710 symptr->howlen = e_star;
712 } else if (*patptr == '[') {
713 const char* lenptr = ++patptr;
714 symptr->howlen = e_number;
715 patptr = group_end( patptr, patend, ']' ) + 1;
716 /* what kind of [] is it? */
717 if (isDIGIT(*lenptr)) {
718 lenptr = get_num( lenptr, &symptr->length );
720 Perl_croak(aTHX_ "Malformed integer in [] in %s",
723 tempsym_t savsym = *symptr;
724 symptr->patend = patptr-1;
725 symptr->patptr = lenptr;
726 savsym.length = measure_struct(symptr);
730 symptr->howlen = e_no_len;
735 while (patptr < patend) {
736 if (isSPACE(*patptr))
738 else if (*patptr == '#') {
740 while (patptr < patend && *patptr != '\n')
745 if (*patptr == '/') {
746 symptr->flags |= FLAG_SLASH;
748 if (patptr < patend &&
749 (isDIGIT(*patptr) || *patptr == '*' || *patptr == '['))
750 Perl_croak(aTHX_ "'/' does not take a repeat count in %s",
757 /* at end - no count, no / */
758 symptr->howlen = e_no_len;
763 symptr->patptr = patptr;
767 symptr->patptr = patptr;
772 There is no way to cleanly handle the case where we should process the
773 string per byte in its upgraded form while it's really in downgraded form
774 (e.g. estimates like strend-s as an upper bound for the number of
775 characters left wouldn't work). So if we foresee the need of this
776 (pattern starts with U or contains U0), we want to work on the encoded
777 version of the string. Users are advised to upgrade their pack string
778 themselves if they need to do a lot of unpacks like this on it
781 need_utf8(const char *pat, const char *patend)
785 PERL_ARGS_ASSERT_NEED_UTF8;
787 while (pat < patend) {
790 pat = (const char *) memchr(pat, '\n', patend-pat);
791 if (!pat) return FALSE;
792 } else if (pat[0] == 'U') {
793 if (first || pat[1] == '0') return TRUE;
794 } else first = FALSE;
801 first_symbol(const char *pat, const char *patend) {
802 PERL_ARGS_ASSERT_FIRST_SYMBOL;
804 while (pat < patend) {
805 if (pat[0] != '#') return pat[0];
807 pat = (const char *) memchr(pat, '\n', patend-pat);
815 =for apidoc unpackstring
817 The engine implementing the unpack() Perl function.
819 Using the template pat..patend, this function unpacks the string
820 s..strend into a number of mortal SVs, which it pushes onto the perl
821 argument (@_) stack (so you will need to issue a C<PUTBACK> before and
822 C<SPAGAIN> after the call to this function). It returns the number of
825 The strend and patend pointers should point to the byte following the last
826 character of each string.
828 Although this function returns its values on the perl argument stack, it
829 doesn't take any parameters from that stack (and thus in particular
830 there's no need to do a PUSHMARK before calling it, unlike L</call_pv> for
836 Perl_unpackstring(pTHX_ const char *pat, const char *patend, const char *s, const char *strend, U32 flags)
840 PERL_ARGS_ASSERT_UNPACKSTRING;
842 if (flags & FLAG_DO_UTF8) flags |= FLAG_WAS_UTF8;
843 else if (need_utf8(pat, patend)) {
844 /* We probably should try to avoid this in case a scalar context call
845 wouldn't get to the "U0" */
846 STRLEN len = strend - s;
847 s = (char *) bytes_to_utf8((U8 *) s, &len);
850 flags |= FLAG_DO_UTF8;
853 if (first_symbol(pat, patend) != 'U' && (flags & FLAG_DO_UTF8))
854 flags |= FLAG_PARSE_UTF8;
856 TEMPSYM_INIT(&sym, pat, patend, flags);
858 return unpack_rec(&sym, s, s, strend, NULL );
862 S_unpack_rec(pTHX_ tempsym_t* symptr, const char *s, const char *strbeg, const char *strend, const char **new_s )
866 const I32 start_sp_offset = SP - PL_stack_base;
871 const int bits_in_uv = CHAR_BIT * sizeof(cuv);
873 bool explicit_length;
874 const bool unpack_only_one = (symptr->flags & FLAG_UNPACK_ONLY_ONE) != 0;
875 bool utf8 = (symptr->flags & FLAG_PARSE_UTF8) ? 1 : 0;
877 PERL_ARGS_ASSERT_UNPACK_REC;
879 symptr->strbeg = s - strbeg;
881 while (next_symbol(symptr)) {
884 I32 datumtype = symptr->code;
886 /* do first one only unless in list context
887 / is implemented by unpacking the count, then popping it from the
888 stack, so must check that we're not in the middle of a / */
890 && (SP - PL_stack_base == start_sp_offset + 1)
891 && (datumtype != '/') ) /* XXX can this be omitted */
894 switch (howlen = symptr->howlen) {
896 len = strend - strbeg; /* long enough */
899 /* e_no_len and e_number */
900 len = symptr->length;
904 explicit_length = TRUE;
906 beyond = s >= strend;
908 props = packprops[TYPE_NO_ENDIANNESS(datumtype)];
910 /* props nonzero means we can process this letter. */
911 const long size = props & PACK_SIZE_MASK;
912 const long howmany = (strend - s) / size;
916 if (!checksum || (props & PACK_SIZE_CANNOT_CSUM)) {
917 if (len && unpack_only_one) len = 1;
923 needs_swap = NEEDS_SWAP(datumtype);
925 switch(TYPE_NO_ENDIANNESS(datumtype)) {
927 Perl_croak(aTHX_ "Invalid type '%c' in unpack", (int)TYPE_NO_MODIFIERS(datumtype) );
930 if (howlen == e_no_len)
931 len = 16; /* len is not specified */
939 tempsym_t savsym = *symptr;
940 const U32 group_modifiers = TYPE_MODIFIERS(datumtype & ~symptr->flags);
941 symptr->flags |= group_modifiers;
942 symptr->patend = savsym.grpend;
943 symptr->previous = &savsym;
946 if (len && unpack_only_one) len = 1;
948 symptr->patptr = savsym.grpbeg;
949 if (utf8) symptr->flags |= FLAG_PARSE_UTF8;
950 else symptr->flags &= ~FLAG_PARSE_UTF8;
951 unpack_rec(symptr, s, strbeg, strend, &s);
952 if (s == strend && savsym.howlen == e_star)
953 break; /* No way to continue */
956 savsym.flags = symptr->flags & ~group_modifiers;
960 case '.' | TYPE_IS_SHRIEKING:
964 const bool u8 = utf8 && !(datumtype & TYPE_IS_SHRIEKING);
965 if (howlen == e_star) from = strbeg;
966 else if (len <= 0) from = s;
968 tempsym_t *group = symptr;
970 while (--len && group) group = group->previous;
971 from = group ? strbeg + group->strbeg : strbeg;
974 newSVuv( u8 ? (UV) utf8_length((const U8*)from, (const U8*)s) : (UV) (s-from)) :
975 newSViv(-(u8 ? (IV) utf8_length((const U8*)s, (const U8*)from) : (IV) (from-s)));
979 case '@' | TYPE_IS_SHRIEKING:
981 s = strbeg + symptr->strbeg;
982 if (utf8 && !(datumtype & TYPE_IS_SHRIEKING))
986 Perl_croak(aTHX_ "'@' outside of string in unpack");
991 Perl_croak(aTHX_ "'@' outside of string with malformed UTF-8 in unpack");
994 Perl_croak(aTHX_ "'@' outside of string in unpack");
998 case 'X' | TYPE_IS_SHRIEKING:
999 if (!len) /* Avoid division by 0 */
1002 const char *hop, *last;
1004 hop = last = strbeg;
1006 hop += UTF8SKIP(hop);
1013 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1017 len = (s - strbeg) % len;
1023 Perl_croak(aTHX_ "'X' outside of string in unpack");
1024 while (--s, UTF8_IS_CONTINUATION(*s)) {
1026 Perl_croak(aTHX_ "'X' outside of string in unpack");
1031 if (len > s - strbeg)
1032 Perl_croak(aTHX_ "'X' outside of string in unpack" );
1036 case 'x' | TYPE_IS_SHRIEKING: {
1038 if (!len) /* Avoid division by 0 */
1040 if (utf8) ai32 = utf8_length((U8 *) strbeg, (U8 *) s) % len;
1041 else ai32 = (s - strbeg) % len;
1042 if (ai32 == 0) break;
1050 Perl_croak(aTHX_ "'x' outside of string in unpack");
1055 if (len > strend - s)
1056 Perl_croak(aTHX_ "'x' outside of string in unpack");
1061 Perl_croak(aTHX_ "'/' must follow a numeric type in unpack");
1067 /* Preliminary length estimate is assumed done in 'W' */
1068 if (len > strend - s) len = strend - s;
1074 for (l=len, hop=s; l>0; l--, hop += UTF8SKIP(hop)) {
1075 if (hop >= strend) {
1077 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1082 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1084 } else if (len > strend - s)
1087 if (datumtype == 'Z') {
1088 /* 'Z' strips stuff after first null */
1089 const char *ptr, *end;
1091 for (ptr = s; ptr < end; ptr++) if (*ptr == 0) break;
1092 sv = newSVpvn(s, ptr-s);
1093 if (howlen == e_star) /* exact for 'Z*' */
1094 len = ptr-s + (ptr != strend ? 1 : 0);
1095 } else if (datumtype == 'A') {
1096 /* 'A' strips both nulls and spaces */
1098 if (utf8 && (symptr->flags & FLAG_WAS_UTF8)) {
1099 for (ptr = s+len-1; ptr >= s; ptr--)
1100 if (*ptr != 0 && !UTF8_IS_CONTINUATION(*ptr) &&
1101 !isSPACE_utf8(ptr)) break;
1102 if (ptr >= s) ptr += UTF8SKIP(ptr);
1105 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1107 for (ptr = s+len-1; ptr >= s; ptr--)
1108 if (*ptr != 0 && !isSPACE(*ptr)) break;
1111 sv = newSVpvn(s, ptr-s);
1112 } else sv = newSVpvn(s, len);
1116 /* Undo any upgrade done due to need_utf8() */
1117 if (!(symptr->flags & FLAG_WAS_UTF8))
1118 sv_utf8_downgrade(sv, 0);
1126 if (howlen == e_star || len > (strend - s) * 8)
1127 len = (strend - s) * 8;
1130 while (len >= 8 && s < strend) {
1131 cuv += PL_bitcount[uni_to_byte(aTHX_ &s, strend, datumtype)];
1136 cuv += PL_bitcount[*(U8 *)s++];
1139 if (len && s < strend) {
1141 bits = SHIFT_BYTE(utf8, s, strend, datumtype);
1142 if (datumtype == 'b')
1144 if (bits & 1) cuv++;
1149 if (bits & 0x80) cuv++;
1156 sv = sv_2mortal(newSV(len ? len : 1));
1159 if (datumtype == 'b') {
1161 const I32 ai32 = len;
1162 for (len = 0; len < ai32; len++) {
1163 if (len & 7) bits >>= 1;
1165 if (s >= strend) break;
1166 bits = uni_to_byte(aTHX_ &s, strend, datumtype);
1167 } else bits = *(U8 *) s++;
1168 *str++ = bits & 1 ? '1' : '0';
1172 const I32 ai32 = len;
1173 for (len = 0; len < ai32; len++) {
1174 if (len & 7) bits <<= 1;
1176 if (s >= strend) break;
1177 bits = uni_to_byte(aTHX_ &s, strend, datumtype);
1178 } else bits = *(U8 *) s++;
1179 *str++ = bits & 0x80 ? '1' : '0';
1183 SvCUR_set(sv, str - SvPVX_const(sv));
1190 /* Preliminary length estimate, acceptable for utf8 too */
1191 if (howlen == e_star || len > (strend - s) * 2)
1192 len = (strend - s) * 2;
1194 sv = sv_2mortal(newSV(len ? len : 1));
1198 if (datumtype == 'h') {
1201 for (len = 0; len < ai32; len++) {
1202 if (len & 1) bits >>= 4;
1204 if (s >= strend) break;
1205 bits = uni_to_byte(aTHX_ &s, strend, datumtype);
1206 } else bits = * (U8 *) s++;
1208 *str++ = PL_hexdigit[bits & 15];
1212 const I32 ai32 = len;
1213 for (len = 0; len < ai32; len++) {
1214 if (len & 1) bits <<= 4;
1216 if (s >= strend) break;
1217 bits = uni_to_byte(aTHX_ &s, strend, datumtype);
1218 } else bits = *(U8 *) s++;
1220 *str++ = PL_hexdigit[(bits >> 4) & 15];
1225 SvCUR_set(sv, str - SvPVX_const(sv));
1232 if (explicit_length)
1233 /* Switch to "character" mode */
1234 utf8 = (symptr->flags & FLAG_DO_UTF8) ? 1 : 0;
1239 while (len-- > 0 && s < strend) {
1244 aint = utf8n_to_uvchr((U8 *) s, strend-s, &retlen,
1245 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1246 if (retlen == (STRLEN) -1 || retlen == 0)
1247 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1251 aint = *(U8 *)(s)++;
1252 if (aint >= 128 && datumtype != 'C') /* fake up signed chars */
1256 else if (checksum > bits_in_uv)
1257 cdouble += (NV)aint;
1265 while (len-- > 0 && s < strend) {
1267 const UV val = utf8n_to_uvchr((U8 *) s, strend-s, &retlen,
1268 ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
1269 if (retlen == (STRLEN) -1 || retlen == 0)
1270 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1274 else if (checksum > bits_in_uv)
1275 cdouble += (NV) val;
1279 } else if (!checksum)
1281 const U8 ch = *(U8 *) s++;
1284 else if (checksum > bits_in_uv)
1285 while (len-- > 0) cdouble += (NV) *(U8 *) s++;
1287 while (len-- > 0) cuv += *(U8 *) s++;
1291 if (explicit_length && howlen != e_star) {
1292 /* Switch to "bytes in UTF-8" mode */
1293 if (symptr->flags & FLAG_DO_UTF8) utf8 = 0;
1295 /* Should be impossible due to the need_utf8() test */
1296 Perl_croak(aTHX_ "U0 mode on a byte string");
1300 if (len > strend - s) len = strend - s;
1302 if (len && unpack_only_one) len = 1;
1306 while (len-- > 0 && s < strend) {
1310 U8 result[UTF8_MAXLEN];
1311 const char *ptr = s;
1313 /* Bug: warns about bad utf8 even if we are short on bytes
1314 and will break out of the loop */
1315 if (!uni_to_bytes(aTHX_ &ptr, strend, (char *) result, 1,
1318 len = UTF8SKIP(result);
1319 if (!uni_to_bytes(aTHX_ &ptr, strend,
1320 (char *) &result[1], len-1, 'U')) break;
1321 auv = utf8n_to_uvuni(result, len, &retlen, UTF8_ALLOW_DEFAULT);
1324 auv = utf8n_to_uvuni((U8*)s, strend - s, &retlen, UTF8_ALLOW_DEFAULT);
1325 if (retlen == (STRLEN) -1 || retlen == 0)
1326 Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
1331 else if (checksum > bits_in_uv)
1332 cdouble += (NV) auv;
1337 case 's' | TYPE_IS_SHRIEKING:
1338 #if SHORTSIZE != SIZE16
1341 SHIFT_VAR(utf8, s, strend, ashort, datumtype);
1344 else if (checksum > bits_in_uv)
1345 cdouble += (NV)ashort;
1357 #if U16SIZE > SIZE16
1360 SHIFT16(utf8, s, strend, &ai16, datumtype);
1361 #if U16SIZE > SIZE16
1367 else if (checksum > bits_in_uv)
1368 cdouble += (NV)ai16;
1373 case 'S' | TYPE_IS_SHRIEKING:
1374 #if SHORTSIZE != SIZE16
1376 unsigned short aushort;
1377 SHIFT_VAR(utf8, s, strend, aushort, datumtype);
1380 else if (checksum > bits_in_uv)
1381 cdouble += (NV)aushort;
1394 #if U16SIZE > SIZE16
1397 SHIFT16(utf8, s, strend, &au16, datumtype);
1398 if (datumtype == 'n')
1399 au16 = PerlSock_ntohs(au16);
1400 if (datumtype == 'v')
1404 else if (checksum > bits_in_uv)
1405 cdouble += (NV) au16;
1410 case 'v' | TYPE_IS_SHRIEKING:
1411 case 'n' | TYPE_IS_SHRIEKING:
1414 # if U16SIZE > SIZE16
1417 SHIFT16(utf8, s, strend, &ai16, datumtype);
1418 /* There should never be any byte-swapping here. */
1419 assert(!TYPE_ENDIANNESS(datumtype));
1420 if (datumtype == ('n' | TYPE_IS_SHRIEKING))
1421 ai16 = (I16) PerlSock_ntohs((U16) ai16);
1422 if (datumtype == ('v' | TYPE_IS_SHRIEKING))
1423 ai16 = (I16) vtohs((U16) ai16);
1426 else if (checksum > bits_in_uv)
1427 cdouble += (NV) ai16;
1433 case 'i' | TYPE_IS_SHRIEKING:
1436 SHIFT_VAR(utf8, s, strend, aint, datumtype);
1439 else if (checksum > bits_in_uv)
1440 cdouble += (NV)aint;
1446 case 'I' | TYPE_IS_SHRIEKING:
1449 SHIFT_VAR(utf8, s, strend, auint, datumtype);
1452 else if (checksum > bits_in_uv)
1453 cdouble += (NV)auint;
1461 SHIFT_VAR(utf8, s, strend, aiv, datumtype);
1464 else if (checksum > bits_in_uv)
1473 SHIFT_VAR(utf8, s, strend, auv, datumtype);
1476 else if (checksum > bits_in_uv)
1482 case 'l' | TYPE_IS_SHRIEKING:
1483 #if LONGSIZE != SIZE32
1486 SHIFT_VAR(utf8, s, strend, along, datumtype);
1489 else if (checksum > bits_in_uv)
1490 cdouble += (NV)along;
1501 #if U32SIZE > SIZE32
1504 SHIFT32(utf8, s, strend, &ai32, datumtype);
1505 #if U32SIZE > SIZE32
1506 if (ai32 > 2147483647) ai32 -= 4294967296;
1510 else if (checksum > bits_in_uv)
1511 cdouble += (NV)ai32;
1516 case 'L' | TYPE_IS_SHRIEKING:
1517 #if LONGSIZE != SIZE32
1519 unsigned long aulong;
1520 SHIFT_VAR(utf8, s, strend, aulong, datumtype);
1523 else if (checksum > bits_in_uv)
1524 cdouble += (NV)aulong;
1537 #if U32SIZE > SIZE32
1540 SHIFT32(utf8, s, strend, &au32, datumtype);
1541 if (datumtype == 'N')
1542 au32 = PerlSock_ntohl(au32);
1543 if (datumtype == 'V')
1547 else if (checksum > bits_in_uv)
1548 cdouble += (NV)au32;
1553 case 'V' | TYPE_IS_SHRIEKING:
1554 case 'N' | TYPE_IS_SHRIEKING:
1557 #if U32SIZE > SIZE32
1560 SHIFT32(utf8, s, strend, &ai32, datumtype);
1561 /* There should never be any byte swapping here. */
1562 assert(!TYPE_ENDIANNESS(datumtype));
1563 if (datumtype == ('N' | TYPE_IS_SHRIEKING))
1564 ai32 = (I32)PerlSock_ntohl((U32)ai32);
1565 if (datumtype == ('V' | TYPE_IS_SHRIEKING))
1566 ai32 = (I32)vtohl((U32)ai32);
1569 else if (checksum > bits_in_uv)
1570 cdouble += (NV)ai32;
1578 SHIFT_VAR(utf8, s, strend, aptr, datumtype);
1579 /* newSVpv generates undef if aptr is NULL */
1580 mPUSHs(newSVpv(aptr, 0));
1588 while (len > 0 && s < strend) {
1590 ch = SHIFT_BYTE(utf8, s, strend, datumtype);
1591 auv = (auv << 7) | (ch & 0x7f);
1592 /* UTF8_IS_XXXXX not right here - using constant 0x80 */
1600 if (++bytes >= sizeof(UV)) { /* promote to string */
1603 sv = Perl_newSVpvf(aTHX_ "%.*"UVuf, (int)TYPE_DIGITS(UV), auv);
1604 while (s < strend) {
1605 ch = SHIFT_BYTE(utf8, s, strend, datumtype);
1606 sv = mul128(sv, (U8)(ch & 0x7f));
1612 t = SvPV_nolen_const(sv);
1621 if ((s >= strend) && bytes)
1622 Perl_croak(aTHX_ "Unterminated compressed integer in unpack");
1626 if (symptr->howlen == e_star)
1627 Perl_croak(aTHX_ "'P' must have an explicit size in unpack");
1629 if (s + sizeof(char*) <= strend) {
1631 SHIFT_VAR(utf8, s, strend, aptr, datumtype);
1632 /* newSVpvn generates undef if aptr is NULL */
1633 PUSHs(newSVpvn_flags(aptr, len, SVs_TEMP));
1640 SHIFT_VAR(utf8, s, strend, aquad, datumtype);
1642 mPUSHs(aquad >= IV_MIN && aquad <= IV_MAX ?
1643 newSViv((IV)aquad) : newSVnv((NV)aquad));
1644 else if (checksum > bits_in_uv)
1645 cdouble += (NV)aquad;
1653 SHIFT_VAR(utf8, s, strend, auquad, datumtype);
1655 mPUSHs(auquad <= UV_MAX ?
1656 newSVuv((UV)auquad) : newSVnv((NV)auquad));
1657 else if (checksum > bits_in_uv)
1658 cdouble += (NV)auquad;
1663 #endif /* HAS_QUAD */
1664 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
1668 SHIFT_VAR(utf8, s, strend, afloat, datumtype);
1678 SHIFT_VAR(utf8, s, strend, adouble, datumtype);
1688 SHIFT_BYTES(utf8, s, strend, anv.bytes, sizeof(anv.bytes), datumtype);
1695 #if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
1699 SHIFT_BYTES(utf8, s, strend, aldouble.bytes, sizeof(aldouble.bytes), datumtype);
1701 mPUSHn(aldouble.ld);
1703 cdouble += aldouble.ld;
1709 const STRLEN l = (STRLEN) (strend - s) * 3 / 4;
1710 sv = sv_2mortal(newSV(l));
1711 if (l) SvPOK_on(sv);
1714 while (next_uni_uu(aTHX_ &s, strend, &len)) {
1719 next_uni_uu(aTHX_ &s, strend, &a);
1720 next_uni_uu(aTHX_ &s, strend, &b);
1721 next_uni_uu(aTHX_ &s, strend, &c);
1722 next_uni_uu(aTHX_ &s, strend, &d);
1723 hunk[0] = (char)((a << 2) | (b >> 4));
1724 hunk[1] = (char)((b << 4) | (c >> 2));
1725 hunk[2] = (char)((c << 6) | d);
1727 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
1735 /* possible checksum byte */
1736 const char *skip = s+UTF8SKIP(s);
1737 if (skip < strend && *skip == '\n')
1743 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
1747 len = PL_uudmap[*(U8*)s++] & 077;
1749 if (s < strend && ISUUCHAR(*s))
1750 a = PL_uudmap[*(U8*)s++] & 077;
1753 if (s < strend && ISUUCHAR(*s))
1754 b = PL_uudmap[*(U8*)s++] & 077;
1757 if (s < strend && ISUUCHAR(*s))
1758 c = PL_uudmap[*(U8*)s++] & 077;
1761 if (s < strend && ISUUCHAR(*s))
1762 d = PL_uudmap[*(U8*)s++] & 077;
1765 hunk[0] = (char)((a << 2) | (b >> 4));
1766 hunk[1] = (char)((b << 4) | (c >> 2));
1767 hunk[2] = (char)((c << 6) | d);
1769 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
1774 else /* possible checksum byte */
1775 if (s + 1 < strend && s[1] == '\n')
1785 if (strchr("fFdD", TYPE_NO_MODIFIERS(datumtype)) ||
1786 (checksum > bits_in_uv &&
1787 strchr("cCsSiIlLnNUWvVqQjJ", TYPE_NO_MODIFIERS(datumtype))) ) {
1790 anv = (NV) (1 << (checksum & 15));
1791 while (checksum >= 16) {
1795 while (cdouble < 0.0)
1797 cdouble = Perl_modf(cdouble / anv, &trouble) * anv;
1798 sv = newSVnv(cdouble);
1801 if (checksum < bits_in_uv) {
1802 UV mask = ((UV)1 << checksum) - 1;
1811 if (symptr->flags & FLAG_SLASH){
1812 if (SP - PL_stack_base - start_sp_offset <= 0)
1814 if( next_symbol(symptr) ){
1815 if( symptr->howlen == e_number )
1816 Perl_croak(aTHX_ "Count after length/code in unpack" );
1818 /* ...end of char buffer then no decent length available */
1819 Perl_croak(aTHX_ "length/code after end of string in unpack" );
1821 /* take top of stack (hope it's numeric) */
1824 Perl_croak(aTHX_ "Negative '/' count in unpack" );
1827 Perl_croak(aTHX_ "Code missing after '/' in unpack" );
1829 datumtype = symptr->code;
1830 explicit_length = FALSE;
1838 return SP - PL_stack_base - start_sp_offset;
1846 I32 gimme = GIMME_V;
1849 const char *pat = SvPV_const(left, llen);
1850 const char *s = SvPV_const(right, rlen);
1851 const char *strend = s + rlen;
1852 const char *patend = pat + llen;
1856 cnt = unpackstring(pat, patend, s, strend,
1857 ((gimme == G_SCALAR) ? FLAG_UNPACK_ONLY_ONE : 0)
1858 | (DO_UTF8(right) ? FLAG_DO_UTF8 : 0));
1861 if ( !cnt && gimme == G_SCALAR )
1862 PUSHs(&PL_sv_undef);
1867 doencodes(U8 *h, const char *s, I32 len)
1869 *h++ = PL_uuemap[len];
1871 *h++ = PL_uuemap[(077 & (s[0] >> 2))];
1872 *h++ = PL_uuemap[(077 & (((s[0] << 4) & 060) | ((s[1] >> 4) & 017)))];
1873 *h++ = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
1874 *h++ = PL_uuemap[(077 & (s[2] & 077))];
1879 const char r = (len > 1 ? s[1] : '\0');
1880 *h++ = PL_uuemap[(077 & (s[0] >> 2))];
1881 *h++ = PL_uuemap[(077 & (((s[0] << 4) & 060) | ((r >> 4) & 017)))];
1882 *h++ = PL_uuemap[(077 & ((r << 2) & 074))];
1883 *h++ = PL_uuemap[0];
1890 S_is_an_int(pTHX_ const char *s, STRLEN l)
1892 SV *result = newSVpvn(s, l);
1893 char *const result_c = SvPV_nolen(result); /* convenience */
1894 char *out = result_c;
1898 PERL_ARGS_ASSERT_IS_AN_INT;
1906 SvREFCNT_dec(result);
1929 SvREFCNT_dec(result);
1935 SvCUR_set(result, out - result_c);
1939 /* pnum must be '\0' terminated */
1941 S_div128(pTHX_ SV *pnum, bool *done)
1944 char * const s = SvPV(pnum, len);
1948 PERL_ARGS_ASSERT_DIV128;
1952 const int i = m * 10 + (*t - '0');
1953 const int r = (i >> 7); /* r < 10 */
1961 SvCUR_set(pnum, (STRLEN) (t - s));
1966 =for apidoc packlist
1968 The engine implementing pack() Perl function.
1974 Perl_packlist(pTHX_ SV *cat, const char *pat, const char *patend, SV **beglist, SV **endlist )
1979 PERL_ARGS_ASSERT_PACKLIST;
1981 TEMPSYM_INIT(&sym, pat, patend, FLAG_PACK);
1983 /* We're going to do changes through SvPVX(cat). Make sure it's valid.
1984 Also make sure any UTF8 flag is loaded */
1985 SvPV_force_nolen(cat);
1987 sym.flags |= FLAG_PARSE_UTF8 | FLAG_DO_UTF8;
1989 (void)pack_rec( cat, &sym, beglist, endlist );
1992 /* like sv_utf8_upgrade, but also repoint the group start markers */
1994 marked_upgrade(pTHX_ SV *sv, tempsym_t *sym_ptr) {
1997 const char *from_ptr, *from_start, *from_end, **marks, **m;
1998 char *to_start, *to_ptr;
2000 if (SvUTF8(sv)) return;
2002 from_start = SvPVX_const(sv);
2003 from_end = from_start + SvCUR(sv);
2004 for (from_ptr = from_start; from_ptr < from_end; from_ptr++)
2005 if (!NATIVE_IS_INVARIANT(*from_ptr)) break;
2006 if (from_ptr == from_end) {
2007 /* Simple case: no character needs to be changed */
2012 len = (from_end-from_ptr)*UTF8_EXPAND+(from_ptr-from_start)+1;
2013 Newx(to_start, len, char);
2014 Copy(from_start, to_start, from_ptr-from_start, char);
2015 to_ptr = to_start + (from_ptr-from_start);
2017 Newx(marks, sym_ptr->level+2, const char *);
2018 for (group=sym_ptr; group; group = group->previous)
2019 marks[group->level] = from_start + group->strbeg;
2020 marks[sym_ptr->level+1] = from_end+1;
2021 for (m = marks; *m < from_ptr; m++)
2022 *m = to_start + (*m-from_start);
2024 for (;from_ptr < from_end; from_ptr++) {
2025 while (*m == from_ptr) *m++ = to_ptr;
2026 to_ptr = (char *) uvchr_to_utf8((U8 *) to_ptr, *(U8 *) from_ptr);
2030 while (*m == from_ptr) *m++ = to_ptr;
2031 if (m != marks + sym_ptr->level+1) {
2034 Perl_croak(aTHX_ "panic: marks beyond string end, m=%p, marks=%p, "
2035 "level=%d", m, marks, sym_ptr->level);
2037 for (group=sym_ptr; group; group = group->previous)
2038 group->strbeg = marks[group->level] - to_start;
2043 SvLEN_set(sv, SvLEN(sv) + SvIVX(sv));
2044 from_start -= SvIVX(sv);
2047 SvFLAGS(sv) &= ~SVf_OOK;
2050 Safefree(from_start);
2051 SvPV_set(sv, to_start);
2052 SvCUR_set(sv, to_ptr - to_start);
2057 /* Exponential string grower. Makes string extension effectively O(n)
2058 needed says how many extra bytes we need (not counting the final '\0')
2059 Only grows the string if there is an actual lack of space
2062 S_sv_exp_grow(pTHX_ SV *sv, STRLEN needed) {
2063 const STRLEN cur = SvCUR(sv);
2064 const STRLEN len = SvLEN(sv);
2067 PERL_ARGS_ASSERT_SV_EXP_GROW;
2069 if (len - cur > needed) return SvPVX(sv);
2070 extend = needed > len ? needed : len;
2071 return SvGROW(sv, len+extend+1);
2076 S_pack_rec(pTHX_ SV *cat, tempsym_t* symptr, SV **beglist, SV **endlist )
2079 tempsym_t lookahead;
2080 I32 items = endlist - beglist;
2081 bool found = next_symbol(symptr);
2082 bool utf8 = (symptr->flags & FLAG_PARSE_UTF8) ? 1 : 0;
2083 bool warn_utf8 = ckWARN(WARN_UTF8);
2085 PERL_ARGS_ASSERT_PACK_REC;
2087 if (symptr->level == 0 && found && symptr->code == 'U') {
2088 marked_upgrade(aTHX_ cat, symptr);
2089 symptr->flags |= FLAG_DO_UTF8;
2092 symptr->strbeg = SvCUR(cat);
2098 SV *lengthcode = NULL;
2099 I32 datumtype = symptr->code;
2100 howlen_t howlen = symptr->howlen;
2101 char *start = SvPVX(cat);
2102 char *cur = start + SvCUR(cat);
2105 #define NEXTFROM (lengthcode ? lengthcode : items-- > 0 ? *beglist++ : &PL_sv_no)
2109 len = strchr("@Xxu", TYPE_NO_MODIFIERS(datumtype)) ?
2113 /* e_no_len and e_number */
2114 len = symptr->length;
2119 packprops_t props = packprops[TYPE_NO_ENDIANNESS(datumtype)];
2121 if (props && !(props & PACK_SIZE_UNPREDICTABLE)) {
2122 /* We can process this letter. */
2123 STRLEN size = props & PACK_SIZE_MASK;
2124 GROWING(utf8, cat, start, cur, (STRLEN) len * size);
2128 /* Look ahead for next symbol. Do we have code/code? */
2129 lookahead = *symptr;
2130 found = next_symbol(&lookahead);
2131 if (symptr->flags & FLAG_SLASH) {
2133 if (!found) Perl_croak(aTHX_ "Code missing after '/' in pack");
2134 if (strchr("aAZ", lookahead.code)) {
2135 if (lookahead.howlen == e_number) count = lookahead.length;
2138 count = sv_len_utf8(*beglist);
2141 if (lookahead.code == 'Z') count++;
2144 if (lookahead.howlen == e_number && lookahead.length < items)
2145 count = lookahead.length;
2148 lookahead.howlen = e_number;
2149 lookahead.length = count;
2150 lengthcode = sv_2mortal(newSViv(count));
2153 needs_swap = NEEDS_SWAP(datumtype);
2155 /* Code inside the switch must take care to properly update
2156 cat (CUR length and '\0' termination) if it updated *cur and
2157 doesn't simply leave using break */
2158 switch(TYPE_NO_ENDIANNESS(datumtype)) {
2160 Perl_croak(aTHX_ "Invalid type '%c' in pack",
2161 (int) TYPE_NO_MODIFIERS(datumtype));
2163 Perl_croak(aTHX_ "'%%' may not be used in pack");
2166 case '.' | TYPE_IS_SHRIEKING:
2168 if (howlen == e_star) from = start;
2169 else if (len == 0) from = cur;
2171 tempsym_t *group = symptr;
2173 while (--len && group) group = group->previous;
2174 from = group ? start + group->strbeg : start;
2177 len = SvIV(fromstr);
2179 case '@' | TYPE_IS_SHRIEKING:
2181 from = start + symptr->strbeg;
2183 if (utf8 && !(datumtype & TYPE_IS_SHRIEKING))
2185 while (len && from < cur) {
2186 from += UTF8SKIP(from);
2190 Perl_croak(aTHX_ "Malformed UTF-8 string in pack");
2192 /* Here we know from == cur */
2194 GROWING(0, cat, start, cur, len);
2195 Zero(cur, len, char);
2197 } else if (from < cur) {
2200 } else goto no_change;
2208 if (len > 0) goto grow;
2209 if (len == 0) goto no_change;
2216 tempsym_t savsym = *symptr;
2217 U32 group_modifiers = TYPE_MODIFIERS(datumtype & ~symptr->flags);
2218 symptr->flags |= group_modifiers;
2219 symptr->patend = savsym.grpend;
2221 symptr->previous = &lookahead;
2224 if (utf8) symptr->flags |= FLAG_PARSE_UTF8;
2225 else symptr->flags &= ~FLAG_PARSE_UTF8;
2226 was_utf8 = SvUTF8(cat);
2227 symptr->patptr = savsym.grpbeg;
2228 beglist = pack_rec(cat, symptr, beglist, endlist);
2229 if (SvUTF8(cat) != was_utf8)
2230 /* This had better be an upgrade while in utf8==0 mode */
2233 if (savsym.howlen == e_star && beglist == endlist)
2234 break; /* No way to continue */
2236 items = endlist - beglist;
2237 lookahead.flags = symptr->flags & ~group_modifiers;
2240 case 'X' | TYPE_IS_SHRIEKING:
2241 if (!len) /* Avoid division by 0 */
2248 hop += UTF8SKIP(hop);
2255 Perl_croak(aTHX_ "Malformed UTF-8 string in pack");
2259 len = (cur-start) % len;
2263 if (len < 1) goto no_change;
2267 Perl_croak(aTHX_ "'%c' outside of string in pack",
2268 (int) TYPE_NO_MODIFIERS(datumtype));
2269 while (--cur, UTF8_IS_CONTINUATION(*cur)) {
2271 Perl_croak(aTHX_ "'%c' outside of string in pack",
2272 (int) TYPE_NO_MODIFIERS(datumtype));
2278 if (cur - start < len)
2279 Perl_croak(aTHX_ "'%c' outside of string in pack",
2280 (int) TYPE_NO_MODIFIERS(datumtype));
2283 if (cur < start+symptr->strbeg) {
2284 /* Make sure group starts don't point into the void */
2286 const STRLEN length = cur-start;
2287 for (group = symptr;
2288 group && length < group->strbeg;
2289 group = group->previous) group->strbeg = length;
2290 lookahead.strbeg = length;
2293 case 'x' | TYPE_IS_SHRIEKING: {
2295 if (!len) /* Avoid division by 0 */
2297 if (utf8) ai32 = utf8_length((U8 *) start, (U8 *) cur) % len;
2298 else ai32 = (cur - start) % len;
2299 if (ai32 == 0) goto no_change;
2311 aptr = SvPV_const(fromstr, fromlen);
2312 if (DO_UTF8(fromstr)) {
2313 const char *end, *s;
2315 if (!utf8 && !SvUTF8(cat)) {
2316 marked_upgrade(aTHX_ cat, symptr);
2317 lookahead.flags |= FLAG_DO_UTF8;
2318 lookahead.strbeg = symptr->strbeg;
2321 cur = start + SvCUR(cat);
2323 if (howlen == e_star) {
2324 if (utf8) goto string_copy;
2328 end = aptr + fromlen;
2329 fromlen = datumtype == 'Z' ? len-1 : len;
2330 while ((I32) fromlen > 0 && s < end) {
2335 Perl_croak(aTHX_ "Malformed UTF-8 string in pack");
2338 if (datumtype == 'Z') len++;
2344 fromlen = len - fromlen;
2345 if (datumtype == 'Z') fromlen--;
2346 if (howlen == e_star) {
2348 if (datumtype == 'Z') len++;
2350 GROWING(0, cat, start, cur, len);
2351 if (!uni_to_bytes(aTHX_ &aptr, end, cur, fromlen,
2352 datumtype | TYPE_IS_PACK))
2353 Perl_croak(aTHX_ "panic: predicted utf8 length not available, "
2354 "for '%c', aptr=%p end=%p cur=%p, fromlen=%"UVuf,
2355 (int)datumtype, aptr, end, cur, (UV)fromlen);
2359 if (howlen == e_star) {
2361 if (datumtype == 'Z') len++;
2363 if (len <= (I32) fromlen) {
2365 if (datumtype == 'Z' && fromlen > 0) fromlen--;
2367 /* assumes a byte expands to at most UTF8_EXPAND bytes on
2369 expected_length <= from_len*UTF8_EXPAND + (len-from_len) */
2370 GROWING(0, cat, start, cur, fromlen*(UTF8_EXPAND-1)+len);
2372 while (fromlen > 0) {
2373 cur = (char *) uvchr_to_utf8((U8 *) cur, * (U8 *) aptr);
2379 if (howlen == e_star) {
2381 if (datumtype == 'Z') len++;
2383 if (len <= (I32) fromlen) {
2385 if (datumtype == 'Z' && fromlen > 0) fromlen--;
2387 GROWING(0, cat, start, cur, len);
2388 Copy(aptr, cur, fromlen, char);
2392 memset(cur, datumtype == 'A' ? ' ' : '\0', len);
2399 const char *str, *end;
2406 str = SvPV_const(fromstr, fromlen);
2407 end = str + fromlen;
2408 if (DO_UTF8(fromstr)) {
2410 utf8_flags = warn_utf8 ? 0 : UTF8_ALLOW_ANY;
2412 utf8_source = FALSE;
2413 utf8_flags = 0; /* Unused, but keep compilers happy */
2415 if (howlen == e_star) len = fromlen;
2416 field_len = (len+7)/8;
2417 GROWING(utf8, cat, start, cur, field_len);
2418 if (len > (I32)fromlen) len = fromlen;
2421 if (datumtype == 'B')
2425 NEXT_UNI_VAL(val, cur, str, end, utf8_flags);
2427 } else bits |= *str++ & 1;
2428 if (l & 7) bits <<= 1;
2430 PUSH_BYTE(utf8, cur, bits);
2435 /* datumtype == 'b' */
2439 NEXT_UNI_VAL(val, cur, str, end, utf8_flags);
2440 if (val & 1) bits |= 0x80;
2441 } else if (*str++ & 1)
2443 if (l & 7) bits >>= 1;
2445 PUSH_BYTE(utf8, cur, bits);
2451 if (datumtype == 'B')
2452 bits <<= 7 - (l & 7);
2454 bits >>= 7 - (l & 7);
2455 PUSH_BYTE(utf8, cur, bits);
2458 /* Determine how many chars are left in the requested field */
2460 if (howlen == e_star) field_len = 0;
2461 else field_len -= l;
2462 Zero(cur, field_len, char);
2468 const char *str, *end;
2475 str = SvPV_const(fromstr, fromlen);
2476 end = str + fromlen;
2477 if (DO_UTF8(fromstr)) {
2479 utf8_flags = warn_utf8 ? 0 : UTF8_ALLOW_ANY;
2481 utf8_source = FALSE;
2482 utf8_flags = 0; /* Unused, but keep compilers happy */
2484 if (howlen == e_star) len = fromlen;
2485 field_len = (len+1)/2;
2486 GROWING(utf8, cat, start, cur, field_len);
2487 if (!utf8 && len > (I32)fromlen) len = fromlen;
2490 if (datumtype == 'H')
2494 NEXT_UNI_VAL(val, cur, str, end, utf8_flags);
2495 if (val < 256 && isALPHA(val))
2496 bits |= (val + 9) & 0xf;
2499 } else if (isALPHA(*str))
2500 bits |= (*str++ + 9) & 0xf;
2502 bits |= *str++ & 0xf;
2503 if (l & 1) bits <<= 4;
2505 PUSH_BYTE(utf8, cur, bits);
2513 NEXT_UNI_VAL(val, cur, str, end, utf8_flags);
2514 if (val < 256 && isALPHA(val))
2515 bits |= ((val + 9) & 0xf) << 4;
2517 bits |= (val & 0xf) << 4;
2518 } else if (isALPHA(*str))
2519 bits |= ((*str++ + 9) & 0xf) << 4;
2521 bits |= (*str++ & 0xf) << 4;
2522 if (l & 1) bits >>= 4;
2524 PUSH_BYTE(utf8, cur, bits);
2530 PUSH_BYTE(utf8, cur, bits);
2533 /* Determine how many chars are left in the requested field */
2535 if (howlen == e_star) field_len = 0;
2536 else field_len -= l;
2537 Zero(cur, field_len, char);
2545 aiv = SvIV(fromstr);
2546 if ((-128 > aiv || aiv > 127))
2547 Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
2548 "Character in 'c' format wrapped in pack");
2549 PUSH_BYTE(utf8, cur, (U8)(aiv & 0xff));
2554 utf8 = (symptr->flags & FLAG_DO_UTF8) ? 1 : 0;
2560 aiv = SvIV(fromstr);
2561 if ((0 > aiv || aiv > 0xff))
2562 Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
2563 "Character in 'C' format wrapped in pack");
2564 PUSH_BYTE(utf8, cur, (U8)(aiv & 0xff));
2569 U8 in_bytes = (U8)IN_BYTES;
2571 end = start+SvLEN(cat)-1;
2572 if (utf8) end -= UTF8_MAXLEN-1;
2576 auv = SvUV(fromstr);
2577 if (in_bytes) auv = auv % 0x100;
2582 SvCUR_set(cat, cur - start);
2584 GROWING(0, cat, start, cur, len+UTF8_MAXLEN);
2585 end = start+SvLEN(cat)-UTF8_MAXLEN;
2587 cur = (char *) uvuni_to_utf8_flags((U8 *) cur,
2590 0 : UNICODE_ALLOW_ANY);
2595 SvCUR_set(cat, cur - start);
2596 marked_upgrade(aTHX_ cat, symptr);
2597 lookahead.flags |= FLAG_DO_UTF8;
2598 lookahead.strbeg = symptr->strbeg;
2601 cur = start + SvCUR(cat);
2602 end = start+SvLEN(cat)-UTF8_MAXLEN;
2605 Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
2606 "Character in 'W' format wrapped in pack");
2611 SvCUR_set(cat, cur - start);
2612 GROWING(0, cat, start, cur, len+1);
2613 end = start+SvLEN(cat)-1;
2615 *(U8 *) cur++ = (U8)auv;
2624 if (!(symptr->flags & FLAG_DO_UTF8)) {
2625 marked_upgrade(aTHX_ cat, symptr);
2626 lookahead.flags |= FLAG_DO_UTF8;
2627 lookahead.strbeg = symptr->strbeg;
2633 end = start+SvLEN(cat);
2634 if (!utf8) end -= UTF8_MAXLEN;
2638 auv = SvUV(fromstr);
2640 U8 buffer[UTF8_MAXLEN], *endb;
2641 endb = uvuni_to_utf8_flags(buffer, auv,
2643 0 : UNICODE_ALLOW_ANY);
2644 if (cur+(endb-buffer)*UTF8_EXPAND >= end) {
2646 SvCUR_set(cat, cur - start);
2647 GROWING(0, cat, start, cur,
2648 len+(endb-buffer)*UTF8_EXPAND);
2649 end = start+SvLEN(cat);
2651 cur = bytes_to_uni(buffer, endb-buffer, cur);
2655 SvCUR_set(cat, cur - start);
2656 GROWING(0, cat, start, cur, len+UTF8_MAXLEN);
2657 end = start+SvLEN(cat)-UTF8_MAXLEN;
2659 cur = (char *) uvuni_to_utf8_flags((U8 *) cur, auv,
2661 0 : UNICODE_ALLOW_ANY);
2666 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
2672 anv = SvNV(fromstr);
2673 # if defined(VMS) && !defined(_IEEE_FP)
2674 /* IEEE fp overflow shenanigans are unavailable on VAX and optional
2675 * on Alpha; fake it if we don't have them.
2679 else if (anv < -FLT_MAX)
2681 else afloat = (float)anv;
2683 afloat = (float)anv;
2686 PUSH_VAR(utf8, cur, afloat);
2694 anv = SvNV(fromstr);
2695 # if defined(VMS) && !defined(_IEEE_FP)
2696 /* IEEE fp overflow shenanigans are unavailable on VAX and optional
2697 * on Alpha; fake it if we don't have them.
2701 else if (anv < -DBL_MAX)
2703 else adouble = (double)anv;
2705 adouble = (double)anv;
2707 DO_BO_PACK(adouble);
2708 PUSH_VAR(utf8, cur, adouble);
2713 Zero(&anv, 1, NV); /* can be long double with unused bits */
2717 /* to work round a gcc/x86 bug; don't use SvNV */
2718 anv.nv = sv_2nv(fromstr);
2720 anv.nv = SvNV(fromstr);
2723 PUSH_BYTES(utf8, cur, anv.bytes, sizeof(anv.bytes));
2727 #if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
2730 /* long doubles can have unused bits, which may be nonzero */
2731 Zero(&aldouble, 1, long double);
2735 /* to work round a gcc/x86 bug; don't use SvNV */
2736 aldouble.ld = (long double)sv_2nv(fromstr);
2738 aldouble.ld = (long double)SvNV(fromstr);
2740 DO_BO_PACK(aldouble);
2741 PUSH_BYTES(utf8, cur, aldouble.bytes, sizeof(aldouble.bytes));
2746 case 'n' | TYPE_IS_SHRIEKING:
2751 ai16 = (I16)SvIV(fromstr);
2752 ai16 = PerlSock_htons(ai16);
2753 PUSH16(utf8, cur, &ai16);
2756 case 'v' | TYPE_IS_SHRIEKING:
2761 ai16 = (I16)SvIV(fromstr);
2763 PUSH16(utf8, cur, &ai16);
2766 case 'S' | TYPE_IS_SHRIEKING:
2767 #if SHORTSIZE != SIZE16
2769 unsigned short aushort;
2771 aushort = SvUV(fromstr);
2772 DO_BO_PACK(aushort);
2773 PUSH_VAR(utf8, cur, aushort);
2783 au16 = (U16)SvUV(fromstr);
2785 PUSH16(utf8, cur, &au16);
2788 case 's' | TYPE_IS_SHRIEKING:
2789 #if SHORTSIZE != SIZE16
2793 ashort = SvIV(fromstr);
2795 PUSH_VAR(utf8, cur, ashort);
2805 ai16 = (I16)SvIV(fromstr);
2807 PUSH16(utf8, cur, &ai16);
2811 case 'I' | TYPE_IS_SHRIEKING:
2815 auint = SvUV(fromstr);
2817 PUSH_VAR(utf8, cur, auint);
2824 aiv = SvIV(fromstr);
2826 PUSH_VAR(utf8, cur, aiv);
2833 auv = SvUV(fromstr);
2835 PUSH_VAR(utf8, cur, auv);
2842 anv = SvNV(fromstr);
2846 SvCUR_set(cat, cur - start);
2847 Perl_croak(aTHX_ "Cannot compress negative numbers in pack");
2850 /* 0xFFFFFFFFFFFFFFFF may cast to 18446744073709551616.0,
2851 which is == UV_MAX_P1. IOK is fine (instead of UV_only), as
2852 any negative IVs will have already been got by the croak()
2853 above. IOK is untrue for fractions, so we test them
2854 against UV_MAX_P1. */
2855 if (SvIOK(fromstr) || anv < UV_MAX_P1) {
2856 char buf[(sizeof(UV)*CHAR_BIT)/7+1];
2857 char *in = buf + sizeof(buf);
2858 UV auv = SvUV(fromstr);
2861 *--in = (char)((auv & 0x7f) | 0x80);
2864 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
2865 PUSH_GROWING_BYTES(utf8, cat, start, cur,
2866 in, (buf + sizeof(buf)) - in);
2867 } else if (SvPOKp(fromstr))
2869 else if (SvNOKp(fromstr)) {
2870 /* 10**NV_MAX_10_EXP is the largest power of 10
2871 so 10**(NV_MAX_10_EXP+1) is definitely unrepresentable
2872 given 10**(NV_MAX_10_EXP+1) == 128 ** x solve for x:
2873 x = (NV_MAX_10_EXP+1) * log (10) / log (128)
2874 And with that many bytes only Inf can overflow.
2875 Some C compilers are strict about integral constant
2876 expressions so we conservatively divide by a slightly
2877 smaller integer instead of multiplying by the exact
2878 floating-point value.
2880 #ifdef NV_MAX_10_EXP
2881 /* char buf[1 + (int)((NV_MAX_10_EXP + 1) * 0.47456)]; -- invalid C */
2882 char buf[1 + (int)((NV_MAX_10_EXP + 1) / 2)]; /* valid C */
2884 /* char buf[1 + (int)((308 + 1) * 0.47456)]; -- invalid C */
2885 char buf[1 + (int)((308 + 1) / 2)]; /* valid C */
2887 char *in = buf + sizeof(buf);
2889 anv = Perl_floor(anv);
2891 const NV next = Perl_floor(anv / 128);
2892 if (in <= buf) /* this cannot happen ;-) */
2893 Perl_croak(aTHX_ "Cannot compress integer in pack");
2894 *--in = (unsigned char)(anv - (next * 128)) | 0x80;
2897 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
2898 PUSH_GROWING_BYTES(utf8, cat, start, cur,
2899 in, (buf + sizeof(buf)) - in);
2908 /* Copy string and check for compliance */
2909 from = SvPV_const(fromstr, len);
2910 if ((norm = is_an_int(from, len)) == NULL)
2911 Perl_croak(aTHX_ "Can only compress unsigned integers in pack");
2913 Newx(result, len, char);
2916 while (!done) *--in = div128(norm, &done) | 0x80;
2917 result[len - 1] &= 0x7F; /* clear continue bit */
2918 PUSH_GROWING_BYTES(utf8, cat, start, cur,
2919 in, (result + len) - in);
2921 SvREFCNT_dec(norm); /* free norm */
2926 case 'i' | TYPE_IS_SHRIEKING:
2930 aint = SvIV(fromstr);
2932 PUSH_VAR(utf8, cur, aint);
2935 case 'N' | TYPE_IS_SHRIEKING:
2940 au32 = SvUV(fromstr);
2941 au32 = PerlSock_htonl(au32);
2942 PUSH32(utf8, cur, &au32);
2945 case 'V' | TYPE_IS_SHRIEKING:
2950 au32 = SvUV(fromstr);
2952 PUSH32(utf8, cur, &au32);
2955 case 'L' | TYPE_IS_SHRIEKING:
2956 #if LONGSIZE != SIZE32
2958 unsigned long aulong;
2960 aulong = SvUV(fromstr);
2962 PUSH_VAR(utf8, cur, aulong);
2972 au32 = SvUV(fromstr);
2974 PUSH32(utf8, cur, &au32);
2977 case 'l' | TYPE_IS_SHRIEKING:
2978 #if LONGSIZE != SIZE32
2982 along = SvIV(fromstr);
2984 PUSH_VAR(utf8, cur, along);
2994 ai32 = SvIV(fromstr);
2996 PUSH32(utf8, cur, &ai32);
3004 auquad = (Uquad_t) SvUV(fromstr);
3006 PUSH_VAR(utf8, cur, auquad);
3013 aquad = (Quad_t)SvIV(fromstr);
3015 PUSH_VAR(utf8, cur, aquad);
3018 #endif /* HAS_QUAD */
3020 len = 1; /* assume SV is correct length */
3021 GROWING(utf8, cat, start, cur, sizeof(char *));
3028 SvGETMAGIC(fromstr);
3029 if (!SvOK(fromstr)) aptr = NULL;
3031 /* XXX better yet, could spirit away the string to
3032 * a safe spot and hang on to it until the result
3033 * of pack() (and all copies of the result) are
3036 if ((SvTEMP(fromstr) || (SvPADTMP(fromstr) &&
3037 !SvREADONLY(fromstr)))) {
3038 Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
3039 "Attempt to pack pointer to temporary value");
3041 if (SvPOK(fromstr) || SvNIOK(fromstr))
3042 aptr = SvPV_nomg_const_nolen(fromstr);
3044 aptr = SvPV_force_flags_nolen(fromstr, 0);
3047 PUSH_VAR(utf8, cur, aptr);
3051 const char *aptr, *aend;
3055 if (len <= 2) len = 45;
3056 else len = len / 3 * 3;
3058 Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
3059 "Field too wide in 'u' format in pack");
3062 aptr = SvPV_const(fromstr, fromlen);
3063 from_utf8 = DO_UTF8(fromstr);
3065 aend = aptr + fromlen;
3066 fromlen = sv_len_utf8_nomg(fromstr);
3067 } else aend = NULL; /* Unused, but keep compilers happy */
3068 GROWING(utf8, cat, start, cur, (fromlen+2) / 3 * 4 + (fromlen+len-1)/len * 2);
3069 while (fromlen > 0) {
3072 U8 hunk[1+63/3*4+1];
3074 if ((I32)fromlen > len)
3080 if (!uni_to_bytes(aTHX_ &aptr, aend, buffer, todo,
3081 'u' | TYPE_IS_PACK)) {
3083 SvCUR_set(cat, cur - start);
3084 Perl_croak(aTHX_ "panic: string is shorter than advertised, "
3085 "aptr=%p, aend=%p, buffer=%p, todo=%ld",
3086 aptr, aend, buffer, (long) todo);
3088 end = doencodes(hunk, buffer, todo);
3090 end = doencodes(hunk, aptr, todo);
3093 PUSH_BYTES(utf8, cur, hunk, end-hunk);
3100 SvCUR_set(cat, cur - start);
3102 *symptr = lookahead;
3111 dVAR; dSP; dMARK; dORIGMARK; dTARGET;
3114 SV *pat_sv = *++MARK;
3115 const char *pat = SvPV_const(pat_sv, fromlen);
3116 const char *patend = pat + fromlen;
3122 packlist(cat, pat, patend, MARK, SP + 1);
3132 * c-indentation-style: bsd
3134 * indent-tabs-mode: nil
3137 * ex: set ts=8 sts=4 sw=4 et: