Uncompress.hx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 haxe.zip;
  23. @:coreApi
  24. class Uncompress {
  25. var s : Dynamic;
  26. public function new( ?windowBits : Int ) : Void {
  27. s = _inflate_init(windowBits);
  28. }
  29. public function execute( src : haxe.io.Bytes, srcPos : Int, dst : haxe.io.Bytes, dstPos : Int ) : { done : Bool, read : Int, write : Int } {
  30. return _inflate_buffer(s,src.getData(),srcPos,dst.getData(),dstPos);
  31. }
  32. public function setFlushMode( f : FlushMode ) : Void {
  33. _set_flush_mode(s,untyped f.__Tag());
  34. }
  35. public function close() : Void {
  36. _inflate_end(s);
  37. }
  38. public static function run( src : haxe.io.Bytes, ?bufsize : Int ) : haxe.io.Bytes {
  39. var u = new Uncompress(null);
  40. if( bufsize == null ) bufsize = 1 << 16; // 64K
  41. var tmp = haxe.io.Bytes.alloc(bufsize);
  42. var b = new haxe.io.BytesBuffer();
  43. var pos = 0;
  44. u.setFlushMode(FlushMode.SYNC);
  45. while( true ) {
  46. var r = u.execute(src,pos,tmp,0);
  47. b.addBytes(tmp,0,r.write);
  48. pos += r.read;
  49. if( r.done )
  50. break;
  51. }
  52. u.close();
  53. return b.getBytes();
  54. }
  55. static var _inflate_init = cpp.Lib.load("zlib","inflate_init",1);
  56. static var _inflate_buffer = cpp.Lib.load("zlib","inflate_buffer",5);
  57. static var _inflate_end = cpp.Lib.load("zlib","inflate_end",1);
  58. static var _set_flush_mode = cpp.Lib.load("zlib","set_flush_mode",2);
  59. }