3 * Copyright (c) 1991-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * He still hopefully carried some of his gear in his pack: a small tinder-box,
12 * two small shallow pans, the smaller fitting into the larger; inside them a
13 * wooden spoon, a short two-pronged fork and some skewers were stowed; and
14 * hidden at the bottom of the pack in a flat wooden box a dwindling treasure,
19 #define PERL_IN_PP_PACK_C
23 * The compiler on Concurrent CX/UX systems has a subtle bug which only
24 * seems to show up when compiling pp.c - it generates the wrong double
25 * precision constant value for (double)UV_MAX when used inline in the body
26 * of the code below, so this makes a static variable up front (which the
27 * compiler seems to get correct) and uses it in place of UV_MAX below.
29 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
30 static double UV_MAX_cxux = ((double)UV_MAX);
34 * Offset for integer pack/unpack.
36 * On architectures where I16 and I32 aren't really 16 and 32 bits,
37 * which for now are all Crays, pack and unpack have to play games.
41 * These values are required for portability of pack() output.
42 * If they're not right on your machine, then pack() and unpack()
43 * wouldn't work right anyway; you'll need to apply the Cray hack.
44 * (I'd like to check them with #if, but you can't use sizeof() in
45 * the preprocessor.) --???
48 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
49 defines are now in config.h. --Andy Dougherty April 1998
54 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
57 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
58 # define PERL_NATINT_PACK
61 #if LONGSIZE > 4 && defined(_CRAY)
62 # if BYTEORDER == 0x12345678
63 # define OFF16(p) (char*)(p)
64 # define OFF32(p) (char*)(p)
66 # if BYTEORDER == 0x87654321
67 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
68 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
70 }}}} bad cray byte order
73 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
74 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
75 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
76 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
77 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
79 # define COPY16(s,p) Copy(s, p, SIZE16, char)
80 # define COPY32(s,p) Copy(s, p, SIZE32, char)
81 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
82 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
83 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
87 S_mul128(pTHX_ SV *sv, U8 m)
90 char *s = SvPV(sv, len);
94 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
95 SV *tmpNew = newSVpvn("0000000000", 10);
98 SvREFCNT_dec(sv); /* free old sv */
103 while (!*t) /* trailing '\0'? */
106 i = ((*t - '0') << 7) + m;
107 *(t--) = '0' + (i % 10);
113 /* Explosives and implosives. */
115 #if 'I' == 73 && 'J' == 74
116 /* On an ASCII/ISO kind of system */
117 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
120 Some other sort of character set - use memchr() so we don't match
123 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
126 #define UNPACK_ONLY_ONE 0x1
127 #define UNPACK_DO_UTF8 0x2
130 S_group_end(pTHX_ register char *pat, register char *patend, char ender)
132 while (pat < patend) {
140 while (pat < patend && *pat != '\n')
144 pat = group_end(pat, patend, ')') + 1;
146 croak("No group ending character `%c' found", ender);
149 /* Returns -1 on no count or on star */
151 S_find_count(pTHX_ char **ppat, register char *patend, int *star)
153 register char *pat = *ppat;
159 else if (*pat == '*') {
164 else if (isDIGIT(*pat) || *pat == '[') {
165 bool brackets = *pat == '[';
171 while (isDIGIT(*pat)) {
172 len = (len * 10) + (*pat++ - '0');
174 croak("Repeat count in unpack overflows");
176 if (brackets && *pat++ != ']')
177 croak("No repeat count ender ] found after digits");
186 S_next_symbol(pTHX_ register char *pat, register char *patend)
188 while (pat < patend) {
191 else if (*pat == '#') {
193 while (pat < patend && *pat != '\n')
206 =for apidoc unpack_str
208 The engine implementing unpack() Perl function.
213 Perl_unpack_str(pTHX_ char *pat, register char *patend, register char *s, char *strbeg, char *strend, char **new_s, I32 ocnt, U32 flags)
218 register I32 bits = 0;
221 I32 start_sp_offset = SP - PL_stack_base;
223 /* These must not be in registers: */
242 const int bits_in_uv = 8 * sizeof(culong);
244 int star; /* 1 if count is *, -1 if no count given, -2 for / */
245 #ifdef PERL_NATINT_PACK
246 int natint; /* native integer */
247 int unatint; /* unsigned native integer */
249 bool do_utf8 = flags & UNPACK_DO_UTF8;
251 while ((pat = next_symbol(pat, patend)) < patend) {
252 datumtype = *pat++ & 0xFF;
253 #ifdef PERL_NATINT_PACK
257 char *natstr = "sSiIlL";
259 if (strchr(natstr, datumtype)) {
260 #ifdef PERL_NATINT_PACK
266 croak("'!' allowed only after types %s", natstr);
268 len = find_count(&pat, patend, &star);
270 len = strend - strbeg; /* long enough */
271 else if (star < 0) /* No explicit len */
272 len = datumtype != '@';
277 croak("Invalid type in unpack: '%c'", (int)datumtype);
278 case ',': /* grandfather in commas but with a warning */
279 if (commas++ == 0 && ckWARN(WARN_UNPACK))
280 Perl_warner(aTHX_ WARN_UNPACK,
281 "Invalid type in unpack: '%c'", (int)datumtype);
284 if (len == 1 && pat[-1] != '1' && pat[-1] != ']')
285 len = 16; /* len is not specified */
294 char *ss = s; /* Move from register */
297 croak("()-group starts with a count");
298 aptr = group_end(beg, patend, ')');
301 len = find_count(&pat, patend, &star);
302 if (star < 0) /* No count */
304 else if (star > 0) /* Star */
305 len = strend - strbeg; /* long enough? */
309 unpack_str(beg, aptr, ss, strbeg, strend, &ss,
310 ocnt + SP - PL_stack_base - start_sp_offset, flags);
311 if (star > 0 && ss == strend)
312 break; /* No way to continue */
319 if (len > strend - strbeg)
320 croak("@ outside of string");
324 if (len > s - strbeg)
325 croak("X outside of string");
329 if (len > strend - s)
330 croak("x outside of string");
334 if (ocnt + SP - PL_stack_base - start_sp_offset <= 0)
335 croak("/ must follow a numeric type");
338 pat++; /* ignore '*' for compatibility with pack */
340 croak("/ cannot take a count" );
347 if (len > strend - s)
352 sv_setpvn(sv, s, len);
353 if (datumtype == 'A' || datumtype == 'Z') {
354 aptr = s; /* borrow register */
355 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
359 if (star > 0) /* exact for 'Z*' */
360 len = s - SvPVX(sv) + 1;
362 else { /* 'A' strips both nulls and spaces */
363 s = SvPVX(sv) + len - 1;
364 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
368 SvCUR_set(sv, s - SvPVX(sv));
369 s = aptr; /* unborrow register */
372 XPUSHs(sv_2mortal(sv));
376 if (star > 0 || len > (strend - s) * 8)
377 len = (strend - s) * 8;
380 Newz(601, PL_bitcount, 256, char);
381 for (bits = 1; bits < 256; bits++) {
382 if (bits & 1) PL_bitcount[bits]++;
383 if (bits & 2) PL_bitcount[bits]++;
384 if (bits & 4) PL_bitcount[bits]++;
385 if (bits & 8) PL_bitcount[bits]++;
386 if (bits & 16) PL_bitcount[bits]++;
387 if (bits & 32) PL_bitcount[bits]++;
388 if (bits & 64) PL_bitcount[bits]++;
389 if (bits & 128) PL_bitcount[bits]++;
393 culong += PL_bitcount[*(unsigned char*)s++];
398 if (datumtype == 'b') {
400 if (bits & 1) culong++;
406 if (bits & 128) culong++;
413 sv = NEWSV(35, len + 1);
417 if (datumtype == 'b') {
419 for (len = 0; len < aint; len++) {
420 if (len & 7) /*SUPPRESS 595*/
424 *str++ = '0' + (bits & 1);
429 for (len = 0; len < aint; len++) {
434 *str++ = '0' + ((bits & 128) != 0);
438 XPUSHs(sv_2mortal(sv));
442 if (star > 0 || len > (strend - s) * 2)
443 len = (strend - s) * 2;
444 sv = NEWSV(35, len + 1);
448 if (datumtype == 'h') {
450 for (len = 0; len < aint; len++) {
455 *str++ = PL_hexdigit[bits & 15];
460 for (len = 0; len < aint; len++) {
465 *str++ = PL_hexdigit[(bits >> 4) & 15];
469 XPUSHs(sv_2mortal(sv));
472 if (len > strend - s)
477 if (aint >= 128) /* fake up signed chars */
479 if (checksum > bits_in_uv)
490 if (aint >= 128) /* fake up signed chars */
493 sv_setiv(sv, (IV)aint);
494 PUSHs(sv_2mortal(sv));
499 unpack_C: /* unpack U will jump here if not UTF-8 */
504 if (len > strend - s)
519 sv_setiv(sv, (IV)auint);
520 PUSHs(sv_2mortal(sv));
531 if (len > strend - s)
534 while (len-- > 0 && s < strend) {
536 auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, 0));
539 if (checksum > bits_in_uv)
540 cdouble += (NV)auint;
548 while (len-- > 0 && s < strend) {
550 auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, 0));
554 sv_setuv(sv, (UV)auint);
555 PUSHs(sv_2mortal(sv));
560 #if SHORTSIZE == SIZE16
561 along = (strend - s) / SIZE16;
563 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
568 #if SHORTSIZE != SIZE16
572 COPYNN(s, &ashort, sizeof(short));
574 if (checksum > bits_in_uv)
575 cdouble += (NV)ashort;
586 #if SHORTSIZE > SIZE16
591 if (checksum > bits_in_uv)
592 cdouble += (NV)ashort;
601 #if SHORTSIZE != SIZE16
605 COPYNN(s, &ashort, sizeof(short));
608 sv_setiv(sv, (IV)ashort);
609 PUSHs(sv_2mortal(sv));
617 #if SHORTSIZE > SIZE16
623 sv_setiv(sv, (IV)ashort);
624 PUSHs(sv_2mortal(sv));
632 #if SHORTSIZE == SIZE16
633 along = (strend - s) / SIZE16;
635 unatint = natint && datumtype == 'S';
636 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
641 #if SHORTSIZE != SIZE16
643 unsigned short aushort;
645 COPYNN(s, &aushort, sizeof(unsigned short));
646 s += sizeof(unsigned short);
647 if (checksum > bits_in_uv)
648 cdouble += (NV)aushort;
660 if (datumtype == 'n')
661 aushort = PerlSock_ntohs(aushort);
664 if (datumtype == 'v')
665 aushort = vtohs(aushort);
667 if (checksum > bits_in_uv)
668 cdouble += (NV)aushort;
677 #if SHORTSIZE != SIZE16
679 unsigned short aushort;
681 COPYNN(s, &aushort, sizeof(unsigned short));
682 s += sizeof(unsigned short);
684 sv_setiv(sv, (UV)aushort);
685 PUSHs(sv_2mortal(sv));
696 if (datumtype == 'n')
697 aushort = PerlSock_ntohs(aushort);
700 if (datumtype == 'v')
701 aushort = vtohs(aushort);
703 sv_setiv(sv, (UV)aushort);
704 PUSHs(sv_2mortal(sv));
710 along = (strend - s) / sizeof(int);
715 Copy(s, &aint, 1, int);
717 if (checksum > bits_in_uv)
727 Copy(s, &aint, 1, int);
731 /* Without the dummy below unpack("i", pack("i",-1))
732 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
733 * cc with optimization turned on.
735 * The bug was detected in
736 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
737 * with optimization (-O4) turned on.
738 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
739 * does not have this problem even with -O4.
741 * This bug was reported as DECC_BUGS 1431
742 * and tracked internally as GEM_BUGS 7775.
744 * The bug is fixed in
745 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
746 * UNIX V4.0F support: DEC C V5.9-006 or later
747 * UNIX V4.0E support: DEC C V5.8-011 or later
750 * See also few lines later for the same bug.
753 sv_setiv(sv, (IV)aint) :
755 sv_setiv(sv, (IV)aint);
756 PUSHs(sv_2mortal(sv));
761 along = (strend - s) / sizeof(unsigned int);
766 Copy(s, &auint, 1, unsigned int);
767 s += sizeof(unsigned int);
768 if (checksum > bits_in_uv)
769 cdouble += (NV)auint;
778 Copy(s, &auint, 1, unsigned int);
779 s += sizeof(unsigned int);
782 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
783 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
784 * See details few lines earlier. */
786 sv_setuv(sv, (UV)auint) :
788 sv_setuv(sv, (UV)auint);
789 PUSHs(sv_2mortal(sv));
794 #if LONGSIZE == SIZE32
795 along = (strend - s) / SIZE32;
797 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
802 #if LONGSIZE != SIZE32
805 COPYNN(s, &along, sizeof(long));
807 if (checksum > bits_in_uv)
808 cdouble += (NV)along;
817 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
821 #if LONGSIZE > SIZE32
822 if (along > 2147483647)
826 if (checksum > bits_in_uv)
827 cdouble += (NV)along;
836 #if LONGSIZE != SIZE32
839 COPYNN(s, &along, sizeof(long));
842 sv_setiv(sv, (IV)along);
843 PUSHs(sv_2mortal(sv));
850 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
854 #if LONGSIZE > SIZE32
855 if (along > 2147483647)
860 sv_setiv(sv, (IV)along);
861 PUSHs(sv_2mortal(sv));
869 #if LONGSIZE == SIZE32
870 along = (strend - s) / SIZE32;
872 unatint = natint && datumtype == 'L';
873 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
878 #if LONGSIZE != SIZE32
880 unsigned long aulong;
882 COPYNN(s, &aulong, sizeof(unsigned long));
883 s += sizeof(unsigned long);
884 if (checksum > bits_in_uv)
885 cdouble += (NV)aulong;
897 if (datumtype == 'N')
898 aulong = PerlSock_ntohl(aulong);
901 if (datumtype == 'V')
902 aulong = vtohl(aulong);
904 if (checksum > bits_in_uv)
905 cdouble += (NV)aulong;
914 #if LONGSIZE != SIZE32
916 unsigned long aulong;
918 COPYNN(s, &aulong, sizeof(unsigned long));
919 s += sizeof(unsigned long);
921 sv_setuv(sv, (UV)aulong);
922 PUSHs(sv_2mortal(sv));
932 if (datumtype == 'N')
933 aulong = PerlSock_ntohl(aulong);
936 if (datumtype == 'V')
937 aulong = vtohl(aulong);
940 sv_setuv(sv, (UV)aulong);
941 PUSHs(sv_2mortal(sv));
947 along = (strend - s) / sizeof(char*);
953 if (sizeof(char*) > strend - s)
956 Copy(s, &aptr, 1, char*);
962 PUSHs(sv_2mortal(sv));
972 while ((len > 0) && (s < strend)) {
973 auv = (auv << 7) | (*s & 0x7f);
974 /* UTF8_IS_XXXXX not right here - using constant 0x80 */
975 if ((U8)(*s++) < 0x80) {
979 PUSHs(sv_2mortal(sv));
983 else if (++bytes >= sizeof(UV)) { /* promote to string */
987 sv = Perl_newSVpvf(aTHX_ "%.*"UVf, (int)TYPE_DIGITS(UV), auv);
989 sv = mul128(sv, *s & 0x7f);
990 if (!(*s++ & 0x80)) {
999 PUSHs(sv_2mortal(sv));
1004 if ((s >= strend) && bytes)
1005 croak("Unterminated compressed integer");
1010 croak("P must have an explicit size");
1012 if (sizeof(char*) > strend - s)
1015 Copy(s, &aptr, 1, char*);
1020 sv_setpvn(sv, aptr, len);
1021 PUSHs(sv_2mortal(sv));
1025 along = (strend - s) / sizeof(Quad_t);
1030 Copy(s, &aquad, 1, Quad_t);
1031 s += sizeof(Quad_t);
1032 if (checksum > bits_in_uv)
1033 cdouble += (NV)aquad;
1042 if (s + sizeof(Quad_t) > strend)
1045 Copy(s, &aquad, 1, Quad_t);
1046 s += sizeof(Quad_t);
1049 if (aquad >= IV_MIN && aquad <= IV_MAX)
1050 sv_setiv(sv, (IV)aquad);
1052 sv_setnv(sv, (NV)aquad);
1053 PUSHs(sv_2mortal(sv));
1058 along = (strend - s) / sizeof(Quad_t);
1063 Copy(s, &auquad, 1, Uquad_t);
1064 s += sizeof(Uquad_t);
1065 if (checksum > bits_in_uv)
1066 cdouble += (NV)auquad;
1075 if (s + sizeof(Uquad_t) > strend)
1078 Copy(s, &auquad, 1, Uquad_t);
1079 s += sizeof(Uquad_t);
1082 if (auquad <= UV_MAX)
1083 sv_setuv(sv, (UV)auquad);
1085 sv_setnv(sv, (NV)auquad);
1086 PUSHs(sv_2mortal(sv));
1091 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
1094 along = (strend - s) / sizeof(float);
1099 Copy(s, &afloat, 1, float);
1108 Copy(s, &afloat, 1, float);
1111 sv_setnv(sv, (NV)afloat);
1112 PUSHs(sv_2mortal(sv));
1118 along = (strend - s) / sizeof(double);
1123 Copy(s, &adouble, 1, double);
1124 s += sizeof(double);
1132 Copy(s, &adouble, 1, double);
1133 s += sizeof(double);
1135 sv_setnv(sv, (NV)adouble);
1136 PUSHs(sv_2mortal(sv));
1142 * Initialise the decode mapping. By using a table driven
1143 * algorithm, the code will be character-set independent
1144 * (and just as fast as doing character arithmetic)
1146 if (PL_uudmap['M'] == 0) {
1149 for (i = 0; i < sizeof(PL_uuemap); i += 1)
1150 PL_uudmap[(U8)PL_uuemap[i]] = i;
1152 * Because ' ' and '`' map to the same value,
1153 * we need to decode them both the same.
1158 along = (strend - s) * 3 / 4;
1159 sv = NEWSV(42, along);
1162 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
1167 len = PL_uudmap[*(U8*)s++] & 077;
1169 if (s < strend && ISUUCHAR(*s))
1170 a = PL_uudmap[*(U8*)s++] & 077;
1173 if (s < strend && ISUUCHAR(*s))
1174 b = PL_uudmap[*(U8*)s++] & 077;
1177 if (s < strend && ISUUCHAR(*s))
1178 c = PL_uudmap[*(U8*)s++] & 077;
1181 if (s < strend && ISUUCHAR(*s))
1182 d = PL_uudmap[*(U8*)s++] & 077;
1185 hunk[0] = (a << 2) | (b >> 4);
1186 hunk[1] = (b << 4) | (c >> 2);
1187 hunk[2] = (c << 6) | d;
1188 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
1193 else if (s[1] == '\n') /* possible checksum byte */
1196 XPUSHs(sv_2mortal(sv));
1201 if (strchr("fFdD", datumtype) ||
1202 (checksum > bits_in_uv && strchr("csSiIlLnNUvVqQ", datumtype)) ) {
1205 adouble = (NV) (1 << (checksum & 15));
1206 while (checksum >= 16) {
1210 while (cdouble < 0.0)
1212 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
1213 sv_setnv(sv, cdouble);
1216 if (checksum < bits_in_uv) {
1217 UV mask = ((UV)1 << checksum) - 1;
1220 sv_setuv(sv, (UV)culong);
1222 XPUSHs(sv_2mortal(sv));
1225 if ((flags & UNPACK_ONLY_ONE)
1226 && SP - PL_stack_base == start_sp_offset + 1) {
1227 /* do first one only unless in list context
1228 / is implmented by unpacking the count, then poping it from the
1229 stack, so must check that we're not in the middle of a / */
1230 if ((pat >= patend) || *pat != '/')
1237 return SP - PL_stack_base - start_sp_offset;
1244 I32 gimme = GIMME_V;
1247 register char *pat = SvPV(left, llen);
1248 #ifdef PACKED_IS_OCTETS
1249 /* Packed side is assumed to be octets - so force downgrade if it
1250 has been UTF-8 encoded by accident
1252 register char *s = SvPVbyte(right, rlen);
1254 register char *s = SvPV(right, rlen);
1256 char *strend = s + rlen;
1257 register char *patend = pat + llen;
1261 cnt = unpack_str(pat, patend, s, s, strend, NULL, 0,
1262 ((gimme == G_SCALAR) ? UNPACK_ONLY_ONE : 0)
1263 | (DO_UTF8(right) ? UNPACK_DO_UTF8 : 0));
1265 if ( !cnt && gimme == G_SCALAR )
1266 PUSHs(&PL_sv_undef);
1271 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
1275 *hunk = PL_uuemap[len];
1276 sv_catpvn(sv, hunk, 1);
1279 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1280 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
1281 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
1282 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
1283 sv_catpvn(sv, hunk, 4);
1288 char r = (len > 1 ? s[1] : '\0');
1289 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1290 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
1291 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
1292 hunk[3] = PL_uuemap[0];
1293 sv_catpvn(sv, hunk, 4);
1295 sv_catpvn(sv, "\n", 1);
1299 S_is_an_int(pTHX_ char *s, STRLEN l)
1302 SV *result = newSVpvn(s, l);
1303 char *result_c = SvPV(result, n_a); /* convenience */
1304 char *out = result_c;
1314 SvREFCNT_dec(result);
1337 SvREFCNT_dec(result);
1343 SvCUR_set(result, out - result_c);
1347 /* pnum must be '\0' terminated */
1349 S_div128(pTHX_ SV *pnum, bool *done)
1352 char *s = SvPV(pnum, len);
1361 i = m * 10 + (*t - '0');
1363 r = (i >> 7); /* r < 10 */
1370 SvCUR_set(pnum, (STRLEN) (t - s));
1374 #define PACK_CHILD 0x1
1377 =for apidoc pack_cat
1379 The engine implementing pack() Perl function.
1384 Perl_pack_cat(pTHX_ SV *cat, char *pat, register char *patend, register SV **beglist, SV **endlist, SV ***next_in_list, U32 flags)
1392 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
1393 static char *space10 = " ";
1396 /* These must not be in registers: */
1411 #ifdef PERL_NATINT_PACK
1412 int natint; /* native integer */
1415 items = endlist - beglist;
1416 #ifndef PACKED_IS_OCTETS
1417 pat = next_symbol(pat, patend);
1418 if (pat < patend && *pat == 'U' && !flags)
1421 while ((pat = next_symbol(pat, patend)) < patend) {
1422 SV *lengthcode = Nullsv;
1423 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *beglist++ : &PL_sv_no)
1424 datumtype = *pat++ & 0xFF;
1425 #ifdef PERL_NATINT_PACK
1429 char *natstr = "sSiIlL";
1431 if (strchr(natstr, datumtype)) {
1432 #ifdef PERL_NATINT_PACK
1438 croak("'!' allowed only after types %s", natstr);
1440 len = find_count(&pat, patend, &star);
1441 if (star > 0) /* Count is '*' */
1442 len = strchr("@Xxu", datumtype) ? 0 : items;
1443 else if (star < 0) /* Default len */
1445 if (*pat == '/') { /* doing lookahead how... */
1447 if ((*pat != 'a' && *pat != 'A' && *pat != 'Z') || pat[1] != '*')
1448 croak("/ must be followed by a*, A* or Z*");
1449 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
1450 ? *beglist : &PL_sv_no)
1451 + (*pat == 'Z' ? 1 : 0)));
1455 croak("Invalid type in pack: '%c'", (int)datumtype);
1456 case ',': /* grandfather in commas but with a warning */
1457 if (commas++ == 0 && ckWARN(WARN_PACK))
1458 Perl_warner(aTHX_ WARN_PACK,
1459 "Invalid type in pack: '%c'", (int)datumtype);
1462 croak("%% may only be used in unpack");
1474 SV **savebeglist = beglist; /* beglist de-register-ed */
1477 croak("()-group starts with a count");
1478 aptr = group_end(beg, patend, ')');
1481 len = find_count(&pat, patend, &star);
1482 if (star < 0) /* No count */
1484 else if (star > 0) /* Star */
1485 len = items; /* long enough? */
1488 pack_cat(cat, beg, aptr, savebeglist, endlist,
1489 &savebeglist, PACK_CHILD);
1490 if (star > 0 && savebeglist == endlist)
1491 break; /* No way to continue */
1493 beglist = savebeglist;
1498 if (SvCUR(cat) < len)
1499 croak("X outside of string");
1506 sv_catpvn(cat, null10, 10);
1509 sv_catpvn(cat, null10, len);
1515 aptr = SvPV(fromstr, fromlen);
1516 if (star > 0) { /* -2 after '/' */
1518 if (datumtype == 'Z')
1521 if (fromlen >= len) {
1522 sv_catpvn(cat, aptr, len);
1523 if (datumtype == 'Z')
1524 *(SvEND(cat)-1) = '\0';
1527 sv_catpvn(cat, aptr, fromlen);
1529 if (datumtype == 'A') {
1531 sv_catpvn(cat, space10, 10);
1534 sv_catpvn(cat, space10, len);
1538 sv_catpvn(cat, null10, 10);
1541 sv_catpvn(cat, null10, len);
1553 str = SvPV(fromstr, fromlen);
1557 SvCUR(cat) += (len+7)/8;
1558 SvGROW(cat, SvCUR(cat) + 1);
1559 aptr = SvPVX(cat) + aint;
1564 if (datumtype == 'B') {
1565 for (len = 0; len++ < aint;) {
1566 items |= *str++ & 1;
1570 *aptr++ = items & 0xff;
1576 for (len = 0; len++ < aint;) {
1582 *aptr++ = items & 0xff;
1588 if (datumtype == 'B')
1589 items <<= 7 - (aint & 7);
1591 items >>= 7 - (aint & 7);
1592 *aptr++ = items & 0xff;
1594 str = SvPVX(cat) + SvCUR(cat);
1609 str = SvPV(fromstr, fromlen);
1613 SvCUR(cat) += (len+1)/2;
1614 SvGROW(cat, SvCUR(cat) + 1);
1615 aptr = SvPVX(cat) + aint;
1620 if (datumtype == 'H') {
1621 for (len = 0; len++ < aint;) {
1623 items |= ((*str++ & 15) + 9) & 15;
1625 items |= *str++ & 15;
1629 *aptr++ = items & 0xff;
1635 for (len = 0; len++ < aint;) {
1637 items |= (((*str++ & 15) + 9) & 15) << 4;
1639 items |= (*str++ & 15) << 4;
1643 *aptr++ = items & 0xff;
1649 *aptr++ = items & 0xff;
1650 str = SvPVX(cat) + SvCUR(cat);
1661 switch (datumtype) {
1663 aint = SvIV(fromstr);
1664 if ((aint < 0 || aint > 255) &&
1666 Perl_warner(aTHX_ WARN_PACK,
1667 "Character in \"C\" format wrapped");
1669 sv_catpvn(cat, &achar, sizeof(char));
1672 aint = SvIV(fromstr);
1673 if ((aint < -128 || aint > 127) &&
1675 Perl_warner(aTHX_ WARN_PACK,
1676 "Character in \"c\" format wrapped");
1678 sv_catpvn(cat, &achar, sizeof(char));
1686 auint = UNI_TO_NATIVE(SvUV(fromstr));
1687 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN + 1);
1688 SvCUR_set(cat, (char*)uvchr_to_utf8((U8*)SvEND(cat),auint)
1693 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
1698 afloat = (float)SvNV(fromstr);
1699 sv_catpvn(cat, (char *)&afloat, sizeof (float));
1706 adouble = (double)SvNV(fromstr);
1707 sv_catpvn(cat, (char *)&adouble, sizeof (double));
1713 ashort = (I16)SvIV(fromstr);
1715 ashort = PerlSock_htons(ashort);
1717 CAT16(cat, &ashort);
1723 ashort = (I16)SvIV(fromstr);
1725 ashort = htovs(ashort);
1727 CAT16(cat, &ashort);
1731 #if SHORTSIZE != SIZE16
1733 unsigned short aushort;
1737 aushort = SvUV(fromstr);
1738 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
1748 aushort = (U16)SvUV(fromstr);
1749 CAT16(cat, &aushort);
1755 #if SHORTSIZE != SIZE16
1761 ashort = SvIV(fromstr);
1762 sv_catpvn(cat, (char *)&ashort, sizeof(short));
1770 ashort = (I16)SvIV(fromstr);
1771 CAT16(cat, &ashort);
1778 auint = SvUV(fromstr);
1779 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
1785 adouble = Perl_floor(SvNV(fromstr));
1788 croak("Cannot compress negative numbers");
1791 #if UVSIZE > 4 && UVSIZE >= NVSIZE
1792 adouble <= 0xffffffff
1794 # ifdef CXUX_BROKEN_CONSTANT_CONVERT
1795 adouble <= UV_MAX_cxux
1802 char buf[1 + sizeof(UV)];
1803 char *in = buf + sizeof(buf);
1804 UV auv = U_V(adouble);
1807 *--in = (auv & 0x7f) | 0x80;
1810 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1811 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1813 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
1814 char *from, *result, *in;
1819 /* Copy string and check for compliance */
1820 from = SvPV(fromstr, len);
1821 if ((norm = is_an_int(from, len)) == NULL)
1822 croak("can compress only unsigned integer");
1824 New('w', result, len, char);
1828 *--in = div128(norm, &done) | 0x80;
1829 result[len - 1] &= 0x7F; /* clear continue bit */
1830 sv_catpvn(cat, in, (result + len) - in);
1832 SvREFCNT_dec(norm); /* free norm */
1834 else if (SvNOKp(fromstr)) {
1835 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
1836 char *in = buf + sizeof(buf);
1839 double next = floor(adouble / 128);
1840 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
1841 if (in <= buf) /* this cannot happen ;-) */
1842 croak("Cannot compress integer");
1844 } while (adouble > 0);
1845 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1846 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1849 char *from, *result, *in;
1854 /* Copy string and check for compliance */
1855 from = SvPV(fromstr, len);
1856 if ((norm = is_an_int(from, len)) == NULL)
1857 croak("can compress only unsigned integer");
1859 New('w', result, len, char);
1863 *--in = div128(norm, &done) | 0x80;
1864 result[len - 1] &= 0x7F; /* clear continue bit */
1865 sv_catpvn(cat, in, (result + len) - in);
1867 SvREFCNT_dec(norm); /* free norm */
1874 aint = SvIV(fromstr);
1875 sv_catpvn(cat, (char*)&aint, sizeof(int));
1881 aulong = SvUV(fromstr);
1883 aulong = PerlSock_htonl(aulong);
1885 CAT32(cat, &aulong);
1891 aulong = SvUV(fromstr);
1893 aulong = htovl(aulong);
1895 CAT32(cat, &aulong);
1899 #if LONGSIZE != SIZE32
1901 unsigned long aulong;
1905 aulong = SvUV(fromstr);
1906 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
1914 aulong = SvUV(fromstr);
1915 CAT32(cat, &aulong);
1920 #if LONGSIZE != SIZE32
1926 along = SvIV(fromstr);
1927 sv_catpvn(cat, (char *)&along, sizeof(long));
1935 along = SvIV(fromstr);
1944 auquad = (Uquad_t)SvUV(fromstr);
1945 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
1951 aquad = (Quad_t)SvIV(fromstr);
1952 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
1957 len = 1; /* assume SV is correct length */
1962 if (fromstr == &PL_sv_undef)
1966 /* XXX better yet, could spirit away the string to
1967 * a safe spot and hang on to it until the result
1968 * of pack() (and all copies of the result) are
1971 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
1972 || (SvPADTMP(fromstr)
1973 && !SvREADONLY(fromstr))))
1975 Perl_warner(aTHX_ WARN_PACK,
1976 "Attempt to pack pointer to temporary value");
1978 if (SvPOK(fromstr) || SvNIOK(fromstr))
1979 aptr = SvPV(fromstr,n_a);
1981 aptr = SvPV_force(fromstr,n_a);
1983 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
1988 aptr = SvPV(fromstr, fromlen);
1989 SvGROW(cat, fromlen * 4 / 3);
1994 while (fromlen > 0) {
2001 doencodes(cat, aptr, todo);
2009 *next_in_list = beglist;
2016 dSP; dMARK; dORIGMARK; dTARGET;
2017 register SV *cat = TARG;
2019 register char *pat = SvPVx(*++MARK, fromlen);
2020 register char *patend = pat + fromlen;
2023 sv_setpvn(cat, "", 0);
2025 pack_cat(cat, pat, patend, MARK, SP + 1, NULL, 0);