Skip to content

Commit e215d5e

Browse files
committed
Care about endianness
1 parent abf8e83 commit e215d5e

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/isal/isal_zlibmodule.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,24 @@ GzipReader_read_from_file(GzipReader *self)
14481448

14491449
static PyObject *BadGzipFile; // Import BadGzipFile error for consistency
14501450

1451+
static inline uint32_t load_u32_le(void *address) {
1452+
#if PY_BIG_ENDIAN
1453+
uint8_t *mem = address;
1454+
return mem[0] | (mem[1] << 8) | (mem[2] << 16) | (mem[3] << 24);
1455+
#else
1456+
return *(uint32_t *)address;
1457+
#endif
1458+
}
1459+
1460+
static inline uint16_t load_u16_le(void *address) {
1461+
#if PY_BIG_ENDIAN
1462+
uint8_t *mem = address;
1463+
return mem[0] | (mem[1] << 8) | (mem[2] << 16) | (mem[3] << 24);
1464+
#else
1465+
return *(uint16_t *)address;
1466+
#endif
1467+
}
1468+
14511469
static Py_ssize_t
14521470
GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_buffer_size)
14531471
{
@@ -1494,15 +1512,15 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
14941512
return -1;
14951513
}
14961514
uint8_t flags = current_pos[3];
1497-
self->_last_mtime = *(uint32_t *)(current_pos + 4);
1515+
self->_last_mtime = load_u32_le(current_pos + 4);
14981516
// Skip XFL and header flag
14991517
uint8_t *header_cursor = current_pos + 10;
15001518
if (flags & FEXTRA) {
15011519
// Read the extra field and discard it.
15021520
if (header_cursor + 2 >= buffer_end) {
15031521
break;
15041522
}
1505-
uint16_t flength = *(uint16_t *)header_cursor;
1523+
uint16_t flength = load_u16_le(header_cursor);
15061524
header_cursor += 2;
15071525
if (header_cursor + flength >= buffer_end) {
15081526
break;
@@ -1529,7 +1547,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15291547
if (header_cursor + 2 >= buffer_end) {
15301548
break;
15311549
}
1532-
uint16_t header_crc = *(uint16_t *)header_cursor;
1550+
uint16_t header_crc = load_u16_le(header_cursor);
15331551
uint16_t crc = crc32_gzip_refl(
15341552
0, current_pos, header_cursor - current_pos) & 0xFFFF;
15351553
if (header_crc != crc) {
@@ -1585,7 +1603,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15851603
if (buffer_end - current_pos < 8) {
15861604
break;
15871605
}
1588-
uint32_t crc = *(uint32_t *)current_pos;
1606+
uint32_t crc = load_u32_le(current_pos);
15891607
current_pos += 4;
15901608
if (crc != self->state.crc) {
15911609
Py_BLOCK_THREADS;
@@ -1596,7 +1614,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
15961614
);
15971615
return -1;
15981616
}
1599-
uint32_t length = *(uint32_t *)current_pos;
1617+
uint32_t length = load_u32_le(current_pos);
16001618
current_pos += 4;
16011619
if (length != self->state.total_out) {
16021620
Py_BLOCK_THREADS;

0 commit comments

Comments
 (0)