2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2014 The Crashpad Authors
|
2014-08-22 13:52:28 -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.
|
|
|
|
|
|
|
|
|
|
#ifndef CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|
|
|
|
|
#define CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2016-01-06 12:22:50 -05:00
|
|
|
|
#include <sys/types.h>
|
2014-08-22 13:52:28 -04:00
|
|
|
|
|
2017-08-30 10:03:33 -04:00
|
|
|
|
#include <algorithm>
|
2017-10-13 10:35:45 -04:00
|
|
|
|
#include <type_traits>
|
2017-08-30 10:03:33 -04:00
|
|
|
|
|
2020-06-22 11:14:31 +02:00
|
|
|
|
#include "base/check_op.h"
|
2017-08-30 10:03:33 -04:00
|
|
|
|
#include "base/strings/string_piece.h"
|
2015-09-14 11:09:46 -07:00
|
|
|
|
#include "util/misc/implicit_cast.h"
|
2014-08-22 13:52:28 -04:00
|
|
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief A map/dictionary collection implementation using a fixed amount of
|
|
|
|
|
//! storage, so that it does not perform any dynamic allocations for its
|
|
|
|
|
//! operations.
|
|
|
|
|
//!
|
|
|
|
|
//! The actual map storage (TSimpleStringDictionary::Entry) is guaranteed to be
|
|
|
|
|
//! POD, so that it can be transmitted over various IPC mechanisms.
|
|
|
|
|
//!
|
|
|
|
|
//! The template parameters control the amount of storage used for the key,
|
|
|
|
|
//! value, and map. The \a KeySize and \a ValueSize are measured in bytes, not
|
|
|
|
|
//! glyphs, and include space for a trailing `NUL` byte. This gives space for
|
|
|
|
|
//! `KeySize - 1` and `ValueSize - 1` characters in an entry. \a NumEntries is
|
|
|
|
|
//! the total number of entries that will fit in the map.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
template <size_t KeySize = 256, size_t ValueSize = 256, size_t NumEntries = 64>
|
|
|
|
|
class TSimpleStringDictionary {
|
|
|
|
|
public:
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Constant and publicly accessible versions of the template
|
|
|
|
|
//! parameters.
|
|
|
|
|
//! \{
|
2014-08-22 13:52:28 -04:00
|
|
|
|
static const size_t key_size = KeySize;
|
|
|
|
|
static const size_t value_size = ValueSize;
|
|
|
|
|
static const size_t num_entries = NumEntries;
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \}
|
2014-08-22 13:52:28 -04:00
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief A single entry in the map.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
struct Entry {
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief The entry’s key.
|
|
|
|
|
//!
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//! This string is always `NUL`-terminated. If this is a 0-length
|
|
|
|
|
//! `NUL`-terminated string, the entry is inactive.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
char key[KeySize];
|
2014-08-22 17:09:03 -04:00
|
|
|
|
|
|
|
|
|
//! \brief The entry’s value.
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//!
|
|
|
|
|
//! This string is always `NUL`-terminated.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
char value[ValueSize];
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Returns the validity of the entry.
|
|
|
|
|
//!
|
|
|
|
|
//! If #key is an empty string, the entry is considered inactive, and this
|
|
|
|
|
//! method returns `false`. Otherwise, returns `true`.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
bool is_active() const {
|
|
|
|
|
return key[0] != '\0';
|
2014-08-22 17:09:03 -04:00
|
|
|
|
}
|
2014-08-22 13:52:28 -04:00
|
|
|
|
};
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief An iterator to traverse all of the active entries in a
|
|
|
|
|
//! TSimpleStringDictionary.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
class Iterator {
|
|
|
|
|
public:
|
|
|
|
|
explicit Iterator(const TSimpleStringDictionary& map)
|
|
|
|
|
: map_(map),
|
|
|
|
|
current_(0) {
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 12:55:12 -07:00
|
|
|
|
Iterator(const Iterator&) = delete;
|
|
|
|
|
Iterator& operator=(const Iterator&) = delete;
|
|
|
|
|
|
2014-10-14 11:10:45 -04:00
|
|
|
|
//! \brief Returns the next entry in the map, or `nullptr` if at the end of
|
|
|
|
|
//! the collection.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
const Entry* Next() {
|
|
|
|
|
while (current_ < map_.num_entries) {
|
|
|
|
|
const Entry* entry = &map_.entries_[current_++];
|
|
|
|
|
if (entry->is_active()) {
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-14 11:10:45 -04:00
|
|
|
|
return nullptr;
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const TSimpleStringDictionary& map_;
|
|
|
|
|
size_t current_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TSimpleStringDictionary()
|
|
|
|
|
: entries_() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSimpleStringDictionary(const TSimpleStringDictionary& other) {
|
|
|
|
|
*this = other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSimpleStringDictionary& operator=(const TSimpleStringDictionary& other) {
|
|
|
|
|
memcpy(entries_, other.entries_, sizeof(entries_));
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Returns the number of active key/value pairs. The upper limit for
|
|
|
|
|
//! this is \a NumEntries.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
size_t GetCount() const {
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
for (size_t i = 0; i < num_entries; ++i) {
|
|
|
|
|
if (entries_[i].is_active()) {
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Given \a key, returns its corresponding value.
|
|
|
|
|
//!
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//! \param[in] key The key to look up. This must not be `nullptr`, nor an
|
|
|
|
|
//! empty string. It must not contain embedded `NUL`s.
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//!
|
|
|
|
|
//! \return The corresponding value for \a key, or if \a key is not found,
|
2014-10-14 11:10:45 -04:00
|
|
|
|
//! `nullptr`.
|
2017-08-30 10:03:33 -04:00
|
|
|
|
const char* GetValueForKey(base::StringPiece key) const {
|
|
|
|
|
DCHECK(key.data());
|
|
|
|
|
DCHECK(key.size());
|
|
|
|
|
DCHECK_EQ(key.find('\0', 0), base::StringPiece::npos);
|
|
|
|
|
if (!key.data() || !key.size()) {
|
2014-10-14 11:10:45 -04:00
|
|
|
|
return nullptr;
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Entry* entry = GetConstEntryForKey(key);
|
|
|
|
|
if (!entry) {
|
2014-10-14 11:10:45 -04:00
|
|
|
|
return nullptr;
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entry->value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Stores \a value into \a key, replacing the existing value if \a key
|
|
|
|
|
//! is already present.
|
|
|
|
|
//!
|
2014-10-14 11:10:45 -04:00
|
|
|
|
//! If \a key is not yet in the map and the map is already full (containing
|
|
|
|
|
//! \a NumEntries active entries), this operation silently fails.
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//!
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//! \param[in] key The key to store. This must not be `nullptr`, nor an empty
|
|
|
|
|
//! string. It must not contain embedded `NUL`s.
|
2014-10-14 11:10:45 -04:00
|
|
|
|
//! \param[in] value The value to store. If `nullptr`, \a key is removed from
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//! the map. Must not contain embedded `NUL`s.
|
|
|
|
|
void SetKeyValue(base::StringPiece key, base::StringPiece value) {
|
|
|
|
|
if (!value.data()) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
RemoveKey(key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 10:03:33 -04:00
|
|
|
|
DCHECK(key.data());
|
|
|
|
|
DCHECK(key.size());
|
|
|
|
|
DCHECK_EQ(key.find('\0', 0), base::StringPiece::npos);
|
|
|
|
|
if (!key.data() || !key.size()) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
// |key| must not be an empty string.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
DCHECK_NE(key[0], '\0');
|
|
|
|
|
if (key[0] == '\0') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 10:03:33 -04:00
|
|
|
|
// |value| must not contain embedded NULs.
|
|
|
|
|
DCHECK_EQ(value.find('\0', 0), base::StringPiece::npos);
|
|
|
|
|
|
2014-08-22 13:52:28 -04:00
|
|
|
|
Entry* entry = GetEntryForKey(key);
|
|
|
|
|
|
|
|
|
|
// If it does not yet exist, attempt to insert it.
|
|
|
|
|
if (!entry) {
|
|
|
|
|
for (size_t i = 0; i < num_entries; ++i) {
|
|
|
|
|
if (!entries_[i].is_active()) {
|
|
|
|
|
entry = &entries_[i];
|
2017-08-30 10:03:33 -04:00
|
|
|
|
SetFromStringPiece(key, entry->key, key_size);
|
2014-08-22 13:52:28 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 11:10:45 -04:00
|
|
|
|
// If the map is out of space, |entry| will be nullptr.
|
2014-08-22 13:52:28 -04:00
|
|
|
|
if (!entry) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
// Sanity check that the key only appears once.
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (size_t i = 0; i < num_entries; ++i) {
|
2017-08-30 10:03:33 -04:00
|
|
|
|
if (EntryKeyEquals(key, entries_[i])) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DCHECK_EQ(count, 1);
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-30 10:03:33 -04:00
|
|
|
|
SetFromStringPiece(value, entry->value, value_size);
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief Removes \a key from the map.
|
|
|
|
|
//!
|
2014-10-14 11:10:45 -04:00
|
|
|
|
//! If \a key is not found, this is a no-op.
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//!
|
2017-08-30 10:03:33 -04:00
|
|
|
|
//! \param[in] key The key of the entry to remove. This must not be `nullptr`,
|
|
|
|
|
//! nor an empty string. It must not contain embedded `NUL`s.
|
|
|
|
|
void RemoveKey(base::StringPiece key) {
|
|
|
|
|
DCHECK(key.data());
|
|
|
|
|
DCHECK(key.size());
|
|
|
|
|
DCHECK_EQ(key.find('\0', 0), base::StringPiece::npos);
|
|
|
|
|
if (!key.data() || !key.size()) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entry* entry = GetEntryForKey(key);
|
|
|
|
|
if (entry) {
|
|
|
|
|
entry->key[0] = '\0';
|
|
|
|
|
entry->value[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-06 16:44:38 -05:00
|
|
|
|
DCHECK_EQ(GetEntryForKey(key), implicit_cast<Entry*>(nullptr));
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2017-08-30 10:03:33 -04:00
|
|
|
|
static void SetFromStringPiece(base::StringPiece src,
|
|
|
|
|
char* dst,
|
|
|
|
|
size_t dst_size) {
|
|
|
|
|
size_t copy_len = std::min(dst_size - 1, src.size());
|
|
|
|
|
src.copy(dst, copy_len);
|
|
|
|
|
dst[copy_len] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool EntryKeyEquals(base::StringPiece key, const Entry& entry) {
|
|
|
|
|
if (key.size() >= KeySize)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Test for a NUL terminator and early out if it's absent.
|
|
|
|
|
if (entry.key[key.size()] != '\0')
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// As there's a NUL terminator at the right position in the entries
|
|
|
|
|
// string, strncmp can do the rest.
|
|
|
|
|
return strncmp(key.data(), entry.key, key.size()) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Entry* GetConstEntryForKey(base::StringPiece key) const {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
for (size_t i = 0; i < num_entries; ++i) {
|
2017-08-30 10:03:33 -04:00
|
|
|
|
if (EntryKeyEquals(key, entries_[i])) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
return &entries_[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-14 11:10:45 -04:00
|
|
|
|
return nullptr;
|
2014-08-22 13:52:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 10:03:33 -04:00
|
|
|
|
Entry* GetEntryForKey(base::StringPiece key) {
|
2014-08-22 13:52:28 -04:00
|
|
|
|
return const_cast<Entry*>(GetConstEntryForKey(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entry entries_[NumEntries];
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-22 17:09:03 -04:00
|
|
|
|
//! \brief A TSimpleStringDictionary with default template parameters.
|
|
|
|
|
//!
|
|
|
|
|
//! For historical reasons this specialized version is available with the same
|
|
|
|
|
//! size factors as a previous implementation.
|
2014-11-05 14:09:01 -05:00
|
|
|
|
using SimpleStringDictionary = TSimpleStringDictionary<256, 256, 64>;
|
2014-08-22 13:52:28 -04:00
|
|
|
|
|
2017-10-13 10:35:45 -04:00
|
|
|
|
static_assert(std::is_standard_layout<SimpleStringDictionary>::value,
|
|
|
|
|
"SimpleStringDictionary must be standard layout");
|
|
|
|
|
|
2014-08-22 13:52:28 -04:00
|
|
|
|
} // namespace crashpad
|
|
|
|
|
|
|
|
|
|
#endif // CRASHPAD_CLIENT_SIMPLE_STRING_DICTIONARY_H_
|