handle NULL strings in strchr_m() and strrchr_m()
[kai/samba.git] / source4 / lib / charset / util_unistr.c
index 9b87f498006d2c4ef442e3f144c7c54e3c878517..5f3b2c53f120c9ebefde45211ecb7021dc52b983 100644 (file)
@@ -20,7 +20,7 @@
 
 #include "includes.h"
 #include "system/locale.h"
-#include "dynconfig.h"
+#include "dynconfig/dynconfig.h"
 #include "param/param.h"
 
 /**
@@ -67,7 +67,7 @@ static void load_case_tables(void)
 /**
  Convert a codepoint_t to upper case.
 **/
-codepoint_t toupper_w(codepoint_t val)
+_PUBLIC_ codepoint_t toupper_w(codepoint_t val)
 {
        if (val < 128) {
                return toupper(val);
@@ -87,7 +87,7 @@ codepoint_t toupper_w(codepoint_t val)
 /**
  Convert a codepoint_t to lower case.
 **/
-codepoint_t tolower_w(codepoint_t val)
+_PUBLIC_ codepoint_t tolower_w(codepoint_t val)
 {
        if (val < 128) {
                return tolower(val);
@@ -107,7 +107,7 @@ codepoint_t tolower_w(codepoint_t val)
 /**
   compare two codepoints case insensitively
 */
-int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
+_PUBLIC_ int codepoint_cmpi(codepoint_t c1, codepoint_t c2)
 {
        if (c1 == c2 ||
            toupper_w(c1) == toupper_w(c2)) {
@@ -386,6 +386,9 @@ _PUBLIC_ size_t strlen_m_term(const char *s)
 **/
 _PUBLIC_ char *strchr_m(const char *s, char c)
 {
+       if (s == NULL) {
+               return NULL;
+       }
        /* characters below 0x3F are guaranteed to not appear in
           non-initial position in multi-byte charsets */
        if ((c & 0xC0) == 0) {
@@ -411,6 +414,10 @@ _PUBLIC_ char *strrchr_m(const char *s, char c)
 {
        char *ret = NULL;
 
+       if (s == NULL) {
+               return NULL;
+       }
+
        /* characters below 0x3F are guaranteed to not appear in
           non-initial position in multi-byte charsets */
        if ((c & 0xC0) == 0) {
@@ -518,8 +525,9 @@ _PUBLIC_ char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
 
 /**
  Convert a string to UPPER case, allocated with talloc
+ source length limited to n bytes
 **/
-_PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
+_PUBLIC_ char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n)
 {
        size_t size=0;
        char *dest;
@@ -531,12 +539,12 @@ _PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
 
        /* this takes advantage of the fact that upper/lower can't
           change the length of a character by more than 1 byte */
-       dest = talloc_array(ctx, char, 2*(strlen(src))+1);
+       dest = talloc_array(ctx, char, 2*(n+1));
        if (dest == NULL) {
                return NULL;
        }
 
-       while (*src) {
+       while (*src && n--) {
                size_t c_size;
                codepoint_t c = next_codepoint(iconv_convenience, src, &c_size);
                src += c_size;
@@ -561,6 +569,22 @@ _PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
        return dest;
 }
 
+/**
+ Convert a string to UPPER case, allocated with talloc
+**/
+_PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
+{
+       return strupper_talloc_n(ctx, src, src?strlen(src):0);
+}
+
+/**
+ talloc_strdup() a unix string to upper case.
+**/
+_PUBLIC_ char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src)
+{
+       return strupper_talloc(ctx, src);
+}
+
 /**
  Convert a string to lower case.
 **/