File: XADCompactProLZHHandle.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (95 lines) | stat: -rw-r--r-- 2,072 bytes parent folder | download | duplicates (2)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#import "XADCompactProLZHHandle.h"
#import "XADException.h"

@implementation XADCompactProLZHHandle

-(id)initWithHandle:(CSHandle *)handle blockSize:(int)blocklen
{
	if((self=[super initWithHandle:handle windowSize:8192]))
	{
		blocksize=blocklen;
		literalcode=lengthcode=offsetcode=nil;
	}
	return self;
}

-(void)dealloc
{
	[literalcode release];
	[lengthcode release];
	[offsetcode release];
	[super dealloc];
}

-(void)resetLZSSHandle
{
	blockcount=blocksize;
	blockstart=0;
}

-(int)nextLiteralOrOffset:(int *)offset andLength:(int *)length atPosition:(off_t)pos
{
	@try
	{
		if(blockcount>=blocksize)
		{
			if(blockstart)
			{
				// Don't let your bad implementations leak into your file formats, people!
				CSInputSkipToByteBoundary(input);
				if((CSInputBufferOffset(input)-blockstart)&1) CSInputSkipBytes(input,3);
				else CSInputSkipBytes(input,2);
			}

			[literalcode release];
			[lengthcode release];
			[offsetcode release];
			literalcode=lengthcode=offsetcode=nil;
			literalcode=[self allocAndParseCodeOfSize:256];
			lengthcode=[self allocAndParseCodeOfSize:64];
			offsetcode=[self allocAndParseCodeOfSize:128];
			blockcount=0;
			blockstart=CSInputBufferOffset(input);
		}

		if(CSInputNextBit(input))
		{
			blockcount+=2;
			return CSInputNextSymbolUsingCode(input,literalcode);
		}
		else
		{
			blockcount+=3;

			*length=CSInputNextSymbolUsingCode(input,lengthcode);

			*offset=CSInputNextSymbolUsingCode(input,offsetcode)<<6;
			*offset|=CSInputNextBitString(input,6);

			return XADLZSSMatch;
		}
	}
	@catch(id e) { }

	return XADLZSSEnd;
}

-(XADPrefixCode *)allocAndParseCodeOfSize:(int)size
{
	int numbytes=CSInputNextByte(input);
	if(numbytes*2>size) [XADException raiseIllegalDataException];

	int codelengths[size];

	for(int i=0;i<numbytes;i++)
	{
		int val=CSInputNextByte(input);
		codelengths[2*i]=val>>4;
		codelengths[2*i+1]=val&0x0f;
	}
	for(int i=numbytes*2;i<size;i++) codelengths[i]=0;

	return [[XADPrefixCode alloc] initWithLengths:codelengths numberOfSymbols:size maximumLength:15 shortestCodeIsZeros:YES];
}

@end