1
/* $Id: string.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pjlib-util/string.h>
22
#include <pj/string.h>
25
PJ_DEF(pj_str_t) pj_str_unescape( pj_pool_t *pool, const pj_str_t *src_str)
27
char *src = src_str->ptr;
28
char *end = src + src_str->slen;
32
if (pj_strchr(src_str, '%')==NULL)
35
dst = dst_str.ptr = (char*) pj_pool_alloc(pool, src_str->slen);
38
if (*src == '%' && src < end-2 && pj_isxdigit(*(src+1)) &&
39
pj_isxdigit(*(src+2)))
41
*dst = (pj_uint8_t) ((pj_hex_digit_to_val(*(src+1)) << 4) +
42
pj_hex_digit_to_val(*(src+2)));
49
dst_str.slen = dst - dst_str.ptr;
53
PJ_DEF(pj_str_t*) pj_strcpy_unescape(pj_str_t *dst_str,
54
const pj_str_t *src_str)
56
const char *src = src_str->ptr;
57
const char *end = src + src_str->slen;
58
char *dst = dst_str->ptr;
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)));
70
dst_str->slen = dst - dst_str->ptr;
74
PJ_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)
77
const char *src = src_str->ptr;
78
const char *src_end = src + src_str->slen;
80
char *dst_end = dst + max;
82
if (max < src_str->slen)
85
while (src != src_end && dst != dst_end) {
86
if (pj_cis_match(unres, *src)) {
89
if (dst < dst_end-2) {
91
pj_val_to_hex_digit(*src, dst);
100
return src==src_end ? dst-dst_str : -1;
103
PJ_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)
107
dst_str->slen = pj_strncpy2_escape(dst_str->ptr, src_str, max, unres);
108
return dst_str->slen < 0 ? NULL : dst_str;