vfs: check userland buffers before reading them.
[haiku.git] / headers / private / storage / ResourceFile.h
blob0f83cf96a121ac929f14cce7030662e18aeef76b
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
9 /*!
10 \file ResourceFile.h
11 ResourceFile interface declaration.
15 #include <ByteOrder.h>
17 #include "OffsetFile.h"
20 struct resource_info;
21 struct PEFContainerHeader;
24 namespace BPrivate {
25 namespace Storage {
28 class Exception;
29 struct MemArea;
30 class ResourceItem;
31 struct resource_parse_info;
32 class ResourcesContainer;
35 /*!
36 \class ResourceFile
37 \brief Represents a file capable of containing resources.
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()).
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.
53 \author <a href='mailto:bonefish@users.sf.net'>Ingo Weinhold</a>
55 \version 0.0.0
57 class ResourceFile {
58 public:
59 ResourceFile();
60 virtual ~ResourceFile();
62 status_t SetTo(BFile* file, bool clobber = false);
63 void Unset();
64 status_t InitCheck() const;
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);
73 private:
74 void _InitFile(BFile& file, bool clobber);
76 void _InitELFFile(BFile& file);
78 template<typename ElfHeader, typename ElfProgramHeader,
79 typename ElfSectionHeader>
80 void _InitELFXFile(BFile& file, uint64 fileSize);
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);
98 status_t _WriteResources(ResourcesContainer& container);
99 status_t _MakeEmptyResourceFile();
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;
108 private:
109 OffsetFile fFile;
110 uint32 fFileType;
111 bool fHostEndianess;
112 bool fEmptyResources;
116 inline int16
117 ResourceFile::_GetInt(int16 value) const
119 return fHostEndianess ? value : (int16)B_SWAP_INT16((uint16)value);
123 inline uint16
124 ResourceFile::_GetInt(uint16 value) const
126 return fHostEndianess ? value : B_SWAP_INT16(value);
130 inline int32
131 ResourceFile::_GetInt(int32 value) const
133 return fHostEndianess ? value : (int32)B_SWAP_INT32((uint32)value);
137 inline uint32
138 ResourceFile::_GetInt(uint32 value) const
140 return fHostEndianess ? value : B_SWAP_INT32(value);
144 inline int64
145 ResourceFile::_GetInt(int64 value) const
147 return fHostEndianess ? value : (int64)B_SWAP_INT64((uint64)value);
151 inline uint64
152 ResourceFile::_GetInt(uint64 value) const
154 return fHostEndianess ? value : B_SWAP_INT64(value);
158 }; // namespace Storage
159 }; // namespace BPrivate
162 #endif // _RESOURCE_FILE_H