blob: 4958278ff074e56d927633178ccc76ad584db4c3 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include <pjlib-util/string.h>
21#include <pj/ctype.h>
22#include <pj/string.h>
23#include <pj/pool.h>
24
25PJ_DEF(pj_str_t) pj_str_unescape( pj_pool_t *pool, const pj_str_t *src_str)
26{
27 char *src = src_str->ptr;
28 char *end = src + src_str->slen;
29 pj_str_t dst_str;
30 char *dst;
31
32 if (pj_strchr(src_str, '%')==NULL)
33 return *src_str;
34
35 dst = dst_str.ptr = (char*) pj_pool_alloc(pool, src_str->slen);
36
37 while (src != end) {
38 if (*src == '%' && src < end-2 && pj_isxdigit(*(src+1)) &&
39 pj_isxdigit(*(src+2)))
40 {
41 *dst = (pj_uint8_t) ((pj_hex_digit_to_val(*(src+1)) << 4) +
42 pj_hex_digit_to_val(*(src+2)));
43 ++dst;
44 src += 3;
45 } else {
46 *dst++ = *src++;
47 }
48 }
49 dst_str.slen = dst - dst_str.ptr;
50 return dst_str;
51}
52
53PJ_DEF(pj_str_t*) pj_strcpy_unescape(pj_str_t *dst_str,
54 const pj_str_t *src_str)
55{
56 const char *src = src_str->ptr;
57 const char *end = src + src_str->slen;
58 char *dst = dst_str->ptr;
59
60 while (src != end) {
61 if (*src == '%' && src < end-2) {
62 *dst = (pj_uint8_t) ((pj_hex_digit_to_val(*(src+1)) << 4) +
63 pj_hex_digit_to_val(*(src+2)));
64 ++dst;
65 src += 3;
66 } else {
67 *dst++ = *src++;
68 }
69 }
70 dst_str->slen = dst - dst_str->ptr;
71 return dst_str;
72}
73
74PJ_DEF(pj_ssize_t) pj_strncpy2_escape( char *dst_str, const pj_str_t *src_str,
75 pj_ssize_t max, const pj_cis_t *unres)
76{
77 const char *src = src_str->ptr;
78 const char *src_end = src + src_str->slen;
79 char *dst = dst_str;
80 char *dst_end = dst + max;
81
82 if (max < src_str->slen)
83 return -1;
84
85 while (src != src_end && dst != dst_end) {
86 if (pj_cis_match(unres, *src)) {
87 *dst++ = *src++;
88 } else {
89 if (dst < dst_end-2) {
90 *dst++ = '%';
91 pj_val_to_hex_digit(*src, dst);
92 dst+=2;
93 ++src;
94 } else {
95 break;
96 }
97 }
98 }
99
100 return src==src_end ? dst-dst_str : -1;
101}
102
103PJ_DEF(pj_str_t*) pj_strncpy_escape(pj_str_t *dst_str,
104 const pj_str_t *src_str,
105 pj_ssize_t max, const pj_cis_t *unres)
106{
107 dst_str->slen = pj_strncpy2_escape(dst_str->ptr, src_str, max, unres);
108 return dst_str->slen < 0 ? NULL : dst_str;
109}
110