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.
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
{
32 class ReaderImplBase
{
35 BErrorOutput
* errorOutput
);
36 virtual ~ReaderImplBase();
38 virtual status_t
Init(int fd
, bool keepFD
);
42 BErrorOutput
* ErrorOutput() const;
45 struct AttributeHandlerContext
{
46 BErrorOutput
* errorOutput
;
48 BPackageContentHandler
* packageContentHandler
;
49 BLowLevelPackageContentHandler
* lowLevelHandler
;
51 bool hasLowLevelHandler
;
56 BHPKGPackageSectionID section
;
58 AttributeHandlerContext(BErrorOutput
* errorOutput
,
59 BPackageContentHandler
* packageContentHandler
,
60 BHPKGPackageSectionID section
);
62 AttributeHandlerContext(BErrorOutput
* errorOutput
,
63 BLowLevelPackageContentHandler
* lowLevelHandler
,
64 BHPKGPackageSectionID section
);
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
);
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
);
104 BPackageInfoAttributeValue
& fPackageInfoValue
;
105 BPackageVersionData
& fPackageVersionData
;
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
);
121 BPackageInfoAttributeValue
& fPackageInfoValue
;
125 struct PackageResolvableExpressionAttributeHandler
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
);
137 BPackageInfoAttributeValue
& fPackageInfoValue
;
141 struct PackageAttributeHandler
: AttributeHandler
{
142 virtual status_t
HandleAttribute(
143 AttributeHandlerContext
* context
, uint8 id
,
144 const AttributeValue
& value
, AttributeHandler
** _handler
);
147 BPackageInfoAttributeValue fPackageInfoValue
;
151 struct LowLevelAttributeHandler
: AttributeHandler
{
152 LowLevelAttributeHandler();
153 LowLevelAttributeHandler(uint8 id
,
154 const BPackageAttributeValue
& value
, void* parentToken
,
157 virtual status_t
HandleAttribute(
158 AttributeHandlerContext
* context
, uint8 id
,
159 const AttributeValue
& value
, AttributeHandler
** _handler
);
160 virtual status_t
Delete(AttributeHandlerContext
* context
);
166 AttributeValue fValue
;
172 uint32 compressedLength
;
173 uint32 uncompressedLength
;
176 uint64 currentOffset
;
177 uint64 stringsLength
;
182 SectionInfo(const char* _name
)
197 typedef SinglyLinkedList
<AttributeHandler
> AttributeHandlerList
;
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
,
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
);
232 SectionInfo fPackageAttributesSection
;
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
);
252 BErrorOutput
* fErrorOutput
;
256 SectionInfo
* fCurrentSection
;
258 AttributeHandlerList fAttributeHandlerStack
;
260 uint8
* fScratchBuffer
;
261 size_t fScratchBufferSize
;
266 ReaderImplBase::FD() const
273 ReaderImplBase::ErrorOutput() const
279 ReaderImplBase::SectionInfo
*
280 ReaderImplBase::CurrentSection()
282 return fCurrentSection
;
287 ReaderImplBase::SetCurrentSection(SectionInfo
* section
)
289 fCurrentSection
= section
;
293 template<typename Type
>
295 ReaderImplBase::_Read(Type
& _value
)
297 return _ReadSectionBuffer(&_value
, sizeof(Type
));
301 inline ReaderImplBase::AttributeHandler
*
302 ReaderImplBase::CurrentAttributeHandler() const
304 return fAttributeHandlerStack
.Head();
309 ReaderImplBase::PushAttributeHandler(AttributeHandler
* handler
)
311 fAttributeHandlerStack
.Add(handler
);
315 inline ReaderImplBase::AttributeHandler
*
316 ReaderImplBase::PopAttributeHandler()
318 return fAttributeHandlerStack
.RemoveHead();
323 ReaderImplBase::ClearAttributeHandlerStack()
325 fAttributeHandlerStack
.MakeEmpty();
329 } // namespace BPrivate
335 } // namespace BPackageKit
338 #endif // _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_