From a3fddb495d08bb6c7d4cb7eb79d9f27059bf7c67 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Mon, 27 May 2024 20:04:43 +0100 Subject: [PATCH] Optimise id3v2.4 comment parsing --- src/Mp3Info.php | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/src/Mp3Info.php b/src/Mp3Info.php index ea90e22..32cedcd 100644 --- a/src/Mp3Info.php +++ b/src/Mp3Info.php @@ -890,34 +890,16 @@ class Mp3Info // break; case 'COMM': # Comments $dataEnd = $this->fileObj->getFilePos() + $frame_size; - $raw = $this->fileObj->getBytes(4); - $data = unpack('C1encoding/A3language', $raw); - // read until \null character - $short_description = null; - $last_null = false; - $actual_text = false; - while ($this->fileObj->getFilePos() < $dataEnd) { - $char = fgetc($fp); - if ($char == "\00" && $actual_text === false) { - if ($data['encoding'] == 0x1) { # two null-bytes for utf-16 - if ($last_null) { - $actual_text = null; - } else { - $last_null = true; - } - } else { # no condition for iso-8859-1 - $actual_text = null; - } - } - elseif ($actual_text !== false) $actual_text .= $char; - else $short_description .= $char; - } - if ($actual_text === false) $actual_text = $short_description; - // list($short_description, $actual_text) = sscanf("s".chr(0)."s", $data['texts']); - // list($short_description, $actual_text) = explode(chr(0), $data['texts']); - $this->tags2[$frame_id][$data['language']] = array( - 'short' => (bool)($data['encoding'] == 0x00) ? mb_convert_encoding($short_description, 'utf-8', 'iso-8859-1') : mb_convert_encoding($short_description, 'utf-8', 'utf-16'), - 'actual' => (bool)($data['encoding'] == 0x00) ? mb_convert_encoding($actual_text, 'utf-8', 'iso-8859-1') : mb_convert_encoding($actual_text, 'utf-8', 'utf-16'), + $encoding = unpack('C', $this->fileObj->getBytes(1))[1]; + $language = $this->fileObj->getBytes(3); + $allText_raw = $this->fileObj->getBytes($dataEnd - $this->fileObj->getFilePos()); + $allText = $this->_getUtf8Text($encoding, $allText_raw); + + list($short_description, $actual_text) = explode("\0", $allText, 2); + + $this->tags2[$frame_id][$language] = array( + 'short' => $short_description, + 'actual' => $actual_text, ); break; // case 'RVAD': # Relative volume adjustment