Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created July 26, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasezaki/76169776e61100f76eb2af062f06bc1e to your computer and use it in GitHub Desktop.
Save sasezaki/76169776e61100f76eb2af062f06bc1e to your computer and use it in GitHub Desktop.
streamのuser_filterを使ったビット演算適用のサンプル(途中まで)
<?php
// php filter.php < data.bin > bit_filtered_data.bin
class bitope_filter extends php_user_filter
{
const KEY = '3.14';
function filter($in, $out, &$consumed, $closing) {
while ($bucket = stream_bucket_make_writeable($in)) {
// ビット演算のもとになるのが何かにもよるけれども..
// $bucket で取得したデータとビット演算もとのデータとの長さ違いの残りには気を付けて
// @see https://github.com/xcezx/Stream_Filter_Mbstring/blob/master/src/Stream/Filter/Mbstring.php
 // $keylength = strlen(self::KEY);
// $data = '';
// while ( = substr($bucket->data, $keylength * $n, $keylength * ($n+1))) {
// $data =
// }
// $bucket->data = $data
$bucket->data = $bucket->data ^ self::KEY;
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
stream_filter_register('bitope', 'bitope_filter');
$fp = STDIN;
stream_filter_append($fp, 'bitope');
stream_copy_to_stream($fp, STDOUT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment