Un-escape URI user -- it takes a pointer to original user str, as well as the new, unescaped one, which MUST have an allocated buffer linked to the 'str' structure ; (the buffer can be allocated with the same length as the original string -- the output string is always shorter (if escaped characters occur) or same-long as the original one). only printable characters are perm
| 574 | unescaped string otherwise |
| 575 | */ |
| 576 | inline static int un_escape(str *user, str *new_user ) |
| 577 | { |
| 578 | int i, j, value; |
| 579 | int hi, lo; |
| 580 | |
| 581 | if( new_user==0 || new_user->s==0) { |
| 582 | LM_CRIT("called with invalid param\n"); |
| 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | new_user->len = 0; |
| 587 | j = 0; |
| 588 | |
| 589 | for (i = 0; i < user->len; i++) { |
| 590 | if (user->s[i] == '%') { |
| 591 | if (i + 2 >= user->len) { |
| 592 | LM_ERR("escape sequence too short in" |
| 593 | " '%.*s' @ %d\n", |
| 594 | user->len, user->s, i ); |
| 595 | goto error; |
| 596 | } |
| 597 | hi=hex2int(user->s[i + 1]); |
| 598 | if (hi<0) { |
| 599 | LM_ERR(" non-hex high digit in an escape" |
| 600 | " sequence in '%.*s' @ %d\n", |
| 601 | user->len, user->s, i+1 ); |
| 602 | goto error; |
| 603 | } |
| 604 | lo=hex2int(user->s[i + 2]); |
| 605 | if (lo<0) { |
| 606 | LM_ERR("non-hex low digit in an escape sequence in " |
| 607 | "'%.*s' @ %d\n", |
| 608 | user->len, user->s, i+2 ); |
| 609 | goto error; |
| 610 | } |
| 611 | value=(hi<<4)+lo; |
| 612 | if (value < 32 || value > 126) { |
| 613 | LM_ERR("non-ASCII escaped character in '%.*s' @ %d\n", |
| 614 | user->len, user->s, i ); |
| 615 | goto error; |
| 616 | } |
| 617 | new_user->s[j] = value; |
| 618 | i+=2; /* consume the two hex digits, for cycle will move to the next char */ |
| 619 | } else { |
| 620 | new_user->s[j] = user->s[i]; |
| 621 | } |
| 622 | j++; /* good -- we translated another character */ |
| 623 | } |
| 624 | new_user->len = j; |
| 625 | return j; |
| 626 | |
| 627 | error: |
| 628 | new_user->len = j; |
| 629 | return -1; |
| 630 | } |
| 631 | |
| 632 | static inline void unescape_crlf(str *in_out) |
| 633 | { |
no test coverage detected