-
Notifications
You must be signed in to change notification settings - Fork 13
/
GZipInputData.m
135 lines (105 loc) · 3.01 KB
/
GZipInputData.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
128
129
130
131
132
133
134
135
//
// ZipInputData.m
// KeePass2
//
// Created by Qiang Yu on 2/2/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "GZipInputData.h"
@interface GZipInputData(PrivateMethods)
-(BOOL)readBlock;
@end
@implementation GZipInputData
#pragma mark -
#pragma mark alloc/dealloc
-(id)initWithDataSource:(id<InputDataSource>)zippedSource{
if(self=[super init]){
_eoz = NO;
_stream.avail_in = _stream.avail_out = 0;
_stream.next_in = _stream.next_out = nil;
_stream.zalloc = Z_NULL; _stream.zfree = Z_NULL; _stream.opaque = Z_NULL;
if(inflateInit2(&_stream, (15+32)))
@throw [NSException exceptionWithName:@"AppError" reason:@"InflateInitFailure" userInfo:nil];
_out = [[ByteBuffer alloc] initWithSize:OUT_BLOCK]; _out._size = 0;
_in = [[ByteBuffer alloc] initWithSize:IN_BLOCK]; _in._size = 0;
_zipped = zippedSource;
[_zipped retain];
}
return self;
}
-(void)dealloc{
[_in release];
[_out release];
[_zipped release];
[super dealloc];
}
#pragma mark -
#pragma mark private methods
// read a block of data
// returning YES means _out is OK to read;
// returning NO means _out is no longer OK to read;
-(BOOL)readBlock{
if(_eoz){
_out._size = 0;
return NO;
}
int32_t ret = 0;
_stream.avail_out = OUT_BLOCK;
_stream.next_out = _out._bytes;
do{
if(!_stream.avail_in){
_in._size = [_zipped readBytes:_in._bytes length:IN_BLOCK];
if(!_in._size){
@throw [NSException exceptionWithName:@"InvalidData" reason:@"UnzipError" userInfo:nil];
}
else{
_stream.avail_in = _in._size;
_stream.next_in = _in._bytes;
}
}
ret = inflate(&_stream, Z_NO_FLUSH);
if(ret){
inflateEnd(&_stream);
if(ret!=Z_STREAM_END)
@throw [NSException exceptionWithName:@"InvalidData" reason:@"UnzipError" userInfo:nil];
else{
_eoz = YES;
break;
}
}
}while(_stream.avail_out);
if(_eoz){
_out._size = OUT_BLOCK - _stream.avail_out;
}else{
_out._size = OUT_BLOCK;
}
return YES;
}
#pragma mark -
#pragma mark InputDataSource interface
-(NSUInteger)readBytes:(void *)buffer length:(NSUInteger)length{
NSUInteger remaining = length; //number of remaining bytes to read
NSUInteger bufferOffset = 0;
while (remaining>0) {
if(_outOffset == _out._size){
_outOffset = 0;
if(![self readBlock]){
return length - remaining;
}
}
NSUInteger bytesToCopy = MIN(remaining, _out._size - _outOffset);
memcpy(buffer+bufferOffset, _out._bytes+_outOffset, bytesToCopy);
_outOffset+=bytesToCopy; bufferOffset+=bytesToCopy;remaining -= bytesToCopy;
}
return length;
}
-(NSUInteger)lengthOfRemainingReadbleBytes{
@throw [NSException exceptionWithName:@"UnsupportedMethod" reason:@"UnsupportedMethod" userInfo:nil];
}
-(NSUInteger)setReadOffset:(NSUInteger) offset{
@throw [NSException exceptionWithName:@"UnsupportedMethod" reason:@"UnsupportedMethod" userInfo:nil];
}
-(NSUInteger)moveReadOffset:(NSInteger) offset{
@throw [NSException exceptionWithName:@"UnsupportedMethod" reason:@"UnsupportedMethod" userInfo:nil];
}
@end