-
Notifications
You must be signed in to change notification settings - Fork 230
/
GTMNSData+zlib.m
429 lines (374 loc) · 16.1 KB
/
GTMNSData+zlib.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//
// GTMNSData+zlib.m
//
// Copyright 2007-2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
#import "GTMNSData+zlib.h"
#import <zlib.h>
#import "GTMDefines.h"
#define kChunkSize 1024
NSString *const GTMNSDataZlibErrorDomain = @"com.google.GTMNSDataZlibErrorDomain";
NSString *const GTMNSDataZlibErrorKey = @"GTMNSDataZlibErrorKey";
NSString *const GTMNSDataZlibRemainingBytesKey = @"GTMNSDataZlibRemainingBytesKey";
typedef enum {
CompressionModeZlib,
CompressionModeGzip,
CompressionModeRaw,
} CompressionMode;
@interface NSData (GTMZlibAdditionsPrivate)
+ (NSData *)gtm_dataByCompressingBytes:(const void *)bytes
length:(NSUInteger)length
compressionLevel:(int)level
mode:(CompressionMode)mode
error:(NSError **)error;
+ (NSData *)gtm_dataByInflatingBytes:(const void *)bytes
length:(NSUInteger)length
isRawData:(BOOL)isRawData
error:(NSError **)error;
@end
@implementation NSData (GTMZlibAdditionsPrivate)
+ (NSData *)gtm_dataByCompressingBytes:(const void *)bytes
length:(NSUInteger)length
compressionLevel:(int)level
mode:(CompressionMode)mode
error:(NSError **)error {
if (!bytes || !length) {
return nil;
}
#if defined(__LP64__) && __LP64__
// Don't support > 32bit length for 64 bit, see note in header.
if (length > UINT_MAX) {
if (error) {
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorGreaterThan32BitsToCompress
userInfo:nil];
}
return nil;
}
#endif
if (level == Z_DEFAULT_COMPRESSION) {
// the default value is actually outside the range, so we have to let it
// through specifically.
} else if (level < Z_BEST_SPEED) {
level = Z_BEST_SPEED;
} else if (level > Z_BEST_COMPRESSION) {
level = Z_BEST_COMPRESSION;
}
z_stream strm;
bzero(&strm, sizeof(z_stream));
int memLevel = 8; // the default
int windowBits = 15; // the default
switch (mode) {
case CompressionModeZlib:
// nothing to do
break;
case CompressionModeGzip:
windowBits += 16; // enable gzip header instead of zlib header
break;
case CompressionModeRaw:
windowBits *= -1; // Negative to mean no header.
break;
}
int retCode;
if ((retCode = deflateInit2(&strm, level, Z_DEFLATED, windowBits,
memLevel, Z_DEFAULT_STRATEGY)) != Z_OK) {
// COV_NF_START - no real way to force this in a unittest (we guard all args)
if (error) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
forKey:GTMNSDataZlibErrorKey];
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorInternal
userInfo:userInfo];
}
return nil;
// COV_NF_END
}
// hint the size at 1/4 the input size
NSMutableData *result = [NSMutableData dataWithCapacity:(length/4)];
unsigned char output[kChunkSize];
// setup the input
strm.avail_in = (unsigned int)length;
strm.next_in = (unsigned char*)bytes;
// loop to collect the data
do {
// update what we're passing in
strm.avail_out = kChunkSize;
strm.next_out = output;
retCode = deflate(&strm, Z_FINISH);
if ((retCode != Z_OK) && (retCode != Z_STREAM_END)) {
// COV_NF_START - no real way to force this in a unittest
// (in inflate, we can feed bogus/truncated data to test, but an error
// here would be some internal issue w/in zlib, and there isn't any real
// way to test it)
if (error) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
forKey:GTMNSDataZlibErrorKey];
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorInternal
userInfo:userInfo];
}
deflateEnd(&strm);
return nil;
// COV_NF_END
}
// collect what we got
unsigned gotBack = kChunkSize - strm.avail_out;
if (gotBack > 0) {
[result appendBytes:output length:gotBack];
}
} while (retCode == Z_OK);
// if the loop exits, we used all input and the stream ended
_GTMDevAssert(strm.avail_in == 0,
@"thought we finished deflate w/o using all input, %u bytes left",
strm.avail_in);
_GTMDevAssert(retCode == Z_STREAM_END,
@"thought we finished deflate w/o getting a result of stream end, code %d",
retCode);
// clean up
deflateEnd(&strm);
return result;
} // gtm_dataByCompressingBytes:length:compressionLevel:useGzip:
+ (NSData *)gtm_dataByInflatingBytes:(const void *)bytes
length:(NSUInteger)length
isRawData:(BOOL)isRawData
error:(NSError **)error {
if (!bytes || !length) {
return nil;
}
#if defined(__LP64__) && __LP64__
// Don't support > 32bit length for 64 bit, see note in header.
if (length > UINT_MAX) {
return nil;
}
#endif
z_stream strm;
bzero(&strm, sizeof(z_stream));
// setup the input
strm.avail_in = (unsigned int)length;
strm.next_in = (unsigned char*)bytes;
int windowBits = 15; // 15 to enable any window size
if (isRawData) {
windowBits *= -1; // make it negative to signal no header.
} else {
windowBits += 32; // and +32 to enable zlib or gzip header detection.
}
int retCode;
if ((retCode = inflateInit2(&strm, windowBits)) != Z_OK) {
// COV_NF_START - no real way to force this in a unittest (we guard all args)
if (error) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
forKey:GTMNSDataZlibErrorKey];
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorInternal
userInfo:userInfo];
}
return nil;
// COV_NF_END
}
// hint the size at 4x the input size
NSMutableData *result = [NSMutableData dataWithCapacity:(length*4)];
unsigned char output[kChunkSize];
// loop to collect the data
do {
// update what we're passing in
strm.avail_out = kChunkSize;
strm.next_out = output;
retCode = inflate(&strm, Z_NO_FLUSH);
if ((retCode != Z_OK) && (retCode != Z_STREAM_END)) {
if (error) {
NSMutableDictionary *userInfo =
[NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:retCode]
forKey:GTMNSDataZlibErrorKey];
if (strm.msg) {
NSString *message = [NSString stringWithUTF8String:strm.msg];
if (message) {
[userInfo setObject:message forKey:NSLocalizedDescriptionKey];
}
}
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorInternal
userInfo:userInfo];
}
inflateEnd(&strm);
return nil;
}
// collect what we got
unsigned gotBack = kChunkSize - strm.avail_out;
if (gotBack > 0) {
[result appendBytes:output length:gotBack];
}
} while (retCode == Z_OK);
// make sure there wasn't more data tacked onto the end of a valid compressed
// stream.
if (strm.avail_in != 0) {
if (error) {
NSDictionary *userInfo =
[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:strm.avail_in]
forKey:GTMNSDataZlibRemainingBytesKey];
*error = [NSError errorWithDomain:GTMNSDataZlibErrorDomain
code:GTMNSDataZlibErrorDataRemaining
userInfo:userInfo];
}
result = nil;
}
// the only way out of the loop was by hitting the end of the stream
_GTMDevAssert(retCode == Z_STREAM_END,
@"thought we finished inflate w/o getting a result of stream end, code %d",
retCode);
// clean up
inflateEnd(&strm);
return result;
} // gtm_dataByInflatingBytes:length:windowBits:
@end
@implementation NSData (GTMZLibAdditions)
+ (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
length:(NSUInteger)length
error:(NSError **)error {
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeGzip
error:error];
} // gtm_dataByGzippingBytes:length:error:
+ (NSData *)gtm_dataByGzippingData:(NSData *)data error:(NSError **)error {
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeGzip
error:error];
} // gtm_dataByGzippingData:error:
+ (NSData *)gtm_dataByGzippingBytes:(const void *)bytes
length:(NSUInteger)length
compressionLevel:(int)level
error:(NSError **)error{
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:level
mode:CompressionModeGzip
error:error];
} // gtm_dataByGzippingBytes:length:level:error
+ (NSData *)gtm_dataByGzippingData:(NSData *)data
compressionLevel:(int)level
error:(NSError **)error{
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:level
mode:CompressionModeGzip
error:error];
} // gtm_dataByGzippingData:level:error
#pragma mark -
+ (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
length:(NSUInteger)length
error:(NSError **)error{
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeZlib
error:error];
} // gtm_dataByDeflatingBytes:length:error
+ (NSData *)gtm_dataByDeflatingData:(NSData *)data error:(NSError **)error {
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeZlib
error:error];
} // gtm_dataByDeflatingData:
+ (NSData *)gtm_dataByDeflatingBytes:(const void *)bytes
length:(NSUInteger)length
compressionLevel:(int)level
error:(NSError **)error {
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:level
mode:CompressionModeZlib
error:error];
} // gtm_dataByDeflatingBytes:length:level:error:
+ (NSData *)gtm_dataByDeflatingData:(NSData *)data
compressionLevel:(int)level
error:(NSError **)error {
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:level
mode:CompressionModeZlib
error:error];
} // gtm_dataByDeflatingData:level:error:
#pragma mark -
+ (NSData *)gtm_dataByInflatingBytes:(const void *)bytes
length:(NSUInteger)length
error:(NSError **)error {
return [self gtm_dataByInflatingBytes:bytes
length:length
isRawData:NO
error:error];
} // gtm_dataByInflatingBytes:length:error:
+ (NSData *)gtm_dataByInflatingData:(NSData *)data
error:(NSError **)error {
return [self gtm_dataByInflatingBytes:[data bytes]
length:[data length]
isRawData:NO
error:error];
} // gtm_dataByInflatingData:
#pragma mark -
+ (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
length:(NSUInteger)length
error:(NSError **)error {
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeRaw
error:error];
} // gtm_dataByRawDeflatingBytes:length:error:
+ (NSData *)gtm_dataByRawDeflatingData:(NSData *)data error:(NSError **)error {
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:Z_DEFAULT_COMPRESSION
mode:CompressionModeRaw
error:error];
} // gtm_dataByRawDeflatingData:error:
+ (NSData *)gtm_dataByRawDeflatingBytes:(const void *)bytes
length:(NSUInteger)length
compressionLevel:(int)level
error:(NSError **)error{
return [self gtm_dataByCompressingBytes:bytes
length:length
compressionLevel:level
mode:CompressionModeRaw
error:error];
} // gtm_dataByRawDeflatingBytes:length:compressionLevel:error:
+ (NSData *)gtm_dataByRawDeflatingData:(NSData *)data
compressionLevel:(int)level
error:(NSError **)error {
return [self gtm_dataByCompressingBytes:[data bytes]
length:[data length]
compressionLevel:level
mode:CompressionModeRaw
error:error];
} // gtm_dataByRawDeflatingData:compressionLevel:error:
+ (NSData *)gtm_dataByRawInflatingBytes:(const void *)bytes
length:(NSUInteger)length
error:(NSError **)error{
return [self gtm_dataByInflatingBytes:bytes
length:length
isRawData:YES
error:error];
} // gtm_dataByRawInflatingBytes:length:error:
+ (NSData *)gtm_dataByRawInflatingData:(NSData *)data
error:(NSError **)error {
return [self gtm_dataByInflatingBytes:[data bytes]
length:[data length]
isRawData:YES
error:error];
} // gtm_dataByRawInflatingData:error:
@end