s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[kamenim/samba.git] / source4 / ntvfs / posix / pvfs_shortname.c
index 818c285fbc8388315bd24ea1fa1a712e7e248dc7..bdd86f20973a2a8545db648352f53e88110de6d2 100644 (file)
@@ -7,7 +7,7 @@
 
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    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., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "include/includes.h"
+#include "includes.h"
+#include "system/locale.h"
 #include "vfs_posix.h"
+#include "param/param.h"
 
 /*
   this mangling scheme uses the following format
   for simplicity, we only allow ascii characters in 8.3 names
 */
 
- /* hash alghorithm changed to FNV1 by idra@samba.org (Simo Sorce).
-  * see http://www.isthe.com/chongo/tech/comp/fnv/index.html for a
-  * discussion on Fowler / Noll / Vo (FNV) Hash by one of it's authors
-  */
-
 /*
   ===============================================================================
   NOTE NOTE NOTE!!!
@@ -55,9 +51,6 @@
 */
 
 
-#include "includes.h"
-#include "vfs_posix.h"
-
 #if 1
 #define M_DEBUG(level, x) DEBUG(level, x)
 #else
 #define FLAG_POSSIBLE3 64
 #define FLAG_POSSIBLE4 128
 
-/* by default have a max of 512 entries in the cache. */
-#ifndef MANGLE_CACHE_SIZE
-#define MANGLE_CACHE_SIZE 512
-#endif
-
 #define DEFAULT_MANGLE_PREFIX 4
 
-#define FNV1_PRIME 0x01000193
-/*the following number is a fnv1 of the string: idra@samba.org 2002 */
-#define FNV1_INIT  0xa6b93095
-
 #define MANGLE_BASECHARS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 #define FLAG_CHECK(c, flag) (ctx->char_flags[(uint8_t)(c)] & (flag))
 
+static const char *reserved_names[] = 
+{ "AUX", "CON", "COM1", "COM2", "COM3", "COM4",
+  "LPT1", "LPT2", "LPT3", "NUL", "PRN", NULL };
+
+
+struct pvfs_mangle_context {
+       uint8_t char_flags[256];
+       /*
+         this determines how many characters are used from the original
+         filename in the 8.3 mangled name. A larger value leads to a weaker
+         hash and more collisions.  The largest possible value is 6.
+       */
+       int mangle_prefix;
+       uint32_t mangle_modulus;
+
+       /* we will use a very simple direct mapped prefix cache. The big
+          advantage of this cache structure is speed and low memory usage 
+
+          The cache is indexed by the low-order bits of the hash, and confirmed by
+          hashing the resulting cache entry to match the known hash
+       */
+       uint32_t cache_size;
+       char **prefix_cache;
+       uint32_t *prefix_cache_hashes;
+
+       /* this is used to reverse the base 36 mapping */
+       unsigned char base_reverse[256];
+};
+
 
 /* 
    hash a string of the specified length. The string does not need to be
 static uint32_t mangle_hash(struct pvfs_mangle_context *ctx,
                            const char *key, size_t length)
 {
-       uint32_t value = FNV1_INIT;
-       codepoint_t c;
-       size_t c_size;
-
-       while (*key && length--) {
-               c = next_codepoint(key, &c_size);
-               c = toupper_w(c);
-                value *= (uint32_t)FNV1_PRIME;
-                value ^= (uint32_t)c;
-               key += c_size;
-        }
-
-       return (value % ctx->mangle_modulus);
+       return pvfs_name_hash(key, length) % ctx->mangle_modulus;
 }
 
 /*
@@ -124,7 +125,7 @@ static uint32_t mangle_hash(struct pvfs_mangle_context *ctx,
 static void cache_insert(struct pvfs_mangle_context *ctx,
                         const char *prefix, int length, uint32_t hash)
 {
-       int i = hash % MANGLE_CACHE_SIZE;
+       int i = hash % ctx->cache_size;
 
        if (ctx->prefix_cache[i]) {
                talloc_free(ctx->prefix_cache[i]);
@@ -139,7 +140,7 @@ static void cache_insert(struct pvfs_mangle_context *ctx,
 */
 static const char *cache_lookup(struct pvfs_mangle_context *ctx, uint32_t hash)
 {
-       int i = hash % MANGLE_CACHE_SIZE;
+       int i = hash % ctx->cache_size;
 
 
        if (!ctx->prefix_cache[i] || hash != ctx->prefix_cache_hashes[i]) {
@@ -158,7 +159,7 @@ static const char *cache_lookup(struct pvfs_mangle_context *ctx, uint32_t hash)
    In this algorithm, mangled names use only pure ascii characters (no
    multi-byte) so we can avoid doing a UCS2 conversion 
  */
-static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
+static bool is_mangled_component(struct pvfs_mangle_context *ctx,
                                 const char *name, size_t len)
 {
        unsigned int i;
@@ -167,19 +168,19 @@ static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
 
        /* check the length */
        if (len > 12 || len < 8)
-               return False;
+               return false;
 
        /* the best distinguishing characteristic is the ~ */
        if (name[6] != '~')
-               return False;
+               return false;
 
        /* check extension */
        if (len > 8) {
                if (name[8] != '.')
-                       return False;
+                       return false;
                for (i=9; name[i] && i < len; i++) {
                        if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
-                               return False;
+                               return false;
                        }
                }
        }
@@ -187,23 +188,23 @@ static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
        /* check lead characters */
        for (i=0;i<ctx->mangle_prefix;i++) {
                if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
-                       return False;
+                       return false;
                }
        }
        
        /* check rest of hash */
        if (! FLAG_CHECK(name[7], FLAG_BASECHAR)) {
-               return False;
+               return false;
        }
        for (i=ctx->mangle_prefix;i<6;i++) {
                if (! FLAG_CHECK(name[i], FLAG_BASECHAR)) {
-                       return False;
+                       return false;
                }
        }
 
        M_DEBUG(10,("is_mangled_component %s (len %u) -> yes\n", name, (unsigned int)len));
 
-       return True;
+       return true;
 }
 
 
@@ -219,7 +220,7 @@ static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
    directory separators. It should return true if any component is
    mangled
  */
-static BOOL is_mangled(struct pvfs_mangle_context *ctx, const char *name)
+static bool is_mangled(struct pvfs_mangle_context *ctx, const char *name)
 {
        const char *p;
        const char *s;
@@ -228,12 +229,12 @@ static BOOL is_mangled(struct pvfs_mangle_context *ctx, const char *name)
 
        for (s=name; (p=strchr(s, '/')); s=p+1) {
                if (is_mangled_component(ctx, s, PTR_DIFF(p, s))) {
-                       return True;
+                       return true;
                }
        }
        
        /* and the last part ... */
-       return is_mangled_component(ctx, s,strlen(s));
+       return is_mangled_component(ctx, s, strlen(s));
 }
 
 
@@ -244,8 +245,8 @@ static BOOL is_mangled(struct pvfs_mangle_context *ctx, const char *name)
    simplifies things greatly (it means that we know the string won't
    get larger when converted from UNIX to DOS formats)
 */
-static BOOL is_8_3(struct pvfs_mangle_context *ctx,
-                  const char *name, BOOL check_case, BOOL allow_wildcards)
+static bool is_8_3(struct pvfs_mangle_context *ctx,
+                  const char *name, bool check_case, bool allow_wildcards)
 {
        int len, i;
        char *dot_p;
@@ -253,7 +254,7 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
        /* as a special case, the names '.' and '..' are allowable 8.3 names */
        if (name[0] == '.') {
                if (!name[1] || (name[1] == '.' && !name[2])) {
-                       return True;
+                       return true;
                }
        }
 
@@ -264,7 +265,7 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
         only be slower, it would be incorrect */
        len = strlen(name);
        if (len > 12)
-               return False;
+               return false;
 
        /* find the '.'. Note that once again we use the non-multibyte
            function */
@@ -274,7 +275,7 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
                /* if the name doesn't contain a '.' then its length
                    must be less than 8 */
                if (len > 8) {
-                       return False;
+                       return false;
                }
        } else {
                int prefix_len, suffix_len;
@@ -285,25 +286,26 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
                suffix_len = len - (prefix_len+1);
 
                if (prefix_len > 8 || suffix_len > 3 || suffix_len == 0) {
-                       return False;
+                       return false;
                }
 
                /* a 8.3 name cannot contain more than 1 '.' */
                if (strchr(dot_p+1, '.')) {
-                       return False;
+                       return false;
                }
        }
 
        /* the length are all OK. Now check to see if the characters themselves are OK */
        for (i=0; name[i]; i++) {
                /* note that we may allow wildcard petterns! */
-               if (!FLAG_CHECK(name[i], FLAG_ASCII|(allow_wildcards ? FLAG_WILDCARD : 0)) && name[i] != '.') {
-                       return False;
+               if (!FLAG_CHECK(name[i], FLAG_ASCII|(allow_wildcards ? FLAG_WILDCARD : 0)) && 
+                   name[i] != '.') {
+                       return false;
                }
        }
 
        /* it is a good 8.3 name */
-       return True;
+       return true;
 }
 
 
@@ -311,8 +313,8 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
   try to find a 8.3 name in the cache, and if found then
   return the original long name. 
 */
-static const char *check_cache(struct pvfs_mangle_context *ctx, 
-                              const char *name)
+static char *check_cache(struct pvfs_mangle_context *ctx, 
+                        TALLOC_CTX *mem_ctx, const char *name)
 {
        uint32_t hash, multiplier;
        unsigned int i;
@@ -349,17 +351,17 @@ static const char *check_cache(struct pvfs_mangle_context *ctx,
        }
 
        if (extension[0]) {
-               return talloc_asprintf(ctx, "%s.%s", prefix, extension);
+               return talloc_asprintf(mem_ctx, "%s.%s", prefix, extension);
        }
 
-       return talloc_strdup(ctx, prefix);
+       return talloc_strdup(mem_ctx, prefix);
 }
 
 
 /*
   look for a DOS reserved name
 */
-static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
+static bool is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
 {
        if (FLAG_CHECK(name[0], FLAG_POSSIBLE1) &&
            FLAG_CHECK(name[1], FLAG_POSSIBLE2) &&
@@ -367,17 +369,14 @@ static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
            FLAG_CHECK(name[3], FLAG_POSSIBLE4)) {
                /* a likely match, scan the lot */
                int i;
-               for (i=0; ctx->reserved_names[i]; i++) {
-                       int len = strlen(ctx->reserved_names[i]);
-                       /* note that we match on COM1 as well as COM1.foo */
-                       if (strncasecmp(name, ctx->reserved_names[i], len) == 0 &&
-                           (name[len] == '.' || name[len] == 0)) {
-                               return True;
+               for (i=0; reserved_names[i]; i++) {
+                       if (strcasecmp(name, reserved_names[i]) == 0) {
+                               return true;
                        }
                }
        }
 
-       return False;
+       return false;
 }
 
 
@@ -385,17 +384,13 @@ static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
  See if a filename is a legal long filename.
  A filename ending in a '.' is not legal unless it's "." or "..". JRA.
 */
-static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
+static bool is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
 {
-       const char *dot_pos = NULL;
-       BOOL alldots = True;
-       size_t numdots = 0;
-
        while (*name) {
                size_t c_size;
                codepoint_t c = next_codepoint(name, &c_size);
                if (c == INVALID_CODEPOINT) {
-                       return False;
+                       return false;
                }
                /* all high chars are OK */
                if (c >= 128) {
@@ -403,28 +398,12 @@ static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
                        continue;
                }
                if (FLAG_CHECK(c, FLAG_ILLEGAL)) {
-                       return False;
-               }
-               if (name[0] == '.') {
-                       dot_pos = name;
-                       numdots++;
-               } else {
-                       alldots = False;
+                       return false;
                }
-
                name += c_size;
        }
 
-       if (dot_pos) {
-               if (alldots && (numdots == 1 || numdots == 2))
-                       return True; /* . or .. is a valid name */
-
-               /* A valid long name cannot end in '.' */
-               if (dot_pos[1] == '\0')
-                       return False;
-       }
-
-       return True;
+       return true;
 }
 
 /*
@@ -439,7 +418,7 @@ static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
   return NULL if we don't need to do any conversion
 */
 static char *name_map(struct pvfs_mangle_context *ctx,
-                     const char *name, BOOL need83, BOOL cache83)
+                     const char *name, bool need83, bool cache83)
 {
        char *dot_p;
        char lead_chars[7];
@@ -454,7 +433,7 @@ static char *name_map(struct pvfs_mangle_context *ctx,
        if (!is_reserved_name(ctx, name)) {
                /* if the name is already a valid 8.3 name then we don't need to 
                   do anything */
-               if (is_8_3(ctx, name, False, False)) {
+               if (is_8_3(ctx, name, false, false)) {
                        return NULL;
                }
 
@@ -490,7 +469,7 @@ static char *name_map(struct pvfs_mangle_context *ctx,
                if (! FLAG_CHECK(lead_chars[i], FLAG_ASCII)) {
                        lead_chars[i] = '_';
                }
-               lead_chars[i] = toupper(lead_chars[i]);
+               lead_chars[i] = toupper((unsigned char)lead_chars[i]);
        }
        for (;i<ctx->mangle_prefix;i++) {
                lead_chars[i] = '_';
@@ -508,7 +487,7 @@ static char *name_map(struct pvfs_mangle_context *ctx,
        extension_length = 0;
        if (dot_p) {
                for (i=1; extension_length < 3 && dot_p[i]; i++) {
-                       char c = dot_p[i];
+                       unsigned char c = dot_p[i];
                        if (FLAG_CHECK(c, FLAG_ASCII)) {
                                extension[extension_length++] = toupper(c);
                        }
@@ -518,7 +497,7 @@ static char *name_map(struct pvfs_mangle_context *ctx,
        /* find the hash for this prefix */
        v = hash = mangle_hash(ctx, name, prefix_len);
 
-       new_name = talloc_array_p(ctx, char, 13);
+       new_name = talloc_array(ctx, char, 13);
        if (new_name == NULL) {
                return NULL;
        }
@@ -567,10 +546,6 @@ static void init_tables(struct pvfs_mangle_context *ctx)
        const char *basechars = MANGLE_BASECHARS;
        int i;
        /* the list of reserved dos names - all of these are illegal */
-       const char *reserved_names[] = 
-               { "AUX", "LOCK$", "CON", "COM1", "COM2", "COM3", "COM4",
-                 "LPT1", "LPT2", "LPT3", "NUL", "PRN", NULL };
-
 
        ZERO_STRUCT(ctx->char_flags);
 
@@ -598,17 +573,15 @@ static void init_tables(struct pvfs_mangle_context *ctx)
                ctx->base_reverse[(uint8_t)basechars[i]] = i;
        }       
 
-       ctx->reserved_names = reserved_names;
-
        /* fill in the reserved names flags. These are used as a very
           fast filter for finding possible DOS reserved filenames */
-       for (i=0; ctx->reserved_names[i]; i++) {
+       for (i=0; reserved_names[i]; i++) {
                unsigned char c1, c2, c3, c4;
 
-               c1 = (unsigned char)ctx->reserved_names[i][0];
-               c2 = (unsigned char)ctx->reserved_names[i][1];
-               c3 = (unsigned char)ctx->reserved_names[i][2];
-               c4 = (unsigned char)ctx->reserved_names[i][3];
+               c1 = (unsigned char)reserved_names[i][0];
+               c2 = (unsigned char)reserved_names[i][1];
+               c3 = (unsigned char)reserved_names[i][2];
+               c4 = (unsigned char)reserved_names[i][3];
 
                ctx->char_flags[c1] |= FLAG_POSSIBLE1;
                ctx->char_flags[c2] |= FLAG_POSSIBLE2;
@@ -635,23 +608,27 @@ NTSTATUS pvfs_mangle_init(struct pvfs_state *pvfs)
 {
        struct pvfs_mangle_context *ctx;
 
-       ctx = talloc_p(pvfs, struct pvfs_mangle_context);
+       ctx = talloc(pvfs, struct pvfs_mangle_context);
        if (ctx == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       ctx->prefix_cache = talloc_array_p(ctx, char *, MANGLE_CACHE_SIZE);
+
+       /* by default have a max of 512 entries in the cache. */
+       ctx->cache_size = lpcfg_parm_int(pvfs->ntvfs->ctx->lp_ctx, NULL, "mangle", "cachesize", 512);
+
+       ctx->prefix_cache = talloc_array(ctx, char *, ctx->cache_size);
        if (ctx->prefix_cache == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       ctx->prefix_cache_hashes = talloc_array_p(ctx, uint32_t, MANGLE_CACHE_SIZE);
+       ctx->prefix_cache_hashes = talloc_array(ctx, uint32_t, ctx->cache_size);
        if (ctx->prefix_cache_hashes == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       memset(ctx->prefix_cache, 0, sizeof(char *)*MANGLE_CACHE_SIZE);
-       memset(ctx->prefix_cache_hashes, 0, sizeof(uint32_t)*MANGLE_CACHE_SIZE);
+       memset(ctx->prefix_cache, 0, sizeof(char *) * ctx->cache_size);
+       memset(ctx->prefix_cache_hashes, 0, sizeof(uint32_t) * ctx->cache_size);
 
-       ctx->mangle_prefix = lp_parm_int(-1, "mangle", "prefix");
+       ctx->mangle_prefix = lpcfg_parm_int(pvfs->ntvfs->ctx->lp_ctx, NULL, "mangle", "prefix", -1);
        if (ctx->mangle_prefix < 0 || ctx->mangle_prefix > 6) {
                ctx->mangle_prefix = DEFAULT_MANGLE_PREFIX;
        }
@@ -669,7 +646,7 @@ NTSTATUS pvfs_mangle_init(struct pvfs_state *pvfs)
 */
 char *pvfs_short_name_component(struct pvfs_state *pvfs, const char *name)
 {
-       return name_map(pvfs->mangle_ctx, name, True, True);
+       return name_map(pvfs->mangle_ctx, name, true, true);
 }
 
 
@@ -695,10 +672,24 @@ const char *pvfs_short_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
 char *pvfs_mangled_lookup(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
                          const char *name)
 {
-       const char *ret;
-       ret = check_cache(pvfs->mangle_ctx, name);
-       if (ret) {
-               return talloc_steal(mem_ctx, ret);
-       }
-       return NULL;
+       return check_cache(pvfs->mangle_ctx, mem_ctx, name);
+}
+
+
+/*
+  look for a DOS reserved name
+*/
+bool pvfs_is_reserved_name(struct pvfs_state *pvfs, const char *name)
+{
+       return is_reserved_name(pvfs->mangle_ctx, name);
+}
+
+
+/*
+  see if a component of a filename could be a mangled name from our
+  mangling code
+*/
+bool pvfs_is_mangled_component(struct pvfs_state *pvfs, const char *name)
+{
+       return is_mangled_component(pvfs->mangle_ctx, name, strlen(name));
 }