xref: /haiku/headers/private/package/hpkg/ReaderImplBase.h (revision cbed190f71b8aff814bf95539c39a1bcfb953ed8)
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__PRIVATE__READER_IMPL_BASE_H_
7 #define _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
8 
9 
10 #include <SupportDefs.h>
11 
12 #include <util/SinglyLinkedList.h>
13 
14 #include <package/hpkg/PackageAttributeValue.h>
15 #include <package/hpkg/PackageContentHandler.h>
16 #include <package/hpkg/PackageInfoAttributeValue.h>
17 
18 
19 namespace BPackageKit {
20 
21 namespace BHPKG {
22 
23 
24 class BErrorOutput;
25 
26 
27 namespace BPrivate {
28 
29 
30 class ReaderImplBase {
31 protected:
32 								ReaderImplBase(
33 									BErrorOutput* errorOutput);
34 	virtual						~ReaderImplBase();
35 
36 	virtual	status_t			Init(int fd, bool keepFD);
37 
38 			int					FD() const;
39 
40 			BErrorOutput*		ErrorOutput() const;
41 
42 protected:
43 			struct AttributeHandlerContext {
44 				BErrorOutput*	errorOutput;
45 				union {
46 					BPackageContentHandler*			packageContentHandler;
47 					BLowLevelPackageContentHandler*	lowLevelHandler;
48 				};
49 				bool			hasLowLevelHandler;
50 
51 				uint64			heapOffset;
52 				uint64			heapSize;
53 
54 				AttributeHandlerContext(BErrorOutput* errorOutput,
55 					BPackageContentHandler* packageContentHandler);
56 
57 				AttributeHandlerContext(BErrorOutput* errorOutput,
58 					BLowLevelPackageContentHandler* lowLevelHandler);
59 
60 				void ErrorOccurred();
61 			};
62 
63 
64 			typedef BPackageAttributeValue AttributeValue;
65 
66 			struct AttributeHandler
67 				: SinglyLinkedListLinkImpl<AttributeHandler> {
68 				virtual ~AttributeHandler();
69 
70 				void SetLevel(int level);
71 				virtual status_t HandleAttribute(
72 					AttributeHandlerContext* context, uint8 id,
73 					const AttributeValue& value, AttributeHandler** _handler);
74 
75 				virtual status_t Delete(AttributeHandlerContext* context);
76 
77 			protected:
78 				int	fLevel;
79 			};
80 
81 
82 			struct IgnoreAttributeHandler : AttributeHandler {
83 			};
84 
85 
86 			struct PackageVersionAttributeHandler : AttributeHandler {
87 				PackageVersionAttributeHandler(
88 					BPackageInfoAttributeValue& packageInfoValue,
89 					BPackageVersionData& versionData, bool notify);
90 
91 				virtual status_t HandleAttribute(
92 					AttributeHandlerContext* context, uint8 id,
93 					const AttributeValue& value, AttributeHandler** _handler);
94 
95 				virtual status_t Delete(AttributeHandlerContext* context);
96 
97 			private:
98 				BPackageInfoAttributeValue&	fPackageInfoValue;
99 				BPackageVersionData&		fPackageVersionData;
100 				bool						fNotify;
101 			};
102 
103 
104 			struct PackageResolvableAttributeHandler : AttributeHandler {
105 				PackageResolvableAttributeHandler(
106 					BPackageInfoAttributeValue& packageInfoValue);
107 
108 				virtual status_t HandleAttribute(
109 					AttributeHandlerContext* context, uint8 id,
110 					const AttributeValue& value, AttributeHandler** _handler);
111 
112 				virtual status_t Delete(AttributeHandlerContext* context);
113 
114 			private:
115 				BPackageInfoAttributeValue&	fPackageInfoValue;
116 			};
117 
118 
119 			struct PackageResolvableExpressionAttributeHandler
120 				: AttributeHandler {
121 				PackageResolvableExpressionAttributeHandler(
122 					BPackageInfoAttributeValue& packageInfoValue);
123 
124 				virtual status_t HandleAttribute(
125 					AttributeHandlerContext* context, uint8 id,
126 					const AttributeValue& value, AttributeHandler** _handler);
127 
128 				virtual status_t Delete(AttributeHandlerContext* context);
129 
130 			private:
131 				BPackageInfoAttributeValue&	fPackageInfoValue;
132 			};
133 
134 
135 			struct PackageAttributeHandler : AttributeHandler {
136 				virtual status_t HandleAttribute(
137 					AttributeHandlerContext* context, uint8 id,
138 					const AttributeValue& value, AttributeHandler** _handler);
139 
140 			private:
141 				BPackageInfoAttributeValue	fPackageInfoValue;
142 			};
143 
144 
145 			struct LowLevelAttributeHandler : AttributeHandler {
146 				LowLevelAttributeHandler();
147 				LowLevelAttributeHandler(uint8 id,
148 					const BPackageAttributeValue& value, void* token);
149 
150 				virtual status_t HandleAttribute(
151 					AttributeHandlerContext* context, uint8 id,
152 					const AttributeValue& value, AttributeHandler** _handler);
153 				virtual status_t Delete(AttributeHandlerContext* context);
154 
155 			private:
156 				void*			fToken;
157 				uint8			fID;
158 				AttributeValue	fValue;
159 			};
160 
161 
162 			struct SectionInfo {
163 				uint32			compression;
164 				uint32			compressedLength;
165 				uint32			uncompressedLength;
166 				uint8*			data;
167 				uint64			offset;
168 				uint64			currentOffset;
169 				uint64			stringsLength;
170 				uint64			stringsCount;
171 				char**			strings;
172 				const char*		name;
173 
174 				SectionInfo(const char* _name)
175 					:
176 					data(NULL),
177 					strings(NULL),
178 					name(_name)
179 				{
180 				}
181 
182 				~SectionInfo()
183 				{
184 					delete[] strings;
185 					delete[] data;
186 				}
187 			};
188 
189 			typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
190 
191 protected:
192 			const char*			CheckCompression(
193 									const SectionInfo& section) const;
194 
195 			status_t			ParseStrings();
196 
197 			status_t			ParsePackageAttributesSection(
198 									AttributeHandlerContext* context,
199 									AttributeHandler* rootAttributeHandler);
200 			status_t			ParseAttributeTree(
201 									AttributeHandlerContext* context);
202 
203 	virtual	status_t			ReadAttributeValue(uint8 type, uint8 encoding,
204 									AttributeValue& _value);
205 
206 			status_t			ReadUnsignedLEB128(uint64& _value);
207 
208 			status_t			ReadBuffer(off_t offset, void* buffer,
209 									size_t size);
210 			status_t			ReadCompressedBuffer(
211 									const SectionInfo& section);
212 
213 	inline	AttributeHandler*	CurrentAttributeHandler() const;
214 	inline	void				PushAttributeHandler(
215 									AttributeHandler* handler);
216 	inline	AttributeHandler*	PopAttributeHandler();
217 	inline	void				ClearAttributeHandlerStack();
218 
219 	inline	SectionInfo*		CurrentSection();
220 	inline	void				SetCurrentSection(SectionInfo* section);
221 
222 protected:
223 			SectionInfo			fPackageAttributesSection;
224 
225 private:
226 	template<typename Type>
227 	inline	status_t			_Read(Type& _value);
228 
229 			status_t			_ReadSectionBuffer(void* buffer, size_t size);
230 
231 			status_t			_ReadAttribute(uint8& _id,
232 									AttributeValue& _value,
233 									bool* _hasChildren = NULL,
234 									uint64* _tag = NULL);
235 
236 			status_t			_ReadString(const char*& _string,
237 									size_t* _stringLength = NULL);
238 
239 private:
240 			BErrorOutput*		fErrorOutput;
241 			int					fFD;
242 			bool				fOwnsFD;
243 
244 			SectionInfo*		fCurrentSection;
245 
246 			AttributeHandlerList fAttributeHandlerStack;
247 
248 			uint8*				fScratchBuffer;
249 			size_t				fScratchBufferSize;
250 };
251 
252 
253 inline int
254 ReaderImplBase::FD() const
255 {
256 	return fFD;
257 }
258 
259 
260 inline BErrorOutput*
261 ReaderImplBase::ErrorOutput() const
262 {
263 	return fErrorOutput;
264 }
265 
266 
267 ReaderImplBase::SectionInfo*
268 ReaderImplBase::CurrentSection()
269 {
270 	return fCurrentSection;
271 }
272 
273 
274 void
275 ReaderImplBase::SetCurrentSection(SectionInfo* section)
276 {
277 	fCurrentSection = section;
278 }
279 
280 
281 template<typename Type>
282 status_t
283 ReaderImplBase::_Read(Type& _value)
284 {
285 	return _ReadSectionBuffer(&_value, sizeof(Type));
286 }
287 
288 
289 inline ReaderImplBase::AttributeHandler*
290 ReaderImplBase::CurrentAttributeHandler() const
291 {
292 	return fAttributeHandlerStack.Head();
293 }
294 
295 
296 inline void
297 ReaderImplBase::PushAttributeHandler(AttributeHandler* handler)
298 {
299 	fAttributeHandlerStack.Add(handler);
300 }
301 
302 
303 inline ReaderImplBase::AttributeHandler*
304 ReaderImplBase::PopAttributeHandler()
305 {
306 	return fAttributeHandlerStack.RemoveHead();
307 }
308 
309 
310 inline void
311 ReaderImplBase::ClearAttributeHandlerStack()
312 {
313 	fAttributeHandlerStack.MakeEmpty();
314 }
315 
316 
317 }	// namespace BPrivate
318 
319 }	// namespace BHPKG
320 
321 }	// namespace BPackageKit
322 
323 
324 #endif	// _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
325