3 * Copyright (c) 1991-2001, 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 #define PERL_IN_PP_PACK_C
15 * The compiler on Concurrent CX/UX systems has a subtle bug which only
16 * seems to show up when compiling pp.c - it generates the wrong double
17 * precision constant value for (double)UV_MAX when used inline in the body
18 * of the code below, so this makes a static variable up front (which the
19 * compiler seems to get correct) and uses it in place of UV_MAX below.
21 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
22 static double UV_MAX_cxux = ((double)UV_MAX);
26 * Offset for integer pack/unpack.
28 * On architectures where I16 and I32 aren't really 16 and 32 bits,
29 * which for now are all Crays, pack and unpack have to play games.
33 * These values are required for portability of pack() output.
34 * If they're not right on your machine, then pack() and unpack()
35 * wouldn't work right anyway; you'll need to apply the Cray hack.
36 * (I'd like to check them with #if, but you can't use sizeof() in
37 * the preprocessor.) --???
40 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
41 defines are now in config.h. --Andy Dougherty April 1998
46 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
49 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
50 # define PERL_NATINT_PACK
53 #if LONGSIZE > 4 && defined(_CRAY)
54 # if BYTEORDER == 0x12345678
55 # define OFF16(p) (char*)(p)
56 # define OFF32(p) (char*)(p)
58 # if BYTEORDER == 0x87654321
59 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
60 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
62 }}}} bad cray byte order
65 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
66 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
67 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
68 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
69 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
71 # define COPY16(s,p) Copy(s, p, SIZE16, char)
72 # define COPY32(s,p) Copy(s, p, SIZE32, char)
73 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
74 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
75 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
79 S_mul128(pTHX_ SV *sv, U8 m)
82 char *s = SvPV(sv, len);
86 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
87 SV *tmpNew = newSVpvn("0000000000", 10);
90 SvREFCNT_dec(sv); /* free old sv */
95 while (!*t) /* trailing '\0'? */
98 i = ((*t - '0') << 7) + m;
99 *(t--) = '0' + (i % 10);
105 /* Explosives and implosives. */
107 #if 'I' == 73 && 'J' == 74
108 /* On an ASCII/ISO kind of system */
109 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
112 Some other sort of character set - use memchr() so we don't match
115 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
123 I32 start_sp_offset = SP - PL_stack_base;
128 register char *pat = SvPV(left, llen);
129 #ifdef PACKED_IS_OCTETS
130 /* Packed side is assumed to be octets - so force downgrade if it
131 has been UTF-8 encoded by accident
133 register char *s = SvPVbyte(right, rlen);
135 register char *s = SvPV(right, rlen);
137 char *strend = s + rlen;
139 register char *patend = pat + llen;
142 register I32 bits = 0;
145 /* These must not be in registers: */
162 register U32 culong = 0;
166 #ifdef PERL_NATINT_PACK
167 int natint; /* native integer */
168 int unatint; /* unsigned native integer */
170 bool do_utf8 = DO_UTF8(right);
172 if (gimme != G_ARRAY) { /* arrange to do first one only */
174 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
175 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
177 while (isDIGIT(*patend) || *patend == '*')
183 while (pat < patend) {
185 datumtype = *pat++ & 0xFF;
186 #ifdef PERL_NATINT_PACK
189 if (isSPACE(datumtype))
191 if (datumtype == '#') {
192 while (pat < patend && *pat != '\n')
197 char *natstr = "sSiIlL";
199 if (strchr(natstr, datumtype)) {
200 #ifdef PERL_NATINT_PACK
206 DIE(aTHX_ "'!' allowed only after types %s", natstr);
211 else if (*pat == '*') {
212 len = strend - strbeg; /* long enough */
216 else if (isDIGIT(*pat)) {
218 while (isDIGIT(*pat)) {
219 len = (len * 10) + (*pat++ - '0');
221 DIE(aTHX_ "Repeat count in unpack overflows");
225 len = (datumtype != '@');
229 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
230 case ',': /* grandfather in commas but with a warning */
231 if (commas++ == 0 && ckWARN(WARN_UNPACK))
232 Perl_warner(aTHX_ WARN_UNPACK,
233 "Invalid type in unpack: '%c'", (int)datumtype);
236 if (len == 1 && pat[-1] != '1')
245 if (len > strend - strbeg)
246 DIE(aTHX_ "@ outside of string");
250 if (len > s - strbeg)
251 DIE(aTHX_ "X outside of string");
255 if (len > strend - s)
256 DIE(aTHX_ "x outside of string");
260 if (start_sp_offset >= SP - PL_stack_base)
261 DIE(aTHX_ "/ must follow a numeric type");
264 pat++; /* ignore '*' for compatibility with pack */
266 DIE(aTHX_ "/ cannot take a count" );
273 if (len > strend - s)
278 sv_setpvn(sv, s, len);
280 if (datumtype == 'A' || datumtype == 'Z') {
281 aptr = s; /* borrow register */
282 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
287 else { /* 'A' strips both nulls and spaces */
288 s = SvPVX(sv) + len - 1;
289 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
293 SvCUR_set(sv, s - SvPVX(sv));
294 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 */
411 if (aint >= 128) /* fake up signed chars */
414 sv_setiv(sv, (IV)aint);
415 PUSHs(sv_2mortal(sv));
420 unpack_C: /* unpack U will jump here if not UTF-8 */
425 if (len > strend - s)
440 sv_setiv(sv, (IV)auint);
441 PUSHs(sv_2mortal(sv));
452 if (len > strend - s)
455 while (len-- > 0 && s < strend) {
457 auint = utf8n_to_uvchr((U8*)s, strend - s, &alen, 0);
461 cdouble += (NV)auint;
469 while (len-- > 0 && s < strend) {
471 auint = utf8n_to_uvchr((U8*)s, strend - s, &alen, 0);
475 sv_setuv(sv, (UV)auint);
476 PUSHs(sv_2mortal(sv));
481 #if SHORTSIZE == SIZE16
482 along = (strend - s) / SIZE16;
484 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
489 #if SHORTSIZE != SIZE16
493 COPYNN(s, &ashort, sizeof(short));
504 #if SHORTSIZE > SIZE16
516 #if SHORTSIZE != SIZE16
520 COPYNN(s, &ashort, sizeof(short));
523 sv_setiv(sv, (IV)ashort);
524 PUSHs(sv_2mortal(sv));
532 #if SHORTSIZE > SIZE16
538 sv_setiv(sv, (IV)ashort);
539 PUSHs(sv_2mortal(sv));
547 #if SHORTSIZE == SIZE16
548 along = (strend - s) / SIZE16;
550 unatint = natint && datumtype == 'S';
551 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
556 #if SHORTSIZE != SIZE16
558 unsigned short aushort;
560 COPYNN(s, &aushort, sizeof(unsigned short));
561 s += sizeof(unsigned short);
572 if (datumtype == 'n')
573 aushort = PerlSock_ntohs(aushort);
576 if (datumtype == 'v')
577 aushort = vtohs(aushort);
586 #if SHORTSIZE != SIZE16
588 unsigned short aushort;
590 COPYNN(s, &aushort, sizeof(unsigned short));
591 s += sizeof(unsigned short);
593 sv_setiv(sv, (UV)aushort);
594 PUSHs(sv_2mortal(sv));
605 if (datumtype == 'n')
606 aushort = PerlSock_ntohs(aushort);
609 if (datumtype == 'v')
610 aushort = vtohs(aushort);
612 sv_setiv(sv, (UV)aushort);
613 PUSHs(sv_2mortal(sv));
619 along = (strend - s) / sizeof(int);
624 Copy(s, &aint, 1, int);
636 Copy(s, &aint, 1, int);
640 /* Without the dummy below unpack("i", pack("i",-1))
641 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
642 * cc with optimization turned on.
644 * The bug was detected in
645 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
646 * with optimization (-O4) turned on.
647 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
648 * does not have this problem even with -O4.
650 * This bug was reported as DECC_BUGS 1431
651 * and tracked internally as GEM_BUGS 7775.
653 * The bug is fixed in
654 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
655 * UNIX V4.0F support: DEC C V5.9-006 or later
656 * UNIX V4.0E support: DEC C V5.8-011 or later
659 * See also few lines later for the same bug.
662 sv_setiv(sv, (IV)aint) :
664 sv_setiv(sv, (IV)aint);
665 PUSHs(sv_2mortal(sv));
670 along = (strend - s) / sizeof(unsigned int);
675 Copy(s, &auint, 1, unsigned int);
676 s += sizeof(unsigned int);
678 cdouble += (NV)auint;
687 Copy(s, &auint, 1, unsigned int);
688 s += sizeof(unsigned int);
691 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
692 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
693 * See details few lines earlier. */
695 sv_setuv(sv, (UV)auint) :
697 sv_setuv(sv, (UV)auint);
698 PUSHs(sv_2mortal(sv));
703 #if LONGSIZE == SIZE32
704 along = (strend - s) / SIZE32;
706 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
711 #if LONGSIZE != SIZE32
714 COPYNN(s, &along, sizeof(long));
717 cdouble += (NV)along;
726 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
730 #if LONGSIZE > SIZE32
731 if (along > 2147483647)
736 cdouble += (NV)along;
745 #if LONGSIZE != SIZE32
748 COPYNN(s, &along, sizeof(long));
751 sv_setiv(sv, (IV)along);
752 PUSHs(sv_2mortal(sv));
759 #if LONGSIZE > SIZE32 && INTSIZE == SIZE32
763 #if LONGSIZE > SIZE32
764 if (along > 2147483647)
769 sv_setiv(sv, (IV)along);
770 PUSHs(sv_2mortal(sv));
778 #if LONGSIZE == SIZE32
779 along = (strend - s) / SIZE32;
781 unatint = natint && datumtype == 'L';
782 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
787 #if LONGSIZE != SIZE32
789 unsigned long aulong;
791 COPYNN(s, &aulong, sizeof(unsigned long));
792 s += sizeof(unsigned long);
794 cdouble += (NV)aulong;
806 if (datumtype == 'N')
807 aulong = PerlSock_ntohl(aulong);
810 if (datumtype == 'V')
811 aulong = vtohl(aulong);
814 cdouble += (NV)aulong;
823 #if LONGSIZE != SIZE32
825 unsigned long aulong;
827 COPYNN(s, &aulong, sizeof(unsigned long));
828 s += sizeof(unsigned long);
830 sv_setuv(sv, (UV)aulong);
831 PUSHs(sv_2mortal(sv));
841 if (datumtype == 'N')
842 aulong = PerlSock_ntohl(aulong);
845 if (datumtype == 'V')
846 aulong = vtohl(aulong);
849 sv_setuv(sv, (UV)aulong);
850 PUSHs(sv_2mortal(sv));
856 along = (strend - s) / sizeof(char*);
862 if (sizeof(char*) > strend - s)
865 Copy(s, &aptr, 1, char*);
871 PUSHs(sv_2mortal(sv));
881 while ((len > 0) && (s < strend)) {
882 auv = (auv << 7) | (*s & 0x7f);
883 /* UTF8_IS_XXXXX not right here - using constant 0x80 */
884 if ((U8)(*s++) < 0x80) {
888 PUSHs(sv_2mortal(sv));
892 else if (++bytes >= sizeof(UV)) { /* promote to string */
896 sv = Perl_newSVpvf(aTHX_ "%.*"UVf, (int)TYPE_DIGITS(UV), auv);
898 sv = mul128(sv, *s & 0x7f);
899 if (!(*s++ & 0x80)) {
908 PUSHs(sv_2mortal(sv));
913 if ((s >= strend) && bytes)
914 DIE(aTHX_ "Unterminated compressed integer");
919 if (sizeof(char*) > strend - s)
922 Copy(s, &aptr, 1, char*);
927 sv_setpvn(sv, aptr, len);
928 PUSHs(sv_2mortal(sv));
932 along = (strend - s) / sizeof(Quad_t);
938 if (s + sizeof(Quad_t) > strend)
941 Copy(s, &aquad, 1, Quad_t);
945 if (aquad >= IV_MIN && aquad <= IV_MAX)
946 sv_setiv(sv, (IV)aquad);
948 sv_setnv(sv, (NV)aquad);
949 PUSHs(sv_2mortal(sv));
953 along = (strend - s) / sizeof(Quad_t);
959 if (s + sizeof(Uquad_t) > strend)
962 Copy(s, &auquad, 1, Uquad_t);
963 s += sizeof(Uquad_t);
966 if (auquad <= UV_MAX)
967 sv_setuv(sv, (UV)auquad);
969 sv_setnv(sv, (NV)auquad);
970 PUSHs(sv_2mortal(sv));
974 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
977 along = (strend - s) / sizeof(float);
982 Copy(s, &afloat, 1, float);
991 Copy(s, &afloat, 1, float);
994 sv_setnv(sv, (NV)afloat);
995 PUSHs(sv_2mortal(sv));
1001 along = (strend - s) / sizeof(double);
1006 Copy(s, &adouble, 1, double);
1007 s += sizeof(double);
1015 Copy(s, &adouble, 1, double);
1016 s += sizeof(double);
1018 sv_setnv(sv, (NV)adouble);
1019 PUSHs(sv_2mortal(sv));
1025 * Initialise the decode mapping. By using a table driven
1026 * algorithm, the code will be character-set independent
1027 * (and just as fast as doing character arithmetic)
1029 if (PL_uudmap['M'] == 0) {
1032 for (i = 0; i < sizeof(PL_uuemap); i += 1)
1033 PL_uudmap[(U8)PL_uuemap[i]] = i;
1035 * Because ' ' and '`' map to the same value,
1036 * we need to decode them both the same.
1041 along = (strend - s) * 3 / 4;
1042 sv = NEWSV(42, along);
1045 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
1050 len = PL_uudmap[*(U8*)s++] & 077;
1052 if (s < strend && ISUUCHAR(*s))
1053 a = PL_uudmap[*(U8*)s++] & 077;
1056 if (s < strend && ISUUCHAR(*s))
1057 b = PL_uudmap[*(U8*)s++] & 077;
1060 if (s < strend && ISUUCHAR(*s))
1061 c = PL_uudmap[*(U8*)s++] & 077;
1064 if (s < strend && ISUUCHAR(*s))
1065 d = PL_uudmap[*(U8*)s++] & 077;
1068 hunk[0] = (a << 2) | (b >> 4);
1069 hunk[1] = (b << 4) | (c >> 2);
1070 hunk[2] = (c << 6) | d;
1071 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
1076 else if (s[1] == '\n') /* possible checksum byte */
1079 XPUSHs(sv_2mortal(sv));
1084 if (strchr("fFdD", datumtype) ||
1085 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
1089 while (checksum >= 16) {
1093 while (checksum >= 4) {
1099 along = (1 << checksum) - 1;
1100 while (cdouble < 0.0)
1102 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
1103 sv_setnv(sv, cdouble);
1106 if (checksum < 32) {
1107 aulong = (1 << checksum) - 1;
1110 sv_setuv(sv, (UV)culong);
1112 XPUSHs(sv_2mortal(sv));
1116 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
1117 PUSHs(&PL_sv_undef);
1122 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
1126 *hunk = PL_uuemap[len];
1127 sv_catpvn(sv, hunk, 1);
1130 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1131 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
1132 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
1133 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
1134 sv_catpvn(sv, hunk, 4);
1139 char r = (len > 1 ? s[1] : '\0');
1140 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
1141 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
1142 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
1143 hunk[3] = PL_uuemap[0];
1144 sv_catpvn(sv, hunk, 4);
1146 sv_catpvn(sv, "\n", 1);
1150 S_is_an_int(pTHX_ char *s, STRLEN l)
1153 SV *result = newSVpvn(s, l);
1154 char *result_c = SvPV(result, n_a); /* convenience */
1155 char *out = result_c;
1165 SvREFCNT_dec(result);
1188 SvREFCNT_dec(result);
1194 SvCUR_set(result, out - result_c);
1198 /* pnum must be '\0' terminated */
1200 S_div128(pTHX_ SV *pnum, bool *done)
1203 char *s = SvPV(pnum, len);
1212 i = m * 10 + (*t - '0');
1214 r = (i >> 7); /* r < 10 */
1221 SvCUR_set(pnum, (STRLEN) (t - s));
1228 dSP; dMARK; dORIGMARK; dTARGET;
1229 register SV *cat = TARG;
1232 register char *pat = SvPVx(*++MARK, fromlen);
1234 register char *patend = pat + fromlen;
1239 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
1240 static char *space10 = " ";
1242 /* These must not be in registers: */
1257 #ifdef PERL_NATINT_PACK
1258 int natint; /* native integer */
1263 sv_setpvn(cat, "", 0);
1265 while (pat < patend) {
1266 SV *lengthcode = Nullsv;
1267 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
1268 datumtype = *pat++ & 0xFF;
1269 #ifdef PERL_NATINT_PACK
1272 if (isSPACE(datumtype)) {
1276 #ifndef PACKED_IS_OCTETS
1277 if (datumtype == 'U' && pat == patcopy+1)
1280 if (datumtype == '#') {
1281 while (pat < patend && *pat != '\n')
1286 char *natstr = "sSiIlL";
1288 if (strchr(natstr, datumtype)) {
1289 #ifdef PERL_NATINT_PACK
1295 DIE(aTHX_ "'!' allowed only after types %s", natstr);
1298 len = strchr("@Xxu", datumtype) ? 0 : items;
1301 else if (isDIGIT(*pat)) {
1303 while (isDIGIT(*pat)) {
1304 len = (len * 10) + (*pat++ - '0');
1306 DIE(aTHX_ "Repeat count in pack overflows");
1313 if ((*pat != 'a' && *pat != 'A' && *pat != 'Z') || pat[1] != '*')
1314 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
1315 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
1316 ? *MARK : &PL_sv_no)
1317 + (*pat == 'Z' ? 1 : 0)));
1321 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
1322 case ',': /* grandfather in commas but with a warning */
1323 if (commas++ == 0 && ckWARN(WARN_PACK))
1324 Perl_warner(aTHX_ WARN_PACK,
1325 "Invalid type in pack: '%c'", (int)datumtype);
1328 DIE(aTHX_ "%% may only be used in unpack");
1339 if (SvCUR(cat) < len)
1340 DIE(aTHX_ "X outside of string");
1347 sv_catpvn(cat, null10, 10);
1350 sv_catpvn(cat, null10, len);
1356 aptr = SvPV(fromstr, fromlen);
1357 if (pat[-1] == '*') {
1359 if (datumtype == 'Z')
1362 if (fromlen >= len) {
1363 sv_catpvn(cat, aptr, len);
1364 if (datumtype == 'Z')
1365 *(SvEND(cat)-1) = '\0';
1368 sv_catpvn(cat, aptr, fromlen);
1370 if (datumtype == 'A') {
1372 sv_catpvn(cat, space10, 10);
1375 sv_catpvn(cat, space10, len);
1379 sv_catpvn(cat, null10, 10);
1382 sv_catpvn(cat, null10, len);
1394 str = SvPV(fromstr, fromlen);
1398 SvCUR(cat) += (len+7)/8;
1399 SvGROW(cat, SvCUR(cat) + 1);
1400 aptr = SvPVX(cat) + aint;
1405 if (datumtype == 'B') {
1406 for (len = 0; len++ < aint;) {
1407 items |= *str++ & 1;
1411 *aptr++ = items & 0xff;
1417 for (len = 0; len++ < aint;) {
1423 *aptr++ = items & 0xff;
1429 if (datumtype == 'B')
1430 items <<= 7 - (aint & 7);
1432 items >>= 7 - (aint & 7);
1433 *aptr++ = items & 0xff;
1435 str = SvPVX(cat) + SvCUR(cat);
1450 str = SvPV(fromstr, fromlen);
1454 SvCUR(cat) += (len+1)/2;
1455 SvGROW(cat, SvCUR(cat) + 1);
1456 aptr = SvPVX(cat) + aint;
1461 if (datumtype == 'H') {
1462 for (len = 0; len++ < aint;) {
1464 items |= ((*str++ & 15) + 9) & 15;
1466 items |= *str++ & 15;
1470 *aptr++ = items & 0xff;
1476 for (len = 0; len++ < aint;) {
1478 items |= (((*str++ & 15) + 9) & 15) << 4;
1480 items |= (*str++ & 15) << 4;
1484 *aptr++ = items & 0xff;
1490 *aptr++ = items & 0xff;
1491 str = SvPVX(cat) + SvCUR(cat);
1502 switch (datumtype) {
1504 aint = SvIV(fromstr);
1505 if ((aint < 0 || aint > 255) &&
1507 Perl_warner(aTHX_ WARN_PACK,
1508 "Character in \"C\" format wrapped");
1510 sv_catpvn(cat, &achar, sizeof(char));
1513 aint = SvIV(fromstr);
1514 if ((aint < -128 || aint > 127) &&
1516 Perl_warner(aTHX_ WARN_PACK,
1517 "Character in \"c\" format wrapped");
1519 sv_catpvn(cat, &achar, sizeof(char));
1527 auint = SvUV(fromstr);
1528 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN + 1);
1529 SvCUR_set(cat, (char*)uvchr_to_utf8((U8*)SvEND(cat),auint)
1534 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
1539 afloat = (float)SvNV(fromstr);
1540 sv_catpvn(cat, (char *)&afloat, sizeof (float));
1547 adouble = (double)SvNV(fromstr);
1548 sv_catpvn(cat, (char *)&adouble, sizeof (double));
1554 ashort = (I16)SvIV(fromstr);
1556 ashort = PerlSock_htons(ashort);
1558 CAT16(cat, &ashort);
1564 ashort = (I16)SvIV(fromstr);
1566 ashort = htovs(ashort);
1568 CAT16(cat, &ashort);
1572 #if SHORTSIZE != SIZE16
1574 unsigned short aushort;
1578 aushort = SvUV(fromstr);
1579 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
1589 aushort = (U16)SvUV(fromstr);
1590 CAT16(cat, &aushort);
1596 #if SHORTSIZE != SIZE16
1602 ashort = SvIV(fromstr);
1603 sv_catpvn(cat, (char *)&ashort, sizeof(short));
1611 ashort = (I16)SvIV(fromstr);
1612 CAT16(cat, &ashort);
1619 auint = SvUV(fromstr);
1620 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
1626 adouble = Perl_floor(SvNV(fromstr));
1629 DIE(aTHX_ "Cannot compress negative numbers");
1632 #if UVSIZE > 4 && UVSIZE >= NVSIZE
1633 adouble <= 0xffffffff
1635 # ifdef CXUX_BROKEN_CONSTANT_CONVERT
1636 adouble <= UV_MAX_cxux
1643 char buf[1 + sizeof(UV)];
1644 char *in = buf + sizeof(buf);
1645 UV auv = U_V(adouble);
1648 *--in = (auv & 0x7f) | 0x80;
1651 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1652 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1654 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
1655 char *from, *result, *in;
1660 /* Copy string and check for compliance */
1661 from = SvPV(fromstr, len);
1662 if ((norm = is_an_int(from, len)) == NULL)
1663 DIE(aTHX_ "can compress only unsigned integer");
1665 New('w', result, len, char);
1669 *--in = div128(norm, &done) | 0x80;
1670 result[len - 1] &= 0x7F; /* clear continue bit */
1671 sv_catpvn(cat, in, (result + len) - in);
1673 SvREFCNT_dec(norm); /* free norm */
1675 else if (SvNOKp(fromstr)) {
1676 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
1677 char *in = buf + sizeof(buf);
1680 double next = floor(adouble / 128);
1681 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
1682 if (in <= buf) /* this cannot happen ;-) */
1683 DIE(aTHX_ "Cannot compress integer");
1685 } while (adouble > 0);
1686 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
1687 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
1690 DIE(aTHX_ "Cannot compress non integer");
1696 aint = SvIV(fromstr);
1697 sv_catpvn(cat, (char*)&aint, sizeof(int));
1703 aulong = SvUV(fromstr);
1705 aulong = PerlSock_htonl(aulong);
1707 CAT32(cat, &aulong);
1713 aulong = SvUV(fromstr);
1715 aulong = htovl(aulong);
1717 CAT32(cat, &aulong);
1721 #if LONGSIZE != SIZE32
1723 unsigned long aulong;
1727 aulong = SvUV(fromstr);
1728 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
1736 aulong = SvUV(fromstr);
1737 CAT32(cat, &aulong);
1742 #if LONGSIZE != SIZE32
1748 along = SvIV(fromstr);
1749 sv_catpvn(cat, (char *)&along, sizeof(long));
1757 along = SvIV(fromstr);
1766 auquad = (Uquad_t)SvUV(fromstr);
1767 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
1773 aquad = (Quad_t)SvIV(fromstr);
1774 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
1779 len = 1; /* assume SV is correct length */
1784 if (fromstr == &PL_sv_undef)
1788 /* XXX better yet, could spirit away the string to
1789 * a safe spot and hang on to it until the result
1790 * of pack() (and all copies of the result) are
1793 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
1794 || (SvPADTMP(fromstr)
1795 && !SvREADONLY(fromstr))))
1797 Perl_warner(aTHX_ WARN_PACK,
1798 "Attempt to pack pointer to temporary value");
1800 if (SvPOK(fromstr) || SvNIOK(fromstr))
1801 aptr = SvPV(fromstr,n_a);
1803 aptr = SvPV_force(fromstr,n_a);
1805 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
1810 aptr = SvPV(fromstr, fromlen);
1811 SvGROW(cat, fromlen * 4 / 3);
1816 while (fromlen > 0) {
1823 doencodes(cat, aptr, todo);