From 6e7cbaa3c7ab9a00ed14f9e316336366a0fa3bc1 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Sun, 1 May 2022 01:12:44 +0200 Subject: [PATCH] Update unpack method --- src/SimpleSCCU.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/SimpleSCCU.php b/src/SimpleSCCU.php index 90be4a4..83b86d4 100644 --- a/src/SimpleSCCU.php +++ b/src/SimpleSCCU.php @@ -26,29 +26,22 @@ public static function unpack (string $bin) : array { fwrite ($fp, $bin); rewind($fp); - $header = unpack('a4app/Nlength/N2/Ncount/a', fread($fp, 21)); + $header = unpack('a4app/Nlength/N2/Ncount', fread($fp, 20)); if ($header['app'] != 'SCCU') { throw new SimpleSCCUException('Invalid app mark', SimpleSCCUException::ERROR_FORMAT_UNKNOWN); } - do { - fseek($fp, -1, SEEK_CUR); - $fhead = unpack('a2mark/Nlength/na/Nb', fread($fp, 12)); + for ($i = $header['count']; $i; $i--) { + $fheader = unpack('a2mark/Nlength/na/Nb', fread($fp, 12)); - $key = ''; - while ("\x00" != $c = fread($fp, 1)) { - $key .= $c; - } + $string = fread($fp, $fheader['length'] - 12); - $value = ''; - while ("\x00" != $c = fread($fp, 1)) { - $value .= $c; - } + $key = current(unpack('Z*', $string)); + $field = unpack(sprintf('Z%ukey/x/Z*value', strlen($key)), $string); - $fields[$key] = $value; + $fields[$key] = $field['value']; + } - while ("\x00" == fread($fp, 1)); - } while (!feof($fp)); fclose($fp); return $fields;