Uncompress.hx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cpp.zip;
  23. class Uncompress {
  24. var s : Dynamic;
  25. public function new( windowBits : Null<Int> ) {
  26. s = _inflate_init(windowBits);
  27. }
  28. public function this_run( src : haxe.io.Bytes, srcPos : Int, dst : haxe.io.Bytes, dstPos : Int ) : { done : Bool, read : Int, write : Int } {
  29. return _inflate_buffer(s,src.getData(),srcPos,dst.getData(),dstPos);
  30. }
  31. public function setFlushMode( f : Flush ) {
  32. _set_flush_mode(s,untyped f.__Tag());
  33. }
  34. public function close() {
  35. _inflate_end(s);
  36. }
  37. public static function run( src : haxe.io.Bytes, ?bufsize ) : haxe.io.Bytes {
  38. var u = new Uncompress(null);
  39. if( bufsize == null ) bufsize = 1 << 16; // 64K
  40. var tmp = haxe.io.Bytes.alloc(bufsize);
  41. var b = new haxe.io.BytesBuffer();
  42. var pos = 0;
  43. u.setFlushMode(Flush.SYNC);
  44. while( true ) {
  45. var r = u.this_run(src,pos,tmp,0);
  46. b.addBytes(tmp,0,r.write);
  47. pos += r.read;
  48. if( r.done )
  49. break;
  50. }
  51. u.close();
  52. return b.getBytes();
  53. }
  54. static var _inflate_init = cpp.Lib.load("zlib","inflate_init",1);
  55. static var _inflate_buffer = cpp.Lib.load("zlib","inflate_buffer",5);
  56. static var _inflate_end = cpp.Lib.load("zlib","inflate_end",1);
  57. static var _set_flush_mode = cpp.Lib.load("zlib","set_flush_mode",2);
  58. }