-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
GzipDecompressOutputStream.m
129 lines (99 loc) · 3.21 KB
/
GzipDecompressOutputStream.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
//
// GzipDecompressOutputStream.m
// Strongbox
//
// Created by Strongbox on 26/06/2020.
// Copyright © 2014-2021 Mark McGuill. All rights reserved.
//
#import "GzipDecompressOutputStream.h"
#import <zlib.h>
#import "Utils.h"
@interface GzipDecompressOutputStream ()
@property NSOutputStream* outputStream;
@property z_stream* stream;
@property NSError* error;
@end
@implementation GzipDecompressOutputStream
- (instancetype)initToOutputStream:(NSOutputStream *)outputStream {
if (self = [super init]) {
if (outputStream == nil) {
return nil;
}
self.stream = malloc(sizeof(z_stream));
if (self.stream == NULL) {
return nil;
}
memset(self.stream, 0, sizeof(z_stream));
self.stream->zalloc = Z_NULL;
self.stream->zfree = Z_NULL;
self.stream->avail_in = (uInt)0;
self.stream->next_in = (uint8_t*)nil;
self.stream->total_out = 0;
self.stream->avail_out = 0;
if (inflateInit2(self.stream, 47) != Z_OK) {
slog(@"Error initializing z_stream");
free(self.stream);
self.stream = nil;
return nil;
}
self.outputStream = outputStream;
}
return self;
}
- (void)open {
if (self.outputStream) {
[self.outputStream open];
}
}
- (void)close {
if (self.outputStream) {
[self.outputStream close];
self.outputStream = nil;
}
if (self.stream) {
free(self.stream);
self.stream = nil;
}
}
- (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)len {
self.stream->avail_in = (uInt)len;
self.stream->next_in = (uint8_t*)buffer;
uInt remainingIn = self.stream->avail_in;
size_t totalWritten = 0;
uint8_t* decompressed = malloc(len);
while (remainingIn > 0) {
self.stream->next_out = decompressed;
self.stream->avail_out = (uInt)len;
int status = inflate (self.stream, Z_SYNC_FLUSH);
if(status == Z_STREAM_END) {
if (inflateEnd(self.stream) != Z_OK) {
slog(@"ERROR inflateEnd GZIP! %d", status);
self.error = [Utils createNSError:@"ERROR inflateEnd GZIP!." errorCode:status];
free(decompressed);
return -1;
}
}
else if (status != Z_OK) {
slog(@"Error: %d", status);
self.error = [Utils createNSError:[NSString stringWithFormat:@"ERROR Error: %d GZIP!", status] errorCode:status];
free(decompressed);
return -1;
}
size_t writtenThisTime = len - self.stream->avail_out;
if ( writtenThisTime > 0 ) {
NSInteger res = [self.outputStream write:decompressed maxLength:writtenThisTime];
if ( res < 0 ) {
slog(@"GzipDecompressOutputStream: Could not write to output stream.");
return res;
}
}
totalWritten += writtenThisTime;
remainingIn = self.stream->avail_in;
}
free(decompressed);
return totalWritten;
}
- (NSError *)streamError {
return self.error ? self.error : self.outputStream.streamError;
}
@end