Skip to content

Commit 28e3b90

Browse files
author
Marcin Gomulak
authored
Initial - all functions working
1 parent 7f546a3 commit 28e3b90

File tree

3 files changed

+987
-0
lines changed

3 files changed

+987
-0
lines changed

LZ77.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#include "Compressions.h"
5+
6+
int localindex = 0;
7+
int outbuffindex = 0;
8+
9+
unsigned char ReadByte(unsigned char * buffer);
10+
unsigned short ReadUInt16(unsigned char * buffer);
11+
void WriteByte(unsigned char * buffer, unsigned char _value);
12+
void WriteBytes(unsigned char * buffer, unsigned char * _value, int offset,int length);
13+
unsigned char * buff;
14+
15+
16+
unsigned char * LZ77 (char*buffer, int buffsize)
17+
{
18+
if(buffsize > 0xFFFFFF)
19+
{
20+
printf("Error, too big input file!");
21+
exit(1);
22+
}
23+
24+
return _LZ77((unsigned char*)buffer, buffsize);
25+
26+
/*int compressedLength = 4;
27+
28+
unsigned char * outbuff = (unsigned char*)malloc(8*2+1);
29+
*(outbuff) = 0x00;
30+
int bufferlength = 1, bufferedblocks = 0;
31+
int readBytes = 0;
32+
while(readBytes < buffsize)
33+
{
34+
if(bufferedblocks==8)
35+
{
36+
writebytes(outbuffer, outbuff, 0, bufferlength);
37+
compressedLength += bufferlength;
38+
*outbuff = 0;
39+
bufferlength = 1;
40+
bufferedblocks = 0;
41+
}
42+
}
43+
*/
44+
}
45+
46+
47+
unsigned char * UNLZ77(unsigned char*buffer, int buffsize)
48+
{
49+
localindex = 1;
50+
int size = ReadUInt16(buffer) | (ReadByte(buffer) << 16);
51+
unsigned char * outbuff = (unsigned char*)malloc(size);
52+
53+
int i = 0;
54+
int j = 0;
55+
while(outbuffindex < size)
56+
{
57+
int flagByte = ReadByte(buffer);
58+
for(i = 0; i<8; i++)
59+
{
60+
if((flagByte & (0x80 >> i)) == 0)
61+
{
62+
*(outbuff+outbuffindex++) = ReadByte(buffer);
63+
}
64+
else
65+
{
66+
unsigned short block = ReadUInt16(buffer);
67+
int count = ((block>>4) & 0xF) + 3;
68+
int disp = ((block & 0xF) << 8) | ((block >> 8)&0xFF);
69+
long outPos = outbuffindex;
70+
long copyPos = outbuffindex - disp - 1;
71+
72+
for (j = 0; j<count; j++)
73+
{
74+
outbuffindex = copyPos++;
75+
unsigned char b = *(outbuff+outbuffindex++);
76+
outbuffindex = outPos++;
77+
*(outbuff+outbuffindex++) = b;
78+
}
79+
}
80+
if(outbuffindex >= size)
81+
break;
82+
}
83+
}
84+
outbuffindex = 0;
85+
mysize = size;
86+
return outbuff;
87+
}
88+
89+
unsigned char ReadByte(unsigned char * buffer)
90+
{
91+
localindex++;
92+
return (unsigned char)*(buffer+localindex-1);
93+
}
94+
95+
unsigned short ReadUInt16(unsigned char * buffer)
96+
{
97+
unsigned char byte1 = ReadByte(buffer);
98+
unsigned char byte2= ReadByte(buffer);
99+
return (byte2 << 8) | byte1;
100+
}
101+
102+
void WriteByte(unsigned char * buffer, unsigned char _value)
103+
{
104+
*(buffer + localindex++) = _value;
105+
}
106+
107+
void WriteBytes(unsigned char * buffer, unsigned char * _value, int offset,int length)
108+
{
109+
int i;
110+
for(i = 0; i<length; i++)
111+
*(buffer+offset++) = *(_value+i);
112+
}

0 commit comments

Comments
 (0)