small merges from SAMBA_3_0; mostly typos, renames, etc...
[ira/wip.git] / source3 / smbd / mangle_hash.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Name mangling
4    Copyright (C) Andrew Tridgell 1992-2002
5    Copyright (C) Simo Sorce 2001
6    Copyright (C) Andrew Bartlett 2002
7    
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.
12    
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.
17    
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.
21 */
22
23
24 /* -------------------------------------------------------------------------- **
25  * Notable problems...
26  *
27  *  March/April 1998  CRH
28  *  - Many of the functions in this module overwrite string buffers passed to
29  *    them.  This causes a variety of problems and is, generally speaking,
30  *    dangerous and scarry.  See the kludge notes in name_map()
31  *    below.
32  *  - It seems that something is calling name_map() twice.  The
33  *    first call is probably some sort of test.  Names which contain
34  *    illegal characters are being doubly mangled.  I'm not sure, but
35  *    I'm guessing the problem is in server.c.
36  *
37  * -------------------------------------------------------------------------- **
38  */
39
40 /* -------------------------------------------------------------------------- **
41  * History...
42  *
43  *  March/April 1998  CRH
44  *  Updated a bit.  Rewrote is_mangled() to be a bit more selective.
45  *  Rewrote the mangled name cache.  Added comments here and there.
46  *  &c.
47  * -------------------------------------------------------------------------- **
48  */
49
50 #include "includes.h"
51
52
53 /* -------------------------------------------------------------------------- **
54  * External Variables...
55  */
56
57 extern int case_default;    /* Are conforming 8.3 names all upper or lower?   */
58 extern BOOL case_mangle;    /* If true, all chars in 8.3 should be same case. */
59
60 /* -------------------------------------------------------------------------- **
61  * Other stuff...
62  *
63  * magic_char     - This is the magic char used for mangling.  It's
64  *                  global.  There is a call to lp_magicchar() in server.c
65  *                  that is used to override the initial value.
66  *
67  * MANGLE_BASE    - This is the number of characters we use for name mangling.
68  *
69  * basechars      - The set characters used for name mangling.  This
70  *                  is static (scope is this file only).
71  *
72  * mangle()       - Macro used to select a character from basechars (i.e.,
73  *                  mangle(n) will return the nth digit, modulo MANGLE_BASE).
74  *
75  * chartest       - array 0..255.  The index range is the set of all possible
76  *                  values of a byte.  For each byte value, the content is a
77  *                  two nibble pair.  See BASECHAR_MASK and ILLEGAL_MASK,
78  *                  below.
79  *
80  * ct_initialized - False until the chartest array has been initialized via
81  *                  a call to init_chartest().
82  *
83  * BASECHAR_MASK  - Masks the upper nibble of a one-byte value.
84  *
85  * ILLEGAL_MASK   - Masks the lower nibble of a one-byte value.
86  *
87  * isbasecahr()   - Given a character, check the chartest array to see
88  *                  if that character is in the basechars set.  This is
89  *                  faster than using strchr_m().
90  *
91  * isillegal()    - Given a character, check the chartest array to see
92  *                  if that character is in the illegal characters set.
93  *                  This is faster than using strchr_m().
94  *
95  * mangled_cache  - Cache header used for storing mangled -> original
96  *                  reverse maps.
97  *
98  * mc_initialized - False until the mangled_cache structure has been
99  *                  initialized via a call to reset_mangled_cache().
100  *
101  * MANGLED_CACHE_MAX_ENTRIES - Default maximum number of entries for the
102  *                  cache.  A value of 0 indicates "infinite".
103  *
104  * MANGLED_CACHE_MAX_MEMORY  - Default maximum amount of memory for the
105  *                  cache.  When the cache was kept as an array of 256
106  *                  byte strings, the default cache size was 50 entries.
107  *                  This required a fixed 12.5Kbytes of memory.  The
108  *                  mangled stack parameter is no longer used (though
109  *                  this might change).  We're now using a fixed 16Kbyte
110  *                  maximum cache size.  This will probably be much more
111  *                  than 50 entries.
112  */
113
114 char magic_char = '~';
115
116 static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
117 #define MANGLE_BASE       (sizeof(basechars)/sizeof(char)-1)
118
119 static unsigned char chartest[256]  = { 0 };
120 static BOOL          ct_initialized = False;
121
122 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
123 #define BASECHAR_MASK 0xf0
124 #define ILLEGAL_MASK  0x0f
125 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
126 #define isillegal(C) ( (chartest[ ((C) & 0xff) ]) & ILLEGAL_MASK )
127
128 static ubi_cacheRoot mangled_cache[1] =  { { { 0, 0, 0, 0 }, 0, 0, 0, 0, 0, 0 } };
129 static BOOL          mc_initialized   = False;
130 #define MANGLED_CACHE_MAX_ENTRIES 1024
131 #define MANGLED_CACHE_MAX_MEMORY 0
132
133 /* -------------------------------------------------------------------------- **
134  * External Variables...
135  */
136
137 extern int case_default;    /* Are conforming 8.3 names all upper or lower?   */
138 extern BOOL case_mangle;    /* If true, all chars in 8.3 should be same case. */
139
140 /* -------------------------------------------------------------------- */
141
142 static NTSTATUS has_valid_chars(const smb_ucs2_t *s, BOOL allow_wildcards)
143 {
144         if (!s || !*s)
145                 return NT_STATUS_INVALID_PARAMETER;
146
147         /* CHECK: this should not be necessary if the ms wild chars
148            are not valid in valid.dat  --- simo */
149         if (!allow_wildcards && ms_has_wild_w(s))
150                 return NT_STATUS_UNSUCCESSFUL;
151
152         while (*s) {
153                 if(!isvalid83_w(*s))
154                         return NT_STATUS_UNSUCCESSFUL;
155                 s++;
156         }
157
158         return NT_STATUS_OK;
159 }
160
161 /* return False if something fail and
162  * return 2 alloced unicode strings that contain prefix and extension
163  */
164
165 static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **prefix,
166                 smb_ucs2_t **extension, BOOL allow_wildcards)
167 {
168         size_t ext_len;
169         smb_ucs2_t *p;
170
171         *extension = 0;
172         *prefix = strdup_w(ucs2_string);
173         if (!*prefix) {
174                 return NT_STATUS_NO_MEMORY;
175         }
176         if ((p = strrchr_w(*prefix, UCS2_CHAR('.')))) {
177                 ext_len = strlen_w(p+1);
178                 if ((ext_len > 0) && (ext_len < 4) && (p != *prefix) &&
179                     (NT_STATUS_IS_OK(has_valid_chars(p+1,allow_wildcards)))) /* check extension */ {
180                         *p = 0;
181                         *extension = strdup_w(p+1);
182                         if (!*extension) {
183                                 SAFE_FREE(*prefix);
184                                 return NT_STATUS_NO_MEMORY;
185                         }
186                 }
187         }
188         return NT_STATUS_OK;
189 }
190
191 /* ************************************************************************** **
192  * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
193  *
194  *  Input:  fname - String containing the name to be tested.
195  *
196  *  Output: NT_STATUS_UNSUCCESSFUL, if the name matches one of the list of reserved names.
197  *
198  *  Notes:  This is a static function called by is_8_3(), below.
199  *
200  * ************************************************************************** **
201  */
202
203 static NTSTATUS is_valid_name(const smb_ucs2_t *fname, BOOL allow_wildcards)
204 {
205         smb_ucs2_t *str, *p;
206         NTSTATUS ret = NT_STATUS_OK;
207
208         if (!fname || !*fname)
209                 return NT_STATUS_INVALID_PARAMETER;
210
211         /* . and .. are valid names. */
212         if (strcmp_wa(fname, ".")==0 || strcmp_wa(fname, "..")==0)
213                 return NT_STATUS_OK;
214
215         /* Name cannot start with '.' */
216         if (*fname == UCS2_CHAR('.'))
217                 return NT_STATUS_UNSUCCESSFUL;
218         
219         ret = has_valid_chars(fname, allow_wildcards);
220         if (NT_STATUS_IS_ERR(ret))
221                 return ret;
222
223         str = strdup_w(fname);
224         p = strchr_w(str, UCS2_CHAR('.'));
225         if (p && p[1] == UCS2_CHAR(0)) {
226                 /* Name cannot end in '.' */
227                 SAFE_FREE(str);
228                 return NT_STATUS_UNSUCCESSFUL;
229         }
230         if (p) *p = 0;
231         strupper_w(str);
232         p = &(str[1]);
233
234         switch(str[0])
235         {
236         case UCS2_CHAR('A'):
237                 if(strcmp_wa(p, "UX") == 0)
238                         ret = NT_STATUS_UNSUCCESSFUL;
239                 break;
240         case UCS2_CHAR('C'):
241                 if((strcmp_wa(p, "LOCK$") == 0)
242                 || (strcmp_wa(p, "ON") == 0)
243                 || (strcmp_wa(p, "OM1") == 0)
244                 || (strcmp_wa(p, "OM2") == 0)
245                 || (strcmp_wa(p, "OM3") == 0)
246                 || (strcmp_wa(p, "OM4") == 0)
247                 )
248                         ret = NT_STATUS_UNSUCCESSFUL;
249                 break;
250         case UCS2_CHAR('L'):
251                 if((strcmp_wa(p, "PT1") == 0)
252                 || (strcmp_wa(p, "PT2") == 0)
253                 || (strcmp_wa(p, "PT3") == 0)
254                 )
255                         ret = NT_STATUS_UNSUCCESSFUL;
256                 break;
257         case UCS2_CHAR('N'):
258                 if(strcmp_wa(p, "UL") == 0)
259                         ret = NT_STATUS_UNSUCCESSFUL;
260                 break;
261         case UCS2_CHAR('P'):
262                 if(strcmp_wa(p, "RN") == 0)
263                         ret = NT_STATUS_UNSUCCESSFUL;
264                 break;
265         default:
266                 break;
267         }
268
269         SAFE_FREE(str);
270         return ret;
271 }
272
273 static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, BOOL allow_wildcards)
274 {
275         smb_ucs2_t *pref = 0, *ext = 0;
276         size_t plen;
277         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
278
279         if (!fname || !*fname)
280                 return NT_STATUS_INVALID_PARAMETER;
281
282         if (strlen_w(fname) > 12)
283                 return NT_STATUS_UNSUCCESSFUL;
284         
285         if (strcmp_wa(fname, ".") == 0 || strcmp_wa(fname, "..") == 0)
286                 return NT_STATUS_OK;
287
288         if (NT_STATUS_IS_ERR(is_valid_name(fname, allow_wildcards)))
289                 goto done;
290
291         if (NT_STATUS_IS_ERR(mangle_get_prefix(fname, &pref, &ext, allow_wildcards)))
292                 goto done;
293         plen = strlen_w(pref);
294
295         if (strchr_wa(pref, '.'))
296                 goto done;
297         if (plen < 1 || plen > 8)
298                 goto done;
299         if (ext && (strlen_w(ext) > 3))
300                 goto done;
301
302         ret = NT_STATUS_OK;
303
304 done:
305         SAFE_FREE(pref);
306         SAFE_FREE(ext);
307         return ret;
308 }
309
310 static BOOL is_8_3(const char *fname, BOOL check_case, BOOL allow_wildcards)
311 {
312         const char *f;
313         smb_ucs2_t *ucs2name;
314         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
315
316         if (!fname || !*fname)
317                 return False;
318         if ((f = strrchr(fname, '/')) == NULL)
319                 f = fname;
320         else
321                 f++;
322
323         if (strlen(f) > 12)
324                 return False;
325         
326         ucs2name = acnv_uxu2(f);
327         if (!ucs2name) {
328                 DEBUG(0,("is_8_3: internal error acnv_uxu2() failed!\n"));
329                 goto done;
330         }
331
332         ret = is_8_3_w(ucs2name, allow_wildcards);
333
334 done:
335         SAFE_FREE(ucs2name);
336
337         if (!NT_STATUS_IS_OK(ret)) { 
338                 return False;
339         }
340         
341         return True;
342 }
343
344
345
346 /* -------------------------------------------------------------------------- **
347  * Functions...
348  */
349
350 /* ************************************************************************** **
351  * Initialize the static character test array.
352  *
353  *  Input:  none
354  *
355  *  Output: none
356  *
357  *  Notes:  This function changes (loads) the contents of the <chartest>
358  *          array.  The scope of <chartest> is this file.
359  *
360  * ************************************************************************** **
361  */
362 static void init_chartest( void )
363 {
364         const char          *illegalchars = "*\\/?<>|\":";
365         const unsigned char *s;
366   
367         memset( (char *)chartest, '\0', 256 );
368
369         for( s = (const unsigned char *)illegalchars; *s; s++ )
370                 chartest[*s] = ILLEGAL_MASK;
371
372         for( s = (const unsigned char *)basechars; *s; s++ )
373                 chartest[*s] |= BASECHAR_MASK;
374
375         ct_initialized = True;
376 }
377
378 /* ************************************************************************** **
379  * Return True if the name *could be* a mangled name.
380  *
381  *  Input:  s - A path name - in UNIX pathname format.
382  *
383  *  Output: True if the name matches the pattern described below in the
384  *          notes, else False.
385  *
386  *  Notes:  The input name is *not* tested for 8.3 compliance.  This must be
387  *          done separately.  This function returns true if the name contains
388  *          a magic character followed by excactly two characters from the
389  *          basechars list (above), which in turn are followed either by the
390  *          nul (end of string) byte or a dot (extension) or by a '/' (end of
391  *          a directory name).
392  *
393  * ************************************************************************** **
394  */
395 static BOOL is_mangled(const char *s)
396 {
397         char *magic;
398
399         if( !ct_initialized )
400                 init_chartest();
401
402         magic = strchr_m( s, magic_char );
403         while( magic && magic[1] && magic[2] ) {         /* 3 chars, 1st is magic. */
404                 if( ('.' == magic[3] || '/' == magic[3] || !(magic[3]))          /* Ends with '.' or nul or '/' ?  */
405                                 && isbasechar( toupper(magic[1]) )           /* is 2nd char basechar?  */
406                                 && isbasechar( toupper(magic[2]) ) )         /* is 3rd char basechar?  */
407                         return( True );                           /* If all above, then true, */
408                 magic = strchr_m( magic+1, magic_char );      /*    else seek next magic. */
409         }
410         return( False );
411 }
412
413 /* ************************************************************************** **
414  * Compare two cache keys and return a value indicating their ordinal
415  * relationship.
416  *
417  *  Input:  ItemPtr - Pointer to a comparison key.  In this case, this will
418  *                    be a mangled name string.
419  *          NodePtr - Pointer to a node in the cache.  The node structure
420  *                    will be followed in memory by a mangled name string.
421  *
422  *  Output: A signed integer, as follows:
423  *            (x < 0)  <==> Key1 less than Key2
424  *            (x == 0) <==> Key1 equals Key2
425  *            (x > 0)  <==> Key1 greater than Key2
426  *
427  *  Notes:  This is a ubiqx-style comparison routine.  See ubi_BinTree for
428  *          more info.
429  *
430  * ************************************************************************** **
431  */
432 static signed int cache_compare( ubi_btItemPtr ItemPtr, ubi_btNodePtr NodePtr )
433 {
434         char *Key1 = (char *)ItemPtr;
435         char *Key2 = (char *)(((ubi_cacheEntryPtr)NodePtr) + 1);
436
437         return( StrCaseCmp( Key1, Key2 ) );
438 }
439
440 /* ************************************************************************** **
441  * Free a cache entry.
442  *
443  *  Input:  WarrenZevon - Pointer to the entry that is to be returned to
444  *                        Nirvana.
445  *  Output: none.
446  *
447  *  Notes:  This function gets around the possibility that the standard
448  *          free() function may be implemented as a macro, or other evil
449  *          subversions (oh, so much fun).
450  *
451  * ************************************************************************** **
452  */
453 static void cache_free_entry( ubi_trNodePtr WarrenZevon )
454 {
455         ZERO_STRUCTP(WarrenZevon);
456         SAFE_FREE( WarrenZevon );
457 }
458
459 /* ************************************************************************** **
460  * Initializes or clears the mangled cache.
461  *
462  *  Input:  none.
463  *  Output: none.
464  *
465  *  Notes:  There is a section below that is commented out.  It shows how
466  *          one might use lp_ calls to set the maximum memory and entry size
467  *          of the cache.  You might also want to remove the constants used
468  *          in ubi_cacheInit() and replace them with lp_ calls.  If so, then
469  *          the calls to ubi_cacheSetMax*() would be moved into the else
470  *          clause.  Another option would be to pass in the max_entries and
471  *          max_memory values as parameters.  crh 09-Apr-1998.
472  *
473  * ************************************************************************** **
474  */
475
476 static void mangle_reset( void )
477 {
478         if( !mc_initialized ) {
479                 (void)ubi_cacheInit( mangled_cache,
480                                 cache_compare,
481                                 cache_free_entry,
482                                 MANGLED_CACHE_MAX_ENTRIES,
483                                 MANGLED_CACHE_MAX_MEMORY );
484                 mc_initialized = True;
485         } else {
486                 (void)ubi_cacheClear( mangled_cache );
487         }
488
489         /*
490         (void)ubi_cacheSetMaxEntries( mangled_cache, lp_mangled_cache_entries() );
491         (void)ubi_cacheSetMaxMemory(  mangled_cache, lp_mangled_cache_memory() );
492         */
493 }
494
495 /* ************************************************************************** **
496  * Add a mangled name into the cache.
497  *
498  *  Notes:  If the mangled cache has not been initialized, then the
499  *          function will simply fail.  It could initialize the cache,
500  *          but that's not the way it was done before I changed the
501  *          cache mechanism, so I'm sticking with the old method.
502  *
503  *          If the extension of the raw name maps directly to the
504  *          extension of the mangled name, then we'll store both names
505  *          *without* extensions.  That way, we can provide consistent
506  *          reverse mangling for all names that match.  The test here is
507  *          a bit more careful than the one done in earlier versions of
508  *          mangle.c:
509  *
510  *            - the extension must exist on the raw name,
511  *            - it must be all lower case
512  *            - it must match the mangled extension (to prove that no
513  *              mangling occurred).
514  *
515  *  crh 07-Apr-1998
516  *
517  * ************************************************************************** **
518  */
519 static void cache_mangled_name( char *mangled_name, char *raw_name )
520 {
521         ubi_cacheEntryPtr new_entry;
522         char             *s1;
523         char             *s2;
524         size_t               mangled_len;
525         size_t               raw_len;
526         size_t               i;
527
528         /* If the cache isn't initialized, give up. */
529         if( !mc_initialized )
530                 return;
531
532         /* Init the string lengths. */
533         mangled_len = strlen( mangled_name );
534         raw_len     = strlen( raw_name );
535
536         /* See if the extensions are unmangled.  If so, store the entry
537          * without the extension, thus creating a "group" reverse map.
538          */
539         s1 = strrchr( mangled_name, '.' );
540         if( s1 && (s2 = strrchr( raw_name, '.' )) ) {
541                 i = 1;
542                 while( s1[i] && (tolower( s1[i] ) == s2[i]) )
543                         i++;
544                 if( !s1[i] && !s2[i] ) {
545                         mangled_len -= i;
546                         raw_len     -= i;
547                 }
548         }
549
550         /* Allocate a new cache entry.  If the allocation fails, just return. */
551         i = sizeof( ubi_cacheEntry ) + mangled_len + raw_len + 2;
552         new_entry = malloc( i );
553         if( !new_entry )
554                 return;
555
556         /* Fill the new cache entry, and add it to the cache. */
557         s1 = (char *)(new_entry + 1);
558         s2 = (char *)&(s1[mangled_len + 1]);
559         (void)StrnCpy( s1, mangled_name, mangled_len );
560         (void)StrnCpy( s2, raw_name,     raw_len );
561         ubi_cachePut( mangled_cache, i, new_entry, s1 );
562 }
563
564 /* ************************************************************************** **
565  * Check for a name on the mangled name stack
566  *
567  *  Input:  s - Input *and* output string buffer.
568  *
569  *  Output: True if the name was found in the cache, else False.
570  *
571  *  Notes:  If a reverse map is found, the function will overwrite the string
572  *          space indicated by the input pointer <s>.  This is frightening.
573  *          It should be rewritten to return NULL if the long name was not
574  *          found, and a pointer to the long name if it was found.
575  *
576  * ************************************************************************** **
577  */
578
579 static BOOL check_cache( char *s )
580 {
581         ubi_cacheEntryPtr FoundPtr;
582         char             *ext_start = NULL;
583         char             *found_name;
584         char             *saved_ext = NULL;
585
586         /* If the cache isn't initialized, give up. */
587         if( !mc_initialized )
588                 return( False );
589
590         FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
591
592         /* If we didn't find the name *with* the extension, try without. */
593         if( !FoundPtr ) {
594                 ext_start = strrchr( s, '.' );
595                 if( ext_start ) {
596                         if((saved_ext = strdup(ext_start)) == NULL)
597                                 return False;
598
599                         *ext_start = '\0';
600                         FoundPtr = ubi_cacheGet( mangled_cache, (ubi_trItemPtr)s );
601                         /* 
602                          * At this point s is the name without the
603                          * extension. We re-add the extension if saved_ext
604                          * is not null, before freeing saved_ext.
605                          */
606                 }
607         }
608
609         /* Okay, if we haven't found it we're done. */
610         if( !FoundPtr ) {
611                 if(saved_ext) {
612                         /* Replace the saved_ext as it was truncated. */
613                         (void)pstrcat( s, saved_ext );
614                         SAFE_FREE(saved_ext);
615                 }
616                 return( False );
617         }
618
619         /* If we *did* find it, we need to copy it into the string buffer. */
620         found_name = (char *)(FoundPtr + 1);
621         found_name += (strlen( found_name ) + 1);
622
623         (void)pstrcpy( s, found_name );
624         if( saved_ext ) {
625                 /* Replace the saved_ext as it was truncated. */
626                 (void)pstrcat( s, saved_ext );
627                 SAFE_FREE(saved_ext);
628         }
629
630         return( True );
631 }
632
633 /*****************************************************************************
634  * do the actual mangling to 8.3 format
635  * the buffer must be able to hold 13 characters (including the null)
636  *****************************************************************************
637  */
638 static void to_8_3(char *s)
639 {
640         int csum;
641         char *p;
642         char extension[4];
643         char base[9];
644         int baselen = 0;
645         int extlen = 0;
646
647         extension[0] = 0;
648         base[0] = 0;
649
650         p = strrchr(s,'.');  
651         if( p && (strlen(p+1) < (size_t)4) ) {
652                 BOOL all_normal = ( strisnormal(p+1) ); /* XXXXXXXXX */
653
654                 if( all_normal && p[1] != 0 ) {
655                         *p = 0;
656                         csum = str_checksum( s );
657                         *p = '.';
658                 } else
659                         csum = str_checksum(s);
660         } else
661                 csum = str_checksum(s);
662
663         strupper( s );
664
665         if( p ) {
666                 if( p == s )
667                         safe_strcpy( extension, "___", 3 );
668                 else {
669                         *p++ = 0;
670                         while( *p && extlen < 3 ) {
671                                 if ( *p != '.') {
672                                         extension[extlen++] = p[0];
673                                 }
674                                 p++;
675                         }
676                         extension[extlen] = 0;
677                 }
678         }
679   
680         p = s;
681
682         while( *p && baselen < 5 ) {
683                 if (*p != '.') {
684                         base[baselen++] = p[0];
685                 }
686                 p++;
687         }
688         base[baselen] = 0;
689   
690         csum = csum % (MANGLE_BASE*MANGLE_BASE);
691   
692         (void)slprintf(s, 12, "%s%c%c%c",
693                 base, magic_char, mangle( csum/MANGLE_BASE ), mangle( csum ) );
694   
695         if( *extension ) {
696                 (void)pstrcat( s, "." );
697                 (void)pstrcat( s, extension );
698         }
699 }
700
701 /*****************************************************************************
702  * Convert a filename to DOS format.  Return True if successful.
703  *
704  *  Input:  OutName - Source *and* destination buffer. 
705  *
706  *                    NOTE that OutName must point to a memory space that
707  *                    is at least 13 bytes in size!
708  *
709  *          need83  - If False, name mangling will be skipped unless the
710  *                    name contains illegal characters.  Mapping will still
711  *                    be done, if appropriate.  This is probably used to
712  *                    signal that a client does not require name mangling,
713  *                    thus skipping the name mangling even on shares which
714  *                    have name-mangling turned on.
715  *          cache83 - If False, the mangled name cache will not be updated.
716  *                    This is usually used to prevent that we overwrite
717  *                    a conflicting cache entry prematurely, i.e. before
718  *                    we know whether the client is really interested in the
719  *                    current name.  (See PR#13758).  UKD.
720  *
721  *  Output: Returns False only if the name wanted mangling but the share does
722  *          not have name mangling turned on.
723  *
724  * ****************************************************************************
725  */
726
727 static void name_map(char *OutName, BOOL need83, BOOL cache83)
728 {
729         smb_ucs2_t *OutName_ucs2;
730         DEBUG(5,("name_map( %s, need83 = %s, cache83 = %s)\n", OutName,
731                  need83 ? "True" : "False", cache83 ? "True" : "False"));
732         
733         if (push_ucs2_allocate(&OutName_ucs2, OutName) == (size_t)-1) {
734                 DEBUG(0, ("push_ucs2_allocate failed!\n"));
735                 return;
736         }
737
738         if( !need83 && NT_STATUS_IS_ERR(is_valid_name(OutName_ucs2, False)))
739                 need83 = True;
740
741         /* check if it's already in 8.3 format */
742         if (need83 && !NT_STATUS_IS_OK(is_8_3_w(OutName_ucs2, False))) {
743                 char *tmp = NULL; 
744
745                 /* mangle it into 8.3 */
746                 if (cache83)
747                         tmp = strdup(OutName);
748
749                 to_8_3(OutName);
750
751                 if(tmp != NULL) {
752                         cache_mangled_name(OutName, tmp);
753                         SAFE_FREE(tmp);
754                 }
755         }
756
757         DEBUG(5,("name_map() ==> [%s]\n", OutName));
758         SAFE_FREE(OutName_ucs2);
759 }
760
761 /*
762   the following provides the abstraction layer to make it easier
763   to drop in an alternative mangling implementation
764 */
765 static struct mangle_fns mangle_fns = {
766         is_mangled,
767         is_8_3,
768         mangle_reset,
769         check_cache,
770         name_map
771 };
772
773 /* return the methods for this mangling implementation */
774 struct mangle_fns *mangle_hash_init(void)
775 {
776         mangle_reset();
777
778         return &mangle_fns;
779 }