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
11 #include "webrtc/base/stringencode.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include "webrtc/base/basictypes.h"
17 #include "webrtc/base/common.h"
18 #include "webrtc/base/stringutils.h"
19
20 namespace rtc {
21
22 /////////////////////////////////////////////////////////////////////////////
23 // String Encoding Utilities
24 /////////////////////////////////////////////////////////////////////////////
25
escape(char * buffer,size_t buflen,const char * source,size_t srclen,const char * illegal,char escape)26 size_t escape(char * buffer, size_t buflen,
27 const char * source, size_t srclen,
28 const char * illegal, char escape) {
29 ASSERT(NULL != buffer); // TODO: estimate output size
30 if (buflen <= 0)
31 return 0;
32
33 size_t srcpos = 0, bufpos = 0;
34 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
35 char ch = source[srcpos++];
36 if ((ch == escape) || ::strchr(illegal, ch)) {
37 if (bufpos + 2 >= buflen)
38 break;
39 buffer[bufpos++] = escape;
40 }
41 buffer[bufpos++] = ch;
42 }
43
44 buffer[bufpos] = '\0';
45 return bufpos;
46 }
47
unescape(char * buffer,size_t buflen,const char * source,size_t srclen,char escape)48 size_t unescape(char * buffer, size_t buflen,
49 const char * source, size_t srclen,
50 char escape) {
51 ASSERT(NULL != buffer); // TODO: estimate output size
52 if (buflen <= 0)
53 return 0;
54
55 size_t srcpos = 0, bufpos = 0;
56 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
57 char ch = source[srcpos++];
58 if ((ch == escape) && (srcpos < srclen)) {
59 ch = source[srcpos++];
60 }
61 buffer[bufpos++] = ch;
62 }
63 buffer[bufpos] = '\0';
64 return bufpos;
65 }
66
encode(char * buffer,size_t buflen,const char * source,size_t srclen,const char * illegal,char escape)67 size_t encode(char * buffer, size_t buflen,
68 const char * source, size_t srclen,
69 const char * illegal, char escape) {
70 ASSERT(NULL != buffer); // TODO: estimate output size
71 if (buflen <= 0)
72 return 0;
73
74 size_t srcpos = 0, bufpos = 0;
75 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
76 char ch = source[srcpos++];
77 if ((ch != escape) && !::strchr(illegal, ch)) {
78 buffer[bufpos++] = ch;
79 } else if (bufpos + 3 >= buflen) {
80 break;
81 } else {
82 buffer[bufpos+0] = escape;
83 buffer[bufpos+1] = hex_encode((static_cast<unsigned char>(ch) >> 4) & 0xF);
84 buffer[bufpos+2] = hex_encode((static_cast<unsigned char>(ch) ) & 0xF);
85 bufpos += 3;
86 }
87 }
88 buffer[bufpos] = '\0';
89 return bufpos;
90 }
91
decode(char * buffer,size_t buflen,const char * source,size_t srclen,char escape)92 size_t decode(char * buffer, size_t buflen,
93 const char * source, size_t srclen,
94 char escape) {
95 if (buflen <= 0)
96 return 0;
97
98 unsigned char h1, h2;
99 size_t srcpos = 0, bufpos = 0;
100 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
101 char ch = source[srcpos++];
102 if ((ch == escape)
103 && (srcpos + 1 < srclen)
104 && hex_decode(source[srcpos], &h1)
105 && hex_decode(source[srcpos+1], &h2)) {
106 buffer[bufpos++] = (h1 << 4) | h2;
107 srcpos += 2;
108 } else {
109 buffer[bufpos++] = ch;
110 }
111 }
112 buffer[bufpos] = '\0';
113 return bufpos;
114 }
115
unsafe_filename_characters()116 const char* unsafe_filename_characters() {
117 // It might be better to have a single specification which is the union of
118 // all operating systems, unless one system is overly restrictive.
119 #if defined(WEBRTC_WIN)
120 return "\\/:*?\"<>|";
121 #else // !WEBRTC_WIN
122 // TODO
123 ASSERT(false);
124 return "";
125 #endif // !WEBRTC_WIN
126 }
127
128 const unsigned char URL_UNSAFE = 0x1; // 0-33 "#$%&+,/:;<=>?@[\]^`{|} 127
129 const unsigned char XML_UNSAFE = 0x2; // "&'<>
130 const unsigned char HTML_UNSAFE = 0x2; // "&'<>
131
132 // ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 6 5 7 8 9 : ; < = > ?
133 //@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
134 //` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
135
136 const unsigned char ASCII_CLASS[128] = {
137 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
138 1,0,3,1,1,1,3,2,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,3,1,3,1,
139 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,
140 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,
141 };
142
url_encode(char * buffer,size_t buflen,const char * source,size_t srclen)143 size_t url_encode(char * buffer, size_t buflen,
144 const char * source, size_t srclen) {
145 if (NULL == buffer)
146 return srclen * 3 + 1;
147 if (buflen <= 0)
148 return 0;
149
150 size_t srcpos = 0, bufpos = 0;
151 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
152 unsigned char ch = source[srcpos++];
153 if ((ch < 128) && (ASCII_CLASS[ch] & URL_UNSAFE)) {
154 if (bufpos + 3 >= buflen) {
155 break;
156 }
157 buffer[bufpos+0] = '%';
158 buffer[bufpos+1] = hex_encode((ch >> 4) & 0xF);
159 buffer[bufpos+2] = hex_encode((ch ) & 0xF);
160 bufpos += 3;
161 } else {
162 buffer[bufpos++] = ch;
163 }
164 }
165 buffer[bufpos] = '\0';
166 return bufpos;
167 }
168
url_decode(char * buffer,size_t buflen,const char * source,size_t srclen)169 size_t url_decode(char * buffer, size_t buflen,
170 const char * source, size_t srclen) {
171 if (NULL == buffer)
172 return srclen + 1;
173 if (buflen <= 0)
174 return 0;
175
176 unsigned char h1, h2;
177 size_t srcpos = 0, bufpos = 0;
178 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
179 unsigned char ch = source[srcpos++];
180 if (ch == '+') {
181 buffer[bufpos++] = ' ';
182 } else if ((ch == '%')
183 && (srcpos + 1 < srclen)
184 && hex_decode(source[srcpos], &h1)
185 && hex_decode(source[srcpos+1], &h2))
186 {
187 buffer[bufpos++] = (h1 << 4) | h2;
188 srcpos += 2;
189 } else {
190 buffer[bufpos++] = ch;
191 }
192 }
193 buffer[bufpos] = '\0';
194 return bufpos;
195 }
196
utf8_decode(const char * source,size_t srclen,unsigned long * value)197 size_t utf8_decode(const char* source, size_t srclen, unsigned long* value) {
198 const unsigned char* s = reinterpret_cast<const unsigned char*>(source);
199 if ((s[0] & 0x80) == 0x00) { // Check s[0] == 0xxxxxxx
200 *value = s[0];
201 return 1;
202 }
203 if ((srclen < 2) || ((s[1] & 0xC0) != 0x80)) { // Check s[1] != 10xxxxxx
204 return 0;
205 }
206 // Accumulate the trailer byte values in value16, and combine it with the
207 // relevant bits from s[0], once we've determined the sequence length.
208 unsigned long value16 = (s[1] & 0x3F);
209 if ((s[0] & 0xE0) == 0xC0) { // Check s[0] == 110xxxxx
210 *value = ((s[0] & 0x1F) << 6) | value16;
211 return 2;
212 }
213 if ((srclen < 3) || ((s[2] & 0xC0) != 0x80)) { // Check s[2] != 10xxxxxx
214 return 0;
215 }
216 value16 = (value16 << 6) | (s[2] & 0x3F);
217 if ((s[0] & 0xF0) == 0xE0) { // Check s[0] == 1110xxxx
218 *value = ((s[0] & 0x0F) << 12) | value16;
219 return 3;
220 }
221 if ((srclen < 4) || ((s[3] & 0xC0) != 0x80)) { // Check s[3] != 10xxxxxx
222 return 0;
223 }
224 value16 = (value16 << 6) | (s[3] & 0x3F);
225 if ((s[0] & 0xF8) == 0xF0) { // Check s[0] == 11110xxx
226 *value = ((s[0] & 0x07) << 18) | value16;
227 return 4;
228 }
229 return 0;
230 }
231
utf8_encode(char * buffer,size_t buflen,unsigned long value)232 size_t utf8_encode(char* buffer, size_t buflen, unsigned long value) {
233 if ((value <= 0x7F) && (buflen >= 1)) {
234 buffer[0] = static_cast<unsigned char>(value);
235 return 1;
236 }
237 if ((value <= 0x7FF) && (buflen >= 2)) {
238 buffer[0] = 0xC0 | static_cast<unsigned char>(value >> 6);
239 buffer[1] = 0x80 | static_cast<unsigned char>(value & 0x3F);
240 return 2;
241 }
242 if ((value <= 0xFFFF) && (buflen >= 3)) {
243 buffer[0] = 0xE0 | static_cast<unsigned char>(value >> 12);
244 buffer[1] = 0x80 | static_cast<unsigned char>((value >> 6) & 0x3F);
245 buffer[2] = 0x80 | static_cast<unsigned char>(value & 0x3F);
246 return 3;
247 }
248 if ((value <= 0x1FFFFF) && (buflen >= 4)) {
249 buffer[0] = 0xF0 | static_cast<unsigned char>(value >> 18);
250 buffer[1] = 0x80 | static_cast<unsigned char>((value >> 12) & 0x3F);
251 buffer[2] = 0x80 | static_cast<unsigned char>((value >> 6) & 0x3F);
252 buffer[3] = 0x80 | static_cast<unsigned char>(value & 0x3F);
253 return 4;
254 }
255 return 0;
256 }
257
html_encode(char * buffer,size_t buflen,const char * source,size_t srclen)258 size_t html_encode(char * buffer, size_t buflen,
259 const char * source, size_t srclen) {
260 ASSERT(NULL != buffer); // TODO: estimate output size
261 if (buflen <= 0)
262 return 0;
263
264 size_t srcpos = 0, bufpos = 0;
265 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
266 unsigned char ch = source[srcpos];
267 if (ch < 128) {
268 srcpos += 1;
269 if (ASCII_CLASS[ch] & HTML_UNSAFE) {
270 const char * escseq = 0;
271 size_t esclen = 0;
272 switch (ch) {
273 case '<': escseq = "<"; esclen = 4; break;
274 case '>': escseq = ">"; esclen = 4; break;
275 case '\'': escseq = "'"; esclen = 5; break;
276 case '\"': escseq = """; esclen = 6; break;
277 case '&': escseq = "&"; esclen = 5; break;
278 default: ASSERT(false);
279 }
280 if (bufpos + esclen >= buflen) {
281 break;
282 }
283 memcpy(buffer + bufpos, escseq, esclen);
284 bufpos += esclen;
285 } else {
286 buffer[bufpos++] = ch;
287 }
288 } else {
289 // Largest value is 0x1FFFFF => � (10 characters)
290 char escseq[11];
291 unsigned long val;
292 if (size_t vallen = utf8_decode(&source[srcpos], srclen - srcpos, &val)) {
293 srcpos += vallen;
294 } else {
295 // Not a valid utf8 sequence, just use the raw character.
296 val = static_cast<unsigned char>(source[srcpos++]);
297 }
298 size_t esclen = sprintfn(escseq, ARRAY_SIZE(escseq), "&#%lu;", val);
299 if (bufpos + esclen >= buflen) {
300 break;
301 }
302 memcpy(buffer + bufpos, escseq, esclen);
303 bufpos += esclen;
304 }
305 }
306 buffer[bufpos] = '\0';
307 return bufpos;
308 }
309
html_decode(char * buffer,size_t buflen,const char * source,size_t srclen)310 size_t html_decode(char * buffer, size_t buflen,
311 const char * source, size_t srclen) {
312 ASSERT(NULL != buffer); // TODO: estimate output size
313 return xml_decode(buffer, buflen, source, srclen);
314 }
315
xml_encode(char * buffer,size_t buflen,const char * source,size_t srclen)316 size_t xml_encode(char * buffer, size_t buflen,
317 const char * source, size_t srclen) {
318 ASSERT(NULL != buffer); // TODO: estimate output size
319 if (buflen <= 0)
320 return 0;
321
322 size_t srcpos = 0, bufpos = 0;
323 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
324 unsigned char ch = source[srcpos++];
325 if ((ch < 128) && (ASCII_CLASS[ch] & XML_UNSAFE)) {
326 const char * escseq = 0;
327 size_t esclen = 0;
328 switch (ch) {
329 case '<': escseq = "<"; esclen = 4; break;
330 case '>': escseq = ">"; esclen = 4; break;
331 case '\'': escseq = "'"; esclen = 6; break;
332 case '\"': escseq = """; esclen = 6; break;
333 case '&': escseq = "&"; esclen = 5; break;
334 default: ASSERT(false);
335 }
336 if (bufpos + esclen >= buflen) {
337 break;
338 }
339 memcpy(buffer + bufpos, escseq, esclen);
340 bufpos += esclen;
341 } else {
342 buffer[bufpos++] = ch;
343 }
344 }
345 buffer[bufpos] = '\0';
346 return bufpos;
347 }
348
xml_decode(char * buffer,size_t buflen,const char * source,size_t srclen)349 size_t xml_decode(char * buffer, size_t buflen,
350 const char * source, size_t srclen) {
351 ASSERT(NULL != buffer); // TODO: estimate output size
352 if (buflen <= 0)
353 return 0;
354
355 size_t srcpos = 0, bufpos = 0;
356 while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
357 unsigned char ch = source[srcpos++];
358 if (ch != '&') {
359 buffer[bufpos++] = ch;
360 } else if ((srcpos + 2 < srclen)
361 && (memcmp(source + srcpos, "lt;", 3) == 0)) {
362 buffer[bufpos++] = '<';
363 srcpos += 3;
364 } else if ((srcpos + 2 < srclen)
365 && (memcmp(source + srcpos, "gt;", 3) == 0)) {
366 buffer[bufpos++] = '>';
367 srcpos += 3;
368 } else if ((srcpos + 4 < srclen)
369 && (memcmp(source + srcpos, "apos;", 5) == 0)) {
370 buffer[bufpos++] = '\'';
371 srcpos += 5;
372 } else if ((srcpos + 4 < srclen)
373 && (memcmp(source + srcpos, "quot;", 5) == 0)) {
374 buffer[bufpos++] = '\"';
375 srcpos += 5;
376 } else if ((srcpos + 3 < srclen)
377 && (memcmp(source + srcpos, "amp;", 4) == 0)) {
378 buffer[bufpos++] = '&';
379 srcpos += 4;
380 } else if ((srcpos < srclen) && (source[srcpos] == '#')) {
381 int int_base = 10;
382 if ((srcpos + 1 < srclen) && (source[srcpos+1] == 'x')) {
383 int_base = 16;
384 srcpos += 1;
385 }
386 char * ptr;
387 // TODO: Fix hack (ptr may go past end of data)
388 unsigned long val = strtoul(source + srcpos + 1, &ptr, int_base);
389 if ((static_cast<size_t>(ptr - source) < srclen) && (*ptr == ';')) {
390 srcpos = ptr - source + 1;
391 } else {
392 // Not a valid escape sequence.
393 break;
394 }
395 if (size_t esclen = utf8_encode(buffer + bufpos, buflen - bufpos, val)) {
396 bufpos += esclen;
397 } else {
398 // Not enough room to encode the character, or illegal character
399 break;
400 }
401 } else {
402 // Unrecognized escape sequence.
403 break;
404 }
405 }
406 buffer[bufpos] = '\0';
407 return bufpos;
408 }
409
410 static const char HEX[] = "0123456789abcdef";
411
hex_encode(unsigned char val)412 char hex_encode(unsigned char val) {
413 ASSERT(val < 16);
414 return (val < 16) ? HEX[val] : '!';
415 }
416
hex_decode(char ch,unsigned char * val)417 bool hex_decode(char ch, unsigned char* val) {
418 if ((ch >= '0') && (ch <= '9')) {
419 *val = ch - '0';
420 } else if ((ch >= 'A') && (ch <= 'Z')) {
421 *val = (ch - 'A') + 10;
422 } else if ((ch >= 'a') && (ch <= 'z')) {
423 *val = (ch - 'a') + 10;
424 } else {
425 return false;
426 }
427 return true;
428 }
429
hex_encode(char * buffer,size_t buflen,const char * csource,size_t srclen)430 size_t hex_encode(char* buffer, size_t buflen,
431 const char* csource, size_t srclen) {
432 return hex_encode_with_delimiter(buffer, buflen, csource, srclen, 0);
433 }
434
hex_encode_with_delimiter(char * buffer,size_t buflen,const char * csource,size_t srclen,char delimiter)435 size_t hex_encode_with_delimiter(char* buffer, size_t buflen,
436 const char* csource, size_t srclen,
437 char delimiter) {
438 ASSERT(NULL != buffer); // TODO: estimate output size
439 if (buflen == 0)
440 return 0;
441
442 // Init and check bounds.
443 const unsigned char* bsource =
444 reinterpret_cast<const unsigned char*>(csource);
445 size_t srcpos = 0, bufpos = 0;
446 size_t needed = delimiter ? (srclen * 3) : (srclen * 2 + 1);
447 if (buflen < needed)
448 return 0;
449
450 while (srcpos < srclen) {
451 unsigned char ch = bsource[srcpos++];
452 buffer[bufpos ] = hex_encode((ch >> 4) & 0xF);
453 buffer[bufpos+1] = hex_encode((ch ) & 0xF);
454 bufpos += 2;
455
456 // Don't write a delimiter after the last byte.
457 if (delimiter && (srcpos < srclen)) {
458 buffer[bufpos] = delimiter;
459 ++bufpos;
460 }
461 }
462
463 // Null terminate.
464 buffer[bufpos] = '\0';
465 return bufpos;
466 }
467
hex_encode(const char * source,size_t srclen)468 std::string hex_encode(const char* source, size_t srclen) {
469 return hex_encode_with_delimiter(source, srclen, 0);
470 }
471
hex_encode_with_delimiter(const char * source,size_t srclen,char delimiter)472 std::string hex_encode_with_delimiter(const char* source, size_t srclen,
473 char delimiter) {
474 const size_t kBufferSize = srclen * 3;
475 char* buffer = STACK_ARRAY(char, kBufferSize);
476 size_t length = hex_encode_with_delimiter(buffer, kBufferSize,
477 source, srclen, delimiter);
478 ASSERT(srclen == 0 || length > 0);
479 return std::string(buffer, length);
480 }
481
hex_decode(char * cbuffer,size_t buflen,const char * source,size_t srclen)482 size_t hex_decode(char * cbuffer, size_t buflen,
483 const char * source, size_t srclen) {
484 return hex_decode_with_delimiter(cbuffer, buflen, source, srclen, 0);
485 }
486
hex_decode_with_delimiter(char * cbuffer,size_t buflen,const char * source,size_t srclen,char delimiter)487 size_t hex_decode_with_delimiter(char* cbuffer, size_t buflen,
488 const char* source, size_t srclen,
489 char delimiter) {
490 ASSERT(NULL != cbuffer); // TODO: estimate output size
491 if (buflen == 0)
492 return 0;
493
494 // Init and bounds check.
495 unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer);
496 size_t srcpos = 0, bufpos = 0;
497 size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
498 if (buflen < needed)
499 return 0;
500
501 while (srcpos < srclen) {
502 if ((srclen - srcpos) < 2) {
503 // This means we have an odd number of bytes.
504 return 0;
505 }
506
507 unsigned char h1, h2;
508 if (!hex_decode(source[srcpos], &h1) ||
509 !hex_decode(source[srcpos + 1], &h2))
510 return 0;
511
512 bbuffer[bufpos++] = (h1 << 4) | h2;
513 srcpos += 2;
514
515 // Remove the delimiter if needed.
516 if (delimiter && (srclen - srcpos) > 1) {
517 if (source[srcpos] != delimiter)
518 return 0;
519 ++srcpos;
520 }
521 }
522
523 return bufpos;
524 }
525
hex_decode(char * buffer,size_t buflen,const std::string & source)526 size_t hex_decode(char* buffer, size_t buflen, const std::string& source) {
527 return hex_decode_with_delimiter(buffer, buflen, source, 0);
528 }
hex_decode_with_delimiter(char * buffer,size_t buflen,const std::string & source,char delimiter)529 size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
530 const std::string& source, char delimiter) {
531 return hex_decode_with_delimiter(buffer, buflen,
532 source.c_str(), source.length(), delimiter);
533 }
534
transform(std::string & value,size_t maxlen,const std::string & source,Transform t)535 size_t transform(std::string& value, size_t maxlen, const std::string& source,
536 Transform t) {
537 char* buffer = STACK_ARRAY(char, maxlen + 1);
538 size_t length = t(buffer, maxlen + 1, source.data(), source.length());
539 value.assign(buffer, length);
540 return length;
541 }
542
s_transform(const std::string & source,Transform t)543 std::string s_transform(const std::string& source, Transform t) {
544 // Ask transformation function to approximate the destination size (returns upper bound)
545 size_t maxlen = t(NULL, 0, source.data(), source.length());
546 char * buffer = STACK_ARRAY(char, maxlen);
547 size_t len = t(buffer, maxlen, source.data(), source.length());
548 std::string result(buffer, len);
549 return result;
550 }
551
tokenize(const std::string & source,char delimiter,std::vector<std::string> * fields)552 size_t tokenize(const std::string& source, char delimiter,
553 std::vector<std::string>* fields) {
554 ASSERT(NULL != fields);
555 fields->clear();
556 size_t last = 0;
557 for (size_t i = 0; i < source.length(); ++i) {
558 if (source[i] == delimiter) {
559 if (i != last) {
560 fields->push_back(source.substr(last, i - last));
561 }
562 last = i + 1;
563 }
564 }
565 if (last != source.length()) {
566 fields->push_back(source.substr(last, source.length() - last));
567 }
568 return fields->size();
569 }
570
tokenize_append(const std::string & source,char delimiter,std::vector<std::string> * fields)571 size_t tokenize_append(const std::string& source, char delimiter,
572 std::vector<std::string>* fields) {
573 if (!fields) return 0;
574
575 std::vector<std::string> new_fields;
576 tokenize(source, delimiter, &new_fields);
577 fields->insert(fields->end(), new_fields.begin(), new_fields.end());
578 return fields->size();
579 }
580
tokenize(const std::string & source,char delimiter,char start_mark,char end_mark,std::vector<std::string> * fields)581 size_t tokenize(const std::string& source, char delimiter, char start_mark,
582 char end_mark, std::vector<std::string>* fields) {
583 if (!fields) return 0;
584 fields->clear();
585
586 std::string remain_source = source;
587 while (!remain_source.empty()) {
588 size_t start_pos = remain_source.find(start_mark);
589 if (std::string::npos == start_pos) break;
590 std::string pre_mark;
591 if (start_pos > 0) {
592 pre_mark = remain_source.substr(0, start_pos - 1);
593 }
594
595 ++start_pos;
596 size_t end_pos = remain_source.find(end_mark, start_pos);
597 if (std::string::npos == end_pos) break;
598
599 // We have found the matching marks. First tokenize the pre-mask. Then add
600 // the marked part as a single field. Finally, loop back for the post-mark.
601 tokenize_append(pre_mark, delimiter, fields);
602 fields->push_back(remain_source.substr(start_pos, end_pos - start_pos));
603 remain_source = remain_source.substr(end_pos + 1);
604 }
605
606 return tokenize_append(remain_source, delimiter, fields);
607 }
608
split(const std::string & source,char delimiter,std::vector<std::string> * fields)609 size_t split(const std::string& source, char delimiter,
610 std::vector<std::string>* fields) {
611 ASSERT(NULL != fields);
612 fields->clear();
613 size_t last = 0;
614 for (size_t i = 0; i < source.length(); ++i) {
615 if (source[i] == delimiter) {
616 fields->push_back(source.substr(last, i - last));
617 last = i + 1;
618 }
619 }
620 fields->push_back(source.substr(last, source.length() - last));
621 return fields->size();
622 }
623
make_char_safe_for_filename(char c)624 char make_char_safe_for_filename(char c) {
625 if (c < 32)
626 return '_';
627
628 switch (c) {
629 case '<':
630 case '>':
631 case ':':
632 case '"':
633 case '/':
634 case '\\':
635 case '|':
636 case '*':
637 case '?':
638 return '_';
639
640 default:
641 return c;
642 }
643 }
644
645 /*
646 void sprintf(std::string& value, size_t maxlen, const char * format, ...) {
647 char * buffer = STACK_ARRAY(char, maxlen + 1);
648 va_list args;
649 va_start(args, format);
650 value.assign(buffer, vsprintfn(buffer, maxlen + 1, format, args));
651 va_end(args);
652 }
653 */
654
655 /////////////////////////////////////////////////////////////////////////////
656
657 } // namespace rtc
658