summaryrefslogtreecommitdiff
path: root/pjlib
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2005-11-20 19:55:42 +0000
committerBenny Prijono <bennylp@teluu.com>2005-11-20 19:55:42 +0000
commita605c06e12510600a67aaa86ea349037f613e53d (patch)
treecb001ad89f212ffaf6985eadc673230ccd25d7b9 /pjlib
parent66864977389a1a59bf7a5d38fcd62559190bb93e (diff)
Added hex character conversion utility in ctype
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@62 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r--pjlib/build/pjlib.dsp11
-rw-r--r--pjlib/include/pj/compat/string.h4
-rw-r--r--pjlib/include/pj/ctype.h55
-rw-r--r--pjlib/include/pj/errno.h33
-rw-r--r--pjlib/include/pj/log.h13
-rw-r--r--pjlib/include/pj/string.h27
-rw-r--r--pjlib/include/pj/string_i.h58
-rw-r--r--pjlib/src/pj/ctype.c24
-rw-r--r--pjlib/src/pj/log.c4
-rw-r--r--pjlib/src/pj/os_core_win32.c4
-rw-r--r--pjlib/src/pj/string.c12
11 files changed, 181 insertions, 64 deletions
diff --git a/pjlib/build/pjlib.dsp b/pjlib/build/pjlib.dsp
index a4222ca2..e4063e1b 100644
--- a/pjlib/build/pjlib.dsp
+++ b/pjlib/build/pjlib.dsp
@@ -178,6 +178,10 @@ SOURCE=..\src\pj\config.c
# End Source File
# Begin Source File
+SOURCE=..\src\pj\ctype.c
+# End Source File
+# Begin Source File
+
SOURCE=..\src\pj\equeue_winnt.c
# End Source File
# Begin Source File
@@ -234,13 +238,6 @@ SOURCE=..\src\pj\ioqueue_select.c
# Begin Source File
SOURCE=..\src\pj\ioqueue_winnt.c
-
-!IF "$(CFG)" == "pjlib - Win32 Release"
-
-!ELSEIF "$(CFG)" == "pjlib - Win32 Debug"
-
-!ENDIF
-
# End Source File
# Begin Source File
diff --git a/pjlib/include/pj/compat/string.h b/pjlib/include/pj/compat/string.h
index 939f9e2d..df739819 100644
--- a/pjlib/include/pj/compat/string.h
+++ b/pjlib/include/pj/compat/string.h
@@ -44,12 +44,14 @@
#define pj_native_strcmp strcmp
+#define pj_native_strncmp strncmp
#define pj_native_strlen strlen
#define pj_native_strcpy strcpy
#define pj_native_strstr strstr
#define pj_native_strchr strchr
#define pj_native_strcasecmp strcasecmp
+#define pj_native_stricmp strcasecmp
#define pj_native_strncasecmp strncasecmp
-
+#define pj_native_strnicmp strncasecmp
#endif /* __PJ_COMPAT_STRING_H__ */
diff --git a/pjlib/include/pj/ctype.h b/pjlib/include/pj/ctype.h
index 96e0b78b..e2ab1033 100644
--- a/pjlib/include/pj/ctype.h
+++ b/pjlib/include/pj/ctype.h
@@ -24,8 +24,11 @@
* @brief C type helper macros.
*/
+#include <pj/types.h>
#include <pj/compat/ctype.h>
+PJ_BEGIN_DECL
+
/**
* @defgroup pj_ctype ctype - Character Type
* @ingroup PJ_MISC
@@ -99,15 +102,6 @@ PJ_INLINE(int) pj_islower(int c) { return islower(c); }
PJ_INLINE(int) pj_isupper(int c) { return isupper(c); }
/**
- * Returns a non-zero value if c is a particular representation of
- * an hexadecimal digit character.
- * @param c The integer character to test.
- * @return Non-zero value if c is a particular representation of
- * an hexadecimal digit character.
- */
-PJ_INLINE(int) pj_isxdigit(int c){ return isxdigit(c); }
-
-/**
* Returns a non-zero value if c is a either a space (' ') or horizontal
* tab ('\\t') character.
* @param c The integer character to test.
@@ -130,7 +124,50 @@ PJ_INLINE(int) pj_tolower(int c) { return tolower(c); }
*/
PJ_INLINE(int) pj_toupper(int c) { return toupper(c); }
+/**
+ * Returns a non-zero value if c is a particular representation of
+ * an hexadecimal digit character.
+ * @param c The integer character to test.
+ * @return Non-zero value if c is a particular representation of
+ * an hexadecimal digit character.
+ */
+PJ_INLINE(int) pj_isxdigit(int c){ return isxdigit(c); }
+
+/**
+ * Array of hex digits, in lowerspace.
+ */
+extern char pj_hex_digits[];
+
+/**
+ * Convert a value to hex representation.
+ * @param value Integral value to convert.
+ * @param p Buffer to hold the hex representation, which must be
+ * at least two bytes length.
+ */
+PJ_INLINE(void) pj_val_to_hex_digit(unsigned value, char *p)
+{
+ *p++ = pj_hex_digits[ (value & 0xF0) >> 4 ];
+ *p = pj_hex_digits[ (value & 0x0F) ];
+}
+
+/**
+ * Convert hex digit c to integral value.
+ * @param c The hex digit character.
+ * @return The integral value between 0 and 15.
+ */
+PJ_INLINE(unsigned) pj_hex_digit_to_val(unsigned c)
+{
+ if (c <= '9')
+ return (c-'0') & 0x0F;
+ else if (c <= 'F')
+ return (c-'A'+10) & 0x0F;
+ else
+ return (c-'a'+10) & 0x0F;
+}
+
/** @} */
+PJ_END_DECL
+
#endif /* __PJ_CTYPE_H__ */
diff --git a/pjlib/include/pj/errno.h b/pjlib/include/pj/errno.h
index a5b92cc2..c45c3b8a 100644
--- a/pjlib/include/pj/errno.h
+++ b/pjlib/include/pj/errno.h
@@ -164,77 +164,77 @@ PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
* @hideinitializer
* Unknown error has been reported.
*/
-#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1)
+#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
/**
* @hideinitializer
* The operation is pending and will be completed later.
*/
-#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2)
+#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
/**
* @hideinitializer
* Too many connecting sockets.
*/
-#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3)
+#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
/**
* @hideinitializer
* Invalid argument.
*/
-#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4)
+#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
/**
* @hideinitializer
* Name too long (eg. hostname too long).
*/
-#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5)
+#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
/**
* @hideinitializer
* Not found.
*/
-#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6)
+#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
/**
* @hideinitializer
* Not enough memory.
*/
-#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7)
+#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
/**
* @hideinitializer
* Bug detected!
*/
-#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8)
+#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
/**
* @hideinitializer
* Operation timed out.
*/
-#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9)
+#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
/**
* @hideinitializer
* Too many objects.
*/
-#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)
+#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
/**
* @hideinitializer
* Object is busy.
*/
-#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)
+#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
/**
* @hideinitializer
* The specified option is not supported.
*/
-#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)
+#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
/**
* @hideinitializer
* Invalid operation.
*/
-#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)
+#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
/**
* @hideinitializer
* Operation is cancelled.
*/
-#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)
+#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
/**
* @hideinitializer
* Object already exists.
*/
-#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 14)
+#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
/** @} */ /* pj_errnum */
@@ -254,18 +254,21 @@ PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
/**
* PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
+ * Effectively the error in this class would be 70000 - 119000.
*/
#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
/**
* PJ_ERRNO_START_SYS converts platform specific error codes into
* pj_status_t values.
+ * Effectively the error in this class would be 120000 - 169000.
*/
#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
/**
* PJ_ERRNO_START_USER are reserved for applications that use error
* codes along with PJLIB codes.
+ * Effectively the error in this class would be 170000 - 219000.
*/
#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
diff --git a/pjlib/include/pj/log.h b/pjlib/include/pj/log.h
index 40cc9795..3f192da7 100644
--- a/pjlib/include/pj/log.h
+++ b/pjlib/include/pj/log.h
@@ -25,7 +25,7 @@
*/
#include <pj/types.h>
-
+#include <stdarg.h>
PJ_BEGIN_DECL
@@ -124,6 +124,17 @@ PJ_DECL(void) pj_log_write(int level, const char *buffer, int len);
#if PJ_LOG_MAX_LEVEL >= 1
/**
+ * Write to log.
+ *
+ * @param sender Source of the message.
+ * @param level Verbosity level.
+ * @param format Format.
+ * @param marker Marker.
+ */
+PJ_DECL(void) pj_log(const char *sender, int level,
+ const char *format, va_list marker);
+
+/**
* Change log output function. The front-end logging functions will call
* this function to write the actual message to the desired device.
* By default, the front-end functions use pj_log_write() to write
diff --git a/pjlib/include/pj/string.h b/pjlib/include/pj/string.h
index e1d553a9..44688cbe 100644
--- a/pjlib/include/pj/string.h
+++ b/pjlib/include/pj/string.h
@@ -174,6 +174,32 @@ PJ_IDECL(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src);
PJ_IDECL(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src);
/**
+ * Copy source string to destination up to the specified max length.
+ *
+ * @param dst The target string.
+ * @param src The source string.
+ * @param max Maximum characters to copy.
+ *
+ * @return the target string.
+ */
+PJ_IDECL(pj_str_t*) pj_strncpy(pj_str_t *dst, const pj_str_t *src,
+ pj_ssize_t max);
+
+/**
+ * Copy source string to destination up to the specified max length,
+ * and NULL terminate the destination. If source string length is
+ * greater than or equal to max, then max-1 will be copied.
+ *
+ * @param dst The target string.
+ * @param src The source string.
+ * @param max Maximum characters to copy.
+ *
+ * @return the target string.
+ */
+PJ_IDECL(pj_str_t*) pj_strncpy_with_null(pj_str_t *dst, const pj_str_t *src,
+ pj_ssize_t max);
+
+/**
* Duplicate string.
*
* @param pool The pool.
@@ -520,6 +546,7 @@ PJ_INLINE(void*) pj_memchr(const void *buf, int c, pj_size_t size)
return memchr(buf, c, size);
}
+
/**
* @}
*/
diff --git a/pjlib/include/pj/string_i.h b/pjlib/include/pj/string_i.h
index bab61ed5..7163b134 100644
--- a/pjlib/include/pj/string_i.h
+++ b/pjlib/include/pj/string_i.h
@@ -21,7 +21,7 @@ PJ_IDEF(pj_str_t) pj_str(char *str)
{
pj_str_t dst;
dst.ptr = str;
- dst.slen = str ? strlen(str) : 0;
+ dst.slen = str ? pj_native_strlen(str) : 0;
return dst;
}
@@ -56,7 +56,7 @@ PJ_IDEF(pj_str_t*) pj_strdup2(pj_pool_t *pool,
pj_str_t *dst,
const char *src)
{
- dst->slen = src ? strlen(src) : 0;
+ dst->slen = src ? pj_native_strlen(src) : 0;
if (dst->slen) {
dst->ptr = (char*)pj_pool_alloc(pool, dst->slen);
pj_memcpy(dst->ptr, src, dst->slen);
@@ -91,12 +91,36 @@ PJ_IDEF(pj_str_t*) pj_strcpy(pj_str_t *dst, const pj_str_t *src)
PJ_IDEF(pj_str_t*) pj_strcpy2(pj_str_t *dst, const char *src)
{
- dst->slen = src ? strlen(src) : 0;
+ dst->slen = src ? pj_native_strlen(src) : 0;
if (dst->slen > 0)
pj_memcpy(dst->ptr, src, dst->slen);
return dst;
}
+PJ_IDEF(pj_str_t*) pj_strncpy( pj_str_t *dst, const pj_str_t *src,
+ pj_ssize_t max)
+{
+ if (max > src->slen) max = src->slen;
+ pj_memcpy(dst->ptr, src->ptr, max);
+ dst->slen = max;
+ return dst;
+}
+
+PJ_IDEF(pj_str_t*) pj_strncpy_with_null( pj_str_t *dst, const pj_str_t *src,
+ pj_ssize_t max)
+{
+ if (max <= src->slen)
+ max = max-1;
+ else
+ max = src->slen;
+
+ pj_memcpy(dst->ptr, src->ptr, max);
+ dst->ptr[max] = '\0';
+ dst->slen = max;
+ return dst;
+}
+
+
PJ_IDEF(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2)
{
pj_ssize_t diff;
@@ -104,8 +128,8 @@ PJ_IDEF(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2)
diff = str1->slen - str2->slen;
if (diff) {
return (int)diff;
- } else if (str1->ptr) {
- return strncmp(str1->ptr, str2->ptr, str1->slen);
+ } else if (str1->ptr && str1->slen) {
+ return pj_native_strncmp(str1->ptr, str2->ptr, str1->slen);
} else {
return 0;
}
@@ -114,14 +138,15 @@ PJ_IDEF(int) pj_strcmp( const pj_str_t *str1, const pj_str_t *str2)
PJ_IDEF(int) pj_strncmp( const pj_str_t *str1, const pj_str_t *str2,
pj_size_t len)
{
- return (str1->ptr && str2->ptr) ? strncmp(str1->ptr, str2->ptr, len) :
- (str1->ptr == str2->ptr ? 0 : 1);
+ return (str1->ptr && str2->ptr) ?
+ pj_native_strncmp(str1->ptr, str2->ptr, len) :
+ (str1->ptr == str2->ptr ? 0 : 1);
}
PJ_IDEF(int) pj_strncmp2( const pj_str_t *str1, const char *str2,
pj_size_t len)
{
- return (str1->ptr && str2) ? strncmp(str1->ptr, str2, len) :
+ return (str1->ptr && str2) ? pj_native_strncmp(str1->ptr, str2, len) :
(str1->ptr==str2 ? 0 : 1);
}
@@ -138,28 +163,31 @@ PJ_IDEF(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2)
if (diff) {
return (int)diff;
} else {
- return strnicmp(str1->ptr, str2->ptr, str1->slen);
+ return pj_native_strnicmp(str1->ptr, str2->ptr, str1->slen);
}
}
PJ_IDEF(int) pj_stricmp2( const pj_str_t *str1, const char *str2)
{
- return (str1->ptr && str2) ? strnicmp(str1->ptr, str2, str1->slen) :
- (str1->ptr==str2 ? 0 : 1);
+ return (str1->ptr && str2) ?
+ pj_native_strnicmp(str1->ptr, str2, str1->slen) :
+ (str1->ptr==str2 ? 0 : 1);
}
PJ_IDEF(int) pj_strnicmp( const pj_str_t *str1, const pj_str_t *str2,
pj_size_t len)
{
- return (str1->ptr && str2->ptr) ? strnicmp(str1->ptr, str2->ptr, len) :
- (str1->ptr == str2->ptr ? 0 : 1);
+ return (str1->ptr && str2->ptr) ?
+ pj_native_strnicmp(str1->ptr, str2->ptr, len) :
+ (str1->ptr == str2->ptr ? 0 : 1);
}
PJ_IDEF(int) pj_strnicmp2( const pj_str_t *str1, const char *str2,
pj_size_t len)
{
- return (str1->ptr && str2) ? strnicmp(str1->ptr, str2, len) :
- (str1->ptr == str2 ? 0 : 1);
+ return (str1->ptr && str2) ?
+ pj_native_strnicmp(str1->ptr, str2, len) :
+ (str1->ptr == str2 ? 0 : 1);
}
PJ_IDEF(void) pj_strcat(pj_str_t *dst, const pj_str_t *src)
diff --git a/pjlib/src/pj/ctype.c b/pjlib/src/pj/ctype.c
new file mode 100644
index 00000000..b274ccaf
--- /dev/null
+++ b/pjlib/src/pj/ctype.c
@@ -0,0 +1,24 @@
+/* $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 <pj/ctype.h>
+
+
+char pj_hex_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+
diff --git a/pjlib/src/pj/log.c b/pjlib/src/pj/log.c
index 2e4e931b..55e7cf39 100644
--- a/pjlib/src/pj/log.c
+++ b/pjlib/src/pj/log.c
@@ -64,8 +64,8 @@ PJ_DEF(pj_log_func*) pj_log_get_log_func(void)
return log_writer;
}
-static void pj_log(const char *sender, int level,
- const char *format, va_list marker)
+PJ_DEF(void) pj_log( const char *sender, int level,
+ const char *format, va_list marker)
{
pj_time_val now;
pj_parsed_time ptime;
diff --git a/pjlib/src/pj/os_core_win32.c b/pjlib/src/pj/os_core_win32.c
index 05f3f341..b8e6b281 100644
--- a/pjlib/src/pj/os_core_win32.c
+++ b/pjlib/src/pj/os_core_win32.c
@@ -129,17 +129,13 @@ PJ_DEF(pj_status_t) pj_init(void)
pj_str_t guid;
pj_status_t rc;
- PJ_LOG(5, ("pj_init", "Initializing PJ Library.."));
-
/* Init Winsock.. */
if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) {
- PJ_LOG(1, ("pj_init", "Winsock initialization has returned an error"));
return PJ_RETURN_OS_ERROR(WSAGetLastError());
}
/* Init this thread's TLS. */
if ((rc=pj_thread_init()) != PJ_SUCCESS) {
- PJ_LOG(1, ("pj_init", "Thread initialization has returned an error"));
return rc;
}
diff --git a/pjlib/src/pj/string.c b/pjlib/src/pj/string.c
index 34a518fb..5c152fb5 100644
--- a/pjlib/src/pj/string.c
+++ b/pjlib/src/pj/string.c
@@ -27,9 +27,6 @@
#endif
-static char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
PJ_DEF(pj_str_t*) pj_strltrim( pj_str_t *str )
{
register char *p = str->ptr;
@@ -50,12 +47,6 @@ PJ_DEF(pj_str_t*) pj_strrtrim( pj_str_t *str )
return str;
}
-PJ_INLINE(void) pj_val_to_hex_digit(unsigned value, char *p)
-{
- *p++ = hex[ (value & 0xF0) >> 4 ];
- *p++ = hex[ (value & 0x0F) ];
-}
-
PJ_DEF(char*) pj_create_random_string(char *str, pj_size_t len)
{
unsigned i;
@@ -72,7 +63,7 @@ PJ_DEF(char*) pj_create_random_string(char *str, pj_size_t len)
p += 8;
}
for (i=i * 8; i<len; ++i) {
- *p++ = hex[ pj_rand() & 0x0F ];
+ *p++ = pj_hex_digits[ pj_rand() & 0x0F ];
}
return str;
}
@@ -129,3 +120,4 @@ PJ_DEF(int) pj_utoa_pad( unsigned long val, char *buf, int min_dig, int pad)
return len;
}
+