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