Finish removal of iconv_convenience in public API's.
[amitay/samba.git] / source4 / ntvfs / posix / pvfs_shortname.c
index 98cd4a99f60b0716303a433d4e0beecceaf35859..fc1cad18949028d471c9ebf7e04d4233d8e08b46 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
@@ -159,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;
@@ -168,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;
                        }
                }
        }
@@ -188,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;
 }
 
 
@@ -220,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;
@@ -229,7 +229,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;
                }
        }
        
@@ -245,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;
@@ -254,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;
                }
        }
 
@@ -265,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 */
@@ -275,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;
@@ -286,12 +286,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;
                }
        }
 
@@ -300,12 +300,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;
 }
 
 
@@ -313,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;
@@ -351,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) &&
@@ -371,12 +371,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;
 }
 
 
@@ -384,13 +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)
 {
        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) {
@@ -398,12 +398,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;
 }
 
 /*
@@ -418,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];
@@ -433,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;
                }
 
@@ -469,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] = '_';
@@ -487,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);
                        }
@@ -497,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;
        }
@@ -608,13 +608,13 @@ 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;
        }
 
        /* by default have a max of 512 entries in the cache. */
-       ctx->cache_size = lp_parm_int(-1, "mangle", "cachesize", 512);
+       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) {
@@ -628,7 +628,7 @@ NTSTATUS pvfs_mangle_init(struct pvfs_state *pvfs)
        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;
        }
@@ -646,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);
 }
 
 
@@ -672,19 +672,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);
 }
@@ -694,7 +689,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));
 }