chromium/pdf/pdfium/pdfium_api_string_buffer_adapter.h

// 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 {};

// 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 {};

template <class AdapterType,
          class StringType,
          typename BufferType,
          typename ReturnType>
std::optional<StringType> CallPDFiumStringBufferApiAndReturnOptional(
    base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
    bool check_expected_size) {}

template <class AdapterType,
          class StringType,
          typename BufferType,
          typename ReturnType>
StringType CallPDFiumStringBufferApi(
    base::RepeatingCallback<ReturnType(BufferType*, ReturnType)> api,
    bool check_expected_size) {}

}  // 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) {}

// 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) {}

// 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) {}

// Expose internal::PDFiumAPIStringBufferAdapter for special cases that cannot
// use the CallPDFiumStringBuffer* functions above.
PDFiumAPIStringBufferAdapter;

}  // namespace chrome_pdf

#endif  // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_