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/DataReader.h> 8 9 #include <DataIO.h> 10 11 #include <string.h> 12 13 #if defined(_KERNEL_MODE) && !defined(_BOOT_MODE) 14 // for user_memcpy() and IS_USER_ADDRESS() 15 #include <KernelExport.h> 16 17 #include <kernel.h> 18 #endif 19 20 21 namespace BPackageKit { 22 23 namespace BHPKG { 24 25 26 // #pragma mark - BDataReader 27 28 29 BDataReader::~BDataReader() 30 { 31 } 32 33 34 // #pragma mark - BAbstractBufferedDataReader 35 36 37 BAbstractBufferedDataReader::~BAbstractBufferedDataReader() 38 { 39 } 40 41 42 status_t 43 BAbstractBufferedDataReader::ReadData(off_t offset, void* buffer, size_t size) 44 { 45 BMemoryIO output(buffer, size); 46 return ReadDataToOutput(offset, size, &output); 47 } 48 49 50 // #pragma mark - BBufferDataReader 51 52 53 BBufferDataReader::BBufferDataReader(const void* data, size_t size) 54 : 55 fData(data), 56 fSize(size) 57 { 58 } 59 60 61 status_t 62 BBufferDataReader::ReadData(off_t offset, void* buffer, size_t size) 63 { 64 if (size == 0) 65 return B_OK; 66 67 if (offset < 0) 68 return B_BAD_VALUE; 69 70 if (size > fSize || offset > (off_t)fSize - (off_t)size) 71 return B_ERROR; 72 73 #if defined(_KERNEL_MODE) && !defined(_BOOT_MODE) 74 if (IS_USER_ADDRESS(buffer)) { 75 if (user_memcpy(buffer, (const uint8*)fData + offset, size) != B_OK) 76 return B_BAD_ADDRESS; 77 } else 78 #endif 79 memcpy(buffer, (const uint8*)fData + offset, size); 80 return B_OK; 81 } 82 83 84 status_t 85 BBufferDataReader::ReadDataToOutput(off_t offset, size_t size, BDataIO* output) 86 { 87 if (size == 0) 88 return B_OK; 89 90 if (offset < 0) 91 return B_BAD_VALUE; 92 93 if (size > fSize || offset > (off_t)fSize - (off_t)size) 94 return B_ERROR; 95 96 return output->WriteExactly((const uint8*)fData + offset, size); 97 } 98 99 100 } // namespace BHPKG 101 102 } // namespace BPackageKit 103