FireBirdLib - Topfield TMS PVR TAP Programming Library
UncompressTFD.c
Go to the documentation of this file.
1#include "libFireBird.h"
2#include "FBLib_compression.h"
3
4// UncompressTFD() is a function wrapper that decodes data blocks
5// encoded with AR002 algorithm. This type uses a header
6
7// The expected block structure is as follows:
8// 0 - header length (always 0x0008)
9// 2 - header CRC16
10// 4 - System ID
11// 6 - file version (always 0x0001)
12// 8 - number of compressed blocks
13
14//Every block has the following header:
15// 0 - compressed size includeing header (word)
16// 2 - CRC-16 (word)
17// 4 - block type
18// 6 - uncompressed size (word)
19// 8 .. (compressed size + 5) - compressed data (byte array)
20dword UncompressTFD(byte *pSrc, byte *pDest, void *pPercentFinishedCallback)
21{
22 TRACEENTER();
23
24 word compSize = 0, uncompSize = 0, NrBlocks = 0;
25 dword outSize = 0, i;
26
27 if(!pSrc || !pDest)
28 {
29 TRACEEXIT();
30 return 0;
31 }
32
33 //PercentFinishedCallback is called for every block. PercentFinished contains a number between 0 and 100
34 void (*PercentFinishedCallback) (dword PercentFinished) = pPercentFinishedCallback;
35
36 if(LOAD_WORD(pSrc) != 8)
37 {
38 //Invalid header
39 TRACEEXIT();
40 return 0;
41 }
42
43 if(CRC16 (0, pSrc + 4, 6) != LOAD_WORD(pSrc + 2))
44 {
45 //Invalid header CRC
46 TRACEEXIT();
47 return 0;
48 }
49
50 if(LOAD_WORD(pSrc + 6) != 1)
51 {
52 //Invalid file version
53 TRACEEXIT();
54 return 0;
55 }
56
57 NrBlocks = LOAD_WORD(pSrc + 8);
58
59 pSrc += 10;
60
61 for(i = 0; i < NrBlocks; i++)
62 {
63 if(PercentFinishedCallback) PercentFinishedCallback(i * 100 / NrBlocks);
64
65 compSize = LOAD_WORD(pSrc) - 6;
66 uncompSize = LOAD_WORD(pSrc + 6);
67
68 if(uncompSize > 0x7ffa)
69 {
70 //Size of uncompressed block too large
71 TRACEEXIT();
72
73 return 0;
74 }
75
76 pSrc += 8;
77
78 if(compSize == uncompSize)
79 {
80 // not compressed data, copy it directly
81 if(pDest) memcpy(pDest, pSrc, uncompSize);
82 }
83 else
84 {
85 // compressed data, uncompress it
86 if(!UncompressBlock(pSrc, compSize, pDest, uncompSize))
87 {
88 //Uncompress failed
89 TRACEEXIT();
90 return 0;
91 }
92 }
93
94 if(pDest) pDest += uncompSize;
95 pSrc += compSize;
96 outSize += uncompSize;
97 }
98 if(PercentFinishedCallback) PercentFinishedCallback(100);
99
100 TRACEEXIT();
101 return outSize;
102}
word CRC16(word StartValue, void *StartAddress, dword Length)
Definition: CRC16.c:23
#define LOAD_WORD(x)
dword UncompressTFD(byte *pSrc, byte *pDest, void *pPercentFinishedCallback)
Definition: UncompressTFD.c:20
word UncompressBlock(byte *inputbuffer, word inputsize, byte *outputbuffer, word BufferSize)
Definition: lh5.c:1342
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243