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