|
2 | 2 | /* This is a generated file, do not modify */
|
3 | 3 | /* Usage: php create_data_file.php /path/to/magic.mgc > data_file.c */
|
4 | 4 | <?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 | + |
5 | 21 | $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)); |
8 | 24 |
|
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"; |
16 | 28 | }
|
17 | 29 | echo "};\n";
|
18 | 30 | ?>
|
0 commit comments