Artifact 9c5065dcc1933a8b7aa214f7fa241ca0476d9bbd11414e7eede42a3618009646:
- File
src/OFTarArchiveEntry.m
— part of check-in
[7ecdc71677]
at
2016-05-22 19:53:38
on branch trunk
— Add OFTarArchive
This only implements the UNIX v7 format for now. (user: js, size: 3384) [annotate] [blame] [check-ins using]
/* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 * Jonathan Schleifer <js@heap.zone> * * 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. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #define OF_TAR_ARCHIVE_ENTRY_M #include "config.h" #import "OFTarArchiveEntry.h" #import "OFTarArchiveEntry+Private.h" #import "OFStream.h" #import "OFDate.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" static OFString* stringFromBuffer(const char *buffer, size_t length) { for (size_t i = 0; i < length; i++) if (buffer[i] == '\0') length = i; return [OFString stringWithUTF8String: buffer length: length]; } static uintmax_t octalValueFromBuffer(const char *buffer, size_t length, size_t max) { uintmax_t value = [stringFromBuffer(buffer, length) octalValue]; if (value > max) @throw [OFOutOfRangeException exception]; return value; } @implementation OFTarArchiveEntry @synthesize name = _name, mode = _mode, size = _size; @synthesize modificationDate = _modificationDate, type = _type; @synthesize targetName = _targetName; - (instancetype)OF_initWithHeader: (char[512])header stream: (OFStream*)stream { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); _stream = [stream retain]; _name = [stringFromBuffer(header, 100) copy]; _mode = (uint32_t)octalValueFromBuffer( header + 100, 8, UINT32_MAX); _size = _toRead = (size_t)octalValueFromBuffer( header + 124, 12, UINT64_MAX); _modificationDate = [[OFDate alloc] initWithTimeIntervalSince1970: (of_time_interval_t)octalValueFromBuffer( header + 136, 12, UINTMAX_MAX)]; _type = header[156]; _targetName = [stringFromBuffer(header + 157, 100) copy]; if (_type == '\0') _type = OF_TAR_ARCHIVE_ENTRY_TYPE_FILE; objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_stream release]; [_name release]; [_modificationDate release]; [_targetName release]; [super dealloc]; } - (size_t)lowlevelReadIntoBuffer: (void*)buffer length: (size_t)length { size_t ret; if (_atEndOfStream) @throw [OFReadFailedException exceptionWithObject: self requestedLength: length]; if ((uint64_t)length > _toRead) length = (size_t)_toRead; ret = [_stream readIntoBuffer: buffer length: length]; _toRead -= ret; if (ret == 0) _atEndOfStream = true; return ret; } - (bool)lowlevelIsAtEndOfStream { return _atEndOfStream; } - (bool)hasDataInReadBuffer { return ([super hasDataInReadBuffer] || [_stream hasDataInReadBuffer]); } - (void)close { _atEndOfStream = true; } - (void)OF_skip { char buffer[512]; while (_toRead >= 512) { [_stream readIntoBuffer: buffer exactLength: 512]; _toRead -= 512; } if (_toRead > 0) { [_stream readIntoBuffer: buffer exactLength: _toRead]; _toRead = 0; } if (_size % 512 != 0) [_stream readIntoBuffer: buffer exactLength: 512 - (_size % 512)]; } @end