2011-02-14 16:09:09 +03:00
|
|
|
/*
|
2013-05-23 23:29:18 +04:00
|
|
|
* Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2011-02-14 16:09:09 +03:00
|
|
|
* Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
|
|
|
|
#define _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ByteOrder.h>
|
2011-02-14 16:09:09 +03:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2013-05-24 05:11:18 +04:00
|
|
|
#include <Array.h>
|
2011-02-14 16:09:09 +03:00
|
|
|
#include <util/SinglyLinkedList.h>
|
|
|
|
|
2013-05-19 12:44:11 +04:00
|
|
|
#include <package/hpkg/ErrorOutput.h>
|
2011-02-14 16:09:09 +03:00
|
|
|
#include <package/hpkg/PackageAttributeValue.h>
|
|
|
|
#include <package/hpkg/PackageContentHandler.h>
|
|
|
|
#include <package/hpkg/PackageInfoAttributeValue.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPackageKit {
|
|
|
|
|
|
|
|
namespace BHPKG {
|
|
|
|
|
|
|
|
|
2013-05-21 21:42:57 +04:00
|
|
|
class BAbstractBufferedDataReader;
|
2011-02-14 16:09:09 +03:00
|
|
|
class BErrorOutput;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
class PackageFileHeapReader;
|
|
|
|
|
|
|
|
|
|
|
|
struct PackageFileSection {
|
|
|
|
uint32 uncompressedLength;
|
|
|
|
uint8* data;
|
|
|
|
uint64 offset;
|
|
|
|
uint64 currentOffset;
|
|
|
|
uint64 stringsLength;
|
|
|
|
uint64 stringsCount;
|
|
|
|
char** strings;
|
|
|
|
const char* name;
|
|
|
|
|
|
|
|
PackageFileSection(const char* _name)
|
|
|
|
:
|
|
|
|
data(NULL),
|
|
|
|
strings(NULL),
|
|
|
|
name(_name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PackageFileSection()
|
|
|
|
{
|
|
|
|
delete[] strings;
|
|
|
|
delete[] data;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-14 16:09:09 +03:00
|
|
|
class ReaderImplBase {
|
2013-10-26 00:39:23 +04:00
|
|
|
public:
|
|
|
|
inline const PackageFileSection& PackageAttributesSection() const
|
|
|
|
{ return fPackageAttributesSection; }
|
|
|
|
|
2011-02-14 16:09:09 +03:00
|
|
|
protected:
|
2013-05-18 06:11:01 +04:00
|
|
|
ReaderImplBase(const char* fileType,
|
2011-02-14 16:09:09 +03:00
|
|
|
BErrorOutput* errorOutput);
|
|
|
|
virtual ~ReaderImplBase();
|
|
|
|
|
|
|
|
int FD() const;
|
|
|
|
|
|
|
|
BErrorOutput* ErrorOutput() const;
|
|
|
|
|
2013-05-22 02:20:13 +04:00
|
|
|
uint16 MinorFormatVersion() const
|
|
|
|
{ return fMinorFormatVersion; }
|
|
|
|
|
2013-05-21 21:42:57 +04:00
|
|
|
uint64 UncompressedHeapSize() const;
|
|
|
|
|
|
|
|
PackageFileHeapReader* RawHeapReader() const
|
|
|
|
{ return fRawHeapReader; }
|
|
|
|
BAbstractBufferedDataReader* HeapReader() const
|
2013-05-18 06:11:01 +04:00
|
|
|
{ return fHeapReader; }
|
2013-05-21 21:42:57 +04:00
|
|
|
// equals RawHeapReader(), if uncached
|
|
|
|
|
|
|
|
BAbstractBufferedDataReader* DetachHeapReader(
|
|
|
|
PackageFileHeapReader** _rawHeapReader
|
|
|
|
= NULL);
|
|
|
|
// Detaches both raw and (if applicable)
|
|
|
|
// cached heap reader. The called gains
|
|
|
|
// ownership. The FD may need to be set on
|
|
|
|
// the raw heap reader, if it shall be used
|
|
|
|
// after destroying this object and Init()
|
|
|
|
// has been called with keepFD == true.
|
2011-02-14 16:09:09 +03:00
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
protected:
|
|
|
|
class AttributeHandlerContext;
|
|
|
|
class AttributeHandler;
|
|
|
|
class IgnoreAttributeHandler;
|
2013-05-23 23:29:18 +04:00
|
|
|
class PackageInfoAttributeHandlerBase;
|
2013-05-18 06:11:01 +04:00
|
|
|
class PackageVersionAttributeHandler;
|
|
|
|
class PackageResolvableAttributeHandler;
|
|
|
|
class PackageResolvableExpressionAttributeHandler;
|
2013-06-06 02:16:28 +04:00
|
|
|
class GlobalWritableFileInfoAttributeHandler;
|
2013-05-23 23:29:18 +04:00
|
|
|
class UserSettingsFileInfoAttributeHandler;
|
2013-05-24 05:11:18 +04:00
|
|
|
class UserAttributeHandler;
|
2013-05-18 06:11:01 +04:00
|
|
|
class PackageAttributeHandler;
|
|
|
|
class LowLevelAttributeHandler;
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
typedef BPackageAttributeValue AttributeValue;
|
|
|
|
typedef SinglyLinkedList<AttributeHandler> AttributeHandlerList;
|
|
|
|
|
|
|
|
protected:
|
2013-05-24 00:59:40 +04:00
|
|
|
template<typename Header, uint32 kMagic, uint16 kVersion,
|
|
|
|
uint16 kMinorVersion>
|
2013-05-20 22:15:59 +04:00
|
|
|
status_t Init(int fd, bool keepFD, Header& header,
|
|
|
|
uint32 flags);
|
2013-05-18 06:11:01 +04:00
|
|
|
status_t InitHeapReader(uint32 compression,
|
|
|
|
uint32 chunkSize, off_t offset,
|
|
|
|
uint64 compressedSize,
|
|
|
|
uint64 uncompressedSize);
|
2013-05-21 21:42:57 +04:00
|
|
|
virtual status_t CreateCachedHeapReader(
|
|
|
|
PackageFileHeapReader* heapReader,
|
|
|
|
BAbstractBufferedDataReader*&
|
|
|
|
_cachedReader);
|
2013-05-18 06:11:01 +04:00
|
|
|
status_t InitSection(PackageFileSection& section,
|
|
|
|
uint64 endOffset, uint64 length,
|
|
|
|
uint64 maxSaneLength, uint64 stringsLength,
|
|
|
|
uint64 stringsCount);
|
|
|
|
status_t PrepareSection(PackageFileSection& section);
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
status_t ParseStrings();
|
|
|
|
|
|
|
|
status_t ParsePackageAttributesSection(
|
|
|
|
AttributeHandlerContext* context,
|
|
|
|
AttributeHandler* rootAttributeHandler);
|
|
|
|
status_t ParseAttributeTree(
|
2011-07-03 06:07:47 +04:00
|
|
|
AttributeHandlerContext* context,
|
|
|
|
bool& _sectionHandled);
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
virtual status_t ReadAttributeValue(uint8 type, uint8 encoding,
|
|
|
|
AttributeValue& _value);
|
|
|
|
|
|
|
|
status_t ReadUnsignedLEB128(uint64& _value);
|
|
|
|
|
|
|
|
status_t ReadBuffer(off_t offset, void* buffer,
|
|
|
|
size_t size);
|
2013-05-18 06:11:01 +04:00
|
|
|
status_t ReadSection(const PackageFileSection& section);
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
inline AttributeHandler* CurrentAttributeHandler() const;
|
|
|
|
inline void PushAttributeHandler(
|
|
|
|
AttributeHandler* handler);
|
|
|
|
inline AttributeHandler* PopAttributeHandler();
|
|
|
|
inline void ClearAttributeHandlerStack();
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
inline PackageFileSection* CurrentSection();
|
|
|
|
inline void SetCurrentSection(PackageFileSection* section);
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
protected:
|
2013-05-18 06:11:01 +04:00
|
|
|
PackageFileSection fPackageAttributesSection;
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
private:
|
2013-05-18 06:11:01 +04:00
|
|
|
status_t _Init(int fd, bool keepFD);
|
|
|
|
|
2011-07-03 06:07:47 +04:00
|
|
|
status_t _ParseAttributeTree(
|
|
|
|
AttributeHandlerContext* context);
|
|
|
|
|
2011-02-14 16:09:09 +03:00
|
|
|
template<typename Type>
|
|
|
|
inline status_t _Read(Type& _value);
|
|
|
|
|
|
|
|
status_t _ReadSectionBuffer(void* buffer, size_t size);
|
|
|
|
|
|
|
|
status_t _ReadAttribute(uint8& _id,
|
|
|
|
AttributeValue& _value,
|
|
|
|
bool* _hasChildren = NULL,
|
|
|
|
uint64* _tag = NULL);
|
|
|
|
|
|
|
|
status_t _ReadString(const char*& _string,
|
|
|
|
size_t* _stringLength = NULL);
|
|
|
|
|
|
|
|
private:
|
2013-05-18 06:11:01 +04:00
|
|
|
const char* fFileType;
|
2011-02-14 16:09:09 +03:00
|
|
|
BErrorOutput* fErrorOutput;
|
|
|
|
int fFD;
|
|
|
|
bool fOwnsFD;
|
2013-05-22 02:20:13 +04:00
|
|
|
uint16 fMinorFormatVersion;
|
2013-05-24 00:59:40 +04:00
|
|
|
uint16 fCurrentMinorFormatVersion;
|
2011-02-14 16:09:09 +03:00
|
|
|
|
2013-05-21 21:42:57 +04:00
|
|
|
PackageFileHeapReader* fRawHeapReader;
|
|
|
|
BAbstractBufferedDataReader* fHeapReader;
|
2013-05-18 06:11:01 +04:00
|
|
|
|
|
|
|
PackageFileSection* fCurrentSection;
|
2011-02-14 16:09:09 +03:00
|
|
|
|
|
|
|
AttributeHandlerList fAttributeHandlerStack;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
// #pragma mark - attribute handlers
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::AttributeHandlerContext {
|
|
|
|
public:
|
|
|
|
BErrorOutput* errorOutput;
|
|
|
|
union {
|
|
|
|
BPackageContentHandler* packageContentHandler;
|
|
|
|
BLowLevelPackageContentHandler* lowLevelHandler;
|
|
|
|
};
|
|
|
|
bool hasLowLevelHandler;
|
2013-05-22 02:20:13 +04:00
|
|
|
bool ignoreUnknownAttributes;
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
BHPKGPackageSectionID section;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AttributeHandlerContext(
|
|
|
|
BErrorOutput* errorOutput,
|
|
|
|
BPackageContentHandler*
|
|
|
|
packageContentHandler,
|
2013-05-22 02:20:13 +04:00
|
|
|
BHPKGPackageSectionID section,
|
|
|
|
bool ignoreUnknownAttributes);
|
2013-05-18 06:11:01 +04:00
|
|
|
AttributeHandlerContext(
|
|
|
|
BErrorOutput* errorOutput,
|
|
|
|
BLowLevelPackageContentHandler*
|
|
|
|
lowLevelHandler,
|
2013-05-22 02:20:13 +04:00
|
|
|
BHPKGPackageSectionID section,
|
|
|
|
bool ignoreUnknownAttributes);
|
2013-05-18 06:11:01 +04:00
|
|
|
|
|
|
|
void ErrorOccurred();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::AttributeHandler
|
|
|
|
: public SinglyLinkedListLinkImpl<AttributeHandler> {
|
|
|
|
public:
|
|
|
|
virtual ~AttributeHandler();
|
|
|
|
|
|
|
|
void SetLevel(int level);
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
|
2013-09-18 22:20:43 +04:00
|
|
|
virtual status_t NotifyDone(AttributeHandlerContext* context);
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
virtual status_t Delete(AttributeHandlerContext* context);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int fLevel;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::IgnoreAttributeHandler : public AttributeHandler {
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-23 23:29:18 +04:00
|
|
|
class ReaderImplBase::PackageInfoAttributeHandlerBase
|
|
|
|
: public AttributeHandler {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef AttributeHandler super;
|
2013-05-23 23:29:18 +04:00
|
|
|
public:
|
|
|
|
PackageInfoAttributeHandlerBase(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
2013-09-18 22:20:43 +04:00
|
|
|
virtual status_t NotifyDone(AttributeHandlerContext* context);
|
2013-05-23 23:29:18 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
BPackageInfoAttributeValue& fPackageInfoValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::PackageVersionAttributeHandler
|
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-18 06:11:01 +04:00
|
|
|
public:
|
|
|
|
PackageVersionAttributeHandler(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue,
|
|
|
|
BPackageVersionData& versionData,
|
|
|
|
bool notify);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
|
2013-09-18 22:20:43 +04:00
|
|
|
virtual status_t NotifyDone(AttributeHandlerContext* context);
|
2013-05-18 06:11:01 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
BPackageVersionData& fPackageVersionData;
|
|
|
|
bool fNotify;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::PackageResolvableAttributeHandler
|
2013-05-23 23:29:18 +04:00
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-18 06:11:01 +04:00
|
|
|
public:
|
|
|
|
PackageResolvableAttributeHandler(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::PackageResolvableExpressionAttributeHandler
|
2013-05-23 23:29:18 +04:00
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-18 06:11:01 +04:00
|
|
|
public:
|
|
|
|
PackageResolvableExpressionAttributeHandler(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
2013-05-23 23:29:18 +04:00
|
|
|
};
|
2013-05-18 06:11:01 +04:00
|
|
|
|
|
|
|
|
2013-06-06 02:16:28 +04:00
|
|
|
class ReaderImplBase::GlobalWritableFileInfoAttributeHandler
|
2013-05-23 23:29:18 +04:00
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-23 23:29:18 +04:00
|
|
|
public:
|
2013-06-06 02:16:28 +04:00
|
|
|
GlobalWritableFileInfoAttributeHandler(
|
2013-05-23 23:29:18 +04:00
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::UserSettingsFileInfoAttributeHandler
|
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-23 23:29:18 +04:00
|
|
|
public:
|
|
|
|
UserSettingsFileInfoAttributeHandler(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
2013-05-18 06:11:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-24 05:11:18 +04:00
|
|
|
class ReaderImplBase::UserAttributeHandler
|
|
|
|
: public PackageInfoAttributeHandlerBase {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef PackageInfoAttributeHandlerBase super;
|
2013-05-24 05:11:18 +04:00
|
|
|
public:
|
|
|
|
UserAttributeHandler(
|
|
|
|
BPackageInfoAttributeValue&
|
|
|
|
packageInfoValue);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
|
2013-09-18 22:20:43 +04:00
|
|
|
virtual status_t NotifyDone(AttributeHandlerContext* context);
|
2013-05-24 05:11:18 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
Array<const char*> fGroups;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
class ReaderImplBase::PackageAttributeHandler : public AttributeHandler {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef AttributeHandler super;
|
2013-05-18 06:11:01 +04:00
|
|
|
public:
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
|
|
|
|
|
|
|
private:
|
|
|
|
BPackageInfoAttributeValue fPackageInfoValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ReaderImplBase::LowLevelAttributeHandler : public AttributeHandler {
|
2013-09-18 22:20:43 +04:00
|
|
|
private:
|
|
|
|
typedef AttributeHandler super;
|
2013-05-18 06:11:01 +04:00
|
|
|
public:
|
|
|
|
LowLevelAttributeHandler();
|
|
|
|
LowLevelAttributeHandler(uint8 id,
|
|
|
|
const BPackageAttributeValue& value,
|
|
|
|
void* parentToken, void* token);
|
|
|
|
|
|
|
|
virtual status_t HandleAttribute(
|
|
|
|
AttributeHandlerContext* context, uint8 id,
|
|
|
|
const AttributeValue& value,
|
|
|
|
AttributeHandler** _handler);
|
2013-09-18 22:20:43 +04:00
|
|
|
|
|
|
|
virtual status_t NotifyDone(AttributeHandlerContext* context);
|
2013-05-18 06:11:01 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void* fParentToken;
|
|
|
|
void* fToken;
|
|
|
|
uint8 fID;
|
|
|
|
AttributeValue fValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// #pragma mark - template and inline methods
|
|
|
|
|
|
|
|
|
2013-05-24 00:59:40 +04:00
|
|
|
template<typename Header, uint32 kMagic, uint16 kVersion, uint16 kMinorVersion>
|
2013-05-18 06:11:01 +04:00
|
|
|
status_t
|
2013-05-20 22:15:59 +04:00
|
|
|
ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags)
|
2013-05-18 06:11:01 +04:00
|
|
|
{
|
|
|
|
status_t error = _Init(fd, keepFD);
|
|
|
|
if (error != B_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
// stat the file
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(FD(), &st) < 0) {
|
|
|
|
ErrorOutput()->PrintError("Error: Failed to access %s file: %s\n",
|
|
|
|
fFileType, strerror(errno));
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the header
|
|
|
|
if ((error = ReadBuffer(0, &header, sizeof(header))) != B_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
// check the header
|
|
|
|
|
|
|
|
// magic
|
|
|
|
if (B_BENDIAN_TO_HOST_INT32(header.magic) != kMagic) {
|
|
|
|
ErrorOutput()->PrintError("Error: Invalid %s file: Invalid "
|
|
|
|
"magic\n", fFileType);
|
|
|
|
return B_BAD_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// version
|
|
|
|
if (B_BENDIAN_TO_HOST_INT16(header.version) != kVersion) {
|
2013-05-20 22:15:59 +04:00
|
|
|
if ((flags & B_HPKG_READER_DONT_PRINT_VERSION_MISMATCH_MESSAGE) == 0) {
|
|
|
|
ErrorOutput()->PrintError("Error: Invalid/unsupported %s file "
|
|
|
|
"version (%d)\n", fFileType,
|
|
|
|
B_BENDIAN_TO_HOST_INT16(header.version));
|
|
|
|
}
|
2013-05-18 06:11:01 +04:00
|
|
|
return B_MISMATCHED_VALUES;
|
|
|
|
}
|
|
|
|
|
2013-05-22 02:20:13 +04:00
|
|
|
fMinorFormatVersion = B_BENDIAN_TO_HOST_INT16(header.minor_version);
|
2013-05-24 00:59:40 +04:00
|
|
|
fCurrentMinorFormatVersion = kMinorVersion;
|
2013-05-22 02:20:13 +04:00
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
// header size
|
|
|
|
uint64 heapOffset = B_BENDIAN_TO_HOST_INT16(header.header_size);
|
|
|
|
if (heapOffset < (off_t)sizeof(header)) {
|
|
|
|
ErrorOutput()->PrintError("Error: Invalid %s file: Invalid header "
|
|
|
|
"size (%" B_PRIu64 ")\n", fFileType, heapOffset);
|
|
|
|
return B_BAD_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// total size
|
|
|
|
uint64 totalSize = B_BENDIAN_TO_HOST_INT64(header.total_size);
|
|
|
|
if (totalSize != (uint64)st.st_size) {
|
|
|
|
ErrorOutput()->PrintError("Error: Invalid %s file: Total size in "
|
|
|
|
"header (%" B_PRIu64 ") doesn't agree with total file size (%"
|
|
|
|
B_PRIdOFF ")\n", fFileType, totalSize, st.st_size);
|
|
|
|
return B_BAD_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// heap size
|
|
|
|
uint64 compressedHeapSize
|
|
|
|
= B_BENDIAN_TO_HOST_INT64(header.heap_size_compressed);
|
|
|
|
if (compressedHeapSize > totalSize
|
|
|
|
|| heapOffset > totalSize - compressedHeapSize) {
|
|
|
|
ErrorOutput()->PrintError("Error: Invalid %s file: Heap size in "
|
|
|
|
"header (%" B_PRIu64 ") doesn't agree with total file size (%"
|
|
|
|
B_PRIu64 ") and heap offset (%" B_PRIu64 ")\n", fFileType,
|
|
|
|
compressedHeapSize, totalSize, heapOffset);
|
|
|
|
return B_BAD_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = InitHeapReader(
|
2013-05-22 02:20:13 +04:00
|
|
|
B_BENDIAN_TO_HOST_INT16(header.heap_compression),
|
2013-05-18 06:11:01 +04:00
|
|
|
B_BENDIAN_TO_HOST_INT32(header.heap_chunk_size), heapOffset,
|
|
|
|
compressedHeapSize,
|
|
|
|
B_BENDIAN_TO_HOST_INT64(header.heap_size_uncompressed));
|
|
|
|
if (error != B_OK)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-14 16:09:09 +03:00
|
|
|
inline int
|
|
|
|
ReaderImplBase::FD() const
|
|
|
|
{
|
|
|
|
return fFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline BErrorOutput*
|
|
|
|
ReaderImplBase::ErrorOutput() const
|
|
|
|
{
|
|
|
|
return fErrorOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
PackageFileSection*
|
2011-02-14 16:09:09 +03:00
|
|
|
ReaderImplBase::CurrentSection()
|
|
|
|
{
|
|
|
|
return fCurrentSection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-05-18 06:11:01 +04:00
|
|
|
ReaderImplBase::SetCurrentSection(PackageFileSection* section)
|
2011-02-14 16:09:09 +03:00
|
|
|
{
|
|
|
|
fCurrentSection = section;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
status_t
|
|
|
|
ReaderImplBase::_Read(Type& _value)
|
|
|
|
{
|
|
|
|
return _ReadSectionBuffer(&_value, sizeof(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline ReaderImplBase::AttributeHandler*
|
|
|
|
ReaderImplBase::CurrentAttributeHandler() const
|
|
|
|
{
|
|
|
|
return fAttributeHandlerStack.Head();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ReaderImplBase::PushAttributeHandler(AttributeHandler* handler)
|
|
|
|
{
|
|
|
|
fAttributeHandlerStack.Add(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline ReaderImplBase::AttributeHandler*
|
|
|
|
ReaderImplBase::PopAttributeHandler()
|
|
|
|
{
|
|
|
|
return fAttributeHandlerStack.RemoveHead();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ReaderImplBase::ClearAttributeHandlerStack()
|
|
|
|
{
|
|
|
|
fAttributeHandlerStack.MakeEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
} // namespace BHPKG
|
|
|
|
|
|
|
|
} // namespace BPackageKit
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _PACKAGE__HPKG__PRIVATE__READER_IMPL_BASE_H_
|