OEMCrypto and OPK v19.3
This commit is contained in:
@@ -5,10 +5,11 @@
|
||||
#include "string_conversions.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include "log.h"
|
||||
@@ -155,6 +156,61 @@ std::vector<uint8_t> Base64DecodeInternal(const char* encoded, size_t length,
|
||||
result.resize(out_index);
|
||||
return result;
|
||||
}
|
||||
|
||||
// To prevent ID character type conversion errors, these
|
||||
// NormalizeIdChar() functions will safely return the
|
||||
// a char type without changing the byte values.
|
||||
constexpr char NormalizeIdChar(char ch) { return ch; }
|
||||
constexpr char NormalizeIdChar(uint8_t ch) { return static_cast<char>(ch); }
|
||||
|
||||
// An escapable ID char is a character which is considered
|
||||
// "human readable" and can be escaped by inserting a
|
||||
// backslash character before it.
|
||||
template <typename CharType>
|
||||
constexpr bool IsEscapableIdChar(CharType ch) {
|
||||
const char nch = NormalizeIdChar(ch);
|
||||
return nch == '"' || nch == '\\';
|
||||
}
|
||||
|
||||
// A "human readable" ID character is any ASCII character
|
||||
// which produces a glyph or a space character.
|
||||
template <typename CharType>
|
||||
bool IsHumanReadableIdChar(CharType ch) {
|
||||
const char nch = NormalizeIdChar(ch);
|
||||
return isgraph(nch) || nch == ' ';
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
std::string SafeByteIdToStringInternal(const Container& id) {
|
||||
using CharType = typename Container::value_type;
|
||||
if (id.empty()) {
|
||||
return "<empty>";
|
||||
}
|
||||
|
||||
// Check if already human readable.
|
||||
const bool human_readable =
|
||||
std::all_of(id.begin(), id.end(), IsHumanReadableIdChar<CharType>);
|
||||
if (!human_readable) {
|
||||
// For non-human readable IDs, just return the hex encoded version.
|
||||
return b2a_hex(id);
|
||||
}
|
||||
|
||||
// For human readable IDs, add quotes, and escape any double quotes
|
||||
// and backslashes.
|
||||
const size_t escape_count =
|
||||
std::count_if(id.begin(), id.end(), IsEscapableIdChar<CharType>);
|
||||
std::string result;
|
||||
result.reserve(id.size() + escape_count + 2);
|
||||
result.push_back('"');
|
||||
for (const auto& ch : id) {
|
||||
if (IsEscapableIdChar(ch)) {
|
||||
result.push_back('\\');
|
||||
}
|
||||
result.push_back(ch);
|
||||
}
|
||||
result.push_back('"');
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// converts an ascii hex string(2 bytes per digit) into a decimal byte string
|
||||
@@ -340,4 +396,11 @@ std::string EncodeUint32(uint32_t u) {
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string SafeByteIdToString(const std::string& id) {
|
||||
return SafeByteIdToStringInternal(id);
|
||||
}
|
||||
|
||||
std::string SafeByteIdToString(const std::vector<uint8_t>& id) {
|
||||
return SafeByteIdToStringInternal(id);
|
||||
}
|
||||
} // namespace wvutil
|
||||
|
||||
Reference in New Issue
Block a user