Updated the resources support with the unmodified Haiku version.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34251 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* Copyright 2001-2007, Ingo Weinhold, [email protected]. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _AUTO_DELETER_H
|
||||
#define _AUTO_DELETER_H
|
||||
|
||||
/*! Scope-based automatic deletion of objects/arrays.
|
||||
ObjectDeleter - deletes an object
|
||||
ArrayDeleter - deletes an array
|
||||
MemoryDeleter - free()s malloc()ed memory
|
||||
CObjectDeleter - calls an arbitrary specified destructor function
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace BPrivate {
|
||||
|
||||
// AutoDeleter
|
||||
|
||||
template<typename C, typename DeleteFunc>
|
||||
class AutoDeleter {
|
||||
public:
|
||||
inline AutoDeleter()
|
||||
: fObject(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
inline AutoDeleter(C *object)
|
||||
: fObject(object)
|
||||
{
|
||||
}
|
||||
|
||||
inline ~AutoDeleter()
|
||||
{
|
||||
fDelete(fObject);
|
||||
}
|
||||
|
||||
inline void SetTo(C *object)
|
||||
{
|
||||
if (object != fObject) {
|
||||
fDelete(fObject);
|
||||
fObject = object;
|
||||
}
|
||||
}
|
||||
|
||||
inline void Unset()
|
||||
{
|
||||
SetTo(NULL);
|
||||
}
|
||||
|
||||
inline void Delete()
|
||||
{
|
||||
SetTo(NULL);
|
||||
}
|
||||
|
||||
inline C *Get() const
|
||||
{
|
||||
return fObject;
|
||||
}
|
||||
|
||||
inline C *Detach()
|
||||
{
|
||||
C *object = fObject;
|
||||
fObject = NULL;
|
||||
return object;
|
||||
}
|
||||
|
||||
protected:
|
||||
C *fObject;
|
||||
DeleteFunc fDelete;
|
||||
};
|
||||
|
||||
|
||||
// ObjectDeleter
|
||||
|
||||
template<typename C>
|
||||
struct ObjectDelete
|
||||
{
|
||||
inline void operator()(C *object)
|
||||
{
|
||||
delete object;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C>
|
||||
struct ObjectDeleter : AutoDeleter<C, ObjectDelete<C> >
|
||||
{
|
||||
ObjectDeleter() : AutoDeleter<C, ObjectDelete<C> >() {}
|
||||
ObjectDeleter(C *object) : AutoDeleter<C, ObjectDelete<C> >(object) {}
|
||||
};
|
||||
|
||||
|
||||
// ArrayDeleter
|
||||
|
||||
template<typename C>
|
||||
struct ArrayDelete
|
||||
{
|
||||
inline void operator()(C *array)
|
||||
{
|
||||
delete[] array;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename C>
|
||||
struct ArrayDeleter : AutoDeleter<C, ArrayDelete<C> >
|
||||
{
|
||||
ArrayDeleter() : AutoDeleter<C, ArrayDelete<C> >() {}
|
||||
ArrayDeleter(C *array) : AutoDeleter<C, ArrayDelete<C> >(array) {}
|
||||
};
|
||||
|
||||
|
||||
// MemoryDeleter
|
||||
|
||||
struct MemoryDelete
|
||||
{
|
||||
inline void operator()(void *memory)
|
||||
{
|
||||
free(memory);
|
||||
}
|
||||
};
|
||||
|
||||
struct MemoryDeleter : AutoDeleter<void, MemoryDelete >
|
||||
{
|
||||
MemoryDeleter() : AutoDeleter<void, MemoryDelete >() {}
|
||||
MemoryDeleter(void *memory) : AutoDeleter<void, MemoryDelete >(memory) {}
|
||||
};
|
||||
|
||||
|
||||
// CObjectDeleter
|
||||
|
||||
template<typename Type, typename DestructorReturnType>
|
||||
struct CObjectDelete
|
||||
{
|
||||
inline void operator()(Type *object)
|
||||
{
|
||||
if (fDestructor != NULL && object != NULL)
|
||||
fDestructor(object);
|
||||
}
|
||||
|
||||
template<typename Destructor>
|
||||
inline void operator=(Destructor destructor)
|
||||
{
|
||||
fDestructor = destructor;
|
||||
}
|
||||
|
||||
private:
|
||||
DestructorReturnType (*fDestructor)(Type*);
|
||||
};
|
||||
|
||||
template<typename Type, typename DestructorReturnType = void>
|
||||
struct CObjectDeleter
|
||||
: AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> >
|
||||
{
|
||||
typedef AutoDeleter<Type, CObjectDelete<Type, DestructorReturnType> > Base;
|
||||
|
||||
template<typename Destructor>
|
||||
CObjectDeleter(Destructor destructor) : Base()
|
||||
{
|
||||
Base::fDelete = destructor;
|
||||
}
|
||||
|
||||
template<typename Destructor>
|
||||
CObjectDeleter(Type *object, Destructor destructor) : Base(object)
|
||||
{
|
||||
Base::fDelete = destructor;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// MethodDeleter
|
||||
|
||||
template<typename Type, typename DestructorReturnType>
|
||||
struct MethodDelete
|
||||
{
|
||||
inline void operator()(Type *object)
|
||||
{
|
||||
if (fDestructor && object != NULL)
|
||||
(object->*fDestructor)();
|
||||
}
|
||||
|
||||
template<typename Destructor>
|
||||
inline void operator=(Destructor destructor)
|
||||
{
|
||||
fDestructor = destructor;
|
||||
}
|
||||
|
||||
private:
|
||||
DestructorReturnType (Type::*fDestructor)();
|
||||
};
|
||||
|
||||
|
||||
template<typename Type, typename DestructorReturnType = void>
|
||||
struct MethodDeleter
|
||||
: AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> >
|
||||
{
|
||||
typedef AutoDeleter<Type, MethodDelete<Type, DestructorReturnType> > Base;
|
||||
|
||||
template<typename Destructor>
|
||||
MethodDeleter(Destructor destructor) : Base()
|
||||
{
|
||||
Base::fDelete = destructor;
|
||||
}
|
||||
|
||||
template<typename Destructor>
|
||||
MethodDeleter(Type *object, Destructor destructor) : Base(object)
|
||||
{
|
||||
Base::fDelete = destructor;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
using BPrivate::ObjectDeleter;
|
||||
using BPrivate::ArrayDeleter;
|
||||
using BPrivate::MemoryDeleter;
|
||||
using BPrivate::CObjectDeleter;
|
||||
using BPrivate::MethodDeleter;
|
||||
|
||||
#endif // _AUTO_DELETER_H
|
||||
@@ -1,14 +1,30 @@
|
||||
// Elf.h
|
||||
|
||||
/*
|
||||
* Copyright 2002-2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _ELF_H
|
||||
#define _ELF_H
|
||||
|
||||
|
||||
#include <SupportDefs.h>
|
||||
|
||||
|
||||
// types
|
||||
typedef uint32 Elf32_Addr;
|
||||
typedef uint16 Elf32_Half;
|
||||
typedef uint32 Elf32_Off;
|
||||
typedef uint16 Elf32_Half;
|
||||
typedef int32 Elf32_Sword;
|
||||
typedef uint32 Elf32_Word;
|
||||
typedef uint32 Elf32_Xword;
|
||||
typedef int32 Elf32_Sxword;
|
||||
|
||||
typedef uint64 Elf64_Addr;
|
||||
typedef uint64 Elf64_Off;
|
||||
typedef uint16 Elf64_Half;
|
||||
typedef int32 Elf64_Sword;
|
||||
typedef uint32 Elf64_Word;
|
||||
typedef uint64 Elf64_Xword;
|
||||
typedef int64 Elf64_Sxword;
|
||||
|
||||
// e_ident indices
|
||||
#define EI_MAG0 0
|
||||
@@ -21,23 +37,9 @@ typedef uint32 Elf32_Word;
|
||||
#define EI_PAD 7
|
||||
#define EI_NIDENT 16
|
||||
|
||||
// object file header
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT];
|
||||
Elf32_Half e_type;
|
||||
Elf32_Half e_machine;
|
||||
Elf32_Word e_version;
|
||||
Elf32_Addr e_entry;
|
||||
Elf32_Off e_phoff;
|
||||
Elf32_Off e_shoff;
|
||||
Elf32_Word e_flags;
|
||||
Elf32_Half e_ehsize;
|
||||
Elf32_Half e_phentsize;
|
||||
Elf32_Half e_phnum;
|
||||
Elf32_Half e_shentsize;
|
||||
Elf32_Half e_shnum;
|
||||
Elf32_Half e_shstrndx;
|
||||
} Elf32_Ehdr;
|
||||
// e_ident EI_VERSION values
|
||||
#define EV_NONE 0
|
||||
#define EV_CURRENT 1
|
||||
|
||||
// e_ident EI_CLASS and EI_DATA values
|
||||
#define ELFCLASSNONE 0
|
||||
@@ -47,18 +49,6 @@ typedef struct {
|
||||
#define ELFDATA2LSB 1
|
||||
#define ELFDATA2MSB 2
|
||||
|
||||
// program header
|
||||
typedef struct {
|
||||
Elf32_Word p_type;
|
||||
Elf32_Off p_offset;
|
||||
Elf32_Addr p_vaddr;
|
||||
Elf32_Addr p_paddr;
|
||||
Elf32_Word p_filesz;
|
||||
Elf32_Word p_memsz;
|
||||
Elf32_Word p_flags;
|
||||
Elf32_Word p_align;
|
||||
} Elf32_Phdr;
|
||||
|
||||
// p_type
|
||||
#define PT_NULL 0
|
||||
#define PT_LOAD 1
|
||||
@@ -70,20 +60,6 @@ typedef struct {
|
||||
#define PT_LOPROC 0x70000000
|
||||
#define PT_HIPROC 0x7fffffff
|
||||
|
||||
// section header
|
||||
typedef struct {
|
||||
Elf32_Word sh_name;
|
||||
Elf32_Word sh_type;
|
||||
Elf32_Word sh_flags;
|
||||
Elf32_Addr sh_addr;
|
||||
Elf32_Off sh_offset;
|
||||
Elf32_Word sh_size;
|
||||
Elf32_Word sh_link;
|
||||
Elf32_Word sh_info;
|
||||
Elf32_Word sh_addralign;
|
||||
Elf32_Word sh_entsize;
|
||||
} Elf32_Shdr;
|
||||
|
||||
// sh_type values
|
||||
#define SHT_NULL 0
|
||||
#define SHT_PROGBITS 1
|
||||
@@ -102,6 +78,17 @@ typedef struct {
|
||||
#define SHT_LOUSER 0x80000000
|
||||
#define SHT_HIUSER 0xffffffff
|
||||
|
||||
// 32 bit definitions
|
||||
#undef _ELFX_BITS
|
||||
#define _ELFX_BITS 32
|
||||
#include <ElfX.h>
|
||||
|
||||
// 64 bit definitions
|
||||
#undef _ELFX_BITS
|
||||
#define _ELFX_BITS 64
|
||||
#include <ElfX.h>
|
||||
|
||||
#undef _ELFX_BITS
|
||||
|
||||
|
||||
#endif // _ELF_H
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2002-2009, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
// No header guard: The file is included twice by <Elf.h> and must not be
|
||||
// included elsewhere. The _ELFX_BITS macro must be define before inclusion.
|
||||
|
||||
|
||||
#undef ElfX
|
||||
|
||||
#if _ELFX_BITS == 32
|
||||
# define ElfX(x) Elf32_##x
|
||||
#elif _ELFX_BITS == 64
|
||||
# define ElfX(x) Elf64_##x
|
||||
#endif
|
||||
|
||||
|
||||
// object file header
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT];
|
||||
ElfX(Half) e_type;
|
||||
ElfX(Half) e_machine;
|
||||
ElfX(Word) e_version;
|
||||
ElfX(Addr) e_entry;
|
||||
ElfX(Off) e_phoff;
|
||||
ElfX(Off) e_shoff;
|
||||
ElfX(Word) e_flags;
|
||||
ElfX(Half) e_ehsize;
|
||||
ElfX(Half) e_phentsize;
|
||||
ElfX(Half) e_phnum;
|
||||
ElfX(Half) e_shentsize;
|
||||
ElfX(Half) e_shnum;
|
||||
ElfX(Half) e_shstrndx;
|
||||
} ElfX(Ehdr);
|
||||
|
||||
// program header
|
||||
typedef struct {
|
||||
ElfX(Word) p_type;
|
||||
#if _ELFX_BITS == 64
|
||||
ElfX(Word) p_flags;
|
||||
#endif
|
||||
ElfX(Off) p_offset;
|
||||
ElfX(Addr) p_vaddr;
|
||||
ElfX(Addr) p_paddr;
|
||||
ElfX(Xword) p_filesz;
|
||||
ElfX(Xword) p_memsz;
|
||||
#if _ELFX_BITS == 32
|
||||
ElfX(Word) p_flags;
|
||||
#endif
|
||||
ElfX(Xword) p_align;
|
||||
} ElfX(Phdr);
|
||||
|
||||
// section header
|
||||
typedef struct {
|
||||
ElfX(Word) sh_name;
|
||||
ElfX(Word) sh_type;
|
||||
ElfX(Xword) sh_flags;
|
||||
ElfX(Addr) sh_addr;
|
||||
ElfX(Off) sh_offset;
|
||||
ElfX(Xword) sh_size;
|
||||
ElfX(Word) sh_link;
|
||||
ElfX(Word) sh_info;
|
||||
ElfX(Xword) sh_addralign;
|
||||
ElfX(Xword) sh_entsize;
|
||||
} ElfX(Shdr);
|
||||
|
||||
#undef ElfX
|
||||
@@ -1,41 +1,47 @@
|
||||
//----------------------------------------------------------------------
|
||||
// This software is part of the OpenBeOS distribution and is covered
|
||||
// by the OpenBeOS license.
|
||||
//---------------------------------------------------------------------
|
||||
/*
|
||||
* Copyright 2002-2009, Ingo Weinhold, [email protected].
|
||||
* 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,112 @@ class ResourcesContainer;
|
||||
of resources to the file.
|
||||
|
||||
\author <a href='mailto:[email protected]'>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);
|
||||
|
||||
status_t _WriteResources(ResourcesContainer &container);
|
||||
status_t _MakeEmptyResourceFile();
|
||||
void _InitELFFile(BFile& file);
|
||||
|
||||
inline int16 _GetInt16(int16 value);
|
||||
inline uint16 _GetUInt16(uint16 value);
|
||||
inline int32 _GetInt32(int32 value);
|
||||
inline uint32 _GetUInt32(uint32 value);
|
||||
template<typename ElfHeader, typename ElfProgramHeader,
|
||||
typename ElfSectionHeader>
|
||||
void _InitELFXFile(BFile& file, uint64 fileSize);
|
||||
|
||||
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();
|
||||
|
||||
inline int16 _GetInt(int16 value) const;
|
||||
inline uint16 _GetInt(uint16 value) const;
|
||||
inline int32 _GetInt(int32 value) const;
|
||||
inline uint32 _GetInt(uint32 value) const;
|
||||
inline int64 _GetInt(int64 value) const;
|
||||
inline uint64 _GetInt(uint64 value) const;
|
||||
|
||||
private:
|
||||
OffsetFile fFile;
|
||||
uint32 fFileType;
|
||||
bool fHostEndianess;
|
||||
bool fEmptyResources;
|
||||
OffsetFile fFile;
|
||||
uint32 fFileType;
|
||||
bool fHostEndianess;
|
||||
bool fEmptyResources;
|
||||
};
|
||||
|
||||
// _GetInt16
|
||||
inline
|
||||
int16
|
||||
ResourceFile::_GetInt16(int16 value)
|
||||
|
||||
inline int16
|
||||
ResourceFile::_GetInt(int16 value) const
|
||||
{
|
||||
return (fHostEndianess ? value : B_SWAP_INT16(value));
|
||||
return fHostEndianess ? value : (int16)B_SWAP_INT16((uint16)value);
|
||||
}
|
||||
|
||||
// _GetUInt16
|
||||
inline
|
||||
uint16
|
||||
ResourceFile::_GetUInt16(uint16 value)
|
||||
|
||||
inline uint16
|
||||
ResourceFile::_GetInt(uint16 value) const
|
||||
{
|
||||
return (fHostEndianess ? value : B_SWAP_INT16(value));
|
||||
return fHostEndianess ? value : B_SWAP_INT16(value);
|
||||
}
|
||||
|
||||
// _GetInt32
|
||||
inline
|
||||
int32
|
||||
ResourceFile::_GetInt32(int32 value)
|
||||
|
||||
inline int32
|
||||
ResourceFile::_GetInt(int32 value) const
|
||||
{
|
||||
return (fHostEndianess ? value : B_SWAP_INT32(value));
|
||||
return fHostEndianess ? value : (int32)B_SWAP_INT32((uint32)value);
|
||||
}
|
||||
|
||||
// _GetUInt32
|
||||
inline
|
||||
uint32
|
||||
ResourceFile::_GetUInt32(uint32 value)
|
||||
|
||||
inline uint32
|
||||
ResourceFile::_GetInt(uint32 value) const
|
||||
{
|
||||
return (fHostEndianess ? value : B_SWAP_INT32(value));
|
||||
return fHostEndianess ? value : B_SWAP_INT32(value);
|
||||
}
|
||||
|
||||
|
||||
inline int64
|
||||
ResourceFile::_GetInt(int64 value) const
|
||||
{
|
||||
return fHostEndianess ? value : (int64)B_SWAP_INT64((uint64)value);
|
||||
}
|
||||
|
||||
|
||||
inline uint64
|
||||
ResourceFile::_GetInt(uint64 value) const
|
||||
{
|
||||
return fHostEndianess ? value : B_SWAP_INT64(value);
|
||||
}
|
||||
|
||||
|
||||
}; // namespace Storage
|
||||
}; // namespace BPrivate
|
||||
|
||||
|
||||
#endif // _RESOURCE_FILE_H
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user