︙ | | | ︙ | |
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
|
#import "crc16.h"
#import "OFChecksumMismatchException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFTruncatedDataException.h"
@interface OFLHAArchive_FileReadStream: OFStream <OFReadyForReadingObserving>
{
OF_KINDOF(OFStream *) _stream;
OF_KINDOF(OFStream *) _decompressedStream;
OFLHAArchiveEntry *_entry;
uint32_t _toRead, _bytesConsumed;
uint16_t _CRC16;
bool _atEndOfStream;
}
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
entry: (OFLHAArchiveEntry *)entry;
- (void)of_skip;
@end
@implementation OFLHAArchive
@synthesize encoding = _encoding;
+ (instancetype)archiveWithStream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
{
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
#import "crc16.h"
#import "OFChecksumMismatchException.h"
#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFWriteFailedException.h"
@interface OFLHAArchive_FileReadStream: OFStream <OFReadyForReadingObserving>
{
OF_KINDOF(OFStream *) _stream;
OF_KINDOF(OFStream *) _decompressedStream;
OFLHAArchiveEntry *_entry;
uint32_t _toRead, _bytesConsumed;
uint16_t _CRC16;
bool _atEndOfStream;
}
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
entry: (OFLHAArchiveEntry *)entry;
- (void)of_skip;
@end
@interface OFLHAArchive_FileWriteStream: OFStream <OFReadyForWritingObserving>
{
OFMutableLHAArchiveEntry *_entry;
of_string_encoding_t _encoding;
OF_KINDOF(OFStream *) _stream;
of_offset_t _headerOffset;
uint32_t _bytesWritten;
uint16_t _CRC16;
}
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
entry: (OFLHAArchiveEntry *)entry
encoding: (of_string_encoding_t)encoding;
@end
@implementation OFLHAArchive
@synthesize encoding = _encoding;
+ (instancetype)archiveWithStream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
{
|
︙ | | | ︙ | |
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
- (instancetype)initWithStream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
{
self = [super init];
@try {
if (![mode isEqual: @"r"])
@throw [OFNotImplementedException
exceptionWithSelector: _cmd
object: self];
_stream = [stream retain];
_encoding = OF_STRING_ENCODING_ISO_8859_1;
} @catch (id e) {
[self release];
@throw e;
}
return self;
|
>
>
|
>
>
>
>
>
>
|
|
>
>
>
>
|
>
>
>
<
|
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
|
- (instancetype)initWithStream: (OF_KINDOF(OFStream *))stream
mode: (OFString *)mode
{
self = [super init];
@try {
_stream = [stream retain];
if ([mode isEqual: @"r"])
_mode = OF_LHA_ARCHIVE_MODE_READ;
else if ([mode isEqual: @"w"])
_mode = OF_LHA_ARCHIVE_MODE_WRITE;
else if ([mode isEqual: @"a"])
_mode = OF_LHA_ARCHIVE_MODE_APPEND;
else
@throw [OFInvalidArgumentException exception];
if ((_mode == OF_LHA_ARCHIVE_MODE_WRITE ||
_mode == OF_LHA_ARCHIVE_MODE_APPEND) &&
![_stream isKindOfClass: [OFSeekableStream class]])
@throw [OFInvalidArgumentException exception];
if (_mode == OF_LHA_ARCHIVE_MODE_APPEND)
[_stream seekToOffset: 0
whence: SEEK_END];
_encoding = OF_STRING_ENCODING_ISO_8859_1;
} @catch (id e) {
[self release];
@throw e;
}
return self;
|
︙ | | | ︙ | |
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
[self close];
[super dealloc];
}
- (OFLHAArchiveEntry *)nextEntry
{
char header[21];
size_t headerLen;
[_lastEntry release];
_lastEntry = nil;
[_lastReturnedStream of_skip];
[_lastReturnedStream close];
[_lastReturnedStream release];
_lastReturnedStream = nil;
for (headerLen = 0; headerLen < 21;) {
|
>
|
|
|
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
[self close];
[super dealloc];
}
- (OFLHAArchiveEntry *)nextEntry
{
OFLHAArchiveEntry *entry;
char header[21];
size_t headerLen;
if (_mode != OF_LHA_ARCHIVE_MODE_READ)
@throw [OFInvalidArgumentException exception];
[_lastReturnedStream of_skip];
[_lastReturnedStream close];
[_lastReturnedStream release];
_lastReturnedStream = nil;
for (headerLen = 0; headerLen < 21;) {
|
︙ | | | ︙ | |
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
|
@throw [OFTruncatedDataException exception];
}
headerLen += [_stream readIntoBuffer: header + headerLen
length: 21 - headerLen];
}
_lastEntry = [[OFLHAArchiveEntry alloc]
of_initWithHeader: header
stream: _stream
encoding: _encoding];
_lastReturnedStream = [[OFLHAArchive_FileReadStream alloc]
of_initWithStream: _stream
entry: _lastEntry];
return [[_lastEntry copy] autorelease];
}
- (OFStream <OFReadyForReadingObserving> *)streamForReadingCurrentEntry
{
if (_lastReturnedStream == nil)
@throw [OFInvalidArgumentException exception];
return [[_lastReturnedStream retain] autorelease];
}
- (void)close
{
if (_stream == nil)
return;
[_lastEntry release];
_lastEntry = nil;
[_lastReturnedStream close];
[_lastReturnedStream release];
_lastReturnedStream = nil;
[_stream release];
_stream = nil;
}
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
<
|
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
|
@throw [OFTruncatedDataException exception];
}
headerLen += [_stream readIntoBuffer: header + headerLen
length: 21 - headerLen];
}
entry = [[[OFLHAArchiveEntry alloc]
of_initWithHeader: header
stream: _stream
encoding: _encoding] autorelease];
_lastReturnedStream = [[OFLHAArchive_FileReadStream alloc]
of_initWithStream: _stream
entry: entry];
return entry;
}
- (OFStream <OFReadyForReadingObserving> *)streamForReadingCurrentEntry
{
if (_mode != OF_LHA_ARCHIVE_MODE_READ)
@throw [OFInvalidArgumentException exception];
if (_lastReturnedStream == nil)
@throw [OFInvalidArgumentException exception];
return [[_lastReturnedStream retain] autorelease];
}
- (OFStream <OFReadyForWritingObserving> *)
streamForWritingEntry: (OFLHAArchiveEntry *)entry
{
if (_mode != OF_LHA_ARCHIVE_MODE_WRITE &&
_mode != OF_LHA_ARCHIVE_MODE_APPEND)
@throw [OFInvalidArgumentException exception];
if (![[entry compressionMethod] isEqual: @"-lh0-"])
@throw [OFNotImplementedException exceptionWithSelector: _cmd
object: self];
[_lastReturnedStream close];
[_lastReturnedStream release];
_lastReturnedStream = nil;
_lastReturnedStream = [[OFLHAArchive_FileWriteStream alloc]
of_initWithStream: _stream
entry: entry
encoding: _encoding];
return [[_lastReturnedStream retain] autorelease];
}
- (void)close
{
if (_stream == nil)
return;
[_lastReturnedStream close];
[_lastReturnedStream release];
_lastReturnedStream = nil;
[_stream release];
_stream = nil;
}
|
︙ | | | ︙ | |
356
357
358
359
360
361
362
363
364
365
366
|
[self of_skip];
[_stream release];
_stream = nil;
[_decompressedStream release];
_decompressedStream = nil;
[super close];
}
@end
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
|
[self of_skip];
[_stream release];
_stream = nil;
[_decompressedStream release];
_decompressedStream = nil;
[super close];
}
@end
@implementation OFLHAArchive_FileWriteStream
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
entry: (OFLHAArchiveEntry *)entry
encoding: (of_string_encoding_t)encoding
{
self = [super init];
@try {
_entry = [entry mutableCopy];
_encoding = encoding;
_headerOffset = [stream seekToOffset: 0
whence: SEEK_CUR];
[_entry of_writeToStream: stream
encoding: _encoding];
/*
* Retain stream last, so that -[close] called by -[dealloc]
* doesn't write in case of an error.
*/
_stream = [stream retain];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[self close];
[_entry release];
[super dealloc];
}
- (size_t)lowlevelWriteBuffer: (const void *)buffer
length: (size_t)length
{
uint32_t bytesWritten;
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
if (UINT32_MAX - _bytesWritten < length)
@throw [OFOutOfRangeException exception];
@try {
bytesWritten = (uint32_t)[_stream writeBuffer: buffer
length: length];
} @catch (OFWriteFailedException *e) {
_bytesWritten += [e bytesWritten];
_CRC16 = of_crc16(_CRC16, buffer, [e bytesWritten]);
@throw e;
}
_bytesWritten += (uint32_t)bytesWritten;
_CRC16 = of_crc16(_CRC16, buffer, bytesWritten);
return bytesWritten;
}
- (bool)lowlevelIsAtEndOfStream
{
if (_stream == nil)
@throw [OFNotOpenException exceptionWithObject: self];
return [_stream isAtEndOfStream];
}
- (int)fileDescriptorForWriting
{
return [_stream fileDescriptorForWriting];
}
- (void)close
{
of_offset_t offset;
if (_stream == nil)
return;
[_entry setUncompressedSize: _bytesWritten];
[_entry setCompressedSize: _bytesWritten];
[_entry setCRC16: _CRC16];
offset = [_stream seekToOffset: 0
whence:SEEK_CUR];
[_stream seekToOffset: _headerOffset
whence: SEEK_SET];
[_entry of_writeToStream: _stream
encoding: _encoding];
[_stream seekToOffset: offset
whence: SEEK_SET];
[_stream release];
_stream = nil;
[super close];
}
@end
|