OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/numerics/safe_math.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" |
| 13 |
| 14 namespace chrome_pdf { |
| 15 |
| 16 template <class StringType> |
| 17 PDFiumAPIStringBufferAdapter<StringType>::PDFiumAPIStringBufferAdapter( |
| 18 StringType* str, |
| 19 size_t expected_size, |
| 20 bool check_expected_size) |
| 21 : str_(str), |
| 22 data_(WriteInto(str, expected_size + 1)), |
| 23 expected_size_(expected_size), |
| 24 check_expected_size_(check_expected_size), |
| 25 is_closed_(false) { |
| 26 } |
| 27 |
| 28 template <class StringType> |
| 29 PDFiumAPIStringBufferAdapter<StringType>::~PDFiumAPIStringBufferAdapter() { |
| 30 DCHECK(is_closed_); |
| 31 } |
| 32 |
| 33 template <class StringType> |
| 34 void* PDFiumAPIStringBufferAdapter<StringType>::GetData() { |
| 35 DCHECK(!is_closed_); |
| 36 return data_; |
| 37 } |
| 38 |
| 39 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) { |
| 47 DCHECK(!is_closed_); |
| 48 is_closed_ = true; |
| 49 |
| 50 if (check_expected_size_) |
| 51 DCHECK_EQ(expected_size_, actual_size); |
| 52 |
| 53 if (actual_size > 0) { |
| 54 DCHECK((*str_)[actual_size - 1] == 0); |
| 55 str_->resize(actual_size - 1); |
| 56 } else { |
| 57 str_->clear(); |
| 58 } |
| 59 } |
| 60 |
| 61 // explicit instantiations |
| 62 template class PDFiumAPIStringBufferAdapter<std::string>; |
| 63 template class PDFiumAPIStringBufferAdapter<base::string16>; |
| 64 |
| 65 } // namespace chrome_pdf |
OLD | NEW |