1 /*
2 Copyright (c) 2011-2021 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 /**
30  * Zlib compression and decompression
31  *
32  * Copyright: Timur Gafarov 2011-2021.
33  * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0).
34  * Authors: Timur Gafarov
35  */
36 module dlib.coding.zlib;
37 
38 import etc.c.zlib;
39 import dlib.core.memory;
40 
41 /**
42  * Zlib encoder that uses provided fixed-length buffer to store results.
43  * Doesn't use garbage collector
44  */
45 struct ZlibBufferedEncoder
46 {
47     z_stream zlibStream;
48     ubyte[] buffer;
49     ubyte[] input;
50     bool ended = true;
51 
52     /// Constructor
53     this(ubyte[] buf, ubyte[] inp)
54     {
55         buffer = buf;
56         input = inp;
57         zlibStream.next_out = buffer.ptr;
58         zlibStream.avail_out = cast(uint)buffer.length;
59         zlibStream.data_type = Z_BINARY;
60         zlibStream.zalloc = null;
61         zlibStream.zfree = null;
62         zlibStream.opaque = null;
63 
64         zlibStream.next_in = inp.ptr;
65         zlibStream.avail_in = cast(uint)inp.length;
66 
67         deflateInit(&zlibStream, Z_BEST_COMPRESSION);
68         ended = false;
69     }
70 
71     /// Encodes the next portion of data and returns a number of bytes written
72     size_t encode()
73     {
74         zlibStream.next_out = buffer.ptr;
75         zlibStream.avail_out = cast(uint)buffer.length;
76         zlibStream.total_out = 0;
77 
78         while (zlibStream.avail_out > 0)
79         {
80             int msg = deflate(&zlibStream, Z_FINISH);
81 
82             if (msg == Z_STREAM_END)
83             {
84                 deflateEnd(&zlibStream);
85                 ended = true;
86                 return zlibStream.total_out;
87             }
88             else if (msg != Z_OK)
89             {
90                 deflateEnd(&zlibStream);
91                 return 0;
92             }
93         }
94 
95         return zlibStream.total_out;
96     }
97 }
98 
99 /**
100  * Zlib decoder that uses provided buffer to store results
101  * Doesn't use garbage collector
102  */
103 struct ZlibDecoder
104 {
105     z_stream zlibStream;
106     ubyte[] buffer;
107     int msg = 0;
108 
109     bool isInitialized = false;
110     bool hasEnded = false;
111 
112     /// Constructor
113     this(ubyte[] buf)
114     {
115         buffer = buf;
116         zlibStream.next_out = buffer.ptr;
117         zlibStream.avail_out = cast(uint)buffer.length;
118         zlibStream.data_type = Z_BINARY;
119     }
120 
121     /**
122      * Decodes a stream. Returns true on success, false on error. 
123      * Decoded data is stored in buffer field, the buffer is resized if necessary
124      */
125     bool decode(ubyte[] input)
126     {
127         zlibStream.next_in = input.ptr;
128         zlibStream.avail_in = cast(uint)input.length;
129 
130         if (!isInitialized)
131         {
132             isInitialized = true;
133             msg = inflateInit(&zlibStream);
134             if (msg)
135             {
136                 inflateEnd(&zlibStream);
137                 return false;
138             }
139         }
140 
141         while (zlibStream.avail_in)
142         {
143             msg = inflate(&zlibStream, Z_NO_FLUSH);
144             if (msg == Z_STREAM_END)
145             {
146                 inflateEnd(&zlibStream);
147                 hasEnded = true;
148                 reallocateBuffer(zlibStream.total_out);
149                 return true;
150             }
151             else if (msg != Z_OK)
152             {
153                 inflateEnd(&zlibStream);
154                 return false;
155             }
156             else if (zlibStream.avail_out == 0)
157             {
158                 reallocateBuffer(buffer.length * 2);
159                 zlibStream.next_out = &buffer[buffer.length / 2];
160                 zlibStream.avail_out = cast(uint)(buffer.length / 2);
161             }
162         }
163 
164         return true;
165     }
166 
167     void reallocateBuffer(size_t len)
168     {
169         ubyte[] buffer2 = New!(ubyte[])(len);
170         for(uint i = 0; i < buffer2.length; i++)
171             if (i < buffer.length)
172                 buffer2[i] = buffer[i];
173         Delete(buffer);
174         buffer = buffer2;
175     }
176 
177     /// Call this when you don't need decoded buffer anymore
178     void free()
179     {
180         Delete(buffer);
181     }
182 }