-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZZInflateInputStream.m
154 lines (130 loc) · 2.92 KB
/
ZZInflateInputStream.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// ZZInflateInputStream.m
// ZipZap
//
// Created by Glen Low on 29/09/12.
// Copyright (c) 2012, Pixelglow Software. All rights reserved.
//
#include <zlib.h>
#import "ZZInflateInputStream.h"
static const NSUInteger _bufferLength = 16384; // 16K buffer
@implementation ZZInflateInputStream
{
NSInputStream* _upstream;
NSMutableData* _readBuffer;
NSStreamStatus _status;
NSError* _error;
z_stream _stream;
}
+ (NSData*)decompressData:(NSData*)data
withUncompressedSize:(NSUInteger)uncompressedSize
{
NSMutableData* inflatedData = [NSMutableData dataWithLength:uncompressedSize];
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.next_in = (Bytef*)data.bytes;
stream.avail_in = (uInt)data.length;
stream.next_out = (Bytef*)inflatedData.mutableBytes;
stream.avail_out = (uInt)inflatedData.length;
inflateInit2(&stream, -15);
int result = inflate(&stream, Z_FINISH);
inflateEnd(&stream);
switch (result)
{
case Z_STREAM_END:
return inflatedData;
default:
// TODO: reference some kind of error
return nil;
}
}
- (instancetype)initWithStream:(NSInputStream*)upstream
{
if ((self = [super init]))
{
_upstream = upstream;
_readBuffer = [NSMutableData dataWithLength:_bufferLength];
_status = NSStreamStatusNotOpen;
_error = nil;
_stream.zalloc = Z_NULL;
_stream.zfree = Z_NULL;
_stream.opaque = Z_NULL;
_stream.next_in = Z_NULL;
_stream.avail_in = 0;
}
return self;
}
- (NSStreamStatus)streamStatus
{
return _status;
}
- (NSError*)streamError
{
return _error;
}
- (void)open
{
[_upstream open];
_status = NSStreamStatusOpen;
inflateInit2(&_stream, -15);
}
- (void)close
{
inflateEnd(&_stream);
[_upstream close];
_status = NSStreamStatusClosed;
}
- (NSInteger)read:(uint8_t*)buffer maxLength:(NSUInteger)len
{
// if buffer is empty and stream is still OK, read in up to 16K bytes from upstream
NSInteger bytesRead;
if (_stream.avail_in == 0)
switch (_upstream.streamStatus)
{
case NSStreamStatusOpening:
case NSStreamStatusOpen:
bytesRead = [_upstream read:_readBuffer.mutableBytes maxLength:_bufferLength];
if (bytesRead >= 0)
{
_stream.next_in = (Bytef*)_readBuffer.bytes;
_stream.avail_in = (uInt)bytesRead;
}
else
{
_status = NSStreamStatusError;
_error = _upstream.streamError;
return -1;
}
break;
default:
break;
}
// zlib available bytes limited to 32 bits
if (len > UINT_MAX)
len = UINT_MAX;
// inflate buffer
_stream.next_out = buffer;
_stream.avail_out = (uInt)len;
switch (inflate(&_stream, Z_NO_FLUSH))
{
case Z_STREAM_END:
_status = NSStreamStatusAtEnd;
break;
// TODO: need to handle Z_DATA_ERROR etc.
default:
break;
}
// return how many bytes produced by inflate
return len - _stream.avail_out;
}
- (BOOL)getBuffer:(uint8_t**)buffer length:(NSUInteger*)len
{
return NO;
}
- (BOOL)hasBytesAvailable
{
return YES;
}
@end