File: XADLZSSHandle.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 (81 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download
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
#import "XADLZSSHandle.h"

@implementation XADLZSSHandle

-(id)initWithName:(NSString *)descname windowSize:(int)windowsize
{
	return [self initWithName:descname length:CSHandleMaxLength windowSize:windowsize];
}

-(id)initWithName:(NSString *)descname length:(off_t)length windowSize:(int)windowsize
{
	if((self=[super initWithName:descname length:length]))
	{
		nextliteral_ptr=(int (*)(id,SEL,int *,int *,off_t))
		[self methodForSelector:@selector(nextLiteralOrOffset:andLength:atPosition:)];

		windowbuffer=malloc(windowsize);
		windowmask=windowsize-1; // Assumes windows are always power-of-two sized!
	}
	return self;
}

-(id)initWithHandle:(CSHandle *)handle windowSize:(int)windowsize
{
	return [self initWithHandle:handle length:CSHandleMaxLength windowSize:windowsize];
}

-(id)initWithHandle:(CSHandle *)handle length:(off_t)length windowSize:(int)windowsize
{
	if((self=[super initWithHandle:handle length:length]))
	{
		nextliteral_ptr=(int (*)(id,SEL,int *,int *,off_t))
		[self methodForSelector:@selector(nextLiteralOrOffset:andLength:atPosition:)];

		windowbuffer=malloc(windowsize);
		windowmask=windowsize-1; // Assumes windows are always power-of-two sized!
	}
	return self;
}

-(void)dealloc
{
	free(windowbuffer);
	[super dealloc];
}

-(void)resetByteStream
{
	matchlength=0;
	matchoffset=0;
	memset(windowbuffer,0,windowmask+1);

	[self resetLZSSHandle];
}

-(uint8_t)produceByteAtOffset:(off_t)pos
{
	if(!matchlength)
	{
		int offset,length;
		int val=nextliteral_ptr(self,@selector(nextLiteralOrOffset:andLength:atPosition:),&offset,&length,pos);

		if(val>=0) return windowbuffer[pos&windowmask]=val;
		else if(val==XADLZSSEnd) CSByteStreamEOF(self);
		else
		{
			matchlength=length;
			matchoffset=pos-offset;
		}
	}

	matchlength--;
	uint8_t byte=windowbuffer[matchoffset++&windowmask];
	return windowbuffer[pos&windowmask]=byte;
}

-(void)resetLZSSHandle {}

-(int)nextLiteralOrOffset:(int *)offset andLength:(int *)length atPosition:(off_t)pos { return XADLZSSEnd; }

@end