File: CSBzip2Handle.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 (111 lines) | stat: -rw-r--r-- 2,513 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#import "CSBzip2Handle.h"

NSString *CSBzip2Exception=@"CSBzip2Exception";

@implementation CSBzip2Handle

+(CSBzip2Handle *)bzip2HandleWithHandle:(CSHandle *)handle
{
	return [[[self alloc] initWithHandle:handle length:CSHandleMaxLength name:[handle name]] autorelease];
}

+(CSBzip2Handle *)bzip2HandleWithHandle:(CSHandle *)handle length:(off_t)length
{
	return [[[self alloc] initWithHandle:handle length:length name:[handle name]] autorelease];
}

-(id)initWithHandle:(CSHandle *)handle length:(off_t)length name:(NSString *)descname
{
	if((self=[super initWithName:descname]))
	{
		parent=[handle retain];
		startoffs=[parent offsetInFile];
		inited=NO;
		checksumcorrect=YES;
	}
	return self;
}

-(void)dealloc
{
	if(inited) BZ2_bzDecompressEnd(&bzs);
	[parent release];

	[super dealloc];
}

-(void)resetStream
{
	[parent seekToFileOffset:startoffs];

	if(inited) BZ2_bzDecompressEnd(&bzs);
	memset(&bzs,0,sizeof(bzs));
	BZ2_bzDecompressInit(&bzs,0,0);

	checksumcorrect=YES;
}

-(int)streamAtMost:(int)num toBuffer:(void *)buffer
{
	bzs.next_out=buffer;
	bzs.avail_out=num;

	while(bzs.avail_out)
	{
		if(!bzs.avail_in)
		{
			bzs.avail_in=[parent readAtMost:sizeof(inbuffer) toBuffer:inbuffer];
			bzs.next_in=(void *)inbuffer;

			if(!bzs.avail_in) [parent _raiseEOF];
		}

		int err=BZ2_bzDecompress(&bzs);
		if(err==BZ_STREAM_END)
		{
			// Attempt to find another concaternated bzip2 stream.

			// Move any remaining data to start of buffer.
			memmove(inbuffer,bzs.next_in,bzs.avail_in);
			bzs.next_in=(void *)inbuffer;

			// Fill up buffer.
			int spaceleft=sizeof(inbuffer)-bzs.avail_in;
			int more=[parent readAtMost:spaceleft toBuffer:inbuffer+bzs.avail_in];
			bzs.avail_in+=more;

			// Check for another stream header.
			if(bzs.avail_in<20||inbuffer[0]!='B'||inbuffer[1]!='Z'||inbuffer[2]!='h'
			||inbuffer[3]<'0'||inbuffer[3]>'9'||inbuffer[4]!=0x31||inbuffer[5]!=0x41
			||inbuffer[6]!=0x59||inbuffer[7]!=0x26||inbuffer[8]!=0x53||inbuffer[9]!=0x59)
			{
				// No other stream available, stop.
				[self endStream];
				break;
			}

			BZ2_bzDecompressEnd(&bzs);
			BZ2_bzDecompressInit(&bzs,0,0);
		}
		else if(err!=BZ_OK)
		{
			if(err==BZ_DATA_ERROR) checksumcorrect=NO;
			[self _raiseBzip2:err];
		}
	}

	return num-bzs.avail_out;
}

-(BOOL)hasChecksum { return YES; }

-(BOOL)isChecksumCorrect { return checksumcorrect; }

-(void)_raiseBzip2:(int)error
{
	[NSException raise:CSBzip2Exception
	format:@"Bzlib error while attepting to read from \"%@\": %d.",name,error];
}

@end