| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 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 |
|
| 23 |
type in; |
| 24 |
type out; |
| 25 |
|
| 26 |
type flush { |
| 27 |
NO; |
| 28 |
SYNC; |
| 29 |
FULL; |
| 30 |
FINISH; |
| 31 |
BLOCK; |
| 32 |
} |
| 33 |
|
| 34 |
type result { |
| 35 |
done : bool; |
| 36 |
read : int; |
| 37 |
write : int; |
| 38 |
} |
| 39 |
|
| 40 |
function init() { |
| 41 |
neko(" |
| 42 |
@set_flush_mode = $loader.loadprim('zlib@set_flush_mode',2); |
| 43 |
@inflate_init = $loader.loadprim('zlib@inflate_init',1); |
| 44 |
@deflate_buffer = $loader.loadprim('zlib@deflate_buffer',5); |
| 45 |
@inflate_buffer = $loader.loadprim('zlib@inflate_buffer',5); |
| 46 |
@deflate_init = $loader.loadprim('zlib@deflate_init',1); |
| 47 |
@deflate_bound = $loader.loadprim('zlib@deflate_bound',2); |
| 48 |
@deflate_end = $loader.loadprim('zlib@deflate_end',1); |
| 49 |
@inflate_end = $loader.loadprim('zlib@inflate_end',1); |
| 50 |
"); |
| 51 |
} |
| 52 |
|
| 53 |
function __result(x) { |
| 54 |
{ done = neko "x.done"; read = neko "x.read"; write = neko "x.write" } |
| 55 |
} |
| 56 |
|
| 57 |
function output(v:int) : out { |
| 58 |
neko("@deflate_init")(v); |
| 59 |
} |
| 60 |
|
| 61 |
function output_bound(o:out,size:int) : int { |
| 62 |
neko("@deflate_bound")(o,size); |
| 63 |
} |
| 64 |
|
| 65 |
function output_end(o:out) : void { |
| 66 |
neko("@deflate_end")(o); |
| 67 |
} |
| 68 |
|
| 69 |
function output_buffer(o:out,in:string,ipos:int,out:string,opos:int) { |
| 70 |
__result(neko "@deflate_buffer(o,in,ipos,out,opos)"); |
| 71 |
} |
| 72 |
|
| 73 |
function input( n : int option ) : in { |
| 74 |
neko("@inflate_init")(match n { None -> neko "null" | Some i -> i }) |
| 75 |
} |
| 76 |
|
| 77 |
function input_buffer(i:in,in:string,ipos:int,out:string,opos:int) { |
| 78 |
__result(neko "@inflate_buffer(i,in,ipos,out,opos)"); |
| 79 |
} |
| 80 |
|
| 81 |
function input_end(i:in) : void { |
| 82 |
neko("@inflate_end")(i); |
| 83 |
} |
| 84 |
|
| 85 |
function output_set_flush_mode(o:out,f:flush) : void { |
| 86 |
neko("@set_flush_mode")(o,string f); |
| 87 |
} |
| 88 |
|
| 89 |
function input_set_flush_mode(i:in,f:flush) : void { |
| 90 |
neko("@set_flush_mode")(i,string f); |
| 91 |
} |
| 92 |
|
| 93 |
function compress(str,level) { |
| 94 |
var c = output level; |
| 95 |
output_set_flush_mode c FINISH; |
| 96 |
var out = String.create (output_bound c (String.length str)); |
| 97 |
var r = output_buffer c str 0 out 0; |
| 98 |
output_end c; |
| 99 |
if !r.done || r.read != String.length str then throw Error("Compression failed"); |
| 100 |
String.sub out 0 r.write; |
| 101 |
} |
| 102 |
|
| 103 |
function uncompress(str) { |
| 104 |
var u = input None; |
| 105 |
var tmp = String.create (1 << 16); // 64K |
| 106 |
var b = Buffer.create(); |
| 107 |
input_set_flush_mode u SYNC; |
| 108 |
function rec loop(pos) { |
| 109 |
var r = input_buffer u str pos tmp 0; |
| 110 |
Buffer.add_sub b tmp 0 r.write; |
| 111 |
if !r.done then loop(pos + r.read); |
| 112 |
} |
| 113 |
loop(0); |
| 114 |
input_end u; |
| 115 |
Buffer.string b; |
| 116 |
} |