Change uint_t to unsigned int in source4
[ira/wip.git] / source4 / ntvfs / posix / pvfs_shortname.c
index 9efd1cec857b3fd293cd6fb5b335639da9a66229..530def9163063dde0787e86a656f2e69459718cb 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 "includes.h"
+#include "system/locale.h"
 #include "vfs_posix.h"
-#include "system/iconv.h"
+#include "param/param.h"
 
 /*
   this mangling scheme uses the following format
 #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 MANGLE_BASECHARS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -87,6 +82,33 @@ static const char *reserved_names[] =
   "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];
+
+       struct smb_iconv_convenience *iconv_convenience;
+};
+
+
 /* 
    hash a string of the specified length. The string does not need to be
    null terminated 
@@ -105,7 +127,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]);
@@ -120,7 +142,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]) {
@@ -139,7 +161,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;
@@ -148,19 +170,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;
                        }
                }
        }
@@ -168,23 +190,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;
 }
 
 
@@ -200,7 +222,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;
@@ -209,7 +231,7 @@ 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;
                }
        }
        
@@ -225,8 +247,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;
@@ -234,7 +256,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;
                }
        }
 
@@ -245,7 +267,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 */
@@ -255,7 +277,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;
@@ -266,12 +288,12 @@ 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;
                }
        }
 
@@ -280,12 +302,12 @@ static BOOL is_8_3(struct pvfs_mangle_context *ctx,
                /* note that we may allow wildcard petterns! */
                if (!FLAG_CHECK(name[i], FLAG_ASCII|(allow_wildcards ? FLAG_WILDCARD : 0)) && 
                    name[i] != '.') {
-                       return False;
+                       return false;
                }
        }
 
        /* it is a good 8.3 name */
-       return True;
+       return true;
 }
 
 
@@ -293,8 +315,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;
@@ -331,17 +353,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) &&
@@ -351,12 +373,12 @@ static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
                int i;
                for (i=0; reserved_names[i]; i++) {
                        if (strcasecmp(name, reserved_names[i]) == 0) {
-                               return True;
+                               return true;
                        }
                }
        }
 
-       return False;
+       return false;
 }
 
 
@@ -364,13 +386,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)
 {
        while (*name) {
                size_t c_size;
-               codepoint_t c = next_codepoint(name, &c_size);
+               codepoint_t c = next_codepoint_convenience(ctx->iconv_convenience, name, &c_size);
                if (c == INVALID_CODEPOINT) {
-                       return False;
+                       return false;
                }
                /* all high chars are OK */
                if (c >= 128) {
@@ -378,12 +400,12 @@ static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
                        continue;
                }
                if (FLAG_CHECK(c, FLAG_ILLEGAL)) {
-                       return False;
+                       return false;
                }
                name += c_size;
        }
 
-       return True;
+       return true;
 }
 
 /*
@@ -398,7 +420,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];
@@ -413,7 +435,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;
                }
 
@@ -449,7 +471,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] = '_';
@@ -467,7 +489,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);
                        }
@@ -477,7 +499,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;
        }
@@ -588,23 +610,29 @@ 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);
+
+       ctx->iconv_convenience = lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx);
+
+       /* by default have a max of 512 entries in the cache. */
+       ctx->cache_size = lp_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", -1);
+       ctx->mangle_prefix = lp_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;
        }
@@ -622,7 +650,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);
 }
 
 
@@ -648,19 +676,14 @@ 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)
+bool pvfs_is_reserved_name(struct pvfs_state *pvfs, const char *name)
 {
        return is_reserved_name(pvfs->mangle_ctx, name);
 }
@@ -670,7 +693,7 @@ BOOL pvfs_is_reserved_name(struct pvfs_state *pvfs, const char *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)
+bool pvfs_is_mangled_component(struct pvfs_state *pvfs, const char *name)
 {
        return is_mangled_component(pvfs->mangle_ctx, name, strlen(name));
 }