forked from klayoutmatthias/dump_oas_gds2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlDeflate.cc
558 lines (455 loc) · 14 KB
/
tlDeflate.cc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
KLayout Layout Viewer
Copyright (C) 2013-2018 Matthias Koefferlein
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tlDeflate.h"
#include "tlException.h"
#include "tlAssert.h"
#include <algorithm>
#include <zlib.h>
namespace tl
{
// ------------------------------------------------------------------------
// The Huffmann decoder core
/**
* @brief The decoder for Huffmann codes
*
* The decoder keeps a Huffmann code tree and decodes a value from a bit stream
* using this tree.
* As specified by RFC1951, the code tree is constructed from a list of code lengths
* vs. value alone.
*/
class HuffmannDecoder
{
public:
/**
* @brief Constructor
*
* Creates an empty code tree.
*/
HuffmannDecoder ()
{
mp_codes = 0;
mp_bitmasks = 0;
m_max_bits = 0;
m_num_codes = 0;
}
/**
* @brief Destructor
*/
~HuffmannDecoder ()
{
if (mp_codes) {
delete [] mp_codes;
}
mp_codes = 0;
if (mp_bitmasks) {
delete [] mp_bitmasks;
}
mp_bitmasks = 0;
}
/**
* @brief Initialize the code tree with the fixed Huffmann code table for literals/lengths
*
* This table is used by compression mode 1.
* It is specified in RFC1951.
*/
void fill_fixed_table_length ()
{
reserve (9);
unsigned short lengths [288];
for (unsigned int i = 0; i < 144; ++i) {
lengths[i] = 8;
}
for (unsigned int i = 144; i < 256; ++i) {
lengths[i] = 9;
}
for (unsigned int i = 256; i < 280; ++i) {
lengths[i] = 7;
}
for (unsigned int i = 280; i < 288; ++i) {
lengths[i] = 8;
}
init_codes (lengths, lengths + sizeof (lengths) / sizeof (lengths [0]));
}
/**
* @brief Initialize the code tree with the fixed Huffmann code table for distances
*
* This table is used by compression mode 1.
* It is specified in RFC1951.
*/
void fill_fixed_table_dist ()
{
reserve (5);
unsigned short lengths [32];
for (unsigned int i = 0; i < 32; ++i) {
lengths[i] = 5;
}
init_codes (lengths, lengths + sizeof (lengths) / sizeof (lengths [0]));
}
/**
* @brief Initialize the code tree from a list of lengths
*
* This method initializes the code tree from a list of lengths, given
* by the sequence [begin_lengths, end_lengths). The codes are assumed to
* range from 0 to distance(begin_lengths, end_lengths).
* See RFC1951 for a description about the procedure.
*/
template <class Iter>
void init_codes (Iter begin_lengths, Iter end_lengths)
{
const unsigned int MAX_BITS = 16;
unsigned short bl_count[MAX_BITS + 1];
unsigned short bitmasks[MAX_BITS + 1];
unsigned short next_code[MAX_BITS + 1];
unsigned int max_bits = 0;
for (unsigned int bits = 0; bits <= MAX_BITS; bits++) {
bl_count[bits] = 0;
}
for (Iter l = begin_lengths; l != end_lengths; ++l) {
tl_assert (*l < MAX_BITS);
if (*l > 0) {
++bl_count [*l];
}
}
unsigned int code = 0;
for (unsigned int bits = 1; bits <= MAX_BITS; bits++) {
if (bl_count[bits - 1] > 0) {
max_bits = bits - 1;
}
code = (code + bl_count[bits - 1]) << 1;
next_code[bits] = code;
}
for (unsigned int bits = 0; bits <= max_bits; bits++) {
bitmasks [bits] = ((1 << bits) - 1) << (max_bits - bits);
}
reserve (max_bits);
unsigned short symbol = 0;
for (Iter l = begin_lengths; l != end_lengths; ++l, ++symbol) {
if (*l > 0) {
unsigned int code = next_code [*l]++;
code <<= (max_bits - *l);
mp_codes [code] = symbol;
mp_bitmasks [code] = bitmasks [*l];
}
}
}
/**
* @brief Decode the next value from a bit stream
*
* This method takes the next value from the bit stream decoding the bits with
* the code tree currently loaded.
*/
unsigned short decode (BitStream &s) const
{
tl_assert (mp_codes != 0);
unsigned int m = m_num_codes / 2;
unsigned int c = 0;
do {
if (s.get_bit ()) {
c |= m;
}
m >>= 1;
} while ((mp_bitmasks [c] & m) != 0);
return mp_codes [c];
}
private:
unsigned short *mp_codes, *mp_bitmasks;
unsigned int m_num_codes, m_max_bits;
void reserve (unsigned int max_bits)
{
m_num_codes = 1 << max_bits;
if (max_bits > m_max_bits) {
m_max_bits = max_bits;
if (mp_codes) {
delete [] mp_codes;
}
mp_codes = new unsigned short [m_num_codes];
if (mp_bitmasks) {
delete [] mp_bitmasks;
}
mp_bitmasks = new unsigned short [m_num_codes];
}
}
};
// ------------------------------------------------------------------------
// InflateFilter implementation
InflateFilter::InflateFilter (tl::InputStream &input)
: m_input (input),
m_b_insert (0), m_b_read (0), m_at_end (false),
m_last_block (false),
m_uncompressed_length (0) // this forces a new block on "process()"
{
for (size_t i = 0; i < sizeof (m_buffer) / sizeof (m_buffer [0]); ++i) {
m_buffer[i] = 0;
}
mp_dist_decoder = new HuffmannDecoder ();
mp_lit_decoder = new HuffmannDecoder ();
}
InflateFilter::~InflateFilter ()
{
delete mp_dist_decoder;
mp_dist_decoder = 0;
delete mp_lit_decoder;
mp_lit_decoder = 0;
}
const char *
InflateFilter::get (size_t n)
{
tl_assert (n < sizeof (m_buffer) / 2);
while ((m_b_insert + sizeof (m_buffer) - m_b_read) % sizeof (m_buffer) < n) {
if (! process ()) {
throw tl::Exception (tl::translate ("Unexpected end of file (DEFLATE implementation)"));
}
}
tl_assert (m_b_read != m_b_insert);
// ensure the block is accessible as a coherent chunk:
if (m_b_read + n >= sizeof (m_buffer)) {
std::rotate (m_buffer, m_buffer + m_b_read, m_buffer + sizeof (m_buffer));
m_b_insert = (m_b_insert - m_b_read + sizeof (m_buffer)) % sizeof (m_buffer);
m_b_read = 0;
}
const char *r = m_buffer + m_b_read;
m_b_read = (m_b_read + n) % sizeof (m_buffer);
return r;
}
void
InflateFilter::unget (size_t n)
{
tl_assert (m_b_read >= n);
m_b_read -= n;
}
bool
InflateFilter::at_end ()
{
if (! m_at_end && m_b_read == m_b_insert) {
if (! process ()) {
m_at_end = true;
}
}
return m_at_end;
}
void
InflateFilter::put_byte (char b)
{
m_buffer [m_b_insert] = b;
m_b_insert = (m_b_insert + 1) % sizeof (m_buffer);
}
void
InflateFilter::put_byte_dist (unsigned int d)
{
put_byte (m_buffer [(m_b_insert - d) % sizeof (m_buffer)]);
}
bool
InflateFilter::process ()
{
while (true) {
bool new_block = false;
if (m_uncompressed_length == 0) {
m_uncompressed_length = -1;
new_block = true;
} else if (m_uncompressed_length > 0) {
put_byte (m_input.get_byte ());
--m_uncompressed_length;
} else {
unsigned int l = mp_lit_decoder->decode (m_input);
if (l < 256) {
put_byte (char (l));
} else if (l == 256) {
new_block = true;
} else {
unsigned int length = 0;
if (l < 265) {
length = l - 254;
} else if (l < 269) {
length = (l - 265) * 2 + 11 + m_input.get_bits (1);
} else if (l < 273) {
length = (l - 269) * 4 + 19 + m_input.get_bits (2);
} else if (l < 277) {
length = (l - 273) * 8 + 35 + m_input.get_bits (3);
} else if (l < 281) {
length = (l - 277) * 16 + 67 + m_input.get_bits (4);
} else if (l < 285) {
length = (l - 281) * 32 + 131 + m_input.get_bits (5);
} else {
length = 258;
}
unsigned int d = mp_dist_decoder->decode (m_input);
unsigned int dist = 0;
if (d < 4) {
dist = d + 1;
} else if (d < 6) {
dist = (d - 4) * 2 + 5 + m_input.get_bits (1);
} else if (d < 8) {
dist = (d - 6) * 4 + 9 + m_input.get_bits (2);
} else if (d < 10) {
dist = (d - 8) * 8 + 17 + m_input.get_bits (3);
} else if (d < 12) {
dist = (d - 10) * 16 + 33 + m_input.get_bits (4);
} else if (d < 14) {
dist = (d - 12) * 32 + 65 + m_input.get_bits (5);
} else if (d < 16) {
dist = (d - 14) * 64 + 129 + m_input.get_bits (6);
} else if (d < 18) {
dist = (d - 16) * 128 + 257 + m_input.get_bits (7);
} else if (d < 20) {
dist = (d - 18) * 256 + 513 + m_input.get_bits (8);
} else if (d < 22) {
dist = (d - 20) * 512 + 1025 + m_input.get_bits (9);
} else if (d < 24) {
dist = (d - 22) * 1024 + 2049 + m_input.get_bits (10);
} else if (d < 26) {
dist = (d - 24) * 2048 + 4097 + m_input.get_bits (11);
} else if (d < 28) {
dist = (d - 26) * 4096 + 8193 + m_input.get_bits (12);
} else {
dist = (d - 28) * 8192 + 16385 + m_input.get_bits (13);
}
while (length-- > 0) {
put_byte_dist (dist);
}
}
}
if (new_block) {
if (m_last_block) {
return false;
}
// read new block header
m_last_block = m_input.get_bit ();
unsigned int t = m_input.get_bits (2);
if (t == 0) {
// uncompressed data
m_input.skip_to_byte ();
m_uncompressed_length = m_input.get_bits (16);
m_input.get_bits (16);
} else if (t == 1 || t == 2) {
if (t == 1) {
// KLUDGE: should use a different decoder object, so we save time to do this:
mp_lit_decoder->fill_fixed_table_length ();
mp_dist_decoder->fill_fixed_table_dist ();
} else {
unsigned int hlit = m_input.get_bits (5) + 257;
unsigned int hdist = m_input.get_bits (5) + 1;
unsigned int hclen = m_input.get_bits (4) + 4;
unsigned int hclengths [19];
for (unsigned int i = 0; i < sizeof (hclengths) / sizeof (hclengths [0]); ++i) {
hclengths [i] = 0;
}
static unsigned int hclen_order [] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2,
14, 1, 15
};
for (unsigned int i = 0; i < hclen; ++i) {
hclengths [hclen_order [i]] = m_input.get_bits (3);
}
HuffmannDecoder ldecoder;
ldecoder.init_codes (hclengths, hclengths + sizeof (hclengths) / sizeof (hclengths[0]));
unsigned int lengths [286 + 32];
unsigned int nlengths = hlit + hdist;
for (unsigned int i = 0; i < nlengths; ) {
unsigned short l = ldecoder.decode (m_input);
if (l < 16) {
lengths [i++] = l;
} else if (l == 16) {
unsigned int n = m_input.get_bits (2) + 3;
tl_assert (i > 0);
l = lengths [i - 1];
while (n-- > 0) {
tl_assert (i < nlengths);
lengths [i++] = l;
}
} else if (l == 17) {
unsigned int n = m_input.get_bits (3) + 3;
while (n-- > 0) {
tl_assert (i < nlengths);
lengths [i++] = 0;
}
} else if (l == 18) {
unsigned int n = m_input.get_bits (7) + 11;
while (n-- > 0) {
tl_assert (i < nlengths);
lengths [i++] = 0;
}
} else {
tl_assert (false);
}
}
mp_lit_decoder->init_codes (lengths, lengths + hlit);
mp_dist_decoder->init_codes (lengths + hlit, lengths + nlengths);
}
} else {
throw tl::Exception (tl::translate ("Invalid compression type: %d"), t);
}
} else {
return true;
}
}
}
// ------------------------------------------------------------------------
// DeflateFilter implementation
// This implementation is based on the zlib
DeflateFilter::DeflateFilter (tl::OutputStream &output)
: m_finished (false), mp_output (&output), m_uc (0), m_cc (0)
{
mp_stream = new z_stream ();
mp_stream->zalloc = (alloc_func)0;
mp_stream->zfree = (free_func)0;
mp_stream->opaque = (voidpf)0;
mp_stream->next_in = (Byte *)0;
mp_stream->avail_in = 0;
mp_stream->next_out = (Byte *)m_buffer;
mp_stream->avail_out = sizeof (m_buffer);
int err = deflateInit2 (mp_stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15 /* == raw deflate data*/, 8 /* == default memory level */, Z_DEFAULT_STRATEGY);
tl_assert (err == Z_OK);
}
DeflateFilter::~DeflateFilter ()
{
delete mp_stream;
}
void
DeflateFilter::put (const char *b, size_t n)
{
m_uc += n;
mp_stream->next_in = (Byte *)b;
mp_stream->avail_in = n;
while (mp_stream->avail_in > 0) {
int err = deflate (mp_stream, Z_NO_FLUSH);
tl_assert (err == Z_OK);
if (mp_stream->avail_out == 0) {
m_cc += sizeof (m_buffer);
mp_output->put (m_buffer, sizeof (m_buffer));
mp_stream->next_out = (Byte *)m_buffer;
mp_stream->avail_out = sizeof (m_buffer);
}
}
}
void
DeflateFilter::flush ()
{
while (true) {
int err = deflate (mp_stream, Z_FINISH);
tl_assert (err == Z_OK || err == Z_STREAM_END);
m_cc += sizeof (m_buffer) - mp_stream->avail_out;
mp_output->put (m_buffer, sizeof (m_buffer) - mp_stream->avail_out);
mp_stream->next_out = (Byte *)m_buffer;
mp_stream->avail_out = sizeof (m_buffer);
if (err == Z_STREAM_END) {
break;
}
}
int err = deflateEnd (mp_stream);
tl_assert (err == Z_OK);
m_finished = true;
}
}