Author: bonefish Date: 2009-11-25 16:00:40 +0100 (Wed, 25 Nov 2009) New Revision: 34244 Changeset: http://dev.haiku-os.org/changeset/34244/haiku Modified: haiku/trunk/headers/private/storage/ResourceFile.h haiku/trunk/src/kits/storage/ResourceFile.cpp Log: * Style cleanup. * Removed AutoDeleter class and use the shared one instead. Modified: haiku/trunk/headers/private/storage/ResourceFile.h =================================================================== --- haiku/trunk/headers/private/storage/ResourceFile.h 2009-11-25 14:24:52 UTC (rev 34243) +++ haiku/trunk/headers/private/storage/ResourceFile.h 2009-11-25 15:00:40 UTC (rev 34244) @@ -1,41 +1,47 @@ -//---------------------------------------------------------------------- -// This software is part of the OpenBeOS distribution and is covered -// by the OpenBeOS license. -//--------------------------------------------------------------------- +/* + * Copyright 2002-2009, Ingo Weinhold, ingo_weinhold@xxxxxxx + * Distributed under the terms of the MIT License. + */ +#ifndef _RESOURCE_FILE_H +#define _RESOURCE_FILE_H + + /*! \file ResourceFile.h ResourceFile interface declaration. */ -#ifndef _RESOURCE_FILE_H -#define _RESOURCE_FILE_H #include <ByteOrder.h> #include "OffsetFile.h" + struct resource_info; struct PEFContainerHeader; + namespace BPrivate { namespace Storage { + class Exception; struct MemArea; class ResourceItem; struct resource_parse_info; class ResourcesContainer; + /*! \class ResourceFile \brief Represents a file capable of containing resources. - + This class provides access to the resources of a file. Basically a ResourceFile object can be set to a file, load infos for the resources without loading their data (InitContainer()), read the data of one (ReadResource()) or all resources (ReadResources()) and write all resources to the file (WriteResources()). - + Note, that the object does only provide the I/O functionality, it does not store any information about the resources -- this is done via a ResourcesContainer. We gain flexibility using this approach, since e.g. @@ -45,88 +51,90 @@ of resources to the file. \author <a href='mailto:bonefish@xxxxxxxxxxxx'>Ingo Weinhold</a> - + \version 0.0.0 */ class ResourceFile { public: - ResourceFile(); - virtual ~ResourceFile(); + ResourceFile(); + virtual ~ResourceFile(); - status_t SetTo(BFile *file, bool clobber = false); - void Unset(); - status_t InitCheck() const; + status_t SetTo(BFile* file, bool clobber = false); + void Unset(); + status_t InitCheck() const; - status_t InitContainer(ResourcesContainer &container); - status_t ReadResource(ResourceItem &resource, bool force = false); - status_t ReadResources(ResourcesContainer &container, bool force = false); - status_t WriteResources(ResourcesContainer &container); + status_t InitContainer(ResourcesContainer& container); + status_t ReadResource(ResourceItem& resource, + bool force = false); + status_t ReadResources(ResourcesContainer& container, + bool force = false); + status_t WriteResources(ResourcesContainer& container); private: - void _InitFile(BFile &file, bool clobber); - void _InitELFFile(BFile &file); - void _InitPEFFile(BFile &file, const PEFContainerHeader &pefHeader); - void _ReadHeader(resource_parse_info &parseInfo); - void _ReadIndex(resource_parse_info &parseInfo); - bool _ReadIndexEntry(resource_parse_info &parseInfo, int32 index, - uint32 tableOffset, bool peekAhead); - void _ReadInfoTable(resource_parse_info &parseInfo); - bool _ReadInfoTableEnd(const void *data, int32 dataSize); - const void *_ReadResourceInfo(resource_parse_info &parseInfo, - const MemArea &area, - const resource_info *info, type_code type, - bool *readIndices); + void _InitFile(BFile& file, bool clobber); + void _InitELFFile(BFile& file); + void _InitPEFFile(BFile& file, + const PEFContainerHeader& pefHeader); + void _ReadHeader(resource_parse_info& parseInfo); + void _ReadIndex(resource_parse_info& parseInfo); + bool _ReadIndexEntry(resource_parse_info& parseInfo, + int32 index, uint32 tableOffset, + bool peekAhead); + void _ReadInfoTable(resource_parse_info& parseInfo); + bool _ReadInfoTableEnd(const void* data, + int32 dataSize); + const void* _ReadResourceInfo( + resource_parse_info& parseInfo, + const MemArea& area, + const resource_info* info, type_code type, + bool* readIndices); - status_t _WriteResources(ResourcesContainer &container); - status_t _MakeEmptyResourceFile(); + status_t _WriteResources(ResourcesContainer& container); + status_t _MakeEmptyResourceFile(); - inline int16 _GetInt16(int16 value); - inline uint16 _GetUInt16(uint16 value); - inline int32 _GetInt32(int32 value); - inline uint32 _GetUInt32(uint32 value); + inline int16 _GetInt16(int16 value); + inline uint16 _GetUInt16(uint16 value); + inline int32 _GetInt32(int32 value); + inline uint32 _GetUInt32(uint32 value); private: - OffsetFile fFile; - uint32 fFileType; - bool fHostEndianess; - bool fEmptyResources; + OffsetFile fFile; + uint32 fFileType; + bool fHostEndianess; + bool fEmptyResources; }; -// _GetInt16 -inline -int16 + +inline int16 ResourceFile::_GetInt16(int16 value) { - return (fHostEndianess ? value : B_SWAP_INT16(value)); + return fHostEndianess ? value : B_SWAP_INT16(value); } -// _GetUInt16 -inline -uint16 + +inline uint16 ResourceFile::_GetUInt16(uint16 value) { - return (fHostEndianess ? value : B_SWAP_INT16(value)); + return fHostEndianess ? value : B_SWAP_INT16(value); } -// _GetInt32 -inline -int32 + +inline int32 ResourceFile::_GetInt32(int32 value) { - return (fHostEndianess ? value : B_SWAP_INT32(value)); + return fHostEndianess ? value : B_SWAP_INT32(value); } -// _GetUInt32 -inline -uint32 + +inline uint32 ResourceFile::_GetUInt32(uint32 value) { - return (fHostEndianess ? value : B_SWAP_INT32(value)); + return fHostEndianess ? value : B_SWAP_INT32(value); } + }; // namespace Storage }; // namespace BPrivate + #endif // _RESOURCE_FILE_H - - Modified: haiku/trunk/src/kits/storage/ResourceFile.cpp =================================================================== --- haiku/trunk/src/kits/storage/ResourceFile.cpp 2009-11-25 14:24:52 UTC (rev 34243) +++ haiku/trunk/src/kits/storage/ResourceFile.cpp 2009-11-25 15:00:40 UTC (rev 34244) @@ -1,33 +1,36 @@ -//---------------------------------------------------------------------- -// This software is part of the OpenBeOS distribution and is covered -// by the OpenBeOS license. -//--------------------------------------------------------------------- +/* + * Copyright 2002-2009, Ingo Weinhold, ingo_weinhold@xxxxxxx + * Distributed under the terms of the MIT License. + */ + + /*! \file ResourceFile.cpp ResourceFile implementation. */ + #include <ResourceFile.h> #include <algorithm> #include <new> #include <stdio.h> -#include "Elf.h" -#include "Exception.h" -#include "Pef.h" -#include "ResourceItem.h" -#include "ResourcesContainer.h" -#include "ResourcesDefs.h" -//#include "Warnings.h" +#include <AutoDeleter.h> -using std::max; -using std::min; -using std::nothrow; +#include <Elf.h> +#include <Exception.h> +#include <Pef.h> +#include <ResourceItem.h> +#include <ResourcesContainer.h> +#include <ResourcesDefs.h> +//#include <Warnings.h> + namespace BPrivate { namespace Storage { + // ELF defs static const uint32 kMaxELFHeaderSize = sizeof(Elf32_Ehdr) + 32; static const char kELFFileMagic[4] = { 0x7f, 'E', 'L', 'F' }; @@ -36,6 +39,7 @@ static const uint32 kMaxResourceCount = 10000; static const uint32 kELFMaxResourceAlignment = 1024 * 1024 * 10; // 10 MB + // recognized file types (indices into kFileTypeNames) enum { FILE_TYPE_UNKNOWN = 0, @@ -46,7 +50,8 @@ FILE_TYPE_EMPTY = 5, }; -const char *kFileTypeNames[] = { + +const char* kFileTypeNames[] = { "unknown", "x86 resource file", "PPC resource file", @@ -55,19 +60,19 @@ "empty file", }; + // debugging //#define DBG(x) x #define DBG(x) #define OUT printf -// helper functions/classes +// #pragma mark - helper functions/classes -// read_exactly -static -void -read_exactly(BPositionIO &file, off_t position, void *buffer, size_t size, - const char *errorMessage = NULL) + +static void +read_exactly(BPositionIO& file, off_t position, void* buffer, size_t size, + const char* errorMessage = NULL) { ssize_t read = file.ReadAt(position, buffer, size); if (read < 0) @@ -81,11 +86,10 @@ } } -// write_exactly -static -void -write_exactly(BPositionIO &file, off_t position, const void *buffer, - size_t size, const char *errorMessage = NULL) + +static void +write_exactly(BPositionIO& file, off_t position, const void* buffer, + size_t size, const char* errorMessage = NULL) { ssize_t written = file.WriteAt(position, buffer, size); if (written < 0) @@ -99,27 +103,25 @@ } } -// align_value + template<typename TV, typename TA> -static inline -TV -align_value(const TV &value, const TA &alignment) +static inline TV +align_value(const TV& value, const TA& alignment) { return ((value + alignment - 1) / alignment) * alignment; } -// calculate_checksum -static -uint32 -calculate_checksum(const void *data, uint32 size) + +static uint32 +calculate_checksum(const void* data, uint32 size) { uint32 checkSum = 0; - const uint8 *csData = (const uint8*)data; - const uint8 *dataEnd = csData + size; - const uint8 *current = csData; + const uint8* csData = (const uint8*)data; + const uint8* dataEnd = csData + size; + const uint8* current = csData; for (; current < dataEnd; current += 4) { uint32 word = 0; - int32 bytes = min(4L, (int32)(dataEnd - current)); + int32 bytes = std::min((int32)4, (int32)(dataEnd - current)); for (int32 i = 0; i < bytes; i++) word = (word << 8) + current[i]; checkSum += word; @@ -127,57 +129,51 @@ return checkSum; } -// skip_bytes -static inline -const void* -skip_bytes(const void *buffer, int32 offset) + +static inline const void* +skip_bytes(const void* buffer, int32 offset) { return (const char*)buffer + offset; } -// skip_bytes -static inline -void* -skip_bytes(void *buffer, int32 offset) + +static inline void* +skip_bytes(void* buffer, int32 offset) { return (char*)buffer + offset; } -// fill_pattern -static -void -fill_pattern(uint32 byteOffset, void *_buffer, uint32 count) + +static void +fill_pattern(uint32 byteOffset, void* _buffer, uint32 count) { - uint32 *buffer = (uint32*)_buffer; + uint32* buffer = (uint32*)_buffer; for (uint32 i = 0; i < count; i++) buffer[i] = kUnusedResourceDataPattern[(byteOffset / 4 + i) % 3]; } -// fill_pattern -static -void -fill_pattern(const void *dataBegin, void *buffer, uint32 count) + +static void +fill_pattern(const void* dataBegin, void* buffer, uint32 count) { fill_pattern((char*)buffer - (const char*)dataBegin, buffer, count); } -// fill_pattern -static -void -fill_pattern(const void *dataBegin, void *buffer, const void *bufferEnd) + +static void +fill_pattern(const void* dataBegin, void* buffer, const void* bufferEnd) { fill_pattern(dataBegin, buffer, ((const char*)bufferEnd - (char*)buffer) / 4); } -// check_pattern -static -bool -check_pattern(uint32 byteOffset, void *_buffer, uint32 count, - bool hostEndianess) + +static bool +check_pattern(uint32 byteOffset, void* _buffer, uint32 count, + bool hostEndianess) { bool result = true; - uint32 *buffer = (uint32*)_buffer; + uint32* buffer = (uint32*)_buffer; for (uint32 i = 0; result && i < count; i++) { uint32 value = buffer[i]; if (!hostEndianess) @@ -188,14 +184,17 @@ return result; } -// MemArea + +// #pragma mark - + + struct MemArea { - MemArea(const void *data, uint32 size) : data(data), size(size) {} + MemArea(const void* data, uint32 size) : data(data), size(size) {} - inline bool check(const void *_current, uint32 skip = 0) const + inline bool check(const void* _current, uint32 skip = 0) const { - const char *start = (const char*)data; - const char *current = (const char*)_current; + const char* start = (const char*)data; + const char* current = (const char*)_current; return (start <= current && start + size >= current + skip); } @@ -203,54 +202,38 @@ uint32 size; }; -// AutoDeleter -template<typename C> -struct AutoDeleter { - AutoDeleter(C *object, bool array = false) : object(object), array(array) - { - } - ~AutoDeleter() - { - if (array) - delete[] object; - else - delete object; - } - - C* object; - bool array; -}; - -// resource_parse_info struct resource_parse_info { off_t file_size; int32 resource_count; - ResourcesContainer *container; - char *info_table; + ResourcesContainer* container; + char* info_table; uint32 info_table_offset; uint32 info_table_size; }; -// constructor +// #pragma mark - + + ResourceFile::ResourceFile() - : fFile(), - fFileType(FILE_TYPE_UNKNOWN), - fHostEndianess(true), - fEmptyResources(true) + : + fFile(), + fFileType(FILE_TYPE_UNKNOWN), + fHostEndianess(true), + fEmptyResources(true) { } -// destructor + ResourceFile::~ResourceFile() { Unset(); } -// SetTo + status_t -ResourceFile::SetTo(BFile *file, bool clobber) +ResourceFile::SetTo(BFile* file, bool clobber) { status_t error = (file ? B_OK : B_BAD_VALUE); Unset(); @@ -268,27 +251,26 @@ return error; } -// Unset + void ResourceFile::Unset() { - // file fFile.Unset(); fFileType = FILE_TYPE_UNKNOWN; fHostEndianess = true; fEmptyResources = true; } -// InitCheck + status_t ResourceFile::InitCheck() const { return fFile.InitCheck(); } -// InitContainer + status_t -ResourceFile::InitContainer(ResourcesContainer &container) +ResourceFile::InitContainer(ResourcesContainer& container) { container.MakeEmpty(); status_t error = InitCheck(); @@ -320,16 +302,16 @@ return error; } -// ReadResource + status_t -ResourceFile::ReadResource(ResourceItem &resource, bool force) +ResourceFile::ReadResource(ResourceItem& resource, bool force) { status_t error = InitCheck(); size_t size = resource.DataSize(); if (error == B_OK && (force || !resource.IsLoaded())) { if (error == B_OK) error = resource.SetSize(size); - void *data = NULL; + void* data = NULL; if (error == B_OK) { data = resource.Data(); ssize_t bytesRead = fFile.ReadAt(resource.Offset(), data, size); @@ -349,14 +331,14 @@ return error; } -// ReadResources + status_t -ResourceFile::ReadResources(ResourcesContainer &container, bool force) +ResourceFile::ReadResources(ResourcesContainer& container, bool force) { status_t error = InitCheck(); int32 count = container.CountResources(); for (int32 i = 0; error == B_OK && i < count; i++) { - if (ResourceItem *resource = container.ResourceAt(i)) + if (ResourceItem* resource = container.ResourceAt(i)) error = ReadResource(*resource, force); else error = B_ERROR; @@ -364,9 +346,9 @@ return error; } -// WriteResources + status_t -ResourceFile::WriteResources(ResourcesContainer &container) +ResourceFile::WriteResources(ResourcesContainer& container) { status_t error = InitCheck(); if (error == B_OK && !fFile.File()->IsWritable()) @@ -381,9 +363,8 @@ } -// _InitFile void -ResourceFile::_InitFile(BFile &file, bool clobber) +ResourceFile::_InitFile(BFile& file, bool clobber) { status_t error = B_OK; fFile.Unset(); @@ -460,9 +441,9 @@ } } -// _InitELFFile + void -ResourceFile::_InitELFFile(BFile &file) +ResourceFile::_InitELFFile(BFile& file) { status_t error = B_OK; // get the file size @@ -501,8 +482,7 @@ // ELF header size if (headerSize < sizeof(Elf32_Ehdr) || headerSize > kMaxELFHeaderSize) { throw Exception(B_IO_ERROR, - "Invalid ELF header: invalid ELF header size: %lu.", - headerSize); + "Invalid ELF header: invalid ELF header size: %lu.", headerSize); } uint32 resourceOffset = headerSize; uint32 resourceAlignment = 0; @@ -512,24 +492,23 @@ if (programHeaderTableOffset < headerSize || programHeaderTableOffset > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF header: invalid program " - "header table offset: %lu.", - programHeaderTableOffset); + "header table offset: %lu.", programHeaderTableOffset); } programHeaderTableSize = programHeaderSize * programHeaderCount; if (programHeaderSize < sizeof(Elf32_Phdr) || programHeaderTableOffset + programHeaderTableSize > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF header: program header " - "table exceeds file: %lu.", - programHeaderTableOffset + programHeaderTableSize); + "table exceeds file: %lu.", + programHeaderTableOffset + programHeaderTableSize); } - resourceOffset = max(resourceOffset, programHeaderTableOffset - + programHeaderTableSize); + resourceOffset = std::max(resourceOffset, + programHeaderTableOffset + programHeaderTableSize); // iterate through the program headers for (int32 i = 0; i < (int32)programHeaderCount; i++) { uint32 shOffset = programHeaderTableOffset + i * programHeaderSize; Elf32_Phdr programHeader; read_exactly(file, shOffset, &programHeader, sizeof(Elf32_Shdr), - "Failed to read ELF program header."); + "Failed to read ELF program header."); // get the header values uint32 type = _GetUInt32(programHeader.p_type); uint32 offset = _GetUInt32(programHeader.p_offset); @@ -540,15 +519,15 @@ if (type != PT_NULL) { if (/*offset < headerSize ||*/ offset > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF program header: " - "invalid program offset: %lu.", offset); + "invalid program offset: %lu.", offset); } uint32 segmentEnd = offset + size; if (segmentEnd > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF section header: " - "segment exceeds file: %lu.", segmentEnd); + "segment exceeds file: %lu.", segmentEnd); } - resourceOffset = max(resourceOffset, segmentEnd); - resourceAlignment = max(resourceAlignment, alignment); + resourceOffset = std::max(resourceOffset, segmentEnd); + resourceAlignment = std::max(resourceAlignment, alignment); } } } @@ -558,24 +537,23 @@ if (sectionHeaderTableOffset < headerSize || sectionHeaderTableOffset > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF header: invalid section " - "header table offset: %lu.", - sectionHeaderTableOffset); + "header table offset: %lu.", sectionHeaderTableOffset); } sectionHeaderTableSize = sectionHeaderSize * sectionHeaderCount; if (sectionHeaderSize < sizeof(Elf32_Shdr) || sectionHeaderTableOffset + sectionHeaderTableSize > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF header: section header " - "table exceeds file: %lu.", - sectionHeaderTableOffset + sectionHeaderTableSize); + "table exceeds file: %lu.", + sectionHeaderTableOffset + sectionHeaderTableSize); } - resourceOffset = max(resourceOffset, sectionHeaderTableOffset - + sectionHeaderTableSize); + resourceOffset = std::max(resourceOffset, + sectionHeaderTableOffset + sectionHeaderTableSize); // iterate through the section headers for (int32 i = 0; i < (int32)sectionHeaderCount; i++) { uint32 shOffset = sectionHeaderTableOffset + i * sectionHeaderSize; Elf32_Shdr sectionHeader; read_exactly(file, shOffset, §ionHeader, sizeof(Elf32_Shdr), - "Failed to read ELF section header."); + "Failed to read ELF section header."); // get the header values uint32 type = _GetUInt32(sectionHeader.sh_type); uint32 offset = _GetUInt32(sectionHeader.sh_offset); @@ -586,14 +564,14 @@ if (type != SHT_NULL && type != SHT_NOBITS) { if (offset < headerSize || offset > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF section header: " - "invalid section offset: %lu.", offset); + "invalid section offset: %lu.", offset); } uint32 sectionEnd = offset + size; if (sectionEnd > fileSize) { throw Exception(B_IO_ERROR, "Invalid ELF section header: " - "section exceeds file: %lu.", sectionEnd); + "section exceeds file: %lu.", sectionEnd); } - resourceOffset = max(resourceOffset, sectionEnd); + resourceOffset = std::max(resourceOffset, sectionEnd); } } } @@ -602,7 +580,7 @@ resourceAlignment = kELFMinResourceAlignment; if (resourceAlignment > kELFMaxResourceAlignment) { throw Exception(B_IO_ERROR, "The ELF object file requires an invalid " - "alignment: %lu.", resourceAlignment); + "alignment: %lu.", resourceAlignment); } resourceOffset = align_value(resourceOffset, resourceAlignment); if (resourceOffset >= fileSize) { @@ -614,9 +592,9 @@ fFile.SetTo(&file, resourceOffset); } -// _InitPEFFile + void -ResourceFile::_InitPEFFile(BFile &file, const PEFContainerHeader &pefHeader) +ResourceFile::_InitPEFFile(BFile& file, const PEFContainerHeader& pefHeader) { status_t error = B_OK; // get the file size @@ -639,21 +617,21 @@ uint32 shOffset = sectionHeaderTableOffset + i * kPEFSectionHeaderSize; PEFSectionHeader sectionHeader; read_exactly(file, shOffset, §ionHeader, kPEFSectionHeaderSize, - "Failed to read PEF section header."); + "Failed to read PEF section header."); // get the header values uint32 offset = _GetUInt32(sectionHeader.containerOffset); uint32 size = _GetUInt32(sectionHeader.packedSize); // check the values if (offset < sectionHeaderTableEnd || offset > fileSize) { throw Exception(B_IO_ERROR, "Invalid PEF section header: invalid " - "section offset: %lu.", offset); + "section offset: %lu.", offset); } uint32 sectionEnd = offset + size; if (sectionEnd > fileSize) { throw Exception(B_IO_ERROR, "Invalid PEF section header: section " - "exceeds file: %lu.", sectionEnd); + "exceeds file: %lu.", sectionEnd); } - resourceOffset = max(resourceOffset, sectionEnd); + resourceOffset = std::max(resourceOffset, sectionEnd); } if (resourceOffset >= fileSize) { // throw Exception("The PEF object file does not contain resources."); @@ -664,21 +642,21 @@ fFile.SetTo(&file, resourceOffset); } -// _ReadHeader + void -ResourceFile::_ReadHeader(resource_parse_info &parseInfo) +ResourceFile::_ReadHeader(resource_parse_info& parseInfo) { // read the header resources_header header; read_exactly(fFile, 0, &header, kResourcesHeaderSize, - "Failed to read the header."); + "Failed to read the header."); // check the header // magic uint32 magic = _GetUInt32(header.rh_resources_magic); if (magic == kResourcesHeaderMagic) { // everything is fine } else if (B_SWAP_INT32(magic) == kResourcesHeaderMagic) { -// const char *endianessStr[2] = { "little", "big" }; +// const char* endianessStr[2] = { "little", "big" }; // int32 endianess // = (fHostEndianess == ((bool)B_HOST_IS_LENDIAN ? 0 : 1)); // Warnings::AddCurrentWarning("Endianess seems to be %s, although %s " @@ -696,8 +674,8 @@ uint32 indexSectionOffset = _GetUInt32(header.rh_index_section_offset); if (indexSectionOffset != kResourceIndexSectionOffset) { throw Exception(B_IO_ERROR, "Unexpected resource index section " - "offset. Is: %lu, should be: %lu.", indexSectionOffset, - kResourceIndexSectionOffset); + "offset. Is: %lu, should be: %lu.", indexSectionOffset, + kResourceIndexSectionOffset); } // admin section size uint32 indexSectionSize = kResourceIndexSectionHeaderSize @@ -707,31 +685,31 @@ uint32 adminSectionSize = _GetUInt32(header.rh_admin_section_size); if (adminSectionSize != indexSectionOffset + indexSectionSize) { throw Exception(B_IO_ERROR, "Unexpected resource admin section size. " - "Is: %lu, should be: %lu.", adminSectionSize, - indexSectionOffset + indexSectionSize); + "Is: %lu, should be: %lu.", adminSectionSize, + indexSectionOffset + indexSectionSize); } // set the resource count parseInfo.resource_count = resourceCount; } -// _ReadIndex + void -ResourceFile::_ReadIndex(resource_parse_info &parseInfo) +ResourceFile::_ReadIndex(resource_parse_info& parseInfo) { - int32 &resourceCount = parseInfo.resource_count; - off_t &fileSize = parseInfo.file_size; + int32& resourceCount = parseInfo.resource_count; + off_t& fileSize = parseInfo.file_size; // read the header resource_index_section_header header; read_exactly(fFile, kResourceIndexSectionOffset, &header, - kResourceIndexSectionHeaderSize, - "Failed to read the resource index section header."); + kResourceIndexSectionHeaderSize, + "Failed to read the resource index section header."); // check the header // index section offset uint32 indexSectionOffset = _GetUInt32(header.rish_index_section_offset); if (indexSectionOffset != kResourceIndexSectionOffset) { throw Exception(B_IO_ERROR, "Unexpected resource index section " - "offset. Is: %lu, should be: %lu.", indexSectionOffset, - kResourceIndexSectionOffset); + "offset. Is: %lu, should be: %lu.", indexSectionOffset, + kResourceIndexSectionOffset); } // index section size uint32 expectedIndexSectionSize = kResourceIndexSectionHeaderSize @@ -741,23 +719,23 @@ uint32 indexSectionSize = _GetUInt32(header.rish_index_section_size); if (indexSectionSize != expectedIndexSectionSize) { throw Exception(B_IO_ERROR, "Unexpected resource index section size. " - "Is: %lu, should be: %lu.", indexSectionSize, - expectedIndexSectionSize); + "Is: %lu, should be: %lu.", indexSectionSize, + expectedIndexSectionSize); } // unknown section offset uint32 unknownSectionOffset = _GetUInt32(header.rish_unknown_section_offset); if (unknownSectionOffset != indexSectionOffset + indexSectionSize) { throw Exception(B_IO_ERROR, "Unexpected resource index section size. " - "Is: %lu, should be: %lu.", unknownSectionOffset, - indexSectionOffset + indexSectionSize); + "Is: %lu, should be: %lu.", unknownSectionOffset, + indexSectionOffset + indexSectionSize); } // unknown section size uint32 unknownSectionSize = _GetUInt32(header.rish_unknown_section_size); if (unknownSectionSize != kUnknownResourceSectionSize) { throw Exception(B_IO_ERROR, "Unexpected resource index section " - "offset. Is: %lu, should be: %lu.", - unknownSectionOffset, kUnknownResourceSectionSize); + "offset. Is: %lu, should be: %lu.", + unknownSectionOffset, kUnknownResourceSectionSize); } // info table offset and size uint32 infoTableOffset = _GetUInt32(header.rish_info_table_offset); @@ -768,15 +746,15 @@ parseInfo.info_table_size = infoTableSize; // read the index entries uint32 indexTableOffset = indexSectionOffset - + kResourceIndexSectionHeaderSize; + + kResourceIndexSectionHeaderSize; int32 maxResourceCount = (unknownSectionOffset - indexTableOffset) - / kResourceIndexEntrySize; + / kResourceIndexEntrySize; int32 actualResourceCount = 0; bool tableEndReached = false; for (int32 i = 0; !tableEndReached && i < maxResourceCount; i++) { // read one entry tableEndReached = !_ReadIndexEntry(parseInfo, i, indexTableOffset, - (i >= resourceCount)); + (i >= resourceCount)); if (!tableEndReached) actualResourceCount++; } @@ -792,22 +770,22 @@ } } -// _ReadIndexEntry + bool -ResourceFile::_ReadIndexEntry(resource_parse_info &parseInfo, int32 index, - uint32 tableOffset, bool peekAhead) +ResourceFile::_ReadIndexEntry(resource_parse_info& parseInfo, int32 index, + uint32 tableOffset, bool peekAhead) { - off_t &fileSize = parseInfo.file_size; + off_t& fileSize = parseInfo.file_size; // bool result = true; resource_index_entry entry; // read one entry off_t entryOffset = tableOffset + index * kResourceIndexEntrySize; read_exactly(fFile, entryOffset, &entry, kResourceIndexEntrySize, - "Failed to read a resource index entry."); + "Failed to read a resource index entry."); // check, if the end is reached early if (result && check_pattern(entryOffset, &entry, - kResourceIndexEntrySize / 4, fHostEndianess)) { + kResourceIndexEntrySize / 4, fHostEndianess)) { if (!peekAhead) { // Warnings::AddCurrentWarning("Unexpected end of resource index " // "table at index: %ld (/%ld).", @@ -824,14 +802,14 @@ // "table."); } else { throw Exception(B_IO_ERROR, "Invalid resource index entry: index: " - "%ld, offset: %lu (%lx), size: %lu (%lx).", - index + 1, offset, offset, size, size); + "%ld, offset: %lu (%lx), size: %lu (%lx).", index + 1, offset, + offset, size, size); } result = false; } // add the entry if (result) { - ResourceItem *item = new(nothrow) ResourceItem; + ResourceItem* item = new(std::nothrow) ResourceItem; if (!item) throw Exception(B_NO_MEMORY); item->SetLocation(offset, size); @@ -843,30 +821,30 @@ return result; } -// _ReadInfoTable + void -ResourceFile::_ReadInfoTable(resource_parse_info &parseInfo) +ResourceFile::_ReadInfoTable(resource_parse_info& parseInfo) { - int32 &resourceCount = parseInfo.resource_count; + int32& resourceCount = parseInfo.resource_count; // read the info table // alloc memory for the table - char *tableData = new(nothrow) char[parseInfo.info_table_size]; + char* tableData = new(std::nothrow) char[parseInfo.info_table_size]; if (!tableData) throw Exception(B_NO_MEMORY); int32 dataSize = parseInfo.info_table_size; parseInfo.info_table = tableData; // freed by the info owner read_exactly(fFile, parseInfo.info_table_offset, tableData, dataSize, - "Failed to read resource info table."); + "Failed to read resource info table."); // - bool *readIndices = new(nothrow) bool[resourceCount + 1]; + bool* readIndices = new(std::nothrow) bool[resourceCount + 1]; // + 1 => always > 0 if (!readIndices) throw Exception(B_NO_MEMORY); - AutoDeleter<bool> readIndicesDeleter(readIndices, true); + ArrayDeleter<bool> readIndicesDeleter(readIndices); for (int32 i = 0; i < resourceCount; i++) readIndices[i] = false; MemArea area(tableData, dataSize); - const void *data = tableData; + const void* data = tableData; // check the table end/check sum [... truncated: 395 lines follow ...]