xref: /PHP-8.1/ext/phar/makestub.php (revision 01b3fc03)
1<?php
2$s = str_replace("\r", '', file_get_contents(dirname(__FILE__) . '/shortarc.php'));
3
4$s .= "\nExtract_Phar::go();\n__HALT_COMPILER();";
5$news = '';
6foreach (token_get_all($s) as $token) {
7    if (is_array($token)) {
8        if ($token[0] == T_COMMENT) {
9            $token[1] = '';
10        }
11        if ($token[0] == T_WHITESPACE) {
12            $n = str_repeat("\n", substr_count($token[1], "\n"));
13            $token[1] = strlen($n) ? $n : ' ';
14        }
15        $news .= $token[1];
16    } else {
17        $news .= $token;
18    }
19}
20$s = $news . ' ?>';
21$slen = strlen($s) - strlen('index.php') - strlen("000");
22$s = str_replace('\\', '\\\\', $s);
23$s = str_replace('"', '\\"', $s);
24$s = str_replace("\n", '\n', $s);
25// now we need to find the location of web index file
26$webs = substr($s, 0, strpos($s, "000"));
27$s = substr($s, strlen($webs) + strlen("000"));
28$s1 = substr($s, 0, strpos($s, 'index.php'));
29$s2 = substr($s, strlen($s1) + strlen('index.php'));
30$s2 = substr($s2, 0, strpos($s2, 'XXXX'));
31$s3 = substr($s, strlen($s2) + 4 + strlen($s1) + strlen('index.php'));
32
33$stub = '/*
34  +----------------------------------------------------------------------+
35  | phar php single-file executable PHP extension generated stub         |
36  +----------------------------------------------------------------------+
37  | Copyright (c) The PHP Group                                          |
38  +----------------------------------------------------------------------+
39  | This source file is subject to version 3.01 of the PHP license,      |
40  | that is bundled with this package in the file LICENSE, and is        |
41  | available through the world-wide-web at the following url:           |
42  | https://www.php.net/license/3_01.txt                                 |
43  | If you did not receive a copy of the PHP license and are unable to   |
44  | obtain it through the world-wide-web, please send a note to          |
45  | license@php.net so we can mail you a copy immediately.               |
46  +----------------------------------------------------------------------+
47  | Authors: Gregory Beaver <cellog@php.net>                             |
48  +----------------------------------------------------------------------+
49*/
50
51static inline void phar_get_stub(const char *index_php, const char *web, size_t *len, char **stub, const int name_len, const int web_len)
52{
53';
54$s1split = str_split($s1, 2046);
55$s3split = str_split($s3, 2046);
56$took = false;
57foreach ($s1split as $i => $chunk) {
58    if ($took) {
59        $s1split[$i] = substr($chunk, 1);
60        $took = false;
61    }
62    if ($chunk[strlen($chunk) - 1] == '\\') {
63        $s1split[$i] .= $s1split[$i + 1][0];
64        $took = true;
65    }
66}
67foreach ($s3split as $i => $chunk) {
68    if ($took) {
69        $s3split[$i] = substr($chunk, 1);
70        $took = false;
71    }
72    if ($chunk[strlen($chunk) - 1] == '\\') {
73        $s3split[$i] .= $s3split[$i + 1][0];
74        $took = true;
75    }
76}
77$stub .= "\tstatic const char newstub0[] = \"" . $webs . '";
78';
79foreach ($s1split as $i => $chunk) {
80    $s1count = $i + 1;
81    $stub .= "\tstatic const char newstub1_" . $i . '[] = "' . $chunk . '";
82';
83}
84$stub .= "\tstatic const char newstub2[] = \"" . $s2 . "\";
85";
86foreach ($s3split as $i => $chunk) {
87    $s3count = $i + 1;
88    $stub .= "\tstatic const char newstub3_" . $i . '[] = "' . $chunk . '";
89';
90}
91$stub .= "\n\tstatic const int newstub_len = " . $slen . ";
92
93\t*len = spprintf(stub, name_len + web_len + newstub_len, \"%s%s" . str_repeat('%s', $s1count) . '%s%s%d'
94    . str_repeat('%s', $s3count) . '", newstub0, web';
95foreach ($s1split as $i => $unused) {
96    $stub .= ', newstub1_' . $i;
97}
98$stub .= ', index_php, newstub2';
99$stub .= ", name_len + web_len + newstub_len";
100foreach ($s3split as $i => $unused) {
101    $stub .= ', newstub3_' . $i;
102}
103$stub .= ");
104}";
105
106file_put_contents(dirname(__FILE__) . '/stub.h', $stub."\n");
107?>
108