| 22 | #include "rtpengine_bracket.h" |
| 23 | |
| 24 | bencode_item_t *bracket_string_unescape(bencode_buffer_t *buf, |
| 25 | const char *s, int len) |
| 26 | { |
| 27 | int i, needs = 0; |
| 28 | |
| 29 | /* fast-path: scan for escape sequences before allocating anything */ |
| 30 | for (i = 0; i < len - 1; i++) { |
| 31 | if ((s[i] == '-' && s[i + 1] == '-') || |
| 32 | (s[i] == '.' && s[i + 1] == '.')) { |
| 33 | needs = 1; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | if (!needs) |
| 38 | /* no escapes found - zero-copy, points directly into input */ |
| 39 | return bencode_string_len(buf, s, len); |
| 40 | |
| 41 | /* slow path: unescape into a stack buffer, then copy into bencode. |
| 42 | * output is always <= input length since each 2-char escape becomes 1 */ |
| 43 | char tmp[BRACKET_MAX_LEN]; |
| 44 | int j = 0; |
| 45 | |
| 46 | for (i = 0; i < len; i++) { |
| 47 | if (i < len - 1 && s[i] == '-' && s[i + 1] == '-') { |
| 48 | tmp[j++] = '='; |
| 49 | i++; /* consume both dashes */ |
| 50 | } else if (i < len - 1 && s[i] == '.' && s[i + 1] == '.') { |
| 51 | tmp[j++] = ' '; |
| 52 | i++; /* consume both dots */ |
| 53 | } else { |
| 54 | tmp[j++] = s[i]; |
| 55 | } |
| 56 | } |
| 57 | /* _dup variant copies tmp into the bencode buffer (tmp is stack) */ |
| 58 | return bencode_string_len_dup(buf, tmp, j); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Two-pass parser for bracket content. |
no test coverage detected