| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 
|  | 2 // | 
|  | 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 // you may not use this file except in compliance with the License. | 
|  | 5 // You may obtain a copy of the License at | 
|  | 6 // | 
|  | 7 //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 // | 
|  | 9 // Unless required by applicable law or agreed to in writing, software | 
|  | 10 // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 // See the License for the specific language governing permissions and | 
|  | 13 // limitations under the License. | 
|  | 14 | 
|  | 15 #include "minidump/minidump_string_writer.h" | 
|  | 16 | 
|  | 17 #include "base/logging.h" | 
|  | 18 #include "minidump/minidump_writer_util.h" | 
|  | 19 #include "util/numeric/safe_assignment.h" | 
|  | 20 | 
|  | 21 namespace crashpad { | 
|  | 22 namespace internal { | 
|  | 23 | 
|  | 24 template <typename Traits> | 
|  | 25 MinidumpStringWriter<Traits>::MinidumpStringWriter() | 
|  | 26     : MinidumpWritable(), string_base_(), string_() { | 
|  | 27 } | 
|  | 28 | 
|  | 29 template <typename Traits> | 
|  | 30 MinidumpStringWriter<Traits>::~MinidumpStringWriter() { | 
|  | 31 } | 
|  | 32 | 
|  | 33 template <typename Traits> | 
|  | 34 bool MinidumpStringWriter<Traits>::Freeze() { | 
|  | 35   DCHECK_EQ(state(), kStateMutable); | 
|  | 36 | 
|  | 37   if (!MinidumpWritable::Freeze()) { | 
|  | 38     return false; | 
|  | 39   } | 
|  | 40 | 
|  | 41   size_t string_bytes = string_.size() * sizeof(string_[0]); | 
|  | 42   if (!AssignIfInRange(&string_base_.Length, string_bytes)) { | 
|  | 43     LOG(ERROR) << "string_bytes " << string_bytes << " out of range"; | 
|  | 44     return false; | 
|  | 45   } | 
|  | 46 | 
|  | 47   return true; | 
|  | 48 } | 
|  | 49 | 
|  | 50 template <typename Traits> | 
|  | 51 size_t MinidumpStringWriter<Traits>::SizeOfObject() { | 
|  | 52   DCHECK_GE(state(), kStateFrozen); | 
|  | 53 | 
|  | 54   // Include the NUL terminator. | 
|  | 55   return sizeof(string_base_) + (string_.size() + 1) * sizeof(string_[0]); | 
|  | 56 } | 
|  | 57 | 
|  | 58 template <typename Traits> | 
|  | 59 bool MinidumpStringWriter<Traits>::WriteObject( | 
|  | 60     FileWriterInterface* file_writer) { | 
|  | 61   DCHECK_EQ(state(), kStateWritable); | 
|  | 62 | 
|  | 63   // The string’s length is stored in string_base_, and its data is stored in | 
|  | 64   // string_. Write them both. | 
|  | 65   WritableIoVec iov; | 
|  | 66   iov.iov_base = &string_base_; | 
|  | 67   iov.iov_len = sizeof(string_base_); | 
|  | 68   std::vector<WritableIoVec> iovecs(1, iov); | 
|  | 69 | 
|  | 70   // Include the NUL terminator. | 
|  | 71   iov.iov_base = &string_[0]; | 
|  | 72   iov.iov_len = (string_.size() + 1) * sizeof(string_[0]); | 
|  | 73   iovecs.push_back(iov); | 
|  | 74 | 
|  | 75   return file_writer->WriteIoVec(&iovecs); | 
|  | 76 } | 
|  | 77 | 
|  | 78 // Explicit template instantiation of the forms of MinidumpStringWriter<> used | 
|  | 79 // as base classes. | 
|  | 80 template class MinidumpStringWriter<MinidumpStringWriterUTF16Traits>; | 
|  | 81 template class MinidumpStringWriter<MinidumpStringWriterUTF8Traits>; | 
|  | 82 | 
|  | 83 void MinidumpUTF16StringWriter::SetUTF8(const std::string& string_utf8) { | 
|  | 84   DCHECK_EQ(state(), kStateMutable); | 
|  | 85 | 
|  | 86   set_string(MinidumpWriterUtil::ConvertUTF8ToUTF16(string_utf8)); | 
|  | 87 } | 
|  | 88 | 
|  | 89 }  // namespace internal | 
|  | 90 }  // namespace crashpad | 
| OLD | NEW | 
|---|