1 /* 2 * Copyright 2009-2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <package/hpkg/PackageDataReader.h> 8 9 #include <string.h> 10 11 #include <algorithm> 12 #include <new> 13 14 #include <package/hpkg/HPKGDefsPrivate.h> 15 #include <package/hpkg/PackageData.h> 16 #include <package/hpkg/ZlibDecompressor.h> 17 18 19 namespace BPackageKit { 20 21 namespace BHPKG { 22 23 24 using namespace BPrivate; 25 26 27 // #pragma mark - PackageDataHeapReader 28 29 30 class PackageDataHeapReader : public BAbstractBufferedDataReader { 31 public: 32 PackageDataHeapReader(BAbstractBufferedDataReader* dataReader, 33 const BPackageData& data) 34 : 35 fDataReader(dataReader), 36 fOffset(data.Offset()), 37 fSize(data.Size()) 38 { 39 } 40 41 virtual status_t ReadData(off_t offset, void* buffer, size_t size) 42 { 43 if (size == 0) 44 return B_OK; 45 46 if (offset < 0) 47 return B_BAD_VALUE; 48 49 if ((uint64)offset > fSize || size > fSize - offset) 50 return B_BAD_VALUE; 51 52 return fDataReader->ReadData(fOffset + offset, buffer, size); 53 } 54 55 virtual status_t ReadDataToOutput(off_t offset, size_t size, 56 BDataOutput* output) 57 { 58 if (size == 0) 59 return B_OK; 60 61 if (offset < 0) 62 return B_BAD_VALUE; 63 64 if ((uint64)offset > fSize || size > fSize - offset) 65 return B_BAD_VALUE; 66 67 return fDataReader->ReadDataToOutput(fOffset + offset, size, output); 68 } 69 70 private: 71 BAbstractBufferedDataReader* fDataReader; 72 uint64 fOffset; 73 uint64 fSize; 74 }; 75 76 77 // #pragma mark - PackageDataHeapReader 78 79 80 class PackageDataInlineReader : public BBufferDataReader { 81 public: 82 PackageDataInlineReader(const BPackageData& data) 83 : 84 BBufferDataReader(fData.InlineData(), data.Size()), 85 fData(data) 86 { 87 } 88 89 private: 90 BPackageData fData; 91 }; 92 93 94 // #pragma mark - BPackageDataReaderFactory 95 96 97 BPackageDataReaderFactory::BPackageDataReaderFactory() 98 { 99 } 100 101 102 status_t 103 BPackageDataReaderFactory::CreatePackageDataReader( 104 BAbstractBufferedDataReader* dataReader, const BPackageData& data, 105 BAbstractBufferedDataReader*& _reader) 106 { 107 BAbstractBufferedDataReader* reader; 108 if (data.IsEncodedInline()) 109 reader = new(std::nothrow) PackageDataInlineReader(data); 110 else 111 reader = new(std::nothrow) PackageDataHeapReader(dataReader, data); 112 113 if (reader == NULL) 114 return B_NO_MEMORY; 115 116 _reader = reader; 117 return B_OK; 118 } 119 120 121 } // namespace BHPKG 122 123 } // namespace BPackageKit 124