From a605c06e12510600a67aaa86ea349037f613e53d Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Sun, 20 Nov 2005 19:55:42 +0000 Subject: Added hex character conversion utility in ctype git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@62 74dad513-b988-da41-8d7b-12977e46ad98 --- pjlib/include/pj/compat/string.h | 4 ++- pjlib/include/pj/ctype.h | 55 ++++++++++++++++++++++++++++++------- pjlib/include/pj/errno.h | 33 ++++++++++++----------- pjlib/include/pj/log.h | 13 ++++++++- pjlib/include/pj/string.h | 27 +++++++++++++++++++ pjlib/include/pj/string_i.h | 58 +++++++++++++++++++++++++++++----------- 6 files changed, 149 insertions(+), 41 deletions(-) (limited to 'pjlib/include/pj') 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 #include +PJ_BEGIN_DECL + /** * @defgroup pj_ctype ctype - Character Type * @ingroup PJ_MISC @@ -98,15 +101,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. @@ -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 - +#include PJ_BEGIN_DECL @@ -123,6 +123,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. 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 @@ -173,6 +173,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. * @@ -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) -- cgit v1.2.3