summaryrefslogtreecommitdiff
path: root/pjlib-util
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2005-11-20 19:57:22 +0000
committerBenny Prijono <bennylp@teluu.com>2005-11-20 19:57:22 +0000
commit37e8cb76f7b1f6e5dada1f8a06be4b963ee71be3 (patch)
tree23d5a584cbd77eb2855def2626d9184e4c6414de /pjlib-util
parenta605c06e12510600a67aaa86ea349037f613e53d (diff)
Added character (un)escaping utilities in string.h
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@63 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util')
-rw-r--r--pjlib-util/build/pjlib_util.dsp8
-rw-r--r--pjlib-util/include/pjlib-util/scanner.h2
-rw-r--r--pjlib-util/include/pjlib-util/string.h85
-rw-r--r--pjlib-util/src/pjlib-util/scanner.c1
-rw-r--r--pjlib-util/src/pjlib-util/string.c99
5 files changed, 195 insertions, 0 deletions
diff --git a/pjlib-util/build/pjlib_util.dsp b/pjlib-util/build/pjlib_util.dsp
index 599294df..f9521bdb 100644
--- a/pjlib-util/build/pjlib_util.dsp
+++ b/pjlib-util/build/pjlib_util.dsp
@@ -93,6 +93,10 @@ SOURCE="..\src\pjlib-util\scanner.c"
# End Source File
# Begin Source File
+SOURCE="..\src\pjlib-util\string.c"
+# End Source File
+# Begin Source File
+
SOURCE="..\src\pjlib-util\stun.c"
# End Source File
# Begin Source File
@@ -125,6 +129,10 @@ SOURCE="..\include\pjlib-util\scanner.h"
# End Source File
# Begin Source File
+SOURCE="..\include\pjlib-util\string.h"
+# End Source File
+# Begin Source File
+
SOURCE="..\include\pjlib-util\stun.h"
# End Source File
# Begin Source File
diff --git a/pjlib-util/include/pjlib-util/scanner.h b/pjlib-util/include/pjlib-util/scanner.h
index b2c58c85..209b2d36 100644
--- a/pjlib-util/include/pjlib-util/scanner.h
+++ b/pjlib-util/include/pjlib-util/scanner.h
@@ -195,6 +195,8 @@ PJ_DECL(void) pj_cis_invert( pj_cis_t *cis );
*
* @param cis The scanner character specification.
* @param c The character to check for matching.
+ *
+ * @return Non-zero if match (not necessarily one).
*/
PJ_INLINE(int) pj_cis_match( const pj_cis_t *cis, int c )
{
diff --git a/pjlib-util/include/pjlib-util/string.h b/pjlib-util/include/pjlib-util/string.h
new file mode 100644
index 00000000..bde48e28
--- /dev/null
+++ b/pjlib-util/include/pjlib-util/string.h
@@ -0,0 +1,85 @@
+/* $Id: $ */
+/*
+ * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#ifndef __PJLIB_UTIL_STRING_H__
+#define __PJLIB_UTIL_STRING_H__
+
+/**
+ * @file string.h
+ * @brief More string functions.
+ */
+
+#include <pj/types.h>
+#include <pjlib-util/scanner.h>
+
+PJ_BEGIN_DECL
+
+/**
+ * Unescape string.
+ *
+ * @param str The string to unescape.
+ */
+PJ_DECL(void) pj_str_unescape(pj_str_t *str);
+
+/**
+ * Unescape string to destination.
+ *
+ * @param dst Target string.
+ * @param src Source string.
+ *
+ * @return Target string.
+ */
+PJ_DECL(pj_str_t*) pj_strcpy_unescape(pj_str_t *dst, const pj_str_t *src);
+
+/**
+ * Copy string to destination while escaping reserved characters, up to
+ * the specified maximum length.
+ *
+ * @param dst Target string.
+ * @param src Source string.
+ * @param max Maximum length to copy to target string.
+ * @param unres Unreserved characters, which are allowed to appear
+ * unescaped.
+ *
+ * @return The target string if all characters have been copied
+ * successfully, or NULL if there's not enough buffer to
+ * escape the strings.
+ */
+PJ_DECL(pj_str_t*) pj_strncpy_escape(pj_str_t *dst, const pj_str_t *src,
+ pj_ssize_t max, const pj_cis_t *unres);
+
+
+/**
+ * Copy string to destination while escaping reserved characters, up to
+ * the specified maximum length.
+ *
+ * @param dst Target string.
+ * @param src Source string.
+ * @param max Maximum length to copy to target string.
+ * @param unres Unreserved characters, which are allowed to appear
+ * unescaped.
+ *
+ * @return The length of the destination, or -1 if there's not
+ * enough buffer.
+ */
+PJ_DECL(pj_ssize_t) pj_strncpy2_escape(char *dst, const pj_str_t *src,
+ pj_ssize_t max, const pj_cis_t *unres);
+
+PJ_END_DECL
+
+#endif /* __PJLIB_UTIL_STRING_H__ */
diff --git a/pjlib-util/src/pjlib-util/scanner.c b/pjlib-util/src/pjlib-util/scanner.c
index 4c19c906..27a1d4d0 100644
--- a/pjlib-util/src/pjlib-util/scanner.c
+++ b/pjlib-util/src/pjlib-util/scanner.c
@@ -47,6 +47,7 @@ PJ_DEF(pj_status_t) pj_cis_init(pj_cis_buf_t *cis_buf, pj_cis_t *cis)
for (i=0; i<PJ_CIS_MAX_INDEX; ++i) {
if ((cis_buf->use_mask & (1 << i)) == 0) {
cis->cis_id = i;
+ cis_buf->use_mask |= (1 << i);
return PJ_SUCCESS;
}
}
diff --git a/pjlib-util/src/pjlib-util/string.c b/pjlib-util/src/pjlib-util/string.c
new file mode 100644
index 00000000..55859eb6
--- /dev/null
+++ b/pjlib-util/src/pjlib-util/string.c
@@ -0,0 +1,99 @@
+/* $Id: $ */
+/*
+ * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <pjlib-util/string.h>
+#include <pj/ctype.h>
+
+PJ_DEF(void) pj_str_unescape(pj_str_t *str)
+{
+ char *src = str->ptr;
+ char *dst = str->ptr;
+ char *end = src + str->slen;
+
+ while (src != end) {
+ if (*src == '%' && src < end-2) {
+ *dst = (pj_uint8_t) ((pj_hex_digit_to_val(*(src+1)) << 4) +
+ pj_hex_digit_to_val(*(src+2)));
+ ++dst;
+ src += 3;
+ } else {
+ ++src;
+ ++dst;
+ }
+ }
+ str->slen = dst - str->ptr;
+}
+
+PJ_DEF(pj_str_t*) pj_strcpy_unescape(pj_str_t *dst_str,
+ const pj_str_t *src_str)
+{
+ const char *src = src_str->ptr;
+ const char *end = src + src_str->slen;
+ char *dst = dst_str->ptr;
+
+ while (src != end) {
+ if (*src == '%' && src < end-2) {
+ *dst = (pj_uint8_t) ((pj_hex_digit_to_val(*(src+1)) << 4) +
+ pj_hex_digit_to_val(*(src+2)));
+ ++dst;
+ src += 3;
+ } else {
+ *dst++ = *src++;
+ }
+ }
+ dst_str->slen = dst - dst_str->ptr;
+ return dst_str;
+}
+
+PJ_DEF(pj_ssize_t) pj_strncpy2_escape( char *dst_str, const pj_str_t *src_str,
+ pj_ssize_t max, const pj_cis_t *unres)
+{
+ const char *src = src_str->ptr;
+ const char *src_end = src + src_str->slen;
+ char *dst = dst_str;
+ char *dst_end = dst + max;
+
+ if (max < src_str->slen)
+ return -1;
+
+ while (src != src_end && dst != dst_end) {
+ if (pj_cis_match(unres, *src)) {
+ *dst++ = *src++;
+ } else {
+ if (dst < dst_end-2) {
+ *dst++ = '%';
+ pj_val_to_hex_digit(*src, dst);
+ dst+=2;
+ ++src;
+ } else {
+ break;
+ }
+ }
+ }
+
+ return src==src_end ? dst-dst_str : -1;
+}
+
+PJ_DEF(pj_str_t*) pj_strncpy_escape(pj_str_t *dst_str,
+ const pj_str_t *src_str,
+ pj_ssize_t max, const pj_cis_t *unres)
+{
+ dst_str->slen = pj_strncpy2_escape(dst_str->ptr, src_str, max, unres);
+ return dst_str->slen < 0 ? NULL : dst_str;
+}
+