btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / package / hpkg / v1 / ReaderImplBase.h
blob4147d422b7e706d1f8c9425d6bb382ad2b3dd080
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
4 * Distributed under the terms of the MIT License.
5 */
6 #ifndef _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_
7 #define _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_
10 #include <SupportDefs.h>
12 #include <util/SinglyLinkedList.h>
14 #include <package/hpkg/PackageInfoAttributeValue.h>
15 #include <package/hpkg/v1/PackageAttributeValue.h>
16 #include <package/hpkg/v1/PackageContentHandler.h>
19 namespace BPackageKit {
21 namespace BHPKG {
24 class BErrorOutput;
27 namespace V1 {
29 namespace BPrivate {
32 class ReaderImplBase {
33 protected:
34 ReaderImplBase(
35 BErrorOutput* errorOutput);
36 virtual ~ReaderImplBase();
38 virtual status_t Init(int fd, bool keepFD);
40 int FD() const;
42 BErrorOutput* ErrorOutput() const;
44 protected:
45 struct AttributeHandlerContext {
46 BErrorOutput* errorOutput;
47 union {
48 BPackageContentHandler* packageContentHandler;
49 BLowLevelPackageContentHandler* lowLevelHandler;
51 bool hasLowLevelHandler;
53 uint64 heapOffset;
54 uint64 heapSize;
56 BHPKGPackageSectionID section;
58 AttributeHandlerContext(BErrorOutput* errorOutput,
59 BPackageContentHandler* packageContentHandler,
60 BHPKGPackageSectionID section);
62 AttributeHandlerContext(BErrorOutput* errorOutput,
63 BLowLevelPackageContentHandler* lowLevelHandler,
64 BHPKGPackageSectionID section);
66 void ErrorOccurred();
70 typedef BPackageAttributeValue AttributeValue;
72 struct AttributeHandler
73 : SinglyLinkedListLinkImpl<AttributeHandler> {
74 virtual ~AttributeHandler();
76 void SetLevel(int level);
77 virtual status_t HandleAttribute(
78 AttributeHandlerContext* context, uint8 id,
79 const AttributeValue& value, AttributeHandler** _handler);
81 virtual status_t Delete(AttributeHandlerContext* context);
83 protected:
84 int fLevel;
88 struct IgnoreAttributeHandler : AttributeHandler {
92 struct PackageVersionAttributeHandler : AttributeHandler {
93 PackageVersionAttributeHandler(
94 BPackageInfoAttributeValue& packageInfoValue,
95 BPackageVersionData& versionData, bool notify);
97 virtual status_t HandleAttribute(
98 AttributeHandlerContext* context, uint8 id,
99 const AttributeValue& value, AttributeHandler** _handler);
101 virtual status_t Delete(AttributeHandlerContext* context);
103 private:
104 BPackageInfoAttributeValue& fPackageInfoValue;
105 BPackageVersionData& fPackageVersionData;
106 bool fNotify;
110 struct PackageResolvableAttributeHandler : AttributeHandler {
111 PackageResolvableAttributeHandler(
112 BPackageInfoAttributeValue& packageInfoValue);
114 virtual status_t HandleAttribute(
115 AttributeHandlerContext* context, uint8 id,
116 const AttributeValue& value, AttributeHandler** _handler);
118 virtual status_t Delete(AttributeHandlerContext* context);
120 private:
121 BPackageInfoAttributeValue& fPackageInfoValue;
125 struct PackageResolvableExpressionAttributeHandler
126 : AttributeHandler {
127 PackageResolvableExpressionAttributeHandler(
128 BPackageInfoAttributeValue& packageInfoValue);
130 virtual status_t HandleAttribute(
131 AttributeHandlerContext* context, uint8 id,
132 const AttributeValue& value, AttributeHandler** _handler);
134 virtual status_t Delete(AttributeHandlerContext* context);
136 private:
137 BPackageInfoAttributeValue& fPackageInfoValue;
141 struct PackageAttributeHandler : AttributeHandler {
142 virtual status_t HandleAttribute(
143 AttributeHandlerContext* context, uint8 id,
144 const AttributeValue& value, AttributeHandler** _handler);
146 private:
147 BPackageInfoAttributeValue fPackageInfoValue;
151 struct LowLevelAttributeHandler : AttributeHandler {
152 LowLevelAttributeHandler();
153 LowLevelAttributeHandler(uint8 id,
154 const BPackageAttributeValue& value, void* parentToken,
155 void* token);
157 virtual status_t HandleAttribute(
158 AttributeHandlerContext* context, uint8 id,
159 const AttributeValue& value, AttributeHandler** _handler);
160 virtual status_t Delete(AttributeHandlerContext* context);
162 private:
163 void* fParentToken;
164 void* fToken;
165 uint8 fID;
166 AttributeValue fValue;
170 struct SectionInfo {
171 uint32 compression;
172 uint32 compressedLength;
173 uint32 uncompressedLength;
174 uint8* data;
175 uint64 offset;
176 uint64 currentOffset;
177 uint64 stringsLength;
178 uint64 stringsCount;
179 char** strings;
180 const char* name;
182 SectionInfo(const char* _name)
184 data(NULL),
185 strings(NULL),
186 name(_name)
190 ~SectionInfo()
192 delete[] strings;
193 delete[] data;
197 typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
199 protected:
200 const char* CheckCompression(
201 const SectionInfo& section) const;
203 status_t ParseStrings();
205 status_t ParsePackageAttributesSection(
206 AttributeHandlerContext* context,
207 AttributeHandler* rootAttributeHandler);
208 status_t ParseAttributeTree(
209 AttributeHandlerContext* context,
210 bool& _sectionHandled);
212 virtual status_t ReadAttributeValue(uint8 type, uint8 encoding,
213 AttributeValue& _value);
215 status_t ReadUnsignedLEB128(uint64& _value);
217 status_t ReadBuffer(off_t offset, void* buffer,
218 size_t size);
219 status_t ReadCompressedBuffer(
220 const SectionInfo& section);
222 inline AttributeHandler* CurrentAttributeHandler() const;
223 inline void PushAttributeHandler(
224 AttributeHandler* handler);
225 inline AttributeHandler* PopAttributeHandler();
226 inline void ClearAttributeHandlerStack();
228 inline SectionInfo* CurrentSection();
229 inline void SetCurrentSection(SectionInfo* section);
231 protected:
232 SectionInfo fPackageAttributesSection;
234 private:
235 status_t _ParseAttributeTree(
236 AttributeHandlerContext* context);
238 template<typename Type>
239 inline status_t _Read(Type& _value);
241 status_t _ReadSectionBuffer(void* buffer, size_t size);
243 status_t _ReadAttribute(uint8& _id,
244 AttributeValue& _value,
245 bool* _hasChildren = NULL,
246 uint64* _tag = NULL);
248 status_t _ReadString(const char*& _string,
249 size_t* _stringLength = NULL);
251 private:
252 BErrorOutput* fErrorOutput;
253 int fFD;
254 bool fOwnsFD;
256 SectionInfo* fCurrentSection;
258 AttributeHandlerList fAttributeHandlerStack;
260 uint8* fScratchBuffer;
261 size_t fScratchBufferSize;
265 inline int
266 ReaderImplBase::FD() const
268 return fFD;
272 inline BErrorOutput*
273 ReaderImplBase::ErrorOutput() const
275 return fErrorOutput;
279 ReaderImplBase::SectionInfo*
280 ReaderImplBase::CurrentSection()
282 return fCurrentSection;
286 void
287 ReaderImplBase::SetCurrentSection(SectionInfo* section)
289 fCurrentSection = section;
293 template<typename Type>
294 status_t
295 ReaderImplBase::_Read(Type& _value)
297 return _ReadSectionBuffer(&_value, sizeof(Type));
301 inline ReaderImplBase::AttributeHandler*
302 ReaderImplBase::CurrentAttributeHandler() const
304 return fAttributeHandlerStack.Head();
308 inline void
309 ReaderImplBase::PushAttributeHandler(AttributeHandler* handler)
311 fAttributeHandlerStack.Add(handler);
315 inline ReaderImplBase::AttributeHandler*
316 ReaderImplBase::PopAttributeHandler()
318 return fAttributeHandlerStack.RemoveHead();
322 inline void
323 ReaderImplBase::ClearAttributeHandlerStack()
325 fAttributeHandlerStack.MakeEmpty();
329 } // namespace BPrivate
331 } // namespace V1
333 } // namespace BHPKG
335 } // namespace BPackageKit
338 #endif // _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_