@@ -1448,6 +1448,24 @@ GzipReader_read_from_file(GzipReader *self)
1448
1448
1449
1449
static PyObject * BadGzipFile ; // Import BadGzipFile error for consistency
1450
1450
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
+
1451
1469
static Py_ssize_t
1452
1470
GzipReader_read_into_buffer (GzipReader * self , uint8_t * out_buffer , size_t out_buffer_size )
1453
1471
{
@@ -1494,15 +1512,15 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1494
1512
return -1 ;
1495
1513
}
1496
1514
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 );
1498
1516
// Skip XFL and header flag
1499
1517
uint8_t * header_cursor = current_pos + 10 ;
1500
1518
if (flags & FEXTRA ) {
1501
1519
// Read the extra field and discard it.
1502
1520
if (header_cursor + 2 >= buffer_end ) {
1503
1521
break ;
1504
1522
}
1505
- uint16_t flength = * ( uint16_t * ) header_cursor ;
1523
+ uint16_t flength = load_u16_le ( header_cursor ) ;
1506
1524
header_cursor += 2 ;
1507
1525
if (header_cursor + flength >= buffer_end ) {
1508
1526
break ;
@@ -1529,7 +1547,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1529
1547
if (header_cursor + 2 >= buffer_end ) {
1530
1548
break ;
1531
1549
}
1532
- uint16_t header_crc = * ( uint16_t * ) header_cursor ;
1550
+ uint16_t header_crc = load_u16_le ( header_cursor ) ;
1533
1551
uint16_t crc = crc32_gzip_refl (
1534
1552
0 , current_pos , header_cursor - current_pos ) & 0xFFFF ;
1535
1553
if (header_crc != crc ) {
@@ -1585,7 +1603,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1585
1603
if (buffer_end - current_pos < 8 ) {
1586
1604
break ;
1587
1605
}
1588
- uint32_t crc = * ( uint32_t * ) current_pos ;
1606
+ uint32_t crc = load_u32_le ( current_pos ) ;
1589
1607
current_pos += 4 ;
1590
1608
if (crc != self -> state .crc ) {
1591
1609
Py_BLOCK_THREADS ;
@@ -1596,7 +1614,7 @@ GzipReader_read_into_buffer(GzipReader *self, uint8_t *out_buffer, size_t out_bu
1596
1614
);
1597
1615
return -1 ;
1598
1616
}
1599
- uint32_t length = * ( uint32_t * ) current_pos ;
1617
+ uint32_t length = load_u32_le ( current_pos ) ;
1600
1618
current_pos += 4 ;
1601
1619
if (length != self -> state .total_out ) {
1602
1620
Py_BLOCK_THREADS ;
0 commit comments