Telegram-Android/TMessagesProj/jni/voip/webrtc/rtc_base/string_encode.cc

285 lines
7.8 KiB
C++
Raw Permalink Normal View History

2020-03-30 15:00:09 +03:00
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
2020-08-14 19:58:22 +03:00
#include "rtc_base/string_encode.h"
2020-03-30 15:00:09 +03:00
#include <cstdio>
2023-02-19 01:24:25 +04:00
#include "absl/strings/string_view.h"
#include "api/array_view.h"
2020-03-30 15:00:09 +03:00
#include "rtc_base/arraysize.h"
#include "rtc_base/checks.h"
namespace rtc {
/////////////////////////////////////////////////////////////////////////////
// String Encoding Utilities
/////////////////////////////////////////////////////////////////////////////
2020-08-14 19:58:22 +03:00
namespace {
const char HEX[] = "0123456789abcdef";
2020-03-30 15:00:09 +03:00
2020-08-14 19:58:22 +03:00
// Convert an unsigned value from 0 to 15 to the hex character equivalent...
2020-03-30 15:00:09 +03:00
char hex_encode(unsigned char val) {
RTC_DCHECK_LT(val, 16);
return (val < 16) ? HEX[val] : '!';
}
2020-08-14 19:58:22 +03:00
// ...and vice-versa.
2020-03-30 15:00:09 +03:00
bool hex_decode(char ch, unsigned char* val) {
if ((ch >= '0') && (ch <= '9')) {
*val = ch - '0';
} else if ((ch >= 'A') && (ch <= 'F')) {
*val = (ch - 'A') + 10;
} else if ((ch >= 'a') && (ch <= 'f')) {
*val = (ch - 'a') + 10;
} else {
return false;
}
return true;
}
2020-08-14 19:58:22 +03:00
size_t hex_encode_output_length(size_t srclen, char delimiter) {
return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2);
2020-03-30 15:00:09 +03:00
}
2020-08-14 19:58:22 +03:00
// hex_encode shows the hex representation of binary data in ascii, with
2022-03-11 19:49:54 +03:00
// `delimiter` between bytes, or none if `delimiter` == 0.
2020-08-14 19:58:22 +03:00
void hex_encode_with_delimiter(char* buffer,
2023-02-19 01:24:25 +04:00
absl::string_view source,
2020-08-14 19:58:22 +03:00
char delimiter) {
RTC_DCHECK(buffer);
2020-03-30 15:00:09 +03:00
// Init and check bounds.
const unsigned char* bsource =
2023-02-19 01:24:25 +04:00
reinterpret_cast<const unsigned char*>(source.data());
2020-03-30 15:00:09 +03:00
size_t srcpos = 0, bufpos = 0;
2023-02-19 01:24:25 +04:00
size_t srclen = source.length();
2020-03-30 15:00:09 +03:00
while (srcpos < srclen) {
unsigned char ch = bsource[srcpos++];
buffer[bufpos] = hex_encode((ch >> 4) & 0xF);
buffer[bufpos + 1] = hex_encode((ch)&0xF);
bufpos += 2;
// Don't write a delimiter after the last byte.
if (delimiter && (srcpos < srclen)) {
buffer[bufpos] = delimiter;
++bufpos;
}
}
}
2020-08-14 19:58:22 +03:00
} // namespace
2023-02-19 01:24:25 +04:00
std::string hex_encode(absl::string_view str) {
return hex_encode_with_delimiter(str, 0);
2020-03-30 15:00:09 +03:00
}
2023-02-19 01:24:25 +04:00
std::string hex_encode_with_delimiter(absl::string_view source,
2020-03-30 15:00:09 +03:00
char delimiter) {
2023-02-19 01:24:25 +04:00
std::string s(hex_encode_output_length(source.length(), delimiter), 0);
hex_encode_with_delimiter(&s[0], source, delimiter);
2020-08-14 19:58:22 +03:00
return s;
2020-03-30 15:00:09 +03:00
}
2023-02-19 01:24:25 +04:00
size_t hex_decode_with_delimiter(ArrayView<char> cbuffer,
absl::string_view source,
2020-03-30 15:00:09 +03:00
char delimiter) {
2023-02-19 01:24:25 +04:00
if (cbuffer.empty())
2020-03-30 15:00:09 +03:00
return 0;
// Init and bounds check.
2023-02-19 01:24:25 +04:00
unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer.data());
2020-03-30 15:00:09 +03:00
size_t srcpos = 0, bufpos = 0;
2023-02-19 01:24:25 +04:00
size_t srclen = source.length();
2020-03-30 15:00:09 +03:00
size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
2023-02-19 01:24:25 +04:00
if (cbuffer.size() < needed)
2020-03-30 15:00:09 +03:00
return 0;
while (srcpos < srclen) {
if ((srclen - srcpos) < 2) {
// This means we have an odd number of bytes.
return 0;
}
unsigned char h1, h2;
if (!hex_decode(source[srcpos], &h1) ||
!hex_decode(source[srcpos + 1], &h2))
return 0;
bbuffer[bufpos++] = (h1 << 4) | h2;
srcpos += 2;
// Remove the delimiter if needed.
if (delimiter && (srclen - srcpos) > 1) {
if (source[srcpos] != delimiter)
return 0;
++srcpos;
}
}
return bufpos;
}
2023-02-19 01:24:25 +04:00
size_t hex_decode(ArrayView<char> buffer, absl::string_view source) {
return hex_decode_with_delimiter(buffer, source, 0);
2020-03-30 15:00:09 +03:00
}
2022-03-11 19:49:54 +03:00
size_t tokenize(absl::string_view source,
2020-03-30 15:00:09 +03:00
char delimiter,
std::vector<std::string>* fields) {
fields->clear();
size_t last = 0;
for (size_t i = 0; i < source.length(); ++i) {
if (source[i] == delimiter) {
if (i != last) {
2022-03-11 19:49:54 +03:00
fields->emplace_back(source.substr(last, i - last));
2020-03-30 15:00:09 +03:00
}
last = i + 1;
}
}
if (last != source.length()) {
2022-03-11 19:49:54 +03:00
fields->emplace_back(source.substr(last, source.length() - last));
2020-03-30 15:00:09 +03:00
}
return fields->size();
}
2022-03-11 19:49:54 +03:00
bool tokenize_first(absl::string_view source,
2020-03-30 15:00:09 +03:00
const char delimiter,
std::string* token,
std::string* rest) {
// Find the first delimiter
size_t left_pos = source.find(delimiter);
2023-02-19 01:24:25 +04:00
if (left_pos == absl::string_view::npos) {
2020-03-30 15:00:09 +03:00
return false;
}
// Look for additional occurrances of delimiter.
size_t right_pos = left_pos + 1;
2022-03-11 19:49:54 +03:00
while (right_pos < source.size() && source[right_pos] == delimiter) {
2020-03-30 15:00:09 +03:00
right_pos++;
}
2022-03-11 19:49:54 +03:00
*token = std::string(source.substr(0, left_pos));
*rest = std::string(source.substr(right_pos));
2020-03-30 15:00:09 +03:00
return true;
}
2023-02-19 01:24:25 +04:00
std::vector<absl::string_view> split(absl::string_view source, char delimiter) {
std::vector<absl::string_view> fields;
2022-03-13 04:58:00 +03:00
size_t last = 0;
for (size_t i = 0; i < source.length(); ++i) {
if (source[i] == delimiter) {
2023-02-19 01:24:25 +04:00
fields.push_back(source.substr(last, i - last));
2022-03-13 04:58:00 +03:00
last = i + 1;
}
2022-03-11 19:49:54 +03:00
}
2023-02-19 01:24:25 +04:00
fields.push_back(source.substr(last));
return fields;
2020-03-30 15:00:09 +03:00
}
std::string ToString(const bool b) {
return b ? "true" : "false";
}
2023-02-19 01:24:25 +04:00
std::string ToString(absl::string_view s) {
2020-03-30 15:00:09 +03:00
return std::string(s);
}
2023-02-19 01:24:25 +04:00
std::string ToString(const char* s) {
return std::string(s);
2020-03-30 15:00:09 +03:00
}
std::string ToString(const short s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const unsigned short s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const unsigned int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const long int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const unsigned long int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const long long int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const unsigned long long int s) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const double d) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const long double d) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
std::string ToString(const void* const p) {
char buf[32];
2020-08-14 19:58:22 +03:00
const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
2020-03-30 15:00:09 +03:00
RTC_DCHECK_LE(len, arraysize(buf));
return std::string(&buf[0], len);
}
2023-02-19 01:24:25 +04:00
bool FromString(absl::string_view s, bool* b) {
2020-03-30 15:00:09 +03:00
if (s == "false") {
*b = false;
return true;
}
if (s == "true") {
*b = true;
return true;
}
return false;
}
} // namespace rtc