integer values _and_ if Perl has been compiled to support those.
Causes a fatal error otherwise.)
+ j A signed integer value (a Perl internal integer, IV).
+ J An unsigned integer value (a Perl internal unsigned integer, UV).
+
f A single-precision float in the native format.
d A double-precision float in the native format.
+ F A floating point value in the native native format
+ (a Perl internal floating point value, NV).
+ D A long double-precision float in the native format.
+ (Long doubles are available only if your system supports long
+ double values _and_ if Perl has been compiled to support those.
+ Causes a fatal error otherwise.)
+
p A pointer to a null-terminated string.
P A pointer to a structure (fixed-length string).
the packed structure contains a byte count followed by the string itself.
You write I<length-item>C</>I<string-item>.
-The I<length-item> can be any C<pack> template letter,
-and describes how the length value is packed.
-The ones likely to be of most use are integer-packing ones like
-C<n> (for Java strings), C<w> (for ASN.1 or SNMP)
-and C<N> (for Sun XDR).
+The I<length-item> can be any C<pack> template letter, and describes
+how the length value is packed. The ones likely to be of most use are
+integer-packing ones like C<n> (for Java strings), C<w> (for ASN.1 or
+SNMP) and C<N> (for Sun XDR).
The I<string-item> must, at present, be C<"A*">, C<"a*"> or C<"Z*">.
For C<unpack> the length of the string is obtained from the I<length-item>,
=item *
-The integer formats C<s>, C<S>, C<i>, C<I>, C<l>, and C<L>
+The integer formats C<s>, C<S>, C<i>, C<I>, C<l>, C<L>, C<j>, and C<J>
are inherently non-portable between processors and operating systems
because they obey the native byteorder and endianness. For example a
4-byte integer 0x12345678 (305419896 decimal) would be ordered natively
This is possible to do, but would be pretty messy to implement, as it
would rely on even more sed hackery in F<perly.fixer>.
-=head2 pack for IV, UVs, NVs, and long doubles
-
-j, J, g, G?
-
=head2 bitfields in pack
=head2 Cross compilation
case 'I':
size = sizeof(unsigned int);
break;
+ case 'j':
+ size = IVSIZE;
+ break;
+ case 'J':
+ size = UVSIZE;
+ break;
case 'l':
#if LONGSIZE == SIZE32
size = SIZE32;
break;
#endif
case 'f':
- case 'F':
size = sizeof(float);
break;
case 'd':
- case 'D':
size = sizeof(double);
break;
+ case 'F':
+ size = NVSIZE;
+ break;
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+ case 'D':
+ size = LONG_DOUBLESIZE;
+ break;
+#endif
}
total += len * size;
}
float afloat;
double adouble;
I32 checksum = 0;
- UV culong = 0;
+ UV cuv = 0;
NV cdouble = 0.0;
- const int bits_in_uv = 8 * sizeof(culong);
+ const int bits_in_uv = 8 * sizeof(cuv);
int commas = 0;
int star; /* 1 if count is *, -1 if no count given, -2 for / */
#ifdef PERL_NATINT_PACK
int natint; /* native integer */
int unatint; /* unsigned native integer */
#endif
+ IV aiv;
+ UV auv;
+ NV anv;
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+ long double aldouble;
+#endif
bool do_utf8 = flags & UNPACK_DO_UTF8;
while ((pat = next_symbol(pat, patend)) < patend) {
if (len == 1 && pat[-1] != '1' && pat[-1] != ']')
len = 16; /* len is not specified */
checksum = len;
- culong = 0;
+ cuv = 0;
cdouble = 0;
continue;
break;
}
}
while (len >= 8) {
- culong += PL_bitcount[*(unsigned char*)s++];
+ cuv += PL_bitcount[*(unsigned char*)s++];
len -= 8;
}
if (len) {
bits = *s;
if (datumtype == 'b') {
while (len-- > 0) {
- if (bits & 1) culong++;
+ if (bits & 1) cuv++;
bits >>= 1;
}
}
else {
while (len-- > 0) {
- if (bits & 128) culong++;
+ if (bits & 128) cuv++;
bits <<= 1;
}
}
if (checksum > bits_in_uv)
cdouble += (NV)aint;
else
- culong += aint;
+ cuv += aint;
}
}
else {
uchar_checksum:
while (len-- > 0) {
auint = *s++ & 255;
- culong += auint;
+ cuv += auint;
}
}
else {
if (checksum > bits_in_uv)
cdouble += (NV)auint;
else
- culong += auint;
+ cuv += auint;
}
}
else {
if (checksum > bits_in_uv)
cdouble += (NV)ashort;
else
- culong += ashort;
+ cuv += ashort;
}
}
if (checksum > bits_in_uv)
cdouble += (NV)ashort;
else
- culong += ashort;
+ cuv += ashort;
}
}
}
if (checksum > bits_in_uv)
cdouble += (NV)aushort;
else
- culong += aushort;
+ cuv += aushort;
}
}
else
if (checksum > bits_in_uv)
cdouble += (NV)aushort;
else
- culong += aushort;
+ cuv += aushort;
}
}
}
if (checksum > bits_in_uv)
cdouble += (NV)aint;
else
- culong += aint;
+ cuv += aint;
}
}
else {
if (checksum > bits_in_uv)
cdouble += (NV)auint;
else
- culong += auint;
+ cuv += auint;
}
}
else {
}
}
break;
+ case 'j':
+ along = (strend - s) / IVSIZE;
+ if (len > along)
+ len = along;
+ if (checksum) {
+ while (len-- > 0) {
+ Copy(s, &aiv, 1, IV);
+ s += IVSIZE;
+ if (checksum > bits_in_uv)
+ cdouble += (NV)aiv;
+ else
+ cuv += aiv;
+ }
+ }
+ else {
+ EXTEND(SP, len);
+ EXTEND_MORTAL(len);
+ while (len-- > 0) {
+ Copy(s, &aiv, 1, IV);
+ s += IVSIZE;
+ sv = NEWSV(40, 0);
+ sv_setiv(sv, aiv);
+ PUSHs(sv_2mortal(sv));
+ }
+ }
+ break;
+ case 'J':
+ along = (strend - s) / UVSIZE;
+ if (len > along)
+ len = along;
+ if (checksum) {
+ while (len-- > 0) {
+ Copy(s, &auv, 1, UV);
+ s += UVSIZE;
+ if (checksum > bits_in_uv)
+ cdouble += (NV)auv;
+ else
+ cuv += auv;
+ }
+ }
+ else {
+ EXTEND(SP, len);
+ EXTEND_MORTAL(len);
+ while (len-- > 0) {
+ Copy(s, &auv, 1, UV);
+ s += UVSIZE;
+ sv = NEWSV(41, 0);
+ sv_setuv(sv, auv);
+ PUSHs(sv_2mortal(sv));
+ }
+ }
+ break;
case 'l':
#if LONGSIZE == SIZE32
along = (strend - s) / SIZE32;
if (checksum > bits_in_uv)
cdouble += (NV)along;
else
- culong += along;
+ cuv += along;
}
}
else
if (checksum > bits_in_uv)
cdouble += (NV)along;
else
- culong += along;
+ cuv += along;
}
}
}
if (checksum > bits_in_uv)
cdouble += (NV)aulong;
else
- culong += aulong;
+ cuv += aulong;
}
}
else
if (checksum > bits_in_uv)
cdouble += (NV)aulong;
else
- culong += aulong;
+ cuv += aulong;
}
}
}
if (checksum > bits_in_uv)
cdouble += (NV)aquad;
else
- culong += aquad;
+ cuv += aquad;
}
}
else {
if (s + sizeof(Quad_t) > strend)
aquad = 0;
else {
- Copy(s, &aquad, 1, Quad_t);
- s += sizeof(Quad_t);
+ Copy(s, &aquad, 1, Quad_t);
+ s += sizeof(Quad_t);
}
sv = NEWSV(42, 0);
if (aquad >= IV_MIN && aquad <= IV_MAX)
- sv_setiv(sv, (IV)aquad);
+ sv_setiv(sv, (IV)aquad);
else
sv_setnv(sv, (NV)aquad);
PUSHs(sv_2mortal(sv));
if (checksum > bits_in_uv)
cdouble += (NV)auquad;
else
- culong += auquad;
+ cuv += auquad;
}
}
else {
#endif
/* float and double added gnb@melba.bby.oz.au 22/11/89 */
case 'f':
- case 'F':
along = (strend - s) / sizeof(float);
if (len > along)
len = along;
}
break;
case 'd':
- case 'D':
along = (strend - s) / sizeof(double);
if (len > along)
len = along;
}
}
break;
+ case 'F':
+ along = (strend - s) / NVSIZE;
+ if (len > along)
+ len = along;
+ if (checksum) {
+ while (len-- > 0) {
+ Copy(s, &anv, 1, NV);
+ s += NVSIZE;
+ cdouble += anv;
+ }
+ }
+ else {
+ EXTEND(SP, len);
+ EXTEND_MORTAL(len);
+ while (len-- > 0) {
+ Copy(s, &anv, 1, NV);
+ s += NVSIZE;
+ sv = NEWSV(48, 0);
+ sv_setnv(sv, anv);
+ PUSHs(sv_2mortal(sv));
+ }
+ }
+ break;
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+ case 'D':
+ along = (strend - s) / LONG_DOUBLESIZE;
+ if (len > along)
+ len = along;
+ if (checksum) {
+ while (len-- > 0) {
+ Copy(s, &aldouble, 1, long double);
+ s += LONG_DOUBLESIZE;
+ cdouble += aldouble;
+ }
+ }
+ else {
+ EXTEND(SP, len);
+ EXTEND_MORTAL(len);
+ while (len-- > 0) {
+ Copy(s, &aldouble, 1, long double);
+ s += LONG_DOUBLESIZE;
+ sv = NEWSV(48, 0);
+ sv_setnv(sv, (NV)aldouble);
+ PUSHs(sv_2mortal(sv));
+ }
+ }
+ break;
+#endif
case 'u':
/* MKS:
* Initialise the decode mapping. By using a table driven
if (checksum) {
sv = NEWSV(42, 0);
if (strchr("fFdD", datumtype) ||
- (checksum > bits_in_uv && strchr("csSiIlLnNUvVqQ", datumtype)) ) {
+ (checksum > bits_in_uv &&
+ strchr("csSiIlLnNUvVqQjJ", datumtype)) ) {
NV trouble;
adouble = (NV) (1 << (checksum & 15));
else {
if (checksum < bits_in_uv) {
UV mask = ((UV)1 << checksum) - 1;
- culong &= mask;
+
+ cuv &= mask;
}
- sv_setuv(sv, (UV)culong);
+ sv_setuv(sv, cuv);
}
XPUSHs(sv_2mortal(sv));
checksum = 0;
unsigned int auint;
I32 along;
U32 aulong;
+ IV aiv;
+ UV auv;
+ NV anv;
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+ long double aldouble;
+#endif
#ifdef HAS_QUAD
Quad_t aquad;
Uquad_t auquad;
break;
/* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
case 'f':
- case 'F':
while (len-- > 0) {
fromstr = NEXTFROM;
afloat = (float)SvNV(fromstr);
}
break;
case 'd':
- case 'D':
while (len-- > 0) {
fromstr = NEXTFROM;
adouble = (double)SvNV(fromstr);
sv_catpvn(cat, (char *)&adouble, sizeof (double));
}
break;
+ case 'F':
+ while (len-- > 0) {
+ fromstr = NEXTFROM;
+ anv = SvNV(fromstr);
+ sv_catpvn(cat, (char *)&anv, NVSIZE);
+ }
+ break;
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+ case 'D':
+ while (len-- > 0) {
+ fromstr = NEXTFROM;
+ aldouble = (long double)SvNV(fromstr);
+ sv_catpvn(cat, (char *)&aldouble, LONG_DOUBLESIZE);
+ }
+ break;
+#endif
case 'n':
while (len-- > 0) {
fromstr = NEXTFROM;
sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
}
break;
+ case 'j':
+ while (len-- > 0) {
+ fromstr = NEXTFROM;
+ aiv = SvIV(fromstr);
+ sv_catpvn(cat, (char*)&aiv, IVSIZE);
+ }
+ break;
+ case 'J':
+ while (len-- > 0) {
+ fromstr = NEXTFROM;
+ auv = SvUV(fromstr);
+ sv_catpvn(cat, (char*)&auv, UVSIZE);
+ }
+ break;
case 'w':
while (len-- > 0) {
fromstr = NEXTFROM;
require './test.pl';
}
-plan tests => 5179;
+plan tests => 5619;
use strict;
use warnings;
}
{ # Repeat count [SUBEXPR]
- my @codes = qw( x A Z a c C B b H h s v n S i I l V N L p P f F d D
- s! S! i! I! l! L! );
+ my @codes = qw( x A Z a c C B b H h s v n S i I l V N L p P f F d
+ s! S! i! I! l! L! j J);
+ my $G;
if (eval { pack 'q', 1 } ) {
push @codes, qw(q Q);
} else {
push @codes, qw(c C); # Keep the count the same
}
+ if (eval { pack 'D', 1 } ) {
+ push @codes, 'D';
+ } else {
+ push @codes, 'd'; # Keep the count the same
+ }
my %val;
@val{@codes} = map { / [Xx] (?{ undef })
| c (?{ 114 })
| [Bb] (?{ '101' })
| [Hh] (?{ 'b8' })
- | [svnSiIlVNLqQ] (?{ 10111 })
+ | [svnSiIlVNLqQjJ] (?{ 10111 })
| [FfDd] (?{ 1.36514538e67 })
| [pP] (?{ "try this buffer" })
/x; $^R } @codes;
is(scalar @b, scalar @a);
is("@b", "@a");
}
+
+is(length(pack("j", 0)), $Config{ivsize});
+is(length(pack("J", 0)), $Config{uvsize});
+is(length(pack("F", 0)), $Config{nvsize});
+
+numbers ('j', -2147483648, -1, 0, 1, 2147483647);
+numbers ('J', 0, 1, 2147483647, 2147483648, 4294967295);
+numbers ('F', -(2**34), -1, 0, 1, 2**34);
+SKIP: {
+ my $t = eval { unpack("D*", pack("D", 12.34)) };
+
+ skip "Long doubles not in use", 56 if $@ =~ /Invalid type in pack/;
+
+ is(length(pack("D", 0)), $Config{longdblsize});
+ numbers ('D', -(2**34), -1, 0, 1, 2**34);
+}
+