1 // Copyright 2019 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_UTILS_FILE_UTIL_H_ 6 #define TESTING_UTILS_FILE_UTIL_H_ 7 8 #include <stdlib.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "public/fpdfview.h" 14 #include "testing/free_deleter.h" 15 16 // Reads the entire contents of a file into a newly alloc'd buffer. 17 std::unique_ptr<char, pdfium::FreeDeleter> GetFileContents(const char* filename, 18 size_t* retlen); 19 20 // Use an ordinary file anywhere a FPDF_FILEACCESS is required. 21 class FileAccessForTesting final : public FPDF_FILEACCESS { 22 public: 23 explicit FileAccessForTesting(const std::string& file_name); 24 25 private: 26 static int SGetBlock(void* param, 27 unsigned long pos, 28 unsigned char* pBuf, 29 unsigned long size); 30 31 int GetBlockImpl(unsigned long pos, unsigned char* pBuf, unsigned long size); 32 33 size_t file_length_; 34 std::unique_ptr<char, pdfium::FreeDeleter> file_contents_; 35 }; 36 37 #endif // TESTING_UTILS_FILE_UTIL_H_ 38