This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The leaktesting of NEWSV() is long dead, so create and initialise
[perl5.git] / pp_pack.c
index 5183eaf..3d47ada 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.
  * some salt.
  */
 
+/* This file contains pp ("push/pop") functions that
+ * execute the opcodes that make up a perl program. A typical pp function
+ * expects to find its arguments on the stack, and usually pushes its
+ * results onto the stack, hence the 'pp' terminology. Each OP structure
+ * contains a pointer to the relevant pp_foo() function.
+ *
+ * This particular file just contains pp_pack() and pp_unpack(). See the
+ * other pp*.c files for the rest of the pp_ functions.
+ */
+
+
 #include "EXTERN.h"
 #define PERL_IN_PP_PACK_C
 #include "perl.h"
 
 /*
- * The compiler on Concurrent CX/UX systems has a subtle bug which only
- * seems to show up when compiling pp.c - it generates the wrong double
- * precision constant value for (double)UV_MAX when used inline in the body
- * of the code below, so this makes a static variable up front (which the
- * compiler seems to get correct) and uses it in place of UV_MAX below.
- */
-#ifdef CXUX_BROKEN_CONSTANT_CONVERT
-static double UV_MAX_cxux = ((double)UV_MAX);
-#endif
-
-/*
  * Offset for integer pack/unpack.
  *
  * On architectures where I16 and I32 aren't really 16 and 32 bits,
@@ -83,7 +83,7 @@ static double UV_MAX_cxux = ((double)UV_MAX);
 /* Avoid stack overflow due to pathological templates. 100 should be plenty. */
 #define MAX_SUB_TEMPLATE_LEVEL 100
 
-/* flags */
+/* flags (note that type modifiers can also be used as flags!) */
 #define FLAG_UNPACK_ONLY_ONE  0x10
 #define FLAG_UNPACK_DO_UTF8   0x08
 #define FLAG_SLASH            0x04
@@ -130,16 +130,21 @@ S_mul128(pTHX_ SV *sv, U8 m)
 #define ISUUCHAR(ch)    (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
 #endif
 
+/* type modifiers */
 #define TYPE_IS_SHRIEKING      0x100
 #define TYPE_IS_BIG_ENDIAN     0x200
 #define TYPE_IS_LITTLE_ENDIAN  0x400
 #define TYPE_ENDIANNESS_MASK   (TYPE_IS_BIG_ENDIAN|TYPE_IS_LITTLE_ENDIAN)
+#define TYPE_ENDIANNESS(t)     ((t) & TYPE_ENDIANNESS_MASK)
 #define TYPE_NO_ENDIANNESS(t)  ((t) & ~TYPE_ENDIANNESS_MASK)
+#define TYPE_MODIFIERS(t)      ((t) & ~0xFF)
 #define TYPE_NO_MODIFIERS(t)   ((t) & 0xFF)
 
+#define ENDIANNESS_ALLOWED_TYPES   "sSiIlLqQjJfFdDpP("
+
 #define DO_BO_UNPACK(var, type)                                               \
         STMT_START {                                                          \
-          switch (datumtype & TYPE_ENDIANNESS_MASK) {                         \
+          switch (TYPE_ENDIANNESS(datumtype)) {                               \
             case TYPE_IS_BIG_ENDIAN:    var = my_betoh ## type (var); break;  \
             case TYPE_IS_LITTLE_ENDIAN: var = my_letoh ## type (var); break;  \
             default: break;                                                   \
@@ -148,7 +153,7 @@ S_mul128(pTHX_ SV *sv, U8 m)
 
 #define DO_BO_PACK(var, type)                                                 \
         STMT_START {                                                          \
-          switch (datumtype & TYPE_ENDIANNESS_MASK) {                         \
+          switch (TYPE_ENDIANNESS(datumtype)) {                               \
             case TYPE_IS_BIG_ENDIAN:    var = my_htobe ## type (var); break;  \
             case TYPE_IS_LITTLE_ENDIAN: var = my_htole ## type (var); break;  \
             default: break;                                                   \
@@ -157,7 +162,7 @@ S_mul128(pTHX_ SV *sv, U8 m)
 
 #define DO_BO_UNPACK_PTR(var, type, pre_cast)                                 \
         STMT_START {                                                          \
-          switch (datumtype & TYPE_ENDIANNESS_MASK) {                         \
+          switch (TYPE_ENDIANNESS(datumtype)) {                               \
             case TYPE_IS_BIG_ENDIAN:                                          \
               var = (void *) my_betoh ## type ((pre_cast) var);               \
               break;                                                          \
@@ -171,7 +176,7 @@ S_mul128(pTHX_ SV *sv, U8 m)
 
 #define DO_BO_PACK_PTR(var, type, pre_cast)                                   \
         STMT_START {                                                          \
-          switch (datumtype & TYPE_ENDIANNESS_MASK) {                         \
+          switch (TYPE_ENDIANNESS(datumtype)) {                               \
             case TYPE_IS_BIG_ENDIAN:                                          \
               var = (void *) my_htobe ## type ((pre_cast) var);               \
               break;                                                          \
@@ -184,8 +189,8 @@ S_mul128(pTHX_ SV *sv, U8 m)
         } STMT_END
 
 #define BO_CANT_DOIT(action, type)                                            \
-         STMT_START {                                                         \
-           switch (datumtype & TYPE_ENDIANNESS_MASK) {                        \
+        STMT_START {                                                          \
+          switch (TYPE_ENDIANNESS(datumtype)) {                               \
              case TYPE_IS_BIG_ENDIAN:                                         \
                Perl_croak(aTHX_ "Can't %s big-endian %ss on this "            \
                                 "platform", #action, #type);                  \
@@ -214,7 +219,7 @@ S_mul128(pTHX_ SV *sv, U8 m)
     defined(my_htoben) && defined(my_betohn)
 # define DO_BO_UNPACK_N(var, type)                                            \
          STMT_START {                                                         \
-           switch (datumtype & TYPE_ENDIANNESS_MASK) {                        \
+           switch (TYPE_ENDIANNESS(datumtype)) {                              \
              case TYPE_IS_BIG_ENDIAN:    my_betohn(&var, sizeof(type)); break;\
              case TYPE_IS_LITTLE_ENDIAN: my_letohn(&var, sizeof(type)); break;\
              default: break;                                                  \
@@ -223,7 +228,7 @@ S_mul128(pTHX_ SV *sv, U8 m)
 
 # define DO_BO_PACK_N(var, type)                                              \
          STMT_START {                                                         \
-           switch (datumtype & TYPE_ENDIANNESS_MASK) {                        \
+           switch (TYPE_ENDIANNESS(datumtype)) {                              \
              case TYPE_IS_BIG_ENDIAN:    my_htoben(&var, sizeof(type)); break;\
              case TYPE_IS_LITTLE_ENDIAN: my_htolen(&var, sizeof(type)); break;\
              default: break;                                                  \
@@ -491,6 +496,7 @@ S_next_symbol(pTHX_ register tempsym_t* symptr )
     } else {
       /* We should have found a template code */ 
       I32 code = *patptr++ & 0xFF;
+      U32 inherited_modifiers = 0;
 
       if (code == ','){ /* grandfather in commas but with a warning */
        if (((symptr->flags & FLAG_COMMA) == 0) && ckWARN(WARN_UNPACK)){
@@ -514,6 +520,12 @@ S_next_symbol(pTHX_ register tempsym_t* symptr )
                      symptr->flags & FLAG_PACK ? "pack" : "unpack" );
       }
 
+      /* look for group modifiers to inherit */
+      if (TYPE_ENDIANNESS(symptr->flags)) {
+        if (strchr(ENDIANNESS_ALLOWED_TYPES, TYPE_NO_MODIFIERS(code)))
+          inherited_modifiers |= TYPE_ENDIANNESS(symptr->flags);
+      }
+
       /* look for modifiers */
       while (patptr < patend) {
         const char *allowed;
@@ -525,24 +537,32 @@ S_next_symbol(pTHX_ register tempsym_t* symptr )
             break;
           case '>':
             modifier = TYPE_IS_BIG_ENDIAN;
-            allowed = "sSiIlLqQjJfFdDpP";
+            allowed = ENDIANNESS_ALLOWED_TYPES;
             break;
           case '<':
             modifier = TYPE_IS_LITTLE_ENDIAN;
-            allowed = "sSiIlLqQjJfFdDpP";
+            allowed = ENDIANNESS_ALLOWED_TYPES;
             break;
           default:
             break;
         }
+
         if (modifier == 0)
           break;
+
         if (!strchr(allowed, TYPE_NO_MODIFIERS(code)))
           Perl_croak(aTHX_ "'%c' allowed only after types %s in %s", *patptr,
                      allowed, symptr->flags & FLAG_PACK ? "pack" : "unpack" );
-        if ((code | modifier) == (code | TYPE_IS_BIG_ENDIAN | TYPE_IS_LITTLE_ENDIAN))
+
+        if (TYPE_ENDIANNESS(code | modifier) == TYPE_ENDIANNESS_MASK)
           Perl_croak(aTHX_ "Can't use both '<' and '>' after type '%c' in %s",
                      (int) TYPE_NO_MODIFIERS(code),
                      symptr->flags & FLAG_PACK ? "pack" : "unpack" );
+        else if (TYPE_ENDIANNESS(code | modifier | inherited_modifiers) ==
+                 TYPE_ENDIANNESS_MASK)
+          Perl_croak(aTHX_ "Can't use '%c' in a group with different byte-order in %s",
+                     *patptr, symptr->flags & FLAG_PACK ? "pack" : "unpack" );
+
         if (ckWARN(WARN_UNPACK)) {
           if (code & modifier)
            Perl_warner(aTHX_ packWARN(WARN_UNPACK),
@@ -550,10 +570,14 @@ S_next_symbol(pTHX_ register tempsym_t* symptr )
                         *patptr, (int) TYPE_NO_MODIFIERS(code),
                         symptr->flags & FLAG_PACK ? "pack" : "unpack" );
         }
+
         code |= modifier;
         patptr++;
       }
 
+      /* inherit modifiers */
+      code |= inherited_modifiers;
+
       /* look for count and/or / */ 
       if (patptr < patend) {
        if (isDIGIT(*patptr)) {
@@ -597,11 +621,11 @@ S_next_symbol(pTHX_ register tempsym_t* symptr )
             if (patptr < patend)
              patptr++;
           } else {
-            if( *patptr == '/' ){ 
+            if (*patptr == '/') {
               symptr->flags |= FLAG_SLASH;
               patptr++;
-              ifpatptr < patend &&
-                  (isDIGIT(*patptr) || *patptr == '*' || *patptr == '[') )
+              if (patptr < patend &&
+                  (isDIGIT(*patptr) || *patptr == '*' || *patptr == '['))
                 Perl_croak(aTHX_ "'/' does not take a repeat count in %s",
                            symptr->flags & FLAG_PACK ? "pack" : "unpack" );
             }
@@ -750,6 +774,8 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
        {
            char *ss = s;               /* Move from register */
             tempsym_t savsym = *symptr;
+           U32 group_modifiers = TYPE_MODIFIERS(datumtype & ~symptr->flags);
+           symptr->flags |= group_modifiers;
             symptr->patend = savsym.grpend;
             symptr->level++;
            PUTBACK;
@@ -761,6 +787,7 @@ S_unpack_rec(pTHX_ register tempsym_t* symptr, register char *s, char *strbeg, c
            }
            SPAGAIN;
            s = ss;
+           symptr->flags &= ~group_modifiers;
             savsym.flags = symptr->flags;
             *symptr = savsym;
            break;
@@ -804,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 */
@@ -927,30 +953,24 @@ 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);
+           }
+           while (len-- > 0) {
+               aint = *s++;
+               if (aint >= 128)        /* fake up signed chars */
+                   aint -= 256;
+               if (!checksum) {
+                   sv = newSViv((IV)aint);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aint;
+               else
+                   cuv += aint;
            }
            break;
        case 'C':
@@ -975,8 +995,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);
+                   sv = newSViv((IV)auint);
                    PUSHs(sv_2mortal(sv));
                }
            }
@@ -990,32 +1009,25 @@ 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);
+           }
+           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) {
+                   sv = newSVuv((UV)auint);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auint;
+               else
+                   cuv += auint;
            }
            break;
        case 's' | TYPE_IS_SHRIEKING:
@@ -1023,30 +1035,24 @@ 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);
+           }
+           while (len-- > 0) {
+               COPYNN(s, &ashort, sizeof(short));
+               DO_BO_UNPACK(ashort, s);
+               s += sizeof(short);
+               if (!checksum) {
+                   sv = newSViv((IV)ashort);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ashort;
+               else
+                   cuv += ashort;
            }
            break;
 #else
@@ -1056,39 +1062,28 @@ 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);
+               s += SIZE16;
+               if (!checksum) {
+                   sv = newSViv((IV)ai16);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai16;
+               else
+                   cuv += ai16;
            }
            break;
        case 'S' | TYPE_IS_SHRIEKING:
@@ -1096,30 +1091,24 @@ 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);
+           }
+           while (len-- > 0) {
+               COPYNN(s, &aushort, sizeof(unsigned short));
+               DO_BO_UNPACK(aushort, s);
+               s += sizeof(unsigned short);
+               if (!checksum) {
+                   sv = newSViv((UV)aushort);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aushort;
+               else
+                   cuv += aushort;
            }
            break;
 #else
@@ -1131,46 +1120,32 @@ 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);
+               if (!checksum) {
+                   sv = newSViv((UV)au16);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)au16;
+               else
+                   cuv += au16;
            }
            break;
        case 'v' | TYPE_IS_SHRIEKING:
@@ -1178,44 +1153,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, &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);
+               if (!checksum) {
+                   sv = newSViv((IV)ai16);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai16;
+               else
+                   cuv += ai16;
            }
            break;
        case 'i':
@@ -1223,55 +1185,24 @@ 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);
+           }
+           while (len-- > 0) {
+               Copy(s, &aint, 1, int);
+               DO_BO_UNPACK(aint, i);
+               s += sizeof(int);
+               if (!checksum) {
+                   sv = newSViv((IV)aint);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aint;
+               else
+                   cuv += aint;
            }
            break;
        case 'I':
@@ -1279,121 +1210,84 @@ 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);
+           }
+           while (len-- > 0) {
+               Copy(s, &auint, 1, unsigned int);
+               DO_BO_UNPACK(auint, i);
+               s += sizeof(unsigned int);
+               if (!checksum) {
+                   sv = newSVuv((UV)auint);
                    PUSHs(sv_2mortal(sv));
                }
+               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);
+               s += IVSIZE;
+               if (!checksum) {
+                   sv = newSViv(aiv);
                    PUSHs(sv_2mortal(sv));
                }
+               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);
+               s += UVSIZE;
+               if (!checksum) {
+                   sv = newSVuv(auv);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auv;
+               else
+                   cuv += auv;
            }
            break;
        case 'l' | TYPE_IS_SHRIEKING:
@@ -1401,30 +1295,24 @@ 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);
+           }
+           while (len-- > 0) {
+               COPYNN(s, &along, sizeof(long));
+               DO_BO_UNPACK(along, l);
+               s += sizeof(long);
+               if (!checksum) {
+                   sv = newSViv((IV)along);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)along;
+               else
+                   cuv += along;
            }
            break;
 #else
@@ -1434,44 +1322,28 @@ 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) {
-#if LONGSIZE > SIZE32 && INTSIZE == SIZE32
-                   I32 along;
-#endif
-                   COPY32(s, &along);
-                   DO_BO_UNPACK(along, 32);
-#if LONGSIZE > SIZE32
-                   if (along > 2147483647)
-                       along -= 4294967296;
-#endif
-                   s += SIZE32;
-                   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) {
-#if LONGSIZE > SIZE32 && INTSIZE == SIZE32
-                   I32 along;
-#endif
-                   COPY32(s, &along);
-                   DO_BO_UNPACK(along, 32);
-#if LONGSIZE > SIZE32
-                   if (along > 2147483647)
-                       along -= 4294967296;
-#endif
-                   s += SIZE32;
-                   sv = NEWSV(42, 0);
-                   sv_setiv(sv, (IV)along);
+           }
+           while (len-- > 0) {
+               COPY32(s, &ai32);
+               DO_BO_UNPACK(ai32, 32);
+#if U32SIZE > SIZE32
+               if (ai32 > 2147483647)
+                   ai32 -= 4294967296;
+#endif
+               s += SIZE32;
+               if (!checksum) {
+                   sv = newSViv((IV)ai32);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai32;
+               else
+                   cuv += ai32;
            }
            break;
        case 'L' | TYPE_IS_SHRIEKING:
@@ -1479,30 +1351,24 @@ 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);
+           }
+           while (len-- > 0) {
+               COPYNN(s, &aulong, sizeof(unsigned long));
+               DO_BO_UNPACK(aulong, l);
+               s += sizeof(unsigned long);
+               if (!checksum) {
+                   sv = newSVuv((UV)aulong);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)aulong;
+               else
+                   cuv += aulong;
            }
            break;
 #else
@@ -1514,46 +1380,32 @@ 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) {
+                    sv = newSVuv((UV)au32);
+                    PUSHs(sv_2mortal(sv));
+                }
+                else if (checksum > bits_in_uv)
+                    cdouble += (NV)au32;
+                else
+                    cuv += au32;
            }
            break;
        case 'V' | TYPE_IS_SHRIEKING:
@@ -1561,44 +1413,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, &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);
+               if (!checksum) {
+                   sv = newSViv((IV)ai32);
                    PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)ai32;
+               else
+                   cuv += ai32;
            }
            break;
        case 'p':
@@ -1615,10 +1454,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':
@@ -1635,8 +1472,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);
+                       sv = newSVuv(auv);
                        PUSHs(sv_2mortal(sv));
                        len--;
                        auv = 0;
@@ -1677,85 +1513,73 @@ 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);
+           }
+           while (len-- > 0) {
+               if (s + sizeof(Quad_t) > strend) {
+                   /* Surely this should never happen? NWC  */
+                   aquad = 0;
+               }
+               else {
+                   Copy(s, &aquad, 1, Quad_t);
+                   DO_BO_UNPACK(aquad, 64);
+                   s += sizeof(Quad_t);
+               }
+               if (!checksum) {
                     if (aquad >= IV_MIN && aquad <= IV_MAX)
-                       sv_setiv(sv, (IV)aquad);
+                       sv = newSViv((IV)aquad);
                     else
-                        sv_setnv(sv, (NV)aquad);
+                        sv = newSVnv((NV)aquad);
                     PUSHs(sv_2mortal(sv));
                 }
-            }
+               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) {
+           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);
-                   if (checksum > bits_in_uv)
-                       cdouble += (NV)auquad;
+               }
+               if (!checksum) {
+                   if (auquad <= UV_MAX)
+                       sv = newSVuv((UV)auquad);
                    else
-                       cuv += auquad;
+                       sv = newSVnv((NV)auquad);
+                   PUSHs(sv_2mortal(sv));
                }
+               else if (checksum > bits_in_uv)
+                   cdouble += (NV)auquad;
+               else
+                   cuv += auquad;
            }
-            else {
-                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));
-                }
-            }
            break;
 #endif
        /* float and double added gnb@melba.bby.oz.au 22/11/89 */
@@ -1763,81 +1587,69 @@ 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);
+           }
+           while (len-- > 0) {
+               Copy(s, &afloat, 1, float);
+               DO_BO_UNPACK_N(afloat, float);
+               s += sizeof(float);
+               if (!checksum) {
+                   sv = newSVnv((NV)afloat);
                    PUSHs(sv_2mortal(sv));
                }
+               else {
+                   cdouble += afloat;
+               }
            }
            break;
        case 'd':
            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);
+           }
+           while (len-- > 0) {
+               Copy(s, &adouble, 1, double);
+               DO_BO_UNPACK_N(adouble, double);
+               s += sizeof(double);
+               if (!checksum) {
+                   sv = newSVnv((NV)adouble);
                    PUSHs(sv_2mortal(sv));
                }
+               else {
+                   cdouble += adouble;
+               }
            }
            break;
        case 'F':
            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);
+           }
+           while (len-- > 0) {
+               Copy(s, &anv, 1, NV);
+               DO_BO_UNPACK_N(anv, NV);
+               s += NVSIZE;
+               if (!checksum) {
+                   sv = newSVnv(anv);
                    PUSHs(sv_2mortal(sv));
                }
+               else {
+                   cdouble += anv;
+               }
            }
            break;
 #if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
@@ -1845,27 +1657,22 @@ 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);
+           }
+           while (len-- > 0) {
+               Copy(s, &aldouble, 1, long double);
+               DO_BO_UNPACK_N(aldouble, long double);
+               s += LONG_DOUBLESIZE;
+               if (!checksum) {
+                   sv = newSVnv((NV)aldouble);
                    PUSHs(sv_2mortal(sv));
                }
+               else {cdouble += aldouble;
+               }
            }
            break;
 #endif
@@ -1931,7 +1738,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))) ) {
@@ -1945,14 +1751,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;
@@ -2269,6 +2075,8 @@ S_pack_rec(pTHX_ SV *cat, register tempsym_t* symptr, register SV **beglist, SV
        case '(':
        {
             tempsym_t savsym = *symptr;
+           U32 group_modifiers = TYPE_MODIFIERS(datumtype & ~symptr->flags);
+           symptr->flags |= group_modifiers;
             symptr->patend = savsym.grpend;
             symptr->level++;
            while (len--) {
@@ -2277,6 +2085,7 @@ S_pack_rec(pTHX_ SV *cat, register tempsym_t* symptr, register SV **beglist, SV
                if (savsym.howlen == e_star && beglist == endlist)
                    break;              /* No way to continue */
            }
+           symptr->flags &= ~group_modifiers;
             lookahead.flags = symptr->flags;
             *symptr = savsym;
            break;
@@ -2965,3 +2774,12 @@ PP(pp_pack)
     RETURN;
 }
 
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vim: expandtab shiftwidth=4:
+*/