lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[amitay/samba.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    Copyright (C) Jeremy Allison 2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "mangle.h"
28 #include "util_tdb.h"
29 #include "lib/param/loadparm.h"
30
31 /* -------------------------------------------------------------------------- **
32  * Other stuff...
33  *
34  * magic_char     - This is the magic char used for mangling.  It's
35  *                  global.  There is a call to lp_mangling_char() in server.c
36  *                  that is used to override the initial value.
37  *
38  * MANGLE_BASE    - This is the number of characters we use for name mangling.
39  *
40  * basechars      - The set characters used for name mangling.  This
41  *                  is static (scope is this file only).
42  *
43  * mangle()       - Macro used to select a character from basechars (i.e.,
44  *                  mangle(n) will return the nth digit, modulo MANGLE_BASE).
45  *
46  * chartest       - array 0..255.  The index range is the set of all possible
47  *                  values of a byte.  For each byte value, the content is a
48  *                  two nibble pair.  See BASECHAR_MASK below.
49  *
50  * ct_initialized - False until the chartest array has been initialized via
51  *                  a call to init_chartest().
52  *
53  * BASECHAR_MASK  - Masks the upper nibble of a one-byte value.
54  *
55  * isbasecahr()   - Given a character, check the chartest array to see
56  *                  if that character is in the basechars set.  This is
57  *                  faster than using strchr_m().
58  *
59  */
60
61 static const char basechars[43]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
62 #define MANGLE_BASE       (sizeof(basechars)/sizeof(char)-1)
63
64 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
65 #define BASECHAR_MASK 0xf0
66 #define isbasechar(C) ( (chartest[ ((C) & 0xff) ]) & BASECHAR_MASK )
67
68 /* -------------------------------------------------------------------- */
69
70
71 /*******************************************************************
72  Determine if a character is valid in a 8.3 name.
73 ********************************************************************/
74
75 static const uint32_t valid_table[] = {
76         0x00000000,0x2fff7bfa,0xefffffff,0xefffffff,0x00000001,0x0fffffee,
77         0xffffffff,0xffffffff,0x00000000,0x00000000,0x00000000,0x01000000,
78         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
79         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
80         0x00000000,0x00000000,0x00000000,0x00000000,0xfffe0000,0xfffe03fb,
81         0x000003ff,0x00000000,0xffff0002,0xffffffff,0x0002ffff,0x00000000,
82         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
83         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
84         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
85         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
86         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
87         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
88         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
89         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
90         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
91         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
92         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
93         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
94         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
95         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
96         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
97         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
98         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
99         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
100         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
101         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
102         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
103         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
104         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
105         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
106         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
107         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
108         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
109         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
110         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
111         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
112         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
113         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
114         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
115         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
116         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
117         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
118         0x00000000,0x00000000,0x00000000,0x00000000,0x33210000,0x080d0063,
119         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
120         0x00400008,0x00000802,0x00000000,0x03ff03ff,0x000f0000,0x00000000,
121         0x00140000,0x00000000,0xe402098d,0x20305fa1,0x00040000,0x00000cc3,
122         0x000000cc,0x80000020,0x00000000,0x00000000,0x00040000,0x00000000,
123         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
124         0x00000000,0x00000000,0x00000000,0x000fffff,0x00000000,0x00000000,
125         0x00000000,0x00000000,0x3999900f,0x99999939,0x00000804,0x00000000,
126         0x00000000,0x300c0003,0x0000c8c0,0x00008000,0x00000060,0x00000000,
127         0x00000005,0x0000a400,0x00000000,0x00000000,0x00000000,0x00000000,
128         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
129         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
130         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
131         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
132         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
133         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
134         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
135         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
136         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
137         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
138         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
139         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
140         0xa03fffef,0x00000000,0xfffffffe,0xffffffff,0x781fffff,0xfffffffe,
141         0xffffffff,0x787fffff,0x00000000,0x00000000,0x00000000,0x00000000,
142         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x02060000,
143         0x00000000,0x00000000,0x00000000,0x000001f0,0x00000000,0x00000000,
144         0x01102008,0x084008cc,0x00822600,0x78000000,0x7000c000,0x00000002,
145         0x00002010,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
146         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
147         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
148         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
149         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
150         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
151         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
152         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
153         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
154         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
155         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
156         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
157         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
158         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
159         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
160         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
161         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
162         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
163         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
164         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
165         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
166         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
167         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
168         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
169         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
170         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
171         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
172         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
173         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
174         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
175         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
176         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
177         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
178         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
179         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
180         0x43f36f8b,0x9b462542,0xe3e0e82c,0x400a0004,0xdb365f65,0x04497977,
181         0xe3f0ecd7,0x18c5603a,0x3403e60b,0x37518000,0x7eebe0c8,0x98698200,
182         0x2d56ad48,0x8060e803,0xad93661c,0xc568c03a,0xc656aa60,0x02403f7e,
183         0x146183cd,0x21751020,0x07122021,0x40bc3000,0x4562a624,0x0a3060a8,
184         0x85740217,0x9c840402,0x14157ffb,0x11e27f34,0x22efb665,0x60ff1f75,
185         0x38403a70,0x676336c3,0x20b24dd9,0x0fc946b0,0x4850bc98,0xa03f8638,
186         0x98162388,0x5232be49,0xeba422ab,0xc72c00dd,0x26e1a1e7,0x8f0a841b,
187         0x559e27eb,0x89bfc241,0x85480014,0x084d6361,0xaad07f0c,0x05cfff3e,
188         0xa803ff1a,0x7b407a41,0x80024745,0x38eb0500,0x1005dc51,0x710c9b34,
189         0x01000397,0xa4046366,0x005180d0,0x430ac000,0x30c89071,0x58000008,
190         0xf7000ed9,0x00415f80,0x941000b0,0x62800018,0x09d00240,0x01568200,
191         0x08015004,0x05101d10,0x001084c1,0x10504025,0x4d8a410f,0xa60d4009,
192         0x914cab19,0x098121c0,0x0203c485,0x80000672,0x00080b04,0x0009141d,
193         0x905c49c9,0x16900009,0x22200c65,0x24338412,0x47960c03,0x42250a04,
194         0xd0880028,0x4f0c4900,0xd3aa14a2,0x3e87d830,0x1f618e04,0x41867ea4,
195         0x2dbbc390,0x211857ad,0x2a48241e,0x4e041138,0x161b0a40,0x88400d60,
196         0x9502020a,0x10608221,0x04000243,0x80001444,0x0c040000,0x70000000,
197         0x00c11a06,0x0c00024a,0x00401a00,0x40451404,0xbdf30029,0x052b0a78,
198         0xbfa0bba9,0x8379407c,0xe91d12fd,0xc5695bf6,0x444aeff6,0xff022115,
199         0x402bed63,0x0242d033,0x00131000,0x5dca1b42,0x020000a0,0x2c61a703,
200         0x8ff24880,0x00000284,0x100d5804,0x0048b200,0x20011894,0x37805004,
201         0x684d3200,0x68be49ea,0x2e42184c,0x21c9a820,0x80b050b9,0xff7c001e,
202         0x14e0849a,0x01e028c1,0xac49870e,0xdddb130f,0x89fbbe1a,0x51b2a2e2,
203         0x32ca5522,0x928b3ec6,0x438f1dbf,0x32986703,0x73c03028,0xa9230811,
204         0x3a65c000,0x04028fe3,0xa6252c4e,0x00a1bf3d,0x8cd43e3a,0x317c06c9,
205         0xd52a00e0,0x0edf018b,0x8c22e34b,0xf0911183,0xa7287d94,0x40fbc9ac,
206         0x07534484,0x44445a90,0x00013fc8,0xf5d40048,0xec5f7701,0x891dc442,
207         0x49286b83,0xd2424109,0x59fe061d,0x3a221840,0x3b9fb7e4,0xc0eaf003,
208         0x82021386,0xe4008980,0x10a1b200,0x0cc44b80,0x8944d309,0x48341faf,
209         0x0c458259,0x0470420a,0x10c8a040,0x44503140,0x01004004,0x05408281,
210         0x642c0108,0x1a056a30,0x051460a6,0x645690cf,0x31000021,0xcbf09c18,
211         0x63e2e120,0x01b5104c,0x9a83538c,0x3281b8b2,0x0a84987a,0x0c0233e7,
212         0xd038d6cd,0x9872e1b1,0xe2848a1e,0x0459c3f4,0x23c2439a,0xd3144845,
213         0x36400292,0xffbd0241,0xe8f0eb09,0xa5d27dc0,0xd24bc242,0xd0afa47f,
214         0x34a11aa0,0x0bd88247,0x651bc453,0xc83ad294,0x40c8001e,0x33140e06,
215         0xb21f615f,0xc0d00088,0xa898a02a,0x166ba1c5,0x85b4af50,0x0604c08b,
216         0x1e04f933,0xa251056e,0x76380400,0x73b8ed07,0x19324406,0xc8164081,
217         0x63097c8a,0xaa042984,0xca9c1c24,0x27614e0e,0x830009d0,0xc10c0846,
218         0x10816011,0x0908540d,0xcc0a000e,0x0c000514,0xa0440430,0x6784008b,
219         0x8a195288,0x8b18865e,0x41602e59,0x9cbe8c10,0x895c6861,0x00089800,
220         0x089a8100,0xc1900018,0xf4a14007,0x640d8505,0x0e4d314e,0xff0a4806,
221         0x2ea81632,0x000b852e,0xca841810,0x696c0e20,0x16000032,0x0390d658,
222         0x1a6851a0,0x11249000,0x432698e1,0x1fae5d52,0xae280fa0,0x5700fafb,
223         0x99406408,0xc044c880,0xb1419005,0xa4c48424,0x603a1a34,0xc1949000,
224         0x003a8246,0xc106180d,0x99100022,0x1511e050,0x00824157,0x022a041a,
225         0x8930004f,0x446ad813,0xed228aa2,0x400511c0,0x01021000,0x31018808,
226         0x02044620,0x0f08f800,0xa2008900,0x22020000,0x16108210,0x10400042,
227         0x126052c0,0x200052f4,0x82308510,0x42021100,0x80b5430a,0xda2070e1,
228         0x08012040,0xfc653500,0xab0419c1,0x62140286,0x00440087,0x42469085,
229         0x0a85405c,0x33803207,0xb8c00400,0xc0d0ce30,0x0080c030,0x0da50508,
230         0x00400a90,0x280c0200,0x40446705,0x41226429,0x000002e8,0x847c4664,
231         0xde200002,0x4049861d,0xc0000a08,0x20010084,0x10108400,0x01c742cd,
232         0xd52a703a,0x1d8f9968,0x3e12be50,0x81d9aef5,0x2412cec4,0x732e0828,
233         0x4b3424ac,0xd41d020c,0x80002a02,0x08110097,0x114411c4,0x7d451786,
234         0x5e4949dd,0x87914040,0xd8c4254c,0x491444ba,0xc8001b92,0x15800271,
235         0x0c0000c1,0xc200096a,0x40024800,0xba493021,0x1c802080,0x1008e2ac,
236         0x00341004,0x841400e3,0x20004020,0x14149810,0x04aa70c2,0x54208688,
237         0x04130c62,0x20109180,0x02064082,0x54011c40,0xe4e90383,0x84802125,
238         0x2810e433,0xe60944c0,0x81260a03,0x080112da,0x97906901,0xf8864001,
239         0x0081e24d,0xa6510a0e,0x81ec011a,0x8441c600,0xb62eadb8,0x8741acef,
240         0x4b028d54,0x02681161,0x2057bb60,0x043350a0,0xf7b4a8c0,0x01122402,
241         0x20009ad3,0x00c82271,0x809e2081,0xe1800c8a,0x8151b009,0x40281031,
242         0x89a52a0e,0x620e69b6,0xd1444425,0x4d548085,0x1fb12c75,0x862dd807,
243         0x5841d97c,0x226e414e,0x9e088200,0xedb7f80d,0x75668c80,0x08149313,
244         0xc8040e32,0x6ea6484e,0x66742c4a,0xba0126c0,0x185dd70c,0x00000000,
245         0x00000000,0x00000000,0x00000000,0x05400000,0x813370a0,0x03a54f81,
246         0x641055ec,0x2344c31a,0x00341462,0x1a090a43,0x13a5187b,0xa8480102,
247         0xc5440440,0xe2dd8106,0x2d481af0,0x0416b626,0x6e405058,0x31128032,
248         0x0c0007e4,0x420a8208,0x803b4840,0x87134860,0x3428850d,0xe5290319,
249         0x870a2345,0x5c1825a9,0xd9c577a6,0x03e85e00,0xa7000081,0x41c6cd54,
250         0xa2042800,0x2b0ab860,0xda9e0020,0x0e1a08ea,0x11c0427e,0x03768908,
251         0x01058621,0x98a80004,0xc44846a0,0x20220d05,0x914854a2,0x28d78a01,
252         0x00087898,0x31221605,0x08804340,0x06a2fa4e,0x92110814,0x9b142002,
253         0x16432e52,0x90105000,0x85ba0041,0x20203042,0x07a84f0b,0x40802f08,
254         0x1a930591,0x0601df50,0x3021a202,0x4e800630,0x04c80cc4,0x8001a004,
255         0xd4316000,0x0a020880,0x00281c00,0x00418e18,0xca106ad0,0x4b00f210,
256         0x1506274d,0x88900220,0x82a85a00,0x81504549,0x80002004,0x2c088804,
257         0x000508d1,0x4ac48001,0x0062e0a0,0x0a42008e,0x6a8c3055,0xe0a5090e,
258         0x42c42906,0x80b34814,0xb330803e,0x733c0102,0x700d1494,0x09400c20,
259         0xc040301a,0xc094a451,0x05c88dca,0xa40c96c2,0x34040001,0x011000c8,
260         0xa9cd550d,0x1cda2428,0x48370142,0x120f7a4d,0x452a32b4,0xd20531fb,
261         0xdc44b894,0x45ca68d7,0x2ed15097,0x42081943,0x9d48d202,0xa0979840,
262         0x064d5409,0x00000000,0x00000000,0x00000000,0x00000000,0x84800000,
263         0x04215542,0x17001c06,0x61107624,0xb9ddff87,0x5c0a659f,0x3c11245d,
264         0x005dadb0,0x00000000,0x00000000,0x00db28d0,0x02000422,0x44080108,
265         0xac409804,0x90288d0a,0xe0018700,0x00310400,0x82211794,0x10540019,
266         0x021a2cb2,0x40039c02,0x8804bd60,0x7900080c,0xba3c1628,0xcb088640,
267         0x90807274,0x0000001e,0xd8000000,0x9c87e188,0x04124034,0x2791ae64,
268         0xe6fbe86b,0x5366408f,0x537feea6,0xb5e4e3ab,0x0002869f,0x01228548,
269         0x48004402,0x20a02116,0x02240004,0x00052080,0x01547e00,0x01ac162c,
270         0x10852a84,0x05308c14,0xfdc3fbc3,0x906060fa,0x40336440,0x96901200,
271         0x4e834b31,0x418200d4,0x1d6a0129,0x02802080,0x02ad8000,0x9f0c2691,
272         0x67018044,0x0c24d96f,0x18d02910,0x50215001,0x04d01000,0x02017090,
273         0x61c30148,0x01000132,0x07190088,0x05620802,0x4c0e0132,0xf0a10405,
274         0x00000002,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
275         0x00000000,0x00800000,0x035e8e8d,0x5a0421bd,0x11703488,0x00000026,
276         0x10000000,0x8804c502,0xf801b815,0x25ed147c,0x3bb0ed60,0x1bd78589,
277         0x1a627af3,0x0ac50d0c,0x524ae5d1,0x6b0d0490,0x5266a35c,0x16122b57,
278         0x1101a872,0x00182949,0x10080948,0x886c6000,0x058f916e,0x39903012,
279         0x49b0f840,0x001b88a0,0x00000000,0x00428500,0x98000058,0x7014ea04,
280         0x611d1628,0x60005193,0x00a71a24,0x00000000,0x43c00000,0x10187120,
281         0xa9270172,0x89066004,0x020cc022,0x40810900,0x8ca0602d,0x00000e34,
282         0x00000000,0x11012100,0xd31a8011,0x0892ec4c,0x85000040,0x1806c7ac,
283         0x0512e03e,0x00348000,0x80cec008,0x0a126d01,0x08568641,0x0027011e,
284         0x083d3751,0x4e05e032,0x048401c0,0x01400081,0x00000000,0x00000000,
285         0x00000000,0x00591aa0,0x882443c8,0xc8001d48,0x72030152,0x04059813,
286         0x04008280,0x0d148a10,0x02088056,0x2704a040,0x4e000000,0x00000000,
287         0x00000000,0xa3200000,0xa0ae1902,0xdf002660,0x7b17f010,0x3ad08121,
288         0x00284180,0x48001003,0x8014cc00,0x00c414cf,0x30202000,0x00000001,
289         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
290         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
291         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
292         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
293         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
294         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
295         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
296         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
297         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
298         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
299         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
300         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
301         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
302         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
303         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
304         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
305         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
306         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
307         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
308         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
309         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
310         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
311         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
312         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
313         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
314         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
315         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
316         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
317         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
318         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
319         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
320         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
321         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
322         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
323         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
324         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
325         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
326         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
327         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
328         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
329         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
330         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
331         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
332         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
333         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
334         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
335         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
336         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
337         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
338         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
339         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
340         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
341         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
342         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
343         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
344         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
345         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
346         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
347         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
348         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
349         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
350         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
351         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
352         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
353         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
354         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
355         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
356         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
357         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
358         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
359         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
360         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
361         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
362         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
363         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
364         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
365         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
366         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
367         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
368         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
369         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
370         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
371         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
372         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
373         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
374         0x00000000,0x00000000,0x00000000,0x00000000,0xffffffff,0xffffffff,
375         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
376         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
377         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
378         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
379         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
380         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
381         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
382         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
383         0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
384         0xffffffff,0xffffffff,0x00ffffff,0x00000000,0x00000000,0x00000000,
385         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
386         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
387         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
388         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
389         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
390         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
391         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
392         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
393         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
394         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
395         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
396         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
397         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
398         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
399         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
400         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
401         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
402         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
403         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
404         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
405         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
406         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
407         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x000f0000,
408         0x00000000,0x00000200,0x00000000,0x00000000,0x00000000,0x00000000,
409         0x10000000,0x00000000,0xffffc000,0x00003fff,0x00000000,0x00000000,
410         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
411         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
412         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
413         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
414         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
415         0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
416         0xfffffffe,0xffffffff,0x7fffffff,0xfffffffe,0xffffffff,0x00000000,
417         0x00000000,0x0000003f
418 };
419
420 #if 0
421 /*
422  * The following program regenerates the good old valid.dat. Try it
423  * yourself :-)
424  */
425 int main(void)
426 {
427         int i;
428         for (i=0; i<65536; i++) {
429                 char c = (valid_table[i/32] & (1<<(i%32))) ? 1 : 0;
430                 write(1, &c, 1);
431         }
432 }
433 #endif
434
435 static bool isvalid83_w(smb_ucs2_t c)
436 {
437         uint16_t idx = SVAL(&c, 0);
438         return (valid_table[idx/32] & (1 << (idx%32))) != 0;
439 }
440
441 static NTSTATUS has_valid_83_chars(const smb_ucs2_t *s, bool allow_wildcards)
442 {
443         if (!*s) {
444                 return NT_STATUS_INVALID_PARAMETER;
445         }
446
447         if (!allow_wildcards && ms_has_wild_w(s)) {
448                 return NT_STATUS_UNSUCCESSFUL;
449         }
450
451         while (*s) {
452                 if(!isvalid83_w(*s)) {
453                         return NT_STATUS_UNSUCCESSFUL;
454                 }
455                 s++;
456         }
457
458         return NT_STATUS_OK;
459 }
460
461 static NTSTATUS has_illegal_chars(const smb_ucs2_t *s, bool allow_wildcards)
462 {
463         if (!allow_wildcards && ms_has_wild_w(s)) {
464                 return NT_STATUS_UNSUCCESSFUL;
465         }
466
467         while (*s) {
468                 if (*s <= 0x1f) {
469                         /* Control characters. */
470                         return NT_STATUS_UNSUCCESSFUL;
471                 }
472                 switch(*s) {
473                         case UCS2_CHAR('\\'):
474                         case UCS2_CHAR('/'):
475                         case UCS2_CHAR('|'):
476                         case UCS2_CHAR(':'):
477                                 return NT_STATUS_UNSUCCESSFUL;
478                 }
479                 s++;
480         }
481
482         return NT_STATUS_OK;
483 }
484
485 /*******************************************************************
486  Duplicate string.
487 ********************************************************************/
488
489 static smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
490 {
491         smb_ucs2_t *dest;
492         size_t len = strlen_w(src);
493         dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
494         if (!dest) {
495                 DEBUG(0,("strdup_w: out of memory!\n"));
496                 return NULL;
497         }
498
499         memcpy(dest, src, len * sizeof(smb_ucs2_t));
500         dest[len] = 0;
501         return dest;
502 }
503
504 /* return False if something fail and
505  * return 2 alloced unicode strings that contain prefix and extension
506  */
507
508 static NTSTATUS mangle_get_prefix(const smb_ucs2_t *ucs2_string, smb_ucs2_t **prefix,
509                 smb_ucs2_t **extension, bool allow_wildcards)
510 {
511         size_t ext_len;
512         smb_ucs2_t *p;
513
514         *extension = 0;
515         *prefix = strdup_w(ucs2_string);
516         if (!*prefix) {
517                 return NT_STATUS_NO_MEMORY;
518         }
519         if ((p = strrchr_w(*prefix, UCS2_CHAR('.')))) {
520                 ext_len = strlen_w(p+1);
521                 if ((ext_len > 0) && (ext_len < 4) && (p != *prefix) &&
522                     (NT_STATUS_IS_OK(has_valid_83_chars(p+1,allow_wildcards)))) /* check extension */ {
523                         *p = 0;
524                         *extension = strdup_w(p+1);
525                         if (!*extension) {
526                                 SAFE_FREE(*prefix);
527                                 return NT_STATUS_NO_MEMORY;
528                         }
529                 }
530         }
531         return NT_STATUS_OK;
532 }
533
534 /* ************************************************************************** **
535  * Return NT_STATUS_UNSUCCESSFUL if a name is a special msdos reserved name.
536  * or contains illegal characters.
537  *
538  *  Input:  fname - String containing the name to be tested.
539  *
540  *  Output: NT_STATUS_UNSUCCESSFUL, if the condition above is true.
541  *
542  *  Notes:  This is a static function called by is_8_3(), below.
543  *
544  * ************************************************************************** **
545  */
546
547 static NTSTATUS is_valid_name(const smb_ucs2_t *fname, bool allow_wildcards, bool only_8_3)
548 {
549         smb_ucs2_t *str, *p;
550         size_t num_ucs2_chars;
551         NTSTATUS ret = NT_STATUS_OK;
552
553         if (!fname || !*fname)
554                 return NT_STATUS_INVALID_PARAMETER;
555
556         /* . and .. are valid names. */
557         if (strcmp_wa(fname, ".")==0 || strcmp_wa(fname, "..")==0)
558                 return NT_STATUS_OK;
559
560         if (only_8_3) {
561                 ret = has_valid_83_chars(fname, allow_wildcards);
562                 if (!NT_STATUS_IS_OK(ret))
563                         return ret;
564         }
565
566         ret = has_illegal_chars(fname, allow_wildcards);
567         if (!NT_STATUS_IS_OK(ret))
568                 return ret;
569
570         /* Name can't end in '.' or ' ' */
571         num_ucs2_chars = strlen_w(fname);
572         if (fname[num_ucs2_chars-1] == UCS2_CHAR('.') || fname[num_ucs2_chars-1] == UCS2_CHAR(' ')) {
573                 return NT_STATUS_UNSUCCESSFUL;
574         }
575
576         str = strdup_w(fname);
577
578         /* Truncate copy after the first dot. */
579         p = strchr_w(str, UCS2_CHAR('.'));
580         if (p) {
581                 *p = 0;
582         }
583
584         strupper_w(str);
585         p = &str[1];
586
587         switch(str[0])
588         {
589         case UCS2_CHAR('A'):
590                 if(strcmp_wa(p, "UX") == 0)
591                         ret = NT_STATUS_UNSUCCESSFUL;
592                 break;
593         case UCS2_CHAR('C'):
594                 if((strcmp_wa(p, "LOCK$") == 0)
595                 || (strcmp_wa(p, "ON") == 0)
596                 || (strcmp_wa(p, "OM1") == 0)
597                 || (strcmp_wa(p, "OM2") == 0)
598                 || (strcmp_wa(p, "OM3") == 0)
599                 || (strcmp_wa(p, "OM4") == 0)
600                 )
601                         ret = NT_STATUS_UNSUCCESSFUL;
602                 break;
603         case UCS2_CHAR('L'):
604                 if((strcmp_wa(p, "PT1") == 0)
605                 || (strcmp_wa(p, "PT2") == 0)
606                 || (strcmp_wa(p, "PT3") == 0)
607                 )
608                         ret = NT_STATUS_UNSUCCESSFUL;
609                 break;
610         case UCS2_CHAR('N'):
611                 if(strcmp_wa(p, "UL") == 0)
612                         ret = NT_STATUS_UNSUCCESSFUL;
613                 break;
614         case UCS2_CHAR('P'):
615                 if(strcmp_wa(p, "RN") == 0)
616                         ret = NT_STATUS_UNSUCCESSFUL;
617                 break;
618         default:
619                 break;
620         }
621
622         SAFE_FREE(str);
623         return ret;
624 }
625
626 static NTSTATUS is_8_3_w(const smb_ucs2_t *fname, bool allow_wildcards)
627 {
628         smb_ucs2_t *pref = 0, *ext = 0;
629         size_t plen;
630         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
631
632         if (!fname || !*fname)
633                 return NT_STATUS_INVALID_PARAMETER;
634
635         if (strlen_w(fname) > 12)
636                 return NT_STATUS_UNSUCCESSFUL;
637
638         if (strcmp_wa(fname, ".") == 0 || strcmp_wa(fname, "..") == 0)
639                 return NT_STATUS_OK;
640
641         /* Name cannot start with '.' */
642         if (*fname == UCS2_CHAR('.'))
643                 return NT_STATUS_UNSUCCESSFUL;
644
645         if (!NT_STATUS_IS_OK(is_valid_name(fname, allow_wildcards, True)))
646                 goto done;
647
648         if (!NT_STATUS_IS_OK(mangle_get_prefix(fname, &pref, &ext, allow_wildcards)))
649                 goto done;
650         plen = strlen_w(pref);
651
652         if (strchr_wa(pref, '.'))
653                 goto done;
654         if (plen < 1 || plen > 8)
655                 goto done;
656         if (ext && (strlen_w(ext) > 3))
657                 goto done;
658
659         ret = NT_STATUS_OK;
660
661 done:
662         SAFE_FREE(pref);
663         SAFE_FREE(ext);
664         return ret;
665 }
666
667 static bool is_8_3(const char *fname, bool check_case, bool allow_wildcards,
668                    const struct share_params *p)
669 {
670         const char *f;
671         smb_ucs2_t *ucs2name;
672         NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
673         size_t size;
674
675         if (!fname || !*fname)
676                 return False;
677         if ((f = strrchr(fname, '/')) == NULL)
678                 f = fname;
679         else
680                 f++;
681
682         if (strlen(f) > 12)
683                 return False;
684
685         if (!push_ucs2_talloc(NULL, &ucs2name, f, &size)) {
686                 DEBUG(0,("is_8_3: internal error push_ucs2_talloc() failed!\n"));
687                 goto done;
688         }
689
690         ret = is_8_3_w(ucs2name, allow_wildcards);
691
692 done:
693         TALLOC_FREE(ucs2name);
694
695         if (!NT_STATUS_IS_OK(ret)) {
696                 return False;
697         }
698
699         return True;
700 }
701
702 /* -------------------------------------------------------------------------- **
703  * Functions...
704  */
705
706 /* ************************************************************************** **
707  * Initialize the static character test array.
708  *
709  *  Input:  none
710  *
711  *  Output: none
712  *
713  *  Notes:  This function changes (loads) the contents of the <chartest>
714  *          array.  The scope of <chartest> is this file.
715  *
716  * ************************************************************************** **
717  */
718
719 static void init_chartest( void )
720 {
721         const unsigned char *s;
722
723         chartest = SMB_MALLOC_ARRAY(unsigned char, 256);
724
725         SMB_ASSERT(chartest != NULL);
726         memset(chartest, '\0', 256);
727
728         for( s = (const unsigned char *)basechars; *s; s++ ) {
729                 chartest[*s] |= BASECHAR_MASK;
730         }
731 }
732
733 /* ************************************************************************** **
734  * Return True if the name *could be* a mangled name.
735  *
736  *  Input:  s - A path name - in UNIX pathname format.
737  *
738  *  Output: True if the name matches the pattern described below in the
739  *          notes, else False.
740  *
741  *  Notes:  The input name is *not* tested for 8.3 compliance.  This must be
742  *          done separately.  This function returns true if the name contains
743  *          a magic character followed by excactly two characters from the
744  *          basechars list (above), which in turn are followed either by the
745  *          nul (end of string) byte or a dot (extension) or by a '/' (end of
746  *          a directory name).
747  *
748  * ************************************************************************** **
749  */
750
751 static bool is_mangled(const char *s, const struct share_params *p)
752 {
753         char *magic;
754         char magic_char;
755
756         magic_char = lp_mangling_char(p);
757
758         if (chartest == NULL) {
759                 init_chartest();
760         }
761
762         magic = strchr_m( s, magic_char );
763         while( magic && magic[1] && magic[2] ) {         /* 3 chars, 1st is magic. */
764                 if( ('.' == magic[3] || '/' == magic[3] || !(magic[3]))          /* Ends with '.' or nul or '/' ?  */
765                                 && isbasechar( toupper_m(magic[1]) )           /* is 2nd char basechar?  */
766                                 && isbasechar( toupper_m(magic[2]) ) )         /* is 3rd char basechar?  */
767                         return( True );                           /* If all above, then true, */
768                 magic = strchr_m( magic+1, magic_char );      /*    else seek next magic. */
769         }
770         return( False );
771 }
772
773 /***************************************************************************
774  Initializes or clears the mangled cache.
775 ***************************************************************************/
776
777 static void mangle_reset( void )
778 {
779         /* We could close and re-open the tdb here... should we ? The old code did
780            the equivalent... JRA. */
781 }
782
783 /***************************************************************************
784  Add a mangled name into the cache.
785  If the extension of the raw name maps directly to the
786  extension of the mangled name, then we'll store both names
787  *without* extensions.  That way, we can provide consistent
788  reverse mangling for all names that match.  The test here is
789  a bit more careful than the one done in earlier versions of
790  mangle.c:
791
792     - the extension must exist on the raw name,
793     - it must be all lower case
794     - it must match the mangled extension (to prove that no
795       mangling occurred).
796   crh 07-Apr-1998
797 **************************************************************************/
798
799 static void cache_mangled_name( const char mangled_name[13],
800                                 const char *raw_name )
801 {
802         TDB_DATA data_val;
803         char mangled_name_key[13];
804         char *s1 = NULL;
805         char *s2 = NULL;
806
807         /* If the cache isn't initialized, give up. */
808         if( !tdb_mangled_cache )
809                 return;
810
811         /* Init the string lengths. */
812         strlcpy(mangled_name_key, mangled_name, sizeof(mangled_name_key));
813
814         /* See if the extensions are unmangled.  If so, store the entry
815          * without the extension, thus creating a "group" reverse map.
816          */
817         s1 = strrchr( mangled_name_key, '.' );
818         if( s1 && (s2 = strrchr( raw_name, '.' )) ) {
819                 size_t i = 1;
820                 while( s1[i] && (tolower_m( s1[i] ) == s2[i]) )
821                         i++;
822                 if( !s1[i] && !s2[i] ) {
823                         /* Truncate at the '.' */
824                         *s1 = '\0';
825                         /*
826                          * DANGER WILL ROBINSON - this
827                          * is changing a const string via
828                          * an aliased pointer ! Remember to
829                          * put it back once we've used it.
830                          * JRA
831                          */
832                         *s2 = '\0';
833                 }
834         }
835
836         /* Allocate a new cache entry.  If the allocation fails, just return. */
837         data_val = string_term_tdb_data(raw_name);
838         if (tdb_store_bystring(tdb_mangled_cache, mangled_name_key, data_val, TDB_REPLACE) != 0) {
839                 DEBUG(0,("cache_mangled_name: Error storing entry %s -> %s\n", mangled_name_key, raw_name));
840         } else {
841                 DEBUG(5,("cache_mangled_name: Stored entry %s -> %s\n", mangled_name_key, raw_name));
842         }
843         /* Restore the change we made to the const string. */
844         if (s2) {
845                 *s2 = '.';
846         }
847 }
848
849 /* ************************************************************************** **
850  * Check for a name on the mangled name stack
851  *
852  *  Input:  s - Input *and* output string buffer.
853  *          maxlen - space in i/o string buffer.
854  *  Output: True if the name was found in the cache, else False.
855  *
856  *  Notes:  If a reverse map is found, the function will overwrite the string
857  *          space indicated by the input pointer <s>.  This is frightening.
858  *          It should be rewritten to return NULL if the long name was not
859  *          found, and a pointer to the long name if it was found.
860  *
861  * ************************************************************************** **
862  */
863
864 static bool lookup_name_from_8_3(TALLOC_CTX *ctx,
865                                 const char *in,
866                                 char **out, /* talloced on the given context. */
867                                 const struct share_params *p)
868 {
869         TDB_DATA data_val;
870         char *saved_ext = NULL;
871         char *s = talloc_strdup(ctx, in);
872
873         /* If the cache isn't initialized, give up. */
874         if(!s || !tdb_mangled_cache ) {
875                 TALLOC_FREE(s);
876                 return False;
877         }
878
879         data_val = tdb_fetch_bystring(tdb_mangled_cache, s);
880
881         /* If we didn't find the name *with* the extension, try without. */
882         if(data_val.dptr == NULL || data_val.dsize == 0) {
883                 char *ext_start = strrchr( s, '.' );
884                 if( ext_start ) {
885                         if((saved_ext = talloc_strdup(ctx,ext_start)) == NULL) {
886                                 TALLOC_FREE(s);
887                                 return False;
888                         }
889
890                         *ext_start = '\0';
891                         data_val = tdb_fetch_bystring(tdb_mangled_cache, s);
892                         /*
893                          * At this point s is the name without the
894                          * extension. We re-add the extension if saved_ext
895                          * is not null, before freeing saved_ext.
896                          */
897                 }
898         }
899
900         /* Okay, if we haven't found it we're done. */
901         if(data_val.dptr == NULL || data_val.dsize == 0) {
902                 TALLOC_FREE(saved_ext);
903                 TALLOC_FREE(s);
904                 return False;
905         }
906
907         /* If we *did* find it, we need to talloc it on the given ctx. */
908         if (saved_ext) {
909                 *out = talloc_asprintf(ctx, "%s%s",
910                                         (char *)data_val.dptr,
911                                         saved_ext);
912         } else {
913                 *out = talloc_strdup(ctx, (char *)data_val.dptr);
914         }
915
916         TALLOC_FREE(s);
917         TALLOC_FREE(saved_ext);
918         SAFE_FREE(data_val.dptr);
919
920         return *out ? True : False;
921 }
922
923 /**
924  Check if a string is in "normal" case.
925 **/
926
927 static bool strisnormal(const char *s, int case_default)
928 {
929         if (case_default == CASE_UPPER)
930                 return(!strhaslower(s));
931
932         return(!strhasupper(s));
933 }
934
935
936 /*****************************************************************************
937  Do the actual mangling to 8.3 format.
938 *****************************************************************************/
939
940 static bool to_8_3(char magic_char, const char *in, char out[13], int default_case)
941 {
942         int csum;
943         char *p;
944         char extension[4];
945         char base[9];
946         int baselen = 0;
947         int extlen = 0;
948         char *s = SMB_STRDUP(in);
949
950         extension[0] = 0;
951         base[0] = 0;
952
953         if (!s) {
954                 return False;
955         }
956
957         p = strrchr(s,'.');
958         if( p && (strlen(p+1) < (size_t)4) ) {
959                 bool all_normal = ( strisnormal(p+1, default_case) ); /* XXXXXXXXX */
960
961                 if( all_normal && p[1] != 0 ) {
962                         *p = 0;
963                         csum = str_checksum( s );
964                         *p = '.';
965                 } else
966                         csum = str_checksum(s);
967         } else
968                 csum = str_checksum(s);
969
970         if (!strupper_m( s )) {
971                 SAFE_FREE(s);
972                 return false;
973         }
974
975         if( p ) {
976                 if( p == s )
977                         strlcpy( extension, "___", 4);
978                 else {
979                         *p++ = 0;
980                         while( *p && extlen < 3 ) {
981                                 if ( *p != '.') {
982                                         extension[extlen++] = p[0];
983                                 }
984                                 p++;
985                         }
986                         extension[extlen] = 0;
987                 }
988         }
989
990         p = s;
991
992         while( *p && baselen < 5 ) {
993                 if (isbasechar(*p)) {
994                         base[baselen++] = p[0];
995                 }
996                 p++;
997         }
998         base[baselen] = 0;
999
1000         csum = csum % (MANGLE_BASE*MANGLE_BASE);
1001
1002         memcpy(out, base, baselen);
1003         out[baselen] = magic_char;
1004         out[baselen+1] = mangle( csum/MANGLE_BASE );
1005         out[baselen+2] = mangle( csum );
1006
1007         if( *extension ) {
1008                 out[baselen+3] = '.';
1009                 strlcpy(&out[baselen+4], extension, 4);
1010         }
1011
1012         SAFE_FREE(s);
1013         return True;
1014 }
1015
1016 static bool must_mangle(const char *name,
1017                         const struct share_params *p)
1018 {
1019         smb_ucs2_t *name_ucs2 = NULL;
1020         NTSTATUS status;
1021         size_t converted_size;
1022
1023         if (!push_ucs2_talloc(NULL, &name_ucs2, name, &converted_size)) {
1024                 DEBUG(0, ("push_ucs2_talloc failed!\n"));
1025                 return False;
1026         }
1027         status = is_valid_name(name_ucs2, False, False);
1028         TALLOC_FREE(name_ucs2);
1029         /* We return true if we *must* mangle, so if it's
1030          * a valid name (status == OK) then we must return
1031          * false. Bug #6939. */
1032         return !NT_STATUS_IS_OK(status);
1033 }
1034
1035 /*****************************************************************************
1036  * Convert a filename to DOS format.  Return True if successful.
1037  *  Input:  in        Incoming name.
1038  *
1039  *          out       8.3 DOS name.
1040  *
1041  *          cache83 - If False, the mangled name cache will not be updated.
1042  *                    This is usually used to prevent that we overwrite
1043  *                    a conflicting cache entry prematurely, i.e. before
1044  *                    we know whether the client is really interested in the
1045  *                    current name.  (See PR#13758).  UKD.
1046  *
1047  * ****************************************************************************
1048  */
1049
1050 static bool hash_name_to_8_3(const char *in,
1051                         char out[13],
1052                         bool cache83,
1053                         int default_case,
1054                         const struct share_params *p)
1055 {
1056         smb_ucs2_t *in_ucs2 = NULL;
1057         size_t converted_size;
1058         char magic_char;
1059
1060         magic_char = lp_mangling_char(p);
1061
1062         DEBUG(5,("hash_name_to_8_3( %s, cache83 = %s)\n", in,
1063                  cache83 ? "True" : "False"));
1064
1065         if (!push_ucs2_talloc(NULL, &in_ucs2, in, &converted_size)) {
1066                 DEBUG(0, ("push_ucs2_talloc failed!\n"));
1067                 return False;
1068         }
1069
1070         /* If it's already 8.3, just copy. */
1071         if (NT_STATUS_IS_OK(is_valid_name(in_ucs2, False, False)) &&
1072                                 NT_STATUS_IS_OK(is_8_3_w(in_ucs2, False))) {
1073                 TALLOC_FREE(in_ucs2);
1074                 strlcpy(out, in, 13);
1075                 return True;
1076         }
1077
1078         TALLOC_FREE(in_ucs2);
1079         if (!to_8_3(magic_char, in, out, default_case)) {
1080                 return False;
1081         }
1082
1083         cache_mangled_name(out, in);
1084
1085         DEBUG(5,("hash_name_to_8_3(%s) ==> [%s]\n", in, out));
1086         return True;
1087 }
1088
1089 /*
1090   the following provides the abstraction layer to make it easier
1091   to drop in an alternative mangling implementation
1092 */
1093 static const struct mangle_fns mangle_hash_fns = {
1094         mangle_reset,
1095         is_mangled,
1096         must_mangle,
1097         is_8_3,
1098         lookup_name_from_8_3,
1099         hash_name_to_8_3
1100 };
1101
1102 /* return the methods for this mangling implementation */
1103 const struct mangle_fns *mangle_hash_init(void)
1104 {
1105         mangle_reset();
1106
1107         if (chartest == NULL) {
1108                 init_chartest();
1109         }
1110
1111         /* Create the in-memory tdb using our custom hash function. */
1112         tdb_mangled_cache = tdb_open_ex("mangled_cache", 1031, TDB_INTERNAL,
1113                                 (O_RDWR|O_CREAT), 0644, NULL, fast_string_hash);
1114
1115         return &mangle_hash_fns;
1116 }