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