1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef PACKAGE_DATA_H 6 #define PACKAGE_DATA_H 7 8 9 #include <package/hpkg/PackageData.h> 10 #include <package/hpkg/v1/PackageData.h> 11 12 #include <string.h> 13 14 15 typedef BPackageKit::BHPKG::BPackageData PackageDataV2; 16 typedef BPackageKit::BHPKG::V1::BPackageData PackageDataV1; 17 18 19 class PackageData { 20 public: 21 explicit PackageData(const PackageDataV1& data); 22 explicit PackageData(const PackageDataV2& data); 23 24 uint8 Version() const { return fVersion; } 25 const PackageDataV1& DataV1() const; 26 const PackageDataV2& DataV2() const; 27 28 uint64 CompressedSize() const; 29 uint64 UncompressedSize() const; 30 31 bool IsEncodedInline() const; 32 const uint8* InlineData() const; 33 34 private: 35 static const size_t kDataSize = sizeof(PackageDataV1) 36 > sizeof(PackageDataV2) 37 ? sizeof(PackageDataV1) 38 : sizeof(PackageDataV2); 39 40 private: 41 union { 42 char fData[kDataSize]; 43 uint64 fAlignmentDummy; 44 }; 45 uint8 fVersion; 46 }; 47 48 49 inline 50 PackageData::PackageData(const PackageDataV1& data) 51 : 52 fVersion(1) 53 { 54 memcpy(&fData, &data, sizeof(data)); 55 } 56 57 58 inline 59 PackageData::PackageData(const PackageDataV2& data) 60 : 61 fVersion(2) 62 { 63 memcpy(&fData, &data, sizeof(data)); 64 } 65 66 67 inline const PackageDataV1& 68 PackageData::DataV1() const 69 { 70 return *(PackageDataV1*)&fData; 71 } 72 73 74 inline const PackageDataV2& 75 PackageData::DataV2() const 76 { 77 return *(PackageDataV2*)&fData; 78 } 79 80 81 inline uint64 82 PackageData::CompressedSize() const 83 { 84 if (fVersion == 1) 85 return DataV1().CompressedSize(); 86 return DataV2().Size(); 87 } 88 89 90 inline uint64 91 PackageData::UncompressedSize() const 92 { 93 if (fVersion == 1) 94 return DataV1().UncompressedSize(); 95 return DataV2().Size(); 96 } 97 98 99 inline bool 100 PackageData::IsEncodedInline() const 101 { 102 if (fVersion == 1) 103 return DataV1().IsEncodedInline(); 104 return DataV2().IsEncodedInline(); 105 } 106 107 108 inline const uint8* 109 PackageData::InlineData() const 110 { 111 if (fVersion == 1) 112 return DataV1().InlineData(); 113 return DataV2().InlineData(); 114 } 115 116 117 #endif // PACKAGE_DATA_H 118