blob: 434d1e6139293b3bcbab7371b759053b743cba63 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <cstdio>
14
Ali Tofigh7fa90572022-03-17 15:47:49 +010015#include "absl/strings/string_view.h"
Ali Tofighfd6a4d62022-03-31 10:36:48 +020016#include "api/array_view.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020017#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
22/////////////////////////////////////////////////////////////////////////////
23// String Encoding Utilities
24/////////////////////////////////////////////////////////////////////////////
25
Niels Möllere7e36012019-05-23 12:10:26 +020026namespace {
27const char HEX[] = "0123456789abcdef";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
Niels Möllere7e36012019-05-23 12:10:26 +020029// Convert an unsigned value from 0 to 15 to the hex character equivalent...
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030char hex_encode(unsigned char val) {
henrikg91d6ede2015-09-17 00:24:34 -070031 RTC_DCHECK_LT(val, 16);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032 return (val < 16) ? HEX[val] : '!';
33}
34
Niels Möllere7e36012019-05-23 12:10:26 +020035// ...and vice-versa.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036bool hex_decode(char ch, unsigned char* val) {
37 if ((ch >= '0') && (ch <= '9')) {
38 *val = ch - '0';
Niels Möller12048c72018-10-29 12:58:48 +010039 } else if ((ch >= 'A') && (ch <= 'F')) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 *val = (ch - 'A') + 10;
Niels Möller12048c72018-10-29 12:58:48 +010041 } else if ((ch >= 'a') && (ch <= 'f')) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 *val = (ch - 'a') + 10;
43 } else {
44 return false;
45 }
46 return true;
47}
48
Niels Möllerf25df352019-05-24 09:14:13 +020049size_t hex_encode_output_length(size_t srclen, char delimiter) {
50 return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2);
51}
52
53// hex_encode shows the hex representation of binary data in ascii, with
Artem Titov96e3b992021-07-26 16:03:14 +020054// `delimiter` between bytes, or none if `delimiter` == 0.
Niels Möllerf25df352019-05-24 09:14:13 +020055void hex_encode_with_delimiter(char* buffer,
Ali Tofighfd6a4d62022-03-31 10:36:48 +020056 absl::string_view source,
Niels Möllerf25df352019-05-24 09:14:13 +020057 char delimiter) {
58 RTC_DCHECK(buffer);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60 // Init and check bounds.
61 const unsigned char* bsource =
Ali Tofighfd6a4d62022-03-31 10:36:48 +020062 reinterpret_cast<const unsigned char*>(source.data());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 size_t srcpos = 0, bufpos = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
Ali Tofighfd6a4d62022-03-31 10:36:48 +020065 size_t srclen = source.length();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066 while (srcpos < srclen) {
67 unsigned char ch = bsource[srcpos++];
Yves Gerey665174f2018-06-19 15:03:05 +020068 buffer[bufpos] = hex_encode((ch >> 4) & 0xF);
69 buffer[bufpos + 1] = hex_encode((ch)&0xF);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 bufpos += 2;
71
72 // Don't write a delimiter after the last byte.
73 if (delimiter && (srcpos < srclen)) {
74 buffer[bufpos] = delimiter;
75 ++bufpos;
76 }
77 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078}
79
Niels Möllere7e36012019-05-23 12:10:26 +020080} // namespace
81
Ali Tofigh7fa90572022-03-17 15:47:49 +010082std::string hex_encode(absl::string_view str) {
Ali Tofighfd6a4d62022-03-31 10:36:48 +020083 return hex_encode_with_delimiter(str, 0);
Peter Thatcher1cf6f812015-05-15 10:40:45 -070084}
85
Ali Tofighfd6a4d62022-03-31 10:36:48 +020086std::string hex_encode_with_delimiter(absl::string_view source,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 char delimiter) {
Ali Tofighfd6a4d62022-03-31 10:36:48 +020088 std::string s(hex_encode_output_length(source.length(), delimiter), 0);
89 hex_encode_with_delimiter(&s[0], source, delimiter);
Niels Möllerf25df352019-05-24 09:14:13 +020090 return s;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091}
92
Ali Tofighfd6a4d62022-03-31 10:36:48 +020093size_t hex_decode_with_delimiter(ArrayView<char> cbuffer,
94 absl::string_view source,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 char delimiter) {
Ali Tofighfd6a4d62022-03-31 10:36:48 +020096 if (cbuffer.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 return 0;
98
99 // Init and bounds check.
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200100 unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer.data());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 size_t srcpos = 0, bufpos = 0;
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200102 size_t srclen = source.length();
103
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200105 if (cbuffer.size() < needed)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 return 0;
107
108 while (srcpos < srclen) {
109 if ((srclen - srcpos) < 2) {
110 // This means we have an odd number of bytes.
111 return 0;
112 }
113
114 unsigned char h1, h2;
115 if (!hex_decode(source[srcpos], &h1) ||
116 !hex_decode(source[srcpos + 1], &h2))
117 return 0;
118
119 bbuffer[bufpos++] = (h1 << 4) | h2;
120 srcpos += 2;
121
122 // Remove the delimiter if needed.
123 if (delimiter && (srclen - srcpos) > 1) {
124 if (source[srcpos] != delimiter)
125 return 0;
126 ++srcpos;
127 }
128 }
129
130 return bufpos;
131}
132
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200133size_t hex_decode(ArrayView<char> buffer, absl::string_view source) {
134 return hex_decode_with_delimiter(buffer, source, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135}
136
Niels Möller44203802021-09-16 15:39:16 +0200137size_t tokenize(absl::string_view source,
Yves Gerey665174f2018-06-19 15:03:05 +0200138 char delimiter,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 std::vector<std::string>* fields) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140 fields->clear();
141 size_t last = 0;
142 for (size_t i = 0; i < source.length(); ++i) {
143 if (source[i] == delimiter) {
144 if (i != last) {
Niels Möller44203802021-09-16 15:39:16 +0200145 fields->emplace_back(source.substr(last, i - last));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 }
147 last = i + 1;
148 }
149 }
150 if (last != source.length()) {
Niels Möller44203802021-09-16 15:39:16 +0200151 fields->emplace_back(source.substr(last, source.length() - last));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 }
153 return fields->size();
154}
155
Niels Möller44203802021-09-16 15:39:16 +0200156bool tokenize_first(absl::string_view source,
Donald Curtis144d0182015-05-15 13:14:24 -0700157 const char delimiter,
158 std::string* token,
159 std::string* rest) {
Donald Curtis0e07f922015-05-15 09:21:23 -0700160 // Find the first delimiter
161 size_t left_pos = source.find(delimiter);
Ali Tofigh7fa90572022-03-17 15:47:49 +0100162 if (left_pos == absl::string_view::npos) {
Donald Curtis0e07f922015-05-15 09:21:23 -0700163 return false;
164 }
165
166 // Look for additional occurrances of delimiter.
167 size_t right_pos = left_pos + 1;
Niels Möller44203802021-09-16 15:39:16 +0200168 while (right_pos < source.size() && source[right_pos] == delimiter) {
Donald Curtis0e07f922015-05-15 09:21:23 -0700169 right_pos++;
170 }
171
Niels Möller44203802021-09-16 15:39:16 +0200172 *token = std::string(source.substr(0, left_pos));
173 *rest = std::string(source.substr(right_pos));
Donald Curtis0e07f922015-05-15 09:21:23 -0700174 return true;
175}
176
Niels Möller2d3186e2022-01-24 14:15:03 +0100177std::vector<absl::string_view> split(absl::string_view source, char delimiter) {
178 std::vector<absl::string_view> fields;
179 size_t last = 0;
180 for (size_t i = 0; i < source.length(); ++i) {
181 if (source[i] == delimiter) {
182 fields.push_back(source.substr(last, i - last));
183 last = i + 1;
184 }
185 }
186 fields.push_back(source.substr(last));
187 return fields;
188}
189
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200190std::string ToString(const bool b) {
191 return b ? "true" : "false";
192}
193
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200194std::string ToString(absl::string_view s) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200195 return std::string(s);
196}
Ali Tofigh7fa90572022-03-17 15:47:49 +0100197
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200198std::string ToString(const char* s) {
Ali Tofigh7fa90572022-03-17 15:47:49 +0100199 return std::string(s);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200200}
201
202std::string ToString(const short s) {
203 char buf[32];
204 const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s);
205 RTC_DCHECK_LE(len, arraysize(buf));
206 return std::string(&buf[0], len);
207}
208std::string ToString(const unsigned short s) {
209 char buf[32];
210 const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s);
211 RTC_DCHECK_LE(len, arraysize(buf));
212 return std::string(&buf[0], len);
213}
214std::string ToString(const int s) {
215 char buf[32];
216 const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s);
217 RTC_DCHECK_LE(len, arraysize(buf));
218 return std::string(&buf[0], len);
219}
220std::string ToString(const unsigned int s) {
221 char buf[32];
222 const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s);
223 RTC_DCHECK_LE(len, arraysize(buf));
224 return std::string(&buf[0], len);
225}
226std::string ToString(const long int s) {
227 char buf[32];
228 const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s);
229 RTC_DCHECK_LE(len, arraysize(buf));
230 return std::string(&buf[0], len);
231}
232std::string ToString(const unsigned long int s) {
233 char buf[32];
234 const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s);
235 RTC_DCHECK_LE(len, arraysize(buf));
236 return std::string(&buf[0], len);
237}
238std::string ToString(const long long int s) {
239 char buf[32];
240 const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s);
241 RTC_DCHECK_LE(len, arraysize(buf));
242 return std::string(&buf[0], len);
243}
244std::string ToString(const unsigned long long int s) {
245 char buf[32];
246 const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s);
247 RTC_DCHECK_LE(len, arraysize(buf));
248 return std::string(&buf[0], len);
249}
250
251std::string ToString(const double d) {
252 char buf[32];
253 const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d);
254 RTC_DCHECK_LE(len, arraysize(buf));
255 return std::string(&buf[0], len);
256}
257
Jonas Olsson88e18482018-09-03 10:15:08 +0200258std::string ToString(const long double d) {
259 char buf[32];
260 const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
261 RTC_DCHECK_LE(len, arraysize(buf));
262 return std::string(&buf[0], len);
263}
264
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200265std::string ToString(const void* const p) {
266 char buf[32];
267 const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
268 RTC_DCHECK_LE(len, arraysize(buf));
269 return std::string(&buf[0], len);
270}
271
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200272bool FromString(absl::string_view s, bool* b) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200273 if (s == "false") {
274 *b = false;
275 return true;
276 }
277 if (s == "true") {
278 *b = true;
279 return true;
280 }
281 return false;
282}
283
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284} // namespace rtc