inflate_add_basic.phpt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. Test incremental inflate_add() functionality
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded("zlib")) {
  6. print "skip - ZLIB extension not loaded";
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. function inflateStream($mode, $flushSize) {
  12. $buffer = "";
  13. $inflated = null;
  14. $resource = inflate_init($mode);
  15. while (true) {
  16. $dataToInflate = yield $inflated;
  17. if (isset($dataToInflate)) {
  18. $buffer .= $dataToInflate;
  19. if (strlen($buffer) >= $flushSize) {
  20. $inflated = inflate_add($resource, $buffer);
  21. $buffer = "";
  22. } else {
  23. $inflated = null;
  24. }
  25. } else {
  26. $inflated = inflate_add($resource, $buffer, ZLIB_FINISH);
  27. }
  28. }
  29. }
  30. $modes = [
  31. 'ZLIB_ENCODING_RAW' => ZLIB_ENCODING_RAW,
  32. 'ZLIB_ENCODING_GZIP' => ZLIB_ENCODING_GZIP,
  33. 'ZLIB_ENCODING_DEFLATE' => ZLIB_ENCODING_DEFLATE,
  34. ];
  35. $flushSizes = [1, 4, 32768];
  36. $flushTypes = [
  37. 'ZLIB_SYNC_FLUSH' => ZLIB_SYNC_FLUSH,
  38. 'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH,
  39. 'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH,
  40. 'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
  41. 'ZLIB_BLOCK' => ZLIB_BLOCK,
  42. ];
  43. $uncompressed = "";
  44. for ($i=0;$i<(32768*2);$i++) {
  45. $uncompressed .= chr(rand(48,125));
  46. }
  47. foreach ($modes as $modeKey => $mode) {
  48. $compressed = zlib_encode($uncompressed, $mode);
  49. $compressedLen = strlen($compressed);
  50. foreach ($flushSizes as $flushSize) {
  51. foreach ($flushTypes as $flushTypeKey => $flushType) {
  52. $inflated = "";
  53. $stream = inflateStream($mode, $flushSize, $flushType);
  54. for ($i=0;$i<$compressedLen;$i++) {
  55. $inflated .= $stream->send($compressed[$i]);
  56. }
  57. $inflated .= $stream->send(null);
  58. if ($inflated !== $uncompressed) {
  59. echo "Error: {$modeKey} | {$flushSize} | {$flushTypeKey}\n";
  60. }
  61. }
  62. }
  63. }
  64. ?>
  65. ===DONE===
  66. --EXPECT--
  67. ===DONE===