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) == ' ')
131 I32 start_sp_offset = SP - PL_stack_base;
136 register char *pat = SvPV(left, llen);
137 #ifdef PACKED_IS_OCTETS
138 /* Packed side is assumed to be octets - so force downgrade if it
139 has been UTF-8 encoded by accident
141 register char *s = SvPVbyte(right, rlen);
143 register char *s = SvPV(right, rlen);
145 char *strend = s + rlen;
147 register char *patend = pat + llen;
150 register I32 bits = 0;
153 /* These must not be in registers: */
172 const int bits_in_uv = 8 * sizeof(culong);
175 #ifdef PERL_NATINT_PACK
176 int natint; /* native integer */
177 int unatint; /* unsigned native integer */
179 bool do_utf8 = DO_UTF8(right);
181 while (pat < patend) {
183 datumtype = *pat++ & 0xFF;
184 #ifdef PERL_NATINT_PACK
187 if (isSPACE(datumtype))
189 if (datumtype == '#') {
190 while (pat < patend && *pat != '\n')
195 char *natstr = "sSiIlL";
197 if (strchr(natstr, datumtype)) {
198 #ifdef PERL_NATINT_PACK
204 DIE(aTHX_ "'!' allowed only after types %s", natstr);
209 else if (*pat == '*') {
210 len = strend - strbeg; /* long enough */
214 else if (isDIGIT(*pat)) {
216 while (isDIGIT(*pat)) {
217 len = (len * 10) + (*pat++ - '0');
219 DIE(aTHX_ "Repeat count in unpack overflows");
223 len = (datumtype != '@');
227 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
228 case ',': /* grandfather in commas but with a warning */
229 if (commas++ == 0 && ckWARN(WARN_UNPACK))
230 Perl_warner(aTHX_ WARN_UNPACK,
231 "Invalid type in unpack: '%c'", (int)datumtype);
234 if (len == 1 && pat[-1] != '1')
243 if (len > strend - strbeg)
244 DIE(aTHX_ "@ outside of string");
248 if (len > s - strbeg)
249 DIE(aTHX_ "X outside of string");
253 if (len > strend - s)
254 DIE(aTHX_ "x outside of string");
258 if (start_sp_offset >= SP - PL_stack_base)
259 DIE(aTHX_ "/ must follow a numeric type");
262 pat++; /* ignore '*' for compatibility with pack */
264 DIE(aTHX_ "/ cannot take a count" );
271 if (len > strend - s)
276 sv_setpvn(sv, s, len);
277 if (datumtype == 'A' || datumtype == 'Z') {
278 aptr = s; /* borrow register */
279 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
283 if (star) /* exact for 'Z*' */
284 len = s - SvPVX(sv) + 1;
286 else { /* 'A' strips both nulls and spaces */
287 s = SvPVX(sv) + len - 1;
288 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
292 SvCUR_set(sv, s - SvPVX(sv));
293 s = aptr; /* unborrow register */
296 XPUSHs(sv_2mortal(sv));
300 if (star || len > (strend - s) * 8)
301 len = (strend - s) * 8;
304 Newz(601, PL_bitcount, 256, char);
305 for (bits = 1; bits < 256; bits++) {
306 if (bits & 1) PL_bitcount[bits]++;
307 if (bits & 2) PL_bitcount[bits]++;
308 if (bits & 4) PL_bitcount[bits]++;
309 if (bits & 8) PL_bitcount[bits]++;
310 if (bits & 16) PL_bitcount[bits]++;
311 if (bits & 32) PL_bitcount[bits]++;
312 if (bits & 64) PL_bitcount[bits]++;
313 if (bits & 128) PL_bitcount[bits]++;
317 culong += PL_bitcount[*(unsigned char*)s++];
322 if (datumtype == 'b') {
324 if (bits & 1) culong++;
330 if (bits & 128) culong++;
337 sv = NEWSV(35, len + 1);
341 if (datumtype == 'b') {
343 for (len = 0; len < aint; len++) {
344 if (len & 7) /*SUPPRESS 595*/
348 *str++ = '0' + (bits & 1);
353 for (len = 0; len < aint; len++) {
358 *str++ = '0' + ((bits & 128) != 0);
362 XPUSHs(sv_2mortal(sv));
366 if (star || len > (strend - s) * 2)
367 len = (strend - s) * 2;
368 sv = NEWSV(35, len + 1);
372 if (datumtype == 'h') {
374 for (len = 0; len < aint; len++) {
379 *str++ = PL_hexdigit[bits & 15];
384 for (len = 0; len < aint; len++) {
389 *str++ = PL_hexdigit[(bits >> 4) & 15];
393 XPUSHs(sv_2mortal(sv));
396 if (len > strend - s)
401 if (aint >= 128) /* fake up signed chars */
403 if (checksum > bits_in_uv)
414 if (aint >= 128) /* fake up signed chars */
417 sv_setiv(sv, (IV)aint);
418 PUSHs(sv_2mortal(sv));
423 unpack_C: /* unpack U will jump here if not UTF-8 */
428 if (len > strend - s)
443 sv_setiv(sv, (IV)auint);
444 PUSHs(sv_2mortal(sv));
455 if (len > strend - s)
458 while (len-- > 0 && s < strend) {
460 auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, 0));
463 if (checksum > bits_in_uv)
464 cdouble += (NV)auint;
472 while (len-- > 0 && s < strend) {
474 auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, 0));
478 sv_setuv(sv, (UV)auint);
479 PUSHs(sv_2mortal(sv));
484 #if SHORTSIZE == SIZE16
485 along = (strend - s) / SIZE16;
487 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
492 #if SHORTSIZE != SIZE16
496 COPYNN(s, &ashort, sizeof(short));
498 if (checksum > bits_in_uv)
499 cdouble += (NV)ashort;
510 #if SHORTSIZE > SIZE16
515 if (checksum > bits_in_uv)
516 cdouble += (NV)ashort;
525 #if SHORTSIZE != SIZE16
529 COPYNN(s, &ashort, sizeof(short));
532 sv_setiv(sv, (IV)ashort);
533 PUSHs(sv_2mortal(sv));
541 #if SHORTSIZE > SIZE16
547 sv_setiv(sv, (IV)ashort);
548 PUSHs(sv_2mortal(sv));
556 #if SHORTSIZE == SIZE16
557 along = (strend - s) / SIZE16;
559 unatint = natint && datumtype == 'S';
560 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
565 #if SHORTSIZE != SIZE16
567 unsigned short aushort;
569 COPYNN(s, &aushort, sizeof(unsigned short));
570 s += sizeof(unsigned short);
571 if (checksum > bits_in_uv)
572 cdouble += (NV)aushort;
584 if (datumtype == 'n')
585 aushort = PerlSock_ntohs(aushort);
588 if (datumtype == 'v')
589 aushort = vtohs(aushort);
591 if (checksum > bits_in_uv)
592 cdouble += (NV)aushort;
601 #if SHORTSIZE != SIZE16
603 unsigned short aushort;
605 COPYNN(s, &aushort, sizeof(unsigned short));
606 s += sizeof(unsigned short);
608 sv_setiv(sv, (UV)aushort);
609 PUSHs(sv_2mortal(sv));
620 if (datumtype == 'n')
621 aushort = PerlSock_ntohs(aushort);
624 if (datumtype == 'v')
625 aushort = vtohs(aushort);
627 sv_setiv(sv, (UV)aushort);
628 PUSHs(sv_2mortal(sv));
634 along = (strend - s) / sizeof(int);
639 Copy(s, &aint, 1, int);
641 if (checksum > bits_in_uv)
651 Copy(s, &aint, 1, int);
655 /* Without the dummy below unpack("i", pack("i",-1))
656 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
657 * cc with optimization turned on.
659 * The bug was detected in
660 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
661 * with optimization (-O4) turned on.
662 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
663 * does not have this problem even with -O4.
665 * This bug was reported as DECC_BUGS 1431
666 * and tracked internally as GEM_BUGS 7775.
668 * The bug is fixed in
669 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
670 * UNIX V4.0F support: DEC C V5.9-006 or later
671 * UNIX V4.0E support: DEC C V5.8-011 or later
674 * See also few lines later for the same bug.
677 sv_setiv(sv, (IV)aint) :
679 sv_setiv(sv, (IV)aint);
680 PUSHs(sv_2mortal(sv));
685 along = (strend - s) / sizeof(unsigned int);
690 Copy(s, &auint, 1, unsigned int);
691 s += sizeof(unsigned int);
692 if (checksum > bits_in_uv)
693 cdouble += (NV)auint;
702 Copy(s, &auint, 1, unsigned int);
703 s += sizeof(unsigned int);
706 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
707 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
708 * See details few lines earlier. */
710 sv_setuv(sv, (UV)auint) :
712 sv_setuv(sv, (UV)auint);
713 PUSHs(sv_2mortal(sv));
718 #if LONGSIZE == SIZE32
719 along = (strend - s) / SIZE32;
721 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
726 #if LONGSIZE != SIZE32
729 COPYNN(s, &along, sizeof(long));
731 if (checksum > bits_in_uv)
732 cdouble += (NV)along;
741 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
745 #if LONGSIZE > SIZE32
746 if (along > 2147483647)
750 if (checksum > bits_in_uv)
751 cdouble += (NV)along;
760 #if LONGSIZE != SIZE32
763 COPYNN(s, &along, sizeof(long));
766 sv_setiv(sv, (IV)along);
767 PUSHs(sv_2mortal(sv));
774 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
778 #if LONGSIZE > SIZE32
779 if (along > 2147483647)
784 sv_setiv(sv, (IV)along);
785 PUSHs(sv_2mortal(sv));
793 #if LONGSIZE == SIZE32
794 along = (strend - s) / SIZE32;
796 unatint = natint && datumtype == 'L';
797 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
802 #if LONGSIZE != SIZE32
804 unsigned long aulong;
806 COPYNN(s, &aulong, sizeof(unsigned long));
807 s += sizeof(unsigned long);
808 if (checksum > bits_in_uv)
809 cdouble += (NV)aulong;
821 if (datumtype == 'N')
822 aulong = PerlSock_ntohl(aulong);
825 if (datumtype == 'V')
826 aulong = vtohl(aulong);
828 if (checksum > bits_in_uv)
829 cdouble += (NV)aulong;
838 #if LONGSIZE != SIZE32
840 unsigned long aulong;
842 COPYNN(s, &aulong, sizeof(unsigned long));
843 s += sizeof(unsigned long);
845 sv_setuv(sv, (UV)aulong);
846 PUSHs(sv_2mortal(sv));
856 if (datumtype == 'N')
857 aulong = PerlSock_ntohl(aulong);
860 if (datumtype == 'V')
861 aulong = vtohl(aulong);
864 sv_setuv(sv, (UV)aulong);
865 PUSHs(sv_2mortal(sv));
871 along = (strend - s) / sizeof(char*);
877 if (sizeof(char*) > strend - s)
880 Copy(s, &aptr, 1, char*);
886 PUSHs(sv_2mortal(sv));
896 while ((len > 0) && (s < strend)) {
897 auv = (auv << 7) | (*s & 0x7f);
898 /* UTF8_IS_XXXXX not right here - using constant 0x80 */
899 if ((U8)(*s++) < 0x80) {
903 PUSHs(sv_2mortal(sv));
907 else if (++bytes >= sizeof(UV)) { /* promote to string */
911 sv = Perl_newSVpvf(aTHX_ "%.*"UVf, (int)TYPE_DIGITS(UV), auv);
913 sv = mul128(sv, *s & 0x7f);
914 if (!(*s++ & 0x80)) {
923 PUSHs(sv_2mortal(sv));
928 if ((s >= strend) && bytes)
929 DIE(aTHX_ "Unterminated compressed integer");
934 DIE(aTHX_ "P must have an explicit size");
936 if (sizeof(char*) > strend - s)
939 Copy(s, &aptr, 1, char*);
944 sv_setpvn(sv, aptr, len);
945 PUSHs(sv_2mortal(sv));
949 along = (strend - s) / sizeof(Quad_t);
954 Copy(s, &aquad, 1, Quad_t);
956 if (checksum > bits_in_uv)
957 cdouble += (NV)aquad;
966 if (s + sizeof(Quad_t) > strend)
969 Copy(s, &aquad, 1, Quad_t);
973 if (aquad >= IV_MIN && aquad <= IV_MAX)
974 sv_setiv(sv, (IV)aquad);
976 sv_setnv(sv, (NV)aquad);
977 PUSHs(sv_2mortal(sv));
982 along = (strend - s) / sizeof(Quad_t);
987 Copy(s, &auquad, 1, Uquad_t);
988 s += sizeof(Uquad_t);
989 if (checksum > bits_in_uv)
990 cdouble += (NV)auquad;
999 if (s + sizeof(Uquad_t) > strend)
1002 Copy(s, &auquad, 1, Uquad_t);
1003 s += sizeof(Uquad_t);
1006 if (auquad <= UV_MAX)
1007 sv_setuv(sv, (UV)auquad);
1009 sv_setnv(sv, (NV)auquad);
1010 PUSHs(sv_2mortal(sv));
1015 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
1018 along = (strend - s) / sizeof(float);
1023 Copy(s, &afloat, 1, float);
1032 Copy(s, &afloat, 1, float);
1035 sv_setnv(sv, (NV)afloat);
1036 PUSHs(sv_2mortal(sv));
1042 along = (strend - s) / sizeof(double);
1047 Copy(s, &adouble, 1, double);
1048 s += sizeof(double);
1056 Copy(s, &adouble, 1, double);
1057 s += sizeof(double);
1059 sv_setnv(sv, (NV)adouble);
1060 PUSHs(sv_2mortal(sv));
1066 * Initialise the decode mapping. By using a table driven
1067 * algorithm, the code will be character-set independent
1068 * (and just as fast as doing character arithmetic)
1070 if (PL_uudmap['M'] == 0) {
1073 for (i = 0; i < sizeof(PL_uuemap); i += 1)
1074 PL_uudmap[(U8)PL_uuemap[i]] = i;
1076 * Because ' ' and '`' map to the same value,
1077 * we need to decode them both the same.
1082 along = (strend - s) * 3 / 4;
1083 sv = NEWSV(42, along);
1086 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
1091 len = PL_uudmap[*(U8*)s++] & 077;
1093 if (s < strend && ISUUCHAR(*s))
1094 a = PL_uudmap[*(U8*)s++] & 077;
1097 if (s < strend && ISUUCHAR(*s))
1098 b = PL_uudmap[*(U8*)s++] & 077;
1101 if (s < strend && ISUUCHAR(*s))
1102 c = PL_uudmap[*(U8*)s++] & 077;
1105 if (s < strend && ISUUCHAR(*s))
1106 d = PL_uudmap[*(U8*)s++] & 077;
1109 hunk[0] = (a << 2) | (b >> 4);
1110 hunk[1] = (b << 4) | (c >> 2);
1111 hunk[2] = (c << 6) | d;
1112 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
1117 else if (s[1] == '\n') /* possible checksum byte */
1120 XPUSHs(sv_2mortal(sv));
1125 if (strchr("fFdD", datumtype) ||
1126 (checksum > bits_in_uv && strchr("csSiIlLnNUvVqQ", datumtype)) ) {
1129 adouble = (NV) (1 << (checksum & 15));
1130 while (checksum >= 16) {
1134 while (cdouble < 0.0)
1136 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
1137 sv_setnv(sv, cdouble);
1140 if (checksum < bits_in_uv) {
1141 UV mask = ((UV)1 << checksum) - 1;
1144 sv_setuv(sv, (UV)culong);
1146 XPUSHs(sv_2mortal(sv));
1149 if (gimme != G_ARRAY &&
1150 SP - PL_stack_base == start_sp_offset + 1) {
1151 /* do first one only unless in list context
1152 / is implmented by unpacking the count, then poping it from the
1153 stack, so must check that we're not in the middle of a / */
1154 if ((pat >= patend) || *pat != '/')
1158 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
1159 PUSHs(&PL_sv_undef);
1164 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
1168 *hunk = PL_uuemap[len];
1169 sv_catpvn(sv, hunk, 1);
1172 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1173 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
1174 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
1175 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
1176 sv_catpvn(sv, hunk, 4);
1181 char r = (len > 1 ? s[1] : '\0');
1182 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1183 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
1184 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
1185 hunk[3] = PL_uuemap[0];
1186 sv_catpvn(sv, hunk, 4);
1188 sv_catpvn(sv, "\n", 1);
1192 S_is_an_int(pTHX_ char *s, STRLEN l)
1195 SV *result = newSVpvn(s, l);
1196 char *result_c = SvPV(result, n_a); /* convenience */
1197 char *out = result_c;
1207 SvREFCNT_dec(result);
1230 SvREFCNT_dec(result);
1236 SvCUR_set(result, out - result_c);
1240 /* pnum must be '\0' terminated */
1242 S_div128(pTHX_ SV *pnum, bool *done)
1245 char *s = SvPV(pnum, len);
1254 i = m * 10 + (*t - '0');
1256 r = (i >> 7); /* r < 10 */
1263 SvCUR_set(pnum, (STRLEN) (t - s));
1270 dSP; dMARK; dORIGMARK; dTARGET;
1271 register SV *cat = TARG;
1274 register char *pat = SvPVx(*++MARK, fromlen);
1276 register char *patend = pat + fromlen;
1281 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
1282 static char *space10 = " ";
1284 /* These must not be in registers: */
1299 #ifdef PERL_NATINT_PACK
1300 int natint; /* native integer */
1305 sv_setpvn(cat, "", 0);
1307 while (pat < patend) {
1308 SV *lengthcode = Nullsv;
1309 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
1310 datumtype = *pat++ & 0xFF;
1311 #ifdef PERL_NATINT_PACK
1314 if (isSPACE(datumtype)) {
1318 #ifndef PACKED_IS_OCTETS
1319 if (datumtype == 'U' && pat == patcopy+1)
1322 if (datumtype == '#') {
1323 while (pat < patend && *pat != '\n')
1328 char *natstr = "sSiIlL";
1330 if (strchr(natstr, datumtype)) {
1331 #ifdef PERL_NATINT_PACK
1337 DIE(aTHX_ "'!' allowed only after types %s", natstr);
1340 len = strchr("@Xxu", datumtype) ? 0 : items;
1343 else if (isDIGIT(*pat)) {
1345 while (isDIGIT(*pat)) {
1346 len = (len * 10) + (*pat++ - '0');
1348 DIE(aTHX_ "Repeat count in pack overflows");
1355 if ((*pat != 'a' && *pat != 'A' && *pat != 'Z') || pat[1] != '*')
1356 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
1357 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
1358 ? *MARK : &PL_sv_no)
1359 + (*pat == 'Z' ? 1 : 0)));
1363 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
1364 case ',': /* grandfather in commas but with a warning */
1365 if (commas++ == 0 && ckWARN(WARN_PACK))
1366 Perl_warner(aTHX_ WARN_PACK,
1367 "Invalid type in pack: '%c'", (int)datumtype);
1370 DIE(aTHX_ "%% may only be used in unpack");
1381 if (SvCUR(cat) < len)
1382 DIE(aTHX_ "X outside of string");
1389 sv_catpvn(cat, null10, 10);
1392 sv_catpvn(cat, null10, len);
1398 aptr = SvPV(fromstr, fromlen);
1399 if (pat[lengthcode ? -2 : -1] == '*') { /* -2 after '/' */
1401 if (datumtype == 'Z')
1404 if (fromlen >= len) {
1405 sv_catpvn(cat, aptr, len);
1406 if (datumtype == 'Z')
1407 *(SvEND(cat)-1) = '\0';
1410 sv_catpvn(cat, aptr, fromlen);
1412 if (datumtype == 'A') {
1414 sv_catpvn(cat, space10, 10);
1417 sv_catpvn(cat, space10, len);
1421 sv_catpvn(cat, null10, 10);
1424 sv_catpvn(cat, null10, len);
1436 str = SvPV(fromstr, fromlen);
1440 SvCUR(cat) += (len+7)/8;
1441 SvGROW(cat, SvCUR(cat) + 1);
1442 aptr = SvPVX(cat) + aint;
1447 if (datumtype == 'B') {
1448 for (len = 0; len++ < aint;) {
1449 items |= *str++ & 1;
1453 *aptr++ = items & 0xff;
1459 for (len = 0; len++ < aint;) {
1465 *aptr++ = items & 0xff;
1471 if (datumtype == 'B')
1472 items <<= 7 - (aint & 7);
1474 items >>= 7 - (aint & 7);
1475 *aptr++ = items & 0xff;
1477 str = SvPVX(cat) + SvCUR(cat);
1492 str = SvPV(fromstr, fromlen);
1496 SvCUR(cat) += (len+1)/2;
1497 SvGROW(cat, SvCUR(cat) + 1);
1498 aptr = SvPVX(cat) + aint;
1503 if (datumtype == 'H') {
1504 for (len = 0; len++ < aint;) {
1506 items |= ((*str++ & 15) + 9) & 15;
1508 items |= *str++ & 15;
1512 *aptr++ = items & 0xff;
1518 for (len = 0; len++ < aint;) {
1520 items |= (((*str++ & 15) + 9) & 15) << 4;
1522 items |= (*str++ & 15) << 4;
1526 *aptr++ = items & 0xff;
1532 *aptr++ = items & 0xff;
1533 str = SvPVX(cat) + SvCUR(cat);
1544 switch (datumtype) {
1546 aint = SvIV(fromstr);
1547 if ((aint < 0 || aint > 255) &&
1549 Perl_warner(aTHX_ WARN_PACK,
1550 "Character in \"C\" format wrapped");
1552 sv_catpvn(cat, &achar, sizeof(char));
1555 aint = SvIV(fromstr);
1556 if ((aint < -128 || aint > 127) &&
1558 Perl_warner(aTHX_ WARN_PACK,
1559 "Character in \"c\" format wrapped");
1561 sv_catpvn(cat, &achar, sizeof(char));
1569 auint = UNI_TO_NATIVE(SvUV(fromstr));
1570 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN + 1);
1571 SvCUR_set(cat, (char*)uvchr_to_utf8((U8*)SvEND(cat),auint)
1576 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
1581 afloat = (float)SvNV(fromstr);
1582 sv_catpvn(cat, (char *)&afloat, sizeof (float));
1589 adouble = (double)SvNV(fromstr);
1590 sv_catpvn(cat, (char *)&adouble, sizeof (double));
1596 ashort = (I16)SvIV(fromstr);
1598 ashort = PerlSock_htons(ashort);
1600 CAT16(cat, &ashort);
1606 ashort = (I16)SvIV(fromstr);
1608 ashort = htovs(ashort);
1610 CAT16(cat, &ashort);
1614 #if SHORTSIZE != SIZE16
1616 unsigned short aushort;
1620 aushort = SvUV(fromstr);
1621 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
1631 aushort = (U16)SvUV(fromstr);
1632 CAT16(cat, &aushort);
1638 #if SHORTSIZE != SIZE16
1644 ashort = SvIV(fromstr);
1645 sv_catpvn(cat, (char *)&ashort, sizeof(short));
1653 ashort = (I16)SvIV(fromstr);
1654 CAT16(cat, &ashort);
1661 auint = SvUV(fromstr);
1662 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
1668 adouble = Perl_floor(SvNV(fromstr));
1671 DIE(aTHX_ "Cannot compress negative numbers");
1674 #if UVSIZE > 4 && UVSIZE >= NVSIZE
1675 adouble <= 0xffffffff
1677 # ifdef CXUX_BROKEN_CONSTANT_CONVERT
1678 adouble <= UV_MAX_cxux
1685 char buf[1 + sizeof(UV)];
1686 char *in = buf + sizeof(buf);
1687 UV auv = U_V(adouble);
1690 *--in = (auv & 0x7f) | 0x80;
1693 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1694 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1696 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
1697 char *from, *result, *in;
1702 /* Copy string and check for compliance */
1703 from = SvPV(fromstr, len);
1704 if ((norm = is_an_int(from, len)) == NULL)
1705 DIE(aTHX_ "can compress only unsigned integer");
1707 New('w', result, len, char);
1711 *--in = div128(norm, &done) | 0x80;
1712 result[len - 1] &= 0x7F; /* clear continue bit */
1713 sv_catpvn(cat, in, (result + len) - in);
1715 SvREFCNT_dec(norm); /* free norm */
1717 else if (SvNOKp(fromstr)) {
1718 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
1719 char *in = buf + sizeof(buf);
1722 double next = floor(adouble / 128);
1723 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
1724 if (in <= buf) /* this cannot happen ;-) */
1725 DIE(aTHX_ "Cannot compress integer");
1727 } while (adouble > 0);
1728 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1729 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1732 char *from, *result, *in;
1737 /* Copy string and check for compliance */
1738 from = SvPV(fromstr, len);
1739 if ((norm = is_an_int(from, len)) == NULL)
1740 DIE(aTHX_ "can compress only unsigned integer");
1742 New('w', result, len, char);
1746 *--in = div128(norm, &done) | 0x80;
1747 result[len - 1] &= 0x7F; /* clear continue bit */
1748 sv_catpvn(cat, in, (result + len) - in);
1750 SvREFCNT_dec(norm); /* free norm */
1757 aint = SvIV(fromstr);
1758 sv_catpvn(cat, (char*)&aint, sizeof(int));
1764 aulong = SvUV(fromstr);
1766 aulong = PerlSock_htonl(aulong);
1768 CAT32(cat, &aulong);
1774 aulong = SvUV(fromstr);
1776 aulong = htovl(aulong);
1778 CAT32(cat, &aulong);
1782 #if LONGSIZE != SIZE32
1784 unsigned long aulong;
1788 aulong = SvUV(fromstr);
1789 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
1797 aulong = SvUV(fromstr);
1798 CAT32(cat, &aulong);
1803 #if LONGSIZE != SIZE32
1809 along = SvIV(fromstr);
1810 sv_catpvn(cat, (char *)&along, sizeof(long));
1818 along = SvIV(fromstr);
1827 auquad = (Uquad_t)SvUV(fromstr);
1828 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
1834 aquad = (Quad_t)SvIV(fromstr);
1835 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
1840 len = 1; /* assume SV is correct length */
1845 if (fromstr == &PL_sv_undef)
1849 /* XXX better yet, could spirit away the string to
1850 * a safe spot and hang on to it until the result
1851 * of pack() (and all copies of the result) are
1854 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
1855 || (SvPADTMP(fromstr)
1856 && !SvREADONLY(fromstr))))
1858 Perl_warner(aTHX_ WARN_PACK,
1859 "Attempt to pack pointer to temporary value");
1861 if (SvPOK(fromstr) || SvNIOK(fromstr))
1862 aptr = SvPV(fromstr,n_a);
1864 aptr = SvPV_force(fromstr,n_a);
1866 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
1871 aptr = SvPV(fromstr, fromlen);
1872 SvGROW(cat, fromlen * 4 / 3);
1877 while (fromlen > 0) {
1884 doencodes(cat, aptr, todo);