2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-10-16 18:09:18 -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_simple_string_dictionary_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-10-16 18:09:18 -04:00
|
|
|
|
#include "base/logging.h"
|
2014-10-23 18:47:27 -04:00
|
|
|
|
#include "util/file/file_writer.h"
|
2014-10-16 18:09:18 -04:00
|
|
|
|
#include "util/numeric/safe_assignment.h"
|
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
|
|
MinidumpSimpleStringDictionaryEntryWriter::
|
|
|
|
|
MinidumpSimpleStringDictionaryEntryWriter()
|
|
|
|
|
: MinidumpWritable(), entry_(), key_(), value_() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpSimpleStringDictionaryEntryWriter::
|
|
|
|
|
~MinidumpSimpleStringDictionaryEntryWriter() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MinidumpSimpleStringDictionaryEntry*
|
2016-11-10 13:23:05 -05:00
|
|
|
|
MinidumpSimpleStringDictionaryEntryWriter::
|
|
|
|
|
GetMinidumpSimpleStringDictionaryEntry() const {
|
2014-10-16 18:09:18 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
return &entry_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MinidumpSimpleStringDictionaryEntryWriter::SetKeyValue(
|
|
|
|
|
const std::string& key,
|
|
|
|
|
const std::string& value) {
|
2014-10-28 17:00:46 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
2014-10-16 18:09:18 -04:00
|
|
|
|
key_.SetUTF8(key);
|
|
|
|
|
value_.SetUTF8(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpSimpleStringDictionaryEntryWriter::Freeze() {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
if (!MinidumpWritable::Freeze()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key_.RegisterRVA(&entry_.key);
|
|
|
|
|
value_.RegisterRVA(&entry_.value);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t MinidumpSimpleStringDictionaryEntryWriter::SizeOfObject() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
// This object doesn’t directly write anything itself. Its
|
|
|
|
|
// MinidumpSimpleStringDictionaryEntry is written by its parent as part of a
|
|
|
|
|
// MinidumpSimpleStringDictionary, and its children are responsible for
|
|
|
|
|
// writing themselves.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<internal::MinidumpWritable*>
|
|
|
|
|
MinidumpSimpleStringDictionaryEntryWriter::Children() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
|
|
|
|
std::vector<MinidumpWritable*> children(1, &key_);
|
|
|
|
|
children.push_back(&value_);
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpSimpleStringDictionaryEntryWriter::WriteObject(
|
|
|
|
|
FileWriterInterface* file_writer) {
|
|
|
|
|
DCHECK_EQ(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
// This object doesn’t directly write anything itself. Its
|
|
|
|
|
// MinidumpSimpleStringDictionaryEntry is written by its parent as part of a
|
|
|
|
|
// MinidumpSimpleStringDictionary, and its children are responsible for
|
|
|
|
|
// writing themselves.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpSimpleStringDictionaryWriter::MinidumpSimpleStringDictionaryWriter()
|
2015-02-05 08:41:16 -08:00
|
|
|
|
: MinidumpWritable(),
|
|
|
|
|
entries_(),
|
|
|
|
|
simple_string_dictionary_base_(new MinidumpSimpleStringDictionary()) {
|
2014-10-16 18:09:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MinidumpSimpleStringDictionaryWriter::~MinidumpSimpleStringDictionaryWriter() {
|
2017-02-07 16:04:41 -05:00
|
|
|
|
for (auto& item : entries_)
|
|
|
|
|
delete item.second;
|
2014-10-16 18:09:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:00:46 -04:00
|
|
|
|
void MinidumpSimpleStringDictionaryWriter::InitializeFromMap(
|
|
|
|
|
const std::map<std::string, std::string>& map) {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
DCHECK(entries_.empty());
|
|
|
|
|
|
|
|
|
|
for (const auto& iterator : map) {
|
2017-10-12 12:42:28 -04:00
|
|
|
|
auto entry = std::make_unique<MinidumpSimpleStringDictionaryEntryWriter>();
|
2014-10-28 17:00:46 -04:00
|
|
|
|
entry->SetKeyValue(iterator.first, iterator.second);
|
2015-12-09 17:36:32 -05:00
|
|
|
|
AddEntry(std::move(entry));
|
2014-10-28 17:00:46 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 18:09:18 -04:00
|
|
|
|
void MinidumpSimpleStringDictionaryWriter::AddEntry(
|
2016-04-25 12:13:07 -07:00
|
|
|
|
std::unique_ptr<MinidumpSimpleStringDictionaryEntryWriter> entry) {
|
2014-10-28 17:00:46 -04:00
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
2014-10-16 18:09:18 -04:00
|
|
|
|
|
2014-10-27 15:01:39 -04:00
|
|
|
|
const std::string& key = entry->Key();
|
|
|
|
|
auto iterator = entries_.find(key);
|
|
|
|
|
if (iterator != entries_.end()) {
|
|
|
|
|
delete iterator->second;
|
|
|
|
|
iterator->second = entry.release();
|
|
|
|
|
} else {
|
|
|
|
|
entries_[key] = entry.release();
|
|
|
|
|
}
|
2014-10-16 18:09:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:00:46 -04:00
|
|
|
|
bool MinidumpSimpleStringDictionaryWriter::IsUseful() const {
|
|
|
|
|
return !entries_.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-16 18:09:18 -04:00
|
|
|
|
bool MinidumpSimpleStringDictionaryWriter::Freeze() {
|
|
|
|
|
DCHECK_EQ(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
if (!MinidumpWritable::Freeze()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t entry_count = entries_.size();
|
2015-02-05 08:41:16 -08:00
|
|
|
|
if (!AssignIfInRange(&simple_string_dictionary_base_->count, entry_count)) {
|
2014-10-16 18:09:18 -04:00
|
|
|
|
LOG(ERROR) << "entry_count " << entry_count << " out of range";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t MinidumpSimpleStringDictionaryWriter::SizeOfObject() {
|
|
|
|
|
DCHECK_GE(state(), kStateFrozen);
|
|
|
|
|
|
2015-02-05 08:41:16 -08:00
|
|
|
|
return sizeof(*simple_string_dictionary_base_) +
|
2014-10-16 18:09:18 -04:00
|
|
|
|
entries_.size() * sizeof(MinidumpSimpleStringDictionaryEntry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<internal::MinidumpWritable*>
|
|
|
|
|
MinidumpSimpleStringDictionaryWriter::Children() {
|
|
|
|
|
DCHECK_GE(state(), kStateMutable);
|
|
|
|
|
|
|
|
|
|
std::vector<MinidumpWritable*> children;
|
|
|
|
|
for (const auto& key_entry : entries_) {
|
|
|
|
|
children.push_back(key_entry.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return children;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MinidumpSimpleStringDictionaryWriter::WriteObject(
|
|
|
|
|
FileWriterInterface* file_writer) {
|
|
|
|
|
DCHECK_GE(state(), kStateWritable);
|
|
|
|
|
|
|
|
|
|
WritableIoVec iov;
|
2015-02-05 08:41:16 -08:00
|
|
|
|
iov.iov_base = simple_string_dictionary_base_.get();
|
|
|
|
|
iov.iov_len = sizeof(*simple_string_dictionary_base_);
|
2014-10-16 18:09:18 -04:00
|
|
|
|
std::vector<WritableIoVec> iovecs(1, iov);
|
|
|
|
|
|
2014-10-23 17:25:20 -04:00
|
|
|
|
for (const auto& key_entry : entries_) {
|
2016-11-10 13:23:05 -05:00
|
|
|
|
iov.iov_base = key_entry.second->GetMinidumpSimpleStringDictionaryEntry();
|
2014-10-16 18:09:18 -04:00
|
|
|
|
iov.iov_len = sizeof(MinidumpSimpleStringDictionaryEntry);
|
2014-10-23 17:25:20 -04:00
|
|
|
|
iovecs.push_back(iov);
|
2014-10-16 18:09:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file_writer->WriteIoVec(&iovecs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace crashpad
|