/* Decoded by unphp.net */ * * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ declare(strict_types=1); namespace Zephir\Backend; use Zephir\FileSystem\HardDisk; use Zephir\StringsManager as BaseStringsManager; use function count; use function implode; use function ksort; use function strlen; use function strtoupper; use function substr; use const PHP_EOL; use const SORT_STRING; /** * Manages the concatenation keys for the extension and the interned strings */ class StringsManager extends BaseStringsManager { /** * Adds a concatenation combination to the manager. */ public function addConcatKey(string $key): void { $this->concatKeys[$key] = true; } /** * Generates the concatenation code. */ public function genConcatCode(): void { $code = ' #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include "php_ext.h" #include #include "ext.h" #include "kernel/main.h" #include "kernel/memory.h" #include "kernel/concat.h"' . PHP_EOL . PHP_EOL; $pcodeh = ' #ifndef ZEPHIR_KERNEL_CONCAT_H #define ZEPHIR_KERNEL_CONCAT_H #include #include #include "kernel/main.h" '; $codeh = ''; $macros = []; ksort($this->concatKeys, SORT_STRING); foreach ($this->concatKeys as $key => $one) { $len = strlen($key); $params = []; $zvalCopy = []; $useCopy = []; $avars = []; $zvars = []; $lengths = []; $sparams = []; $lparams = []; for ($i = 0; $i < $len; ++$i) { $n = $i + 1; $t = substr($key, $i, 1); $sparams[] = 'op' . $n; if ('s' == $t) { $params[] = 'const char *op' . $n . ', uint32_t op' . $n . '_len'; $lparams[] = 'op' . $n . ', sizeof(op' . $n . ')-1'; $lengths[] = 'op' . $n . '_len'; $avars[$n] = 's'; } else { $params[] = 'zval *op' . $n; $lparams[] = 'op' . $n; $zvalCopy[] = 'op' . $n . '_copy'; $useCopy[] = 'use_copy' . $n . ' = 0'; $lengths[] = 'Z_STRLEN_P(op' . $n . ')'; $zvars[] = $n; $avars[$n] = 'v'; } } $macros[] = '#define ZEPHIR_CONCAT_' . strtoupper($key) . '(result, ' . implode(', ', $sparams) . ') \' . PHP_EOL . " " . ' zephir_concat_' . $key . '(result, ' . implode(', ', $lparams) . ', 0);'; $macros[] = '#define ZEPHIR_SCONCAT_' . strtoupper($key) . '(result, ' . implode(', ', $sparams) . ') \' . PHP_EOL . " " . ' zephir_concat_' . $key . '(result, ' . implode(', ', $lparams) . ', 1);'; $macros[] = ''; $proto = 'void zephir_concat_' . $key . '(zval *result, ' . implode(', ', $params) . ', int self_var)'; $codeh .= $proto . ';' . PHP_EOL; $code .= $proto . '{' . PHP_EOL . PHP_EOL; if (count($zvalCopy)) { $code .= " " . 'zval result_copy, ' . implode(', ', $zvalCopy) . ';' . PHP_EOL; $code .= " " . 'int use_copy = 0, ' . implode(', ', $useCopy) . ';' . PHP_EOL; } else { $code .= " " . 'zval result_copy;' . PHP_EOL; $code .= " " . 'int use_copy = 0;' . PHP_EOL; } $code .= " " . 'size_t offset = 0, length;' . PHP_EOL . PHP_EOL; foreach ($zvars as $zvar) { $code .= " " . 'if (Z_TYPE_P(op' . $zvar . ') != IS_STRING) {' . PHP_EOL; $code .= " " . ' use_copy' . $zvar . ' = zend_make_printable_zval(op' . $zvar . ', &op' . $zvar . '_copy);' . PHP_EOL; $code .= " " . ' if (use_copy' . $zvar . ') {' . PHP_EOL; $code .= " " . ' op' . $zvar . ' = &op' . $zvar . '_copy;' . PHP_EOL; $code .= " " . ' }' . PHP_EOL; $code .= " " . '}' . PHP_EOL . PHP_EOL; } $code .= " " . 'length = ' . implode(' + ', $lengths) . ';' . PHP_EOL; $code .= " " . 'if (self_var) {' . PHP_EOL; $code .= PHP_EOL; $code .= " " . 'if (Z_TYPE_P(result) != IS_STRING) {' . PHP_EOL; $code .= " " . 'use_copy = zend_make_printable_zval(result, &result_copy);' . PHP_EOL; $code .= " " . 'if (use_copy) {' . PHP_EOL; $code .= " " . 'ZEPHIR_CPY_WRT_CTOR(result, (&result_copy));' . PHP_EOL; $code .= " " . '}' . PHP_EOL; $code .= " " . '}' . PHP_EOL . PHP_EOL; $code .= " " . 'offset = Z_STRLEN_P(result);' . PHP_EOL; $code .= " " . 'length += offset;' . PHP_EOL; $code .= " " . 'Z_STR_P(result) = zend_string_realloc(Z_STR_P(result), length, 0);' . PHP_EOL; $code .= PHP_EOL; $code .= " " . '} else {' . PHP_EOL; $code .= " " . 'ZVAL_STR(result, zend_string_alloc(length, 0));' . PHP_EOL; $code .= " " . '}' . PHP_EOL . PHP_EOL; $position = ''; foreach ($avars as $n => $type) { if ('s' == $type) { $code .= " " . 'memcpy(Z_STRVAL_P(result) + offset' . $position . ', op' . $n . ', op' . $n . '_len);' . PHP_EOL; $position .= ' + op' . $n . '_len'; } else { $code .= " " . 'memcpy(Z_STRVAL_P(result) + offset' . $position . ', Z_STRVAL_P(op' . $n . '), Z_STRLEN_P(op' . $n . '));' . PHP_EOL; $position .= ' + Z_STRLEN_P(op' . $n . ')'; } } $code .= " " . 'Z_STRVAL_P(result)[length] = 0;' . PHP_EOL; $code .= " " . 'zend_string_forget_hash_val(Z_STR_P(result));' . PHP_EOL; foreach ($zvars as $zvar) { $code .= " " . 'if (use_copy' . $zvar . ') {' . PHP_EOL; $code .= " " . ' zval_dtor(op' . $zvar . ');' . PHP_EOL; $code .= " " . '}' . PHP_EOL . PHP_EOL; } $code .= " " . 'if (use_copy) {' . PHP_EOL; $code .= " " . ' zval_dtor(&result_copy);' . PHP_EOL; $code .= " " . '}' . PHP_EOL . PHP_EOL; $code .= '}' . PHP_EOL . PHP_EOL; } $code .= <<