Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
494 theseven 1
/***************************************************************************
2
 *             __________               __   ___.
3
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7
 *                     \/            \/     \/    \/            \/
8
 * $Id$
9
 *
10
 * Original source:
11
 * Copyright (c) 2003 by Joergen Ibsen / Jibz
12
 *
13
 * Rockbox adaptation:
14
 * Copyright (c) 2010 by Marcin Bukat
15
 *
16
 * emCORE adaptation:
17
 * Copyright (c) 2011 by Michael Sparmann
18
 *
19
 * This program is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU General Public License
21
 * as published by the Free Software Foundation; either version 2
22
 * of the License, or (at your option) any later version.
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
25
 * KIND, either express or implied.
26
 *
27
 ****************************************************************************/
28
 
29
/*
30
 * tinflate  -  tiny inflate
31
 *
32
 * Copyright (c) 2003 by Joergen Ibsen / Jibz
33
 * All Rights Reserved
34
 *
35
 * http://www.ibsensoftware.com/
36
 *
37
 * This software is provided 'as-is', without any express
38
 * or implied warranty.  In no event will the authors be
39
 * held liable for any damages arising from the use of
40
 * this software.
41
 *
42
 * Permission is granted to anyone to use this software
43
 * for any purpose, including commercial applications,
44
 * and to alter it and redistribute it freely, subject to
45
 * the following restrictions:
46
 *
47
 * 1. The origin of this software must not be
48
 *    misrepresented; you must not claim that you
49
 *    wrote the original software. If you use this
50
 *    software in a product, an acknowledgment in
51
 *    the product documentation would be appreciated
52
 *    but is not required.
53
 *
54
 * 2. Altered source versions must be plainly marked
55
 *    as such, and must not be misrepresented as
56
 *    being the original software.
57
 *
58
 * 3. This notice may not be removed or altered from
59
 *    any source distribution.
60
 */
61
 
62
 
63
//#define DEBUG_CONSOLES 2
64
//#define DEBUG_PRINT_SOURCE_LINE
65
 
66
 
67
#include "emcorelib.h"
68
#include "tinf.h"
69
 
70
/* ------------------------------ *
71
 * -- internal data structures -- *
72
 * ------------------------------ */
73
 
74
typedef struct {
75
   unsigned short table[16];  /* table of code length counts */
76
   unsigned short trans[288]; /* code -> symbol translation table */
77
} TINF_TREE;
78
 
79
typedef struct {
80
   unsigned short table[16];
81
   unsigned short trans[32];
82
} TINF_SDTREE;
83
 
84
typedef struct {
85
   TINF_TREE sltree;
86
   TINF_TREE sdtree;
87
   unsigned char length_bits[30];
88
   unsigned short length_base[30];
89
   unsigned char dist_bits[30];
90
   unsigned short dist_base[30];
91
   unsigned char clcidx[19];
92
} TINF_TABLES;
93
 
94
typedef struct {
95
   const unsigned char *source;
96
   unsigned int tag;
97
   unsigned int bitcount;
98
   unsigned int bytecount;
99
 
100
   unsigned char *dest;
101
   unsigned int *destLen;
102
 
103
   TINF_TREE ltree; /* dynamic length/symbol tree */
104
   TINF_TREE dtree; /* dynamic distance tree */
105
} TINF_DATA;
106
 
107
/* static tables */
108
TINF_TABLES tbl = {
109
    .sltree = {
110
        .table = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0018,
111
                  0x0098, 0x0070, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
112
 
113
        .trans = {0x0100, 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0106, 0x0107,
114
                  0x0108, 0x0109, 0x010a, 0x010b, 0x010c, 0x010d, 0x010e, 0x010f,
115
                  0x0110, 0x0111, 0x0112, 0x0113, 0x0114, 0x0115, 0x0116, 0x0117,
116
                  0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
117
                  0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
118
                  0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
119
                  0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
120
                  0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
121
                  0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
122
                  0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
123
                  0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
124
                  0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
125
                  0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
126
                  0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
127
                  0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
128
                  0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
129
                  0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
130
                  0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
131
                  0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
132
                  0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
133
                  0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
134
                  0x0118, 0x0119, 0x011a, 0x011b, 0x011c, 0x011d, 0x011e, 0x011f,
135
                  0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
136
                  0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
137
                  0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
138
                  0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
139
                  0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
140
                  0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
141
                  0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
142
                  0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
143
                  0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
144
                  0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
145
                  0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
146
                  0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
147
                  0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
148
                  0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff},
149
              },
150
 
151
    .sdtree = {
152
        .table = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0000, 0x0000,
153
                  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
154
 
155
        .trans = {0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
156
                  0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
157
                  0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
158
                  0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f},
159
              },
160
 
161
    .length_bits = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162
                    0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02,
163
                    0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04,
164
                    0x05, 0x05, 0x05, 0x05, 0x00, 0x06},
165
 
166
    .length_base = {0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
167
                    0x000b, 0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f,
168
                    0x0023, 0x002b, 0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073,
169
                    0x0083, 0x00a3, 0x00c3, 0x00e3, 0x0102, 0x0143},
170
 
171
    .dist_bits = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02,
172
                  0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06, 0x06,
173
                  0x07, 0x07, 0x08, 0x08, 0x09, 0x09, 0x0a, 0x0a,
174
                  0x0b, 0x0b, 0x0c, 0x0c, 0x0d, 0x0d},
175
 
176
    .dist_base = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d,
177
                  0x0011, 0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1,
178
                  0x0101, 0x0181, 0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01,
179
                  0x1001, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001},
180
 
181
    .clcidx = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15},
182
};
183
 
184
 
185
/* ----------------------- *
186
 * -- utility functions -- *
187
 * ----------------------- */
188
 
189
/* given an array of code lengths, build a tree */
190
static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned int num)
191
{
192
   unsigned short offs[16];
193
   unsigned int i, sum;
194
 
195
   /* clear code length count table */
196
   /* for (i = 0; i < 16; ++i) t->table[i] = 0; */
197
   memset(t->table, 0, sizeof(short)*16);
198
 
199
   /* scan symbol lengths, and sum code length counts */
200
   for (i = 0; i < num; ++i) t->table[lengths[i]]++;
201
 
202
   t->table[0] = 0;
203
 
204
   /* compute offset table for distribution sort */
205
   for (sum = 0, i = 0; i < 16; ++i)
206
   {
207
      offs[i] = sum;
208
      sum += t->table[i];
209
   }
210
 
211
   /* create code->symbol translation table (symbols sorted by code) */
212
   for (i = 0; i < num; ++i)
213
   {
214
      if (lengths[i]) t->trans[offs[lengths[i]]++] = i;
215
   }
216
}
217
 
218
/* ---------------------- *
219
 * -- decode functions -- *
220
 * ---------------------- */
221
 
222
/* get one bit from source stream */
223
static int tinf_getbit(TINF_DATA *d)
224
{
225
   unsigned int bit;
226
//   DEBUGF("tinf_getbit: bytecount=%d, bitcount=%d, source=0x%08X",
227
//          d->bytecount, d->bitcount, d->source);
228
 
229
   /* check if tag is empty */
230
   if (!--d->bitcount)
231
   {
232
       while (!--d->bytecount)
233
       {
234
           DEBUGF("tinf_getbit: refilling bytes");
235
   DEBUGF("tinf_getbit: bytecount=%d, bitcount=%d, source=0x%08X",
236
          d->bytecount, d->bitcount, d->source);
237
           d->bytecount = (d->source[4] << 24) | (d->source[5] << 16)
238
                        | (d->source[6] << 8) | d->source[7];
239
           d->source += 12;
240
   DEBUGF("tinf_getbit: bytecount=%d, bitcount=%d, source=0x%08X",
241
          d->bytecount, d->bitcount, d->source);
242
       }
243
      /* load next tag */
244
      d->tag = *d->source++;
245
      d->bitcount = 8;
246
   }
247
 
248
   /* shift bit out of tag */
249
   bit = d->tag & 1;
250
   d->tag >>= 1;
251
 
252
//   DEBUGF("tinf_getbit: returning bit %d", bit);
253
   return bit;
254
}
255
 
256
/* read a num bit value from a stream and add base */
257
static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base)
258
{
259
   unsigned int val = 0;
260
 
261
   /* read num bits */
262
   if (num)
263
   {
264
      unsigned int limit = 1 << (num);
265
      unsigned int mask;
266
 
267
      for (mask = 1; mask < limit; mask <<= 1)
268
         if (tinf_getbit(d)) val |= mask;
269
   }
270
 
271
   return val + base;
272
}
273
 
274
/* given a data stream and a tree, decode a symbol */
275
static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
276
{
277
   int sum = 0, cur = 0, len = 0;
278
 
279
   /* get more bits while code value is above sum */
280
   do {
281
 
282
      cur = (cur << 1) + tinf_getbit(d);
283
 
284
      ++len;
285
 
286
      sum += t->table[len];
287
      cur -= t->table[len];
288
 
289
   } while (cur >= 0);
290
 
291
   return t->trans[sum + cur];
292
}
293
 
294
/* given a data stream, decode dynamic trees from it */
295
static void tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl)
296
{
297
   TINF_TREE code_tree;
298
   unsigned char lengths[288+32];
299
   unsigned int hlit, hdist, hclen;
300
   unsigned int i, num, length;
301
 
302
   /* get 5 bits HLIT (257-286) */
303
   hlit = tinf_read_bits(d, 5, 257);
304
 
305
   /* get 5 bits HDIST (1-32) */
306
   hdist = tinf_read_bits(d, 5, 1);
307
 
308
   /* get 4 bits HCLEN (4-19) */
309
   hclen = tinf_read_bits(d, 4, 4);
310
 
311
   /* for (i = 0; i < 19; ++i) lengths[i] = 0; */
312
   memset(lengths, 0, sizeof(unsigned char)*19);
313
 
314
   /* read code lengths for code length alphabet */
315
   for (i = 0; i < hclen; ++i)
316
   {
317
      /* get 3 bits code length (0-7) */
318
      unsigned int clen = tinf_read_bits(d, 3, 0);
319
 
320
      lengths[tbl->clcidx[i]] = clen;
321
   }
322
 
323
   /* build code length tree */
324
   tinf_build_tree(&code_tree, lengths, 19);
325
 
326
   /* decode code lengths for the dynamic trees */
327
   for (num = 0; num < hlit + hdist; )
328
   {
329
      int sym = tinf_decode_symbol(d, &code_tree);
330
 
331
      switch (sym)
332
      {
333
      case 16:
334
         /* copy previous code length 3-6 times (read 2 bits) */
335
         {
336
            unsigned char prev = lengths[num - 1];
337
            for (length = tinf_read_bits(d, 2, 3); length; --length)
338
            {
339
               lengths[num++] = prev;
340
            }
341
         }
342
         break;
343
      case 17:
344
         /* repeat code length 0 for 3-10 times (read 3 bits) */
345
         for (length = tinf_read_bits(d, 3, 3); length; --length)
346
         {
347
            lengths[num++] = 0;
348
         }
349
         break;
350
      case 18:
351
         /* repeat code length 0 for 11-138 times (read 7 bits) */
352
         for (length = tinf_read_bits(d, 7, 11); length; --length)
353
         {
354
            lengths[num++] = 0;
355
         }
356
         break;
357
      default:
358
         /* values 0-15 represent the actual code lengths */
359
         lengths[num++] = sym;
360
         break;
361
      }
362
   }
363
 
364
   /* build dynamic trees */
365
   tinf_build_tree(lt, lengths, hlit);
366
   tinf_build_tree(dt, lengths + hlit, hdist);
367
}
368
 
369
/* ----------------------------- *
370
 * -- block inflate functions -- *
371
 * ----------------------------- */
372
 
373
/* given a stream and two trees, inflate a block of data */
374
static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl)
375
{
376
   /* remember current output position */
377
   unsigned char *start = d->dest;
378
 
379
   while (1)
380
   {
381
      int sym = tinf_decode_symbol(d, lt);
382
 
383
      /* check for end of block */
384
      if (sym == 256)
385
      {
386
         *d->destLen += d->dest - start;
387
         return 0;
388
      }
389
 
390
      if (sym < 256)
391
      {
392
         *d->dest++ = sym;
393
 
394
      } else {
395
 
396
         int length, dist, offs;
397
         int i;
398
 
399
         sym -= 257;
400
 
401
         /* possibly get more bits from length code */
402
         length = tinf_read_bits(d, tbl->length_bits[sym], tbl->length_base[sym]);
403
 
404
         dist = tinf_decode_symbol(d, dt);
405
 
406
         /* possibly get more bits from distance code */
407
         offs = tinf_read_bits(d, tbl->dist_bits[dist], tbl->dist_base[dist]);
408
 
409
         /* copy match */
410
         for (i = 0; i < length; ++i)
411
         {
412
            d->dest[i] = d->dest[i - offs];
413
         }
414
 
415
         d->dest += length;
416
      }
417
   }
418
}
419
 
420
/* inflate an uncompressed block of data */
421
static int tinf_inflate_uncompressed_block(TINF_DATA *d)
422
{
423
   unsigned int length, invlength;
424
   /* unsigned int i; */
425
 
426
   /* get length */
427
   length = d->source[1];
428
   length = (length << 8) + d->source[0];
429
 
430
   /* get one's complement of length */
431
   invlength = d->source[3];
432
   invlength = (invlength << 8) + d->source[2];
433
 
434
   /* check length */
435
   if (length != (~invlength & 0x0000ffff)) return -5;
436
 
437
   d->source += 4;
438
 
439
   /* copy block */
440
   /* for (i = length; i; --i) *d->dest++ = *d->source++; */
441
   memcpy(d->dest, d->source, sizeof(unsigned char)*length);
442
   d->dest += sizeof(unsigned char)*length;
443
   d->source += sizeof(unsigned char)*length;
444
 
445
   /* make sure we start next block on a byte boundary */
446
   d->bitcount = 0;
447
 
448
   *d->destLen += length;
449
 
450
   return 0;
451
}
452
 
453
/* inflate a block of data compressed with fixed huffman trees */
454
static int tinf_inflate_fixed_block(TINF_DATA *d, TINF_TABLES *tbl)
455
{
456
   /* decode block using fixed trees */
457
   return tinf_inflate_block_data(d, &tbl->sltree, &tbl->sdtree, tbl);
458
}
459
 
460
/* inflate a block of data compressed with dynamic huffman trees */
461
static int tinf_inflate_dynamic_block(TINF_DATA *d, TINF_TABLES *tbl)
462
{
463
   /* decode trees from stream */
464
   tinf_decode_trees(d, &d->ltree, &d->dtree, tbl);
465
 
466
   /* decode block using decoded trees */
467
   return tinf_inflate_block_data(d, &d->ltree, &d->dtree, tbl);
468
}
469
 
470
/* ---------------------- *
471
 * -- public functions -- *
472
 * ---------------------- */
473
 
474
/* inflate stream from source to dest */
475
int tinf_uncompress(void *dest, unsigned int *destLen,
476
                    const void *source, unsigned int sourceLen)
477
{
478
    TINF_DATA d;
479
    int bfinal;
480
 
481
    d.source = (const unsigned char *)(source + 10);
482
    d.bitcount = 1;
483
    d.bytecount = ((d.source[-10] << 24) | (d.source[-9] << 16)
484
                 | (d.source[-8] << 8) | d.source[-7]) + 1;
485
 
486
    d.dest = (unsigned char *)dest;
487
    d.destLen = destLen;
488
 
489
    *destLen = 0;
490
 
491
   DEBUGF("start: bytecount=%d, bitcount=%d, source=0x%08X",
492
          d.bytecount, d.bitcount, d.source);
493
 
494
   do {
495
 
496
       unsigned int btype;
497
       int res;
498
 
499
       /* read final block flag */
500
       bfinal = tinf_getbit(&d);
501
 
502
       /* read block type (2 bits) */
503
       btype = tinf_read_bits(&d, 2, 0);
504
 
505
       /* decompress block */
506
       switch (btype)
507
       {
508
       case 0:
509
          /* decompress uncompressed block */
510
          res = tinf_inflate_uncompressed_block(&d);
511
          break;
512
        case 1:
513
          /* decompress block with fixed huffman trees */
514
          res = tinf_inflate_fixed_block(&d,&tbl);
515
          break;
516
       case 2:
517
          /* decompress block with dynamic huffman trees */
518
          res = tinf_inflate_dynamic_block(&d,&tbl);
519
          break;
520
       default:
521
          return -6;
522
       }
523
 
524
       if (res) return res;
525
 
526
       if (d.source > (unsigned char *)source + sourceLen)
527
           return -7;
528
    } while (!bfinal);
529
 
530
    return 0;
531
}