Helper for Syncsafe size values

This commit is contained in:
Markus Birth 2024-05-27 20:03:14 +01:00
parent c0d64e81aa
commit d474f40403

View File

@ -254,6 +254,13 @@ class Mp3Info
return $data; return $data;
} }
protected function getSyncsafeSize(string $rawBytes): int
{
$sizeBytes = unpack('C4', $rawBytes);
$size = $sizeBytes[1] << 21 | $sizeBytes[2] << 14 | $sizeBytes[3] << 7 | $sizeBytes[4];
return $size;
}
/** /**
* Reads audio file in binary mode. * Reads audio file in binary mode.
* mpeg audio file structure: * mpeg audio file structure:
@ -278,8 +285,7 @@ class Mp3Info
} else { } else {
$this->fileObj->seekForward(2); // 2 bytes of tag version $this->fileObj->seekForward(2); // 2 bytes of tag version
$this->fileObj->seekForward(1); // 1 byte of tag flags $this->fileObj->seekForward(1); // 1 byte of tag flags
$sizeBytes = unpack('C4', $this->fileObj->getBytes(4)); $size = $this->getSyncsafeSize($this->fileObj->getBytes(4));
$size = $sizeBytes[1] << 21 | $sizeBytes[2] << 14 | $sizeBytes[3] << 7 | $sizeBytes[4];
$size += 10; // add header size $size += 10; // add header size
$audioSize -= ($this->_id3Size = $size); $audioSize -= ($this->_id3Size = $size);
} }
@ -745,17 +751,16 @@ class Mp3Info
protected function parseId3v24Body($lastByte) protected function parseId3v24Body($lastByte)
{ {
while ($this->fileObj->getFilePos() < $lastByte) { while ($this->fileObj->getFilePos() < $lastByte) {
$raw = $this->fileObj->getBytes(10); $frame_id = $this->fileObj->getBytes(4);
$frame_id = substr($raw, 0, 4);
if ($frame_id == str_repeat(chr(0), 4)) { if ($frame_id == str_repeat(chr(0), 4)) {
$this->fileObj->seekTo($lastByte); $this->fileObj->seekTo($lastByte);
break; break;
} }
$data = unpack('C4frame_size/H2flags', substr($raw, 4)); $frame_size = $this->getSyncsafeSize($this->fileObj->getBytes(4));
$frame_size = $data['frame_size1'] << 21 | $data['frame_size2'] << 14 | $data['frame_size3'] << 7 | $data['frame_size4'];
$data = unpack('H2flags', $this->fileObj->getBytes(2));
$flags = base_convert($data['flags'], 16, 2); $flags = base_convert($data['flags'], 16, 2);
$this->id3v2TagsFlags[$frame_id] = array( $this->id3v2TagsFlags[$frame_id] = array(
'frame_size' => $frame_size, 'frame_size' => $frame_size,