xref: /haiku/headers/private/storage/ResourceFile.h (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
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 
20 struct resource_info;
21 struct PEFContainerHeader;
22 
23 
24 namespace BPrivate {
25 namespace Storage {
26 
27 
28 class Exception;
29 struct MemArea;
30 class ResourceItem;
31 struct resource_parse_info;
32 class 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 */
57 class ResourceFile {
58 public:
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 
73 private:
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(resource_parse_info& parseInfo,
87 									int32 index, uint32 tableOffset,
88 									bool peekAhead);
89 			void				_ReadInfoTable(resource_parse_info& parseInfo);
90 			bool				_ReadInfoTableEnd(const void* data,
91 									int32 dataSize);
92 			const void*			_ReadResourceInfo(
93 									resource_parse_info& parseInfo,
94 									const MemArea& area,
95 									const resource_info* info, type_code type,
96 									bool* readIndices);
97 
98 			status_t			_WriteResources(ResourcesContainer& container);
99 			status_t			_MakeEmptyResourceFile();
100 
101 	inline	int16				_GetInt(int16 value) const;
102 	inline	uint16				_GetInt(uint16 value) const;
103 	inline	int32				_GetInt(int32 value) const;
104 	inline	uint32				_GetInt(uint32 value) const;
105 	inline	int64				_GetInt(int64 value) const;
106 	inline	uint64				_GetInt(uint64 value) const;
107 
108 private:
109 			OffsetFile			fFile;
110 			uint32				fFileType;
111 			bool				fHostEndianess;
112 			bool				fEmptyResources;
113 };
114 
115 
116 inline int16
117 ResourceFile::_GetInt(int16 value) const
118 {
119 	return fHostEndianess ? value : (int16)B_SWAP_INT16((uint16)value);
120 }
121 
122 
123 inline uint16
124 ResourceFile::_GetInt(uint16 value) const
125 {
126 	return fHostEndianess ? value : B_SWAP_INT16(value);
127 }
128 
129 
130 inline int32
131 ResourceFile::_GetInt(int32 value) const
132 {
133 	return fHostEndianess ? value : (int32)B_SWAP_INT32((uint32)value);
134 }
135 
136 
137 inline uint32
138 ResourceFile::_GetInt(uint32 value) const
139 {
140 	return fHostEndianess ? value : B_SWAP_INT32(value);
141 }
142 
143 
144 inline int64
145 ResourceFile::_GetInt(int64 value) const
146 {
147 	return fHostEndianess ? value : (int64)B_SWAP_INT64((uint64)value);
148 }
149 
150 
151 inline uint64
152 ResourceFile::_GetInt(uint64 value) const
153 {
154 	return fHostEndianess ? value : B_SWAP_INT64(value);
155 }
156 
157 
158 };	// namespace Storage
159 };	// namespace BPrivate
160 
161 
162 #endif	// _RESOURCE_FILE_H
163