-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathzlibx.d
84 lines (71 loc) · 2.19 KB
/
zlibx.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// This code is in the public domain.
module zlibx;
import core.memory;
import std.string : format;
import std.zlib, etc.c.zlib, std.conv;
static import etc.c.zlib;
alias std.zlib.Z_SYNC_FLUSH Z_SYNC_FLUSH;
debug import std.stdio : stderr;
/// Avoid bug(?) in D zlib implementation with 7zip-generated zlib streams
ubyte[] exactUncompress(ubyte[] srcbuf, size_t destlen)
{
etc.c.zlib.z_stream zs;
auto destbuf = new ubyte[destlen];
uint err;
zs.next_in = srcbuf.ptr;
zs.avail_in = to!uint(srcbuf.length);
zs.next_out = destbuf.ptr;
zs.avail_out = to!uint(destbuf.length);
err = etc.c.zlib.inflateInit2(&zs, 15);
if (err)
{
GC.free(destbuf.ptr);
throw new ZlibException(err);
}
while (true)
{
err = etc.c.zlib.inflate(&zs, Z_SYNC_FLUSH);
if (err != Z_OK && err != Z_STREAM_END)
{
Lerr:
GC.free(destbuf.ptr);
etc.c.zlib.inflateEnd(&zs);
throw new ZlibException(err);
}
if (err == Z_STREAM_END)
break;
else
if (zs.avail_out == 0)
{
debug stderr.writefln("Wrong uncompressed file length (read %d/%d bytes and wrote %d/%d bytes)",
srcbuf .length - zs.avail_in , srcbuf .length,
destlen - zs.avail_out, destlen);
auto out_pos = zs.next_out - destbuf.ptr;
destbuf.length = 1024 + destbuf.length * 2;
zs.next_out = destbuf.ptr + out_pos;
zs.avail_out = to!uint(destbuf.length - out_pos);
continue;
}
else
if (zs.avail_in == 0)
{
debug stderr.writefln("Unterminated Zlib stream (read %d/%d bytes and wrote %d/%d bytes)",
srcbuf .length - zs.avail_in , srcbuf .length,
destlen - zs.avail_out, destlen);
break;
}
else
throw new Exception(format("Unexpected zlib state (err=%d, avail_in == %d, avail_out = %d)",
err, zs.avail_in , zs.avail_out));
}
if (zs.avail_in != 0 || zs.avail_out != 0)
debug stderr.writefln("Zlib stream incongruity (read %d/%d bytes and wrote %d/%d bytes)",
srcbuf .length - zs.avail_in , srcbuf .length,
destlen - zs.avail_out, destlen);
err = etc.c.zlib.inflateEnd(&zs);
if (err != Z_OK)
goto Lerr;
if (zs.avail_out != 0)
debug stderr.writefln("Too little data in zlib stream: expected %d, got %d", destlen, destlen - zs.avail_out);
return destbuf[0..$-zs.avail_out];
}