1 // Copyright 2014 PDFium 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 TESTING_FX_STRING_TESTHELPERS_H_ 6 #define TESTING_FX_STRING_TESTHELPERS_H_ 7 8 #include <ostream> 9 10 #include "core/fxcrt/cfx_datetime.h" 11 #include "core/fxcrt/fx_stream.h" 12 13 // Output stream operator so GTEST macros work with CFX_DateTime objects. 14 std::ostream& operator<<(std::ostream& os, const CFX_DateTime& dt); 15 16 class CFX_InvalidSeekableReadStream : public IFX_SeekableReadStream { 17 public: 18 template <typename T, typename... Args> 19 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); 20 21 // IFX_SeekableReadStream overrides: ReadBlock(void * buffer,FX_FILESIZE offset,size_t size)22 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { 23 return false; 24 } GetSize()25 FX_FILESIZE GetSize() override { return data_size_; } 26 27 private: CFX_InvalidSeekableReadStream(FX_FILESIZE data_size)28 explicit CFX_InvalidSeekableReadStream(FX_FILESIZE data_size) 29 : data_size_(data_size) {} 30 31 FX_FILESIZE data_size_; 32 }; 33 34 class CFX_BufferSeekableReadStream : public IFX_SeekableReadStream { 35 public: 36 template <typename T, typename... Args> 37 friend RetainPtr<T> pdfium::MakeRetain(Args&&... args); 38 39 // IFX_SeekableReadStream: ReadBlock(void * buffer,FX_FILESIZE offset,size_t size)40 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { 41 if (offset < 0 || static_cast<size_t>(offset) >= data_size_) 42 return false; 43 44 if (static_cast<size_t>(offset) + size > data_size_) 45 size = data_size_ - static_cast<size_t>(offset); 46 if (size == 0) 47 return false; 48 49 memcpy(buffer, data_ + offset, size); 50 return true; 51 } 52 GetSize()53 FX_FILESIZE GetSize() override { 54 return static_cast<FX_FILESIZE>(data_size_); 55 } 56 57 private: CFX_BufferSeekableReadStream(const unsigned char * src,size_t src_size)58 CFX_BufferSeekableReadStream(const unsigned char* src, size_t src_size) 59 : data_(src), data_size_(src_size) {} 60 61 const unsigned char* data_; 62 size_t data_size_; 63 }; 64 65 #endif // TESTING_FX_STRING_TESTHELPERS_H_ 66