aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/WebAssembly/BinaryFormat
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-07-13 15:37:47 +0900
committernsfisis <nsfisis@gmail.com>2025-07-13 15:37:50 +0900
commitc50662e1e81289c44b4da4107944221328bb5f1c (patch)
treeda3af6225c513f548e040955fb32bd4a25ff636d /src/WebAssembly/BinaryFormat
parent43806ead64146d285e846d8809c976b60e8abd87 (diff)
downloadphp-waddiwasi-c50662e1e81289c44b4da4107944221328bb5f1c.tar.gz
php-waddiwasi-c50662e1e81289c44b4da4107944221328bb5f1c.tar.zst
php-waddiwasi-c50662e1e81289c44b4da4107944221328bb5f1c.zip
fix: utf-8 decoding
Diffstat (limited to 'src/WebAssembly/BinaryFormat')
-rw-r--r--src/WebAssembly/BinaryFormat/Decoder.php31
1 files changed, 2 insertions, 29 deletions
diff --git a/src/WebAssembly/BinaryFormat/Decoder.php b/src/WebAssembly/BinaryFormat/Decoder.php
index f0a5c29..ff23a5d 100644
--- a/src/WebAssembly/BinaryFormat/Decoder.php
+++ b/src/WebAssembly/BinaryFormat/Decoder.php
@@ -1119,35 +1119,8 @@ final class Decoder
*/
private function implodeUtf8BytesToString(array $bytes): ?string
{
- $s = '';
- $count = count($bytes);
- for ($i = 0; $i < $count; $i++) {
- if (($bytes[$i] & 0x80) === 0) {
- $code = $bytes[$i];
- } elseif (($bytes[$i] & 0xE0) === 0xC0) {
- if ($count <= $i + 1) {
- return null;
- }
- $code = (($bytes[$i] & 0x1F) << 6) | ($bytes[$i + 1] & 0x3F);
- $i++;
- } elseif (($bytes[$i] & 0xF0) === 0xE0) {
- if ($count <= $i + 2) {
- return null;
- }
- $code = (($bytes[$i] & 0x0F) << 12) | (($bytes[$i + 1] & 0x3F) << 6) | ($bytes[$i + 2] & 0x3F);
- $i += 2;
- } elseif (($bytes[$i] & 0xF8) === 0xF0) {
- if ($count <= $i + 3) {
- return null;
- }
- $code = (($bytes[$i] & 0x07) << 18) | (($bytes[$i + 1] & 0x3F) << 12) | (($bytes[$i + 2] & 0x3F) << 6) | ($bytes[$i + 3] & 0x3F);
- $i += 3;
- } else {
- return null;
- }
- $s .= mb_chr($code, 'UTF-8');
- }
- return $s;
+ $s = pack('C*', ...$bytes);
+ return mb_check_encoding($s, 'UTF-8') ? $s : null;
}
/**