| 794 | } |
| 795 | |
| 796 | static ssize_t |
| 797 | xo_escape_json (xo_buffer_t *xbp, ssize_t len, xo_xff_flags_t flags UNUSED) |
| 798 | { |
| 799 | ssize_t delta = 0; |
| 800 | char *cp, *ep, *ip; |
| 801 | |
| 802 | for (cp = xbp->xb_curp, ep = cp + len; cp < ep; cp++) { |
| 803 | if (*cp == '\\' || *cp == '"') |
| 804 | delta += 1; |
| 805 | else if (*cp == '\n' || *cp == '\r') |
| 806 | delta += 1; |
| 807 | } |
| 808 | |
| 809 | if (delta == 0) /* Nothing to escape; bail */ |
| 810 | return len; |
| 811 | |
| 812 | if (!xo_buf_has_room(xbp, delta)) /* No room; bail, but don't append */ |
| 813 | return 0; |
| 814 | |
| 815 | ep = xbp->xb_curp; |
| 816 | cp = ep + len; |
| 817 | ip = cp + delta; |
| 818 | do { |
| 819 | cp -= 1; |
| 820 | ip -= 1; |
| 821 | |
| 822 | if (*cp == '\\' || *cp == '"') { |
| 823 | *ip-- = *cp; |
| 824 | *ip = '\\'; |
| 825 | } else if (*cp == '\n') { |
| 826 | *ip-- = 'n'; |
| 827 | *ip = '\\'; |
| 828 | } else if (*cp == '\r') { |
| 829 | *ip-- = 'r'; |
| 830 | *ip = '\\'; |
| 831 | } else { |
| 832 | *ip = *cp; |
| 833 | } |
| 834 | |
| 835 | } while (cp > ep && cp != ip); |
| 836 | |
| 837 | return len + delta; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * PARAM-VALUE = UTF-8-STRING ; characters '"', '\' and |
no test coverage detected