Subversion Repositories freemyipod

Rev

Rev 494 | Details | Compare with Previous | 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
 
649 theseven 63
//#define DEBUG_CONSOLES 2
64
//#define DEBUG_PRINT_SOURCE_LINE
494 theseven 65
 
649 theseven 66
 
494 theseven 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 */
649 theseven 230
   if (!d->bitcount)
494 theseven 231
   {
649 theseven 232
       while (!d->bytecount)
494 theseven 233
       {
234
           DEBUGF("tinf_getbit: refilling bytes");
649 theseven 235
           DEBUGF("tinf_getbit: bytecount=%d, bitcount=%d, source=0x%08X",
236
                  d->bytecount, d->bitcount, d->source);
494 theseven 237
           d->bytecount = (d->source[4] << 24) | (d->source[5] << 16)
238
                        | (d->source[6] << 8) | d->source[7];
239
           d->source += 12;
649 theseven 240
           DEBUGF("tinf_getbit: bytecount=%d, bitcount=%d, source=0x%08X",
241
                  d->bytecount, d->bitcount, d->source);
494 theseven 242
       }
243
      /* load next tag */
244
      d->tag = *d->source++;
245
      d->bitcount = 8;
649 theseven 246
      d->bytecount--;
494 theseven 247
   }
248
 
249
   /* shift bit out of tag */
250
   bit = d->tag & 1;
251
   d->tag >>= 1;
649 theseven 252
   d->bitcount--;
494 theseven 253
 
254
//   DEBUGF("tinf_getbit: returning bit %d", bit);
255
   return bit;
256
}
257
 
258
/* read a num bit value from a stream and add base */
259
static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base)
260
{
261
   unsigned int val = 0;
262
 
263
   /* read num bits */
264
   if (num)
265
   {
266
      unsigned int limit = 1 << (num);
267
      unsigned int mask;
268
 
269
      for (mask = 1; mask < limit; mask <<= 1)
270
         if (tinf_getbit(d)) val |= mask;
271
   }
272
 
273
   return val + base;
274
}
275
 
276
/* given a data stream and a tree, decode a symbol */
277
static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t)
278
{
279
   int sum = 0, cur = 0, len = 0;
280
 
281
   /* get more bits while code value is above sum */
282
   do {
283
 
284
      cur = (cur << 1) + tinf_getbit(d);
285
 
286
      ++len;
287
 
288
      sum += t->table[len];
289
      cur -= t->table[len];
290
 
291
   } while (cur >= 0);
292
 
293
   return t->trans[sum + cur];
294
}
295
 
296
/* given a data stream, decode dynamic trees from it */
297
static void tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl)
298
{
299
   TINF_TREE code_tree;
300
   unsigned char lengths[288+32];
301
   unsigned int hlit, hdist, hclen;
302
   unsigned int i, num, length;
303
 
304
   /* get 5 bits HLIT (257-286) */
305
   hlit = tinf_read_bits(d, 5, 257);
306
 
307
   /* get 5 bits HDIST (1-32) */
308
   hdist = tinf_read_bits(d, 5, 1);
309
 
310
   /* get 4 bits HCLEN (4-19) */
311
   hclen = tinf_read_bits(d, 4, 4);
312
 
313
   /* for (i = 0; i < 19; ++i) lengths[i] = 0; */
314
   memset(lengths, 0, sizeof(unsigned char)*19);
315
 
316
   /* read code lengths for code length alphabet */
317
   for (i = 0; i < hclen; ++i)
318
   {
319
      /* get 3 bits code length (0-7) */
320
      unsigned int clen = tinf_read_bits(d, 3, 0);
321
 
322
      lengths[tbl->clcidx[i]] = clen;
323
   }
324
 
325
   /* build code length tree */
326
   tinf_build_tree(&code_tree, lengths, 19);
327
 
328
   /* decode code lengths for the dynamic trees */
329
   for (num = 0; num < hlit + hdist; )
330
   {
331
      int sym = tinf_decode_symbol(d, &code_tree);
332
 
333
      switch (sym)
334
      {
335
      case 16:
336
         /* copy previous code length 3-6 times (read 2 bits) */
337
         {
338
            unsigned char prev = lengths[num - 1];
339
            for (length = tinf_read_bits(d, 2, 3); length; --length)
340
            {
341
               lengths[num++] = prev;
342
            }
343
         }
344
         break;
345
      case 17:
346
         /* repeat code length 0 for 3-10 times (read 3 bits) */
347
         for (length = tinf_read_bits(d, 3, 3); length; --length)
348
         {
349
            lengths[num++] = 0;
350
         }
351
         break;
352
      case 18:
353
         /* repeat code length 0 for 11-138 times (read 7 bits) */
354
         for (length = tinf_read_bits(d, 7, 11); length; --length)
355
         {
356
            lengths[num++] = 0;
357
         }
358
         break;
359
      default:
360
         /* values 0-15 represent the actual code lengths */
361
         lengths[num++] = sym;
362
         break;
363
      }
364
   }
365
 
366
   /* build dynamic trees */
367
   tinf_build_tree(lt, lengths, hlit);
368
   tinf_build_tree(dt, lengths + hlit, hdist);
369
}
370
 
371
/* ----------------------------- *
372
 * -- block inflate functions -- *
373
 * ----------------------------- */
374
 
375
/* given a stream and two trees, inflate a block of data */
376
static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl)
377
{
378
   /* remember current output position */
379
   unsigned char *start = d->dest;
380
 
381
   while (1)
382
   {
383
      int sym = tinf_decode_symbol(d, lt);
384
 
385
      /* check for end of block */
386
      if (sym == 256)
387
      {
388
         *d->destLen += d->dest - start;
389
         return 0;
390
      }
391
 
392
      if (sym < 256)
393
      {
394
         *d->dest++ = sym;
395
 
396
      } else {
397
 
398
         int length, dist, offs;
399
         int i;
400
 
401
         sym -= 257;
402
 
403
         /* possibly get more bits from length code */
404
         length = tinf_read_bits(d, tbl->length_bits[sym], tbl->length_base[sym]);
405
 
406
         dist = tinf_decode_symbol(d, dt);
407
 
408
         /* possibly get more bits from distance code */
409
         offs = tinf_read_bits(d, tbl->dist_bits[dist], tbl->dist_base[dist]);
410
 
411
         /* copy match */
412
         for (i = 0; i < length; ++i)
413
         {
414
            d->dest[i] = d->dest[i - offs];
415
         }
416
 
417
         d->dest += length;
418
      }
419
   }
420
}
421
 
422
/* inflate an uncompressed block of data */
423
static int tinf_inflate_uncompressed_block(TINF_DATA *d)
424
{
425
   unsigned int length, invlength;
426
   /* unsigned int i; */
427
 
428
   /* get length */
429
   length = d->source[1];
430
   length = (length << 8) + d->source[0];
431
 
432
   /* get one's complement of length */
433
   invlength = d->source[3];
434
   invlength = (invlength << 8) + d->source[2];
435
 
436
   /* check length */
437
   if (length != (~invlength & 0x0000ffff)) return -5;
438
 
439
   d->source += 4;
440
 
441
   /* copy block */
442
   /* for (i = length; i; --i) *d->dest++ = *d->source++; */
443
   memcpy(d->dest, d->source, sizeof(unsigned char)*length);
444
   d->dest += sizeof(unsigned char)*length;
445
   d->source += sizeof(unsigned char)*length;
446
 
447
   /* make sure we start next block on a byte boundary */
448
   d->bitcount = 0;
449
 
450
   *d->destLen += length;
451
 
452
   return 0;
453
}
454
 
455
/* inflate a block of data compressed with fixed huffman trees */
456
static int tinf_inflate_fixed_block(TINF_DATA *d, TINF_TABLES *tbl)
457
{
458
   /* decode block using fixed trees */
459
   return tinf_inflate_block_data(d, &tbl->sltree, &tbl->sdtree, tbl);
460
}
461
 
462
/* inflate a block of data compressed with dynamic huffman trees */
463
static int tinf_inflate_dynamic_block(TINF_DATA *d, TINF_TABLES *tbl)
464
{
465
   /* decode trees from stream */
466
   tinf_decode_trees(d, &d->ltree, &d->dtree, tbl);
467
 
468
   /* decode block using decoded trees */
469
   return tinf_inflate_block_data(d, &d->ltree, &d->dtree, tbl);
470
}
471
 
472
/* ---------------------- *
473
 * -- public functions -- *
474
 * ---------------------- */
475
 
476
/* inflate stream from source to dest */
477
int tinf_uncompress(void *dest, unsigned int *destLen,
478
                    const void *source, unsigned int sourceLen)
479
{
480
    TINF_DATA d;
481
    int bfinal;
482
 
483
    d.source = (const unsigned char *)(source + 10);
649 theseven 484
    d.bitcount = 0;
494 theseven 485
    d.bytecount = ((d.source[-10] << 24) | (d.source[-9] << 16)
649 theseven 486
                 | (d.source[-8] << 8) | d.source[-7]) - 2;
494 theseven 487
 
488
    d.dest = (unsigned char *)dest;
489
    d.destLen = destLen;
490
 
491
    *destLen = 0;
492
 
493
   DEBUGF("start: bytecount=%d, bitcount=%d, source=0x%08X",
494
          d.bytecount, d.bitcount, d.source);
495
 
496
   do {
497
 
498
       unsigned int btype;
499
       int res;
500
 
501
       /* read final block flag */
502
       bfinal = tinf_getbit(&d);
503
 
504
       /* read block type (2 bits) */
505
       btype = tinf_read_bits(&d, 2, 0);
506
 
507
       /* decompress block */
508
       switch (btype)
509
       {
510
       case 0:
511
          /* decompress uncompressed block */
512
          res = tinf_inflate_uncompressed_block(&d);
513
          break;
514
        case 1:
515
          /* decompress block with fixed huffman trees */
516
          res = tinf_inflate_fixed_block(&d,&tbl);
517
          break;
518
       case 2:
519
          /* decompress block with dynamic huffman trees */
520
          res = tinf_inflate_dynamic_block(&d,&tbl);
521
          break;
522
       default:
523
          return -6;
524
       }
525
 
526
       if (res) return res;
527
 
528
       if (d.source > (unsigned char *)source + sourceLen)
649 theseven 529
       {
530
           DEBUGF("tinf_uncompress: Hit end of buffer! (source=0x%08X, len=%d, current=0x%08X)",
531
                  source, sourceLen, d.source);
494 theseven 532
           return -7;
649 theseven 533
       }
494 theseven 534
    } while (!bfinal);
535
 
649 theseven 536
    d.bytecount -= 4;
537
 
538
    if (d.bytecount)
539
    {
540
        DEBUGF("tinf_uncompress: %d leftover bytes, %d bits!", d.bytecount, d.bitcount);
541
        return -8;
542
    }
543
 
494 theseven 544
    return 0;
545
}