Skip to content

Commit 5b9430b

Browse files
author
Greg Beaver
committed
MFB: fix all remaining big-endian issues
1 parent a249c4d commit 5b9430b

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

ext/phar/phar.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,19 @@ void phar_entry_remove(phar_entry_data *idata, char **error TSRMLS_DC) /* {{{ */
510510
var = ((((unsigned char*)(buffer))[1]) << 8) \
511511
| (((unsigned char*)(buffer))[0]); \
512512
(buffer) += 2
513-
# define PHAR_ZIP_32(buffer) ((((unsigned char*)(buffer))[3]) << 24) \
514-
| ((((unsigned char*)(buffer))[2]) << 16) \
515-
| ((((unsigned char*)(buffer))[1]) << 8) \
516-
| (((unsigned char*)(buffer))[0])
517-
# define PHAR_ZIP_16(buffer) ((((unsigned char*)(buffer))[1]) << 8) \
518-
| (((unsigned char*)(buffer))[0])
513+
static inline php_uint32 phar_fix_32(php_uint32 buffer)
514+
{
515+
return ((((unsigned char *)&buffer)[3]) << 24) |
516+
((((unsigned char *)&buffer)[2]) << 16) |
517+
((((unsigned char *)&buffer)[1]) << 8) |
518+
(((unsigned char *)&buffer)[0]);
519+
}
520+
static inline php_uint16 phar_fix_16(php_uint16 buffer)
521+
{
522+
return ((((unsigned char *)&buffer)[1]) << 8) | ((unsigned char *)&buffer)[0];
523+
}
524+
# define PHAR_ZIP_32(buffer) phar_fix_32((php_uint32)(buffer))
525+
# define PHAR_ZIP_16(buffer) phar_fix_16((php_uint16)(buffer))
519526
#else
520527
# define PHAR_GET_32(buffer, var) \
521528
var = *(php_uint32*)(buffer); \

ext/phar/zip.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ int phar_parse_zipfile(php_stream *fp, char *fname, int fname_len, char *alias,
383383
php_stream_seek(fp, loc + PHAR_GET_16(zipentry.extra_len), SEEK_SET);
384384
}
385385

386-
switch (zipentry.compressed) {
386+
switch (PHAR_GET_16(zipentry.compressed)) {
387387
case PHAR_ZIP_COMP_NONE :
388388
/* compression flag already set */
389389
break;
@@ -450,7 +450,7 @@ int phar_parse_zipfile(php_stream *fp, char *fname, int fname_len, char *alias,
450450
}
451451

452452
p = buf;
453-
entry.metadata_len = zipentry.comment_len;
453+
entry.metadata_len = PHAR_GET_16(zipentry.comment_len);
454454

455455
if (phar_parse_metadata(&p, &(entry.metadata), PHAR_GET_16(zipentry.comment_len) TSRMLS_CC) == FAILURE) {
456456
entry.metadata_len = 0;
@@ -997,6 +997,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, int defau
997997
char *temperr = NULL;
998998
struct _phar_zip_pass pass;
999999
phar_zip_dir_end eocd;
1000+
php_uint32 cdir_size, cdir_offset;
10001001

10011002
pass.error = &temperr;
10021003
entry.flags = PHAR_ENT_PERM_DEF_FILE;
@@ -1198,7 +1199,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, int defau
11981199
memset(&eocd, 0, sizeof(eocd));
11991200

12001201
strncpy(eocd.signature, "PK\5\6", 4);
1201-
eocd.counthere = eocd.count = zend_hash_num_elements(&phar->manifest);
1202+
eocd.counthere = eocd.count = PHAR_GET_16(zend_hash_num_elements(&phar->manifest));
12021203
zend_hash_apply_with_argument(&phar->manifest, phar_zip_changed_apply, (void *) &pass TSRMLS_CC);
12031204

12041205
if (temperr) {
@@ -1217,11 +1218,13 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, int defau
12171218
}
12181219

12191220
/* save zip */
1220-
eocd.cdir_size = php_stream_tell(pass.centralfp);
1221-
eocd.cdir_offset = php_stream_tell(pass.filefp);
1221+
cdir_size = php_stream_tell(pass.centralfp);
1222+
cdir_offset = php_stream_tell(pass.filefp);
1223+
eocd.cdir_size = PHAR_SET_32(cdir_size);
1224+
eocd.cdir_offset = PHAR_SET_32(cdir_offset);
12221225
php_stream_seek(pass.centralfp, 0, SEEK_SET);
12231226

1224-
if (eocd.cdir_size != php_stream_copy_to_stream(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL)) {
1227+
if (cdir_size != php_stream_copy_to_stream(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL)) {
12251228
if (error) {
12261229
spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname);
12271230
}

0 commit comments

Comments
 (0)