FireBirdLib - Topfield TMS PVR TAP Programming Library
UncompressFirmware.c
Go to the documentation of this file.
1#include "libFireBird.h"
2#include "FBLib_compression.h"
3
4// UncompressFirmware() is a function wrapper that decodes data blocks
5// encoded with AR002 algorithm until the uncompressed size field
6// is set to 0xfefe. This is normaly used for the Loader or the Firmware
7// inside of the flash memory.
8// The expected block structure is as follows:
9// 0 - uncompressed size (word)
10// 2 - compressed size (incl CRC-16) (word)
11// 4 - CRC-16 (word)
12// 6 .. (compressed size + 5) - compressed data (byte array)
13dword UncompressFirmware(byte *pSrc, byte *pDest, void *pPercentFinishedCallback)
14{
15 TRACEENTER();
16
17 word compSize = 0, uncompSize = 0;
18 dword outSize = 0, NrBlocks = 0, CurrentBlock = 0;
19 byte *OrigpSrc;
20
21 if(!pSrc || !pDest)
22 {
23 TRACEEXIT();
24 return 0;
25 }
26
27 //PercentFinishedCallback is called for every block. PercentFinished contains a number between 0 and 100
28 void (*PercentFinishedCallback) (dword PercentFinished) = pPercentFinishedCallback;
29
30 OrigpSrc = pSrc;
31 uncompSize = LOAD_WORD(pSrc + 0);
32 compSize = LOAD_WORD(pSrc + 2);
33
34 //Count the number of blocks
35 while(uncompSize != 0xfefe)
36 {
37 NrBlocks++;
38
39 if(uncompSize > 0x8000)
40 {
41 //Uncompressed data block size too large
42 TRACEEXIT();
43
44 return 0;
45 }
46
47 pSrc += 4;
48 pSrc += compSize;
49
50 uncompSize = LOAD_WORD(pSrc + 0);
51 compSize = LOAD_WORD(pSrc + 2);
52 }
53
54 pSrc = OrigpSrc;
55 uncompSize = LOAD_WORD(pSrc + 0);
56 compSize = LOAD_WORD(pSrc + 2);
57
58 while(uncompSize != 0xfefe)
59 {
60 if(PercentFinishedCallback) PercentFinishedCallback(CurrentBlock * 100 / NrBlocks);
61 CurrentBlock++;
62
63 if(uncompSize > 0x8000)
64 {
65 //Uncompressed data block size too large
66 TRACEEXIT();
67
68 return 0;
69 }
70
71 pSrc += 6;
72
73 if(compSize == uncompSize)
74 {
75 // data not compressed, copy it directly
76 if(pDest) memcpy(pDest, pSrc, uncompSize);
77 }
78 else
79 {
80 // compressed data, uncompress it
81 if(!UncompressBlock(pSrc, compSize, pDest, uncompSize))
82 {
83 //Uncompress has failed
84 TRACEEXIT();
85 return 0;
86 }
87 }
88
89 if(pDest) pDest += uncompSize;
90 pSrc += compSize;
91 outSize += uncompSize;
92
93 uncompSize = LOAD_WORD(pSrc + 0);
94 compSize = LOAD_WORD(pSrc + 2);
95 }
96 if(PercentFinishedCallback) PercentFinishedCallback(100);
97
98 TRACEEXIT();
99 return outSize;
100}
#define LOAD_WORD(x)
dword UncompressFirmware(byte *pSrc, byte *pDest, void *pPercentFinishedCallback)
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