1 /*******************************************************************************
2 
3     Class encapsulating an lzo chunk compressor and a memory buffer to store
4     de/compression results.
5 
6     Copyright:
7         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
8         All rights reserved.
9 
10     License:
11         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
12         Alternatively, this file may be distributed under the terms of the Tango
13         3-Clause BSD License (see LICENSE_BSD.txt for details).
14 
15 *******************************************************************************/
16 
17 module ocean.io.compress.lzo.LzoChunkCompressor;
18 
19 
20 
21 
22 import ocean.io.compress.Lzo;
23 
24 import ocean.io.compress.lzo.LzoChunk;
25 
26 import ocean.io.compress.lzo.LzoHeader;
27 
28 
29 
30 
31 /*******************************************************************************
32 
33     Lzo chunk compressor
34 
35 *******************************************************************************/
36 
37 class LzoChunkCompressor
38 {
39     /***************************************************************************
40 
41         Constants defining whether de/compression headers expect the chunk
42         length to be stored inline (ie as part of the chunk array).
43 
44     ***************************************************************************/
45 
46     private static immutable bool DecompressLenghtInline = false;
47     private static immutable bool CompressLenghtInline = true;
48 
49 
50     /***************************************************************************
51 
52         Chunk decompressor.
53 
54     ***************************************************************************/
55 
56     public class Decompressor
57     {
58         /***********************************************************************
59 
60             Aliases for the lzo header & chunk.
61 
62         ***********************************************************************/
63 
64         public alias LzoHeader!(DecompressLenghtInline) Header;
65         public alias LzoChunk!(DecompressLenghtInline) Chunk;
66 
67 
68         /***********************************************************************
69 
70             Lzo chunk instance, used to do the decompression.
71 
72         ***********************************************************************/
73 
74         private Chunk chunk;
75 
76 
77         /***********************************************************************
78 
79             Constructor.
80 
81         ***********************************************************************/
82 
83         public this ( )
84         {
85             this.chunk = new Chunk(this.outer.lzo);
86         }
87 
88 
89         /***********************************************************************
90 
91             Decompresses provided data.
92 
93             Params:
94                 source = data to decompress
95 
96             Returns:
97                 decompressed data (a slice into the outer class' results buffer)
98 
99         ***********************************************************************/
100 
101         public char[] decompress ( char[] source )
102         {
103             this.chunk.uncompress(cast(void[])source, this.outer.result);
104             return this.outer.result;
105         }
106 
107 
108         /***********************************************************************
109 
110             Tells whether the provided data is an lzo start chunk.
111 
112             Params:
113                 array = data to check
114 
115             Returns:
116                 true if data is an lzo start chunk
117 
118         ***********************************************************************/
119 
120         public bool isStartChunk ( char[] array )
121         {
122             Header header;
123 
124             if ( array.length < header.read_length )
125             {
126                 return false;
127             }
128             else
129             {
130                 return header.tryReadStart(array[0..header.read_length]);
131             }
132         }
133     }
134 
135 
136     /***************************************************************************
137 
138         Chunk compressor.
139 
140     ***************************************************************************/
141 
142     public class Compressor
143     {
144         /***********************************************************************
145 
146             Aliases for the lzo header & chunk.
147 
148         ***********************************************************************/
149 
150         public alias LzoHeader!(CompressLenghtInline) Header;
151         public alias LzoChunk!(CompressLenghtInline) Chunk;
152 
153 
154         /***********************************************************************
155 
156             Lzo chunk instance, used to do the compression.
157 
158         ***********************************************************************/
159 
160         private Chunk chunk;
161 
162 
163         /***********************************************************************
164 
165             Constructor.
166 
167         ***********************************************************************/
168 
169         public this ( )
170         {
171             this.chunk = new Chunk(this.outer.lzo);
172         }
173 
174 
175         /***********************************************************************
176 
177             Compresses provided data.
178 
179             Params:
180                 source = data to compress
181 
182             Returns:
183                 compressed data (a slice into the outer class' results buffer)
184 
185         ***********************************************************************/
186 
187         public char[] compress ( char[] source )
188         {
189             this.chunk.compress(cast(void[])source, this.outer.result);
190             return this.outer.result;
191         }
192 
193 
194         /***********************************************************************
195 
196             Tells whether the provided data is an lzo start chunk.
197 
198             Params:
199                 array = data to check
200 
201             Returns:
202                 true if data is an lzo start chunk
203 
204         ***********************************************************************/
205 
206         public bool isStartChunk ( char[] array )
207         {
208             Header header;
209 
210             if ( array.length < header.read_length )
211             {
212                 return false;
213             }
214             else
215             {
216                 return header.tryReadStart(array[0..header.read_length]);
217             }
218         }
219     }
220 
221 
222     /***************************************************************************
223 
224         Chunk de/compressor instances.
225 
226     ***************************************************************************/
227 
228     public Decompressor decompressor;
229     public Compressor compressor;
230 
231 
232     /***************************************************************************
233 
234         Internal lzo object.
235 
236     ***************************************************************************/
237 
238     public Lzo lzo;
239 
240 
241     /***************************************************************************
242 
243         Internal de/compression results buffer.
244 
245     ***************************************************************************/
246 
247     private char[] result;
248 
249 
250     /***************************************************************************
251 
252         Constructor.
253 
254     ***************************************************************************/
255 
256     public this ( )
257     {
258         this.lzo = new Lzo;
259 
260         this.compressor = new Compressor;
261         this.decompressor = new Decompressor;
262     }
263 }