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 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" | 5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/numerics/safe_math.h" | |
11 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
12 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
13 | 12 |
14 namespace chrome_pdf { | 13 namespace chrome_pdf { |
15 | 14 |
16 template <class StringType> | 15 template <class StringType> |
17 PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter( | 16 PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter( |
18 StringType* str, | 17 StringType* str, |
19 size_t expected_size, | 18 size_t expected_size, |
20 bool check_expected_size) | 19 bool check_expected_size) |
21 : str_(str), | 20 : str_(str), |
22 data_(base::WriteInto(str, expected_size + 1)), | 21 data_(base::WriteInto(str, expected_size + 1)), |
23 expected_size_(expected_size), | 22 expected_size_(expected_size), |
24 check_expected_size_(check_expected_size), | 23 check_expected_size_(check_expected_size), |
25 is_closed_(false) { | 24 is_closed_(false) { |
26 } | 25 } |
27 | 26 |
28 template <class StringType> | 27 template <class StringType> |
29 PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() { | 28 PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() { |
30 DCHECK(is_closed_); | 29 DCHECK(is_closed_); |
31 } | 30 } |
32 | 31 |
33 template <class StringType> | 32 template <class StringType> |
34 void* PDFiumAPIStringBufferAdapter<StringType>::GetData() { | 33 void* PDFiumAPIStringBufferAdapter<StringType>::GetData() { |
35 DCHECK(!is_closed_); | 34 DCHECK(!is_closed_); |
36 return data_; | 35 return data_; |
37 } | 36 } |
38 | 37 |
39 template <class StringType> | 38 template <class StringType> |
40 void PDFiumAPIStringBufferAdapter<StringType>::Close(int actual_size) { | |
41 base::CheckedNumeric<size_t> unsigned_size = actual_size; | |
42 Close(unsigned_size.ValueOrDie()); | |
43 } | |
44 | |
45 template <class StringType> | |
46 void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) { | 39 void PDFiumAPIStringBufferAdapter<StringType>::Close(size_t actual_size) { |
47 DCHECK(!is_closed_); | 40 DCHECK(!is_closed_); |
48 is_closed_ = true; | 41 is_closed_ = true; |
49 | 42 |
50 if (check_expected_size_) | 43 if (check_expected_size_) |
51 DCHECK_EQ(expected_size_, actual_size); | 44 DCHECK_EQ(expected_size_, actual_size); |
52 | 45 |
53 if (actual_size > 0) { | 46 if (actual_size > 0) { |
54 DCHECK((*str_)[actual_size - 1] == 0); | 47 DCHECK((*str_)[actual_size - 1] == 0); |
55 str_->resize(actual_size - 1); | 48 str_->resize(actual_size - 1); |
56 } else { | 49 } else { |
57 str_->clear(); | 50 str_->clear(); |
58 } | 51 } |
59 } | 52 } |
60 | 53 |
| 54 template <class StringType> |
| 55 PDFiumAPIStringBufferSizeInBytesAdapter<StringType>:: |
| 56 PDFiumAPIStringBufferSizeInBytesAdapter(StringType* str, |
| 57 size_t expected_size, |
| 58 bool check_expected_size) |
| 59 : adapter_(str, |
| 60 expected_size / sizeof(typename StringType::value_type), |
| 61 check_expected_size) { |
| 62 DCHECK(expected_size % sizeof(typename StringType::value_type) == 0); |
| 63 } |
| 64 |
| 65 template <class StringType> |
| 66 PDFiumAPIStringBufferSizeInBytesAdapter< |
| 67 StringType>::~PDFiumAPIStringBufferSizeInBytesAdapter() = default; |
| 68 |
| 69 template <class StringType> |
| 70 void* PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::GetData() { |
| 71 return adapter_.GetData(); |
| 72 } |
| 73 |
| 74 template <class StringType> |
| 75 void PDFiumAPIStringBufferSizeInBytesAdapter<StringType>::Close( |
| 76 size_t actual_size) { |
| 77 DCHECK(actual_size % sizeof(typename StringType::value_type) == 0); |
| 78 adapter_.Close(actual_size / sizeof(typename StringType::value_type)); |
| 79 } |
| 80 |
61 // explicit instantiations | 81 // explicit instantiations |
62 template class PDFiumAPIStringBufferAdapter<std::string>; | 82 template class PDFiumAPIStringBufferAdapter<std::string>; |
63 template class PDFiumAPIStringBufferAdapter<base::string16>; | 83 template class PDFiumAPIStringBufferAdapter<base::string16>; |
| 84 template class PDFiumAPIStringBufferSizeInBytesAdapter<base::string16>; |
64 | 85 |
65 } // namespace chrome_pdf | 86 } // namespace chrome_pdf |
OLD | NEW |