OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 5 #ifndef PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 6 #define PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/numerics/safe_math.h" |
9 | 10 |
10 namespace chrome_pdf { | 11 namespace chrome_pdf { |
11 | 12 |
12 // Helper to deal with the fact that many PDFium APIs write the null-terminator | 13 // Helper to deal with the fact that many PDFium APIs write the null-terminator |
13 // into string buffers that are passed to them, but the PDF plugin likes to pass | 14 // into string buffers that are passed to them, but the PDF plugin likes to pass |
14 // in std::strings / base::string16s, where one should not count on the internal | 15 // in std::strings / base::string16s, where one should not count on the internal |
15 // string buffers to be null-terminated. | 16 // string buffers to be null-terminated. |
16 | 17 |
17 template <class StringType> | 18 template <class StringType> |
18 class PDFiumAPIStringBufferAdapter { | 19 class PDFiumAPIStringBufferAdapter { |
19 public: | 20 public: |
20 // |str| is the string to write into. | 21 // |str| is the string to write into. |
21 // |expected_size| is the number of characters the PDFium API will write, | 22 // |expected_size| is the number of characters the PDFium API will write, |
22 // including the null-terminator. It should be at least 1. | 23 // including the null-terminator. It should be at least 1. |
23 // |check_expected_size| whether to check the actual number of characters | 24 // |check_expected_size| whether to check the actual number of characters |
24 // written into |str| against |expected_size| when calling Close(). | 25 // written into |str| against |expected_size| when calling Close(). |
25 PDFiumAPIStringBufferAdapter(StringType* str, | 26 PDFiumAPIStringBufferAdapter(StringType* str, |
26 size_t expected_size, | 27 size_t expected_size, |
27 bool check_expected_size); | 28 bool check_expected_size); |
28 ~PDFiumAPIStringBufferAdapter(); | 29 ~PDFiumAPIStringBufferAdapter(); |
29 | 30 |
30 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to | 31 // Returns a pointer to |str_|'s buffer. The buffer's size is large enough to |
31 // hold |expected_size_| + 1 characters, so the PDFium API that uses the | 32 // hold |expected_size_| + 1 characters, so the PDFium API that uses the |
32 // pointer has space to write a null-terminator. | 33 // pointer has space to write a null-terminator. |
33 void* GetData(); | 34 void* GetData(); |
34 | 35 |
35 // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra | 36 // Resizes |str_| to |actual_size| - 1 characters, thereby removing the extra |
36 // null-terminator. This must be called prior to the adapter's destruction. | 37 // null-terminator. This must be called prior to the adapter's destruction. |
37 // The pointer returned by GetData() should be considered invalid. | 38 // The pointer returned by GetData() should be considered invalid. |
38 void Close(int actual_size); | |
39 void Close(size_t actual_size); | 39 void Close(size_t actual_size); |
40 | 40 |
| 41 template <typename IntType> |
| 42 void Close(IntType actual_size) { |
| 43 base::CheckedNumeric<size_t> unsigned_size = actual_size; |
| 44 Close(unsigned_size.ValueOrDie()); |
| 45 } |
| 46 |
41 private: | 47 private: |
42 StringType* const str_; | 48 StringType* const str_; |
43 void* const data_; | 49 void* const data_; |
44 const size_t expected_size_; | 50 const size_t expected_size_; |
45 const bool check_expected_size_; | 51 const bool check_expected_size_; |
46 bool is_closed_; | 52 bool is_closed_; |
47 | 53 |
48 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter); | 54 DISALLOW_COPY_AND_ASSIGN(PDFiumAPIStringBufferAdapter); |
49 }; | 55 }; |
50 | 56 |
| 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 { |
| 64 public: |
| 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, |
| 72 size_t expected_size, |
| 73 bool check_expected_size); |
| 74 ~PDFiumAPIStringBufferSizeInBytesAdapter(); |
| 75 |
| 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. |
| 79 void* GetData(); |
| 80 |
| 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); |
| 86 |
| 87 template <typename IntType> |
| 88 void Close(IntType actual_size) { |
| 89 base::CheckedNumeric<size_t> unsigned_size = actual_size; |
| 90 Close(unsigned_size.ValueOrDie()); |
| 91 } |
| 92 |
| 93 private: |
| 94 PDFiumAPIStringBufferAdapter<StringType> adapter_; |
| 95 }; |
| 96 |
51 } // namespace chrome_pdf | 97 } // namespace chrome_pdf |
52 | 98 |
53 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ | 99 #endif // PDF_PDFIUM_PDFIUM_API_STRING_BUFFER_ADAPTER_H_ |
OLD | NEW |