1
2
3
4
5
6
7
8
9
10
11
|
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
* 2018, 2019, 2020
* Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
|
<
<
|
|
1
2
3
4
5
6
7
8
9
|
/*
* Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This file is part of ObjFW. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE.QPL included in
* the packaging of this file.
*
|
︙ | | | ︙ | |
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
|
}
static void
seekOrThrowInvalidFormat(OFSeekableStream *stream,
of_offset_t offset, int whence)
{
@try {
[stream seekToOffset: offset
whence: whence];
} @catch (OFSeekFailedException *e) {
if (e.errNo == EINVAL)
@throw [OFInvalidFormatException exception];
@throw e;
}
}
@implementation OFZIPArchive
@synthesize archiveComment = _archiveComment;
+ (instancetype)archiveWithStream: (OFStream *)stream
mode: (OFString *)mode
{
return [[[self alloc] initWithStream: stream
mode: mode] autorelease];
}
#ifdef OF_HAVE_FILES
+ (instancetype)archiveWithPath: (OFString *)path
mode: (OFString *)mode
{
return [[[self alloc] initWithPath: path
mode: mode] autorelease];
}
#endif
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithStream: (OFStream *)stream
mode: (OFString *)mode
{
self = [super init];
@try {
if ([mode isEqual: @"r"])
_mode = OF_ZIP_ARCHIVE_MODE_READ;
else if ([mode isEqual: @"w"])
|
|
<
|
<
|
<
|
<
|
<
|
<
|
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
|
}
static void
seekOrThrowInvalidFormat(OFSeekableStream *stream,
of_offset_t offset, int whence)
{
@try {
[stream seekToOffset: offset whence: whence];
} @catch (OFSeekFailedException *e) {
if (e.errNo == EINVAL)
@throw [OFInvalidFormatException exception];
@throw e;
}
}
@implementation OFZIPArchive
@synthesize archiveComment = _archiveComment;
+ (instancetype)archiveWithStream: (OFStream *)stream mode: (OFString *)mode
{
return [[[self alloc] initWithStream: stream mode: mode] autorelease];
}
#ifdef OF_HAVE_FILES
+ (instancetype)archiveWithPath: (OFString *)path mode: (OFString *)mode
{
return [[[self alloc] initWithPath: path mode: mode] autorelease];
}
#endif
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithStream: (OFStream *)stream mode: (OFString *)mode
{
self = [super init];
@try {
if ([mode isEqual: @"r"])
_mode = OF_ZIP_ARCHIVE_MODE_READ;
else if ([mode isEqual: @"w"])
|
︙ | | | ︙ | |
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
|
@throw e;
}
return self;
}
#ifdef OF_HAVE_FILES
- (instancetype)initWithPath: (OFString *)path
mode: (OFString *)mode
{
OFFile *file;
if ([mode isEqual: @"a"])
file = [[OFFile alloc] initWithPath: path
mode: @"r+"];
else
file = [[OFFile alloc] initWithPath: path
mode: mode];
@try {
self = [self initWithStream: file
mode: mode];
} @finally {
[file release];
}
return self;
}
#endif
|
|
<
|
<
|
<
|
<
|
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
|
@throw e;
}
return self;
}
#ifdef OF_HAVE_FILES
- (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode
{
OFFile *file;
if ([mode isEqual: @"a"])
file = [[OFFile alloc] initWithPath: path mode: @"r+"];
else
file = [[OFFile alloc] initWithPath: path mode: mode];
@try {
self = [self initWithStream: file mode: mode];
} @finally {
[file release];
}
return self;
}
#endif
|
︙ | | | ︙ | |
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
OFZIPArchiveEntry *entry = [[[OFZIPArchiveEntry alloc]
of_initWithStream: _stream] autorelease];
if ([_pathToEntryMap objectForKey: entry.fileName] != nil)
@throw [OFInvalidFormatException exception];
[_entries addObject: entry];
[_pathToEntryMap setObject: entry
forKey: entry.fileName];
}
objc_autoreleasePoolPop(pool);
}
- (OFArray *)entries
{
|
|
<
|
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
OFZIPArchiveEntry *entry = [[[OFZIPArchiveEntry alloc]
of_initWithStream: _stream] autorelease];
if ([_pathToEntryMap objectForKey: entry.fileName] != nil)
@throw [OFInvalidFormatException exception];
[_entries addObject: entry];
[_pathToEntryMap setObject: entry forKey: entry.fileName];
}
objc_autoreleasePoolPop(pool);
}
- (OFArray *)entries
{
|
︙ | | | ︙ | |
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
|
{
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
return _atEndOfStream;
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer
length: (size_t)length
{
size_t ret;
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
if (_atEndOfStream)
return 0;
if (_stream.atEndOfStream && !_decompressedStream.hasDataInReadBuffer)
@throw [OFTruncatedDataException exception];
#if SIZE_MAX >= UINT64_MAX
if (length > UINT64_MAX)
@throw [OFOutOfRangeException exception];
#endif
if ((uint64_t)length > _toRead)
length = (size_t)_toRead;
ret = [_decompressedStream readIntoBuffer: buffer
length: length];
_toRead -= ret;
_CRC32 = of_crc32(_CRC32, buffer, ret);
if (_toRead == 0) {
_atEndOfStream = true;
|
|
<
|
<
|
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
|
{
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
return _atEndOfStream;
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
{
size_t ret;
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
if (_atEndOfStream)
return 0;
if (_stream.atEndOfStream && !_decompressedStream.hasDataInReadBuffer)
@throw [OFTruncatedDataException exception];
#if SIZE_MAX >= UINT64_MAX
if (length > UINT64_MAX)
@throw [OFOutOfRangeException exception];
#endif
if ((uint64_t)length > _toRead)
length = (size_t)_toRead;
ret = [_decompressedStream readIntoBuffer: buffer length: length];
_toRead -= ret;
_CRC32 = of_crc32(_CRC32, buffer, ret);
if (_toRead == 0) {
_atEndOfStream = true;
|
︙ | | | ︙ | |
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
[self close];
[_entry release];
[super dealloc];
}
- (size_t)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length
{
size_t bytesWritten;
#if SIZE_MAX >= INT64_MAX
if (length > INT64_MAX)
@throw [OFOutOfRangeException exception];
#endif
if (INT64_MAX - _bytesWritten < (int64_t)length)
@throw [OFOutOfRangeException exception];
bytesWritten = [_stream writeBuffer: buffer
length: length];
_bytesWritten += (int64_t)bytesWritten;
_CRC32 = of_crc32(_CRC32, buffer, length);
return bytesWritten;
}
|
|
<
|
<
|
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
|
[self close];
[_entry release];
[super dealloc];
}
- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
{
size_t bytesWritten;
#if SIZE_MAX >= INT64_MAX
if (length > INT64_MAX)
@throw [OFOutOfRangeException exception];
#endif
if (INT64_MAX - _bytesWritten < (int64_t)length)
@throw [OFOutOfRangeException exception];
bytesWritten = [_stream writeBuffer: buffer length: length];
_bytesWritten += (int64_t)bytesWritten;
_CRC32 = of_crc32(_CRC32, buffer, length);
return bytesWritten;
}
|
︙ | | | ︙ | |