2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - 8.3 name routines
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "vfs_posix.h"
25 #include "system/iconv.h"
28 this mangling scheme uses the following format
32 where nnnnn is a base 36 hash, and A represents characters from the original string
34 The hash is taken of the leading part of the long filename, in uppercase
36 for simplicity, we only allow ascii characters in 8.3 names
39 /* hash alghorithm changed to FNV1 by idra@samba.org (Simo Sorce).
40 * see http://www.isthe.com/chongo/tech/comp/fnv/index.html for a
41 * discussion on Fowler / Noll / Vo (FNV) Hash by one of it's authors
45 ===============================================================================
48 This file deliberately uses non-multibyte string functions in many places. This
49 is *not* a mistake. This code is multi-byte safe, but it gets this property
50 through some very subtle knowledge of the way multi-byte strings are encoded
51 and the fact that this mangling algorithm only supports ascii characters in
54 please don't convert this file to use the *_m() functions!!
55 ===============================================================================
60 #define M_DEBUG(level, x) DEBUG(level, x)
62 #define M_DEBUG(level, x)
65 /* these flags are used to mark characters in as having particular
67 #define FLAG_BASECHAR 1
69 #define FLAG_ILLEGAL 4
70 #define FLAG_WILDCARD 8
72 /* the "possible" flags are used as a fast way to find possible DOS
74 #define FLAG_POSSIBLE1 16
75 #define FLAG_POSSIBLE2 32
76 #define FLAG_POSSIBLE3 64
77 #define FLAG_POSSIBLE4 128
79 /* by default have a max of 512 entries in the cache. */
80 #ifndef MANGLE_CACHE_SIZE
81 #define MANGLE_CACHE_SIZE 512
84 #define DEFAULT_MANGLE_PREFIX 4
86 #define FNV1_PRIME 0x01000193
87 /*the following number is a fnv1 of the string: idra@samba.org 2002 */
88 #define FNV1_INIT 0xa6b93095
90 #define MANGLE_BASECHARS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
92 #define FLAG_CHECK(c, flag) (ctx->char_flags[(uint8_t)(c)] & (flag))
94 static const char *reserved_names[] =
95 { "AUX", "CON", "COM1", "COM2", "COM3", "COM4",
96 "LPT1", "LPT2", "LPT3", "NUL", "PRN", NULL };
100 hash a string of the specified length. The string does not need to be
103 this hash needs to be fast with a low collision rate (what hash doesn't?)
105 static uint32_t mangle_hash(struct pvfs_mangle_context *ctx,
106 const char *key, size_t length)
108 uint32_t value = FNV1_INIT;
112 while (*key && length--) {
113 c = next_codepoint(key, &c_size);
115 value *= (uint32_t)FNV1_PRIME;
116 value ^= (uint32_t)c;
120 return (value % ctx->mangle_modulus);
124 insert an entry into the prefix cache. The string might not be null
126 static void cache_insert(struct pvfs_mangle_context *ctx,
127 const char *prefix, int length, uint32_t hash)
129 int i = hash % MANGLE_CACHE_SIZE;
131 if (ctx->prefix_cache[i]) {
132 talloc_free(ctx->prefix_cache[i]);
135 ctx->prefix_cache[i] = talloc_strndup(ctx->prefix_cache, prefix, length);
136 ctx->prefix_cache_hashes[i] = hash;
140 lookup an entry in the prefix cache. Return NULL if not found.
142 static const char *cache_lookup(struct pvfs_mangle_context *ctx, uint32_t hash)
144 int i = hash % MANGLE_CACHE_SIZE;
147 if (!ctx->prefix_cache[i] || hash != ctx->prefix_cache_hashes[i]) {
151 /* yep, it matched */
152 return ctx->prefix_cache[i];
157 determine if a string is possibly in a mangled format, ignoring
160 In this algorithm, mangled names use only pure ascii characters (no
161 multi-byte) so we can avoid doing a UCS2 conversion
163 static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
164 const char *name, size_t len)
168 M_DEBUG(10,("is_mangled_component %s (len %u) ?\n", name, (unsigned int)len));
170 /* check the length */
171 if (len > 12 || len < 8)
174 /* the best distinguishing characteristic is the ~ */
178 /* check extension */
182 for (i=9; name[i] && i < len; i++) {
183 if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
189 /* check lead characters */
190 for (i=0;i<ctx->mangle_prefix;i++) {
191 if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
196 /* check rest of hash */
197 if (! FLAG_CHECK(name[7], FLAG_BASECHAR)) {
200 for (i=ctx->mangle_prefix;i<6;i++) {
201 if (! FLAG_CHECK(name[i], FLAG_BASECHAR)) {
206 M_DEBUG(10,("is_mangled_component %s (len %u) -> yes\n", name, (unsigned int)len));
214 determine if a string is possibly in a mangled format, ignoring
217 In this algorithm, mangled names use only pure ascii characters (no
218 multi-byte) so we can avoid doing a UCS2 conversion
220 NOTE! This interface must be able to handle a path with unix
221 directory separators. It should return true if any component is
224 static BOOL is_mangled(struct pvfs_mangle_context *ctx, const char *name)
229 M_DEBUG(10,("is_mangled %s ?\n", name));
231 for (s=name; (p=strchr(s, '/')); s=p+1) {
232 if (is_mangled_component(ctx, s, PTR_DIFF(p, s))) {
237 /* and the last part ... */
238 return is_mangled_component(ctx, s, strlen(s));
243 see if a filename is an allowable 8.3 name.
245 we are only going to allow ascii characters in 8.3 names, as this
246 simplifies things greatly (it means that we know the string won't
247 get larger when converted from UNIX to DOS formats)
249 static BOOL is_8_3(struct pvfs_mangle_context *ctx,
250 const char *name, BOOL check_case, BOOL allow_wildcards)
255 /* as a special case, the names '.' and '..' are allowable 8.3 names */
256 if (name[0] == '.') {
257 if (!name[1] || (name[1] == '.' && !name[2])) {
262 /* the simplest test is on the overall length of the
263 filename. Note that we deliberately use the ascii string
264 length (not the multi-byte one) as it is faster, and gives us
265 the result we need in this case. Using strlen_m would not
266 only be slower, it would be incorrect */
271 /* find the '.'. Note that once again we use the non-multibyte
273 dot_p = strchr(name, '.');
276 /* if the name doesn't contain a '.' then its length
277 must be less than 8 */
282 int prefix_len, suffix_len;
284 /* if it does contain a dot then the prefix must be <=
285 8 and the suffix <= 3 in length */
286 prefix_len = PTR_DIFF(dot_p, name);
287 suffix_len = len - (prefix_len+1);
289 if (prefix_len > 8 || suffix_len > 3 || suffix_len == 0) {
293 /* a 8.3 name cannot contain more than 1 '.' */
294 if (strchr(dot_p+1, '.')) {
299 /* the length are all OK. Now check to see if the characters themselves are OK */
300 for (i=0; name[i]; i++) {
301 /* note that we may allow wildcard petterns! */
302 if (!FLAG_CHECK(name[i], FLAG_ASCII|(allow_wildcards ? FLAG_WILDCARD : 0)) &&
308 /* it is a good 8.3 name */
314 try to find a 8.3 name in the cache, and if found then
315 return the original long name.
317 static const char *check_cache(struct pvfs_mangle_context *ctx,
320 uint32_t hash, multiplier;
325 /* make sure that this is a mangled name from this cache */
326 if (!is_mangled(ctx, name)) {
327 M_DEBUG(10,("check_cache: %s -> not mangled\n", name));
331 /* we need to extract the hash from the 8.3 name */
332 hash = ctx->base_reverse[(unsigned char)name[7]];
333 for (multiplier=36, i=5;i>=ctx->mangle_prefix;i--) {
334 uint32_t v = ctx->base_reverse[(unsigned char)name[i]];
335 hash += multiplier * v;
339 /* now look in the prefix cache for that hash */
340 prefix = cache_lookup(ctx, hash);
342 M_DEBUG(10,("check_cache: %s -> %08X -> not found\n", name, hash));
346 /* we found it - construct the full name */
347 if (name[8] == '.') {
348 strncpy(extension, name+9, 3);
355 return talloc_asprintf(ctx, "%s.%s", prefix, extension);
358 return talloc_strdup(ctx, prefix);
363 look for a DOS reserved name
365 static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
367 if (FLAG_CHECK(name[0], FLAG_POSSIBLE1) &&
368 FLAG_CHECK(name[1], FLAG_POSSIBLE2) &&
369 FLAG_CHECK(name[2], FLAG_POSSIBLE3) &&
370 FLAG_CHECK(name[3], FLAG_POSSIBLE4)) {
371 /* a likely match, scan the lot */
373 for (i=0; reserved_names[i]; i++) {
374 if (strcasecmp(name, reserved_names[i]) == 0) {
385 See if a filename is a legal long filename.
386 A filename ending in a '.' is not legal unless it's "." or "..". JRA.
388 static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
392 codepoint_t c = next_codepoint(name, &c_size);
393 if (c == INVALID_CODEPOINT) {
396 /* all high chars are OK */
401 if (FLAG_CHECK(c, FLAG_ILLEGAL)) {
411 the main forward mapping function, which converts a long filename to
414 if need83 is not set then we only do the mangling if the name is illegal
417 if cache83 is not set then we don't cache the result
419 return NULL if we don't need to do any conversion
421 static char *name_map(struct pvfs_mangle_context *ctx,
422 const char *name, BOOL need83, BOOL cache83)
427 unsigned int extension_length, i;
428 unsigned int prefix_len;
431 const char *basechars = MANGLE_BASECHARS;
433 /* reserved names are handled specially */
434 if (!is_reserved_name(ctx, name)) {
435 /* if the name is already a valid 8.3 name then we don't need to
437 if (is_8_3(ctx, name, False, False)) {
441 /* if the caller doesn't strictly need 8.3 then just check for illegal
443 if (!need83 && is_legal_name(ctx, name)) {
448 /* find the '.' if any */
449 dot_p = strrchr(name, '.');
452 /* if the extension contains any illegal characters or
453 is too long or zero length then we treat it as part
455 for (i=0; i<4 && dot_p[i+1]; i++) {
456 if (! FLAG_CHECK(dot_p[i+1], FLAG_ASCII)) {
461 if (i == 0 || i == 4) dot_p = NULL;
464 /* the leading characters in the mangled name is taken from
465 the first characters of the name, if they are ascii otherwise
468 for (i=0;i<ctx->mangle_prefix && name[i];i++) {
469 lead_chars[i] = name[i];
470 if (! FLAG_CHECK(lead_chars[i], FLAG_ASCII)) {
473 lead_chars[i] = toupper(lead_chars[i]);
475 for (;i<ctx->mangle_prefix;i++) {
479 /* the prefix is anything up to the first dot */
481 prefix_len = PTR_DIFF(dot_p, name);
483 prefix_len = strlen(name);
486 /* the extension of the mangled name is taken from the first 3
487 ascii chars after the dot */
488 extension_length = 0;
490 for (i=1; extension_length < 3 && dot_p[i]; i++) {
492 if (FLAG_CHECK(c, FLAG_ASCII)) {
493 extension[extension_length++] = toupper(c);
498 /* find the hash for this prefix */
499 v = hash = mangle_hash(ctx, name, prefix_len);
501 new_name = talloc_array_p(ctx, char, 13);
502 if (new_name == NULL) {
506 /* now form the mangled name. */
507 for (i=0;i<ctx->mangle_prefix;i++) {
508 new_name[i] = lead_chars[i];
510 new_name[7] = basechars[v % 36];
512 for (i=5; i>=ctx->mangle_prefix; i--) {
514 new_name[i] = basechars[v % 36];
517 /* add the extension */
518 if (extension_length) {
520 memcpy(&new_name[9], extension, extension_length);
521 new_name[9+extension_length] = 0;
527 /* put it in the cache */
528 cache_insert(ctx, name, prefix_len, hash);
531 M_DEBUG(10,("name_map: %s -> %08X -> %s (cache=%d)\n",
532 name, hash, new_name, cache83));
538 /* initialise the flags table
540 we allow only a very restricted set of characters as 'ascii' in this
541 mangling backend. This isn't a significant problem as modern clients
542 use the 'long' filenames anyway, and those don't have these
545 static void init_tables(struct pvfs_mangle_context *ctx)
547 const char *basechars = MANGLE_BASECHARS;
549 /* the list of reserved dos names - all of these are illegal */
551 ZERO_STRUCT(ctx->char_flags);
553 for (i=1;i<128;i++) {
554 if ((i >= '0' && i <= '9') ||
555 (i >= 'a' && i <= 'z') ||
556 (i >= 'A' && i <= 'Z')) {
557 ctx->char_flags[i] |= (FLAG_ASCII | FLAG_BASECHAR);
559 if (strchr("_-$~", i)) {
560 ctx->char_flags[i] |= FLAG_ASCII;
563 if (strchr("*\\/?<>|\":", i)) {
564 ctx->char_flags[i] |= FLAG_ILLEGAL;
567 if (strchr("*?\"<>", i)) {
568 ctx->char_flags[i] |= FLAG_WILDCARD;
572 ZERO_STRUCT(ctx->base_reverse);
574 ctx->base_reverse[(uint8_t)basechars[i]] = i;
577 /* fill in the reserved names flags. These are used as a very
578 fast filter for finding possible DOS reserved filenames */
579 for (i=0; reserved_names[i]; i++) {
580 unsigned char c1, c2, c3, c4;
582 c1 = (unsigned char)reserved_names[i][0];
583 c2 = (unsigned char)reserved_names[i][1];
584 c3 = (unsigned char)reserved_names[i][2];
585 c4 = (unsigned char)reserved_names[i][3];
587 ctx->char_flags[c1] |= FLAG_POSSIBLE1;
588 ctx->char_flags[c2] |= FLAG_POSSIBLE2;
589 ctx->char_flags[c3] |= FLAG_POSSIBLE3;
590 ctx->char_flags[c4] |= FLAG_POSSIBLE4;
591 ctx->char_flags[tolower(c1)] |= FLAG_POSSIBLE1;
592 ctx->char_flags[tolower(c2)] |= FLAG_POSSIBLE2;
593 ctx->char_flags[tolower(c3)] |= FLAG_POSSIBLE3;
594 ctx->char_flags[tolower(c4)] |= FLAG_POSSIBLE4;
596 ctx->char_flags[(unsigned char)'.'] |= FLAG_POSSIBLE4;
599 ctx->mangle_modulus = 1;
600 for (i=0;i<(7-ctx->mangle_prefix);i++) {
601 ctx->mangle_modulus *= 36;
606 initialise the mangling code
608 NTSTATUS pvfs_mangle_init(struct pvfs_state *pvfs)
610 struct pvfs_mangle_context *ctx;
612 ctx = talloc_p(pvfs, struct pvfs_mangle_context);
614 return NT_STATUS_NO_MEMORY;
616 ctx->prefix_cache = talloc_array_p(ctx, char *, MANGLE_CACHE_SIZE);
617 if (ctx->prefix_cache == NULL) {
618 return NT_STATUS_NO_MEMORY;
620 ctx->prefix_cache_hashes = talloc_array_p(ctx, uint32_t, MANGLE_CACHE_SIZE);
621 if (ctx->prefix_cache_hashes == NULL) {
622 return NT_STATUS_NO_MEMORY;
625 memset(ctx->prefix_cache, 0, sizeof(char *)*MANGLE_CACHE_SIZE);
626 memset(ctx->prefix_cache_hashes, 0, sizeof(uint32_t)*MANGLE_CACHE_SIZE);
628 ctx->mangle_prefix = lp_parm_int(-1, "mangle", "prefix");
629 if (ctx->mangle_prefix < 0 || ctx->mangle_prefix > 6) {
630 ctx->mangle_prefix = DEFAULT_MANGLE_PREFIX;
635 pvfs->mangle_ctx = ctx;
642 return the short name for a component of a full name
644 char *pvfs_short_name_component(struct pvfs_state *pvfs, const char *name)
646 return name_map(pvfs->mangle_ctx, name, True, True);
651 return the short name for a given entry in a directory
653 const char *pvfs_short_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
654 struct pvfs_filename *name)
656 char *p = strrchr(name->full_name, '/');
657 char *ret = pvfs_short_name_component(pvfs, p+1);
661 talloc_steal(mem_ctx, ret);
666 lookup a mangled name, returning the original long name if present
669 char *pvfs_mangled_lookup(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
673 ret = check_cache(pvfs->mangle_ctx, name);
675 return talloc_steal(mem_ctx, ret);
682 look for a DOS reserved name
684 BOOL pvfs_is_reserved_name(struct pvfs_state *pvfs, const char *name)
686 return is_reserved_name(pvfs->mangle_ctx, name);
691 see if a component of a filename could be a mangled name from our
694 BOOL pvfs_is_mangled_component(struct pvfs_state *pvfs, const char *name)
696 return is_mangled_component(pvfs->mangle_ctx, name, strlen(name));