php-src/ext/standard/pack.c

1149 lines
27 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Author: Chris Schneider <cschneid@relog.ch> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-04-23 20:06:01 +00:00
#include "php.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
#define O_RDONLY _O_RDONLY
#include "win32/param.h"
2002-09-06 07:44:30 +00:00
#elif defined(NETWARE)
#ifdef USE_WINSOCK
#include <novsock2.h>
#else
#include <sys/socket.h>
#endif
#include <sys/param.h>
#else
#include <sys/param.h>
#endif
#include "ext/standard/head.h"
#include "php_string.h"
#include "pack.h"
#if HAVE_PWD_H
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
#include "win32/pwd.h"
#else
#include <pwd.h>
#endif
#endif
#include "fsock.h"
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#define INC_OUTPUTPOS(a,b) \
2006-02-26 10:57:00 +00:00
if ((a) < 0 || ((INT_MAX - outputpos)/((int)b)) < (a)) { \
efree(formatcodes); \
efree(formatargs); \
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: integer overflow in format string", code); \
RETURN_FALSE; \
} \
outputpos += (a)*(b);
/* Whether machine is little endian */
char machine_little_endian;
/* Mapping of byte from char (8bit) to long for machine endian */
static int byte_map[1];
2013-07-13 12:37:04 +00:00
/* Mappings of bytes from int (machine dependent) to int for machine endian */
static int int_map[sizeof(int)];
/* Mappings of bytes from shorts (16bit) for all endian environments */
static int machine_endian_short_map[2];
static int big_endian_short_map[2];
static int little_endian_short_map[2];
/* Mappings of bytes from longs (32bit) for all endian environments */
static int machine_endian_long_map[4];
static int big_endian_long_map[4];
static int little_endian_long_map[4];
#if SIZEOF_ZEND_LONG > 4
/* Mappings of bytes from quads (64bit) for all endian environments */
static int machine_endian_longlong_map[8];
static int big_endian_longlong_map[8];
static int little_endian_longlong_map[8];
#endif
/* {{{ php_pack
*/
2014-08-25 18:22:49 +00:00
static void php_pack(zval *val, size_t size, int *map, char *output)
{
2016-06-23 09:47:06 +00:00
size_t i;
char *v;
2014-08-25 19:51:49 +00:00
convert_to_long_ex(val);
2014-08-25 17:24:55 +00:00
v = (char *) &Z_LVAL_P(val);
for (i = 0; i < size; i++) {
*output++ = v[map[i]];
}
}
/* }}} */
/* pack() idea stolen from Perl (implemented formats behave the same as there except J and P)
* Implemented formats are Z, A, a, h, H, c, C, s, S, i, I, l, L, n, N, q, Q, J, P, f, d, x, X, @.
*/
2000-07-09 14:26:50 +00:00
/* {{{ proto string pack(string format, mixed arg1 [, mixed arg2 [, mixed ...]])
Takes one or more arguments and packs them into a binary string according to the format argument */
PHP_FUNCTION(pack)
{
zval *argv = NULL;
2016-06-23 09:47:06 +00:00
int num_args = 0;
size_t i;
int currentarg;
char *format;
2015-09-09 01:58:37 +00:00
size_t formatlen;
char *formatcodes;
int *formatargs;
2016-06-23 09:47:06 +00:00
size_t formatcount = 0;
int outputpos = 0, outputsize = 0;
2015-07-01 11:59:58 +00:00
zend_string *output;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s*", &format, &formatlen, &argv, &num_args) == FAILURE) {
2008-06-20 20:54:32 +00:00
return;
}
/* We have a maximum of <formatlen> format codes to deal with */
2003-08-11 23:16:54 +00:00
formatcodes = safe_emalloc(formatlen, sizeof(*formatcodes), 0);
formatargs = safe_emalloc(formatlen, sizeof(*formatargs), 0);
2015-09-09 01:58:37 +00:00
currentarg = 0;
/* Preprocess format into formatcodes and formatargs */
for (i = 0; i < formatlen; formatcount++) {
char code = format[i++];
int arg = 1;
/* Handle format arguments if any */
if (i < formatlen) {
char c = format[i];
if (c == '*') {
arg = -1;
i++;
}
else if (c >= '0' && c <= '9') {
arg = atoi(&format[i]);
2015-01-03 09:22:58 +00:00
while (format[i] >= '0' && format[i] <= '9' && i < formatlen) {
i++;
}
}
}
/* Handle special arg '*' for all codes and check argv overflows */
switch ((int) code) {
/* Never uses any args */
2015-01-03 09:22:58 +00:00
case 'x':
case 'X':
case '@':
if (arg < 0) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: '*' ignored", code);
arg = 1;
}
break;
/* Always uses one arg */
2015-01-03 09:22:58 +00:00
case 'a':
case 'A':
case 'Z':
case 'h':
case 'H':
2008-06-20 20:54:32 +00:00
if (currentarg >= num_args) {
efree(formatcodes);
efree(formatargs);
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: not enough arguments", code);
RETURN_FALSE;
}
if (arg < 0) {
2015-09-09 01:58:37 +00:00
convert_to_string(&argv[currentarg]);
2014-08-25 17:24:55 +00:00
arg = Z_STRLEN(argv[currentarg]);
if (code == 'Z') {
/* add one because Z is always NUL-terminated:
* pack("Z*", "aa") === "aa\0"
* pack("Z2", "aa") === "a\0" */
arg++;
}
}
currentarg++;
break;
/* Use as many args as specified */
case 'q':
case 'Q':
case 'J':
case 'P':
#if SIZEOF_ZEND_LONG < 8
efree(formatcodes);
efree(formatargs);
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "64-bit format codes are not available for 32-bit versions of PHP");
RETURN_FALSE;
#endif
2015-01-03 09:22:58 +00:00
case 'c':
case 'C':
case 's':
case 'S':
case 'i':
case 'I':
2015-01-03 09:22:58 +00:00
case 'l':
case 'L':
case 'n':
case 'N':
case 'v':
case 'V':
2015-01-03 09:22:58 +00:00
case 'f':
case 'd':
if (arg < 0) {
2008-06-20 20:54:32 +00:00
arg = num_args - currentarg;
}
currentarg += arg;
2008-06-20 20:54:32 +00:00
if (currentarg > num_args) {
efree(formatcodes);
efree(formatargs);
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: too few arguments", code);
RETURN_FALSE;
}
break;
default:
efree(formatcodes);
efree(formatargs);
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: unknown format code", code);
RETURN_FALSE;
}
formatcodes[formatcount] = code;
formatargs[formatcount] = arg;
}
2008-06-20 20:54:32 +00:00
if (currentarg < num_args) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "%d arguments unused", (num_args - currentarg));
}
/* Calculate output length and upper bound while processing*/
for (i = 0; i < formatcount; i++) {
int code = (int) formatcodes[i];
int arg = formatargs[i];
switch ((int) code) {
2015-01-03 09:22:58 +00:00
case 'h':
case 'H':
INC_OUTPUTPOS((arg + (arg % 2)) / 2,1) /* 4 bit per arg */
break;
2015-01-03 09:22:58 +00:00
case 'a':
case 'A':
case 'Z':
2015-01-03 09:22:58 +00:00
case 'c':
case 'C':
case 'x':
INC_OUTPUTPOS(arg,1) /* 8 bit per arg */
break;
2015-01-03 09:22:58 +00:00
case 's':
case 'S':
case 'n':
case 'v':
INC_OUTPUTPOS(arg,2) /* 16 bit per arg */
break;
2015-01-03 09:22:58 +00:00
case 'i':
case 'I':
INC_OUTPUTPOS(arg,sizeof(int))
break;
2015-01-03 09:22:58 +00:00
case 'l':
case 'L':
case 'N':
case 'V':
INC_OUTPUTPOS(arg,4) /* 32 bit per arg */
break;
#if SIZEOF_ZEND_LONG > 4
case 'q':
case 'Q':
case 'J':
case 'P':
INC_OUTPUTPOS(arg,8) /* 32 bit per arg */
break;
#endif
case 'f':
INC_OUTPUTPOS(arg,sizeof(float))
break;
case 'd':
INC_OUTPUTPOS(arg,sizeof(double))
break;
case 'X':
outputpos -= arg;
if (outputpos < 0) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: outside of string", code);
outputpos = 0;
}
break;
case '@':
outputpos = arg;
break;
}
if (outputsize < outputpos) {
outputsize = outputpos;
}
}
2015-07-01 11:59:58 +00:00
output = zend_string_alloc(outputsize, 0);
outputpos = 0;
2015-09-09 01:58:37 +00:00
currentarg = 0;
/* Do actual packing */
for (i = 0; i < formatcount; i++) {
int code = (int) formatcodes[i];
int arg = formatargs[i];
switch ((int) code) {
2015-01-03 09:22:58 +00:00
case 'a':
case 'A':
case 'Z': {
2016-06-23 09:47:06 +00:00
size_t arg_cp = (code != 'Z') ? arg : MAX(0, arg - 1);
2014-04-25 12:41:12 +00:00
zend_string *str = zval_get_string(&argv[currentarg++]);
2015-07-01 11:59:58 +00:00
memset(&ZSTR_VAL(output)[outputpos], (code == 'a' || code == 'Z') ? '\0' : ' ', arg);
memcpy(&ZSTR_VAL(output)[outputpos], ZSTR_VAL(str),
(ZSTR_LEN(str) < arg_cp) ? ZSTR_LEN(str) : arg_cp);
2014-04-25 12:41:12 +00:00
outputpos += arg;
2014-08-25 17:24:55 +00:00
zend_string_release(str);
break;
}
2015-01-03 09:22:58 +00:00
case 'h':
case 'H': {
int nibbleshift = (code == 'h') ? 0 : 4;
int first = 1;
2014-04-25 12:41:12 +00:00
zend_string *str = zval_get_string(&argv[currentarg++]);
char *v = ZSTR_VAL(str);
2014-04-25 12:41:12 +00:00
outputpos--;
2016-06-23 09:47:06 +00:00
if ((size_t)arg > ZSTR_LEN(str)) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: not enough characters in string", code);
arg = ZSTR_LEN(str);
}
while (arg-- > 0) {
char n = *v++;
if (n >= '0' && n <= '9') {
n -= '0';
} else if (n >= 'A' && n <= 'F') {
n -= ('A' - 10);
} else if (n >= 'a' && n <= 'f') {
n -= ('a' - 10);
} else {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: illegal hex digit %c", code, n);
n = 0;
}
if (first--) {
2015-07-01 11:59:58 +00:00
ZSTR_VAL(output)[++outputpos] = 0;
} else {
first = 1;
}
2015-07-01 11:59:58 +00:00
ZSTR_VAL(output)[outputpos] |= (n << nibbleshift);
nibbleshift = (nibbleshift + 4) & 7;
}
outputpos++;
2014-08-25 17:24:55 +00:00
zend_string_release(str);
break;
}
2015-01-03 09:22:58 +00:00
case 'c':
case 'C':
while (arg-- > 0) {
2015-07-01 11:59:58 +00:00
php_pack(&argv[currentarg++], 1, byte_map, &ZSTR_VAL(output)[outputpos]);
outputpos++;
}
break;
2015-01-03 09:22:58 +00:00
case 's':
case 'S':
case 'n':
case 'v': {
int *map = machine_endian_short_map;
if (code == 'n') {
map = big_endian_short_map;
} else if (code == 'v') {
map = little_endian_short_map;
}
while (arg-- > 0) {
2015-07-01 11:59:58 +00:00
php_pack(&argv[currentarg++], 2, map, &ZSTR_VAL(output)[outputpos]);
outputpos += 2;
}
break;
}
2015-01-03 09:22:58 +00:00
case 'i':
case 'I':
while (arg-- > 0) {
2015-07-01 11:59:58 +00:00
php_pack(&argv[currentarg++], sizeof(int), int_map, &ZSTR_VAL(output)[outputpos]);
outputpos += sizeof(int);
}
break;
2015-01-03 09:22:58 +00:00
case 'l':
case 'L':
case 'N':
case 'V': {
int *map = machine_endian_long_map;
if (code == 'N') {
map = big_endian_long_map;
} else if (code == 'V') {
map = little_endian_long_map;
}
while (arg-- > 0) {
2015-07-01 11:59:58 +00:00
php_pack(&argv[currentarg++], 4, map, &ZSTR_VAL(output)[outputpos]);
outputpos += 4;
}
break;
}
#if SIZEOF_ZEND_LONG > 4
case 'q':
case 'Q':
case 'J':
case 'P': {
int *map = machine_endian_longlong_map;
if (code == 'J') {
map = big_endian_longlong_map;
} else if (code == 'P') {
map = little_endian_longlong_map;
}
while (arg-- > 0) {
2015-07-01 11:59:58 +00:00
php_pack(&argv[currentarg++], 8, map, &ZSTR_VAL(output)[outputpos]);
outputpos += 8;
}
break;
}
#endif
case 'f': {
while (arg-- > 0) {
2014-04-25 12:41:12 +00:00
float v = (float) zval_get_double(&argv[currentarg++]);
2015-07-01 11:59:58 +00:00
memcpy(&ZSTR_VAL(output)[outputpos], &v, sizeof(v));
outputpos += sizeof(v);
}
break;
}
case 'd': {
while (arg-- > 0) {
2014-04-25 12:41:12 +00:00
double v = (double) zval_get_double(&argv[currentarg++]);
2015-07-01 11:59:58 +00:00
memcpy(&ZSTR_VAL(output)[outputpos], &v, sizeof(v));
outputpos += sizeof(v);
}
break;
}
case 'x':
2015-07-01 11:59:58 +00:00
memset(&ZSTR_VAL(output)[outputpos], '\0', arg);
outputpos += arg;
break;
case 'X':
outputpos -= arg;
if (outputpos < 0) {
outputpos = 0;
}
break;
case '@':
if (arg > outputpos) {
2015-07-01 11:59:58 +00:00
memset(&ZSTR_VAL(output)[outputpos], '\0', arg - outputpos);
}
outputpos = arg;
break;
}
}
efree(formatcodes);
efree(formatargs);
2015-07-01 11:59:58 +00:00
ZSTR_VAL(output)[outputpos] = '\0';
ZSTR_LEN(output) = outputpos;
RETURN_NEW_STR(output);
}
/* }}} */
/* {{{ php_unpack
*/
2014-08-25 18:22:49 +00:00
static zend_long php_unpack(char *data, size_t size, int issigned, int *map)
{
2014-08-25 17:24:55 +00:00
zend_long result;
char *cresult = (char *) &result;
2016-06-23 09:47:06 +00:00
size_t i;
result = issigned ? -1 : 0;
for (i = 0; i < size; i++) {
cresult[map[i]] = *data++;
}
return result;
}
/* }}} */
/* unpack() is based on Perl's unpack(), but is modified a bit from there.
* Rather than depending on error-prone ordered lists or syntactically
2015-01-03 09:22:58 +00:00
* unpleasant pass-by-reference, we return an object with named parameters
* (like *_fetch_object()). Syntax is "f[repeat]name/...", where "f" is the
2000-09-10 11:03:32 +00:00
* formatter char (like pack()), "[repeat]" is the optional repeater argument,
* and "name" is the name of the variable to use.
* Example: "c2chars/nints" will return an object with fields
* chars1, chars2, and ints.
* Numeric pack types will return numbers, a and A will return strings,
* f and d will return doubles.
* Implemented formats are Z, A, a, h, H, c, C, s, S, i, I, l, L, n, N, q, Q, J, P, f, d, x, X, @.
*/
/* {{{ proto array unpack(string format, string input)
Unpack binary string into named array elements according to format argument */
PHP_FUNCTION(unpack)
{
char *format, *input;
zend_string *formatarg, *inputarg;
zend_long formatlen, inputpos, inputlen;
int i;
zend_long offset = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &formatarg,
&inputarg, &offset) == FAILURE) {
2008-06-20 14:53:57 +00:00
return;
}
format = ZSTR_VAL(formatarg);
formatlen = ZSTR_LEN(formatarg);
input = ZSTR_VAL(inputarg);
inputlen = ZSTR_LEN(inputarg);
inputpos = 0;
if (offset < 0 || offset > inputlen) {
php_error_docref(NULL, E_WARNING, "Offset " ZEND_LONG_FMT " is out of input range" , offset);
RETURN_FALSE;
}
input += offset;
inputlen -= offset;
array_init(return_value);
while (formatlen-- > 0) {
char type = *(format++);
char c;
int arg = 1, argb;
char *name;
int namelen;
int size=0;
/* Handle format arguments if any */
if (formatlen > 0) {
c = *format;
if (c >= '0' && c <= '9') {
arg = atoi(format);
while (formatlen > 0 && *format >= '0' && *format <= '9') {
format++;
formatlen--;
}
} else if (c == '*') {
arg = -1;
format++;
formatlen--;
}
}
/* Get of new value in array */
name = format;
argb = arg;
while (formatlen > 0 && *format != '/') {
formatlen--;
format++;
}
namelen = format - name;
if (namelen > 200)
namelen = 200;
switch ((int) type) {
/* Never use any input */
2015-01-03 09:22:58 +00:00
case 'X':
size = -1;
break;
case '@':
size = 0;
break;
2015-01-03 09:22:58 +00:00
case 'a':
case 'A':
case 'Z':
size = arg;
arg = 1;
break;
2015-01-03 09:22:58 +00:00
case 'h':
case 'H':
size = (arg > 0) ? (arg + (arg % 2)) / 2 : arg;
2000-09-10 13:47:51 +00:00
arg = 1;
break;
/* Use 1 byte of input */
2015-01-03 09:22:58 +00:00
case 'c':
case 'C':
case 'x':
size = 1;
break;
/* Use 2 bytes of input */
2015-01-03 09:22:58 +00:00
case 's':
case 'S':
case 'n':
case 'v':
size = 2;
break;
/* Use sizeof(int) bytes of input */
2015-01-03 09:22:58 +00:00
case 'i':
case 'I':
size = sizeof(int);
break;
/* Use 4 bytes of input */
2015-01-03 09:22:58 +00:00
case 'l':
case 'L':
case 'N':
case 'V':
size = 4;
break;
/* Use 8 bytes of input */
case 'q':
case 'Q':
case 'J':
case 'P':
#if SIZEOF_ZEND_LONG > 4
size = 8;
break;
#else
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "64-bit format codes are not available for 32-bit versions of PHP");
zval_dtor(return_value);
RETURN_FALSE;
#endif
/* Use sizeof(float) bytes of input */
case 'f':
size = sizeof(float);
break;
/* Use sizeof(double) bytes of input */
case 'd':
size = sizeof(double);
break;
default:
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Invalid format type %c", type);
zval_dtor(return_value);
RETURN_FALSE;
break;
}
2015-05-11 08:10:35 +00:00
if (size != 0 && size != -1 && size < 0) {
php_error_docref(NULL, E_WARNING, "Type %c: integer overflow", type);
2015-05-11 08:10:35 +00:00
zval_dtor(return_value);
RETURN_FALSE;
}
/* Do actual unpacking */
for (i = 0; i != arg; i++ ) {
/* Space for name + number, safe as namelen is ensured <= 200 */
char n[256];
2004-02-24 21:49:28 +00:00
if (arg != 1 || namelen == 0) {
/* Need to add element number to name */
snprintf(n, sizeof(n), "%.*s%d", namelen, name, i + 1);
} else {
/* Truncate name to next format code or end of string */
snprintf(n, sizeof(n), "%.*s", namelen, name);
}
if (size != 0 && size != -1 && INT_MAX - size + 1 < inputpos) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: integer overflow", type);
zval_dtor(return_value);
RETURN_FALSE;
}
if ((inputpos + size) <= inputlen) {
switch ((int) type) {
case 'a': {
/* a will not strip any trailing whitespace or null padding */
2016-06-23 09:47:06 +00:00
zend_long len = inputlen - inputpos; /* Remaining string */
/* If size was given take minimum of len and size */
if ((size >= 0) && (len > size)) {
len = size;
}
size = len;
2014-04-15 11:40:40 +00:00
add_assoc_stringl(return_value, n, &input[inputpos], len);
break;
}
case 'A': {
/* A will strip any trailing whitespace */
char padn = '\0'; char pads = ' '; char padt = '\t'; char padc = '\r'; char padl = '\n';
2015-01-08 08:20:20 +00:00
zend_long len = inputlen - inputpos; /* Remaining string */
/* If size was given take minimum of len and size */
if ((size >= 0) && (len > size)) {
len = size;
}
size = len;
/* Remove trailing white space and nulls chars from unpacked data */
while (--len >= 0) {
if (input[inputpos + len] != padn
&& input[inputpos + len] != pads
&& input[inputpos + len] != padt
&& input[inputpos + len] != padc
&& input[inputpos + len] != padl
)
break;
}
2014-04-15 11:40:40 +00:00
add_assoc_stringl(return_value, n, &input[inputpos], len + 1);
break;
}
/* New option added for Z to remain in-line with the Perl implementation */
case 'Z': {
/* Z will strip everything after the first null character */
char pad = '\0';
2016-06-23 09:47:06 +00:00
zend_long s,
2012-04-28 15:32:44 +00:00
len = inputlen - inputpos; /* Remaining string */
/* If size was given take minimum of len and size */
if ((size >= 0) && (len > size)) {
len = size;
}
size = len;
/* Remove everything after the first null */
for (s=0 ; s < len ; s++) {
if (input[inputpos + s] == pad)
break;
}
len = s;
2014-04-15 11:40:40 +00:00
add_assoc_stringl(return_value, n, &input[inputpos], len);
break;
}
2015-01-03 09:22:58 +00:00
case 'h':
case 'H': {
2016-06-23 09:47:06 +00:00
zend_long len = (inputlen - inputpos) * 2; /* Remaining */
int nibbleshift = (type == 'h') ? 0 : 4;
int first = 1;
char *buf;
2016-06-23 09:47:06 +00:00
zend_long ipos, opos;
/* If size was given take minimum of len and size */
if (size >= 0 && len > (size * 2)) {
len = size * 2;
2015-01-03 09:22:58 +00:00
}
if (len > 0 && argb > 0) {
len -= argb % 2;
}
buf = emalloc(len + 1);
for (ipos = opos = 0; opos < len; opos++) {
2009-08-24 18:40:13 +00:00
char cc = (input[inputpos + ipos] >> nibbleshift) & 0xf;
2009-08-24 18:40:13 +00:00
if (cc < 10) {
cc += '0';
} else {
2009-08-24 18:40:13 +00:00
cc += 'a' - 10;
}
2009-08-24 18:40:13 +00:00
buf[opos] = cc;
nibbleshift = (nibbleshift + 4) & 7;
if (first-- == 0) {
ipos++;
first = 1;
}
}
buf[len] = '\0';
2014-04-15 11:40:40 +00:00
add_assoc_stringl(return_value, n, buf, len);
efree(buf);
break;
}
2015-01-03 09:22:58 +00:00
case 'c':
case 'C': {
int issigned = (type == 'c') ? (input[inputpos] & 0x80) : 0;
2014-08-25 17:24:55 +00:00
zend_long v = php_unpack(&input[inputpos], 1, issigned, byte_map);
add_assoc_long(return_value, n, v);
break;
}
2015-01-03 09:22:58 +00:00
case 's':
case 'S':
case 'n':
case 'v': {
2014-08-25 17:24:55 +00:00
zend_long v;
int issigned = 0;
int *map = machine_endian_short_map;
if (type == 's') {
issigned = input[inputpos + (machine_little_endian ? 1 : 0)] & 0x80;
} else if (type == 'n') {
map = big_endian_short_map;
} else if (type == 'v') {
map = little_endian_short_map;
}
1999-12-18 04:01:20 +00:00
v = php_unpack(&input[inputpos], 2, issigned, map);
2014-08-25 17:24:55 +00:00
add_assoc_long(return_value, n, v);
break;
}
2015-01-03 09:22:58 +00:00
case 'i':
case 'I': {
2014-08-25 17:24:55 +00:00
zend_long v;
int issigned = 0;
if (type == 'i') {
issigned = input[inputpos + (machine_little_endian ? (sizeof(int) - 1) : 0)] & 0x80;
}
v = php_unpack(&input[inputpos], sizeof(int), issigned, int_map);
2014-08-25 17:24:55 +00:00
add_assoc_long(return_value, n, v);
break;
}
2015-01-03 09:22:58 +00:00
case 'l':
case 'L':
case 'N':
case 'V': {
int issigned = 0;
int *map = machine_endian_long_map;
2014-08-25 17:24:55 +00:00
zend_long v = 0;
if (type == 'l' || type == 'L') {
issigned = input[inputpos + (machine_little_endian ? 3 : 0)] & 0x80;
} else if (type == 'N') {
issigned = input[inputpos] & 0x80;
map = big_endian_long_map;
} else if (type == 'V') {
issigned = input[inputpos + 3] & 0x80;
map = little_endian_long_map;
}
2014-08-25 19:20:44 +00:00
if (SIZEOF_ZEND_LONG > 4 && issigned) {
v = ~INT_MAX;
}
v |= php_unpack(&input[inputpos], 4, issigned, map);
2014-08-25 19:20:44 +00:00
if (SIZEOF_ZEND_LONG > 4) {
if (type == 'l') {
2015-01-03 09:22:58 +00:00
v = (signed int) v;
} else {
v = (unsigned int) v;
}
}
2014-08-25 17:24:55 +00:00
add_assoc_long(return_value, n, v);
break;
}
#if SIZEOF_ZEND_LONG > 4
case 'q':
case 'Q':
case 'J':
case 'P': {
int issigned = 0;
int *map = machine_endian_longlong_map;
2014-10-23 12:36:55 +00:00
zend_long v = 0;
if (type == 'q' || type == 'Q') {
issigned = input[inputpos + (machine_little_endian ? 7 : 0)] & 0x80;
} else if (type == 'J') {
issigned = input[inputpos] & 0x80;
map = big_endian_longlong_map;
} else if (type == 'P') {
issigned = input[inputpos + 7] & 0x80;
map = little_endian_longlong_map;
}
v = php_unpack(&input[inputpos], 8, issigned, map);
if (type == 'q') {
2014-10-23 12:36:55 +00:00
v = (zend_long) v;
} else {
2014-10-23 12:36:55 +00:00
v = (zend_ulong) v;
}
add_assoc_long(return_value, n, v);
break;
}
#endif
case 'f': {
float v;
memcpy(&v, &input[inputpos], sizeof(float));
add_assoc_double(return_value, n, (double)v);
break;
}
case 'd': {
double v;
2000-09-10 11:03:32 +00:00
memcpy(&v, &input[inputpos], sizeof(double));
add_assoc_double(return_value, n, v);
break;
}
case 'x':
/* Do nothing with input, just skip it */
break;
case 'X':
if (inputpos < size) {
inputpos = -size;
i = arg - 1; /* Break out of for loop */
if (arg >= 0) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: outside of string", type);
}
}
break;
case '@':
if (arg <= inputlen) {
inputpos = arg;
} else {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: outside of string", type);
}
i = arg - 1; /* Done, break out of for loop */
break;
}
inputpos += size;
if (inputpos < 0) {
if (size != -1) { /* only print warning if not working with * */
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "Type %c: outside of string", type);
}
inputpos = 0;
}
} else if (arg < 0) {
/* Reached end of input for '*' repeater */
break;
} else {
php_error_docref(NULL, E_WARNING, "Type %c: not enough input, need %d, have " ZEND_LONG_FMT, type, size, inputlen - inputpos);
zval_dtor(return_value);
RETURN_FALSE;
}
}
if (formatlen > 0) {
formatlen--; /* Skip '/' separator, does no harm if inputlen == 0 */
format++;
}
}
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(pack)
{
int machine_endian_check = 1;
int i;
machine_little_endian = ((char *)&machine_endian_check)[0];
if (machine_little_endian) {
/* Where to get lo to hi bytes from */
byte_map[0] = 0;
2003-09-26 08:09:56 +00:00
for (i = 0; i < (int)sizeof(int); i++) {
int_map[i] = i;
}
machine_endian_short_map[0] = 0;
machine_endian_short_map[1] = 1;
big_endian_short_map[0] = 1;
big_endian_short_map[1] = 0;
little_endian_short_map[0] = 0;
little_endian_short_map[1] = 1;
machine_endian_long_map[0] = 0;
machine_endian_long_map[1] = 1;
machine_endian_long_map[2] = 2;
machine_endian_long_map[3] = 3;
big_endian_long_map[0] = 3;
big_endian_long_map[1] = 2;
big_endian_long_map[2] = 1;
big_endian_long_map[3] = 0;
little_endian_long_map[0] = 0;
little_endian_long_map[1] = 1;
little_endian_long_map[2] = 2;
little_endian_long_map[3] = 3;
#if SIZEOF_ZEND_LONG > 4
machine_endian_longlong_map[0] = 0;
machine_endian_longlong_map[1] = 1;
machine_endian_longlong_map[2] = 2;
machine_endian_longlong_map[3] = 3;
machine_endian_longlong_map[4] = 4;
machine_endian_longlong_map[5] = 5;
machine_endian_longlong_map[6] = 6;
machine_endian_longlong_map[7] = 7;
big_endian_longlong_map[0] = 7;
big_endian_longlong_map[1] = 6;
big_endian_longlong_map[2] = 5;
big_endian_longlong_map[3] = 4;
big_endian_longlong_map[4] = 3;
big_endian_longlong_map[5] = 2;
big_endian_longlong_map[6] = 1;
big_endian_longlong_map[7] = 0;
little_endian_longlong_map[0] = 0;
little_endian_longlong_map[1] = 1;
little_endian_longlong_map[2] = 2;
little_endian_longlong_map[3] = 3;
little_endian_longlong_map[4] = 4;
little_endian_longlong_map[5] = 5;
little_endian_longlong_map[6] = 6;
little_endian_longlong_map[7] = 7;
#endif
}
else {
zval val;
2014-08-25 17:24:55 +00:00
int size = sizeof(Z_LVAL(val));
Z_LVAL(val)=0; /*silence a warning*/
/* Where to get hi to lo bytes from */
byte_map[0] = size - 1;
2003-09-26 08:09:56 +00:00
for (i = 0; i < (int)sizeof(int); i++) {
int_map[i] = size - (sizeof(int) - i);
}
machine_endian_short_map[0] = size - 2;
machine_endian_short_map[1] = size - 1;
big_endian_short_map[0] = size - 2;
big_endian_short_map[1] = size - 1;
little_endian_short_map[0] = size - 1;
little_endian_short_map[1] = size - 2;
machine_endian_long_map[0] = size - 4;
machine_endian_long_map[1] = size - 3;
machine_endian_long_map[2] = size - 2;
machine_endian_long_map[3] = size - 1;
big_endian_long_map[0] = size - 4;
big_endian_long_map[1] = size - 3;
big_endian_long_map[2] = size - 2;
big_endian_long_map[3] = size - 1;
little_endian_long_map[0] = size - 1;
little_endian_long_map[1] = size - 2;
little_endian_long_map[2] = size - 3;
little_endian_long_map[3] = size - 4;
#if SIZEOF_ZEND_LONG > 4
machine_endian_longlong_map[0] = size - 8;
machine_endian_longlong_map[1] = size - 7;
machine_endian_longlong_map[2] = size - 6;
machine_endian_longlong_map[3] = size - 5;
machine_endian_longlong_map[4] = size - 4;
machine_endian_longlong_map[5] = size - 3;
machine_endian_longlong_map[6] = size - 2;
machine_endian_longlong_map[7] = size - 1;
big_endian_longlong_map[0] = size - 8;
big_endian_longlong_map[1] = size - 7;
big_endian_longlong_map[2] = size - 6;
big_endian_longlong_map[3] = size - 5;
big_endian_longlong_map[4] = size - 4;
big_endian_longlong_map[5] = size - 3;
big_endian_longlong_map[6] = size - 2;
big_endian_longlong_map[7] = size - 1;
little_endian_longlong_map[0] = size - 1;
little_endian_longlong_map[1] = size - 2;
little_endian_longlong_map[2] = size - 3;
little_endian_longlong_map[3] = size - 4;
little_endian_longlong_map[4] = size - 5;
little_endian_longlong_map[5] = size - 6;
little_endian_longlong_map[6] = size - 7;
little_endian_longlong_map[7] = size - 8;
#endif
}
return SUCCESS;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/