mirror of
https://pdfium.googlesource.com/pdfium
synced 2026-07-31 02:43:52 +08:00
Introduce safe, container-templated span-based I/O wrappers. Helps centralize some UNSAFE_BUFFERS() usage. -- Add spanread/spanwrite overloads for FILE*. -- Add spanread/spanwrite overloads for POSIX file descriptors. -- Add spanread/spanwrite overloads for Windows HANDLE. -- Change FileAccessIface::Write() signature to return bool. -- Update image_diff, write.cc, file_util, and cfx_folderfontinfo. -- Update CFX_FileAccess_Posix to use FD wrappers. -- Update CFX_FileAccess_Windows to use HANDLE wrappers. -- Update TemporaryFileTest to use safe spanwrite wrapper. -- Add unit tests for all overloads in span_util_unittest.cpp. Note that CFX_FileAccess_Posix changes may have fixed a latent error underflow bug in the process. Change-Id: Ic2c4088d364be0a2f38c5d30854a61fdacc6c69d Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/149030 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
// Copyright 2019 The PDFium Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef TESTING_UTILS_FILE_UTIL_H_
|
|
#define TESTING_UTILS_FILE_UTIL_H_
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "build/build_config.h"
|
|
#include "public/fpdfview.h"
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
namespace pdfium {
|
|
|
|
// Scoper for FILE*.
|
|
struct FileCloser {
|
|
void operator()(FILE* f) const;
|
|
};
|
|
using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
|
|
|
|
#if BUILDFLAG(IS_POSIX)
|
|
// Scoper for POSIX file descriptor.
|
|
class ScopedFD {
|
|
public:
|
|
ScopedFD();
|
|
explicit ScopedFD(int fd);
|
|
|
|
ScopedFD(const ScopedFD&) = delete;
|
|
ScopedFD& operator=(const ScopedFD&) = delete;
|
|
|
|
~ScopedFD();
|
|
|
|
int get() const { return fd_; }
|
|
|
|
private:
|
|
int fd_ = -1;
|
|
};
|
|
#endif // BUILDFLAG(IS_POSIX)
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
// Scoper for Windows HANDLE.
|
|
class ScopedHandle {
|
|
public:
|
|
ScopedHandle();
|
|
explicit ScopedHandle(HANDLE handle);
|
|
|
|
ScopedHandle(const ScopedHandle&) = delete;
|
|
ScopedHandle& operator=(const ScopedHandle&) = delete;
|
|
|
|
~ScopedHandle();
|
|
|
|
HANDLE get() const { return handle_; }
|
|
|
|
private:
|
|
HANDLE handle_ = INVALID_HANDLE_VALUE;
|
|
};
|
|
#endif // BUILDFLAG(IS_WIN)
|
|
|
|
} // namespace pdfium
|
|
|
|
// Returns true if the path can be read from.
|
|
bool CanReadFile(const char* filename);
|
|
|
|
// Reads the entire contents of a file into a vector. Returns an empty vector on
|
|
// failure. Note that this function assumes reading an empty file is not a valid
|
|
// use case, and treats such an action as a failure.
|
|
std::vector<uint8_t> GetFileContents(const char* filename);
|
|
|
|
// Use an ordinary file anywhere a FPDF_FILEACCESS is required.
|
|
class FileAccessForTesting final : public FPDF_FILEACCESS {
|
|
public:
|
|
explicit FileAccessForTesting(const std::string& file_name);
|
|
|
|
private:
|
|
static int SGetBlock(void* param,
|
|
unsigned long pos,
|
|
unsigned char* pBuf,
|
|
unsigned long size);
|
|
|
|
int GetBlockImpl(unsigned long pos, unsigned char* pBuf, unsigned long size);
|
|
|
|
std::vector<uint8_t> file_contents_;
|
|
};
|
|
|
|
#endif // TESTING_UTILS_FILE_UTIL_H_
|