Skip to content

Commit b6a0c53

Browse files
committed
Fix bug #65106: PHP fails to compile ext/fileinfo
Make data_file.c's generator output its array initialization "by list of strings", instead of "by list of chars": that makes the compiler happier. Use strtr() with a precomputed map, instead of a loop over ord(), to generate in 1/100th the time.
1 parent f8f7fd2 commit b6a0c53

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

ext/fileinfo/create_data_file.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
/* This is a generated file, do not modify */
33
/* Usage: php create_data_file.php /path/to/magic.mgc > data_file.c */
44
<?php
5+
/*--- Initialization of our translation table ---*/
6+
7+
for ($i = 0; $i < 0x100; ++$i) {
8+
$map[chr($i)] = sprintf('\x%02X', $i);
9+
}
10+
// \0 is a shortcut for \x00; as the majority of the input file is \0's,
11+
// we divide the generated file's size by nearly 2 (30 MB -> 16 MB).
12+
$map[chr(0)] = '\0';
13+
14+
/*--- File generation ---*/
15+
16+
// https://github.com/php/php-src/pull/10422
17+
// Some compilers (GCC, clang) do not like long lists; some (MSVC) do not like long strings.
18+
// CHUNK_SIZE splitting our ~10 MB binary source should give a good compromise between both.
19+
const CHUNK_SIZE = 1024;
20+
521
$dta = file_get_contents( $argv[1] );
6-
$dta_l = strlen($dta);
7-
$j = 0;
22+
$chunks = str_split($dta, CHUNK_SIZE);
23+
$chunks[count($chunks) - 1] = str_pad($chunks[count($chunks) - 1], CHUNK_SIZE, chr(0));
824

9-
echo "const unsigned char php_magic_database[$dta_l] = {\n";
10-
for ($i = 0; $i < $dta_l; $i++) {
11-
printf("0x%02X, ", ord($dta[$i]));
12-
if ($j % 16 == 15) {
13-
echo "\n";
14-
}
15-
$j++;
25+
echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
26+
foreach ($chunks as $chunk) {
27+
echo '"' . strtr($chunk, $map) . "\",\n";
1628
}
1729
echo "};\n";
1830
?>

0 commit comments

Comments
 (0)