Support for decoding and encoding frames with unsynchronisation schema (ID3v2.4 only)

git-svn-id: http://php-reader.googlecode.com/svn/trunk@107 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2008-08-03 19:09:16 +00:00
parent 809bf58885
commit 741de5a2ab
10 changed files with 221 additions and 93 deletions

View File

@@ -72,7 +72,7 @@ abstract class ID3_Object
public function __construct($reader = null, &$options = array())
{
$this->_reader = $reader;
$this->_options = $options;
$this->_options = &$options;
}
/**
@@ -101,7 +101,7 @@ abstract class ID3_Object
*
* @param Array $options The options array.
*/
public function setOptions(&$options) { $this->_options = $options; }
public function setOptions(&$options) { $this->_options = &$options; }
/**
* Sets the given option the given value.
@@ -168,6 +168,49 @@ abstract class ID3_Object
($val & 0x7f0000) >> 2 | ($val & 0x7f000000) >> 3;
}
/**
* Applies the unsynchronisation scheme to the given data string.
*
* Whenever a false synchronisation is found within the data, one zeroed byte
* is inserted after the first false synchronisation byte. This has the side
* effect that all 0xff00 combinations have to be altered, so they will not
* be affected by the decoding process. Therefore all the 0xff00 combinations
* have to be replaced with the 0xff0000 combination during the
* unsynchronisation.
*
* @param string $data The input data.
* @return string
*/
protected function encodeUnsynchronisation(&$data)
{
$result = "";
for ($i = 0, $j = 0; $i < strlen($data) - 1; $i++)
if (ord($data[$i]) == 0xff &&
((($tmp = ord($data[$i + 1])) & 0xe0) == 0xe0 || $tmp == 0x0)) {
$result .= substr($data, $j, $i + 1 - $j) . "\0";
$j = $i + 1;
}
return $result . substr($data, $j);
}
/**
* Reverses the unsynchronisation scheme from the given data string.
*
* @see encodeUnsyncronisation
* @param string $data The input data.
* @return string
*/
protected function decodeUnsynchronisation(&$data)
{
$result = "";
for ($i = 0, $j = 0; $i < strlen($data) - 1; $i++)
if (ord($data[$i]) == 0xff && ord($data[$i + 1]) == 0x0) {
$result .= substr($data, $j, $i + 1 - $j);
$j = $i + 2;
}
return $result . substr($data, $j);
}
/**
* Splits UTF-16 formatted binary data up according to null terminators
* residing in the string, up to a given limit.