1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

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.

Avoids ambiguous 0x tokens (as specified in C standards), by using octal.

Closes GH-10422.
This commit is contained in:
Guillaume Outters
2023-01-31 20:40:11 +01:00
committed by Niels Dossche
parent 6150bf5ee4
commit bcd3eec44a
2 changed files with 8355 additions and 532145 deletions

View File

@@ -2,17 +2,46 @@
/* This is a generated file, do not modify */
/* Usage: php create_data_file.php /path/to/magic.mgc > data_file.c */
<?php
$dta = file_get_contents( $argv[1] );
$dta_l = strlen($dta);
$j = 0;
/*--- Initialization of our translation table ---*/
echo "const unsigned char php_magic_database[$dta_l] = {\n";
for ($i = 0; $i < $dta_l; $i++) {
printf("0x%02X, ", ord($dta[$i]));
if ($j % 16 == 15) {
echo "\n";
}
$j++;
// By default, everything gets mapped to its \o notation
// (not \x, because by C's norm, \x eats as many chars as possible, while \o stops at exactly 3;
// thus \x0ABACK_2_MALICE is interpreted as hex \x0ABAC (which overflows) followed by string K_2_MALICE,
// while \o0120 is unambiguously a CR followed by digit 0).
for ($i = 0; $i < 0x100; ++$i) {
$map[chr($i)] = sprintf('\%03o', $i);
}
// \0 is a shortcut for \x00; as the majority of the input file is \0's,
// we divide the generated file's size by nearly 2 (30 MB -> 16 MB).
$map[chr(0)] = '\0';
$map["\n"] = '\n';
// Displayable ASCII can be output as is: strings for file types will appear readable.
for ($i = ord(' '); $i < 0x7F; ++$i) {
$map[chr($i)] = chr($i);
}
// … Except digits following a \0: \012 will be interpreted as octal 012, and not \0 followed by 12.
// Then we have to express \0 in a full unambiguous 3-chars octal code.
for ($i = ord('0'); $i <= ord('9'); ++$i) {
$map[chr(0).chr($i)] = '\000'.chr($i);
}
// … Except " and \ because we enclose the result into quotes and escape with \.
$map['"'] = '\"';
$map['\\'] = '\\\\';
/*--- File generation ---*/
// https://github.com/php/php-src/pull/10422
// Some compilers (GCC, clang) do not like long lists; some (MSVC) do not like long strings.
// CHUNK_SIZE splitting our ~10 MB binary source should give a good compromise between both.
const CHUNK_SIZE = 1024;
$dta = file_get_contents( $argv[1] );
$chunks = str_split($dta, CHUNK_SIZE);
$chunks[count($chunks) - 1] = str_pad($chunks[count($chunks) - 1], CHUNK_SIZE, chr(0));
echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
foreach ($chunks as $chunk) {
echo '"' . strtr($chunk, $map) . '",' . "\n";
}
echo "};\n";
?>

File diff suppressed because it is too large Load Diff