blob: f2a1508dcb39281761a480328c6a2e04b140bef6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/stringencode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <stdio.h>
14#include <stdlib.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19namespace rtc {
20
21/////////////////////////////////////////////////////////////////////////////
22// String Encoding Utilities
23/////////////////////////////////////////////////////////////////////////////
24
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025size_t url_decode(char * buffer, size_t buflen,
26 const char * source, size_t srclen) {
deadbeef37f5ecf2017-02-27 14:06:41 -080027 if (nullptr == buffer)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028 return srclen + 1;
29 if (buflen <= 0)
30 return 0;
31
32 unsigned char h1, h2;
33 size_t srcpos = 0, bufpos = 0;
34 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
35 unsigned char ch = source[srcpos++];
36 if (ch == '+') {
37 buffer[bufpos++] = ' ';
38 } else if ((ch == '%')
39 && (srcpos + 1 < srclen)
40 && hex_decode(source[srcpos], &h1)
41 && hex_decode(source[srcpos+1], &h2))
42 {
43 buffer[bufpos++] = (h1 << 4) | h2;
44 srcpos += 2;
45 } else {
46 buffer[bufpos++] = ch;
47 }
48 }
49 buffer[bufpos] = '\0';
50 return bufpos;
51}
52
53size_t utf8_decode(const char* source, size_t srclen, unsigned long* value) {
54 const unsigned char* s = reinterpret_cast<const unsigned char*>(source);
55 if ((s[0] & 0x80) == 0x00) { // Check s[0] == 0xxxxxxx
56 *value = s[0];
57 return 1;
58 }
59 if ((srclen < 2) || ((s[1] & 0xC0) != 0x80)) { // Check s[1] != 10xxxxxx
60 return 0;
61 }
62 // Accumulate the trailer byte values in value16, and combine it with the
63 // relevant bits from s[0], once we've determined the sequence length.
64 unsigned long value16 = (s[1] & 0x3F);
65 if ((s[0] & 0xE0) == 0xC0) { // Check s[0] == 110xxxxx
66 *value = ((s[0] & 0x1F) << 6) | value16;
67 return 2;
68 }
69 if ((srclen < 3) || ((s[2] & 0xC0) != 0x80)) { // Check s[2] != 10xxxxxx
70 return 0;
71 }
72 value16 = (value16 << 6) | (s[2] & 0x3F);
73 if ((s[0] & 0xF0) == 0xE0) { // Check s[0] == 1110xxxx
74 *value = ((s[0] & 0x0F) << 12) | value16;
75 return 3;
76 }
77 if ((srclen < 4) || ((s[3] & 0xC0) != 0x80)) { // Check s[3] != 10xxxxxx
78 return 0;
79 }
80 value16 = (value16 << 6) | (s[3] & 0x3F);
81 if ((s[0] & 0xF8) == 0xF0) { // Check s[0] == 11110xxx
82 *value = ((s[0] & 0x07) << 18) | value16;
83 return 4;
84 }
85 return 0;
86}
87
88size_t utf8_encode(char* buffer, size_t buflen, unsigned long value) {
89 if ((value <= 0x7F) && (buflen >= 1)) {
90 buffer[0] = static_cast<unsigned char>(value);
91 return 1;
92 }
93 if ((value <= 0x7FF) && (buflen >= 2)) {
94 buffer[0] = 0xC0 | static_cast<unsigned char>(value >> 6);
95 buffer[1] = 0x80 | static_cast<unsigned char>(value & 0x3F);
96 return 2;
97 }
98 if ((value <= 0xFFFF) && (buflen >= 3)) {
99 buffer[0] = 0xE0 | static_cast<unsigned char>(value >> 12);
100 buffer[1] = 0x80 | static_cast<unsigned char>((value >> 6) & 0x3F);
101 buffer[2] = 0x80 | static_cast<unsigned char>(value & 0x3F);
102 return 3;
103 }
104 if ((value <= 0x1FFFFF) && (buflen >= 4)) {
105 buffer[0] = 0xF0 | static_cast<unsigned char>(value >> 18);
106 buffer[1] = 0x80 | static_cast<unsigned char>((value >> 12) & 0x3F);
107 buffer[2] = 0x80 | static_cast<unsigned char>((value >> 6) & 0x3F);
108 buffer[3] = 0x80 | static_cast<unsigned char>(value & 0x3F);
109 return 4;
110 }
111 return 0;
112}
113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114static const char HEX[] = "0123456789abcdef";
115
116char hex_encode(unsigned char val) {
henrikg91d6ede2015-09-17 00:24:34 -0700117 RTC_DCHECK_LT(val, 16);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 return (val < 16) ? HEX[val] : '!';
119}
120
121bool hex_decode(char ch, unsigned char* val) {
122 if ((ch >= '0') && (ch <= '9')) {
123 *val = ch - '0';
124 } else if ((ch >= 'A') && (ch <= 'Z')) {
125 *val = (ch - 'A') + 10;
126 } else if ((ch >= 'a') && (ch <= 'z')) {
127 *val = (ch - 'a') + 10;
128 } else {
129 return false;
130 }
131 return true;
132}
133
134size_t hex_encode(char* buffer, size_t buflen,
135 const char* csource, size_t srclen) {
136 return hex_encode_with_delimiter(buffer, buflen, csource, srclen, 0);
137}
138
139size_t hex_encode_with_delimiter(char* buffer, size_t buflen,
140 const char* csource, size_t srclen,
141 char delimiter) {
Henrik Grunell84879882018-03-23 15:33:03 +0100142 RTC_DCHECK(buffer); // TODO(kwiberg): estimate output size
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 if (buflen == 0)
144 return 0;
145
146 // Init and check bounds.
147 const unsigned char* bsource =
148 reinterpret_cast<const unsigned char*>(csource);
149 size_t srcpos = 0, bufpos = 0;
150 size_t needed = delimiter ? (srclen * 3) : (srclen * 2 + 1);
151 if (buflen < needed)
152 return 0;
153
154 while (srcpos < srclen) {
155 unsigned char ch = bsource[srcpos++];
156 buffer[bufpos ] = hex_encode((ch >> 4) & 0xF);
157 buffer[bufpos+1] = hex_encode((ch ) & 0xF);
158 bufpos += 2;
159
160 // Don't write a delimiter after the last byte.
161 if (delimiter && (srcpos < srclen)) {
162 buffer[bufpos] = delimiter;
163 ++bufpos;
164 }
165 }
166
167 // Null terminate.
168 buffer[bufpos] = '\0';
169 return bufpos;
170}
171
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700172std::string hex_encode(const std::string& str) {
173 return hex_encode(str.c_str(), str.size());
174}
175
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176std::string hex_encode(const char* source, size_t srclen) {
177 return hex_encode_with_delimiter(source, srclen, 0);
178}
179
180std::string hex_encode_with_delimiter(const char* source, size_t srclen,
181 char delimiter) {
182 const size_t kBufferSize = srclen * 3;
183 char* buffer = STACK_ARRAY(char, kBufferSize);
184 size_t length = hex_encode_with_delimiter(buffer, kBufferSize,
185 source, srclen, delimiter);
henrikg91d6ede2015-09-17 00:24:34 -0700186 RTC_DCHECK(srclen == 0 || length > 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 return std::string(buffer, length);
188}
189
190size_t hex_decode(char * cbuffer, size_t buflen,
191 const char * source, size_t srclen) {
192 return hex_decode_with_delimiter(cbuffer, buflen, source, srclen, 0);
193}
194
195size_t hex_decode_with_delimiter(char* cbuffer, size_t buflen,
196 const char* source, size_t srclen,
197 char delimiter) {
Henrik Grunell84879882018-03-23 15:33:03 +0100198 RTC_DCHECK(cbuffer); // TODO(kwiberg): estimate output size
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 if (buflen == 0)
200 return 0;
201
202 // Init and bounds check.
203 unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer);
204 size_t srcpos = 0, bufpos = 0;
205 size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
206 if (buflen < needed)
207 return 0;
208
209 while (srcpos < srclen) {
210 if ((srclen - srcpos) < 2) {
211 // This means we have an odd number of bytes.
212 return 0;
213 }
214
215 unsigned char h1, h2;
216 if (!hex_decode(source[srcpos], &h1) ||
217 !hex_decode(source[srcpos + 1], &h2))
218 return 0;
219
220 bbuffer[bufpos++] = (h1 << 4) | h2;
221 srcpos += 2;
222
223 // Remove the delimiter if needed.
224 if (delimiter && (srclen - srcpos) > 1) {
225 if (source[srcpos] != delimiter)
226 return 0;
227 ++srcpos;
228 }
229 }
230
231 return bufpos;
232}
233
234size_t hex_decode(char* buffer, size_t buflen, const std::string& source) {
235 return hex_decode_with_delimiter(buffer, buflen, source, 0);
236}
237size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
238 const std::string& source, char delimiter) {
239 return hex_decode_with_delimiter(buffer, buflen,
240 source.c_str(), source.length(), delimiter);
241}
242
243size_t transform(std::string& value, size_t maxlen, const std::string& source,
244 Transform t) {
245 char* buffer = STACK_ARRAY(char, maxlen + 1);
246 size_t length = t(buffer, maxlen + 1, source.data(), source.length());
247 value.assign(buffer, length);
248 return length;
249}
250
251std::string s_transform(const std::string& source, Transform t) {
252 // Ask transformation function to approximate the destination size (returns upper bound)
deadbeef37f5ecf2017-02-27 14:06:41 -0800253 size_t maxlen = t(nullptr, 0, source.data(), source.length());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 char * buffer = STACK_ARRAY(char, maxlen);
255 size_t len = t(buffer, maxlen, source.data(), source.length());
256 std::string result(buffer, len);
257 return result;
258}
259
260size_t tokenize(const std::string& source, char delimiter,
261 std::vector<std::string>* fields) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262 fields->clear();
263 size_t last = 0;
264 for (size_t i = 0; i < source.length(); ++i) {
265 if (source[i] == delimiter) {
266 if (i != last) {
267 fields->push_back(source.substr(last, i - last));
268 }
269 last = i + 1;
270 }
271 }
272 if (last != source.length()) {
273 fields->push_back(source.substr(last, source.length() - last));
274 }
275 return fields->size();
276}
277
deadbeef0a6c4ca2015-10-06 11:38:28 -0700278size_t tokenize_with_empty_tokens(const std::string& source,
279 char delimiter,
280 std::vector<std::string>* fields) {
281 fields->clear();
282 size_t last = 0;
283 for (size_t i = 0; i < source.length(); ++i) {
284 if (source[i] == delimiter) {
285 fields->push_back(source.substr(last, i - last));
286 last = i + 1;
287 }
288 }
289 fields->push_back(source.substr(last, source.length() - last));
290 return fields->size();
291}
292
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293size_t tokenize_append(const std::string& source, char delimiter,
294 std::vector<std::string>* fields) {
295 if (!fields) return 0;
296
297 std::vector<std::string> new_fields;
298 tokenize(source, delimiter, &new_fields);
299 fields->insert(fields->end(), new_fields.begin(), new_fields.end());
300 return fields->size();
301}
302
303size_t tokenize(const std::string& source, char delimiter, char start_mark,
304 char end_mark, std::vector<std::string>* fields) {
305 if (!fields) return 0;
306 fields->clear();
307
308 std::string remain_source = source;
309 while (!remain_source.empty()) {
310 size_t start_pos = remain_source.find(start_mark);
311 if (std::string::npos == start_pos) break;
312 std::string pre_mark;
313 if (start_pos > 0) {
314 pre_mark = remain_source.substr(0, start_pos - 1);
315 }
316
317 ++start_pos;
318 size_t end_pos = remain_source.find(end_mark, start_pos);
319 if (std::string::npos == end_pos) break;
320
321 // We have found the matching marks. First tokenize the pre-mask. Then add
322 // the marked part as a single field. Finally, loop back for the post-mark.
323 tokenize_append(pre_mark, delimiter, fields);
324 fields->push_back(remain_source.substr(start_pos, end_pos - start_pos));
325 remain_source = remain_source.substr(end_pos + 1);
326 }
327
328 return tokenize_append(remain_source, delimiter, fields);
329}
330
Donald Curtis144d0182015-05-15 13:14:24 -0700331bool tokenize_first(const std::string& source,
332 const char delimiter,
333 std::string* token,
334 std::string* rest) {
Donald Curtis0e07f922015-05-15 09:21:23 -0700335 // Find the first delimiter
336 size_t left_pos = source.find(delimiter);
337 if (left_pos == std::string::npos) {
338 return false;
339 }
340
341 // Look for additional occurrances of delimiter.
342 size_t right_pos = left_pos + 1;
Donald Curtis144d0182015-05-15 13:14:24 -0700343 while (source[right_pos] == delimiter) {
Donald Curtis0e07f922015-05-15 09:21:23 -0700344 right_pos++;
345 }
346
347 *token = source.substr(0, left_pos);
348 *rest = source.substr(right_pos);
349 return true;
350}
351
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700352std::string join(const std::vector<std::string>& source, char delimiter) {
353 if (source.size() == 0) {
354 return std::string();
355 }
356 // Find length of the string to be returned to pre-allocate memory.
357 size_t source_string_length = 0;
358 for (size_t i = 0; i < source.size(); ++i) {
359 source_string_length += source[i].length();
360 }
361
362 // Build the joined string.
363 std::string joined_string;
364 joined_string.reserve(source_string_length + source.size() - 1);
365 for (size_t i = 0; i < source.size(); ++i) {
366 if (i != 0) {
367 joined_string += delimiter;
368 }
369 joined_string += source[i];
370 }
371 return joined_string;
372}
373
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374size_t split(const std::string& source, char delimiter,
375 std::vector<std::string>* fields) {
henrikg91d6ede2015-09-17 00:24:34 -0700376 RTC_DCHECK(fields);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000377 fields->clear();
378 size_t last = 0;
379 for (size_t i = 0; i < source.length(); ++i) {
380 if (source[i] == delimiter) {
381 fields->push_back(source.substr(last, i - last));
382 last = i + 1;
383 }
384 }
385 fields->push_back(source.substr(last, source.length() - last));
386 return fields->size();
387}
388
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000389} // namespace rtc