17575abbcSIngo Weinhold/*
27575abbcSIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
37575abbcSIngo Weinhold * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
47575abbcSIngo Weinhold * Distributed under the terms of the MIT License.
57575abbcSIngo Weinhold */
67575abbcSIngo Weinhold#ifndef _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_
77575abbcSIngo Weinhold#define _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_
87575abbcSIngo Weinhold
97575abbcSIngo Weinhold
107575abbcSIngo Weinhold#include <SupportDefs.h>
117575abbcSIngo Weinhold
127575abbcSIngo Weinhold#include <util/SinglyLinkedList.h>
137575abbcSIngo Weinhold
147575abbcSIngo Weinhold#include <package/hpkg/PackageInfoAttributeValue.h>
157575abbcSIngo Weinhold#include <package/hpkg/v1/PackageAttributeValue.h>
167575abbcSIngo Weinhold#include <package/hpkg/v1/PackageContentHandler.h>
177575abbcSIngo Weinhold
187575abbcSIngo Weinhold
197575abbcSIngo Weinholdnamespace BPackageKit {
207575abbcSIngo Weinhold
217575abbcSIngo Weinholdnamespace BHPKG {
227575abbcSIngo Weinhold
237575abbcSIngo Weinhold
247575abbcSIngo Weinholdclass BErrorOutput;
257575abbcSIngo Weinhold
267575abbcSIngo Weinhold
277575abbcSIngo Weinholdnamespace V1 {
287575abbcSIngo Weinhold
297575abbcSIngo Weinholdnamespace BPrivate {
307575abbcSIngo Weinhold
317575abbcSIngo Weinhold
327575abbcSIngo Weinholdclass ReaderImplBase {
337575abbcSIngo Weinholdprotected:
347575abbcSIngo Weinhold								ReaderImplBase(
357575abbcSIngo Weinhold									BErrorOutput* errorOutput);
367575abbcSIngo Weinhold	virtual						~ReaderImplBase();
377575abbcSIngo Weinhold
387575abbcSIngo Weinhold	virtual	status_t			Init(int fd, bool keepFD);
397575abbcSIngo Weinhold
407575abbcSIngo Weinhold			int					FD() const;
417575abbcSIngo Weinhold
427575abbcSIngo Weinhold			BErrorOutput*		ErrorOutput() const;
437575abbcSIngo Weinhold
447575abbcSIngo Weinholdprotected:
457575abbcSIngo Weinhold			struct AttributeHandlerContext {
467575abbcSIngo Weinhold				BErrorOutput*	errorOutput;
477575abbcSIngo Weinhold				union {
487575abbcSIngo Weinhold					BPackageContentHandler*			packageContentHandler;
497575abbcSIngo Weinhold					BLowLevelPackageContentHandler*	lowLevelHandler;
507575abbcSIngo Weinhold				};
517575abbcSIngo Weinhold				bool			hasLowLevelHandler;
527575abbcSIngo Weinhold
537575abbcSIngo Weinhold				uint64			heapOffset;
547575abbcSIngo Weinhold				uint64			heapSize;
557575abbcSIngo Weinhold
567575abbcSIngo Weinhold				BHPKGPackageSectionID	section;
577575abbcSIngo Weinhold
587575abbcSIngo Weinhold				AttributeHandlerContext(BErrorOutput* errorOutput,
597575abbcSIngo Weinhold					BPackageContentHandler* packageContentHandler,
607575abbcSIngo Weinhold					BHPKGPackageSectionID section);
617575abbcSIngo Weinhold
627575abbcSIngo Weinhold				AttributeHandlerContext(BErrorOutput* errorOutput,
637575abbcSIngo Weinhold					BLowLevelPackageContentHandler* lowLevelHandler,
647575abbcSIngo Weinhold					BHPKGPackageSectionID section);
657575abbcSIngo Weinhold
667575abbcSIngo Weinhold				void ErrorOccurred();
677575abbcSIngo Weinhold			};
687575abbcSIngo Weinhold
697575abbcSIngo Weinhold
707575abbcSIngo Weinhold			typedef BPackageAttributeValue AttributeValue;
717575abbcSIngo Weinhold
727575abbcSIngo Weinhold			struct AttributeHandler
737575abbcSIngo Weinhold				: SinglyLinkedListLinkImpl<AttributeHandler> {
747575abbcSIngo Weinhold				virtual ~AttributeHandler();
757575abbcSIngo Weinhold
767575abbcSIngo Weinhold				void SetLevel(int level);
777575abbcSIngo Weinhold				virtual status_t HandleAttribute(
787575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
797575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
807575abbcSIngo Weinhold
817575abbcSIngo Weinhold				virtual status_t Delete(AttributeHandlerContext* context);
827575abbcSIngo Weinhold
837575abbcSIngo Weinhold			protected:
847575abbcSIngo Weinhold				int	fLevel;
857575abbcSIngo Weinhold			};
867575abbcSIngo Weinhold
877575abbcSIngo Weinhold
887575abbcSIngo Weinhold			struct IgnoreAttributeHandler : AttributeHandler {
897575abbcSIngo Weinhold			};
907575abbcSIngo Weinhold
917575abbcSIngo Weinhold
927575abbcSIngo Weinhold			struct PackageVersionAttributeHandler : AttributeHandler {
937575abbcSIngo Weinhold				PackageVersionAttributeHandler(
947575abbcSIngo Weinhold					BPackageInfoAttributeValue& packageInfoValue,
957575abbcSIngo Weinhold					BPackageVersionData& versionData, bool notify);
967575abbcSIngo Weinhold
977575abbcSIngo Weinhold				virtual status_t HandleAttribute(
987575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
997575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
1007575abbcSIngo Weinhold
1017575abbcSIngo Weinhold				virtual status_t Delete(AttributeHandlerContext* context);
1027575abbcSIngo Weinhold
1037575abbcSIngo Weinhold			private:
1047575abbcSIngo Weinhold				BPackageInfoAttributeValue&	fPackageInfoValue;
1057575abbcSIngo Weinhold				BPackageVersionData&		fPackageVersionData;
1067575abbcSIngo Weinhold				bool						fNotify;
1077575abbcSIngo Weinhold			};
1087575abbcSIngo Weinhold
1097575abbcSIngo Weinhold
1107575abbcSIngo Weinhold			struct PackageResolvableAttributeHandler : AttributeHandler {
1117575abbcSIngo Weinhold				PackageResolvableAttributeHandler(
1127575abbcSIngo Weinhold					BPackageInfoAttributeValue& packageInfoValue);
1137575abbcSIngo Weinhold
1147575abbcSIngo Weinhold				virtual status_t HandleAttribute(
1157575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
1167575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
1177575abbcSIngo Weinhold
1187575abbcSIngo Weinhold				virtual status_t Delete(AttributeHandlerContext* context);
1197575abbcSIngo Weinhold
1207575abbcSIngo Weinhold			private:
1217575abbcSIngo Weinhold				BPackageInfoAttributeValue&	fPackageInfoValue;
1227575abbcSIngo Weinhold			};
1237575abbcSIngo Weinhold
1247575abbcSIngo Weinhold
1257575abbcSIngo Weinhold			struct PackageResolvableExpressionAttributeHandler
1267575abbcSIngo Weinhold				: AttributeHandler {
1277575abbcSIngo Weinhold				PackageResolvableExpressionAttributeHandler(
1287575abbcSIngo Weinhold					BPackageInfoAttributeValue& packageInfoValue);
1297575abbcSIngo Weinhold
1307575abbcSIngo Weinhold				virtual status_t HandleAttribute(
1317575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
1327575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
1337575abbcSIngo Weinhold
1347575abbcSIngo Weinhold				virtual status_t Delete(AttributeHandlerContext* context);
1357575abbcSIngo Weinhold
1367575abbcSIngo Weinhold			private:
1377575abbcSIngo Weinhold				BPackageInfoAttributeValue&	fPackageInfoValue;
1387575abbcSIngo Weinhold			};
1397575abbcSIngo Weinhold
1407575abbcSIngo Weinhold
1417575abbcSIngo Weinhold			struct PackageAttributeHandler : AttributeHandler {
1427575abbcSIngo Weinhold				virtual status_t HandleAttribute(
1437575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
1447575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
1457575abbcSIngo Weinhold
1467575abbcSIngo Weinhold			private:
1477575abbcSIngo Weinhold				BPackageInfoAttributeValue	fPackageInfoValue;
1487575abbcSIngo Weinhold			};
1497575abbcSIngo Weinhold
1507575abbcSIngo Weinhold
1517575abbcSIngo Weinhold			struct LowLevelAttributeHandler : AttributeHandler {
1527575abbcSIngo Weinhold				LowLevelAttributeHandler();
1537575abbcSIngo Weinhold				LowLevelAttributeHandler(uint8 id,
1547575abbcSIngo Weinhold					const BPackageAttributeValue& value, void* parentToken,
1557575abbcSIngo Weinhold					void* token);
1567575abbcSIngo Weinhold
1577575abbcSIngo Weinhold				virtual status_t HandleAttribute(
1587575abbcSIngo Weinhold					AttributeHandlerContext* context, uint8 id,
1597575abbcSIngo Weinhold					const AttributeValue& value, AttributeHandler** _handler);
1607575abbcSIngo Weinhold				virtual status_t Delete(AttributeHandlerContext* context);
1617575abbcSIngo Weinhold
1627575abbcSIngo Weinhold			private:
1637575abbcSIngo Weinhold				void*			fParentToken;
1647575abbcSIngo Weinhold				void*			fToken;
1657575abbcSIngo Weinhold				uint8			fID;
1667575abbcSIngo Weinhold				AttributeValue	fValue;
1677575abbcSIngo Weinhold			};
1687575abbcSIngo Weinhold
1697575abbcSIngo Weinhold
1707575abbcSIngo Weinhold			struct SectionInfo {
1717575abbcSIngo Weinhold				uint32			compression;
1727575abbcSIngo Weinhold				uint32			compressedLength;
1737575abbcSIngo Weinhold				uint32			uncompressedLength;
1747575abbcSIngo Weinhold				uint8*			data;
1757575abbcSIngo Weinhold				uint64			offset;
1767575abbcSIngo Weinhold				uint64			currentOffset;
1777575abbcSIngo Weinhold				uint64			stringsLength;
1787575abbcSIngo Weinhold				uint64			stringsCount;
1797575abbcSIngo Weinhold				char**			strings;
1807575abbcSIngo Weinhold				const char*		name;
1817575abbcSIngo Weinhold
1827575abbcSIngo Weinhold				SectionInfo(const char* _name)
1837575abbcSIngo Weinhold					:
1847575abbcSIngo Weinhold					data(NULL),
1857575abbcSIngo Weinhold					strings(NULL),
1867575abbcSIngo Weinhold					name(_name)
1877575abbcSIngo Weinhold				{
1887575abbcSIngo Weinhold				}
1897575abbcSIngo Weinhold
1907575abbcSIngo Weinhold				~SectionInfo()
1917575abbcSIngo Weinhold				{
1927575abbcSIngo Weinhold					delete[] strings;
1937575abbcSIngo Weinhold					delete[] data;
1947575abbcSIngo Weinhold				}
1957575abbcSIngo Weinhold			};
1967575abbcSIngo Weinhold
1977575abbcSIngo Weinhold			typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
1987575abbcSIngo Weinhold
1997575abbcSIngo Weinholdprotected:
2007575abbcSIngo Weinhold			const char*			CheckCompression(
2017575abbcSIngo Weinhold									const SectionInfo& section) const;
2027575abbcSIngo Weinhold
2037575abbcSIngo Weinhold			status_t			ParseStrings();
2047575abbcSIngo Weinhold
2057575abbcSIngo Weinhold			status_t			ParsePackageAttributesSection(
2067575abbcSIngo Weinhold									AttributeHandlerContext* context,
2077575abbcSIngo Weinhold									AttributeHandler* rootAttributeHandler);
2087575abbcSIngo Weinhold			status_t			ParseAttributeTree(
2097575abbcSIngo Weinhold									AttributeHandlerContext* context,
2107575abbcSIngo Weinhold									bool& _sectionHandled);
2117575abbcSIngo Weinhold
2127575abbcSIngo Weinhold	virtual	status_t			ReadAttributeValue(uint8 type, uint8 encoding,
2137575abbcSIngo Weinhold									AttributeValue& _value);
2147575abbcSIngo Weinhold
2157575abbcSIngo Weinhold			status_t			ReadUnsignedLEB128(uint64& _value);
2167575abbcSIngo Weinhold
2177575abbcSIngo Weinhold			status_t			ReadBuffer(off_t offset, void* buffer,
2187575abbcSIngo Weinhold									size_t size);
2197575abbcSIngo Weinhold			status_t			ReadCompressedBuffer(
2207575abbcSIngo Weinhold									const SectionInfo& section);
2217575abbcSIngo Weinhold
2227575abbcSIngo Weinhold	inline	AttributeHandler*	CurrentAttributeHandler() const;
2237575abbcSIngo Weinhold	inline	void				PushAttributeHandler(
2247575abbcSIngo Weinhold									AttributeHandler* handler);
2257575abbcSIngo Weinhold	inline	AttributeHandler*	PopAttributeHandler();
2267575abbcSIngo Weinhold	inline	void				ClearAttributeHandlerStack();
2277575abbcSIngo Weinhold
2287575abbcSIngo Weinhold	inline	SectionInfo*		CurrentSection();
2297575abbcSIngo Weinhold	inline	void				SetCurrentSection(SectionInfo* section);
2307575abbcSIngo Weinhold
2317575abbcSIngo Weinholdprotected:
2327575abbcSIngo Weinhold			SectionInfo			fPackageAttributesSection;
2337575abbcSIngo Weinhold
2347575abbcSIngo Weinholdprivate:
2357575abbcSIngo Weinhold			status_t			_ParseAttributeTree(
2367575abbcSIngo Weinhold									AttributeHandlerContext* context);
2377575abbcSIngo Weinhold
2387575abbcSIngo Weinhold	template<typename Type>
2397575abbcSIngo Weinhold	inline	status_t			_Read(Type& _value);
2407575abbcSIngo Weinhold
2417575abbcSIngo Weinhold			status_t			_ReadSectionBuffer(void* buffer, size_t size);
2427575abbcSIngo Weinhold
2437575abbcSIngo Weinhold			status_t			_ReadAttribute(uint8& _id,
2447575abbcSIngo Weinhold									AttributeValue& _value,
2457575abbcSIngo Weinhold									bool* _hasChildren = NULL,
2467575abbcSIngo Weinhold									uint64* _tag = NULL);
2477575abbcSIngo Weinhold
2487575abbcSIngo Weinhold			status_t			_ReadString(const char*& _string,
2497575abbcSIngo Weinhold									size_t* _stringLength = NULL);
2507575abbcSIngo Weinhold
2517575abbcSIngo Weinholdprivate:
2527575abbcSIngo Weinhold			BErrorOutput*		fErrorOutput;
2537575abbcSIngo Weinhold			int					fFD;
2547575abbcSIngo Weinhold			bool				fOwnsFD;
2557575abbcSIngo Weinhold
2567575abbcSIngo Weinhold			SectionInfo*		fCurrentSection;
2577575abbcSIngo Weinhold
2587575abbcSIngo Weinhold			AttributeHandlerList fAttributeHandlerStack;
2597575abbcSIngo Weinhold
2607575abbcSIngo Weinhold			uint8*				fScratchBuffer;
2617575abbcSIngo Weinhold			size_t				fScratchBufferSize;
2627575abbcSIngo Weinhold};
2637575abbcSIngo Weinhold
2647575abbcSIngo Weinhold
2657575abbcSIngo Weinholdinline int
2667575abbcSIngo WeinholdReaderImplBase::FD() const
2677575abbcSIngo Weinhold{
2687575abbcSIngo Weinhold	return fFD;
2697575abbcSIngo Weinhold}
2707575abbcSIngo Weinhold
2717575abbcSIngo Weinhold
2727575abbcSIngo Weinholdinline BErrorOutput*
2737575abbcSIngo WeinholdReaderImplBase::ErrorOutput() const
2747575abbcSIngo Weinhold{
2757575abbcSIngo Weinhold	return fErrorOutput;
2767575abbcSIngo Weinhold}
2777575abbcSIngo Weinhold
2787575abbcSIngo Weinhold
2797575abbcSIngo WeinholdReaderImplBase::SectionInfo*
2807575abbcSIngo WeinholdReaderImplBase::CurrentSection()
2817575abbcSIngo Weinhold{
2827575abbcSIngo Weinhold	return fCurrentSection;
2837575abbcSIngo Weinhold}
2847575abbcSIngo Weinhold
2857575abbcSIngo Weinhold
2867575abbcSIngo Weinholdvoid
2877575abbcSIngo WeinholdReaderImplBase::SetCurrentSection(SectionInfo* section)
2887575abbcSIngo Weinhold{
2897575abbcSIngo Weinhold	fCurrentSection = section;
2907575abbcSIngo Weinhold}
2917575abbcSIngo Weinhold
2927575abbcSIngo Weinhold
2937575abbcSIngo Weinholdtemplate<typename Type>
2947575abbcSIngo Weinholdstatus_t
2957575abbcSIngo WeinholdReaderImplBase::_Read(Type& _value)
2967575abbcSIngo Weinhold{
2977575abbcSIngo Weinhold	return _ReadSectionBuffer(&_value, sizeof(Type));
2987575abbcSIngo Weinhold}
2997575abbcSIngo Weinhold
3007575abbcSIngo Weinhold
3017575abbcSIngo Weinholdinline ReaderImplBase::AttributeHandler*
3027575abbcSIngo WeinholdReaderImplBase::CurrentAttributeHandler() const
3037575abbcSIngo Weinhold{
3047575abbcSIngo Weinhold	return fAttributeHandlerStack.Head();
3057575abbcSIngo Weinhold}
3067575abbcSIngo Weinhold
3077575abbcSIngo Weinhold
3087575abbcSIngo Weinholdinline void
3097575abbcSIngo WeinholdReaderImplBase::PushAttributeHandler(AttributeHandler* handler)
3107575abbcSIngo Weinhold{
3117575abbcSIngo Weinhold	fAttributeHandlerStack.Add(handler);
3127575abbcSIngo Weinhold}
3137575abbcSIngo Weinhold
3147575abbcSIngo Weinhold
3157575abbcSIngo Weinholdinline ReaderImplBase::AttributeHandler*
3167575abbcSIngo WeinholdReaderImplBase::PopAttributeHandler()
3177575abbcSIngo Weinhold{
3187575abbcSIngo Weinhold	return fAttributeHandlerStack.RemoveHead();
3197575abbcSIngo Weinhold}
3207575abbcSIngo Weinhold
3217575abbcSIngo Weinhold
3227575abbcSIngo Weinholdinline void
3237575abbcSIngo WeinholdReaderImplBase::ClearAttributeHandlerStack()
3247575abbcSIngo Weinhold{
3257575abbcSIngo Weinhold	fAttributeHandlerStack.MakeEmpty();
3267575abbcSIngo Weinhold}
3277575abbcSIngo Weinhold
3287575abbcSIngo Weinhold
3297575abbcSIngo Weinhold}	// namespace BPrivate
3307575abbcSIngo Weinhold
3317575abbcSIngo Weinhold}	// namespace V1
3327575abbcSIngo Weinhold
3337575abbcSIngo Weinhold}	// namespace BHPKG
3347575abbcSIngo Weinhold
3357575abbcSIngo Weinhold}	// namespace BPackageKit
3367575abbcSIngo Weinhold
3377575abbcSIngo Weinhold
3387575abbcSIngo Weinhold#endif	// _PACKAGE__HPKG__V1__PRIVATE__READER_IMPL_BASE_H_
339