Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Base64OutputStream on HHVM #2

Merged
merged 3 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/php/module.xp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace text\encode;

module xp-framework/text-encode {

/** @return void */
public function initialize() {
if (defined('HHVM_VERSION')) {
class_alias(HHVMBase64OutputStream::class, Base64OutputStream::class);
}
}
}
115 changes: 115 additions & 0 deletions src/main/php/text/encode/HHVMBase64OutputStream.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php namespace text\encode;

use io\streams\OutputStream;
use io\streams\Streams;
use lang\Type;

/**
* OuputStream that encodes data to base64 encoding
*
* @see rfc://2045 section 6.8
* @test xp://net.xp_framework.unittest.text.encode.Base64OutputStreamTest
*/
class HHVMBase64OutputStream implements OutputStream {
const TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const PAD = '=';

private $out, $remain;
private $pos= 0;

/**
* Constructor
*
* @param io.streams.OutputStream out
* @param int lineLength limit maximum line length
*/
public function __construct(OutputStream $out, $lineLength= 0) {
$this->out= $out;
$this->remain= '';
if ($lineLength) {
$pos= 0;
$this->write= function($bytes) use(&$pos, $lineLength) {
$l= strlen($bytes);
$o= 0;
while ($l - $o > $lineLength - $pos) {
$this->out->write(substr($bytes, $o, $lineLength - $pos)."\n");
$pos= 0;
$o+= $lineLength;
}
$this->out->write(substr($bytes, $o));
$pos= $l - $o;
};
} else {
$this->write= Type::forName('function(var): var')->cast([$this->out, 'write']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is essentially Closure::fromCallable(), but this PHP 7.1 feature doesn't exist in HHVM yet, see facebook/hhvm#7874

}
}

/**
* Write a string
*
* @param var arg
*/
public function write($arg) {
$input= $this->remain.$arg;

$limit= strlen($input) - 2;
$encoded= '';
$offset= 0;
while ($offset < $limit) {
$encoded.=
self::TABLE[ord($input{$offset}) >> 2].
self::TABLE[((ord($input{$offset}) & 0x03) << 4) + (ord($input{$offset + 1}) >> 4)].
self::TABLE[((ord($input{$offset + 1}) & 0x0f) << 2) + (ord($input{$offset + 2}) >> 6)].
self::TABLE[ord($input{$offset + 2}) & 0x3f]
;
$offset+= 3;
}

$this->write->__invoke($encoded);
$this->remain= substr($input, $offset);
}

/**
* Flush this buffer
*
*/
public function flush() {
$this->out->flush();
}

/**
* Close this buffer. Flushes this buffer and then calls the close()
* method on the underlying OuputStream.
*
*/
public function close() {
switch (strlen($this->remain)) {
case 1:
$this->write->__invoke(
self::TABLE[ord($this->remain{0}) >> 2].
self::TABLE[(ord($this->remain{0}) & 0x03) << 4].
self::PAD.
self::PAD
);
break;
case 2:
$this->write->__invoke(
self::TABLE[ord($this->remain{0}) >> 2].
self::TABLE[((ord($this->remain{0}) & 0x03) << 4) + (ord($this->remain{1}) >> 4)].
self::TABLE[((ord($this->remain{1}) & 0x0f) << 2)].
self::PAD
);
break;
}
$this->out->close();
$this->remain= '';
}

/**
* Destructor. Ensures output stream is closed.
*
*/
public function __destruct() {
$this->out->close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*
* @see xp://text.encode.Base64OutputStream
*/
#[@action([
# new VerifyThat(function() { return in_array("convert.*", stream_get_filters()); }),
# new VerifyThat(function() { return !defined('HHVM_VERSION'); })
#])]
#[@action(new VerifyThat(function() { return in_array("convert.*", stream_get_filters()); }))]
class Base64OutputStreamTest extends \unittest\TestCase {

/**
Expand Down