From afb059d3e508ad4fd38f4a9fd643a4bedf3e0b3f Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 16 May 2013 12:34:04 +0200 Subject: [PATCH] Define local strcasecmp funcs and use them when appropriate. --- include/yaz/matchstr.h | 19 +++++++++++++++++++ src/comstack.c | 13 ++++++------- src/http.c | 17 ++++++----------- src/matchstr.c | 21 +++++++++++++++++++++ test/test_matchstr.c | 24 ++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/include/yaz/matchstr.h b/include/yaz/matchstr.h index ff25c44..778fa39 100644 --- a/include/yaz/matchstr.h +++ b/include/yaz/matchstr.h @@ -67,6 +67,25 @@ YAZ_EXPORT int yaz_strcmp_del(const char *a, const char *b, const char *b_del); */ int yaz_memcmp(const void *a, const void *b, size_t len_a, size_t len_b); +/** \brief ala strncasecmp - no locale + \param s1 first buffer + \param s2 second buffer + \param n number of bytes to compare + \retval 0 buffers are equal + \retval >0 a > b + \retval <0 a < b +*/ +int yaz_strncasecmp(const char *s1, const char *s2, size_t n); + +/** \brief ala strcasecmp - no locale + \param s1 first buffer + \param s2 second buffer + \retval 0 buffers are equal + \retval >0 a > b + \retval <0 a < b +*/ +int yaz_strcasecmp(const char *s1, const char *s2); + YAZ_END_CDECL diff --git a/src/comstack.c b/src/comstack.c index eac17f7..1755fc7 100644 --- a/src/comstack.c +++ b/src/comstack.c @@ -19,10 +19,7 @@ #include #include #include - -#ifdef WIN32 -#define strncasecmp _strnicmp -#endif +#include #if HAVE_GNUTLS_H #define ENABLE_SSL 1 @@ -389,17 +386,19 @@ static int cs_complete_http(const char *buf, int len, int head_only) break; } else if (i < len - 20 && - !strncasecmp((const char *) buf+i, "Transfer-Encoding:", 18)) + !yaz_strncasecmp((const char *) buf+i, + "Transfer-Encoding:", 18)) { i+=18; while (buf[i] == ' ') i++; if (i < len - 8) - if (!strncasecmp((const char *) buf+i, "chunked", 7)) + if (!yaz_strncasecmp((const char *) buf+i, "chunked", 7)) chunked = 1; } else if (i < len - 17 && - !strncasecmp((const char *)buf+i, "Content-Length:", 15)) + !yaz_strncasecmp((const char *)buf+i, + "Content-Length:", 15)) { i+= 15; while (buf[i] == ' ') diff --git a/src/http.c b/src/http.c index 005a2e6..e4eecb9 100644 --- a/src/http.c +++ b/src/http.c @@ -17,11 +17,6 @@ #include #include -#ifdef WIN32 -#define strncasecmp _strnicmp -#define strcasecmp _stricmp -#endif - static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, char **content_buf, int *content_len) { @@ -64,9 +59,9 @@ static int decode_headers_content(ODR o, int off, Z_HTTP_Header **headers, memcpy ((*headers)->value, o->buf + po, i - po); (*headers)->value[i - po] = '\0'; - if (!strcasecmp((*headers)->name, "Transfer-Encoding") + if (!yaz_strcasecmp((*headers)->name, "Transfer-Encoding") && - !strcasecmp((*headers)->value, "chunked")) + !yaz_strcasecmp((*headers)->value, "chunked")) chunked = 1; headers = &(*headers)->next; if (i < o->size-1 && o->buf[i] == '\r') @@ -211,7 +206,7 @@ void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n, { while (*hp) { - if (!strcmp((*hp)->name, n)) + if (!yaz_strcasecmp((*hp)->name, n)) { (*hp)->value = odr_strdup(o, v); return; @@ -227,7 +222,7 @@ void z_HTTP_header_set(ODR o, Z_HTTP_Header **hp, const char *n, const char *z_HTTP_header_lookup(const Z_HTTP_Header *hp, const char *n) { for (; hp; hp = hp->next) - if (!yaz_matchstr(hp->name, n)) + if (!yaz_strcasecmp(hp->name, n)) return hp->value; return 0; } @@ -578,8 +573,8 @@ int yaz_encode_http_response(ODR o, Z_HTTP_Response *hr) odr_write2(o, sbuf, strlen(sbuf)); for (h = hr->headers; h; h = h->next) { - if (yaz_matchstr(h->name, "Content-Length") - && yaz_matchstr(h->name, "Transfer-Encoding")) + if (yaz_strcasecmp(h->name, "Content-Length") + && yaz_strcasecmp(h->name, "Transfer-Encoding")) { /* skip Content-Length if given. content_len rules */ odr_write2(o, h->name, strlen(h->name)); odr_write2(o, ": ", 2); diff --git a/src/matchstr.c b/src/matchstr.c index 101e777..82aa006 100644 --- a/src/matchstr.c +++ b/src/matchstr.c @@ -18,6 +18,27 @@ #include #include +int yaz_strcasecmp(const char *s1, const char *s2) +{ + return yaz_strncasecmp(s1, s2, strlen(s1) + 1); +} + +int yaz_strncasecmp(const char *s1, const char *s2, size_t n) +{ + while (n--) + { + unsigned char c1 = *s1++; + unsigned char c2 = *s2++; + if (yaz_isupper(c1)) + c1 = yaz_tolower(c1); + if (yaz_isupper(c2)) + c2 = yaz_tolower(c2); + if (c1 != c2) + return c1 - c2; + } + return 0; +} + int yaz_matchstr(const char *s1, const char *s2) { while (*s1 && *s2) diff --git a/test/test_matchstr.c b/test/test_matchstr.c index e623d1e..132776f 100644 --- a/test/test_matchstr.c +++ b/test/test_matchstr.c @@ -32,6 +32,30 @@ int main (int argc, char **argv) YAZ_CHECK(yaz_matchstr("a123", "a1.") > 0); YAZ_CHECK(yaz_matchstr("a123", "a...") == 0); + YAZ_CHECK_EQ(yaz_strncasecmp("a", "b", 0), 0); + YAZ_CHECK_EQ(yaz_strncasecmp("a", "a", 1), 0); + YAZ_CHECK_EQ(yaz_strncasecmp("a", "a", 2), 0); + YAZ_CHECK_EQ(yaz_strncasecmp("a", "b", 1), -1); + YAZ_CHECK_EQ(yaz_strncasecmp("a", "b", 2), -1); + YAZ_CHECK_EQ(yaz_strncasecmp("b", "a", 1), 1); + YAZ_CHECK_EQ(yaz_strncasecmp("b", "a", 2), 1); + + YAZ_CHECK_EQ(yaz_strncasecmp("bb", "ba", 1), 0); + YAZ_CHECK_EQ(yaz_strncasecmp("bb", "ba", 2), 1); + YAZ_CHECK_EQ(yaz_strncasecmp("ba", "bb", 2), -1); + YAZ_CHECK_EQ(yaz_strncasecmp("ba", "b", 2), 'a'); + YAZ_CHECK_EQ(yaz_strncasecmp("b", "ba", 2), -'a'); + + YAZ_CHECK_EQ(yaz_strcasecmp("", ""), 0); + YAZ_CHECK_EQ(yaz_strcasecmp("a", "a"), 0); + YAZ_CHECK_EQ(yaz_strcasecmp("a", "b"), -1); + YAZ_CHECK_EQ(yaz_strcasecmp("b", "a"), 1); + + YAZ_CHECK_EQ(yaz_strcasecmp("bb", "ba"), 1); + YAZ_CHECK_EQ(yaz_strcasecmp("ba", "bb"), -1); + YAZ_CHECK_EQ(yaz_strcasecmp("ba", "b"), 'a'); + YAZ_CHECK_EQ(yaz_strcasecmp("b", "ba"), -'a'); + YAZ_CHECK_TERM; } -- 1.7.10.4