1999-04-17 08:37:12 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2005-08-03 22:08:58 +08:00
|
|
|
| Copyright (c) 1997-2005 The PHP Group |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-06-11 04:04:29 +08:00
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
1999-07-16 21:13:16 +08: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. |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
| Author: Chris Schneider <cschneid@relog.ch> |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
1999-04-24 04:06:01 +08:00
|
|
|
|
1999-04-17 08:37:12 +08: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 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-17 08:37:12 +08:00
|
|
|
#define O_RDONLY _O_RDONLY
|
|
|
|
#include "win32/param.h"
|
2002-09-06 15:44:30 +08:00
|
|
|
#elif defined(NETWARE)
|
|
|
|
#ifdef USE_WINSOCK
|
|
|
|
#include <novsock2.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/param.h>
|
|
|
|
#else
|
1999-04-17 08:37:12 +08:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
1999-04-22 08:25:57 +08:00
|
|
|
#include "ext/standard/head.h"
|
1999-04-17 08:37:12 +08:00
|
|
|
#include "safe_mode.h"
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "php_string.h"
|
1999-04-17 08:37:12 +08:00
|
|
|
#include "pack.h"
|
|
|
|
#if HAVE_PWD_H
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-04-17 08:37:12 +08:00
|
|
|
#include "win32/pwd.h"
|
|
|
|
#else
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#include "fsock.h"
|
|
|
|
#if HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2004-11-28 20:44:28 +08:00
|
|
|
#define INC_OUTPUTPOS(a,b) \
|
|
|
|
if ((a) < 0 || ((INT_MAX - outputpos)/(b)) < (a)) { \
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: integer overflow in format string", code); \
|
|
|
|
RETURN_FALSE; \
|
|
|
|
} \
|
|
|
|
outputpos += (a)*(b);
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
/* 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];
|
|
|
|
|
|
|
|
/* Mappings of bytes from int (machine dependant) 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];
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ php_pack
|
|
|
|
*/
|
2001-09-04 07:30:33 +08:00
|
|
|
static void php_pack(zval **val, int size, int *map, char *output)
|
1999-04-17 08:37:12 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *v;
|
|
|
|
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_long_ex(val);
|
2001-09-04 07:30:33 +08:00
|
|
|
v = (char *) &Z_LVAL_PP(val);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2001-09-04 07:30:33 +08:00
|
|
|
*output++ = v[map[i]];
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/* pack() idea stolen from Perl (implemented formats behave the same as there)
|
2001-08-12 01:03:37 +08:00
|
|
|
* Implemented formats are A, a, h, H, c, C, s, S, i, I, l, L, n, N, f, d, x, X, @.
|
1999-04-17 08:37:12 +08:00
|
|
|
*/
|
2000-07-09 22:26:50 +08: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 */
|
1999-04-17 08:37:12 +08:00
|
|
|
PHP_FUNCTION(pack)
|
|
|
|
{
|
2001-09-04 07:30:33 +08:00
|
|
|
zval ***argv;
|
1999-04-17 08:37:12 +08:00
|
|
|
int argc, i;
|
|
|
|
int currentarg;
|
|
|
|
char *format;
|
|
|
|
int formatlen;
|
|
|
|
char *formatcodes;
|
|
|
|
int *formatargs;
|
|
|
|
int formatcount = 0;
|
|
|
|
int outputpos = 0, outputsize = 0;
|
|
|
|
char *output;
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
argc = ZEND_NUM_ARGS();
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
2003-08-12 07:16:54 +08:00
|
|
|
argv = safe_emalloc(argc, sizeof(zval **), 0);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
1999-12-20 10:09:58 +08:00
|
|
|
if (zend_get_parameters_array_ex(argc, argv) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
efree(argv);
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_string_ex(argv[0]);
|
2001-09-04 07:30:33 +08:00
|
|
|
format = Z_STRVAL_PP(argv[0]);
|
|
|
|
formatlen = Z_STRLEN_PP(argv[0]);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/* We have a maximum of <formatlen> format codes to deal with */
|
2003-08-12 07:16:54 +08:00
|
|
|
formatcodes = safe_emalloc(formatlen, sizeof(*formatcodes), 0);
|
|
|
|
formatargs = safe_emalloc(formatlen, sizeof(*formatargs), 0);
|
1999-04-17 08:37:12 +08:00
|
|
|
currentarg = 1;
|
|
|
|
|
|
|
|
/* 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++;
|
|
|
|
}
|
2001-09-04 07:30:33 +08:00
|
|
|
else if (c >= '0' && c <= '9') {
|
1999-04-17 08:37:12 +08:00
|
|
|
arg = atoi(&format[i]);
|
|
|
|
|
|
|
|
while (format[i] >= '0' && format[i] <= '9' && i < formatlen) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle special arg '*' for all codes and check argv overflows */
|
2001-09-04 07:30:33 +08:00
|
|
|
switch ((int) code) {
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Never uses any args */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
case '@':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (arg < 0) {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: '*' ignored", code);
|
1999-04-17 08:37:12 +08:00
|
|
|
arg = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Always uses one arg */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (currentarg >= argc) {
|
|
|
|
efree(argv);
|
|
|
|
efree(formatcodes);
|
|
|
|
efree(formatargs);
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: not enough arguments", code);
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg < 0) {
|
2005-12-16 06:15:01 +08:00
|
|
|
convert_to_string_ex(argv[currentarg]);
|
2001-09-04 07:30:33 +08:00
|
|
|
arg = Z_STRLEN_PP(argv[currentarg]);
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
currentarg++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use as many args as specified */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
case 'f':
|
|
|
|
case 'd':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (arg < 0) {
|
|
|
|
arg = argc - currentarg;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentarg += arg;
|
|
|
|
|
|
|
|
if (currentarg > argc) {
|
|
|
|
efree(argv);
|
|
|
|
efree(formatcodes);
|
|
|
|
efree(formatargs);
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: too few arguments", code);
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
default:
|
2002-08-14 05:56:39 +08:00
|
|
|
efree(argv);
|
|
|
|
efree(formatcodes);
|
|
|
|
efree(formatargs);
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: unknown format code", code);
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatcodes[formatcount] = code;
|
|
|
|
formatargs[formatcount] = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentarg < argc) {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%d arguments unused", (argc - currentarg));
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate output length and upper bound while processing*/
|
|
|
|
for (i = 0; i < formatcount; i++) {
|
2001-09-04 07:30:33 +08:00
|
|
|
int code = (int) formatcodes[i];
|
1999-04-17 08:37:12 +08:00
|
|
|
int arg = formatargs[i];
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
switch ((int) code) {
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS((arg + 1) / 2,1) /* 4 bit per arg */
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
case 'x':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,1) /* 8 bit per arg */
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'n':
|
|
|
|
case 'v':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,2) /* 16 bit per arg */
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,sizeof(int))
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'N':
|
|
|
|
case 'V':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,4) /* 32 bit per arg */
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'f':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,sizeof(float))
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'd':
|
2004-11-28 20:44:28 +08:00
|
|
|
INC_OUTPUTPOS(arg,sizeof(double))
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'X':
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos -= arg;
|
|
|
|
|
|
|
|
if (outputpos < 0) {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", code);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case '@':
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos = arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputsize < outputpos) {
|
|
|
|
outputsize = outputpos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output = emalloc(outputsize + 1);
|
|
|
|
outputpos = 0;
|
|
|
|
currentarg = 1;
|
|
|
|
|
|
|
|
/* Do actual packing */
|
|
|
|
for (i = 0; i < formatcount; i++) {
|
2001-09-04 07:30:33 +08:00
|
|
|
int code = (int) formatcodes[i];
|
1999-04-17 08:37:12 +08:00
|
|
|
int arg = formatargs[i];
|
2001-09-04 07:30:33 +08:00
|
|
|
zval **val;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
switch ((int) code) {
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
1999-04-17 08:37:12 +08:00
|
|
|
memset(&output[outputpos], (code == 'a') ? '\0' : ' ', arg);
|
|
|
|
val = argv[currentarg++];
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_string_ex(val);
|
2001-09-04 07:30:33 +08:00
|
|
|
memcpy(&output[outputpos], Z_STRVAL_PP(val),
|
2003-01-12 07:05:19 +08:00
|
|
|
(Z_STRLEN_PP(val) < arg) ? Z_STRLEN_PP(val) : arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos += arg;
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'h':
|
|
|
|
case 'H': {
|
1999-04-17 08:37:12 +08:00
|
|
|
int nibbleshift = (code == 'h') ? 0 : 4;
|
|
|
|
int first = 1;
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
val = argv[currentarg++];
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_string_ex(val);
|
2001-09-04 07:30:33 +08:00
|
|
|
v = Z_STRVAL_PP(val);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos--;
|
2003-01-12 07:05:19 +08:00
|
|
|
if(arg > Z_STRLEN_PP(val)) {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: not enough characters in string", code);
|
2001-09-04 07:30:33 +08:00
|
|
|
arg = Z_STRLEN_PP(val);
|
2000-09-10 21:34:03 +08:00
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
while (arg-- > 0) {
|
2001-09-04 07:30:33 +08:00
|
|
|
char n = *v++;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
if (n >= '0' && n <= '9') {
|
1999-04-17 08:37:12 +08:00
|
|
|
n -= '0';
|
2001-09-04 07:30:33 +08:00
|
|
|
} else if (n >= 'A' && n <= 'F') {
|
1999-04-17 08:37:12 +08:00
|
|
|
n -= ('A' - 10);
|
2001-09-04 07:30:33 +08:00
|
|
|
} else if (n >= 'a' && n <= 'f') {
|
1999-04-17 08:37:12 +08:00
|
|
|
n -= ('a' - 10);
|
|
|
|
} else {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: illegal hex digit %c", code, n);
|
1999-04-17 08:37:12 +08:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first--) {
|
|
|
|
output[++outputpos] = 0;
|
|
|
|
} else {
|
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
output[outputpos] |= (n << nibbleshift);
|
|
|
|
nibbleshift = (nibbleshift + 4) & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
outputpos++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
1999-04-17 08:37:12 +08:00
|
|
|
while (arg-- > 0) {
|
1999-12-18 12:01:20 +08:00
|
|
|
php_pack(argv[currentarg++], 1, byte_map, &output[outputpos]);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'n':
|
|
|
|
case 'v': {
|
1999-04-17 08:37:12 +08:00
|
|
|
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) {
|
1999-12-18 12:01:20 +08:00
|
|
|
php_pack(argv[currentarg++], 2, map, &output[outputpos]);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
1999-04-17 08:37:12 +08:00
|
|
|
while (arg-- > 0) {
|
1999-12-18 12:01:20 +08:00
|
|
|
php_pack(argv[currentarg++], sizeof(int), int_map, &output[outputpos]);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos += sizeof(int);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'N':
|
|
|
|
case 'V': {
|
1999-04-17 08:37:12 +08:00
|
|
|
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) {
|
1999-12-18 12:01:20 +08:00
|
|
|
php_pack(argv[currentarg++], 4, map, &output[outputpos]);
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos += 4;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'f': {
|
|
|
|
float v;
|
|
|
|
|
|
|
|
while (arg-- > 0) {
|
|
|
|
val = argv[currentarg++];
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_double_ex(val);
|
2001-09-04 07:30:33 +08:00
|
|
|
v = (float) Z_DVAL_PP(val);
|
1999-04-17 08:37:12 +08:00
|
|
|
memcpy(&output[outputpos], &v, sizeof(v));
|
|
|
|
outputpos += sizeof(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'd': {
|
|
|
|
double v;
|
|
|
|
|
|
|
|
while (arg-- > 0) {
|
|
|
|
val = argv[currentarg++];
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_double_ex(val);
|
2001-09-04 07:30:33 +08:00
|
|
|
v = (double) Z_DVAL_PP(val);
|
1999-04-17 08:37:12 +08:00
|
|
|
memcpy(&output[outputpos], &v, sizeof(v));
|
|
|
|
outputpos += sizeof(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'x':
|
1999-04-17 08:37:12 +08:00
|
|
|
memset(&output[outputpos], '\0', arg);
|
|
|
|
outputpos += arg;
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'X':
|
1999-04-17 08:37:12 +08:00
|
|
|
outputpos -= arg;
|
|
|
|
|
|
|
|
if (outputpos < 0) {
|
|
|
|
outputpos = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case '@':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (arg > outputpos) {
|
|
|
|
memset(&output[outputpos], '\0', arg - outputpos);
|
|
|
|
}
|
|
|
|
outputpos = arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
efree(argv);
|
|
|
|
efree(formatcodes);
|
|
|
|
efree(formatargs);
|
|
|
|
output[outputpos] = '\0';
|
|
|
|
RETVAL_STRINGL(output, outputpos, 1);
|
|
|
|
efree(output);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ php_unpack
|
|
|
|
*/
|
1999-12-18 12:01:20 +08:00
|
|
|
static long php_unpack(char *data, int size, int issigned, int *map)
|
1999-04-17 08:37:12 +08:00
|
|
|
{
|
|
|
|
long result;
|
2001-09-04 07:30:33 +08:00
|
|
|
char *cresult = (char *) &result;
|
1999-04-17 08:37:12 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
result = issigned ? -1 : 0;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2001-09-04 07:30:33 +08:00
|
|
|
cresult[map[i]] = *data++;
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/* unpack() is based on Perl's unpack(), but is modified a bit from there.
|
|
|
|
* Rather than depending on error-prone ordered lists or syntactically
|
|
|
|
* unpleasant pass-by-reference, we return an object with named paramters
|
|
|
|
* (like *_fetch_object()). Syntax is "f[repeat]name/...", where "f" is the
|
2000-09-10 19:03:32 +08:00
|
|
|
* formatter char (like pack()), "[repeat]" is the optional repeater argument,
|
1999-04-17 08:37:12 +08:00
|
|
|
* 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.
|
2001-08-12 01:03:37 +08:00
|
|
|
* Implemented formats are A, a, h, H, c, C, s, S, i, I, l, L, n, N, f, d, x, X, @.
|
1999-04-17 08:37:12 +08:00
|
|
|
*/
|
|
|
|
/* {{{ proto array unpack(string format, string input)
|
|
|
|
Unpack binary string into named array elements according to format argument */
|
|
|
|
PHP_FUNCTION(unpack)
|
|
|
|
{
|
2001-09-04 07:30:33 +08:00
|
|
|
zval **formatarg;
|
|
|
|
zval **inputarg;
|
1999-04-17 08:37:12 +08:00
|
|
|
char *format;
|
|
|
|
char *input;
|
|
|
|
int formatlen;
|
|
|
|
int inputpos, inputlen;
|
|
|
|
int i;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 2 ||
|
|
|
|
zend_get_parameters_ex(2, &formatarg, &inputarg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
|
|
|
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_string_ex(formatarg);
|
|
|
|
convert_to_string_ex(inputarg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
format = Z_STRVAL_PP(formatarg);
|
|
|
|
formatlen = Z_STRLEN_PP(formatarg);
|
|
|
|
input = Z_STRVAL_PP(inputarg);
|
|
|
|
inputlen = Z_STRLEN_PP(inputarg);
|
1999-04-17 08:37:12 +08:00
|
|
|
inputpos = 0;
|
|
|
|
|
2002-12-06 06:28:02 +08:00
|
|
|
array_init(return_value);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
while (formatlen-- > 0) {
|
|
|
|
char type = *(format++);
|
|
|
|
char c;
|
|
|
|
int arg = 1;
|
|
|
|
char *name;
|
|
|
|
int namelen;
|
|
|
|
int size=0;
|
|
|
|
|
|
|
|
/* Handle format arguments if any */
|
|
|
|
if (formatlen > 0) {
|
|
|
|
c = *format;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
if (c >= '0' && c <= '9') {
|
1999-04-17 08:37:12 +08:00
|
|
|
arg = atoi(format);
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
while (formatlen > 0 && *format >= '0' && *format <= '9') {
|
1999-04-17 08:37:12 +08:00
|
|
|
format++;
|
|
|
|
formatlen--;
|
|
|
|
}
|
|
|
|
} else if (c == '*') {
|
|
|
|
arg = -1;
|
|
|
|
format++;
|
|
|
|
formatlen--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get of new value in array */
|
|
|
|
name = format;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
while (formatlen > 0 && *format != '/') {
|
1999-04-17 08:37:12 +08:00
|
|
|
formatlen--;
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
|
|
|
|
namelen = format - name;
|
|
|
|
|
|
|
|
if (namelen > 200)
|
|
|
|
namelen = 200;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
switch ((int) type) {
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Never use any input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'X':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = -1;
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case '@':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = 0;
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'a':
|
|
|
|
case 'A':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = arg;
|
|
|
|
arg = 1;
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
size = (arg > 0) ? arg / 2 : arg;
|
2000-09-10 21:47:51 +08:00
|
|
|
arg = 1;
|
|
|
|
break;
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Use 1 byte of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
case 'x':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use 2 bytes of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'n':
|
|
|
|
case 'v':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use sizeof(int) bytes of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = sizeof(int);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use 4 bytes of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'N':
|
|
|
|
case 'V':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use sizeof(float) bytes of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'f':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = sizeof(float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Use sizeof(double) bytes of input */
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'd':
|
1999-04-17 08:37:12 +08:00
|
|
|
size = sizeof(double);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do actual unpacking */
|
2001-09-04 07:30:33 +08:00
|
|
|
for (i = 0; i != arg; i++ ) {
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Space for name + number, safe as namelen is ensured <= 200 */
|
|
|
|
char n[256];
|
|
|
|
|
2004-02-25 05:49:28 +08:00
|
|
|
if (arg != 1 || namelen == 0) {
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Need to add element number to name */
|
|
|
|
sprintf(n, "%.*s%d", namelen, name, i + 1);
|
|
|
|
} else {
|
|
|
|
/* Truncate name to next format code or end of string */
|
|
|
|
sprintf(n, "%.*s", namelen, name);
|
|
|
|
}
|
|
|
|
|
2004-11-28 20:44:28 +08:00
|
|
|
if (size != 0 && size != -1 && INT_MAX - size + 1 < inputpos) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: integer overflow", type);
|
|
|
|
inputpos = 0;
|
|
|
|
}
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
if ((inputpos + size) <= inputlen) {
|
2001-09-04 07:30:33 +08:00
|
|
|
switch ((int) type) {
|
|
|
|
case 'a':
|
|
|
|
case 'A': {
|
1999-04-17 08:37:12 +08:00
|
|
|
char pad = (type == 'a') ? '\0' : ' ';
|
|
|
|
int 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 padding chars from unpacked data */
|
|
|
|
while (--len >= 0) {
|
|
|
|
if (input[inputpos + len] != pad)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_assoc_stringl(return_value, n, &input[inputpos], len + 1, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'h':
|
|
|
|
case 'H': {
|
1999-04-17 08:37:12 +08:00
|
|
|
int len = (inputlen - inputpos) * 2; /* Remaining */
|
|
|
|
int nibbleshift = (type == 'h') ? 0 : 4;
|
|
|
|
int first = 1;
|
|
|
|
char *buf;
|
|
|
|
int ipos, opos;
|
|
|
|
|
|
|
|
/* If size was given take minimum of len and size */
|
2001-09-04 07:30:33 +08:00
|
|
|
if (size >= 0 && len > (size * 2)) {
|
|
|
|
len = size * 2;
|
2000-09-10 21:47:51 +08:00
|
|
|
}
|
2001-09-04 07:30:33 +08:00
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
buf = emalloc(len + 1);
|
|
|
|
|
|
|
|
for (ipos = opos = 0; opos < len; opos++) {
|
|
|
|
char c = (input[inputpos + ipos] >> nibbleshift) & 0xf;
|
|
|
|
|
|
|
|
if (c < 10) {
|
|
|
|
c += '0';
|
|
|
|
} else {
|
|
|
|
c += 'a' - 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[opos] = c;
|
|
|
|
nibbleshift = (nibbleshift + 4) & 7;
|
|
|
|
|
|
|
|
if (first-- == 0) {
|
|
|
|
ipos++;
|
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
add_assoc_stringl(return_value, n, buf, len, 1);
|
|
|
|
efree(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'c':
|
|
|
|
case 'C': {
|
1999-04-17 08:37:12 +08:00
|
|
|
int issigned = (type == 'c') ? (input[inputpos] & 0x80) : 0;
|
1999-12-18 12:01:20 +08:00
|
|
|
long v = php_unpack(&input[inputpos], 1, issigned, byte_map);
|
1999-04-17 08:37:12 +08:00
|
|
|
add_assoc_long(return_value, n, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case 'n':
|
|
|
|
case 'v': {
|
1999-04-17 08:37:12 +08:00
|
|
|
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 12:01:20 +08:00
|
|
|
v = php_unpack(&input[inputpos], 2, issigned, map);
|
1999-04-17 08:37:12 +08:00
|
|
|
add_assoc_long(return_value, n, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'i':
|
|
|
|
case 'I': {
|
1999-04-17 08:37:12 +08:00
|
|
|
long v;
|
|
|
|
int issigned = 0;
|
|
|
|
|
|
|
|
if (type == 'i') {
|
|
|
|
issigned = input[inputpos + (machine_little_endian ? (sizeof(int) - 1) : 0)] & 0x80;
|
|
|
|
}
|
|
|
|
|
1999-12-18 12:01:20 +08:00
|
|
|
v = php_unpack(&input[inputpos], sizeof(int), issigned, int_map);
|
1999-04-17 08:37:12 +08:00
|
|
|
add_assoc_long(return_value, n, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'N':
|
|
|
|
case 'V': {
|
1999-04-17 08:37:12 +08:00
|
|
|
int issigned = 0;
|
|
|
|
int *map = machine_endian_long_map;
|
|
|
|
long v;
|
|
|
|
|
|
|
|
if (type == 'l') {
|
|
|
|
issigned = input[inputpos + (machine_little_endian ? 3 : 0)] & 0x80;
|
|
|
|
} else if (type == 'N') {
|
|
|
|
map = big_endian_long_map;
|
|
|
|
} else if (type == 'V') {
|
|
|
|
map = little_endian_long_map;
|
|
|
|
}
|
|
|
|
|
1999-12-18 12:01:20 +08:00
|
|
|
v = php_unpack(&input[inputpos], 4, issigned, map);
|
1999-04-17 08:37:12 +08:00
|
|
|
add_assoc_long(return_value, n, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 19:03:32 +08:00
|
|
|
memcpy(&v, &input[inputpos], sizeof(double));
|
1999-04-17 08:37:12 +08:00
|
|
|
add_assoc_double(return_value, n, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'x':
|
1999-04-17 08:37:12 +08:00
|
|
|
/* Do nothing with input, just skip it */
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case 'X':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (inputpos < size) {
|
|
|
|
inputpos = -size;
|
|
|
|
i = arg - 1; /* Break out of for loop */
|
|
|
|
|
|
|
|
if (arg >= 0) {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", type);
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2001-09-04 07:30:33 +08:00
|
|
|
case '@':
|
1999-04-17 08:37:12 +08:00
|
|
|
if (arg <= inputlen) {
|
|
|
|
inputpos = arg;
|
|
|
|
} else {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", type);
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i = arg - 1; /* Done, break out of for loop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputpos += size;
|
2004-11-28 20:44:28 +08:00
|
|
|
if (inputpos < 0) {
|
2005-01-26 06:50:39 +08:00
|
|
|
if (size != -1) { /* only print warning if not working with * */
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: outside of string", type);
|
|
|
|
}
|
2004-11-28 20:44:28 +08:00
|
|
|
inputpos = 0;
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
} else if (arg < 0) {
|
|
|
|
/* Reached end of input for '*' repeater */
|
|
|
|
break;
|
|
|
|
} else {
|
2002-12-06 04:59:49 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Type %c: not enough input, need %d, have %d", type, size, inputlen - inputpos);
|
2002-08-14 07:12:54 +08:00
|
|
|
zval_dtor(return_value);
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
formatlen--; /* Skip '/' separator, does no harm if inputlen == 0 */
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
1999-07-27 04:09:08 +08:00
|
|
|
PHP_MINIT_FUNCTION(pack)
|
1999-04-17 08:37:12 +08:00
|
|
|
{
|
|
|
|
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 16:09:56 +08:00
|
|
|
for (i = 0; i < (int)sizeof(int); i++) {
|
1999-04-17 08:37:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
else {
|
2001-09-04 07:30:33 +08:00
|
|
|
zval val;
|
2001-09-26 05:58:48 +08:00
|
|
|
int size = sizeof(Z_LVAL(val));
|
|
|
|
Z_LVAL(val)=0; /*silence a warning*/
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/* Where to get hi to lo bytes from */
|
|
|
|
byte_map[0] = size - 1;
|
|
|
|
|
2003-09-26 16:09:56 +08:00
|
|
|
for (i = 0; i < (int)sizeof(int); i++) {
|
1999-04-17 08:37:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
2001-06-05 21:12:10 +08:00
|
|
|
* c-basic-offset: 4
|
1999-04-17 08:37:12 +08:00
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
1999-04-17 08:37:12 +08:00
|
|
|
*/
|