|
| 1 | +// |
| 2 | +// TMGZIPEncodedRequestBody.m |
| 3 | +// TMTumblrSDK |
| 4 | +// |
| 5 | +// Created by Michael Benedict on 2/9/18. |
| 6 | +// |
| 7 | + |
| 8 | +#import "TMRequestBody.h" |
| 9 | +#import "TMGZIPEncodedRequestBody.h" |
| 10 | +#include <zlib.h> |
| 11 | +#define CHUNKSIZE (1024*4) |
| 12 | + |
| 13 | +@interface TMGZIPEncodedRequestBody () |
| 14 | + |
| 15 | +@property (nonatomic, nonnull, readonly) id<TMRequestBody> originalBody; |
| 16 | +@property (nonatomic, nonnull, readonly) NSData *compressedBodyData; |
| 17 | + |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation TMGZIPEncodedRequestBody |
| 21 | + |
| 22 | +- (nonnull instancetype)initWithRequestBody:(nonnull id<TMRequestBody>)body { |
| 23 | + NSParameterAssert(body); |
| 24 | + self = [super init]; |
| 25 | + |
| 26 | + if (self) { |
| 27 | + _originalBody = body; |
| 28 | + _compressedBodyData = [self compressedBody:[body bodyData]]; |
| 29 | + } |
| 30 | + |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +#pragma mark - TMRequestBody |
| 35 | + |
| 36 | +- (nullable NSString *)contentType { |
| 37 | + return [_originalBody contentType]; |
| 38 | +} |
| 39 | + |
| 40 | +- (nullable NSString *)contentEncoding { |
| 41 | + if (!_compressedBodyData ) { |
| 42 | + return nil; |
| 43 | + } |
| 44 | + else { |
| 45 | + return @"gzip"; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +- (nullable NSData *)bodyData { |
| 51 | + if (!_compressedBodyData) { |
| 52 | + return [_originalBody bodyData]; |
| 53 | + } |
| 54 | + else { |
| 55 | + return _compressedBodyData; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +- (nonnull NSDictionary *)parameters { |
| 60 | + return [_originalBody parameters]; |
| 61 | +} |
| 62 | + |
| 63 | +- (BOOL)encodeParameters { |
| 64 | + return [_originalBody encodeParameters]; |
| 65 | +} |
| 66 | + |
| 67 | +- (NSData *)compressedBody:(NSData *)data { |
| 68 | + z_stream strm; |
| 69 | + strm.zalloc = Z_NULL; |
| 70 | + strm.zfree = Z_NULL; |
| 71 | + strm.opaque = Z_NULL; |
| 72 | + strm.total_out = 0; |
| 73 | + strm.next_in=(Bytef *)[data bytes]; |
| 74 | + strm.avail_in = (unsigned int)[data length]; |
| 75 | + |
| 76 | + /* windowBits (15+16) - Base two logarithm of the maximum window size (the size of the history buffer). It should be in the range 8..15. |
| 77 | + Add 16 to windowBits to write a simple gzip header and trailer around the compressed data instead of a zlib wrapper. The gzip header |
| 78 | + will have no file name, no extra data, no comment, no modification time (set to zero), no header crc, and the |
| 79 | + operating system will be set to 255 (unknown). |
| 80 | + |
| 81 | + 15+16 - will give gzip compatible output |
| 82 | + |
| 83 | + memLevel - The memLevel parameter specifies how much memory should be allocated for the internal compression state. |
| 84 | + memLevel=1 uses minimum memory but is slow and reduces compression ratio; memLevel=9 uses maximum memory for |
| 85 | + optimal speed. The default value is 8. See zconf.h for total memory usage as a function of windowBits and memLevel. |
| 86 | + */ |
| 87 | + |
| 88 | + if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) == Z_OK) { |
| 89 | + |
| 90 | + // 16K chunks for expansion |
| 91 | + NSMutableData *compressed = [[NSMutableData alloc] initWithLength:CHUNKSIZE]; |
| 92 | + |
| 93 | + do { |
| 94 | + if (strm.total_out >= [compressed length]) { |
| 95 | + [compressed increaseLengthBy:CHUNKSIZE]; |
| 96 | + } |
| 97 | + |
| 98 | + strm.next_out = [compressed mutableBytes] + strm.total_out; |
| 99 | + strm.avail_out = (unsigned int)([compressed length] - strm.total_out); |
| 100 | + |
| 101 | + if (Z_STREAM_ERROR == deflate(&strm, Z_FINISH)) { |
| 102 | + return nil; |
| 103 | + } |
| 104 | + |
| 105 | + } while (strm.avail_out == 0); |
| 106 | + |
| 107 | + if (deflateEnd(&strm) != Z_OK) { |
| 108 | + return nil; |
| 109 | + } |
| 110 | + |
| 111 | + [compressed setLength: strm.total_out]; |
| 112 | + |
| 113 | + return compressed; |
| 114 | + } |
| 115 | + else { |
| 116 | + return nil; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +@end |
0 commit comments