1919#include <stdio.h>
2020#include <stdlib.h>
2121#include <errno.h>
22+ #include <limits.h>
2223#include <sys/types.h>
2324#include <sys/stat.h>
2425#include <fcntl.h>
@@ -63,30 +64,125 @@ typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_uint);
6364typedef ZEND_SET_ALIGNED (1 , int unaligned_int ) ;
6465
6566/* Mapping of byte from char (8bit) to long for machine endian */
66- static int byte_map [1 ];
67+ static const int byte_map [1 ] = {
68+ #ifdef WORDS_BIGENDIAN
69+ sizeof (Z_LVAL_P ((zval * )0 )) - 1
70+ #else
71+ 0
72+ #endif
73+ };
6774
6875/* Mappings of bytes from int (machine dependent) to int for machine endian */
69- static int int_map [sizeof (int )];
76+ static const int int_map [sizeof (int )] = {
77+ #ifdef WORDS_BIGENDIAN
78+ #if UINT_MAX == 0xFFFFFFFFU
79+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
80+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
81+ #elif UINT_MAX == 0xFFFFFFFFFFFFFFFFU
82+ sizeof (Z_LVAL_P ((zval * )0 )) - 8 , sizeof (Z_LVAL_P ((zval * )0 )) - 7 ,
83+ sizeof (Z_LVAL_P ((zval * )0 )) - 6 , sizeof (Z_LVAL_P ((zval * )0 )) - 5 ,
84+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
85+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
86+ #else
87+ #error "Unsupported size of int: not 4 or 8 bytes"
88+ #endif
89+ #else // little endian
90+ #if UINT_MAX == 0xFFFFFFFFU
91+ 0 , 1 , 2 , 3
92+ #elif UINT_MAX == 0xFFFFFFFFFFFFFFFFU
93+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7
94+ #else
95+ #error "Unsupported size of int: not 4 or 8 bytes"
96+ #endif
97+ #endif
98+ };
7099
71100/* Mappings of bytes from shorts (16bit) for all endian environments */
72- static int machine_endian_short_map [2 ];
73- static int big_endian_short_map [2 ];
74- static int little_endian_short_map [2 ];
101+ static const int machine_endian_short_map [2 ] = {
102+ #ifdef WORDS_BIGENDIAN
103+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
104+ #else
105+ 0 , 1
106+ #endif
107+ };
108+
109+ static const int big_endian_short_map [2 ] = {
110+ #ifdef WORDS_BIGENDIAN
111+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
112+ #else
113+ 1 , 0
114+ #endif
115+ };
116+ static const int little_endian_short_map [2 ] = {
117+ #ifdef WORDS_BIGENDIAN
118+ sizeof (Z_LVAL_P ((zval * )0 )) - 1 , sizeof (Z_LVAL_P ((zval * )0 )) - 2
119+ #else
120+ 0 , 1
121+ #endif
122+ };
75123
76124/* Mappings of bytes from longs (32bit) for all endian environments */
77- static int machine_endian_long_map [4 ];
78- static int big_endian_long_map [4 ];
79- static int little_endian_long_map [4 ];
125+ static const int machine_endian_long_map [4 ] = {
126+ #ifdef WORDS_BIGENDIAN
127+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
128+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
129+ #else
130+ 0 , 1 , 2 , 3
131+ #endif
132+ };
133+ static const int big_endian_long_map [4 ] = {
134+ #ifdef WORDS_BIGENDIAN
135+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
136+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
137+ #else
138+ 3 , 2 , 1 , 0
139+ #endif
140+ };
141+ static const int little_endian_long_map [4 ] = {
142+ #ifdef WORDS_BIGENDIAN
143+ sizeof (Z_LVAL_P ((zval * )0 )) - 1 , sizeof (Z_LVAL_P ((zval * )0 )) - 2 ,
144+ sizeof (Z_LVAL_P ((zval * )0 )) - 3 , sizeof (Z_LVAL_P ((zval * )0 )) - 4
145+ #else
146+ 0 , 1 , 2 , 3
147+ #endif
148+ };
80149
81150#if SIZEOF_ZEND_LONG > 4
82151/* Mappings of bytes from quads (64bit) for all endian environments */
83- static int machine_endian_longlong_map [8 ];
84- static int big_endian_longlong_map [8 ];
85- static int little_endian_longlong_map [8 ];
152+ static const int machine_endian_longlong_map [8 ] = {
153+ #ifdef WORDS_BIGENDIAN
154+ sizeof (Z_LVAL_P ((zval * )0 )) - 8 , sizeof (Z_LVAL_P ((zval * )0 )) - 7 ,
155+ sizeof (Z_LVAL_P ((zval * )0 )) - 6 , sizeof (Z_LVAL_P ((zval * )0 )) - 5 ,
156+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
157+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
158+ #else
159+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7
160+ #endif
161+ };
162+ static const int big_endian_longlong_map [8 ] = {
163+ #ifdef WORDS_BIGENDIAN
164+ sizeof (Z_LVAL_P ((zval * )0 )) - 8 , sizeof (Z_LVAL_P ((zval * )0 )) - 7 ,
165+ sizeof (Z_LVAL_P ((zval * )0 )) - 6 , sizeof (Z_LVAL_P ((zval * )0 )) - 5 ,
166+ sizeof (Z_LVAL_P ((zval * )0 )) - 4 , sizeof (Z_LVAL_P ((zval * )0 )) - 3 ,
167+ sizeof (Z_LVAL_P ((zval * )0 )) - 2 , sizeof (Z_LVAL_P ((zval * )0 )) - 1
168+ #else
169+ 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0
170+ #endif
171+ };
172+ static const int little_endian_longlong_map [8 ] = {
173+ #ifdef WORDS_BIGENDIAN
174+ sizeof (Z_LVAL_P ((zval * )0 )) - 1 , sizeof (Z_LVAL_P ((zval * )0 )) - 2 ,
175+ sizeof (Z_LVAL_P ((zval * )0 )) - 3 , sizeof (Z_LVAL_P ((zval * )0 )) - 4 ,
176+ sizeof (Z_LVAL_P ((zval * )0 )) - 5 , sizeof (Z_LVAL_P ((zval * )0 )) - 6 ,
177+ sizeof (Z_LVAL_P ((zval * )0 )) - 7 , sizeof (Z_LVAL_P ((zval * )0 )) - 8
178+ #else
179+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7
180+ #endif
181+ };
86182#endif
87183
88184/* {{{ php_pack */
89- static void php_pack (zval * val , size_t size , int * map , char * output )
185+ static void php_pack (zval * val , size_t size , const int * const map , char * output )
90186{
91187 size_t i ;
92188 char * v ;
@@ -538,7 +634,7 @@ PHP_FUNCTION(pack)
538634 case 'S' :
539635 case 'n' :
540636 case 'v' : {
541- int * map = machine_endian_short_map ;
637+ const int * map = machine_endian_short_map ;
542638
543639 if (code == 'n' ) {
544640 map = big_endian_short_map ;
@@ -565,7 +661,7 @@ PHP_FUNCTION(pack)
565661 case 'L' :
566662 case 'N' :
567663 case 'V' : {
568- int * map = machine_endian_long_map ;
664+ const int * map = machine_endian_long_map ;
569665
570666 if (code == 'N' ) {
571667 map = big_endian_long_map ;
@@ -585,7 +681,7 @@ PHP_FUNCTION(pack)
585681 case 'Q' :
586682 case 'J' :
587683 case 'P' : {
588- int * map = machine_endian_longlong_map ;
684+ const int * map = machine_endian_longlong_map ;
589685
590686 if (code == 'J' ) {
591687 map = big_endian_longlong_map ;
@@ -1199,127 +1295,3 @@ PHP_FUNCTION(unpack)
11991295 }
12001296}
12011297/* }}} */
1202-
1203- /* {{{ PHP_MINIT_FUNCTION */
1204- PHP_MINIT_FUNCTION (pack )
1205- {
1206- int i ;
1207-
1208- if (MACHINE_LITTLE_ENDIAN ) {
1209- /* Where to get lo to hi bytes from */
1210- byte_map [0 ] = 0 ;
1211-
1212- for (i = 0 ; i < (int )sizeof (int ); i ++ ) {
1213- int_map [i ] = i ;
1214- }
1215-
1216- machine_endian_short_map [0 ] = 0 ;
1217- machine_endian_short_map [1 ] = 1 ;
1218- big_endian_short_map [0 ] = 1 ;
1219- big_endian_short_map [1 ] = 0 ;
1220- little_endian_short_map [0 ] = 0 ;
1221- little_endian_short_map [1 ] = 1 ;
1222-
1223- machine_endian_long_map [0 ] = 0 ;
1224- machine_endian_long_map [1 ] = 1 ;
1225- machine_endian_long_map [2 ] = 2 ;
1226- machine_endian_long_map [3 ] = 3 ;
1227- big_endian_long_map [0 ] = 3 ;
1228- big_endian_long_map [1 ] = 2 ;
1229- big_endian_long_map [2 ] = 1 ;
1230- big_endian_long_map [3 ] = 0 ;
1231- little_endian_long_map [0 ] = 0 ;
1232- little_endian_long_map [1 ] = 1 ;
1233- little_endian_long_map [2 ] = 2 ;
1234- little_endian_long_map [3 ] = 3 ;
1235-
1236- #if SIZEOF_ZEND_LONG > 4
1237- machine_endian_longlong_map [0 ] = 0 ;
1238- machine_endian_longlong_map [1 ] = 1 ;
1239- machine_endian_longlong_map [2 ] = 2 ;
1240- machine_endian_longlong_map [3 ] = 3 ;
1241- machine_endian_longlong_map [4 ] = 4 ;
1242- machine_endian_longlong_map [5 ] = 5 ;
1243- machine_endian_longlong_map [6 ] = 6 ;
1244- machine_endian_longlong_map [7 ] = 7 ;
1245- big_endian_longlong_map [0 ] = 7 ;
1246- big_endian_longlong_map [1 ] = 6 ;
1247- big_endian_longlong_map [2 ] = 5 ;
1248- big_endian_longlong_map [3 ] = 4 ;
1249- big_endian_longlong_map [4 ] = 3 ;
1250- big_endian_longlong_map [5 ] = 2 ;
1251- big_endian_longlong_map [6 ] = 1 ;
1252- big_endian_longlong_map [7 ] = 0 ;
1253- little_endian_longlong_map [0 ] = 0 ;
1254- little_endian_longlong_map [1 ] = 1 ;
1255- little_endian_longlong_map [2 ] = 2 ;
1256- little_endian_longlong_map [3 ] = 3 ;
1257- little_endian_longlong_map [4 ] = 4 ;
1258- little_endian_longlong_map [5 ] = 5 ;
1259- little_endian_longlong_map [6 ] = 6 ;
1260- little_endian_longlong_map [7 ] = 7 ;
1261- #endif
1262- }
1263- else {
1264- zval val ;
1265- int size = sizeof (Z_LVAL (val ));
1266- Z_LVAL (val )= 0 ; /*silence a warning*/
1267-
1268- /* Where to get hi to lo bytes from */
1269- byte_map [0 ] = size - 1 ;
1270-
1271- for (i = 0 ; i < (int )sizeof (int ); i ++ ) {
1272- int_map [i ] = size - (sizeof (int ) - i );
1273- }
1274-
1275- machine_endian_short_map [0 ] = size - 2 ;
1276- machine_endian_short_map [1 ] = size - 1 ;
1277- big_endian_short_map [0 ] = size - 2 ;
1278- big_endian_short_map [1 ] = size - 1 ;
1279- little_endian_short_map [0 ] = size - 1 ;
1280- little_endian_short_map [1 ] = size - 2 ;
1281-
1282- machine_endian_long_map [0 ] = size - 4 ;
1283- machine_endian_long_map [1 ] = size - 3 ;
1284- machine_endian_long_map [2 ] = size - 2 ;
1285- machine_endian_long_map [3 ] = size - 1 ;
1286- big_endian_long_map [0 ] = size - 4 ;
1287- big_endian_long_map [1 ] = size - 3 ;
1288- big_endian_long_map [2 ] = size - 2 ;
1289- big_endian_long_map [3 ] = size - 1 ;
1290- little_endian_long_map [0 ] = size - 1 ;
1291- little_endian_long_map [1 ] = size - 2 ;
1292- little_endian_long_map [2 ] = size - 3 ;
1293- little_endian_long_map [3 ] = size - 4 ;
1294-
1295- #if SIZEOF_ZEND_LONG > 4
1296- machine_endian_longlong_map [0 ] = size - 8 ;
1297- machine_endian_longlong_map [1 ] = size - 7 ;
1298- machine_endian_longlong_map [2 ] = size - 6 ;
1299- machine_endian_longlong_map [3 ] = size - 5 ;
1300- machine_endian_longlong_map [4 ] = size - 4 ;
1301- machine_endian_longlong_map [5 ] = size - 3 ;
1302- machine_endian_longlong_map [6 ] = size - 2 ;
1303- machine_endian_longlong_map [7 ] = size - 1 ;
1304- big_endian_longlong_map [0 ] = size - 8 ;
1305- big_endian_longlong_map [1 ] = size - 7 ;
1306- big_endian_longlong_map [2 ] = size - 6 ;
1307- big_endian_longlong_map [3 ] = size - 5 ;
1308- big_endian_longlong_map [4 ] = size - 4 ;
1309- big_endian_longlong_map [5 ] = size - 3 ;
1310- big_endian_longlong_map [6 ] = size - 2 ;
1311- big_endian_longlong_map [7 ] = size - 1 ;
1312- little_endian_longlong_map [0 ] = size - 1 ;
1313- little_endian_longlong_map [1 ] = size - 2 ;
1314- little_endian_longlong_map [2 ] = size - 3 ;
1315- little_endian_longlong_map [3 ] = size - 4 ;
1316- little_endian_longlong_map [4 ] = size - 5 ;
1317- little_endian_longlong_map [5 ] = size - 6 ;
1318- little_endian_longlong_map [6 ] = size - 7 ;
1319- little_endian_longlong_map [7 ] = size - 8 ;
1320- #endif
1321- }
1322-
1323- return SUCCESS ;
1324- }
1325- /* }}} */
0 commit comments