2011-02-08 01:25:16 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|
|
|
|
#define _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
|
2011-02-12 22:21:37 +03:00
|
|
|
#include <package/hpkg/HPKGDefsPrivate.h>
|
2011-02-08 01:25:16 +03:00
|
|
|
|
2013-05-19 03:07:08 +04:00
|
|
|
#include <package/hpkg/PackageWriter.h>
|
2011-02-08 01:25:16 +03:00
|
|
|
#include <package/hpkg/Strings.h>
|
|
|
|
|
|
|
|
#include <package/PackageInfo.h>
|
|
|
|
|
2014-06-30 23:35:03 +04:00
|
|
|
#include <package/hpkg/PackageFileHeapWriter.h>
|
|
|
|
|
|
|
|
|
|
|
|
class BCompressionAlgorithm;
|
|
|
|
class BCompressionParameters;
|
|
|
|
class BDecompressionParameters;
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
|
|
|
|
namespace BPackageKit {
|
|
|
|
|
|
|
|
namespace BHPKG {
|
|
|
|
|
|
|
|
|
|
|
|
class BDataReader;
|
|
|
|
class BErrorOutput;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
|
|
|
struct hpkg_header;
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
class WriterImplBase {
|
|
|
|
public:
|
2013-05-18 06:11:01 +04:00
|
|
|
WriterImplBase(const char* fileType,
|
|
|
|
BErrorOutput* errorOutput);
|
2011-02-08 01:25:16 +03:00
|
|
|
~WriterImplBase();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
struct AttributeValue {
|
|
|
|
union {
|
|
|
|
int64 signedInt;
|
|
|
|
uint64 unsignedInt;
|
|
|
|
CachedString* string;
|
|
|
|
struct {
|
|
|
|
uint64 size;
|
|
|
|
union {
|
|
|
|
uint64 offset;
|
|
|
|
uint8 raw[B_HPKG_MAX_INLINE_DATA_SIZE];
|
|
|
|
};
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
uint8 type;
|
|
|
|
int8 encoding;
|
|
|
|
|
|
|
|
AttributeValue();
|
|
|
|
~AttributeValue();
|
|
|
|
|
|
|
|
void SetTo(int8 value);
|
|
|
|
void SetTo(uint8 value);
|
|
|
|
void SetTo(int16 value);
|
|
|
|
void SetTo(uint16 value);
|
|
|
|
void SetTo(int32 value);
|
|
|
|
void SetTo(uint32 value);
|
|
|
|
void SetTo(int64 value);
|
|
|
|
void SetTo(uint64 value);
|
|
|
|
void SetTo(CachedString* value);
|
|
|
|
void SetToData(uint64 size, uint64 offset);
|
|
|
|
void SetToData(uint64 size, const void* rawData);
|
|
|
|
uint8 ApplicableEncoding() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static uint8 _ApplicableIntEncoding(uint64 value);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PackageAttribute :
|
|
|
|
public DoublyLinkedListLinkImpl<PackageAttribute>,
|
|
|
|
public AttributeValue {
|
2011-02-12 22:21:37 +03:00
|
|
|
BHPKGAttributeID id;
|
2011-02-08 01:25:16 +03:00
|
|
|
DoublyLinkedList<PackageAttribute> children;
|
|
|
|
|
2011-02-12 22:21:37 +03:00
|
|
|
PackageAttribute(BHPKGAttributeID id_, uint8 type,
|
2011-02-08 01:25:16 +03:00
|
|
|
uint8 encoding);
|
|
|
|
~PackageAttribute();
|
|
|
|
|
|
|
|
void AddChild(PackageAttribute* child);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _DeleteChildren();
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DoublyLinkedList<PackageAttribute> PackageAttributeList;
|
|
|
|
|
|
|
|
protected:
|
2013-07-04 18:37:24 +04:00
|
|
|
status_t Init(const char* fileName, size_t headerSize,
|
2013-05-19 03:07:08 +04:00
|
|
|
const BPackageWriterParameters& parameters);
|
2011-02-08 01:25:16 +03:00
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
void RegisterPackageInfo(
|
|
|
|
PackageAttributeList& attributeList,
|
|
|
|
const BPackageInfo& packageInfo);
|
|
|
|
void RegisterPackageVersion(
|
|
|
|
PackageAttributeList& attributeList,
|
2011-06-27 05:57:25 +04:00
|
|
|
const BPackageVersion& version,
|
|
|
|
BHPKGAttributeID attributeID
|
|
|
|
= kDefaultVersionAttributeID);
|
2011-02-08 13:59:38 +03:00
|
|
|
void RegisterPackageResolvableExpressionList(
|
|
|
|
PackageAttributeList& attributeList,
|
|
|
|
const BObjectList<
|
|
|
|
BPackageResolvableExpression>& list,
|
|
|
|
uint8 id);
|
|
|
|
|
2013-07-04 21:20:14 +04:00
|
|
|
PackageAttribute* AddStringAttribute(BHPKGAttributeID id,
|
|
|
|
const BString& value,
|
|
|
|
DoublyLinkedList<PackageAttribute>& list);
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
int32 WriteCachedStrings(const StringCache& cache,
|
|
|
|
uint32 minUsageCount);
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
int32 WritePackageAttributes(
|
|
|
|
const PackageAttributeList& attributes,
|
|
|
|
uint32& _stringsLengthUncompressed);
|
2011-02-08 01:25:16 +03:00
|
|
|
void WritePackageVersion(
|
|
|
|
const BPackageVersion& version);
|
|
|
|
void WritePackageResolvableExpressionList(
|
|
|
|
const BObjectList<
|
|
|
|
BPackageResolvableExpression>& list,
|
|
|
|
uint8 id);
|
|
|
|
|
|
|
|
void WriteAttributeValue(const AttributeValue& value,
|
|
|
|
uint8 encoding);
|
|
|
|
void WriteUnsignedLEB128(uint64 value);
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void Write(const Type& value);
|
2013-05-18 06:11:01 +04:00
|
|
|
inline void WriteString(const char* string);
|
|
|
|
inline void WriteBuffer(const void* data, size_t size);
|
|
|
|
// appends data to the heap
|
2011-02-08 01:25:16 +03:00
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
void RawWriteBuffer(const void* buffer, size_t size,
|
2011-02-08 01:25:16 +03:00
|
|
|
off_t offset);
|
2013-05-18 06:11:01 +04:00
|
|
|
// writes to the file directly
|
2011-02-08 01:25:16 +03:00
|
|
|
|
2011-02-14 23:25:10 +03:00
|
|
|
inline int FD() const;
|
2011-07-03 06:26:54 +04:00
|
|
|
inline uint32 Flags() const;
|
2011-02-08 13:59:38 +03:00
|
|
|
|
2011-02-14 23:25:10 +03:00
|
|
|
inline const PackageAttributeList& PackageAttributes() const;
|
|
|
|
inline PackageAttributeList& PackageAttributes();
|
|
|
|
|
|
|
|
inline const StringCache& PackageStringCache() const;
|
|
|
|
inline StringCache& PackageStringCache();
|
|
|
|
|
|
|
|
inline void SetFinished(bool finished);
|
2011-02-08 13:59:38 +03:00
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
protected:
|
|
|
|
PackageFileHeapWriter* fHeapWriter;
|
2014-06-30 23:35:03 +04:00
|
|
|
BCompressionAlgorithm* fCompressionAlgorithm;
|
|
|
|
BCompressionParameters* fCompressionParameters;
|
|
|
|
BCompressionAlgorithm* fDecompressionAlgorithm;
|
|
|
|
BDecompressionParameters* fDecompressionParameters;
|
2013-05-18 06:11:01 +04:00
|
|
|
|
2011-06-27 05:57:25 +04:00
|
|
|
private:
|
|
|
|
static const BHPKGAttributeID kDefaultVersionAttributeID
|
|
|
|
= B_HPKG_ATTRIBUTE_ID_PACKAGE_VERSION_MAJOR;
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
private:
|
2013-05-24 05:11:18 +04:00
|
|
|
inline PackageAttribute* _AddStringAttributeIfNotEmpty(
|
|
|
|
BHPKGAttributeID id, const BString& value,
|
|
|
|
DoublyLinkedList<PackageAttribute>& list);
|
2013-05-25 02:20:55 +04:00
|
|
|
void _AddStringAttributeList(BHPKGAttributeID id,
|
|
|
|
const BStringList& value,
|
|
|
|
DoublyLinkedList<PackageAttribute>& list);
|
2011-02-08 13:59:38 +03:00
|
|
|
void _WritePackageAttributes(
|
|
|
|
const PackageAttributeList& attributes);
|
|
|
|
|
|
|
|
private:
|
2013-05-18 06:11:01 +04:00
|
|
|
const char* fFileType;
|
2011-02-08 01:25:16 +03:00
|
|
|
BErrorOutput* fErrorOutput;
|
|
|
|
const char* fFileName;
|
2013-05-19 03:07:08 +04:00
|
|
|
BPackageWriterParameters fParameters;
|
2011-02-08 01:25:16 +03:00
|
|
|
int fFD;
|
|
|
|
bool fFinished;
|
|
|
|
|
|
|
|
StringCache fPackageStringCache;
|
2011-02-08 13:59:38 +03:00
|
|
|
PackageAttributeList fPackageAttributes;
|
2011-02-08 01:25:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-05-18 06:11:01 +04:00
|
|
|
template<typename Type>
|
2011-02-08 01:25:16 +03:00
|
|
|
inline void
|
2013-05-18 06:11:01 +04:00
|
|
|
WriterImplBase::Write(const Type& value)
|
2011-02-08 01:25:16 +03:00
|
|
|
{
|
2013-05-18 06:11:01 +04:00
|
|
|
WriteBuffer(&value, sizeof(Type));
|
2011-02-08 01:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
2013-05-18 06:11:01 +04:00
|
|
|
WriterImplBase::WriteString(const char* string)
|
2011-02-08 01:25:16 +03:00
|
|
|
{
|
2013-05-18 06:11:01 +04:00
|
|
|
WriteBuffer(string, strlen(string) + 1);
|
2011-02-08 01:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
2013-05-18 06:11:01 +04:00
|
|
|
WriterImplBase::WriteBuffer(const void* data, size_t size)
|
2011-02-08 01:25:16 +03:00
|
|
|
{
|
2014-06-24 00:55:16 +04:00
|
|
|
fHeapWriter->AddDataThrows(data, size);
|
2011-02-08 01:25:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
inline int
|
|
|
|
WriterImplBase::FD() const
|
|
|
|
{
|
|
|
|
return fFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-03 06:26:54 +04:00
|
|
|
inline uint32
|
|
|
|
WriterImplBase::Flags() const
|
|
|
|
{
|
2013-05-19 03:07:08 +04:00
|
|
|
return fParameters.Flags();
|
2011-07-03 06:26:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
inline const WriterImplBase::PackageAttributeList&
|
|
|
|
WriterImplBase::PackageAttributes() const
|
|
|
|
{
|
|
|
|
return fPackageAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline WriterImplBase::PackageAttributeList&
|
|
|
|
WriterImplBase::PackageAttributes()
|
|
|
|
{
|
|
|
|
return fPackageAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-14 23:25:10 +03:00
|
|
|
inline const StringCache&
|
|
|
|
WriterImplBase::PackageStringCache() const
|
|
|
|
{
|
|
|
|
return fPackageStringCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline StringCache&
|
|
|
|
WriterImplBase::PackageStringCache()
|
|
|
|
{
|
|
|
|
return fPackageStringCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
inline void
|
|
|
|
WriterImplBase::SetFinished(bool finished)
|
|
|
|
{
|
|
|
|
fFinished = finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-24 05:11:18 +04:00
|
|
|
inline WriterImplBase::PackageAttribute*
|
|
|
|
WriterImplBase::_AddStringAttributeIfNotEmpty(BHPKGAttributeID id,
|
|
|
|
const BString& value, DoublyLinkedList<PackageAttribute>& list)
|
|
|
|
{
|
|
|
|
if (value.IsEmpty())
|
|
|
|
return NULL;
|
2013-07-04 21:20:14 +04:00
|
|
|
return AddStringAttribute(id, value, list);
|
2013-05-24 05:11:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
} // namespace BHPKG
|
|
|
|
|
|
|
|
} // namespace BPackageKit
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|