Correct the editor block to match the perl 5 policy on tabs
[p5sagit/p5-mst-13.2.git] / pp_pack.c
index 427269f..6903415 100644 (file)
--- a/pp_pack.c
+++ b/pp_pack.c
@@ -1,7 +1,7 @@
 /*    pp_pack.c
  *
  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, 2004, by Larry Wall and others
+ *    2000, 2001, 2002, 2003, 2004, 2005, by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -831,8 +831,7 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                len = strend - s;
            if (checksum)
                goto uchar_checksum;
-           sv = NEWSV(35, len);
-           sv_setpvn(sv, s, len);
+           sv = newSVpvn(s, len);
            if (len > 0 && (datumtype == 'A' || datumtype == 'Z')) {
                aptr = s;       /* borrow register */
                if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
@@ -954,30 +953,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
        case 'c':
            if (len > strend - s)
                len = strend - s;
-           if (checksum) {
-               while (len-- > 0) {
-                   aint = *s++;
-                   if (aint >= 128)    /* fake up signed chars */
-                       aint -= 256;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aint;
-                   else
-                       cuv += aint;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   aint = *s++;
-                   if (aint >= 128)    /* fake up signed chars */
-                       aint -= 256;
-                   sv = NEWSV(36, 0);
-                   sv_setiv(sv, (IV)aint);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               aint = *s++;
+               if (aint >= 128)        /* fake up signed chars */
+                   aint -= 256;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)aint)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aint;
+               else
+                   cuv += aint;
            }
            break;
        case 'C':
@@ -1002,9 +994,7 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                EXTEND_MORTAL(len);
                while (len-- > 0) {
                    auint = *s++ & 255;
-                   sv = NEWSV(37, 0);
-                   sv_setiv(sv, (IV)auint);
-                   PUSHs(sv_2mortal(sv));
+                   PUSHs(sv_2mortal(newSViv((IV)auint)));
                }
            }
            break;
@@ -1017,32 +1007,24 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                 goto unpack_C;
            if (len > strend - s)
                len = strend - s;
-           if (checksum) {
-               while (len-- > 0 && s < strend) {
-                   STRLEN alen;
-                   auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANYUV));
-                   along = alen;
-                   s += along;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)auint;
-                   else
-                       cuv += auint;
-               }
-           }
-           else {
-                if (len && unpack_only_one)
+           if (!checksum) {
+               if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0 && s < strend) {
-                   STRLEN alen;
-                   auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANYUV));
-                   along = alen;
-                   s += along;
-                   sv = NEWSV(37, 0);
-                   sv_setuv(sv, (UV)auint);
-                   PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0 && s < strend) {
+               STRLEN alen;
+               auint = NATIVE_TO_UNI(utf8n_to_uvchr((U8*)s, strend - s, &alen, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANYUV));
+               along = alen;
+               s += along;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVuv((UV)auint)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auint;
+               else
+                   cuv += auint;
            }
            break;
        case 's' | TYPE_IS_SHRIEKING:
@@ -1050,30 +1032,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(short);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPYNN(s, &ashort, sizeof(short));
-                   DO_BO_UNPACK(ashort, s);
-                   s += sizeof(short);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)ashort;
-                   else
-                       cuv += ashort;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPYNN(s, &ashort, sizeof(short));
-                   DO_BO_UNPACK(ashort, s);
-                   s += sizeof(short);
-                   sv = NEWSV(38, 0);
-                   sv_setiv(sv, (IV)ashort);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               COPYNN(s, &ashort, sizeof(short));
+               DO_BO_UNPACK(ashort, s);
+               s += sizeof(short);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)ashort)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ashort;
+               else
+                   cuv += ashort;
            }
            break;
 #else
@@ -1083,39 +1058,27 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE16;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY16(s, &ai16);
-                   DO_BO_UNPACK(ai16, 16);
-#if U16SIZE > SIZE16
-                   if (ai16 > 32767)
-                       ai16 -= 65536;
-#endif
-                   s += SIZE16;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)ai16;
-                   else
-                       cuv += ai16;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-
-               while (len-- > 0) {
-                   COPY16(s, &ai16);
-                   DO_BO_UNPACK(ai16, 16);
+           }
+           while (len-- > 0) {
+               COPY16(s, &ai16);
+               DO_BO_UNPACK(ai16, 16);
 #if U16SIZE > SIZE16
-                   if (ai16 > 32767)
-                       ai16 -= 65536;
+               if (ai16 > 32767)
+                   ai16 -= 65536;
 #endif
-                   s += SIZE16;
-                   sv = NEWSV(38, 0);
-                   sv_setiv(sv, (IV)ai16);
-                   PUSHs(sv_2mortal(sv));
+               s += SIZE16;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)ai16)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai16;
+               else
+                   cuv += ai16;
            }
            break;
        case 'S' | TYPE_IS_SHRIEKING:
@@ -1123,30 +1086,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(unsigned short);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPYNN(s, &aushort, sizeof(unsigned short));
-                   DO_BO_UNPACK(aushort, s);
-                   s += sizeof(unsigned short);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aushort;
-                   else
-                       cuv += aushort;
-               }
-           }
-           else {
-                if (len && unpack_only_one)
+           if (!checksum) {
+               if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPYNN(s, &aushort, sizeof(unsigned short));
-                   DO_BO_UNPACK(aushort, s);
-                   s += sizeof(unsigned short);
-                   sv = NEWSV(39, 0);
-                   sv_setiv(sv, (UV)aushort);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               COPYNN(s, &aushort, sizeof(unsigned short));
+               DO_BO_UNPACK(aushort, s);
+               s += sizeof(unsigned short);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((UV)aushort)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aushort;
+               else
+                   cuv += aushort;
            }
            break;
 #else
@@ -1158,46 +1114,31 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE16;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY16(s, &au16);
-                   DO_BO_UNPACK(au16, 16);
-                   s += SIZE16;
-#ifdef HAS_NTOHS
-                   if (datumtype == 'n')
-                       au16 = PerlSock_ntohs(au16);
-#endif
-#ifdef HAS_VTOHS
-                   if (datumtype == 'v')
-                       au16 = vtohs(au16);
-#endif
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)au16;
-                   else
-                       cuv += au16;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPY16(s, &au16);
-                   DO_BO_UNPACK(au16, 16);
-                   s += SIZE16;
-                   sv = NEWSV(39, 0);
+           }
+           while (len-- > 0) {
+               COPY16(s, &au16);
+               DO_BO_UNPACK(au16, 16);
+               s += SIZE16;
 #ifdef HAS_NTOHS
-                   if (datumtype == 'n')
-                       au16 = PerlSock_ntohs(au16);
+               if (datumtype == 'n')
+                   au16 = PerlSock_ntohs(au16);
 #endif
 #ifdef HAS_VTOHS
-                   if (datumtype == 'v')
-                       au16 = vtohs(au16);
+               if (datumtype == 'v')
+                   au16 = vtohs(au16);
 #endif
-                   sv_setiv(sv, (UV)au16);
-                   PUSHs(sv_2mortal(sv));
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((UV)au16)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)au16;
+               else
+                   cuv += au16;
            }
            break;
        case 'v' | TYPE_IS_SHRIEKING:
@@ -1205,44 +1146,30 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE16;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY16(s, &ai16);
-                   s += SIZE16;
-#ifdef HAS_NTOHS
-                   if (datumtype == ('n' | TYPE_IS_SHRIEKING))
-                       ai16 = (I16)PerlSock_ntohs((U16)ai16);
-#endif
-#ifdef HAS_VTOHS
-                   if (datumtype == ('v' | TYPE_IS_SHRIEKING))
-                       ai16 = (I16)vtohs((U16)ai16);
-#endif
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)ai16;
-                   else
-                       cuv += ai16;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPY16(s, &ai16);
-                   s += SIZE16;
+           }
+           while (len-- > 0) {
+               COPY16(s, &ai16);
+               s += SIZE16;
 #ifdef HAS_NTOHS
-                   if (datumtype == ('n' | TYPE_IS_SHRIEKING))
-                       ai16 = (I16)PerlSock_ntohs((U16)ai16);
+               if (datumtype == ('n' | TYPE_IS_SHRIEKING))
+                   ai16 = (I16)PerlSock_ntohs((U16)ai16);
 #endif
 #ifdef HAS_VTOHS
-                   if (datumtype == ('v' | TYPE_IS_SHRIEKING))
-                       ai16 = (I16)vtohs((U16)ai16);
+               if (datumtype == ('v' | TYPE_IS_SHRIEKING))
+                   ai16 = (I16)vtohs((U16)ai16);
 #endif
-                   sv = NEWSV(39, 0);
-                   sv_setiv(sv, (IV)ai16);
-                   PUSHs(sv_2mortal(sv));
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)ai16)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai16;
+               else
+                   cuv += ai16;
            }
            break;
        case 'i':
@@ -1250,55 +1177,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(int);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &aint, 1, int);
-                   DO_BO_UNPACK(aint, i);
-                   s += sizeof(int);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aint;
-                   else
-                       cuv += aint;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &aint, 1, int);
-                   DO_BO_UNPACK(aint, i);
-                   s += sizeof(int);
-                   sv = NEWSV(40, 0);
-#ifdef __osf__
-                    /* Without the dummy below unpack("i", pack("i",-1))
-                     * return 0xFFffFFff instead of -1 for Digital Unix V4.0
-                     * cc with optimization turned on.
-                    *
-                    * The bug was detected in
-                    * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
-                    * with optimization (-O4) turned on.
-                    * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
-                    * does not have this problem even with -O4.
-                    *
-                    * This bug was reported as DECC_BUGS 1431
-                    * and tracked internally as GEM_BUGS 7775.
-                    *
-                    * The bug is fixed in
-                    * Tru64 UNIX V5.0:      Compaq C V6.1-006 or later
-                    * UNIX V4.0F support:   DEC C V5.9-006 or later
-                    * UNIX V4.0E support:   DEC C V5.8-011 or later
-                    * and also in DTK.
-                    *
-                    * See also few lines later for the same bug.
-                    */
-                    (aint) ?
-                       sv_setiv(sv, (IV)aint) :
-#endif
-                   sv_setiv(sv, (IV)aint);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               Copy(s, &aint, 1, int);
+               DO_BO_UNPACK(aint, i);
+               s += sizeof(int);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)aint)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aint;
+               else
+                   cuv += aint;
            }
            break;
        case 'I':
@@ -1306,121 +1201,81 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(unsigned int);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &auint, 1, unsigned int);
-                   DO_BO_UNPACK(auint, i);
-                   s += sizeof(unsigned int);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)auint;
-                   else
-                       cuv += auint;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &auint, 1, unsigned int);
-                   DO_BO_UNPACK(auint, i);
-                   s += sizeof(unsigned int);
-                   sv = NEWSV(41, 0);
-#ifdef __osf__
-                    /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
-                     * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
-                    * See details few lines earlier. */
-                    (auint) ?
-                       sv_setuv(sv, (UV)auint) :
-#endif
-                   sv_setuv(sv, (UV)auint);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               Copy(s, &auint, 1, unsigned int);
+               DO_BO_UNPACK(auint, i);
+               s += sizeof(unsigned int);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVuv((UV)auint)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auint;
+               else
+                   cuv += auint;
            }
            break;
        case 'j':
            along = (strend - s) / IVSIZE;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &aiv, 1, IV);
-#if IVSIZE == INTSIZE
-                   DO_BO_UNPACK(aiv, i);
-#elif IVSIZE == LONGSIZE
-                   DO_BO_UNPACK(aiv, l);
-#elif defined(HAS_QUAD) && IVSIZE == U64SIZE
-                   DO_BO_UNPACK(aiv, 64);
-#endif
-                   s += IVSIZE;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aiv;
-                   else
-                       cuv += aiv;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &aiv, 1, IV);
+           }
+           while (len-- > 0) {
+               Copy(s, &aiv, 1, IV);
 #if IVSIZE == INTSIZE
-                   DO_BO_UNPACK(aiv, i);
+               DO_BO_UNPACK(aiv, i);
 #elif IVSIZE == LONGSIZE
-                   DO_BO_UNPACK(aiv, l);
+               DO_BO_UNPACK(aiv, l);
 #elif defined(HAS_QUAD) && IVSIZE == U64SIZE
-                   DO_BO_UNPACK(aiv, 64);
+               DO_BO_UNPACK(aiv, 64);
 #endif
-                   s += IVSIZE;
-                   sv = NEWSV(40, 0);
-                   sv_setiv(sv, aiv);
-                   PUSHs(sv_2mortal(sv));
+               s += IVSIZE;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv(aiv)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aiv;
+               else
+                   cuv += aiv;
            }
            break;
        case 'J':
            along = (strend - s) / UVSIZE;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &auv, 1, UV);
-#if UVSIZE == INTSIZE
-                   DO_BO_UNPACK(auv, i);
-#elif UVSIZE == LONGSIZE
-                   DO_BO_UNPACK(auv, l);
-#elif defined(HAS_QUAD) && UVSIZE == U64SIZE
-                   DO_BO_UNPACK(auv, 64);
-#endif
-                   s += UVSIZE;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)auv;
-                   else
-                       cuv += auv;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &auv, 1, UV);
+           }
+           while (len-- > 0) {
+               Copy(s, &auv, 1, UV);
 #if UVSIZE == INTSIZE
-                   DO_BO_UNPACK(auv, i);
+               DO_BO_UNPACK(auv, i);
 #elif UVSIZE == LONGSIZE
-                   DO_BO_UNPACK(auv, l);
+               DO_BO_UNPACK(auv, l);
 #elif defined(HAS_QUAD) && UVSIZE == U64SIZE
-                   DO_BO_UNPACK(auv, 64);
+               DO_BO_UNPACK(auv, 64);
 #endif
-                   s += UVSIZE;
-                   sv = NEWSV(41, 0);
-                   sv_setuv(sv, auv);
-                   PUSHs(sv_2mortal(sv));
+               s += UVSIZE;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVuv(auv)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auv;
+               else
+                   cuv += auv;
            }
            break;
        case 'l' | TYPE_IS_SHRIEKING:
@@ -1428,30 +1283,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(long);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPYNN(s, &along, sizeof(long));
-                   DO_BO_UNPACK(along, l);
-                   s += sizeof(long);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)along;
-                   else
-                       cuv += along;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPYNN(s, &along, sizeof(long));
-                   DO_BO_UNPACK(along, l);
-                   s += sizeof(long);
-                   sv = NEWSV(42, 0);
-                   sv_setiv(sv, (IV)along);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               COPYNN(s, &along, sizeof(long));
+               DO_BO_UNPACK(along, l);
+               s += sizeof(long);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)along)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)along;
+               else
+                   cuv += along;
            }
            break;
 #else
@@ -1461,38 +1309,27 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE32;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY32(s, &ai32);
-                   DO_BO_UNPACK(ai32, 32);
-#if U32SIZE > SIZE32
-                   if (ai32 > 2147483647)
-                       ai32 -= 4294967296;
-#endif
-                   s += SIZE32;
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)ai32;
-                   else
-                       cuv += ai32;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPY32(s, &ai32);
-                   DO_BO_UNPACK(ai32, 32);
+           }
+           while (len-- > 0) {
+               COPY32(s, &ai32);
+               DO_BO_UNPACK(ai32, 32);
 #if U32SIZE > SIZE32
-                   if (ai32 > 2147483647)
-                       ai32 -= 4294967296;
+               if (ai32 > 2147483647)
+                   ai32 -= 4294967296;
 #endif
-                   s += SIZE32;
-                   sv = NEWSV(42, 0);
-                   sv_setiv(sv, (IV)ai32);
-                   PUSHs(sv_2mortal(sv));
+               s += SIZE32;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)ai32)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai32;
+               else
+                   cuv += ai32;
            }
            break;
        case 'L' | TYPE_IS_SHRIEKING:
@@ -1500,30 +1337,23 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(unsigned long);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPYNN(s, &aulong, sizeof(unsigned long));
-                   DO_BO_UNPACK(aulong, l);
-                   s += sizeof(unsigned long);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aulong;
-                   else
-                       cuv += aulong;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPYNN(s, &aulong, sizeof(unsigned long));
-                   DO_BO_UNPACK(aulong, l);
-                   s += sizeof(unsigned long);
-                   sv = NEWSV(43, 0);
-                   sv_setuv(sv, (UV)aulong);
-                   PUSHs(sv_2mortal(sv));
-               }
+           }
+           while (len-- > 0) {
+               COPYNN(s, &aulong, sizeof(unsigned long));
+               DO_BO_UNPACK(aulong, l);
+               s += sizeof(unsigned long);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVuv((UV)aulong)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aulong;
+               else
+                   cuv += aulong;
            }
            break;
 #else
@@ -1535,46 +1365,31 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE32;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY32(s, &au32);
-                   DO_BO_UNPACK(au32, 32);
-                   s += SIZE32;
-#ifdef HAS_NTOHL
-                   if (datumtype == 'N')
-                       au32 = PerlSock_ntohl(au32);
-#endif
-#ifdef HAS_VTOHL
-                   if (datumtype == 'V')
-                       au32 = vtohl(au32);
-#endif
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)au32;
-                   else
-                       cuv += au32;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPY32(s, &au32);
-                   DO_BO_UNPACK(au32, 32);
-                   s += SIZE32;
+           }
+           while (len-- > 0) {
+               COPY32(s, &au32);
+               DO_BO_UNPACK(au32, 32);
+               s += SIZE32;
 #ifdef HAS_NTOHL
-                   if (datumtype == 'N')
-                       au32 = PerlSock_ntohl(au32);
+               if (datumtype == 'N')
+                   au32 = PerlSock_ntohl(au32);
 #endif
 #ifdef HAS_VTOHL
-                   if (datumtype == 'V')
-                       au32 = vtohl(au32);
+               if (datumtype == 'V')
+                   au32 = vtohl(au32);
 #endif
-                   sv = NEWSV(43, 0);
-                   sv_setuv(sv, (UV)au32);
-                   PUSHs(sv_2mortal(sv));
-               }
+                if (!checksum) {
+                    PUSHs(sv_2mortal(newSVuv((UV)au32)));
+                }
+                else if (checksum > bits_in_uv)
+                    cdouble += (NV)au32;
+                else
+                    cuv += au32;
            }
            break;
        case 'V' | TYPE_IS_SHRIEKING:
@@ -1582,44 +1397,30 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / SIZE32;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   COPY32(s, &ai32);
-                   s += SIZE32;
-#ifdef HAS_NTOHL
-                   if (datumtype == ('N' | TYPE_IS_SHRIEKING))
-                       ai32 = (I32)PerlSock_ntohl((U32)ai32);
-#endif
-#ifdef HAS_VTOHL
-                   if (datumtype == ('V' | TYPE_IS_SHRIEKING))
-                       ai32 = (I32)vtohl((U32)ai32);
-#endif
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)ai32;
-                   else
-                       cuv += ai32;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   COPY32(s, &ai32);
-                   s += SIZE32;
+           }
+           while (len-- > 0) {
+               COPY32(s, &ai32);
+               s += SIZE32;
 #ifdef HAS_NTOHL
-                   if (datumtype == ('N' | TYPE_IS_SHRIEKING))
-                       ai32 = (I32)PerlSock_ntohl((U32)ai32);
+               if (datumtype == ('N' | TYPE_IS_SHRIEKING))
+                   ai32 = (I32)PerlSock_ntohl((U32)ai32);
 #endif
 #ifdef HAS_VTOHL
-                   if (datumtype == ('V' | TYPE_IS_SHRIEKING))
-                       ai32 = (I32)vtohl((U32)ai32);
+               if (datumtype == ('V' | TYPE_IS_SHRIEKING))
+                   ai32 = (I32)vtohl((U32)ai32);
 #endif
-                   sv = NEWSV(43, 0);
-                   sv_setiv(sv, (IV)ai32);
-                   PUSHs(sv_2mortal(sv));
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSViv((IV)ai32)));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai32;
+               else
+                   cuv += ai32;
            }
            break;
        case 'p':
@@ -1636,10 +1437,8 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                    DO_BO_UNPACK_P(aptr);
                    s += sizeof(char*);
                }
-               sv = NEWSV(44, 0);
-               if (aptr)
-                   sv_setpv(sv, aptr);
-               PUSHs(sv_2mortal(sv));
+               /* newSVpv generates undef if aptr is NULL */
+               PUSHs(sv_2mortal(newSVpv(aptr, 0)));
            }
            break;
        case 'w':
@@ -1656,9 +1455,7 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                    /* UTF8_IS_XXXXX not right here - using constant 0x80 */
                    if ((U8)(*s++) < 0x80) {
                        bytes = 0;
-                       sv = NEWSV(40, 0);
-                       sv_setuv(sv, auv);
-                       PUSHs(sv_2mortal(sv));
+                       PUSHs(sv_2mortal(newSVuv(auv)));
                        len--;
                        auv = 0;
                    }
@@ -1698,85 +1495,59 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                DO_BO_UNPACK_P(aptr);
                s += sizeof(char*);
            }
-           sv = NEWSV(44, 0);
-           if (aptr)
-               sv_setpvn(sv, aptr, len);
-           PUSHs(sv_2mortal(sv));
+           /* newSVpvn generates undef if aptr is NULL */
+           PUSHs(sv_2mortal(newSVpvn(aptr, len)));
            break;
 #ifdef HAS_QUAD
        case 'q':
            along = (strend - s) / sizeof(Quad_t);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &aquad, 1, Quad_t);
-                   DO_BO_UNPACK(aquad, 64);
-                   s += sizeof(Quad_t);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)aquad;
-                   else
-                       cuv += aquad;
-               }
-           }
-            else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                 EXTEND(SP, len);
                 EXTEND_MORTAL(len);
-                while (len-- > 0) {
-                    if (s + sizeof(Quad_t) > strend)
-                        aquad = 0;
-                    else {
-                       Copy(s, &aquad, 1, Quad_t);
-                       DO_BO_UNPACK(aquad, 64);
-                       s += sizeof(Quad_t);
-                    }
-                    sv = NEWSV(42, 0);
-                    if (aquad >= IV_MIN && aquad <= IV_MAX)
-                       sv_setiv(sv, (IV)aquad);
-                    else
-                        sv_setnv(sv, (NV)aquad);
-                    PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0) {
+               assert (s + sizeof(Quad_t) <= strend);
+               Copy(s, &aquad, 1, Quad_t);
+               DO_BO_UNPACK(aquad, 64);
+               s += sizeof(Quad_t);
+               if (!checksum) {
+                    PUSHs(sv_2mortal((aquad >= IV_MIN && aquad <= IV_MAX) ?
+                                    newSViv((IV)aquad) : newSVnv((NV)aquad)));
                 }
-            }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aquad;
+               else
+                   cuv += aquad;
+           }
            break;
        case 'Q':
            along = (strend - s) / sizeof(Uquad_t);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &auquad, 1, Uquad_t);
-                   DO_BO_UNPACK(auquad, 64);
-                   s += sizeof(Uquad_t);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)auquad;
-                   else
-                       cuv += auquad;
-               }
-           }
-            else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                 EXTEND(SP, len);
                 EXTEND_MORTAL(len);
-                while (len-- > 0) {
-                    if (s + sizeof(Uquad_t) > strend)
-                        auquad = 0;
-                    else {
-                        Copy(s, &auquad, 1, Uquad_t);
-                       DO_BO_UNPACK(auquad, 64);
-                        s += sizeof(Uquad_t);
-                    }
-                    sv = NEWSV(43, 0);
-                    if (auquad <= UV_MAX)
-                        sv_setuv(sv, (UV)auquad);
-                    else
-                   sv_setnv(sv, (NV)auquad);
-                    PUSHs(sv_2mortal(sv));
-                }
-            }
+           }
+           while (len-- > 0) {
+               assert (s + sizeof(Uquad_t) <= strend);
+               Copy(s, &auquad, 1, Uquad_t);
+               DO_BO_UNPACK(auquad, 64);
+               s += sizeof(Uquad_t);
+               if (!checksum) {
+                   PUSHs(sv_2mortal((auquad <= UV_MAX) ?
+                                    newSVuv((UV)auquad) : newSVnv((NV)auquad)));
+               }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auquad;
+               else
+                   cuv += auquad;
+           }
            break;
 #endif
        /* float and double added gnb@melba.bby.oz.au 22/11/89 */
@@ -1784,26 +1555,21 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(float);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &afloat, 1, float);
-                   DO_BO_UNPACK_N(afloat, float);
-                   s += sizeof(float);
-                   cdouble += afloat;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &afloat, 1, float);
-                   DO_BO_UNPACK_N(afloat, float);
-                   s += sizeof(float);
-                   sv = NEWSV(47, 0);
-                   sv_setnv(sv, (NV)afloat);
-                   PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0) {
+               Copy(s, &afloat, 1, float);
+               DO_BO_UNPACK_N(afloat, float);
+               s += sizeof(float);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVnv((NV)afloat)));
+               }
+               else {
+                   cdouble += afloat;
                }
            }
            break;
@@ -1811,26 +1577,21 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / sizeof(double);
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &adouble, 1, double);
-                   DO_BO_UNPACK_N(adouble, double);
-                   s += sizeof(double);
-                   cdouble += adouble;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &adouble, 1, double);
-                   DO_BO_UNPACK_N(adouble, double);
-                   s += sizeof(double);
-                   sv = NEWSV(48, 0);
-                   sv_setnv(sv, (NV)adouble);
-                   PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0) {
+               Copy(s, &adouble, 1, double);
+               DO_BO_UNPACK_N(adouble, double);
+               s += sizeof(double);
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVnv((NV)adouble)));
+               }
+               else {
+                   cdouble += adouble;
                }
            }
            break;
@@ -1838,26 +1599,21 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / NVSIZE;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &anv, 1, NV);
-                   DO_BO_UNPACK_N(anv, NV);
-                   s += NVSIZE;
-                   cdouble += anv;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &anv, 1, NV);
-                   DO_BO_UNPACK_N(anv, NV);
-                   s += NVSIZE;
-                   sv = NEWSV(48, 0);
-                   sv_setnv(sv, anv);
-                   PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0) {
+               Copy(s, &anv, 1, NV);
+               DO_BO_UNPACK_N(anv, NV);
+               s += NVSIZE;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVnv(anv)));
+               }
+               else {
+                   cdouble += anv;
                }
            }
            break;
@@ -1866,26 +1622,20 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            along = (strend - s) / LONG_DOUBLESIZE;
            if (len > along)
                len = along;
-           if (checksum) {
-               while (len-- > 0) {
-                   Copy(s, &aldouble, 1, long double);
-                   DO_BO_UNPACK_N(aldouble, long double);
-                   s += LONG_DOUBLESIZE;
-                   cdouble += aldouble;
-               }
-           }
-           else {
+           if (!checksum) {
                 if (len && unpack_only_one)
                     len = 1;
                EXTEND(SP, len);
                EXTEND_MORTAL(len);
-               while (len-- > 0) {
-                   Copy(s, &aldouble, 1, long double);
-                   DO_BO_UNPACK_N(aldouble, long double);
-                   s += LONG_DOUBLESIZE;
-                   sv = NEWSV(48, 0);
-                   sv_setnv(sv, (NV)aldouble);
-                   PUSHs(sv_2mortal(sv));
+           }
+           while (len-- > 0) {
+               Copy(s, &aldouble, 1, long double);
+               DO_BO_UNPACK_N(aldouble, long double);
+               s += LONG_DOUBLESIZE;
+               if (!checksum) {
+                   PUSHs(sv_2mortal(newSVnv((NV)aldouble)));
+               }
+               else {cdouble += aldouble;
                }
            }
            break;
@@ -1952,7 +1702,6 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
        }
 
        if (checksum) {
-           sv = NEWSV(42, 0);
            if (strchr("fFdD", TYPE_NO_MODIFIERS(datumtype)) ||
              (checksum > bits_in_uv &&
               strchr("csSiIlLnNUvVqQjJ", TYPE_NO_MODIFIERS(datumtype))) ) {
@@ -1966,14 +1715,14 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
                while (cdouble < 0.0)
                    cdouble += adouble;
                cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
-               sv_setnv(sv, cdouble);
+               sv = newSVnv(cdouble);
            }
            else {
                if (checksum < bits_in_uv) {
                    UV mask = ((UV)1 << checksum) - 1;
                    cuv &= mask;
                }
-               sv_setuv(sv, cuv);
+               sv = newSVuv(cuv);
            }
            XPUSHs(sv_2mortal(sv));
            checksum = 0;
@@ -2989,3 +2738,12 @@ PP(pp_pack)
     RETURN;
 }
 
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vim: shiftwidth=4:
+*/