/* pp_pack.c
*
- * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
+ * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+ * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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.
* wooden spoon, a short two-pronged fork and some skewers were stowed; and
* hidden at the bottom of the pack in a flat wooden box a dwindling treasure,
* some salt.
+ *
+ * [p.653 of _The Lord of the Rings_, IV/iv: "Of Herbs and Stewed Rabbit"]
*/
/* This file contains pp ("push/pop") functions that
* other pp*.c files for the rest of the pp_ functions.
*/
-
#include "EXTERN.h"
#define PERL_IN_PP_PACK_C
#include "perl.h"
(symptr)->grpend = NULL; \
(symptr)->code = 0; \
(symptr)->length = 0; \
- (symptr)->howlen = 0; \
+ (symptr)->howlen = e_no_len; \
(symptr)->level = 0; \
(symptr)->flags = (f); \
(symptr)->strbeg = 0; \
(symptr)->previous = NULL; \
} STMT_END
-#if PERL_VERSION >= 9
-# define PERL_PACK_CAN_BYTEORDER
-# define PERL_PACK_CAN_SHRIEKSIGN
+typedef union {
+ NV nv;
+ U8 bytes[sizeof(NV)];
+} NV_bytes;
+
+#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
+typedef union {
+ long double ld;
+ U8 bytes[sizeof(long double)];
+} ld_bytes;
#endif
#ifndef CHAR_BIT
# define OFF32(p) ((char *) (p))
#endif
-/* Only to be used inside a loop (see the break) */
-#define SHIFT16(utf8, s, strend, p, datumtype) STMT_START { \
- if (utf8) { \
- if (!uni_to_bytes(aTHX_ &(s), strend, OFF16(p), SIZE16, datumtype)) break; \
- } else { \
- Copy(s, OFF16(p), SIZE16, char); \
- (s) += SIZE16; \
- } \
-} STMT_END
-
-/* Only to be used inside a loop (see the break) */
-#define SHIFT32(utf8, s, strend, p, datumtype) STMT_START { \
- if (utf8) { \
- if (!uni_to_bytes(aTHX_ &(s), strend, OFF32(p), SIZE32, datumtype)) break; \
- } else { \
- Copy(s, OFF32(p), SIZE32, char); \
- (s) += SIZE32; \
- } \
-} STMT_END
+#define PUSH16(utf8, cur, p, needs_swap) \
+ PUSH_BYTES(utf8, cur, OFF16(p), SIZE16, needs_swap)
+#define PUSH32(utf8, cur, p, needs_swap) \
+ PUSH_BYTES(utf8, cur, OFF32(p), SIZE32, needs_swap)
-#define PUSH16(utf8, cur, p) PUSH_BYTES(utf8, cur, OFF16(p), SIZE16)
-#define PUSH32(utf8, cur, p) PUSH_BYTES(utf8, cur, OFF32(p), SIZE32)
+#if BYTEORDER == 0x4321 || BYTEORDER == 0x87654321 /* big-endian */
+# define NEEDS_SWAP(d) (TYPE_ENDIANNESS(d) == TYPE_IS_LITTLE_ENDIAN)
+#elif BYTEORDER == 0x1234 || BYTEORDER == 0x12345678 /* little-endian */
+# define NEEDS_SWAP(d) (TYPE_ENDIANNESS(d) == TYPE_IS_BIG_ENDIAN)
+#else
+# error "Unsupported byteorder"
+ /* Need to add code here to re-instate mixed endian support.
+ NEEDS_SWAP would need to hold a flag indicating which action to
+ take, and S_reverse_copy and the code in uni_to_bytes would need
+ logic adding to deal with any mixed-endian transformations needed.
+ */
+#endif
/* Only to be used inside a loop (see the break) */
-#define SHIFT_VAR(utf8, s, strend, var, datumtype) \
+#define SHIFT_BYTES(utf8, s, strend, buf, len, datumtype, needs_swap) \
STMT_START { \
- if (utf8) { \
+ if (UNLIKELY(utf8)) { \
if (!uni_to_bytes(aTHX_ &s, strend, \
- (char *) &var, sizeof(var), datumtype)) break;\
+ (char *) (buf), len, datumtype)) break; \
} else { \
- Copy(s, (char *) &var, sizeof(var), char); \
- s += sizeof(var); \
+ if (UNLIKELY(needs_swap)) \
+ S_reverse_copy(s, (char *) (buf), len); \
+ else \
+ Copy(s, (char *) (buf), len, char); \
+ s += len; \
} \
} STMT_END
-#define PUSH_VAR(utf8, aptr, var) \
- PUSH_BYTES(utf8, aptr, &(var), sizeof(var))
+#define SHIFT16(utf8, s, strend, p, datumtype, needs_swap) \
+ SHIFT_BYTES(utf8, s, strend, OFF16(p), SIZE16, datumtype, needs_swap)
+
+#define SHIFT32(utf8, s, strend, p, datumtype, needs_swap) \
+ SHIFT_BYTES(utf8, s, strend, OFF32(p), SIZE32, datumtype, needs_swap)
+
+#define SHIFT_VAR(utf8, s, strend, var, datumtype, needs_swap) \
+ SHIFT_BYTES(utf8, s, strend, &(var), sizeof(var), datumtype, needs_swap)
+
+#define PUSH_VAR(utf8, aptr, var, needs_swap) \
+ PUSH_BYTES(utf8, aptr, &(var), sizeof(var), needs_swap)
/* Avoid stack overflow due to pathological templates. 100 should be plenty. */
#define MAX_SUB_TEMPLATE_LEVEL 100
char *s = SvPV(sv, len);
char *t;
+ PERL_ARGS_ASSERT_MUL128;
+
if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
SV * const tmpNew = newSVpvs("0000000000");
#define TYPE_MODIFIERS(t) ((t) & ~0xFF)
#define TYPE_NO_MODIFIERS(t) ((t) & 0xFF)
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
-# define SHRIEKING_ALLOWED_TYPES "sSiIlLxXnNvV@."
-#else
-# define SHRIEKING_ALLOWED_TYPES "sSiIlLxX"
-#endif
-
-#ifndef PERL_PACK_CAN_BYTEORDER
-/* Put "can't" first because it is shorter */
-# define TYPE_ENDIANNESS(t) 0
-# define TYPE_NO_ENDIANNESS(t) (t)
-
-# define ENDIANNESS_ALLOWED_TYPES ""
-
-# define DO_BO_UNPACK(var, type)
-# define DO_BO_PACK(var, type)
-# define DO_BO_UNPACK_PTR(var, type, pre_cast, post_cast)
-# define DO_BO_PACK_PTR(var, type, pre_cast, post_cast)
-# define DO_BO_UNPACK_N(var, type)
-# define DO_BO_PACK_N(var, type)
-# define DO_BO_UNPACK_P(var)
-# define DO_BO_PACK_P(var)
-# define DO_BO_UNPACK_PC(var)
-# define DO_BO_PACK_PC(var)
-
-#else /* PERL_PACK_CAN_BYTEORDER */
-
# define TYPE_ENDIANNESS(t) ((t) & TYPE_ENDIANNESS_MASK)
# define TYPE_NO_ENDIANNESS(t) ((t) & ~TYPE_ENDIANNESS_MASK)
# define ENDIANNESS_ALLOWED_TYPES "sSiIlLqQjJfFdDpP("
-# define DO_BO_UNPACK(var, type) \
- STMT_START { \
- 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; \
- } \
- } STMT_END
-
-# define DO_BO_PACK(var, type) \
- STMT_START { \
- 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; \
- } \
- } STMT_END
-
-# define DO_BO_UNPACK_PTR(var, type, pre_cast, post_cast) \
- STMT_START { \
- switch (TYPE_ENDIANNESS(datumtype)) { \
- case TYPE_IS_BIG_ENDIAN: \
- var = (post_cast*) my_betoh ## type ((pre_cast) var); \
- break; \
- case TYPE_IS_LITTLE_ENDIAN: \
- var = (post_cast *) my_letoh ## type ((pre_cast) var); \
- break; \
- default: \
- break; \
- } \
- } STMT_END
-
-# define DO_BO_PACK_PTR(var, type, pre_cast, post_cast) \
- STMT_START { \
- switch (TYPE_ENDIANNESS(datumtype)) { \
- case TYPE_IS_BIG_ENDIAN: \
- var = (post_cast *) my_htobe ## type ((pre_cast) var); \
- break; \
- case TYPE_IS_LITTLE_ENDIAN: \
- var = (post_cast *) my_htole ## type ((pre_cast) var); \
- break; \
- default: \
- break; \
- } \
- } STMT_END
-
-# define BO_CANT_DOIT(action, type) \
- 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); \
- break; \
- case TYPE_IS_LITTLE_ENDIAN: \
- Perl_croak(aTHX_ "Can't %s little-endian %ss on this " \
- "platform", #action, #type); \
- break; \
- default: \
- break; \
- } \
- } STMT_END
-
-# if PTRSIZE == INTSIZE
-# define DO_BO_UNPACK_P(var) DO_BO_UNPACK_PTR(var, i, int, void)
-# define DO_BO_PACK_P(var) DO_BO_PACK_PTR(var, i, int, void)
-# define DO_BO_UNPACK_PC(var) DO_BO_UNPACK_PTR(var, i, int, char)
-# define DO_BO_PACK_PC(var) DO_BO_PACK_PTR(var, i, int, char)
-# elif PTRSIZE == LONGSIZE
-# define DO_BO_UNPACK_P(var) DO_BO_UNPACK_PTR(var, l, long, void)
-# define DO_BO_PACK_P(var) DO_BO_PACK_PTR(var, l, long, void)
-# define DO_BO_UNPACK_PC(var) DO_BO_UNPACK_PTR(var, l, long, char)
-# define DO_BO_PACK_PC(var) DO_BO_PACK_PTR(var, l, long, char)
-# elif PTRSIZE == IVSIZE
-# define DO_BO_UNPACK_P(var) DO_BO_UNPACK_PTR(var, l, IV, void)
-# define DO_BO_PACK_P(var) DO_BO_PACK_PTR(var, l, IV, void)
-# define DO_BO_UNPACK_PC(var) DO_BO_UNPACK_PTR(var, l, IV, char)
-# define DO_BO_PACK_PC(var) DO_BO_PACK_PTR(var, l, IV, char)
-# else
-# define DO_BO_UNPACK_P(var) BO_CANT_DOIT(unpack, pointer)
-# define DO_BO_PACK_P(var) BO_CANT_DOIT(pack, pointer)
-# define DO_BO_UNPACK_PC(var) BO_CANT_DOIT(unpack, pointer)
-# define DO_BO_PACK_PC(var) BO_CANT_DOIT(pack, pointer)
-# endif
-
-# if defined(my_htolen) && defined(my_letohn) && \
- defined(my_htoben) && defined(my_betohn)
-# define DO_BO_UNPACK_N(var, type) \
- STMT_START { \
- 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; \
- } \
- } STMT_END
-
-# define DO_BO_PACK_N(var, type) \
- STMT_START { \
- 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; \
- } \
- } STMT_END
-# else
-# define DO_BO_UNPACK_N(var, type) BO_CANT_DOIT(unpack, type)
-# define DO_BO_PACK_N(var, type) BO_CANT_DOIT(pack, type)
-# endif
-
-#endif /* PERL_PACK_CAN_BYTEORDER */
-
#define PACK_SIZE_CANNOT_CSUM 0x80
#define PACK_SIZE_UNPREDICTABLE 0x40 /* Not a fixed size element */
#define PACK_SIZE_MASK 0x3F
-/* These tables are regenerated by genpacksizetables.pl (and then hand pasted
- in). You're unlikely ever to need to regenerate them. */
-
-#if TYPE_IS_SHRIEKING != 0x100
- ++++shriek offset should be 256
-#endif
+#include "packsizetables.c"
-typedef U8 packprops_t;
-#if 'J'-'I' == 1
-/* ASCII */
-STATIC const packprops_t packprops[512] = {
- /* normal */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,
- /* C */ sizeof(unsigned char) | PACK_SIZE_UNPREDICTABLE,
-#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
- /* D */ LONG_DOUBLESIZE,
-#else
- 0,
-#endif
- 0,
- /* F */ NVSIZE,
- 0, 0,
- /* I */ sizeof(unsigned int),
- /* J */ UVSIZE,
- 0,
- /* L */ SIZE32,
- 0,
- /* N */ SIZE32,
- 0, 0,
-#if defined(HAS_QUAD)
- /* Q */ sizeof(Uquad_t),
-#else
- 0,
-#endif
- 0,
- /* S */ SIZE16,
- 0,
- /* U */ sizeof(char) | PACK_SIZE_UNPREDICTABLE,
- /* V */ SIZE32,
- /* W */ sizeof(unsigned char) | PACK_SIZE_UNPREDICTABLE,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* c */ sizeof(char),
- /* d */ sizeof(double),
- 0,
- /* f */ sizeof(float),
- 0, 0,
- /* i */ sizeof(int),
- /* j */ IVSIZE,
- 0,
- /* l */ SIZE32,
- 0,
- /* n */ SIZE16,
- 0,
- /* p */ sizeof(char *) | PACK_SIZE_CANNOT_CSUM,
-#if defined(HAS_QUAD)
- /* q */ sizeof(Quad_t),
-#else
- 0,
-#endif
- 0,
- /* s */ SIZE16,
- 0, 0,
- /* v */ SIZE16,
- /* w */ sizeof(char) | PACK_SIZE_UNPREDICTABLE | PACK_SIZE_CANNOT_CSUM,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0,
- /* shrieking */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* I */ sizeof(unsigned int),
- 0, 0,
- /* L */ sizeof(unsigned long),
- 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* N */ SIZE32,
-#else
- 0,
-#endif
- 0, 0, 0, 0,
- /* S */ sizeof(unsigned short),
- 0, 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* V */ SIZE32,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0,
- /* i */ sizeof(int),
- 0, 0,
- /* l */ sizeof(long),
- 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* n */ SIZE16,
-#else
- 0,
-#endif
- 0, 0, 0, 0,
- /* s */ sizeof(short),
- 0, 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* v */ SIZE16,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-#else
-/* EBCDIC (or bust) */
-STATIC const packprops_t packprops[512] = {
- /* normal */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,
- /* c */ sizeof(char),
- /* d */ sizeof(double),
- 0,
- /* f */ sizeof(float),
- 0, 0,
- /* i */ sizeof(int),
- 0, 0, 0, 0, 0, 0, 0,
- /* j */ IVSIZE,
- 0,
- /* l */ SIZE32,
- 0,
- /* n */ SIZE16,
- 0,
- /* p */ sizeof(char *) | PACK_SIZE_CANNOT_CSUM,
-#if defined(HAS_QUAD)
- /* q */ sizeof(Quad_t),
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* s */ SIZE16,
- 0, 0,
- /* v */ SIZE16,
- /* w */ sizeof(char) | PACK_SIZE_UNPREDICTABLE | PACK_SIZE_CANNOT_CSUM,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* C */ sizeof(unsigned char) | PACK_SIZE_UNPREDICTABLE,
-#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
- /* D */ LONG_DOUBLESIZE,
-#else
- 0,
-#endif
- 0,
- /* F */ NVSIZE,
- 0, 0,
- /* I */ sizeof(unsigned int),
- 0, 0, 0, 0, 0, 0, 0,
- /* J */ UVSIZE,
- 0,
- /* L */ SIZE32,
- 0,
- /* N */ SIZE32,
- 0, 0,
-#if defined(HAS_QUAD)
- /* Q */ sizeof(Uquad_t),
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* S */ SIZE16,
- 0,
- /* U */ sizeof(char) | PACK_SIZE_UNPREDICTABLE,
- /* V */ SIZE32,
- /* W */ sizeof(unsigned char) | PACK_SIZE_UNPREDICTABLE,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* shrieking */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* i */ sizeof(int),
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* l */ sizeof(long),
- 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* n */ SIZE16,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* s */ sizeof(short),
- 0, 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* v */ SIZE16,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0,
- /* I */ sizeof(unsigned int),
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* L */ sizeof(unsigned long),
- 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* N */ SIZE32,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- /* S */ sizeof(unsigned short),
- 0, 0,
-#if defined(PERL_PACK_CAN_SHRIEKSIGN)
- /* V */ SIZE32,
-#else
- 0,
-#endif
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-#endif
+static void
+S_reverse_copy(const char *src, char *dest, STRLEN len)
+{
+ dest += len;
+ while (len--)
+ *--dest = *src++;
+}
STATIC U8
uni_to_byte(pTHX_ const char **s, const char *end, I32 datumtype)
STRLEN retlen;
UV val = utf8n_to_uvchr((U8 *) *s, end-*s, &retlen,
ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
- /* We try to process malformed UTF-8 as much as possible (preferrably with
+ /* We try to process malformed UTF-8 as much as possible (preferably with
warnings), but these two mean we make no progress in the string and
might enter an infinite loop */
if (retlen == (STRLEN) -1 || retlen == 0)
Perl_croak(aTHX_ "Malformed UTF-8 string in '%c' format in unpack",
(int) TYPE_NO_MODIFIERS(datumtype));
if (val >= 0x100) {
- if (ckWARN(WARN_UNPACK))
- Perl_warner(aTHX_ packWARN(WARN_UNPACK),
- "Character in '%c' format wrapped in unpack",
- (int) TYPE_NO_MODIFIERS(datumtype));
+ Perl_ck_warner(aTHX_ packWARN(WARN_UNPACK),
+ "Character in '%c' format wrapped in unpack",
+ (int) TYPE_NO_MODIFIERS(datumtype));
val &= 0xff;
}
*s += retlen;
int bad = 0;
const U32 flags = ckWARN(WARN_UTF8) ?
UTF8_CHECK_ONLY : (UTF8_CHECK_ONLY | UTF8_ALLOW_ANY);
+ const bool needs_swap = NEEDS_SWAP(datumtype);
+
+ if (UNLIKELY(needs_swap))
+ buf += buf_len;
+
for (;buf_len > 0; buf_len--) {
if (from >= end) return FALSE;
val = utf8n_to_uvchr((U8 *) from, end-from, &retlen, flags);
bad |= 2;
val &= 0xff;
}
- *(U8 *)buf++ = (U8)val;
+ if (UNLIKELY(needs_swap))
+ *(U8 *)--buf = (U8)val;
+ else
+ *(U8 *)buf++ = (U8)val;
}
/* We have enough characters for the buffer. Did we have problems ? */
if (bad) {
const int flags = ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY;
for (ptr = *s; ptr < from; ptr += UTF8SKIP(ptr)) {
if (ptr >= end) break;
- utf8n_to_uvuni((U8 *) ptr, end-ptr, &retlen, flags);
+ utf8n_to_uvchr((U8 *) ptr, end-ptr, &retlen, flags);
}
if (from > end) from = end;
}
- if ((bad & 2) && ckWARN(WARN_UNPACK))
- Perl_warner(aTHX_ packWARN(datumtype & TYPE_IS_PACK ?
+ if ((bad & 2))
+ Perl_ck_warner(aTHX_ packWARN(datumtype & TYPE_IS_PACK ?
WARN_PACK : WARN_UNPACK),
- "Character(s) in '%c' format wrapped in %s",
- (int) TYPE_NO_MODIFIERS(datumtype),
- datumtype & TYPE_IS_PACK ? "pack" : "unpack");
+ "Character(s) in '%c' format wrapped in %s",
+ (int) TYPE_NO_MODIFIERS(datumtype),
+ datumtype & TYPE_IS_PACK ? "pack" : "unpack");
}
*s = from;
return TRUE;
STATIC bool
next_uni_uu(pTHX_ const char **s, const char *end, I32 *out)
{
- dVAR;
STRLEN retlen;
const UV val = utf8n_to_uvchr((U8 *) *s, end-*s, &retlen, UTF8_CHECK_ONLY);
if (val >= 0x100 || !ISUUCHAR(val) ||
return TRUE;
}
-STATIC void
-bytes_to_uni(pTHX_ const U8 *start, STRLEN len, char **dest) {
- U8 buffer[UTF8_MAXLEN];
- const U8 * const end = start + len;
- char *d = *dest;
- while (start < end) {
- const int length =
- uvuni_to_utf8_flags(buffer, NATIVE_TO_UNI(*start), 0) - buffer;
- switch(length) {
- case 1:
- *d++ = buffer[0];
- break;
- case 2:
- *d++ = buffer[0];
- *d++ = buffer[1];
- break;
- default:
- Perl_croak(aTHX_ "Perl bug: value %d UTF-8 expands to %d bytes",
- *start, length);
- }
- start++;
+STATIC char *
+S_bytes_to_uni(const U8 *start, STRLEN len, char *dest, const bool needs_swap) {
+ PERL_ARGS_ASSERT_BYTES_TO_UNI;
+
+ if (UNLIKELY(needs_swap)) {
+ const U8 *p = start + len;
+ while (p-- > start) {
+ append_utf8_from_native_byte(*p, (U8 **) & dest);
+ }
+ } else {
+ const U8 * const end = start + len;
+ while (start < end) {
+ append_utf8_from_native_byte(*start, (U8 **) & dest);
+ start++;
+ }
}
- *dest = d;
+ return dest;
}
-#define PUSH_BYTES(utf8, cur, buf, len) \
+#define PUSH_BYTES(utf8, cur, buf, len, needs_swap) \
STMT_START { \
- if (utf8) bytes_to_uni(aTHX_ (U8 *) buf, len, &(cur)); \
+ if (UNLIKELY(utf8)) \
+ (cur) = S_bytes_to_uni((U8 *) buf, len, (cur), needs_swap); \
else { \
- Copy(buf, cur, len, char); \
+ if (UNLIKELY(needs_swap)) \
+ S_reverse_copy((char *)(buf), cur, len); \
+ else \
+ Copy(buf, cur, len, char); \
(cur) += (len); \
} \
} STMT_END
(start) = sv_exp_grow(cat, gl); \
(cur) = (start) + SvCUR(cat); \
} \
- PUSH_BYTES(utf8, cur, buf, glen); \
+ PUSH_BYTES(utf8, cur, buf, glen, 0); \
} STMT_END
#define PUSH_BYTE(utf8, s, byte) \
STMT_START { \
if (utf8) { \
const U8 au8 = (byte); \
- bytes_to_uni(aTHX_ &au8, 1, &(s)); \
+ (s) = S_bytes_to_uni(&au8, 1, (s), 0); \
} else *(U8 *)(s)++ = (byte); \
} STMT_END
static const char *_action( const tempsym_t* symptr )
{
- return ( symptr->flags & FLAG_PACK ) ? "pack" : "unpack";
+ return (const char *)(( symptr->flags & FLAG_PACK ) ? "pack" : "unpack");
}
/* Returns the sizeof() struct described by pat */
{
I32 total = 0;
+ PERL_ARGS_ASSERT_MEASURE_STRUCT;
+
while (next_symbol(symptr)) {
I32 len;
int size;
case e_star:
Perl_croak(aTHX_ "Within []-length '*' not allowed in %s",
_action( symptr ) );
- break;
+
default:
/* e_no_len and e_number */
len = symptr->length;
Perl_croak(aTHX_ "Invalid type '%c' in %s",
(int)TYPE_NO_MODIFIERS(symptr->code),
_action( symptr ) );
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case '.' | TYPE_IS_SHRIEKING:
case '@' | TYPE_IS_SHRIEKING:
-#endif
case '@':
case '.':
case '/':
if (!len) /* Avoid division by 0 */
len = 1;
len = total % len; /* Assumed: the start is aligned. */
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'X':
size = -1;
if (total < len)
len = len - star;
else
len = 0;
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'x':
case 'A':
case 'Z':
* returns char pointer to char after match, or NULL
*/
STATIC const char *
-S_group_end(pTHX_ register const char *patptr, register const char *patend, char ender)
+S_group_end(pTHX_ const char *patptr, const char *patend, char ender)
{
+ PERL_ARGS_ASSERT_GROUP_END;
+
while (patptr < patend) {
const char c = *patptr++;
}
Perl_croak(aTHX_ "No group ending character '%c' found in template",
ender);
- return 0;
+ NOT_REACHED; /* NOTREACHED */
}
* Advances char pointer to 1st non-digit char and returns number
*/
STATIC const char *
-S_get_num(pTHX_ register const char *patptr, I32 *lenptr )
+S_get_num(pTHX_ const char *patptr, I32 *lenptr )
{
I32 len = *patptr++ - '0';
+
+ PERL_ARGS_ASSERT_GET_NUM;
+
while (isDIGIT(*patptr)) {
if (len >= 0x7FFFFFFF/10)
Perl_croak(aTHX_ "pack/unpack repeat count overflow");
const char* patptr = symptr->patptr;
const char* const patend = symptr->patend;
+ PERL_ARGS_ASSERT_NEXT_SYMBOL;
+
symptr->flags &= ~FLAG_SLASH;
while (patptr < patend) {
switch (*patptr) {
case '!':
modifier = TYPE_IS_SHRIEKING;
- allowed = SHRIEKING_ALLOWED_TYPES;
+ allowed = "sSiIlLxXnNvV@.";
break;
-#ifdef PERL_PACK_CAN_BYTEORDER
case '>':
modifier = TYPE_IS_BIG_ENDIAN;
allowed = ENDIANNESS_ALLOWED_TYPES;
modifier = TYPE_IS_LITTLE_ENDIAN;
allowed = ENDIANNESS_ALLOWED_TYPES;
break;
-#endif /* PERL_PACK_CAN_BYTEORDER */
default:
allowed = "";
modifier = 0;
Perl_croak(aTHX_ "Can't use '%c' in a group with different byte-order in %s",
*patptr, _action( symptr ) );
- if ((code & modifier) && ckWARN(WARN_UNPACK)) {
- Perl_warner(aTHX_ packWARN(WARN_UNPACK),
- "Duplicate modifier '%c' after '%c' in %s",
- *patptr, (int) TYPE_NO_MODIFIERS(code),
- _action( symptr ) );
+ if ((code & modifier)) {
+ Perl_ck_warner(aTHX_ packWARN(WARN_UNPACK),
+ "Duplicate modifier '%c' after '%c' in %s",
+ *patptr, (int) TYPE_NO_MODIFIERS(code),
+ _action( symptr ) );
}
code |= modifier;
need_utf8(const char *pat, const char *patend)
{
bool first = TRUE;
+
+ PERL_ARGS_ASSERT_NEED_UTF8;
+
while (pat < patend) {
if (pat[0] == '#') {
pat++;
STATIC char
first_symbol(const char *pat, const char *patend) {
+ PERL_ARGS_ASSERT_FIRST_SYMBOL;
+
while (pat < patend) {
if (pat[0] != '#') return pat[0];
pat++;
}
/*
+
+=head1 Pack and Unpack
+
=for apidoc unpackstring
-The engine implementing unpack() Perl function. C<unpackstring> puts the
-extracted list items on the stack and returns the number of elements.
-Issue C<PUTBACK> before and C<SPAGAIN> after the call to this function.
+The engine implementing the unpack() Perl function.
+
+Using the template pat..patend, this function unpacks the string
+s..strend into a number of mortal SVs, which it pushes onto the perl
+argument (@_) stack (so you will need to issue a C<PUTBACK> before and
+C<SPAGAIN> after the call to this function). It returns the number of
+pushed elements.
+
+The strend and patend pointers should point to the byte following the last
+character of each string.
+
+Although this function returns its values on the perl argument stack, it
+doesn't take any parameters from that stack (and thus in particular
+there's no need to do a PUSHMARK before calling it, unlike L</call_pv> for
+example).
=cut */
{
tempsym_t sym;
+ PERL_ARGS_ASSERT_UNPACKSTRING;
+
if (flags & FLAG_DO_UTF8) flags |= FLAG_WAS_UTF8;
else if (need_utf8(pat, patend)) {
/* We probably should try to avoid this in case a scalar context call
return unpack_rec(&sym, s, s, strend, NULL );
}
-STATIC
-I32
+STATIC I32
S_unpack_rec(pTHX_ tempsym_t* symptr, const char *s, const char *strbeg, const char *strend, const char **new_s )
{
- dVAR; dSP;
- SV *sv;
+ dSP;
+ SV *sv = NULL;
const I32 start_sp_offset = SP - PL_stack_base;
howlen_t howlen;
-
I32 checksum = 0;
UV cuv = 0;
NV cdouble = 0.0;
bool explicit_length;
const bool unpack_only_one = (symptr->flags & FLAG_UNPACK_ONLY_ONE) != 0;
bool utf8 = (symptr->flags & FLAG_PARSE_UTF8) ? 1 : 0;
+
+ PERL_ARGS_ASSERT_UNPACK_REC;
+
symptr->strbeg = s - strbeg;
while (next_symbol(symptr)) {
packprops_t props;
I32 len;
I32 datumtype = symptr->code;
+ bool needs_swap;
/* do first one only unless in list context
/ is implemented by unpacking the count, then popping it from the
stack, so must check that we're not in the middle of a / */
}
}
+ needs_swap = NEEDS_SWAP(datumtype);
+
switch(TYPE_NO_ENDIANNESS(datumtype)) {
default:
Perl_croak(aTHX_ "Invalid type '%c' in unpack", (int)TYPE_NO_MODIFIERS(datumtype) );
cuv = 0;
cdouble = 0;
continue;
- break;
+
case '(':
{
tempsym_t savsym = *symptr;
symptr->previous = &savsym;
symptr->level++;
PUTBACK;
+ if (len && unpack_only_one) len = 1;
while (len--) {
symptr->patptr = savsym.grpbeg;
if (utf8) symptr->flags |= FLAG_PARSE_UTF8;
*symptr = savsym;
break;
}
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case '.' | TYPE_IS_SHRIEKING:
-#endif
case '.': {
const char *from;
SV *sv;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
const bool u8 = utf8 && !(datumtype & TYPE_IS_SHRIEKING);
-#else /* PERL_PACK_CAN_SHRIEKSIGN */
- const bool u8 = utf8;
-#endif
if (howlen == e_star) from = strbeg;
else if (len <= 0) from = s;
else {
sv = from <= s ?
newSVuv( u8 ? (UV) utf8_length((const U8*)from, (const U8*)s) : (UV) (s-from)) :
newSViv(-(u8 ? (IV) utf8_length((const U8*)s, (const U8*)from) : (IV) (from-s)));
- XPUSHs(sv_2mortal(sv));
+ mXPUSHs(sv);
break;
}
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case '@' | TYPE_IS_SHRIEKING:
-#endif
case '@':
s = strbeg + symptr->strbeg;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
if (utf8 && !(datumtype & TYPE_IS_SHRIEKING))
-#else /* PERL_PACK_CAN_SHRIEKSIGN */
- if (utf8)
-#endif
{
while (len > 0) {
if (s >= strend)
break;
}
len = (s - strbeg) % len;
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'X':
if (utf8) {
while (len > 0) {
if (ai32 == 0) break;
len -= ai32;
}
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'x':
if (utf8) {
while (len>0) {
break;
case '/':
Perl_croak(aTHX_ "'/' must follow a numeric type in unpack");
- break;
+
case 'A':
case 'Z':
case 'a':
if (utf8 && (symptr->flags & FLAG_WAS_UTF8)) {
for (ptr = s+len-1; ptr >= s; ptr--)
if (*ptr != 0 && !UTF8_IS_CONTINUATION(*ptr) &&
- !is_utf8_space((U8 *) ptr)) break;
+ !isSPACE_utf8(ptr)) break;
if (ptr >= s) ptr += UTF8SKIP(ptr);
else ptr++;
if (ptr > s+len)
if (!(symptr->flags & FLAG_WAS_UTF8))
sv_utf8_downgrade(sv, 0);
}
- XPUSHs(sv_2mortal(sv));
+ mXPUSHs(sv);
s += len;
break;
case 'B':
if (howlen == e_star || len > (strend - s) * 8)
len = (strend - s) * 8;
if (checksum) {
- if (!PL_bitcount) {
- int bits;
- Newxz(PL_bitcount, 256, char);
- for (bits = 1; bits < 256; bits++) {
- if (bits & 1) PL_bitcount[bits]++;
- if (bits & 2) PL_bitcount[bits]++;
- if (bits & 4) PL_bitcount[bits]++;
- if (bits & 8) PL_bitcount[bits]++;
- if (bits & 16) PL_bitcount[bits]++;
- if (bits & 32) PL_bitcount[bits]++;
- if (bits & 64) PL_bitcount[bits]++;
- if (bits & 128) PL_bitcount[bits]++;
- }
- }
if (utf8)
while (len >= 8 && s < strend) {
cuv += PL_bitcount[uni_to_byte(aTHX_ &s, strend, datumtype)];
}
case 'H':
case 'h': {
- char *str;
+ char *str = NULL;
/* Preliminary length estimate, acceptable for utf8 too */
if (howlen == e_star || len > (strend - s) * 2)
len = (strend - s) * 2;
- sv = sv_2mortal(newSV(len ? len : 1));
- SvPOK_on(sv);
- str = SvPVX(sv);
+ if (!checksum) {
+ sv = sv_2mortal(newSV(len ? len : 1));
+ SvPOK_on(sv);
+ str = SvPVX(sv);
+ }
if (datumtype == 'h') {
U8 bits = 0;
I32 ai32 = len;
if (s >= strend) break;
bits = uni_to_byte(aTHX_ &s, strend, datumtype);
} else bits = * (U8 *) s++;
- *str++ = PL_hexdigit[bits & 15];
+ if (!checksum)
+ *str++ = PL_hexdigit[bits & 15];
}
} else {
U8 bits = 0;
if (s >= strend) break;
bits = uni_to_byte(aTHX_ &s, strend, datumtype);
} else bits = *(U8 *) s++;
- *str++ = PL_hexdigit[(bits >> 4) & 15];
+ if (!checksum)
+ *str++ = PL_hexdigit[(bits >> 4) & 15];
}
}
- *str = '\0';
- SvCUR_set(sv, str - SvPVX_const(sv));
- XPUSHs(sv);
+ if (!checksum) {
+ *str = '\0';
+ SvCUR_set(sv, str - SvPVX_const(sv));
+ XPUSHs(sv);
+ }
break;
}
+ case 'C':
+ if (len == 0) {
+ if (explicit_length)
+ /* Switch to "character" mode */
+ utf8 = (symptr->flags & FLAG_DO_UTF8) ? 1 : 0;
+ break;
+ }
+ /* FALLTHROUGH */
case 'c':
- while (len-- > 0) {
- int aint = SHIFT_BYTE(utf8, s, strend, datumtype);
- if (aint >= 128) /* fake up signed chars */
+ while (len-- > 0 && s < strend) {
+ int aint;
+ if (utf8)
+ {
+ STRLEN retlen;
+ aint = utf8n_to_uvchr((U8 *) s, strend-s, &retlen,
+ ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
+ if (retlen == (STRLEN) -1 || retlen == 0)
+ Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
+ s += retlen;
+ }
+ else
+ aint = *(U8 *)(s)++;
+ if (aint >= 128 && datumtype != 'C') /* fake up signed chars */
aint -= 256;
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)aint)));
+ mPUSHi(aint);
else if (checksum > bits_in_uv)
cdouble += (NV)aint;
else
cuv += aint;
}
break;
- case 'C':
case 'W':
W_checksum:
- if (len == 0) {
- if (explicit_length && datumtype == 'C')
- /* Switch to "character" mode */
- utf8 = (symptr->flags & FLAG_DO_UTF8) ? 1 : 0;
- break;
- }
- if (datumtype == 'C' ?
- (symptr->flags & FLAG_DO_UTF8) &&
- !(symptr->flags & FLAG_WAS_UTF8) : utf8) {
+ if (utf8) {
while (len-- > 0 && s < strend) {
STRLEN retlen;
const UV val = utf8n_to_uvchr((U8 *) s, strend-s, &retlen,
Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
s += retlen;
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV) val)));
+ mPUSHu(val);
else if (checksum > bits_in_uv)
cdouble += (NV) val;
else
} else if (!checksum)
while (len-- > 0) {
const U8 ch = *(U8 *) s++;
- PUSHs(sv_2mortal(newSVuv((UV) ch)));
+ mPUSHu(ch);
}
else if (checksum > bits_in_uv)
while (len-- > 0) cdouble += (NV) *(U8 *) s++;
break;
case 'U':
if (len == 0) {
- if (explicit_length) {
+ if (explicit_length && howlen != e_star) {
/* Switch to "bytes in UTF-8" mode */
if (symptr->flags & FLAG_DO_UTF8) utf8 = 0;
else
len = UTF8SKIP(result);
if (!uni_to_bytes(aTHX_ &ptr, strend,
(char *) &result[1], len-1, 'U')) break;
- auv = utf8n_to_uvuni(result, len, &retlen, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANYUV);
+ auv = utf8n_to_uvchr(result, len, &retlen, UTF8_ALLOW_DEFAULT);
s = ptr;
} else {
- auv = utf8n_to_uvuni((U8*)s, strend - s, &retlen, ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANYUV);
+ auv = utf8n_to_uvchr((U8*)s, strend - s, &retlen, UTF8_ALLOW_DEFAULT);
if (retlen == (STRLEN) -1 || retlen == 0)
Perl_croak(aTHX_ "Malformed UTF-8 string in unpack");
s += retlen;
}
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV) auv)));
+ mPUSHu(auv);
else if (checksum > bits_in_uv)
cdouble += (NV) auv;
else
#if SHORTSIZE != SIZE16
while (len-- > 0) {
short ashort;
- SHIFT_VAR(utf8, s, strend, ashort, datumtype);
- DO_BO_UNPACK(ashort, s);
+ SHIFT_VAR(utf8, s, strend, ashort, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)ashort)));
+ mPUSHi(ashort);
else if (checksum > bits_in_uv)
cdouble += (NV)ashort;
else
}
break;
#else
- /* Fallthrough! */
+ /* FALLTHROUGH */
#endif
case 's':
while (len-- > 0) {
#if U16SIZE > SIZE16
ai16 = 0;
#endif
- SHIFT16(utf8, s, strend, &ai16, datumtype);
- DO_BO_UNPACK(ai16, 16);
+ SHIFT16(utf8, s, strend, &ai16, datumtype, needs_swap);
#if U16SIZE > SIZE16
if (ai16 > 32767)
ai16 -= 65536;
#endif
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)ai16)));
+ mPUSHi(ai16);
else if (checksum > bits_in_uv)
cdouble += (NV)ai16;
else
#if SHORTSIZE != SIZE16
while (len-- > 0) {
unsigned short aushort;
- SHIFT_VAR(utf8, s, strend, aushort, datumtype);
- DO_BO_UNPACK(aushort, s);
+ SHIFT_VAR(utf8, s, strend, aushort, datumtype, needs_swap,
+ needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV) aushort)));
+ mPUSHu(aushort);
else if (checksum > bits_in_uv)
cdouble += (NV)aushort;
else
}
break;
#else
- /* Fallhrough! */
+ /* FALLTHROUGH */
#endif
case 'v':
case 'n':
#if U16SIZE > SIZE16
au16 = 0;
#endif
- SHIFT16(utf8, s, strend, &au16, datumtype);
- DO_BO_UNPACK(au16, 16);
-#ifdef HAS_NTOHS
+ SHIFT16(utf8, s, strend, &au16, datumtype, needs_swap);
if (datumtype == 'n')
au16 = PerlSock_ntohs(au16);
-#endif
-#ifdef HAS_VTOHS
if (datumtype == 'v')
au16 = vtohs(au16);
-#endif
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV)au16)));
+ mPUSHu(au16);
else if (checksum > bits_in_uv)
cdouble += (NV) au16;
else
cuv += au16;
}
break;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'v' | TYPE_IS_SHRIEKING:
case 'n' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
# if U16SIZE > SIZE16
ai16 = 0;
# endif
- SHIFT16(utf8, s, strend, &ai16, datumtype);
-# ifdef HAS_NTOHS
+ SHIFT16(utf8, s, strend, &ai16, datumtype, needs_swap);
+ /* There should never be any byte-swapping here. */
+ assert(!TYPE_ENDIANNESS(datumtype));
if (datumtype == ('n' | TYPE_IS_SHRIEKING))
ai16 = (I16) PerlSock_ntohs((U16) ai16);
-# endif /* HAS_NTOHS */
-# ifdef HAS_VTOHS
if (datumtype == ('v' | TYPE_IS_SHRIEKING))
ai16 = (I16) vtohs((U16) ai16);
-# endif /* HAS_VTOHS */
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)ai16)));
+ mPUSHi(ai16);
else if (checksum > bits_in_uv)
cdouble += (NV) ai16;
else
cuv += ai16;
}
break;
-#endif /* PERL_PACK_CAN_SHRIEKSIGN */
case 'i':
case 'i' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
int aint;
- SHIFT_VAR(utf8, s, strend, aint, datumtype);
- DO_BO_UNPACK(aint, i);
+ SHIFT_VAR(utf8, s, strend, aint, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)aint)));
+ mPUSHi(aint);
else if (checksum > bits_in_uv)
cdouble += (NV)aint;
else
case 'I' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
unsigned int auint;
- SHIFT_VAR(utf8, s, strend, auint, datumtype);
- DO_BO_UNPACK(auint, i);
+ SHIFT_VAR(utf8, s, strend, auint, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV)auint)));
+ mPUSHu(auint);
else if (checksum > bits_in_uv)
cdouble += (NV)auint;
else
case 'j':
while (len-- > 0) {
IV aiv;
- SHIFT_VAR(utf8, s, strend, aiv, datumtype);
-#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);
-#else
- Perl_croak(aTHX_ "'j' not supported on this platform");
-#endif
+ SHIFT_VAR(utf8, s, strend, aiv, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSViv(aiv)));
+ mPUSHi(aiv);
else if (checksum > bits_in_uv)
cdouble += (NV)aiv;
else
case 'J':
while (len-- > 0) {
UV auv;
- SHIFT_VAR(utf8, s, strend, auv, datumtype);
-#if IVSIZE == INTSIZE
- DO_BO_UNPACK(auv, i);
-#elif IVSIZE == LONGSIZE
- DO_BO_UNPACK(auv, l);
-#elif defined(HAS_QUAD) && IVSIZE == U64SIZE
- DO_BO_UNPACK(auv, 64);
-#else
- Perl_croak(aTHX_ "'J' not supported on this platform");
-#endif
+ SHIFT_VAR(utf8, s, strend, auv, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVuv(auv)));
+ mPUSHu(auv);
else if (checksum > bits_in_uv)
cdouble += (NV)auv;
else
#if LONGSIZE != SIZE32
while (len-- > 0) {
long along;
- SHIFT_VAR(utf8, s, strend, along, datumtype);
- DO_BO_UNPACK(along, l);
+ SHIFT_VAR(utf8, s, strend, along, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)along)));
+ mPUSHi(along);
else if (checksum > bits_in_uv)
cdouble += (NV)along;
else
}
break;
#else
- /* Fallthrough! */
+ /* FALLTHROUGH */
#endif
case 'l':
while (len-- > 0) {
#if U32SIZE > SIZE32
ai32 = 0;
#endif
- SHIFT32(utf8, s, strend, &ai32, datumtype);
- DO_BO_UNPACK(ai32, 32);
+ SHIFT32(utf8, s, strend, &ai32, datumtype, needs_swap);
#if U32SIZE > SIZE32
if (ai32 > 2147483647) ai32 -= 4294967296;
#endif
if (!checksum)
- PUSHs(sv_2mortal(newSViv((IV)ai32)));
+ mPUSHi(ai32);
else if (checksum > bits_in_uv)
cdouble += (NV)ai32;
else
#if LONGSIZE != SIZE32
while (len-- > 0) {
unsigned long aulong;
- SHIFT_VAR(utf8, s, strend, aulong, datumtype);
- DO_BO_UNPACK(aulong, l);
+ SHIFT_VAR(utf8, s, strend, aulong, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV)aulong)));
+ mPUSHu(aulong);
else if (checksum > bits_in_uv)
cdouble += (NV)aulong;
else
}
break;
#else
- /* Fall through! */
+ /* FALLTHROUGH */
#endif
case 'V':
case 'N':
#if U32SIZE > SIZE32
au32 = 0;
#endif
- SHIFT32(utf8, s, strend, &au32, datumtype);
- DO_BO_UNPACK(au32, 32);
-#ifdef HAS_NTOHL
+ SHIFT32(utf8, s, strend, &au32, datumtype, needs_swap);
if (datumtype == 'N')
au32 = PerlSock_ntohl(au32);
-#endif
-#ifdef HAS_VTOHL
if (datumtype == 'V')
au32 = vtohl(au32);
-#endif
if (!checksum)
- PUSHs(sv_2mortal(newSVuv((UV)au32)));
+ mPUSHu(au32);
else if (checksum > bits_in_uv)
cdouble += (NV)au32;
else
cuv += au32;
}
break;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'V' | TYPE_IS_SHRIEKING:
case 'N' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
I32 ai32;
-# if U32SIZE > SIZE32
+#if U32SIZE > SIZE32
ai32 = 0;
-# endif
- SHIFT32(utf8, s, strend, &ai32, datumtype);
-# ifdef HAS_NTOHL
+#endif
+ SHIFT32(utf8, s, strend, &ai32, datumtype, needs_swap);
+ /* There should never be any byte swapping here. */
+ assert(!TYPE_ENDIANNESS(datumtype));
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)
- PUSHs(sv_2mortal(newSViv((IV)ai32)));
+ mPUSHi(ai32);
else if (checksum > bits_in_uv)
cdouble += (NV)ai32;
else
cuv += ai32;
}
break;
-#endif /* PERL_PACK_CAN_SHRIEKSIGN */
case 'p':
while (len-- > 0) {
const char *aptr;
- SHIFT_VAR(utf8, s, strend, aptr, datumtype);
- DO_BO_UNPACK_PC(aptr);
+ SHIFT_VAR(utf8, s, strend, aptr, datumtype, needs_swap);
/* newSVpv generates undef if aptr is NULL */
- PUSHs(sv_2mortal(newSVpv(aptr, 0)));
+ mPUSHs(newSVpv(aptr, 0));
}
break;
case 'w':
/* UTF8_IS_XXXXX not right here - using constant 0x80 */
if (ch < 0x80) {
bytes = 0;
- PUSHs(sv_2mortal(newSVuv(auv)));
+ mPUSHu(auv);
len--;
auv = 0;
continue;
if (++bytes >= sizeof(UV)) { /* promote to string */
const char *t;
- sv = Perl_newSVpvf(aTHX_ "%.*"UVf, (int)TYPE_DIGITS(UV), auv);
+ sv = Perl_newSVpvf(aTHX_ "%.*"UVuf, (int)TYPE_DIGITS(UV), auv);
while (s < strend) {
ch = SHIFT_BYTE(utf8, s, strend, datumtype);
sv = mul128(sv, (U8)(ch & 0x7f));
while (*t == '0')
t++;
sv_chop(sv, t);
- PUSHs(sv_2mortal(sv));
+ mPUSHs(sv);
len--;
auv = 0;
}
EXTEND(SP, 1);
if (s + sizeof(char*) <= strend) {
char *aptr;
- SHIFT_VAR(utf8, s, strend, aptr, datumtype);
- DO_BO_UNPACK_PC(aptr);
+ SHIFT_VAR(utf8, s, strend, aptr, datumtype, needs_swap);
/* newSVpvn generates undef if aptr is NULL */
- PUSHs(sv_2mortal(newSVpvn(aptr, len)));
+ PUSHs(newSVpvn_flags(aptr, len, SVs_TEMP));
}
break;
-#ifdef HAS_QUAD
+#if defined(HAS_QUAD) && IVSIZE >= 8
case 'q':
while (len-- > 0) {
Quad_t aquad;
- SHIFT_VAR(utf8, s, strend, aquad, datumtype);
- DO_BO_UNPACK(aquad, 64);
+ SHIFT_VAR(utf8, s, strend, aquad, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(aquad >= IV_MIN && aquad <= IV_MAX ?
- newSViv((IV)aquad) : newSVnv((NV)aquad)));
+ mPUSHs(newSViv((IV)aquad));
else if (checksum > bits_in_uv)
cdouble += (NV)aquad;
else
case 'Q':
while (len-- > 0) {
Uquad_t auquad;
- SHIFT_VAR(utf8, s, strend, auquad, datumtype);
- DO_BO_UNPACK(auquad, 64);
+ SHIFT_VAR(utf8, s, strend, auquad, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(auquad <= UV_MAX ?
- newSVuv((UV)auquad):newSVnv((NV)auquad)));
+ mPUSHs(newSVuv((UV)auquad));
else if (checksum > bits_in_uv)
cdouble += (NV)auquad;
else
cuv += auquad;
}
break;
-#endif /* HAS_QUAD */
+#endif
/* float and double added gnb@melba.bby.oz.au 22/11/89 */
case 'f':
while (len-- > 0) {
float afloat;
- SHIFT_VAR(utf8, s, strend, afloat, datumtype);
- DO_BO_UNPACK_N(afloat, float);
+ SHIFT_VAR(utf8, s, strend, afloat, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVnv((NV)afloat)));
+ mPUSHn(afloat);
else
cdouble += afloat;
}
case 'd':
while (len-- > 0) {
double adouble;
- SHIFT_VAR(utf8, s, strend, adouble, datumtype);
- DO_BO_UNPACK_N(adouble, double);
+ SHIFT_VAR(utf8, s, strend, adouble, datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVnv((NV)adouble)));
+ mPUSHn(adouble);
else
cdouble += adouble;
}
break;
case 'F':
while (len-- > 0) {
- NV anv;
- SHIFT_VAR(utf8, s, strend, anv, datumtype);
- DO_BO_UNPACK_N(anv, NV);
+ NV_bytes anv;
+ SHIFT_BYTES(utf8, s, strend, anv.bytes, sizeof(anv.bytes),
+ datumtype, needs_swap);
if (!checksum)
- PUSHs(sv_2mortal(newSVnv(anv)));
+ mPUSHn(anv.nv);
else
- cdouble += anv;
+ cdouble += anv.nv;
}
break;
#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
case 'D':
while (len-- > 0) {
- long double aldouble;
- SHIFT_VAR(utf8, s, strend, aldouble, datumtype);
- DO_BO_UNPACK_N(aldouble, long double);
+ ld_bytes aldouble;
+ SHIFT_BYTES(utf8, s, strend, aldouble.bytes,
+ sizeof(aldouble.bytes), datumtype, needs_swap);
+ /* The most common long double format, the x86 80-bit
+ * extended precision, has either 2 or 6 unused bytes,
+ * which may contain garbage, which may contain
+ * unintentional data. While we do zero the bytes of
+ * the long double data in pack(), here in unpack() we
+ * don't, because it's really hard to envision that
+ * reading the long double off aldouble would be
+ * affected by the unused bytes.
+ *
+ * Note that trying to unpack 'long doubles' of 'long
+ * doubles' packed in another system is in the general
+ * case doomed without having more detail. */
if (!checksum)
- PUSHs(sv_2mortal(newSVnv((NV)aldouble)));
+ mPUSHn(aldouble.ld);
else
- cdouble += aldouble;
+ cdouble += aldouble.ld;
}
break;
#endif
case 'u':
- /* MKS:
- * Initialise the decode mapping. By using a table driven
- * algorithm, the code will be character-set independent
- * (and just as fast as doing character arithmetic)
- */
- if (PL_uudmap['M'] == 0) {
- size_t i;
-
- for (i = 0; i < sizeof(PL_uuemap); ++i)
- PL_uudmap[(U8)PL_uuemap[i]] = i;
- /*
- * Because ' ' and '`' map to the same value,
- * we need to decode them both the same.
- */
- PL_uudmap[' '] = 0;
- }
- {
+ if (!checksum) {
const STRLEN l = (STRLEN) (strend - s) * 3 / 4;
sv = sv_2mortal(newSV(l));
if (l) SvPOK_on(sv);
if (utf8) {
while (next_uni_uu(aTHX_ &s, strend, &len)) {
I32 a, b, c, d;
- char hunk[4];
+ char hunk[3];
- hunk[3] = '\0';
while (len > 0) {
next_uni_uu(aTHX_ &s, strend, &a);
next_uni_uu(aTHX_ &s, strend, &b);
hunk[0] = (char)((a << 2) | (b >> 4));
hunk[1] = (char)((b << 4) | (c >> 2));
hunk[2] = (char)((c << 6) | d);
- sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
+ if (!checksum)
+ sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
len -= 3;
}
if (s < strend) {
} else {
while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
I32 a, b, c, d;
- char hunk[4];
+ char hunk[3];
- hunk[3] = '\0';
len = PL_uudmap[*(U8*)s++] & 077;
while (len > 0) {
if (s < strend && ISUUCHAR(*s))
hunk[0] = (char)((a << 2) | (b >> 4));
hunk[1] = (char)((b << 4) | (c >> 2));
hunk[2] = (char)((c << 6) | d);
- sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
+ if (!checksum)
+ sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
len -= 3;
}
if (*s == '\n')
s += 2;
}
}
- XPUSHs(sv);
+ if (!checksum)
+ XPUSHs(sv);
break;
}
}
sv = newSVuv(cuv);
}
- XPUSHs(sv_2mortal(sv));
+ mXPUSHs(sv);
checksum = 0;
}
if (symptr->flags & FLAG_SLASH){
if (SP - PL_stack_base - start_sp_offset <= 0)
- Perl_croak(aTHX_ "'/' must follow a numeric type in unpack");
+ break;
if( next_symbol(symptr) ){
if( symptr->howlen == e_number )
Perl_croak(aTHX_ "Count after length/code in unpack" );
PP(pp_unpack)
{
- dVAR;
dSP;
dPOPPOPssrl;
I32 gimme = GIMME_V;
bool skip = 1;
bool ignore = 0;
+ PERL_ARGS_ASSERT_IS_AN_INT;
+
while (*s) {
switch (*s) {
case ' ':
char *t = s;
int m = 0;
+ PERL_ARGS_ASSERT_DIV128;
+
*done = 1;
while (*t) {
const int i = m * 10 + (*t - '0');
*/
void
-Perl_packlist(pTHX_ SV *cat, const char *pat, const char *patend, register SV **beglist, SV **endlist )
+Perl_packlist(pTHX_ SV *cat, const char *pat, const char *patend, SV **beglist, SV **endlist )
{
- dVAR;
- STRLEN no_len;
tempsym_t sym;
+ PERL_ARGS_ASSERT_PACKLIST;
+
TEMPSYM_INIT(&sym, pat, patend, FLAG_PACK);
/* We're going to do changes through SvPVX(cat). Make sure it's valid.
Also make sure any UTF8 flag is loaded */
- SvPV_force(cat, no_len);
+ SvPV_force_nolen(cat);
if (DO_UTF8(cat))
sym.flags |= FLAG_PARSE_UTF8 | FLAG_DO_UTF8;
from_start = SvPVX_const(sv);
from_end = from_start + SvCUR(sv);
for (from_ptr = from_start; from_ptr < from_end; from_ptr++)
- if (!NATIVE_IS_INVARIANT(*from_ptr)) break;
+ if (!NATIVE_BYTE_IS_INVARIANT(*from_ptr)) break;
if (from_ptr == from_end) {
/* Simple case: no character needs to be changed */
SvUTF8_on(sv);
if (m != marks + sym_ptr->level+1) {
Safefree(marks);
Safefree(to_start);
- Perl_croak(aTHX_ "Assertion: marks beyond string end");
+ Perl_croak(aTHX_ "panic: marks beyond string end, m=%p, marks=%p, "
+ "level=%d", m, marks, sym_ptr->level);
}
for (group=sym_ptr; group; group = group->previous)
group->strbeg = marks[group->level] - to_start;
const STRLEN cur = SvCUR(sv);
const STRLEN len = SvLEN(sv);
STRLEN extend;
+
+ PERL_ARGS_ASSERT_SV_EXP_GROW;
+
if (len - cur > needed) return SvPVX(sv);
extend = needed > len ? needed : len;
return SvGROW(sv, len+extend+1);
}
+static void
+S_sv_check_infnan(pTHX_ SV *sv, I32 datumtype)
+{
+ SvGETMAGIC(sv);
+ if (UNLIKELY(isinfnansv(sv))) {
+ const I32 c = TYPE_NO_MODIFIERS(datumtype);
+ const NV nv = SvNV_nomg(sv);
+ if (c == 'w')
+ Perl_croak(aTHX_ "Cannot compress %"NVgf" in pack", nv);
+ else
+ Perl_croak(aTHX_ "Cannot pack %"NVgf" with '%c'", nv, (int) c);
+ }
+}
+
+#define SvIV_no_inf(sv,d) (S_sv_check_infnan(aTHX_ sv,d), SvIV_nomg(sv))
+#define SvUV_no_inf(sv,d) (S_sv_check_infnan(aTHX_ sv,d), SvUV_nomg(sv))
+
STATIC
SV **
S_pack_rec(pTHX_ SV *cat, tempsym_t* symptr, SV **beglist, SV **endlist )
{
- dVAR;
tempsym_t lookahead;
I32 items = endlist - beglist;
bool found = next_symbol(symptr);
bool utf8 = (symptr->flags & FLAG_PARSE_UTF8) ? 1 : 0;
bool warn_utf8 = ckWARN(WARN_UTF8);
+ char* from;
+
+ PERL_ARGS_ASSERT_PACK_REC;
if (symptr->level == 0 && found && symptr->code == 'U') {
marked_upgrade(aTHX_ cat, symptr);
howlen_t howlen = symptr->howlen;
char *start = SvPVX(cat);
char *cur = start + SvCUR(cat);
+ bool needs_swap;
#define NEXTFROM (lengthcode ? lengthcode : items-- > 0 ? *beglist++ : &PL_sv_no)
+#define PEEKFROM (lengthcode ? lengthcode : items > 0 ? *beglist : &PL_sv_no)
switch (howlen) {
case e_star:
if (lookahead.howlen == e_number) count = lookahead.length;
else {
if (items > 0) {
- if (SvGAMAGIC(*beglist)) {
- /* Avoid reading the active data more than once
- by copying it to a temporary. */
- STRLEN len;
- const char *const pv = SvPV_const(*beglist, len);
- SV *const temp = sv_2mortal(newSVpvn(pv, len));
- if (SvUTF8(*beglist))
- SvUTF8_on(temp);
- *beglist = temp;
- }
- count = DO_UTF8(*beglist) ?
- sv_len_utf8(*beglist) : sv_len(*beglist);
+ count = sv_len_utf8(*beglist);
}
else count = 0;
if (lookahead.code == 'Z') count++;
lengthcode = sv_2mortal(newSViv(count));
}
+ needs_swap = NEEDS_SWAP(datumtype);
+
/* Code inside the switch must take care to properly update
cat (CUR length and '\0' termination) if it updated *cur and
doesn't simply leave using break */
- switch(TYPE_NO_ENDIANNESS(datumtype)) {
+ switch (TYPE_NO_ENDIANNESS(datumtype)) {
default:
Perl_croak(aTHX_ "Invalid type '%c' in pack",
(int) TYPE_NO_MODIFIERS(datumtype));
case '%':
Perl_croak(aTHX_ "'%%' may not be used in pack");
- {
- char *from;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
+
case '.' | TYPE_IS_SHRIEKING:
-#endif
case '.':
if (howlen == e_star) from = start;
else if (len == 0) from = cur;
from = group ? start + group->strbeg : start;
}
fromstr = NEXTFROM;
- len = SvIV(fromstr);
+ len = SvIV_no_inf(fromstr, datumtype);
goto resize;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case '@' | TYPE_IS_SHRIEKING:
-#endif
case '@':
from = start + symptr->strbeg;
resize:
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
if (utf8 && !(datumtype & TYPE_IS_SHRIEKING))
-#else /* PERL_PACK_CAN_SHRIEKSIGN */
- if (utf8)
-#endif
if (len >= 0) {
while (len && from < cur) {
from += UTF8SKIP(from);
goto shrink;
}
break;
- }
+
case '(': {
tempsym_t savsym = *symptr;
U32 group_modifiers = TYPE_MODIFIERS(datumtype & ~symptr->flags);
if (savsym.howlen == e_star && beglist == endlist)
break; /* No way to continue */
}
+ items = endlist - beglist;
lookahead.flags = symptr->flags & ~group_modifiers;
goto no_change;
}
break;
}
len = (cur-start) % len;
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'X':
if (utf8) {
if (len < 1) goto no_change;
if (ai32 == 0) goto no_change;
len -= ai32;
}
- /* FALL THROUGH */
+ /* FALLTHROUGH */
case 'x':
goto grow;
case 'A':
GROWING(0, cat, start, cur, len);
if (!uni_to_bytes(aTHX_ &aptr, end, cur, fromlen,
datumtype | TYPE_IS_PACK))
- Perl_croak(aTHX_ "Perl bug: predicted utf8 length not available");
+ Perl_croak(aTHX_ "panic: predicted utf8 length not available, "
+ "for '%c', aptr=%p end=%p cur=%p, fromlen=%"UVuf,
+ (int)datumtype, aptr, end, cur, (UV)fromlen);
cur += fromlen;
len -= fromlen;
} else if (utf8) {
}
memset(cur, datumtype == 'A' ? ' ' : '\0', len);
cur += len;
+ SvTAINT(cat);
break;
}
case 'B':
while (len-- > 0) {
IV aiv;
fromstr = NEXTFROM;
- aiv = SvIV(fromstr);
- if ((-128 > aiv || aiv > 127) &&
- ckWARN(WARN_PACK))
- Perl_warner(aTHX_ packWARN(WARN_PACK),
- "Character in 'c' format wrapped in pack");
+ aiv = SvIV_no_inf(fromstr, datumtype);
+ if ((-128 > aiv || aiv > 127))
+ Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
+ "Character in 'c' format wrapped in pack");
PUSH_BYTE(utf8, cur, (U8)(aiv & 0xff));
}
break;
utf8 = (symptr->flags & FLAG_DO_UTF8) ? 1 : 0;
break;
}
- GROWING(0, cat, start, cur, len);
while (len-- > 0) {
IV aiv;
fromstr = NEXTFROM;
- aiv = SvIV(fromstr);
- if ((0 > aiv || aiv > 0xff) &&
- ckWARN(WARN_PACK))
- Perl_warner(aTHX_ packWARN(WARN_PACK),
- "Character in 'C' format wrapped in pack");
- *cur++ = (char)(aiv & 0xff);
+ aiv = SvIV_no_inf(fromstr, datumtype);
+ if ((0 > aiv || aiv > 0xff))
+ Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
+ "Character in 'C' format wrapped in pack");
+ PUSH_BYTE(utf8, cur, (U8)(aiv & 0xff));
}
break;
case 'W': {
char *end;
- U8 in_bytes = IN_BYTES;
+ U8 in_bytes = (U8)IN_BYTES;
end = start+SvLEN(cat)-1;
if (utf8) end -= UTF8_MAXLEN-1;
while (len-- > 0) {
UV auv;
fromstr = NEXTFROM;
- auv = SvUV(fromstr);
+ auv = SvUV_no_inf(fromstr, datumtype);
if (in_bytes) auv = auv % 0x100;
if (utf8) {
W_utf8:
GROWING(0, cat, start, cur, len+UTF8_MAXLEN);
end = start+SvLEN(cat)-UTF8_MAXLEN;
}
- cur = (char *) uvuni_to_utf8_flags((U8 *) cur,
- NATIVE_TO_UNI(auv),
+ cur = (char *) uvchr_to_utf8_flags((U8 *) cur,
+ auv,
warn_utf8 ?
0 : UNICODE_ALLOW_ANY);
} else {
end = start+SvLEN(cat)-UTF8_MAXLEN;
goto W_utf8;
}
- if (ckWARN(WARN_PACK))
- Perl_warner(aTHX_ packWARN(WARN_PACK),
- "Character in 'W' format wrapped in pack");
+ Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
+ "Character in 'W' format wrapped in pack");
auv &= 0xff;
}
if (cur >= end) {
while (len-- > 0) {
UV auv;
fromstr = NEXTFROM;
- auv = SvUV(fromstr);
+ auv = SvUV_no_inf(fromstr, datumtype);
if (utf8) {
U8 buffer[UTF8_MAXLEN], *endb;
- endb = uvuni_to_utf8_flags(buffer, auv,
+ endb = uvchr_to_utf8_flags(buffer, auv,
warn_utf8 ?
0 : UNICODE_ALLOW_ANY);
if (cur+(endb-buffer)*UTF8_EXPAND >= end) {
len+(endb-buffer)*UTF8_EXPAND);
end = start+SvLEN(cat);
}
- bytes_to_uni(aTHX_ buffer, endb-buffer, &cur);
+ cur = S_bytes_to_uni(buffer, endb-buffer, cur, 0);
} else {
if (cur >= end) {
*cur = '\0';
GROWING(0, cat, start, cur, len+UTF8_MAXLEN);
end = start+SvLEN(cat)-UTF8_MAXLEN;
}
- cur = (char *) uvuni_to_utf8_flags((U8 *) cur, auv,
+ cur = (char *) uvchr_to_utf8_flags((U8 *) cur, auv,
warn_utf8 ?
0 : UNICODE_ALLOW_ANY);
}
NV anv;
fromstr = NEXTFROM;
anv = SvNV(fromstr);
-#ifdef __VOS__
- /* VOS does not automatically map a floating-point overflow
- during conversion from double to float into infinity, so we
- do it by hand. This code should either be generalized for
- any OS that needs it, or removed if and when VOS implements
- posix-976 (suggestion to support mapping to infinity).
- Paul.Green@stratus.com 02-04-02. */
- if (anv > FLT_MAX)
- afloat = _float_constants[0]; /* single prec. inf. */
- else if (anv < -FLT_MAX)
- afloat = _float_constants[0]; /* single prec. inf. */
- else afloat = (float) anv;
-#else /* __VOS__ */
-# if defined(VMS) && !defined(__IEEE_FP)
+# if defined(VMS) && !defined(_IEEE_FP)
/* IEEE fp overflow shenanigans are unavailable on VAX and optional
* on Alpha; fake it if we don't have them.
*/
# else
afloat = (float)anv;
# endif
-#endif /* __VOS__ */
- DO_BO_PACK_N(afloat, float);
- PUSH_VAR(utf8, cur, afloat);
+ PUSH_VAR(utf8, cur, afloat, needs_swap);
}
break;
case 'd':
NV anv;
fromstr = NEXTFROM;
anv = SvNV(fromstr);
-#ifdef __VOS__
- /* VOS does not automatically map a floating-point overflow
- during conversion from long double to double into infinity,
- so we do it by hand. This code should either be generalized
- for any OS that needs it, or removed if and when VOS
- implements posix-976 (suggestion to support mapping to
- infinity). Paul.Green@stratus.com 02-04-02. */
- if (anv > DBL_MAX)
- adouble = _double_constants[0]; /* double prec. inf. */
- else if (anv < -DBL_MAX)
- adouble = _double_constants[0]; /* double prec. inf. */
- else adouble = (double) anv;
-#else /* __VOS__ */
-# if defined(VMS) && !defined(__IEEE_FP)
+# if defined(VMS) && !defined(_IEEE_FP)
/* IEEE fp overflow shenanigans are unavailable on VAX and optional
* on Alpha; fake it if we don't have them.
*/
# else
adouble = (double)anv;
# endif
-#endif /* __VOS__ */
- DO_BO_PACK_N(adouble, double);
- PUSH_VAR(utf8, cur, adouble);
+ PUSH_VAR(utf8, cur, adouble, needs_swap);
}
break;
case 'F': {
- NV anv;
+ NV_bytes anv;
Zero(&anv, 1, NV); /* can be long double with unused bits */
while (len-- > 0) {
fromstr = NEXTFROM;
- anv = SvNV(fromstr);
- DO_BO_PACK_N(anv, NV);
- PUSH_VAR(utf8, cur, anv);
+#ifdef __GNUC__
+ /* to work round a gcc/x86 bug; don't use SvNV */
+ anv.nv = sv_2nv(fromstr);
+#else
+ anv.nv = SvNV(fromstr);
+#endif
+ PUSH_BYTES(utf8, cur, anv.bytes, sizeof(anv.bytes), needs_swap);
}
break;
}
#if defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE)
case 'D': {
- long double aldouble;
+ ld_bytes aldouble;
/* long doubles can have unused bits, which may be nonzero */
Zero(&aldouble, 1, long double);
while (len-- > 0) {
fromstr = NEXTFROM;
- aldouble = (long double)SvNV(fromstr);
- DO_BO_PACK_N(aldouble, long double);
- PUSH_VAR(utf8, cur, aldouble);
+# ifdef __GNUC__
+ /* to work round a gcc/x86 bug; don't use SvNV */
+ aldouble.ld = (long double)sv_2nv(fromstr);
+# else
+ aldouble.ld = (long double)SvNV(fromstr);
+# endif
+ PUSH_BYTES(utf8, cur, aldouble.bytes, sizeof(aldouble.bytes),
+ needs_swap);
}
break;
}
#endif
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'n' | TYPE_IS_SHRIEKING:
-#endif
case 'n':
while (len-- > 0) {
I16 ai16;
fromstr = NEXTFROM;
- ai16 = (I16)SvIV(fromstr);
-#ifdef HAS_HTONS
+ ai16 = (I16)SvIV_no_inf(fromstr, datumtype);
ai16 = PerlSock_htons(ai16);
-#endif
- PUSH16(utf8, cur, &ai16);
+ PUSH16(utf8, cur, &ai16, FALSE);
}
break;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'v' | TYPE_IS_SHRIEKING:
-#endif
case 'v':
while (len-- > 0) {
I16 ai16;
fromstr = NEXTFROM;
- ai16 = (I16)SvIV(fromstr);
-#ifdef HAS_HTOVS
+ ai16 = (I16)SvIV_no_inf(fromstr, datumtype);
ai16 = htovs(ai16);
-#endif
- PUSH16(utf8, cur, &ai16);
+ PUSH16(utf8, cur, &ai16, FALSE);
}
break;
case 'S' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
unsigned short aushort;
fromstr = NEXTFROM;
- aushort = SvUV(fromstr);
- DO_BO_PACK(aushort, s);
- PUSH_VAR(utf8, cur, aushort);
+ aushort = SvUV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, aushort, needs_swap);
}
break;
#else
- /* Fall through! */
+ /* FALLTHROUGH */
#endif
case 'S':
while (len-- > 0) {
U16 au16;
fromstr = NEXTFROM;
- au16 = (U16)SvUV(fromstr);
- DO_BO_PACK(au16, 16);
- PUSH16(utf8, cur, &au16);
+ au16 = (U16)SvUV_no_inf(fromstr, datumtype);
+ PUSH16(utf8, cur, &au16, needs_swap);
}
break;
case 's' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
short ashort;
fromstr = NEXTFROM;
- ashort = SvIV(fromstr);
- DO_BO_PACK(ashort, s);
- PUSH_VAR(utf8, cur, ashort);
+ ashort = SvIV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, ashort, needs_swap);
}
break;
#else
- /* Fall through! */
+ /* FALLTHROUGH */
#endif
case 's':
while (len-- > 0) {
I16 ai16;
fromstr = NEXTFROM;
- ai16 = (I16)SvIV(fromstr);
- DO_BO_PACK(ai16, 16);
- PUSH16(utf8, cur, &ai16);
+ ai16 = (I16)SvIV_no_inf(fromstr, datumtype);
+ PUSH16(utf8, cur, &ai16, needs_swap);
}
break;
case 'I':
while (len-- > 0) {
unsigned int auint;
fromstr = NEXTFROM;
- auint = SvUV(fromstr);
- DO_BO_PACK(auint, i);
- PUSH_VAR(utf8, cur, auint);
+ auint = SvUV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, auint, needs_swap);
}
break;
case 'j':
while (len-- > 0) {
IV aiv;
fromstr = NEXTFROM;
- aiv = SvIV(fromstr);
-#if IVSIZE == INTSIZE
- DO_BO_PACK(aiv, i);
-#elif IVSIZE == LONGSIZE
- DO_BO_PACK(aiv, l);
-#elif defined(HAS_QUAD) && IVSIZE == U64SIZE
- DO_BO_PACK(aiv, 64);
-#else
- Perl_croak(aTHX_ "'j' not supported on this platform");
-#endif
- PUSH_VAR(utf8, cur, aiv);
+ aiv = SvIV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, aiv, needs_swap);
}
break;
case 'J':
while (len-- > 0) {
UV auv;
fromstr = NEXTFROM;
- auv = SvUV(fromstr);
-#if UVSIZE == INTSIZE
- DO_BO_PACK(auv, i);
-#elif UVSIZE == LONGSIZE
- DO_BO_PACK(auv, l);
-#elif defined(HAS_QUAD) && UVSIZE == U64SIZE
- DO_BO_PACK(auv, 64);
-#else
- Perl_croak(aTHX_ "'J' not supported on this platform");
-#endif
- PUSH_VAR(utf8, cur, auv);
+ auv = SvUV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, auv, needs_swap);
}
break;
case 'w':
while (len-- > 0) {
NV anv;
fromstr = NEXTFROM;
- anv = SvNV(fromstr);
+ S_sv_check_infnan(aTHX_ fromstr, datumtype);
+ anv = SvNV_nomg(fromstr);
if (anv < 0) {
*cur = '\0';
if (SvIOK(fromstr) || anv < UV_MAX_P1) {
char buf[(sizeof(UV)*CHAR_BIT)/7+1];
char *in = buf + sizeof(buf);
- UV auv = SvUV(fromstr);
+ UV auv = SvUV_nomg(fromstr);
do {
*--in = (char)((auv & 0x7f) | 0x80);
goto w_string;
else if (SvNOKp(fromstr)) {
/* 10**NV_MAX_10_EXP is the largest power of 10
- so 10**(NV_MAX_10_EXP+1) is definately unrepresentable
+ so 10**(NV_MAX_10_EXP+1) is definitely unrepresentable
given 10**(NV_MAX_10_EXP+1) == 128 ** x solve for x:
x = (NV_MAX_10_EXP+1) * log (10) / log (128)
And with that many bytes only Inf can overflow.
w_string:
/* Copy string and check for compliance */
- from = SvPV_const(fromstr, len);
+ from = SvPV_nomg_const(fromstr, len);
if ((norm = is_an_int(from, len)) == NULL)
Perl_croak(aTHX_ "Can only compress unsigned integers in pack");
while (len-- > 0) {
int aint;
fromstr = NEXTFROM;
- aint = SvIV(fromstr);
- DO_BO_PACK(aint, i);
- PUSH_VAR(utf8, cur, aint);
+ aint = SvIV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, aint, needs_swap);
}
break;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'N' | TYPE_IS_SHRIEKING:
-#endif
case 'N':
while (len-- > 0) {
U32 au32;
fromstr = NEXTFROM;
- au32 = SvUV(fromstr);
-#ifdef HAS_HTONL
+ au32 = SvUV_no_inf(fromstr, datumtype);
au32 = PerlSock_htonl(au32);
-#endif
- PUSH32(utf8, cur, &au32);
+ PUSH32(utf8, cur, &au32, FALSE);
}
break;
-#ifdef PERL_PACK_CAN_SHRIEKSIGN
case 'V' | TYPE_IS_SHRIEKING:
-#endif
case 'V':
while (len-- > 0) {
U32 au32;
fromstr = NEXTFROM;
- au32 = SvUV(fromstr);
-#ifdef HAS_HTOVL
+ au32 = SvUV_no_inf(fromstr, datumtype);
au32 = htovl(au32);
-#endif
- PUSH32(utf8, cur, &au32);
+ PUSH32(utf8, cur, &au32, FALSE);
}
break;
case 'L' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
unsigned long aulong;
fromstr = NEXTFROM;
- aulong = SvUV(fromstr);
- DO_BO_PACK(aulong, l);
- PUSH_VAR(utf8, cur, aulong);
+ aulong = SvUV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, aulong, needs_swap);
}
break;
#else
while (len-- > 0) {
U32 au32;
fromstr = NEXTFROM;
- au32 = SvUV(fromstr);
- DO_BO_PACK(au32, 32);
- PUSH32(utf8, cur, &au32);
+ au32 = SvUV_no_inf(fromstr, datumtype);
+ PUSH32(utf8, cur, &au32, needs_swap);
}
break;
case 'l' | TYPE_IS_SHRIEKING:
while (len-- > 0) {
long along;
fromstr = NEXTFROM;
- along = SvIV(fromstr);
- DO_BO_PACK(along, l);
- PUSH_VAR(utf8, cur, along);
+ along = SvIV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, along, needs_swap);
}
break;
#else
while (len-- > 0) {
I32 ai32;
fromstr = NEXTFROM;
- ai32 = SvIV(fromstr);
- DO_BO_PACK(ai32, 32);
- PUSH32(utf8, cur, &ai32);
+ ai32 = SvIV_no_inf(fromstr, datumtype);
+ PUSH32(utf8, cur, &ai32, needs_swap);
}
break;
-#ifdef HAS_QUAD
+#if defined(HAS_QUAD) && IVSIZE >= 8
case 'Q':
while (len-- > 0) {
Uquad_t auquad;
fromstr = NEXTFROM;
- auquad = (Uquad_t) SvUV(fromstr);
- DO_BO_PACK(auquad, 64);
- PUSH_VAR(utf8, cur, auquad);
+ auquad = (Uquad_t) SvUV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, auquad, needs_swap);
}
break;
case 'q':
while (len-- > 0) {
Quad_t aquad;
fromstr = NEXTFROM;
- aquad = (Quad_t)SvIV(fromstr);
- DO_BO_PACK(aquad, 64);
- PUSH_VAR(utf8, cur, aquad);
+ aquad = (Quad_t)SvIV_no_inf(fromstr, datumtype);
+ PUSH_VAR(utf8, cur, aquad, needs_swap);
}
break;
-#endif /* HAS_QUAD */
+#endif
case 'P':
len = 1; /* assume SV is correct length */
GROWING(utf8, cat, start, cur, sizeof(char *));
- /* Fall through! */
+ /* FALLTHROUGH */
case 'p':
while (len-- > 0) {
const char *aptr;
* gone.
*/
if ((SvTEMP(fromstr) || (SvPADTMP(fromstr) &&
- !SvREADONLY(fromstr))) && ckWARN(WARN_PACK)) {
- Perl_warner(aTHX_ packWARN(WARN_PACK),
- "Attempt to pack pointer to temporary value");
+ !SvREADONLY(fromstr)))) {
+ Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
+ "Attempt to pack pointer to temporary value");
}
if (SvPOK(fromstr) || SvNIOK(fromstr))
aptr = SvPV_nomg_const_nolen(fromstr);
else
aptr = SvPV_force_flags_nolen(fromstr, 0);
}
- DO_BO_PACK_PC(aptr);
- PUSH_VAR(utf8, cur, aptr);
+ PUSH_VAR(utf8, cur, aptr, needs_swap);
}
break;
case 'u': {
if (len <= 2) len = 45;
else len = len / 3 * 3;
if (len >= 64) {
- if (ckWARN(WARN_PACK))
- Perl_warner(aTHX_ packWARN(WARN_PACK),
- "Field too wide in 'u' format in pack");
+ Perl_ck_warner(aTHX_ packWARN(WARN_PACK),
+ "Field too wide in 'u' format in pack");
len = 63;
}
aptr = SvPV_const(fromstr, fromlen);
from_utf8 = DO_UTF8(fromstr);
if (from_utf8) {
aend = aptr + fromlen;
- fromlen = sv_len_utf8(fromstr);
+ fromlen = sv_len_utf8_nomg(fromstr);
} else aend = NULL; /* Unused, but keep compilers happy */
GROWING(utf8, cat, start, cur, (fromlen+2) / 3 * 4 + (fromlen+len-1)/len * 2);
while (fromlen > 0) {
'u' | TYPE_IS_PACK)) {
*cur = '\0';
SvCUR_set(cat, cur - start);
- Perl_croak(aTHX_ "Assertion: string is shorter than advertised");
+ Perl_croak(aTHX_ "panic: string is shorter than advertised, "
+ "aptr=%p, aend=%p, buffer=%p, todo=%ld",
+ aptr, aend, buffer, (long) todo);
}
end = doencodes(hunk, buffer, todo);
} else {
end = doencodes(hunk, aptr, todo);
aptr += todo;
}
- PUSH_BYTES(utf8, cur, hunk, end-hunk);
+ PUSH_BYTES(utf8, cur, hunk, end-hunk, 0);
fromlen -= todo;
}
break;
PP(pp_pack)
{
- dVAR; dSP; dMARK; dORIGMARK; dTARGET;
- register SV *cat = TARG;
+ dSP; dMARK; dORIGMARK; dTARGET;
+ SV *cat = TARG;
STRLEN fromlen;
SV *pat_sv = *++MARK;
- register const char *pat = SvPV_const(pat_sv, fromlen);
- register const char *patend = pat + fromlen;
+ const char *pat = SvPV_const(pat_sv, fromlen);
+ const char *patend = pat + fromlen;
MARK++;
- sv_setpvn(cat, "", 0);
+ sv_setpvs(cat, "");
SvUTF8_off(cat);
packlist(cat, pat, patend, MARK, SP + 1);
* Local variables:
* c-indentation-style: bsd
* c-basic-offset: 4
- * indent-tabs-mode: t
+ * indent-tabs-mode: nil
* End:
*
- * ex: set ts=8 sts=4 sw=4 noet:
+ * ex: set ts=8 sts=4 sw=4 et:
*/