
This is an automated patch. Please avoid assigning unrelated bug. #cleanup This is a follow-up to @thestig comment: https://chromium-review.googlesource.com/c/chromium/src/+/5009410/7/pdf/accessibility_helper.cc#11 Several developers, including myself rewrote optional without properly reordering the #includes at the beginning. This patches automatically fixes it. Script: ------- ``` function replace { echo "Replacing $1 by $2" git grep -l "$1" \ | cut -f1 -d: \ | grep -v \ -e "*win*" \ -e "third_party/*" \ -e "tools/*" \ | grep \ -e "\.h" \ -e "\.cc" \ | sort \ | uniq \ | xargs sed -i "s/$1/$2/g" } replace "#include <optional>" "" git add -u git commit -m "remove optional" git revert HEAD --no-commit git add -u echo "Formatting": echo "IncludeBlocks: Regroup" >> ".clang-format" echo "IncludeIsMainRegex: \"(_(android|apple|chromeos|freebsd|fuchsia|fuzzer|ios|linux|mac|nacl|openbsd|posix|stubs?|win))?(_(unit|browser|perf)?tests?)?$\"" >> ".clang-format" git add -u git cl format --upstream=HEAD git checkout origin/main -- ".clang-format" git add -u git commit -m "revert with format" git reset --soft origin/main git add -u git commit -m "Automated format" ``` cleanup: This is a no-op patch formatting includes. Bug: 40288126 Change-Id: I5f61b1207c097a4c6b20a034f9d1b323975b1851 AX-Relnotes: n/a. Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5335142 Owners-Override: Lei Zhang <thestig@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org> Cr-Commit-Position: refs/heads/main@{#1267143}
179 lines
7.0 KiB
C++
179 lines
7.0 KiB
C++
// Copyright 2015 The Chromium Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
|
|
#define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#include "base/functional/callback.h"
|
|
#include "base/memory/raw_ptr.h"
|
|
#include "base/numerics/safe_math.h"
|
|
|
|
namespace chrome_pdf {
|
|
|
|
namespace internal {
|
|
|
|
// Helper to deal with the fact that many PDFium APIs write the null-terminator
|
|
// into string buffers that are passed to them, but the PDF code likes to use
|
|
// std::strings / std::u16strings, where one should not count on the internal
|
|
// string buffers to be null-terminated.
|
|
template <class StringType>
|
|
class PDFiumAPIStringBufferAdapter {
|
|
public:
|
|
// `str` is the string to write into.
|
|
// `expected_size` is the number of characters the PDFium API will write,
|
|
// including the null-terminator. It should be at least 1.
|
|
// `check_expected_size` whether to check the actual number of characters
|
|
// written into `str` against `expected_size` when calling Close().
|
|
PDFiumAPIStringBufferAdapter(StringType* str,
|
|
size_t expected_size,
|
|
bool check_expected_size);
|
|
PDFiumAPIStringBufferAdapter(const PDFiumAPIStringBufferAdapter&) = delete;
|
|
PDFiumAPIStringBufferAdapter& operator=(const PDFiumAPIStringBufferAdapter&) =
|
|
delete;
|
|
~PDFiumAPIStringBufferAdapter();
|
|
|
|
// Returns a pointer to `str_`'s buffer. The buffer's size is large enough to
|
|
// hold `expected_size_` + 1 characters, so the PDFium API that uses the
|
|
// pointer has space to write a null-terminator.
|
|
void* GetData();
|
|
|
|
// Resizes `str_` to `actual_size` - 1 characters, thereby removing the extra
|
|
// null-terminator. This must be called prior to the adapter's destruction.
|
|
// The pointer returned by GetData() should be considered invalid.
|
|
void Close(size_t actual_size);
|
|
|
|
template <typename IntType>
|
|
void Close(IntType actual_size) {
|
|
Close(base::checked_cast<size_t>(actual_size));
|
|
}
|
|
|
|
private:
|
|
const raw_ptr<StringType> str_;
|
|
const raw_ptr<void> data_;
|
|
const size_t expected_size_;
|
|
const bool check_expected_size_;
|
|
bool is_closed_;
|
|
};
|
|
|
|
// Helper to deal with the fact that many PDFium APIs write the null-terminator
|
|
// into string buffers that are passed to them, but the PDF code likes to use
|
|
// std::strings / std::u16strings, where one should not count on the internal
|
|
// string buffers to be null-terminated. This version is suitable for APIs that
|
|
// work in terms of number of bytes instead of the number of characters. Though
|
|
// for std::strings, PDFiumAPIStringBufferAdapter is equivalent.
|
|
class PDFiumAPIStringBufferSizeInBytesAdapter {
|
|
public:
|
|
// `str` is the string to write into.
|
|
// `expected_size` is the number of bytes the PDFium API will write,
|
|
// including the null-terminator. It should be at least the size of a
|
|
// character in bytes.
|
|
// `check_expected_size` whether to check the actual number of bytes
|
|
// written into `str` against `expected_size` when calling Close().
|
|
PDFiumAPIStringBufferSizeInBytesAdapter(std::u16string* str,
|
|
size_t expected_size,
|
|
bool check_expected_size);
|
|
~PDFiumAPIStringBufferSizeInBytesAdapter();
|
|
|
|
// Returns a pointer to `str_`'s buffer. The buffer's size is large enough to
|
|
// hold `expected_size_` + sizeof(char16_t) bytes, so the PDFium API that
|
|
// uses the pointer has space to write a null-terminator.
|
|
void* GetData();
|
|
|
|
// Resizes `str_` to `actual_size` - sizeof(char16_t) bytes, thereby
|
|
// removing the extra null-terminator. This must be called prior to the
|
|
// adapter's destruction. The pointer returned by GetData() should be
|
|
// considered invalid.
|
|
void Close(size_t actual_size);
|
|
|
|
template <typename IntType>
|
|
void Close(IntType actual_size) {
|
|
Close(base::checked_cast<size_t>(actual_size));
|
|
}
|
|
|
|
private:
|
|
PDFiumAPIStringBufferAdapter<std::u16string> adapter_;
|
|
};
|
|
|
|
template <class AdapterType,
|
|
class StringType,
|
|
typename BufferType,
|
|
typename ReturnType>
|
|
std::optional<StringType> CallPDFiumStringBufferApiAndReturnOptional(
|
|
base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
|
|
bool check_expected_size) {
|
|
ReturnType expected_size = api.Run(nullptr, 0);
|
|
if (expected_size == 0)
|
|
return std::nullopt;
|
|
|
|
StringType str;
|
|
AdapterType api_string_adapter(&str, expected_size, check_expected_size);
|
|
auto* data = reinterpret_cast<BufferType*>(api_string_adapter.GetData());
|
|
api_string_adapter.Close(api.Run(data, expected_size));
|
|
return str;
|
|
}
|
|
|
|
template <class AdapterType,
|
|
class StringType,
|
|
typename BufferType,
|
|
typename ReturnType>
|
|
StringType CallPDFiumStringBufferApi(
|
|
base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
|
|
bool check_expected_size) {
|
|
std::optional<StringType> result =
|
|
CallPDFiumStringBufferApiAndReturnOptional<AdapterType, StringType>(
|
|
api, check_expected_size);
|
|
return result.value_or(StringType());
|
|
}
|
|
|
|
} // namespace internal
|
|
|
|
// Helper function to call PDFium APIs where the output buffer is expected to
|
|
// hold UTF-16 data, and the buffer length is specified in bytes.
|
|
template <typename BufferType>
|
|
std::u16string CallPDFiumWideStringBufferApi(
|
|
base::RepeatingCallback<unsigned long(BufferType*, unsigned long)> api,
|
|
bool check_expected_size) {
|
|
using adapter_type = internal::PDFiumAPIStringBufferSizeInBytesAdapter;
|
|
return internal::CallPDFiumStringBufferApi<adapter_type, std::u16string>(
|
|
api, check_expected_size);
|
|
}
|
|
|
|
// Variant of CallPDFiumWideStringBufferApi() that distinguishes between API
|
|
// call failures and empty string return values.
|
|
template <typename BufferType>
|
|
std::optional<std::u16string> CallPDFiumWideStringBufferApiAndReturnOptional(
|
|
base::RepeatingCallback<unsigned long(BufferType*, unsigned long)> api,
|
|
bool check_expected_size) {
|
|
using adapter_type = internal::PDFiumAPIStringBufferSizeInBytesAdapter;
|
|
return internal::CallPDFiumStringBufferApiAndReturnOptional<adapter_type,
|
|
std::u16string>(
|
|
api, check_expected_size);
|
|
}
|
|
|
|
// Helper function to call PDFium APIs where the output buffer is expected to
|
|
// hold ASCII or UTF-8 data, and the buffer length is specified in bytes.
|
|
template <typename BufferType, typename ReturnType>
|
|
std::string CallPDFiumStringBufferApi(
|
|
base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
|
|
bool check_expected_size) {
|
|
using adapter_type = internal::PDFiumAPIStringBufferAdapter<std::string>;
|
|
return internal::CallPDFiumStringBufferApi<adapter_type, std::string>(
|
|
api, check_expected_size);
|
|
}
|
|
|
|
// Expose internal::PDFiumAPIStringBufferAdapter for special cases that cannot
|
|
// use the CallPDFiumStringBuffer* functions above.
|
|
template <class StringType>
|
|
using PDFiumAPIStringBufferAdapter =
|
|
internal::PDFiumAPIStringBufferAdapter<StringType>;
|
|
|
|
} // namespace chrome_pdf
|
|
|
|
#endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
|