FireBirdLib - Topfield TMS PVR TAP Programming Library
CompressTFD.c
Go to the documentation of this file.
1#include "libFireBird.h"
2#include "FBLib_compression.h"
3
4// CompressTFD() is a function wrapper that encodes 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 including 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 CompressTFD(byte *pSrc, dword SourceBufferSize, byte *pDest, word TFDType, word SysID, void *pPercentFinishedCallback)
21{
22 TRACEENTER();
23
24 word OrigSize, CompSize;
25 dword OutBufferSize, NrBlocks = 0, FullSize = SourceBufferSize;
26 byte *FileHeader;
27
28 if(!pSrc || !pDest)
29 {
30 TRACEEXIT();
31 return 0;
32 }
33
34 //PercentFinishedCallback is called for every block. PercentFinished contains a number between 0 and 100
35 void (*PercentFinishedCallback) (dword PercentFinished) = pPercentFinishedCallback;
36
37 //Build the tfd file header
38 FileHeader = pDest;
39 if(pDest)
40 {
41 STORE_WORD(pDest , 0x0008);
42 STORE_WORD(pDest + 4, SysID);
43 STORE_WORD(pDest + 6, 0x0001);
44 pDest += 10;
45 }
46 OutBufferSize = 10;
47
48 while(SourceBufferSize)
49 {
50 if(PercentFinishedCallback) PercentFinishedCallback((FullSize - SourceBufferSize) * 100 / FullSize);
51
52 NrBlocks++;
53 OrigSize = (SourceBufferSize > 0x7ffa) ? 0x7ffa : SourceBufferSize;
54
55 if(pDest)
56 {
57 CompSize = CompressBlock(pSrc, OrigSize, pDest + 8);
58
59 //Build the block header
60 STORE_WORD(pDest , CompSize + 6);
61 STORE_WORD(pDest + 4, TFDType);
62 STORE_WORD(pDest + 6, OrigSize);
63 STORE_WORD(pDest + 2, CRC16(0, pDest + 4, 4 + CompSize));
64 pDest += CompSize + 8;
65 }
66 else CompSize = CompressBlock(pSrc, OrigSize, NULL);
67
68 OutBufferSize += CompSize + 8;
69 SourceBufferSize -= OrigSize;
70 pSrc += OrigSize;
71 }
72
73 if(FileHeader)
74 {
75 STORE_WORD(FileHeader + 8, NrBlocks);
76 STORE_WORD(FileHeader + 2, CRC16 (0, FileHeader + 4, 6));
77 }
78
79 if(PercentFinishedCallback) PercentFinishedCallback(100);
80
81 TRACEEXIT();
82 return OutBufferSize;
83}
word CRC16(word StartValue, void *StartAddress, dword Length)
Definition: CRC16.c:23
dword CompressTFD(byte *pSrc, dword SourceBufferSize, byte *pDest, word TFDType, word SysID, void *pPercentFinishedCallback)
Definition: CompressTFD.c:20
#define STORE_WORD(x, y)
word CompressBlock(byte *inputbuffer, word inputsize, byte *outputbuffer)
Definition: lh5.c:1309
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243