1/*
2 * Copyright 2002-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _RESOURCE_FILE_H
6#define _RESOURCE_FILE_H
7
8
9/*!
10	\file ResourceFile.h
11	ResourceFile interface declaration.
12*/
13
14
15#include <ByteOrder.h>
16
17#include "OffsetFile.h"
18
19
20struct resource_info;
21struct PEFContainerHeader;
22
23
24namespace BPrivate {
25namespace Storage {
26
27
28class Exception;
29struct MemArea;
30class ResourceItem;
31struct resource_parse_info;
32class ResourcesContainer;
33
34
35/*!
36	\class ResourceFile
37	\brief Represents a file capable of containing resources.
38
39	This class provides access to the resources of a file.
40	Basically a ResourceFile object can be set to a file, load infos for the
41	resources without loading their data (InitContainer()), read the data of
42	one (ReadResource()) or all resources (ReadResources()) and write all
43	resources to the file (WriteResources()).
44
45	Note, that the object does only provide the I/O functionality, it does
46	not store any information about the resources -- this is done via a
47	ResourcesContainer. We gain flexibility using this approach, since e.g.
48	a certain resource may be represented by more than one ResourceItem and
49	we can have as many ResourcesContainers for the resources as we like.
50	In particular it is nice, that at any time we can write an arbitrary set
51	of resources to the file.
52
53	\author <a href='mailto:bonefish@users.sf.net'>Ingo Weinhold</a>
54
55	\version 0.0.0
56*/
57class ResourceFile {
58public:
59								ResourceFile();
60	virtual						~ResourceFile();
61
62			status_t			SetTo(BFile* file, bool clobber = false);
63			void				Unset();
64			status_t			InitCheck() const;
65
66			status_t			InitContainer(ResourcesContainer& container);
67			status_t			ReadResource(ResourceItem& resource,
68									bool force = false);
69			status_t			ReadResources(ResourcesContainer& container,
70									bool force = false);
71			status_t			WriteResources(ResourcesContainer& container);
72
73private:
74			void				_InitFile(BFile& file, bool clobber);
75
76			void				_InitELFFile(BFile& file);
77
78			template<typename ElfHeader, typename ElfProgramHeader,
79				typename ElfSectionHeader>
80			void				_InitELFXFile(BFile& file, uint64 fileSize);
81
82			void				_InitPEFFile(BFile& file,
83									const PEFContainerHeader& pefHeader);
84			void				_ReadHeader(resource_parse_info& parseInfo);
85			void				_ReadIndex(resource_parse_info& parseInfo);
86			bool				_ReadIndexEntry(BPositionIO& buffer,
87									resource_parse_info& parseInfo,
88									int32 index, uint32 tableOffset,
89									bool peekAhead);
90			void				_ReadInfoTable(resource_parse_info& parseInfo);
91			bool				_ReadInfoTableEnd(const void* data,
92									int32 dataSize);
93			const void*			_ReadResourceInfo(
94									resource_parse_info& parseInfo,
95									const MemArea& area,
96									const resource_info* info, type_code type,
97									bool* readIndices);
98
99			status_t			_WriteResources(ResourcesContainer& container);
100			status_t			_MakeEmptyResourceFile();
101
102	inline	int16				_GetInt(int16 value) const;
103	inline	uint16				_GetInt(uint16 value) const;
104	inline	int32				_GetInt(int32 value) const;
105	inline	uint32				_GetInt(uint32 value) const;
106	inline	int64				_GetInt(int64 value) const;
107	inline	uint64				_GetInt(uint64 value) const;
108
109private:
110			OffsetFile			fFile;
111			uint32				fFileType;
112			bool				fHostEndianess;
113			bool				fEmptyResources;
114};
115
116
117inline int16
118ResourceFile::_GetInt(int16 value) const
119{
120	return fHostEndianess ? value : (int16)B_SWAP_INT16((uint16)value);
121}
122
123
124inline uint16
125ResourceFile::_GetInt(uint16 value) const
126{
127	return fHostEndianess ? value : B_SWAP_INT16(value);
128}
129
130
131inline int32
132ResourceFile::_GetInt(int32 value) const
133{
134	return fHostEndianess ? value : (int32)B_SWAP_INT32((uint32)value);
135}
136
137
138inline uint32
139ResourceFile::_GetInt(uint32 value) const
140{
141	return fHostEndianess ? value : B_SWAP_INT32(value);
142}
143
144
145inline int64
146ResourceFile::_GetInt(int64 value) const
147{
148	return fHostEndianess ? value : (int64)B_SWAP_INT64((uint64)value);
149}
150
151
152inline uint64
153ResourceFile::_GetInt(uint64 value) const
154{
155	return fHostEndianess ? value : B_SWAP_INT64(value);
156}
157
158
159};	// namespace Storage
160};	// namespace BPrivate
161
162
163#endif	// _RESOURCE_FILE_H
164