fix SWF library read operations / Exceptions

This commit is contained in:
DataHoarder 2021-12-31 07:24:58 +01:00
parent 1ae2d8e2a7
commit 0643bcc333
3 changed files with 53 additions and 52 deletions

View file

@ -268,7 +268,7 @@ class SWFrec {
}
if ($this->io->bytePos != $bytePosEnd) {
echo sprintf("\n\nTHANOS THERE ARE %d bytes left\n", $bytePosEnd - $this->io->bytePos);
// throw new Exception("OOPS");
// throw new \Exception("OOPS");
}
// var_dump($actions);
return $actions;
@ -358,7 +358,7 @@ class SWFrec {
$val = $this->io->collectUI16();
break;
default:
throw new Exception(sprintf("Internal error: type=%d", $type));
throw new \Exception(sprintf("Internal error: type=%d", $type));
}
$actionData[] = array('type' => $type, 'val' => $val);
}
@ -384,7 +384,7 @@ class SWFrec {
$actionData['sceneBias'] = $this->io->collectUI16();
}
} else {
throw new Exception(sprintf("Internal error: actionCode=0x%02X, actionLength=%d", $actionCode, $actionLength));
throw new \Exception(sprintf("Internal error: actionCode=0x%02X, actionLength=%d", $actionCode, $actionLength));
}
return $actionData;
}

View file

@ -599,7 +599,7 @@ class SWFtag {
} else if ($ret['bitmapFormat'] == 4 || $ret['bitmapFormat'] == 5) {
$ret['pixelData'] = $data;
} else {
throw new Exception(sprintf('Internal error: bitmapFormat=%d', $ret['bitmapFormat']));
throw new \Exception(sprintf('Internal error: bitmapFormat=%d', $ret['bitmapFormat']));
}
return $ret;
}
@ -624,7 +624,7 @@ class SWFtag {
$ret['imageData'] = $this->io->collectBytes($alphaDataOffset);
$ret['alphaData'] = gzuncompress($this->io->collectBytes($bytePosEnd - $this->io->bytePos)); // ZLIB uncompress alpha channel
} else {
throw new Exception(sprintf('Internal error: version=%d', $version));
throw new \Exception(sprintf('Internal error: version=%d', $version));
}
return $ret;
}

View file

@ -331,7 +331,7 @@ class TTF {
'version' => $version,
'glyphIdArray' => $glyphIdArray);
} else if ($format == 2) {
throw new Exception('cmap format is 2');
throw new \Exception('cmap format is 2');
} else if ($format == 4) {
$segCountX2 = self::getUshort($b, $off);
$searchRange = self::getUshort($b, $off);
@ -392,9 +392,9 @@ class TTF {
$length = self::getUlong($b, $off);
$language = self::getUlong($b, $off);
if ($format == '8.0') {
throw new Exception('cmap format is 8.0');
throw new \Exception('cmap format is 8.0');
} else if ($format == '10.0') {
throw new Exception('cmap format is 10.0');
throw new \Exception('cmap format is 10.0');
} else if ($format == '12.0') {
$nGroups = self::getUlong($b, $off);
$startCharCodes = array();
@ -414,7 +414,7 @@ class TTF {
'endCharCodes' => $endCharCodes,
'startGlyphCodes' => $startGlyphCodes);
} else {
throw new Exception('Internal error: unknwon cmap format');
throw new \Exception('Internal error: unknwon cmap format');
}
}
}
@ -440,7 +440,7 @@ class TTF {
$cnt = count($table['startCharCodes']);
$length = 16 + 12 * $cnt; // Size for format 12.0 table
} else {
throw new Exception('Internal error');
throw new \Exception('Internal error');
}
$sz += $length;
$lengths[] = $length;
@ -534,7 +534,7 @@ class TTF {
self::setUlong($b, $off, $startGlyphCodes[$grp]);
}
} else {
throw new Exception('Internal error');
throw new \Exception('Internal error');
}
$offset += $lengths[$i++];
}
@ -582,14 +582,14 @@ class TTF {
} else if ($index >= 258 && $index <= 32767) {
$gn[] = $glyphNames[$index - 258];
} else {
throw new Exception(sprintf('Internal error - glyphNameIndex is %d', $index));
throw new \Exception(sprintf('Internal error - glyphNameIndex is %d', $index));
}
}
$post['glyphNames'] = $gn;
} else if ($post['formatType'] == '3.0') {
; // Nothing more
} else {
throw new Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
throw new \Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
}
return $post;
}
@ -611,7 +611,7 @@ class TTF {
} else if ($post['formatType'] == '3.0') {
; // Nothing more
} else {
throw new Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
throw new \Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
}
$b = str_repeat(chr(0), $sz);
@ -649,7 +649,7 @@ class TTF {
} else if ($post['formatType'] == '3.0') {
; // Nothing more
} else {
throw new Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
throw new \Exception(sprintf('Internal error - formatType is %s', $post['formatType']));
}
return $b;
}
@ -716,11 +716,11 @@ class TTF {
// Calculate the checksum adjustment for 'head' table
$checksum = self::calculateTableChecksum(substr($sb, 0, 12 + 16 * $numTables));
foreach ($checksums as $chk) {
$checksum = bcadd($checksum, $chk);
$checksum += $chk;
}
$checksum = bcsub('2981146554', $checksum); // This is "0xB1B0AFBA"
while (bccomp($checksum, '0') < 0) {
$checksum = bcadd($checksum, '4294967296'); // This is "0x100000000"
$checksum = 0xB1B0AFBA - $checksum;
while ($checksum < 0) {
$checksum += 4294967296; // This is "0x100000000"
}
$off = $offsets['head'] + 8;
self::setUlong($sb, $off, $checksum);
@ -781,7 +781,7 @@ class TTF {
return $glyphIdArray[$charCode - $firstCode];
}
} else {
throw new Exception('Internal error');
throw new \Exception('Internal error');
}
return -1;
}
@ -833,7 +833,7 @@ class TTF {
}
}
} else {
throw new Exception('Internal error');
throw new \Exception('Internal error');
}
return null;
}
@ -1007,7 +1007,7 @@ class TTF {
}
private static function calculateTableChecksum($data) {
$ret = '0';
$ret = 0;
// "Right" pad with zeros
while ((strlen($data) % 4) != 0) {
@ -1028,18 +1028,19 @@ class TTF {
}
private static function setByte(&$b, &$off, $val) {
$b{$off++} = chr($val);
$b[$off++] = chr($val);
}
private static function getUshort($b, &$off) {
$num = ord($b[$off++]);
$num = 256 * $num + ord($b[$off++]);
return $num;
$ret = 0;
$ret += ord($b[$off++]) << 8;
$ret += ord($b[$off++]);
return $ret;
}
private static function setUshort(&$b, &$off, $val) {
$b{$off++} = chr($val / 256);
$b{$off++} = chr($val % 256);
$b[$off++] = chr(($val >> 8) & 0xFF);
$b[$off++] = chr(($val) & 0xFF);
}
private static function getShort($b, &$off) {
@ -1048,29 +1049,29 @@ class TTF {
}
private static function setShort(&$b, &$off, $val) {
$b{$off++} = chr(($val >> 8) & 0xff);
$b{$off++} = chr($val & 0xff);
$b[$off++] = chr(($val >> 8) & 0xff);
$b[$off++] = chr($val & 0xff);
}
private static function getUlong($b, &$off) {
$ret = '0';
$ret = bcadd($ret, bcmul(ord($b[$off++]), '16777216'));
$ret = bcadd($ret, bcmul(ord($b[$off++]), '65536'));
$ret = bcadd($ret, bcmul(ord($b[$off++]), '256'));
$ret = bcadd($ret, ord($b[$off++]));
$ret = 0;
$ret += ord($b[$off++]) << 24;
$ret += ord($b[$off++]) << 16;
$ret += ord($b[$off++]) << 8;
$ret += ord($b[$off++]);
return $ret;
}
private static function setUlong(&$b, &$off, $val) {
$b{$off++} = chr(bcmod(bcdiv($val, '16777216', 0), '256'));
$b{$off++} = chr(bcmod(bcdiv($val, '65536', 0), '256'));
$b{$off++} = chr(bcmod(bcdiv($val, '256', 0), '256'));
$b{$off++} = chr(bcmod($val, '256'));
$b[$off++] = chr(($val >> 24) & 0xFF);
$b[$off++] = chr(($val >> 16) & 0xFF);
$b[$off++] = chr(($val >> 8) & 0xFF);
$b[$off++] = chr(($val) & 0xFF);
}
static function getLong($b, &$off) {
$ret = self::getUlong($b, $off);
return bccomp($ret, '2147483648') < 0 ? $ret : bcsub($ret, '4294967296');
return $ret < 2147483648 ? $ret : $ret - 4294967296; //TODO: hmm
}
static function getFixed($b, &$off) {
@ -1094,8 +1095,8 @@ class TTF {
}
}
static function setFixed(&$b, &$off, $val) {
if ($val{0} == '-') {
static function setFixed(&$b, &$off, string $val) {
if ($val[0] == '-') {
$sign = -1;
$val = substr($val, 1);
} else {
@ -1110,10 +1111,10 @@ class TTF {
}
$mantissa *= $sign;
$b{$off++} = chr(($mantissa >> 8) & 0xff);
$b{$off++} = chr(($mantissa >> 0) & 0xff);
$b{$off++} = chr(($fraction >> 8) & 0xff);
$b{$off++} = chr(($fraction >> 0) & 0xff);
$b[$off++] = chr(($mantissa >> 8) & 0xff);
$b[$off++] = chr(($mantissa >> 0) & 0xff);
$b[$off++] = chr(($fraction >> 8) & 0xff);
$b[$off++] = chr(($fraction >> 0) & 0xff);
}
private static function getFword($b, &$off) {
@ -1133,8 +1134,8 @@ class TTF {
}
private static function getF2dot14($b, &$off) {
$val1 = ord($b{$off});
$val2 = ord($b{$off + 1});
$val1 = ord($b[$off]);
$val2 = ord($b[$off + 1]);
$val = 256 * $val1 + $val2;
$mantissa = ($val >> 14) & 0x03;
@ -1163,7 +1164,7 @@ class TTF {
private static function setRaw(&$b, &$off, $val, $num) {
$i = 0;
while ($i < $num) {
$b{$off++} = $val{$i++};
$b[$off++] = $val[$i++];
}
}
@ -1196,7 +1197,7 @@ class TTF {
$bit1 = $flag & $mask1;
$bit4 = $flag & $mask2;
if ($bit1 != 0) {
$b = ord($code{$off++});
$b = ord($code[$off++]);
if ($bit4 != 0) {
// Positive 8-bit
$val = $b;
@ -1210,8 +1211,8 @@ class TTF {
$val = 0;
} else {
// Signed 16-bit
$b1 = ord($code{$off++});
$b2 = ord($code{$off++});
$b1 = ord($code[$off++]);
$b2 = ord($code[$off++]);
$b = 256 * $b1 + $b2;
if ($b >= 32768)
$b -= 65536;