|
4 | 4 | <?php
|
5 | 5 | /*--- Initialization of our translation table ---*/
|
6 | 6 |
|
| 7 | + // By default, everything gets mapped to its \o notation |
| 8 | + // (not \x, because by C's norm, \x eats as many chars as possible, while \o stops at exactly 3; |
| 9 | + // thus \x0ABACK_2_MALICE is interpreted as hex \x0ABAC (which overflows) followed by string K_2_MALICE, |
| 10 | + // while \o0120 is unambiguously a CR followed by digit 0). |
7 | 11 | for ($i = 0; $i < 0x100; ++$i) {
|
8 |
| - $map[chr($i)] = sprintf('\x%02X', $i); |
| 12 | + $map[chr($i)] = sprintf('\%03o', $i); |
9 | 13 | }
|
10 | 14 | // \0 is a shortcut for \x00; as the majority of the input file is \0's,
|
11 | 15 | // we divide the generated file's size by nearly 2 (30 MB -> 16 MB).
|
12 | 16 | $map[chr(0)] = '\0';
|
| 17 | + $map["\n"] = '\n'; |
| 18 | + // Displayable ASCII can be output as is: strings for file types will appear readable. |
| 19 | + for ($i = ord(' '); $i < 0x7F; ++$i) { |
| 20 | + $map[chr($i)] = chr($i); |
| 21 | + } |
| 22 | + // … Except digits following a \0: \012 will be interpreted as octal 012, and not \0 followed by 12. |
| 23 | + // Then we have to express \0 in a full unambiguous 3-chars octal code. |
| 24 | + for ($i = ord('0'); $i <= ord('9'); ++$i) { |
| 25 | + $map[chr(0).chr($i)] = '\000'.chr($i); |
| 26 | + } |
| 27 | + // … Except " and \ because we enclose the result into quotes and escape with \. |
| 28 | + $map['"'] = '\"'; |
| 29 | + $map['\\'] = '\\\\'; |
13 | 30 |
|
14 | 31 | /*--- File generation ---*/
|
15 | 32 |
|
|
24 | 41 |
|
25 | 42 | echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
|
26 | 43 | foreach ($chunks as $chunk) {
|
27 |
| - echo '"' . strtr($chunk, $map) . "\",\n"; |
| 44 | + echo '"' . strtr($chunk, $map) . '",' . "\n"; |
28 | 45 | }
|
29 | 46 | echo "};\n";
|
30 | 47 | ?>
|
0 commit comments