Skip to content

Commit 064fa4b

Browse files
committed
php: fileinfo encore plus compilable
= On tente d'utiliser les modifs proposées en php/php-src#10422. darcs-hash:b20b797bc93bc04e5940871d382d85a414ec7d97
1 parent f6891af commit 064fa4b

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ v 8.3.3 || true
195195
# Si on nous demande de nous installer sous l'alias phpx, on renseigne le numéro de version à la place du 'x'.
196196
aliasVersion 'x'
197197

198+
# Si un PHP fonctionnel est déniché, on s'en servira pour de l'autogénération.
199+
ANCIEN_PHP="`command -v php`"
200+
[ -z "$ANCIEN_PHP" ] || $ANCIEN_PHP --version | grep -q Zend || ANCIEN_PHP=
201+
198202
# Si certains logiciels sont déjà installés, on laisse le configure PHP les détecter, mais on s'assure auparavant que ce sera notre version qu'il détectera, en l'ajoutant aux prérequis.
199203
if optionSi postgresql sh -c 'psql --version 2> /dev/null | grep -q PostgreSQL'
200204
then
@@ -271,7 +275,18 @@ atomicconst()
271275
fileinfoSobre()
272276
{
273277
# https://bugs.php.net/bug.php?id=65106
278+
if [ -n "$ANCIEN_PHP" ]
279+
then
280+
(
281+
cd ext/fileinfo &&
282+
cp "$SCRIPTS/php.data_file_to_mgc.c" ./ &&
283+
$CC -o /tmp/data_file_to_mgc php.data_file_to_mgc.c &&
284+
/tmp/data_file_to_mgc &&
285+
$ANCIEN_PHP "$SCRIPTS/php.create_data_file.php" magic.mgc > data_file.c
286+
)
287+
else
274288
filtrer ext/fileinfo/data_file.c sed -e 's#^0x#"\\x#' -e 's#, 0x#\\x#g' -e 's#, *$#"#' -e 's#{ *$##' -e 's#}##' -e 's#, *;#";#'
289+
fi
275290
}
276291

277292
cve201911043()

php.create_data_file.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env php
2+
/* This is a generated file, do not modify */
3+
/* Usage: php create_data_file.php /path/to/magic.mgc > data_file.c */
4+
<?php
5+
/*--- Initialization of our translation table ---*/
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).
11+
for ($i = 0; $i < 0x100; ++$i) {
12+
$map[chr($i)] = sprintf('\%03o', $i);
13+
}
14+
// \0 is a shortcut for \x00; as the majority of the input file is \0's,
15+
// we divide the generated file's size by nearly 2 (30 MB -> 16 MB).
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['\\'] = '\\\\';
30+
31+
/*--- File generation ---*/
32+
33+
// https://github.com/php/php-src/pull/10422
34+
// Some compilers (GCC, clang) do not like long lists; some (MSVC) do not like long strings.
35+
// CHUNK_SIZE splitting our ~10 MB binary source should give a good compromise between both.
36+
const CHUNK_SIZE = 1024;
37+
38+
$dta = file_get_contents( $argv[1] );
39+
$chunks = str_split($dta, CHUNK_SIZE);
40+
$chunks[count($chunks) - 1] = str_pad($chunks[count($chunks) - 1], CHUNK_SIZE, chr(0));
41+
42+
echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
43+
foreach ($chunks as $chunk) {
44+
echo '"' . strtr($chunk, $map) . '",' . "\n";
45+
}
46+
echo "};\n";
47+
?>

php.data_file_to_mgc.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <fcntl.h>
2+
#include <unistd.h>
3+
4+
#include "data_file.c"
5+
6+
int main(int argc, char ** argv)
7+
{
8+
int out = open("magic.mgc", O_WRONLY|O_CREAT|O_TRUNC, 0644);
9+
write(out, php_magic_database, sizeof(php_magic_database));
10+
close(out);
11+
return 0;
12+
}

0 commit comments

Comments
 (0)