Uncompress.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package cpp.zip;
  26. class Uncompress {
  27. var s : Dynamic;
  28. public function new( windowBits : Null<Int> ) {
  29. s = _inflate_init(windowBits);
  30. }
  31. public function this_run( src : haxe.io.Bytes, srcPos : Int, dst : haxe.io.Bytes, dstPos : Int ) : { done : Bool, read : Int, write : Int } {
  32. return _inflate_buffer(s,src.getData(),srcPos,dst.getData(),dstPos);
  33. }
  34. public function setFlushMode( f : Flush ) {
  35. _set_flush_mode(s,untyped f.__Tag());
  36. }
  37. public function close() {
  38. _inflate_end(s);
  39. }
  40. public static function run( src : haxe.io.Bytes, ?bufsize ) : haxe.io.Bytes {
  41. var u = new Uncompress(null);
  42. if( bufsize == null ) bufsize = 1 << 16; // 64K
  43. var tmp = haxe.io.Bytes.alloc(bufsize);
  44. var b = new haxe.io.BytesBuffer();
  45. var pos = 0;
  46. u.setFlushMode(Flush.SYNC);
  47. while( true ) {
  48. var r = u.this_run(src,pos,tmp,0);
  49. b.addBytes(tmp,0,r.write);
  50. pos += r.read;
  51. if( r.done )
  52. break;
  53. }
  54. u.close();
  55. return b.getBytes();
  56. }
  57. static var _inflate_init = cpp.Lib.load("zlib","inflate_init",1);
  58. static var _inflate_buffer = cpp.Lib.load("zlib","inflate_buffer",5);
  59. static var _inflate_end = cpp.Lib.load("zlib","inflate_end",1);
  60. static var _set_flush_mode = cpp.Lib.load("zlib","set_flush_mode",2);
  61. }