Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

Commit

Permalink
Upgrade to Confetti 0.2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Apr 8, 2014
1 parent 04b8e40 commit 58e846a
Show file tree
Hide file tree
Showing 34 changed files with 578 additions and 521 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"require": {
"php": ">=5.3",
"eloquent/confetti": "~0.1.0",
"eloquent/confetti": "~0.2.0",
"evenement/evenement": ">=1,<3",
"icecave/isolator": "~2",
"react/stream": ">=0.3,<0.5"
Expand Down
14 changes: 8 additions & 6 deletions src/Base16/Base16DecodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Eloquent\Confetti\AbstractTransform;
use Eloquent\Confetti\TransformInterface;
use Eloquent\Endec\Exception\InvalidEncodedDataException;
use Exception;

/**
* Decodes data using base16 (hexadecimal) encoding.
Expand Down Expand Up @@ -56,22 +55,25 @@ public static function instance()
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
$consume = $this->blocksSize(strlen($data), 2, $isEnd);
if (!$consume) {
return array('', 0);
return array('', 0, null);
}

$consumedData = substr($data, 0, $consume);
if (!preg_match('/^([[:xdigit:]]{2})+$/', $consumedData)) {
throw new InvalidEncodedDataException('base16', $consumedData);
return array(
'',
0,
new InvalidEncodedDataException('base16', $consumedData)
);
}

return array(pack('H*', $consumedData), $consume);
return array(pack('H*', $consumedData), $consume, null);
}

private static $instance;
Expand Down
6 changes: 2 additions & 4 deletions src/Base16/Base16EncodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Eloquent\Endec\Base16;

use Eloquent\Confetti\TransformInterface;
use Exception;

/**
* Encodes data using base16 (hexadecimal) encoding.
Expand Down Expand Up @@ -54,12 +53,11 @@ public static function instance()
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
return array(strtoupper(bin2hex($data)), strlen($data));
return array(strtoupper(bin2hex($data)), strlen($data), null);
}

private static $instance;
Expand Down
98 changes: 52 additions & 46 deletions src/Base32/AbstractBase32DecodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Eloquent\Confetti\TransformInterface;
use Eloquent\Endec\Exception\EncodingExceptionInterface;
use Eloquent\Endec\Exception\InvalidEncodedDataException;
use Exception;

/**
* An abstract base class for implementing base32 decode transforms.
Expand Down Expand Up @@ -42,71 +41,78 @@ abstract class AbstractBase32DecodeTransform implements TransformInterface
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
$paddedLength = strlen($data);
$data = rtrim($data, '=');
$length = strlen($data);
$consumedBytes = intval($length / 8) * 8;
$consumed = intval($length / 8) * 8;
$index = 0;
$output = '';

while ($index < $consumedBytes) {
$output .= $this->map8(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++)
);
}

if (($isEnd || $paddedLength > $length) && $consumedBytes !== $length) {
$remaining = $length - $consumedBytes;
$consumedBytes = $length;

if (2 === $remaining) {
$output .= $this->map2(
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (4 === $remaining) {
$output .= $this->map4(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (5 === $remaining) {
$output .= $this->map5(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
try {
while ($index < $consumed) {
$output .= $this->map8(
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (7 === $remaining) {
$output .= $this->map7(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
$this->mapByte($data, $index++)
);
} else {
throw new InvalidEncodedDataException($this->key(), $data);
}

if (($isEnd || $paddedLength > $length) && $consumed !== $length) {
$remaining = $length - $consumed;
$consumed = $length;

if (2 === $remaining) {
$output .= $this->map2(
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (4 === $remaining) {
$output .= $this->map4(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (5 === $remaining) {
$output .= $this->map5(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} elseif (7 === $remaining) {
$output .= $this->map7(
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index++),
$this->mapByte($data, $index)
);
} else {
return array(
'',
0,
new InvalidEncodedDataException($this->key(), $data)
);
}
}
} catch (EncodingExceptionInterface $error) {
return array('', 0, $error);
}

return array($output, $consumedBytes + $paddedLength - $length);
return array($output, $consumed + $paddedLength - $length, null);
}

private function map2($a, $b)
Expand Down
16 changes: 7 additions & 9 deletions src/Base32/AbstractBase32EncodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Eloquent\Endec\Base32;

use Eloquent\Confetti\TransformInterface;
use Exception;

/**
* An abstract base class for implementing base32 encode transforms.
Expand Down Expand Up @@ -50,17 +49,16 @@ protected function __construct(array $alphabet)
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
$length = strlen($data);
$consumedBytes = intval($length / 5) * 5;
$consumed = intval($length / 5) * 5;
$index = 0;
$output = '';

while ($index < $consumedBytes) {
while ($index < $consumed) {
$output .= $this->map5(
ord($data[$index++]),
ord($data[$index++]),
Expand All @@ -70,9 +68,9 @@ public function transform($data, &$context, $isEnd = false)
);
}

if ($isEnd && $consumedBytes !== $length) {
$remaining = $length - $consumedBytes;
$consumedBytes = $length;
if ($isEnd && $consumed !== $length) {
$remaining = $length - $consumed;
$consumed = $length;

if (1 === $remaining) {
$output .= $this->map1(
Expand All @@ -99,7 +97,7 @@ public function transform($data, &$context, $isEnd = false)
}
}

return array($output, $consumedBytes);
return array($output, $consumed, null);
}

private function map1($a)
Expand Down
20 changes: 13 additions & 7 deletions src/Base64/Base64DecodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Eloquent\Confetti\AbstractTransform;
use Eloquent\Confetti\TransformInterface;
use Eloquent\Endec\Exception\InvalidEncodedDataException;
use Exception;

/**
* Decodes data using base64 encoding.
Expand Down Expand Up @@ -56,27 +55,34 @@ public static function instance()
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
$consume = $this->blocksSize(strlen($data), 4, $isEnd);
if (!$consume) {
return array('', 0);
return array('', 0, null);
}

$consumedData = substr($data, 0, $consume);
if (1 === strlen(rtrim($consumedData, '=')) % 4) {
throw new InvalidEncodedDataException('base64', $consumedData);
return array(
'',
0,
new InvalidEncodedDataException('base64', $consumedData),
);
}

$outputBuffer = base64_decode($consumedData, true);
if (false === $outputBuffer) {
throw new InvalidEncodedDataException('base64', $consumedData);
return array(
'',
0,
new InvalidEncodedDataException('base64', $consumedData),
);
}

return array($outputBuffer, $consume);
return array($outputBuffer, $consume, null);
}

private static $instance;
Expand Down
8 changes: 3 additions & 5 deletions src/Base64/Base64EncodeTransform.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Eloquent\Confetti\AbstractTransform;
use Eloquent\Confetti\TransformInterface;
use Exception;

/**
* Encodes data using base64 encoding.
Expand Down Expand Up @@ -55,17 +54,16 @@ public static function instance()
* @param mixed &$context An arbitrary context value.
* @param boolean $isEnd True if all supplied data must be transformed.
*
* @return tuple<string,integer> A 2-tuple of the transformed data, and the number of bytes consumed.
* @throws Exception If the data cannot be transformed.
* @return tuple<string,integer,mixed> A 3-tuple of the transformed data, the number of bytes consumed, and any resulting error.
*/
public function transform($data, &$context, $isEnd = false)
{
$consume = $this->blocksSize(strlen($data), 3, $isEnd);
if (!$consume) {
return array('', 0);
return array('', 0, null);
}

return array(base64_encode(substr($data, 0, $consume)), $consume);
return array(base64_encode(substr($data, 0, $consume)), $consume, null);
}

private static $instance;
Expand Down
Loading

0 comments on commit 58e846a

Please sign in to comment.