Skip to content

Fix bug #65106: PHP fails to compile ext/fileinfo #10422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions ext/fileinfo/create_data_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
/*--- Initialization of our translation table ---*/

// 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] );
$dta_l = strlen($dta);
$j = 0;
$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[$dta_l] = {\n";
for ($i = 0; $i < $dta_l; $i++) {
printf("0x%02X, ", ord($dta[$i]));
if ($j % 16 == 15) {
echo "\n";
}
$j++;
echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
foreach ($chunks as $chunk) {
echo '"' . strtr($chunk, $map) . '",' . "\n";
}
echo "};\n";
?>
Loading