2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-08-01 14:44:57 -04:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#include "minidump/minidump_string_writer.h"
|
|
|
|
|
|
2015-12-09 17:36:32 -05:00
|
|
|
|
#include <utility>
|
|
|
|
|
|
2023-10-18 12:08:10 -07:00
|
|
|
|
#include "base/check_op.h"
|
2014-08-01 14:44:57 -04:00
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
#include "minidump/minidump_writer_util.h"
|
2014-10-23 18:47:27 -04:00
|
|
|
|
#include "util/file/file_writer.h"
|
2014-08-01 14:44:57 -04:00
|
|
|
|
#include "util/numeric/safe_assignment.h"
|
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
|
MinidumpStringWriter<Traits>::MinidumpStringWriter()
|
2015-02-04 17:34:43 -08:00
|
|
|
|
: MinidumpWritable(), string_base_(new MinidumpStringType()), string_() {
|
2014-08-01 14:44:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
|
MinidumpStringWriter<Traits>::~MinidumpStringWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
|
bool MinidumpStringWriter<Traits>::Freeze() {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
if (!MinidumpWritable::Freeze()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t string_bytes = string_.size() * sizeof(string_[0]);
|
2015-02-04 17:34:43 -08:00
|
|
|
|
if (!AssignIfInRange(&string_base_->Length, string_bytes)) {
|
2014-08-01 14:44:57 -04:00
|
|
|
|
LOG(ERROR) << "string_bytes " << string_bytes << " out of range";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
|
size_t MinidumpStringWriter<Traits>::SizeOfObject() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
// Include the NUL terminator.
|
2015-02-04 17:34:43 -08:00
|
|
|
|
return sizeof(*string_base_) + (string_.size() + 1) * sizeof(string_[0]);
|
2014-08-01 14:44:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Traits>
|
|
|
|
|
bool MinidumpStringWriter<Traits>::WriteObject(
|
|
|
|
|
FileWriterInterface* file_writer) {
|
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
// The string’s length is stored in string_base_, and its data is stored in
|
|
|
|
|
// string_. Write them both.
|
|
|
|
|
WritableIoVec iov;
|
2015-02-04 17:34:43 -08:00
|
|
|
|
iov.iov_base = string_base_.get();
|
|
|
|
|
iov.iov_len = sizeof(*string_base_);
|
2014-08-01 14:44:57 -04:00
|
|
|
|
std::vector<WritableIoVec> iovecs(1, iov);
|
|
|
|
|
|
|
|
|
|
// Include the NUL terminator.
|
|
|
|
|
iov.iov_base = &string_[0];
|
|
|
|
|
iov.iov_len = (string_.size() + 1) * sizeof(string_[0]);
|
|
|
|
|
iovecs.push_back(iov);
|
|
|
|
|
|
|
|
|
|
return file_writer->WriteIoVec(&iovecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Explicit template instantiation of the forms of MinidumpStringWriter<> used
|
|
|
|
|
// as base classes.
|
|
|
|
|
template class MinidumpStringWriter<MinidumpStringWriterUTF16Traits>;
|
|
|
|
|
template class MinidumpStringWriter<MinidumpStringWriterUTF8Traits>;
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
MinidumpUTF16StringWriter::~MinidumpUTF16StringWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 14:44:57 -04:00
|
|
|
|
void MinidumpUTF16StringWriter::SetUTF8(const std::string& string_utf8) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
set_string(MinidumpWriterUtil::ConvertUTF8ToUTF16(string_utf8));
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
MinidumpUTF8StringWriter::~MinidumpUTF8StringWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template <typename MinidumpStringWriterType>
|
|
|
|
|
MinidumpStringListWriter<MinidumpStringWriterType>::MinidumpStringListWriter()
|
2014-11-07 09:57:07 -05:00
|
|
|
|
: MinidumpRVAListWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template <typename MinidumpStringWriterType>
|
|
|
|
|
MinidumpStringListWriter<
|
|
|
|
|
MinidumpStringWriterType>::~MinidumpStringListWriter() {
|
2014-11-07 09:57:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template <typename MinidumpStringWriterType>
|
|
|
|
|
void MinidumpStringListWriter<MinidumpStringWriterType>::InitializeFromVector(
|
2014-11-07 09:57:07 -05:00
|
|
|
|
const std::vector<std::string>& vector) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
DCHECK(IsEmpty());
|
|
|
|
|
|
|
|
|
|
for (const std::string& string : vector) {
|
|
|
|
|
AddStringUTF8(string);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template <typename MinidumpStringWriterType>
|
|
|
|
|
void MinidumpStringListWriter<MinidumpStringWriterType>::AddStringUTF8(
|
2014-11-07 09:57:07 -05:00
|
|
|
|
const std::string& string_utf8) {
|
2017-10-12 12:42:28 -04:00
|
|
|
|
auto string_writer = std::make_unique<MinidumpStringWriterType>();
|
2014-11-07 09:57:07 -05:00
|
|
|
|
string_writer->SetUTF8(string_utf8);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
AddChild(std::move(string_writer));
|
2014-11-07 09:57:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template <typename MinidumpStringWriterType>
|
|
|
|
|
bool MinidumpStringListWriter<MinidumpStringWriterType>::IsUseful() const {
|
2014-11-07 09:57:07 -05:00
|
|
|
|
return !IsEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Explicit template instantiation of the forms of MinidumpStringListWriter<>
|
|
|
|
|
// used as type aliases.
|
2014-11-07 11:45:44 -05:00
|
|
|
|
template class MinidumpStringListWriter<MinidumpUTF16StringWriter>;
|
|
|
|
|
template class MinidumpStringListWriter<MinidumpUTF8StringWriter>;
|
2014-11-07 09:57:07 -05:00
|
|
|
|
|
2014-08-01 14:44:57 -04:00
|
|
|
|
} // namespace internal
|
|
|
|
|
} // namespace crashpad
|