Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef _BSDIFF_MEMORY_FILE_H_ |
| 6 | #define _BSDIFF_MEMORY_FILE_H_ |
| 7 | |
| 8 | #include <memory> |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 9 | |
Alex Deymo | ddf9db5 | 2017-03-02 16:10:41 -0800 | [diff] [blame] | 10 | #include "bsdiff/file_interface.h" |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 11 | |
| 12 | namespace bsdiff { |
| 13 | |
| 14 | class MemoryFile : public FileInterface { |
| 15 | public: |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 16 | // Creates a read only MemoryFile based on the underlying |data| passed. |
| 17 | // The MemoryFile will use data starting from |data| with length of |size| as |
| 18 | // the file content. Write is not supported. |
| 19 | MemoryFile(const uint8_t* data, size_t size); |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 20 | |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 21 | ~MemoryFile() = default; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 22 | |
| 23 | // FileInterface overrides. |
| 24 | bool Read(void* buf, size_t count, size_t* bytes_read) override; |
| 25 | bool Write(const void* buf, size_t count, size_t* bytes_written) override; |
| 26 | bool Seek(off_t pos) override; |
| 27 | bool Close() override; |
| 28 | bool GetSize(uint64_t* size) override; |
| 29 | |
| 30 | private: |
Sen Jiang | 716d569 | 2016-05-09 16:43:34 -0700 | [diff] [blame] | 31 | const uint8_t* data_ = nullptr; |
| 32 | size_t size_ = 0; |
| 33 | off_t offset_ = 0; |
Sen Jiang | 5b372b6 | 2016-03-28 16:14:35 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | } // namespace bsdiff |
| 37 | |
| 38 | #endif // _BSDIFF_MEMORY_FILE_H_ |