1 /*
2 Copyright (c) 2011-2015 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 // dimage is actually stripped out part of dlib - just to support reading PNG and JPEG
30 module dimage.zlib; //dlib.coding.zlib
31 
32 private
33 {
34     import etc.c.zlib;
35     import dimage.memory;
36 }
37 
38 struct ZlibBufferedEncoder
39 {
40     z_stream zlibStream;
41     ubyte[] buffer;
42     ubyte[] input;
43     bool ended = true;
44 
45     this(ubyte[] buf, ubyte[] inp)
46     {
47         buffer = buf;
48         input = inp;
49         zlibStream.next_out = buffer.ptr;
50         zlibStream.avail_out = cast(uint)buffer.length;
51         zlibStream.data_type = Z_BINARY;
52         zlibStream.zalloc = null;
53         zlibStream.zfree = null;
54         zlibStream.opaque = null;
55 
56         zlibStream.next_in = inp.ptr;
57         zlibStream.avail_in = cast(uint)inp.length;
58 
59         deflateInit(&zlibStream, Z_BEST_COMPRESSION);
60         ended = false;
61     }
62 
63     size_t encode()
64     {
65         zlibStream.next_out = buffer.ptr;
66         zlibStream.avail_out = cast(uint)buffer.length;
67         zlibStream.total_out = 0;
68 
69         while (zlibStream.avail_out > 0)
70         {
71             int msg = deflate(&zlibStream, Z_FINISH);
72 
73             if (msg == Z_STREAM_END)
74             {
75                 deflateEnd(&zlibStream);
76                 ended = true;
77                 return zlibStream.total_out;
78             }
79             else if (msg != Z_OK)
80             {
81                 deflateEnd(&zlibStream);
82                 return 0;
83             }
84         }
85 
86         return zlibStream.total_out;
87     }
88 }
89 
90 struct ZlibDecoder
91 {
92     z_stream zlibStream;
93     ubyte[] buffer;
94     int msg = 0;
95 
96     bool isInitialized = false;
97     bool hasEnded = false;
98 
99     this(ubyte[] buf)
100     {
101         buffer = buf;
102         zlibStream.next_out = buffer.ptr;
103         zlibStream.avail_out = cast(uint)buffer.length;
104         zlibStream.data_type = Z_BINARY;
105     }
106 
107     bool decode(ubyte[] input)
108     {
109         zlibStream.next_in = input.ptr;
110         zlibStream.avail_in = cast(uint)input.length;
111 
112         if (!isInitialized)
113         {
114             isInitialized = true;
115             msg = inflateInit(&zlibStream);
116             if (msg)
117             {
118                 inflateEnd(&zlibStream);
119                 return false;
120             }
121         }
122 
123         while (zlibStream.avail_in)
124         {
125             msg = inflate(&zlibStream, Z_NO_FLUSH);
126 
127             if (msg == Z_STREAM_END)
128             {
129                 inflateEnd(&zlibStream);
130                 hasEnded = true;
131                 reallocateBuffer(zlibStream.total_out);
132                 return true;
133             }
134             else if (msg != Z_OK)
135             {
136                 inflateEnd(&zlibStream);
137                 return false;
138             }
139             else if (zlibStream.avail_out == 0)
140             {
141                 reallocateBuffer(buffer.length * 2);
142                 zlibStream.next_out = &buffer[buffer.length / 2];
143                 zlibStream.avail_out = cast(uint)(buffer.length / 2);
144             }
145         }
146 
147         return true;
148     }
149 
150     void reallocateBuffer(size_t len)
151     {
152         ubyte[] buffer2 = New!(ubyte[])(len);
153         for(uint i = 0; i < buffer2.length; i++)
154             if (i < buffer.length)
155                 buffer2[i] = buffer[i];
156         Delete(buffer);
157         buffer = buffer2;
158     }
159 
160     void free()
161     {
162         Delete(buffer);
163     }
164 }
165