-
Notifications
You must be signed in to change notification settings - Fork 45
/
CSBzip2Handle.m
128 lines (105 loc) · 3.22 KB
/
CSBzip2Handle.m
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* CSBzip2Handle.m
*
* Copyright (c) 2017-present, MacPaw Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#import "CSBzip2Handle.h"
NSString *CSBzip2Exception=@"CSBzip2Exception";
@implementation CSBzip2Handle
+(CSBzip2Handle *)bzip2HandleWithHandle:(CSHandle *)handle
{
return [[[self alloc] initWithHandle:handle length:CSHandleMaxLength] autorelease];
}
+(CSBzip2Handle *)bzip2HandleWithHandle:(CSHandle *)handle length:(off_t)length
{
return [[[self alloc] initWithHandle:handle length:length] autorelease];
}
-(id)initWithHandle:(CSHandle *)handle length:(off_t)length
{
if((self=[super initWithParentHandle:handle length:length]))
{
startoffs=[parent offsetInFile];
inited=NO;
checksumcorrect=YES;
}
return self;
}
-(void)dealloc
{
if(inited) BZ2_bzDecompressEnd(&bzs);
[super dealloc];
}
-(void)resetStream
{
[parent seekToFileOffset:startoffs];
if(inited) BZ2_bzDecompressEnd(&bzs);
memset(&bzs,0,sizeof(bzs));
inited=(BZ2_bzDecompressInit(&bzs,0,0)==BZ_OK);
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);
inited=(BZ2_bzDecompressInit(&bzs,0,0)==BZ_OK);
}
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.",[self name],error];
}
@end