1 // Copyright 2015 The Chromium 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.
5 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_
8 #include "base/basictypes.h"
9 #include "base/numerics/safe_math.h"
11 namespace chrome_pdf
{
13 // Helper to deal with the fact that many PDFium APIs write the null-terminator
14 // into string buffers that are passed to them, but the PDF plugin likes to pass
15 // in std::strings / base::string16s, where one should not count on the internal
16 // string buffers to be null-terminated.
18 template <class StringType
>
19 class PDFiumAPIStringBufferAdapter
{
21 // |str| is the string to write into.
22 // |expected_size| is the number of characters the PDFium API will write,
23 // including the null-terminator. It should be at least 1.
24 // |check_expected_size| whether to check the actual number of characters
25 // written into |str| against |expected_size| when calling Close().
26 PDFiumAPIStringBufferAdapter(StringType
* str
,
28 bool check_expected_size
);
29 ~PDFiumAPIStringBufferAdapter();
31 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
32 // hold |expected_size_| + 1 characters, so the PDFium API that uses the
33 // pointer has space to write a null-terminator.
36 // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra
37 // null-terminator. This must be called prior to the adapter's destruction.
38 // The pointer returned by GetData() should be considered invalid.
39 void Close(size_t actual_size
);
41 template <typename IntType
>
42 void Close(IntType actual_size
) {
43 base::CheckedNumeric
<size_t> unsigned_size
= actual_size
;
44 Close(unsigned_size
.ValueOrDie());
48 StringType
* const str_
;
50 const size_t expected_size_
;
51 const bool check_expected_size_
;
54 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter
);
57 // Helper to deal with the fact that many PDFium APIs write the null-terminator
58 // into string buffers that are passed to them, but the PDF plugin likes to pass
59 // in std::strings / base::string16s, where one should not count on the internal
60 // string buffers to be null-terminated. This version is suitable for APIs that
61 // work in terms of number of bytes instead of the number of characters.
62 template <class StringType
>
63 class PDFiumAPIStringBufferSizeInBytesAdapter
{
65 // |str| is the string to write into.
66 // |expected_size| is the number of bytes the PDFium API will write,
67 // including the null-terminator. It should be at least the size of a
68 // character in bytes.
69 // |check_expected_size| whether to check the actual number of bytes
70 // written into |str| against |expected_size| when calling Close().
71 PDFiumAPIStringBufferSizeInBytesAdapter(StringType
* str
,
73 bool check_expected_size
);
74 ~PDFiumAPIStringBufferSizeInBytesAdapter();
76 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to
77 // hold |expected_size_| + sizeof(StringType::value_type) bytes, so the PDFium
78 // API that uses the pointer has space to write a null-terminator.
81 // Resizes |str_| to |actual_size| - sizeof(StringType::value_type) bytes,
82 // thereby removing the extra null-terminator. This must be called prior to
83 // the adapter's destruction. The pointer returned by GetData() should be
84 // considered invalid.
85 void Close(size_t actual_size
);
87 template <typename IntType
>
88 void Close(IntType actual_size
) {
89 base::CheckedNumeric
<size_t> unsigned_size
= actual_size
;
90 Close(unsigned_size
.ValueOrDie());
94 PDFiumAPIStringBufferAdapter
<StringType
> adapter_
;
97 } // namespace chrome_pdf
99 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_