1 /* 2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <package/hpkg/DataReader.h> 8 9 #include <package/hpkg/DataOutput.h> 10 11 #include <string.h> 12 13 14 namespace BPackageKit { 15 16 namespace BHPKG { 17 18 19 // #pragma mark - BDataReader 20 21 22 BDataReader::~BDataReader() 23 { 24 } 25 26 27 // #pragma mark - BAbstractBufferedDataReader 28 29 30 BAbstractBufferedDataReader::~BAbstractBufferedDataReader() 31 { 32 } 33 34 35 status_t 36 BAbstractBufferedDataReader::ReadData(off_t offset, void* buffer, size_t size) 37 { 38 BBufferDataOutput output(buffer, size); 39 return ReadDataToOutput(offset, size, &output); 40 } 41 42 43 // #pragma mark - BBufferDataReader 44 45 46 BBufferDataReader::BBufferDataReader(const void* data, size_t size) 47 : 48 fData(data), 49 fSize(size) 50 { 51 } 52 53 54 status_t 55 BBufferDataReader::ReadData(off_t offset, void* buffer, size_t size) 56 { 57 if (size == 0) 58 return B_OK; 59 60 if (offset < 0) 61 return B_BAD_VALUE; 62 63 if (size > fSize || offset > (off_t)fSize - (off_t)size) 64 return B_ERROR; 65 66 memcpy(buffer, (const uint8*)fData + offset, size); 67 return B_OK; 68 } 69 70 71 status_t 72 BBufferDataReader::ReadDataToOutput(off_t offset, size_t size, 73 BDataOutput* output) 74 { 75 if (size == 0) 76 return B_OK; 77 78 if (offset < 0) 79 return B_BAD_VALUE; 80 81 if (size > fSize || offset > (off_t)fSize - (off_t)size) 82 return B_ERROR; 83 84 return output->WriteData((const uint8*)fData + offset, size); 85 } 86 87 88 } // namespace BHPKG 89 90 } // namespace BPackageKit 91