-
Notifications
You must be signed in to change notification settings - Fork 0
/
bzip.cpp
44 lines (42 loc) · 1.19 KB
/
bzip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stddef.h>
#include "definitions.h"
#include "bwt.h"
#include "huffman.h"
#include "mtf.h"
using namespace std;
byte* BZIPEncode(const byte* in, int inSize, int& outSize, int& lastBytePosition, byte* codesLengths)
{
if (!inSize) return NULL;
byte* out = NULL;
byte* BWTMTFEncodedBlock = new byte[inSize];
try
{
lastBytePosition = BWTEncode(in, BWTMTFEncodedBlock, inSize);
MTFEncode(BWTMTFEncodedBlock, inSize);
out = HuffmanEncode(BWTMTFEncodedBlock, inSize, outSize, codesLengths);
}
catch(...)
{
delete [] BWTMTFEncodedBlock;
throw;
}
delete [] BWTMTFEncodedBlock;
return out;
}
void BZIPDecode(const byte* in, int inSize, byte* out, int outSize, int lastBytePosition, const byte* codesLengths)
{
if (!inSize || !outSize) return;
byte* HuffmanDecodedBlock = new byte[outSize];
try
{
HuffmanDecode(in, inSize, HuffmanDecodedBlock, outSize, codesLengths);
MTFDecode(HuffmanDecodedBlock, outSize);
BWTDecode(HuffmanDecodedBlock, out, outSize, lastBytePosition);
}
catch(...)
{
delete [] HuffmanDecodedBlock;
throw;
}
delete [] HuffmanDecodedBlock;
}