lib/util Use lib/util/ms_fnmatch.c in common for gen_fnmatch()
[abartlet/samba.git/.git] / source3 / lib / ms_fnmatch.c
index 9dc942c5f2ed3db3fe32ade3920b4d8c267b8088..272355b7d203d707c2aa943eba6c85480408fb86 100644 (file)
@@ -55,7 +55,7 @@ struct max_n {
 */
 static int ms_fnmatch_core(const smb_ucs2_t *p, const smb_ucs2_t *n,
                           struct max_n *max_n, const smb_ucs2_t *ldot,
-                          BOOL is_case_sensitive)
+                          bool is_case_sensitive)
 {
        smb_ucs2_t c;
        int i;
@@ -129,7 +129,7 @@ static int ms_fnmatch_core(const smb_ucs2_t *p, const smb_ucs2_t *n,
                                if (is_case_sensitive) {
                                        return -1;
                                }
-                               if (toupper_w(c) != toupper_w(*n)) {
+                               if (toupper_m(c) != toupper_m(*n)) {
                                        return -1;
                                }
                        }
@@ -145,13 +145,16 @@ static int ms_fnmatch_core(const smb_ucs2_t *p, const smb_ucs2_t *n,
        return -1;
 }
 
-int ms_fnmatch(const char *pattern, const char *string, BOOL translate_pattern,
-              BOOL is_case_sensitive)
+int ms_fnmatch(const char *pattern, const char *string, bool translate_pattern,
+              bool is_case_sensitive)
 {
        smb_ucs2_t *p = NULL;
        smb_ucs2_t *s = NULL;
        int ret, count, i;
        struct max_n *max_n = NULL;
+       struct max_n *max_n_free = NULL;
+       struct max_n one_max_n;
+       size_t converted_size;
 
        if (ISDOTDOT(string)) {
                string = ".";
@@ -167,12 +170,12 @@ int ms_fnmatch(const char *pattern, const char *string, BOOL translate_pattern,
                }
        }
 
-       if (push_ucs2_allocate(&p, pattern) == (size_t)-1) {
+       if (!push_ucs2_talloc(talloc_tos(), &p, pattern, &converted_size)) {
                return -1;
        }
 
-       if (push_ucs2_allocate(&s, string) == (size_t)-1) {
-               SAFE_FREE(p);
+       if (!push_ucs2_talloc(talloc_tos(), &s, string, &converted_size)) {
+               TALLOC_FREE(p);
                return -1;
        }
 
@@ -201,25 +204,28 @@ int ms_fnmatch(const char *pattern, const char *string, BOOL translate_pattern,
        }
 
        if (count != 0) {
-               max_n = SMB_CALLOC_ARRAY(struct max_n, count);
-               if (!max_n) {
-                       SAFE_FREE(p);
-                       SAFE_FREE(s);
-                       return -1;
+               if (count == 1) {
+                       /*
+                        * We're doing this a LOT, so save the effort to allocate
+                        */
+                       ZERO_STRUCT(one_max_n);
+                       max_n = &one_max_n;
+               }
+               else {
+                       max_n = SMB_CALLOC_ARRAY(struct max_n, count);
+                       if (!max_n) {
+                               TALLOC_FREE(p);
+                               TALLOC_FREE(s);
+                               return -1;
+                       }
+                       max_n_free = max_n;
                }
        }
 
        ret = ms_fnmatch_core(p, s, max_n, strrchr_w(s, UCS2_CHAR('.')), is_case_sensitive);
 
-       SAFE_FREE(max_n);
-       SAFE_FREE(p);
-       SAFE_FREE(s);
+       SAFE_FREE(max_n_free);
+       TALLOC_FREE(p);
+       TALLOC_FREE(s);
        return ret;
 }
-
-
-/* a generic fnmatch function - uses for non-CIFS pattern matching */
-int gen_fnmatch(const char *pattern, const char *string)
-{
-       return ms_fnmatch(pattern, string, PROTOCOL_NT1, False);
-}