1 /* $NetBSD: ntfs_subr.c,v 1.23 1999/10/31 19:45:26 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: releng/8.4/sys/fs/ntfs/ntfs_subr.c 230206 2012-01-16 08:31:32Z kevlo $
29 */
30
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/vnode.h>
37 #include <sys/mount.h>
38 #include <sys/bio.h>
39 #include <sys/buf.h>
40 #include <sys/file.h>
41 #include <sys/malloc.h>
42 #include <sys/lock.h>
43 #include <sys/iconv.h>
44
45 /* #define NTFS_DEBUG 1 */
46 #include <fs/ntfs/ntfs.h>
47 #include <fs/ntfs/ntfsmount.h>
48 #include <fs/ntfs/ntfs_inode.h>
49 #include <fs/ntfs/ntfs_vfsops.h>
50 #include <fs/ntfs/ntfs_subr.h>
51 #include <fs/ntfs/ntfs_compr.h>
52 #include <fs/ntfs/ntfs_ihash.h>
53
54 MALLOC_DEFINE(M_NTFSNTVATTR, "ntfs_vattr", "NTFS file attribute information");
55 MALLOC_DEFINE(M_NTFSRDATA, "ntfsd_resdata", "NTFS resident data");
56 MALLOC_DEFINE(M_NTFSRUN, "ntfs_vrun", "NTFS vrun storage");
57 MALLOC_DEFINE(M_NTFSDECOMP, "ntfs_decomp", "NTFS decompression temporary");
58
59 static int ntfs_ntlookupattr(struct ntfsmount *, const char *, int, int *, char **);
60 static int ntfs_findvattr(struct ntfsmount *, struct ntnode *, struct ntvattr **, struct ntvattr **, u_int32_t, const char *, size_t, cn_t);
61 static int ntfs_uastricmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
62 static int ntfs_uastrcmp(struct ntfsmount *, const wchar *, size_t, const char *, size_t);
63
64 /* table for mapping Unicode chars into uppercase; it's filled upon first
65 * ntfs mount, freed upon last ntfs umount */
66 static wchar *ntfs_toupper_tab;
67 #define NTFS_TOUPPER(ch) (ntfs_toupper_tab[(ch)])
68 static struct lock ntfs_toupper_lock;
69 static signed int ntfs_toupper_usecount;
70
71 struct iconv_functions *ntfs_iconv = NULL;
72
73 /* support macro for ntfs_ntvattrget() */
74 #define NTFS_AALPCMP(aalp,type,name,namelen) ( \
75 (aalp->al_type == type) && (aalp->al_namelen == namelen) && \
76 !NTFS_UASTRCMP(aalp->al_name,aalp->al_namelen,name,namelen) )
77
78 /*
79 *
80 */
81 int
82 ntfs_ntvattrrele(vap)
83 struct ntvattr * vap;
84 {
85 dprintf(("ntfs_ntvattrrele: ino: %d, type: 0x%x\n",
86 vap->va_ip->i_number, vap->va_type));
87
88 ntfs_ntrele(vap->va_ip);
89
90 return (0);
91 }
92
93 /*
94 * find the attribute in the ntnode
95 */
96 static int
97 ntfs_findvattr(ntmp, ip, lvapp, vapp, type, name, namelen, vcn)
98 struct ntfsmount *ntmp;
99 struct ntnode *ip;
100 struct ntvattr **lvapp, **vapp;
101 u_int32_t type;
102 const char *name;
103 size_t namelen;
104 cn_t vcn;
105 {
106 int error;
107 struct ntvattr *vap;
108
109 if((ip->i_flag & IN_LOADED) == 0) {
110 dprintf(("ntfs_findvattr: node not loaded, ino: %d\n",
111 ip->i_number));
112 error = ntfs_loadntnode(ntmp,ip);
113 if (error) {
114 printf("ntfs_findvattr: FAILED TO LOAD INO: %d\n",
115 ip->i_number);
116 return (error);
117 }
118 }
119
120 *lvapp = NULL;
121 *vapp = NULL;
122 LIST_FOREACH(vap, &ip->i_valist, va_list) {
123 ddprintf(("ntfs_findvattr: type: 0x%x, vcn: %d - %d\n", \
124 vap->va_type, (u_int32_t) vap->va_vcnstart, \
125 (u_int32_t) vap->va_vcnend));
126 if ((vap->va_type == type) &&
127 (vap->va_vcnstart <= vcn) && (vap->va_vcnend >= vcn) &&
128 (vap->va_namelen == namelen) &&
129 (strncmp(name, vap->va_name, namelen) == 0)) {
130 *vapp = vap;
131 ntfs_ntref(vap->va_ip);
132 return (0);
133 }
134 if (vap->va_type == NTFS_A_ATTRLIST)
135 *lvapp = vap;
136 }
137
138 return (-1);
139 }
140
141 /*
142 * Search attribute specifed in ntnode (load ntnode if nessecary).
143 * If not found but ATTR_A_ATTRLIST present, read it in and search throught.
144 * VOP_VGET node needed, and lookup througth it's ntnode (load if nessesary).
145 *
146 * ntnode should be locked
147 */
148 int
149 ntfs_ntvattrget(
150 struct ntfsmount * ntmp,
151 struct ntnode * ip,
152 u_int32_t type,
153 const char *name,
154 cn_t vcn,
155 struct ntvattr ** vapp)
156 {
157 struct ntvattr *lvap = NULL;
158 struct attr_attrlist *aalp;
159 struct attr_attrlist *nextaalp;
160 struct vnode *newvp;
161 struct ntnode *newip;
162 caddr_t alpool;
163 size_t namelen, len;
164 int error;
165
166 *vapp = NULL;
167
168 if (name) {
169 dprintf(("ntfs_ntvattrget: " \
170 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
171 ip->i_number, type, name, (u_int32_t) vcn));
172 namelen = strlen(name);
173 } else {
174 dprintf(("ntfs_ntvattrget: " \
175 "ino: %d, type: 0x%x, vcn: %d\n", \
176 ip->i_number, type, (u_int32_t) vcn));
177 name = "";
178 namelen = 0;
179 }
180
181 error = ntfs_findvattr(ntmp, ip, &lvap, vapp, type, name, namelen, vcn);
182 if (error >= 0)
183 return (error);
184
185 if (!lvap) {
186 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
187 "ino: %d, type: 0x%x, name: %s, vcn: %d\n", \
188 ip->i_number, type, name, (u_int32_t) vcn));
189 return (ENOENT);
190 }
191 /* Scan $ATTRIBUTE_LIST for requested attribute */
192 len = lvap->va_datalen;
193 alpool = malloc(len, M_TEMP, M_WAITOK);
194 error = ntfs_readntvattr_plain(ntmp, ip, lvap, 0, len, alpool, &len,
195 NULL);
196 if (error)
197 goto out;
198
199 aalp = (struct attr_attrlist *) alpool;
200 nextaalp = NULL;
201
202 for(; len > 0; aalp = nextaalp) {
203 dprintf(("ntfs_ntvattrget: " \
204 "attrlist: ino: %d, attr: 0x%x, vcn: %d\n", \
205 aalp->al_inumber, aalp->al_type, \
206 (u_int32_t) aalp->al_vcnstart));
207
208 if (len > aalp->reclen) {
209 nextaalp = NTFS_NEXTREC(aalp, struct attr_attrlist *);
210 } else {
211 nextaalp = NULL;
212 }
213 len -= aalp->reclen;
214
215 if (!NTFS_AALPCMP(aalp, type, name, namelen) ||
216 (nextaalp && (nextaalp->al_vcnstart <= vcn) &&
217 NTFS_AALPCMP(nextaalp, type, name, namelen)))
218 continue;
219
220 dprintf(("ntfs_ntvattrget: attribute in ino: %d\n",
221 aalp->al_inumber));
222
223 /* this is not a main record, so we can't use just plain
224 vget() */
225 error = ntfs_vgetex(ntmp->ntm_mountp, aalp->al_inumber,
226 NTFS_A_DATA, NULL, LK_EXCLUSIVE,
227 VG_EXT, curthread, &newvp);
228 if (error) {
229 printf("ntfs_ntvattrget: CAN'T VGET INO: %d\n",
230 aalp->al_inumber);
231 goto out;
232 }
233 newip = VTONT(newvp);
234 /* XXX have to lock ntnode */
235 error = ntfs_findvattr(ntmp, newip, &lvap, vapp,
236 type, name, namelen, vcn);
237 vput(newvp);
238 if (error == 0)
239 goto out;
240 printf("ntfs_ntvattrget: ATTRLIST ERROR.\n");
241 break;
242 }
243 error = ENOENT;
244
245 dprintf(("ntfs_ntvattrget: UNEXISTED ATTRIBUTE: " \
246 "ino: %d, type: 0x%x, name: %.*s, vcn: %d\n", \
247 ip->i_number, type, (int) namelen, name, (u_int32_t) vcn));
248 out:
249 free(alpool, M_TEMP);
250 return (error);
251 }
252
253 /*
254 * Read ntnode from disk, make ntvattr list.
255 *
256 * ntnode should be locked
257 */
258 int
259 ntfs_loadntnode(
260 struct ntfsmount * ntmp,
261 struct ntnode * ip)
262 {
263 struct filerec *mfrp;
264 daddr_t bn;
265 int error,off;
266 struct attr *ap;
267 struct ntvattr *nvap;
268
269 dprintf(("ntfs_loadntnode: loading ino: %d\n",ip->i_number));
270
271 mfrp = malloc(ntfs_bntob(ntmp->ntm_bpmftrec),
272 M_TEMP, M_WAITOK);
273
274 if (ip->i_number < NTFS_SYSNODESNUM) {
275 struct buf *bp;
276
277 dprintf(("ntfs_loadntnode: read system node\n"));
278
279 bn = ntfs_cntobn(ntmp->ntm_mftcn) +
280 ntmp->ntm_bpmftrec * ip->i_number;
281
282 error = bread(ntmp->ntm_devvp,
283 bn, ntfs_bntob(ntmp->ntm_bpmftrec),
284 NOCRED, &bp);
285 if (error) {
286 printf("ntfs_loadntnode: BREAD FAILED\n");
287 brelse(bp);
288 goto out;
289 }
290 memcpy(mfrp, bp->b_data, ntfs_bntob(ntmp->ntm_bpmftrec));
291 bqrelse(bp);
292 } else {
293 struct vnode *vp;
294
295 vp = ntmp->ntm_sysvn[NTFS_MFTINO];
296 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
297 ip->i_number * ntfs_bntob(ntmp->ntm_bpmftrec),
298 ntfs_bntob(ntmp->ntm_bpmftrec), mfrp, NULL);
299 if (error) {
300 printf("ntfs_loadntnode: ntfs_readattr failed\n");
301 goto out;
302 }
303 }
304
305 /* Check if magic and fixups are correct */
306 error = ntfs_procfixups(ntmp, NTFS_FILEMAGIC, (caddr_t)mfrp,
307 ntfs_bntob(ntmp->ntm_bpmftrec));
308 if (error) {
309 printf("ntfs_loadntnode: BAD MFT RECORD %d\n",
310 (u_int32_t) ip->i_number);
311 goto out;
312 }
313
314 dprintf(("ntfs_loadntnode: load attrs for ino: %d\n",ip->i_number));
315 off = mfrp->fr_attroff;
316 ap = (struct attr *) ((caddr_t)mfrp + off);
317
318 LIST_INIT(&ip->i_valist);
319
320 while (ap->a_hdr.a_type != -1) {
321 error = ntfs_attrtontvattr(ntmp, &nvap, ap);
322 if (error)
323 break;
324 nvap->va_ip = ip;
325
326 LIST_INSERT_HEAD(&ip->i_valist, nvap, va_list);
327
328 off += ap->a_hdr.reclen;
329 ap = (struct attr *) ((caddr_t)mfrp + off);
330 }
331 if (error) {
332 printf("ntfs_loadntnode: failed to load attr ino: %d\n",
333 ip->i_number);
334 goto out;
335 }
336
337 ip->i_mainrec = mfrp->fr_mainrec;
338 ip->i_nlink = mfrp->fr_nlink;
339 ip->i_frflag = mfrp->fr_flags;
340
341 ip->i_flag |= IN_LOADED;
342
343 out:
344 free(mfrp, M_TEMP);
345 return (error);
346 }
347
348 /*
349 * Routine locks ntnode and increase usecount, just opposite of
350 * ntfs_ntput().
351 */
352 int
353 ntfs_ntget(ip)
354 struct ntnode *ip;
355 {
356 dprintf(("ntfs_ntget: get ntnode %d: %p, usecount: %d\n",
357 ip->i_number, ip, ip->i_usecount));
358
359 mtx_lock(&ip->i_interlock);
360 ip->i_usecount++;
361 lockmgr(&ip->i_lock, LK_EXCLUSIVE | LK_INTERLOCK, &ip->i_interlock);
362
363 return 0;
364 }
365
366 /*
367 * Routine search ntnode in hash, if found: lock, inc usecount and return.
368 * If not in hash allocate structure for ntnode, prefill it, lock,
369 * inc count and return.
370 *
371 * ntnode returned locked
372 */
373 int
374 ntfs_ntlookup(
375 struct ntfsmount * ntmp,
376 ino_t ino,
377 struct ntnode ** ipp)
378 {
379 struct ntnode *ip;
380
381 dprintf(("ntfs_ntlookup: looking for ntnode %d\n", ino));
382
383 do {
384 ip = ntfs_nthashlookup(ntmp->ntm_devvp->v_rdev, ino);
385 if (ip != NULL) {
386 ntfs_ntget(ip);
387 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
388 ino, ip, ip->i_usecount));
389 *ipp = ip;
390 return (0);
391 }
392 } while (lockmgr(&ntfs_hashlock, LK_EXCLUSIVE | LK_SLEEPFAIL, NULL));
393
394 ip = malloc(sizeof(struct ntnode), M_NTFSNTNODE,
395 M_WAITOK | M_ZERO);
396 ddprintf(("ntfs_ntlookup: allocating ntnode: %d: %p\n", ino, ip));
397
398 /* Generic initialization */
399 ip->i_devvp = ntmp->ntm_devvp;
400 ip->i_dev = ntmp->ntm_devvp->v_rdev;
401 ip->i_number = ino;
402 ip->i_mp = ntmp;
403
404 LIST_INIT(&ip->i_fnlist);
405 VREF(ip->i_devvp);
406
407 /* init lock and lock the newborn ntnode */
408 lockinit(&ip->i_lock, PINOD, "ntnode", 0, 0);
409 mtx_init(&ip->i_interlock, "ntnode interlock", NULL, MTX_DEF);
410 ntfs_ntget(ip);
411
412 ntfs_nthashins(ip);
413
414 lockmgr(&ntfs_hashlock, LK_RELEASE, NULL);
415
416 *ipp = ip;
417
418 dprintf(("ntfs_ntlookup: ntnode %d: %p, usecount: %d\n",
419 ino, ip, ip->i_usecount));
420
421 return (0);
422 }
423
424 /*
425 * Decrement usecount of ntnode and unlock it, if usecount reach zero,
426 * deallocate ntnode.
427 *
428 * ntnode should be locked on entry, and unlocked on return.
429 */
430 void
431 ntfs_ntput(ip)
432 struct ntnode *ip;
433 {
434 struct ntvattr *vap;
435
436 dprintf(("ntfs_ntput: rele ntnode %d: %p, usecount: %d\n",
437 ip->i_number, ip, ip->i_usecount));
438
439 mtx_lock(&ip->i_interlock);
440 ip->i_usecount--;
441
442 #ifdef DIAGNOSTIC
443 if (ip->i_usecount < 0) {
444 panic("ntfs_ntput: ino: %d usecount: %d \n",
445 ip->i_number,ip->i_usecount);
446 }
447 #endif
448
449 if (ip->i_usecount > 0) {
450 lockmgr(&ip->i_lock, LK_RELEASE|LK_INTERLOCK, &ip->i_interlock);
451 return;
452 }
453
454 dprintf(("ntfs_ntput: deallocating ntnode: %d\n", ip->i_number));
455
456 if (LIST_FIRST(&ip->i_fnlist))
457 panic("ntfs_ntput: ntnode has fnodes\n");
458
459 ntfs_nthashrem(ip);
460
461 while ((vap = LIST_FIRST(&ip->i_valist)) != NULL) {
462 LIST_REMOVE(vap,va_list);
463 ntfs_freentvattr(vap);
464 }
465 lockmgr(&ip->i_lock, LK_RELEASE | LK_INTERLOCK, &ip->i_interlock);
466 mtx_destroy(&ip->i_interlock);
467 lockdestroy(&ip->i_lock);
468 vrele(ip->i_devvp);
469 free(ip, M_NTFSNTNODE);
470 }
471
472 /*
473 * increment usecount of ntnode
474 */
475 void
476 ntfs_ntref(ip)
477 struct ntnode *ip;
478 {
479 mtx_lock(&ip->i_interlock);
480 ip->i_usecount++;
481 mtx_unlock(&ip->i_interlock);
482
483 dprintf(("ntfs_ntref: ino %d, usecount: %d\n",
484 ip->i_number, ip->i_usecount));
485
486 }
487
488 /*
489 * Decrement usecount of ntnode.
490 */
491 void
492 ntfs_ntrele(ip)
493 struct ntnode *ip;
494 {
495 dprintf(("ntfs_ntrele: rele ntnode %d: %p, usecount: %d\n",
496 ip->i_number, ip, ip->i_usecount));
497
498 mtx_lock(&ip->i_interlock);
499 ip->i_usecount--;
500
501 if (ip->i_usecount < 0)
502 panic("ntfs_ntrele: ino: %d usecount: %d \n",
503 ip->i_number,ip->i_usecount);
504 mtx_unlock(&ip->i_interlock);
505 }
506
507 /*
508 * Deallocate all memory allocated for ntvattr
509 */
510 void
511 ntfs_freentvattr(vap)
512 struct ntvattr * vap;
513 {
514 if (vap->va_flag & NTFS_AF_INRUN) {
515 if (vap->va_vruncn)
516 free(vap->va_vruncn, M_NTFSRUN);
517 if (vap->va_vruncl)
518 free(vap->va_vruncl, M_NTFSRUN);
519 } else {
520 if (vap->va_datap)
521 free(vap->va_datap, M_NTFSRDATA);
522 }
523 free(vap, M_NTFSNTVATTR);
524 }
525
526 /*
527 * Convert disk image of attribute into ntvattr structure,
528 * runs are expanded also.
529 */
530 int
531 ntfs_attrtontvattr(
532 struct ntfsmount * ntmp,
533 struct ntvattr ** rvapp,
534 struct attr * rap)
535 {
536 int error, i;
537 struct ntvattr *vap;
538
539 error = 0;
540 *rvapp = NULL;
541
542 vap = malloc(sizeof(struct ntvattr),
543 M_NTFSNTVATTR, M_WAITOK | M_ZERO);
544 vap->va_ip = NULL;
545 vap->va_flag = rap->a_hdr.a_flag;
546 vap->va_type = rap->a_hdr.a_type;
547 vap->va_compression = rap->a_hdr.a_compression;
548 vap->va_index = rap->a_hdr.a_index;
549
550 ddprintf(("type: 0x%x, index: %d", vap->va_type, vap->va_index));
551
552 vap->va_namelen = rap->a_hdr.a_namelen;
553 if (rap->a_hdr.a_namelen) {
554 wchar *unp = (wchar *) ((caddr_t) rap + rap->a_hdr.a_nameoff);
555 ddprintf((", name:["));
556 for (i = 0; i < vap->va_namelen; i++) {
557 vap->va_name[i] = unp[i];
558 ddprintf(("%c", vap->va_name[i]));
559 }
560 ddprintf(("]"));
561 }
562 if (vap->va_flag & NTFS_AF_INRUN) {
563 ddprintf((", nonres."));
564 vap->va_datalen = rap->a_nr.a_datalen;
565 vap->va_allocated = rap->a_nr.a_allocated;
566 vap->va_vcnstart = rap->a_nr.a_vcnstart;
567 vap->va_vcnend = rap->a_nr.a_vcnend;
568 vap->va_compressalg = rap->a_nr.a_compressalg;
569 error = ntfs_runtovrun(&(vap->va_vruncn), &(vap->va_vruncl),
570 &(vap->va_vruncnt),
571 (caddr_t) rap + rap->a_nr.a_dataoff);
572 } else {
573 vap->va_compressalg = 0;
574 ddprintf((", res."));
575 vap->va_datalen = rap->a_r.a_datalen;
576 vap->va_allocated = rap->a_r.a_datalen;
577 vap->va_vcnstart = 0;
578 vap->va_vcnend = ntfs_btocn(vap->va_allocated);
579 vap->va_datap = malloc(vap->va_datalen,
580 M_NTFSRDATA, M_WAITOK);
581 memcpy(vap->va_datap, (caddr_t) rap + rap->a_r.a_dataoff,
582 rap->a_r.a_datalen);
583 }
584 ddprintf((", len: %d", vap->va_datalen));
585
586 if (error)
587 free(vap, M_NTFSNTVATTR);
588 else
589 *rvapp = vap;
590
591 ddprintf(("\n"));
592
593 return (error);
594 }
595
596 /*
597 * Expand run into more utilizable and more memory eating format.
598 */
599 int
600 ntfs_runtovrun(
601 cn_t ** rcnp,
602 cn_t ** rclp,
603 u_long * rcntp,
604 u_int8_t * run)
605 {
606 u_int32_t off;
607 u_int32_t sz, i;
608 cn_t *cn;
609 cn_t *cl;
610 u_long cnt;
611 cn_t prev;
612 cn_t tmp;
613
614 off = 0;
615 cnt = 0;
616 i = 0;
617 while (run[off]) {
618 off += (run[off] & 0xF) + ((run[off] >> 4) & 0xF) + 1;
619 cnt++;
620 }
621 cn = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
622 cl = malloc(cnt * sizeof(cn_t), M_NTFSRUN, M_WAITOK);
623
624 off = 0;
625 cnt = 0;
626 prev = 0;
627 while (run[off]) {
628
629 sz = run[off++];
630 cl[cnt] = 0;
631
632 for (i = 0; i < (sz & 0xF); i++)
633 cl[cnt] += (u_int32_t) run[off++] << (i << 3);
634
635 sz >>= 4;
636 if (run[off + sz - 1] & 0x80) {
637 tmp = ((u_int64_t) - 1) << (sz << 3);
638 for (i = 0; i < sz; i++)
639 tmp |= (u_int64_t) run[off++] << (i << 3);
640 } else {
641 tmp = 0;
642 for (i = 0; i < sz; i++)
643 tmp |= (u_int64_t) run[off++] << (i << 3);
644 }
645 if (tmp)
646 prev = cn[cnt] = prev + tmp;
647 else
648 cn[cnt] = tmp;
649
650 cnt++;
651 }
652 *rcnp = cn;
653 *rclp = cl;
654 *rcntp = cnt;
655 return (0);
656 }
657
658 /*
659 * Compare unicode and ascii string case insens.
660 */
661 static int
662 ntfs_uastricmp(ntmp, ustr, ustrlen, astr, astrlen)
663 struct ntfsmount *ntmp;
664 const wchar *ustr;
665 size_t ustrlen;
666 const char *astr;
667 size_t astrlen;
668 {
669 const char *astrp = astr;
670 char tmpbuf[5];
671 int len, res;
672 size_t i, j, mbstrlen = astrlen;
673
674 if (ntmp->ntm_ic_l2u) {
675 for (i = 0, j = 0; i < ustrlen && j < astrlen; i++) {
676 len = 4;
677 res = ((int) NTFS_TOUPPER(ustr[i])) -
678 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
679 astrp += len;
680 j += len;
681 mbstrlen -= len - 1;
682
683 if (res)
684 return res;
685 }
686 } else {
687 /*
688 * We use NTFS_82U(NTFS_U28(c)) to get rid of unicode
689 * symbols not covered by translation table
690 */
691 for (i = 0; i < ustrlen && i < astrlen; i++) {
692 res = ((int) NTFS_TOUPPER(NTFS_82U(NTFS_U28(ustr[i]), &len))) -
693 ((int)NTFS_TOUPPER(NTFS_82U(astrp, &len)));
694 astrp++;
695 if (res)
696 return res;
697 }
698 }
699 return (ustrlen - mbstrlen);
700 }
701
702 /*
703 * Compare unicode and ascii string case sens.
704 */
705 static int
706 ntfs_uastrcmp(ntmp, ustr, ustrlen, astr, astrlen)
707 struct ntfsmount *ntmp;
708 const wchar *ustr;
709 size_t ustrlen;
710 const char *astr;
711 size_t astrlen;
712 {
713 char *c, tmpbuf[5];
714 size_t i, j, mbstrlen = astrlen;
715 int res;
716
717 for (i = 0, j = 0; (i < ustrlen) && (j < astrlen); i++, mbstrlen++) {
718 c = NTFS_U28(ustr[i]);
719 while (*c != '\0') {
720 res = (int) (*c++ - astr[j++]);
721 if (res)
722 return res;
723 mbstrlen--;
724 }
725 }
726 return (ustrlen - mbstrlen);
727 }
728
729 /*
730 * Search fnode in ntnode, if not found allocate and preinitialize.
731 *
732 * ntnode should be locked on entry.
733 */
734 int
735 ntfs_fget(
736 struct ntfsmount *ntmp,
737 struct ntnode *ip,
738 int attrtype,
739 char *attrname,
740 struct fnode **fpp)
741 {
742 struct fnode *fp;
743
744 dprintf(("ntfs_fget: ino: %d, attrtype: 0x%x, attrname: %s\n",
745 ip->i_number,attrtype, attrname?attrname:""));
746 *fpp = NULL;
747 LIST_FOREACH(fp, &ip->i_fnlist, f_fnlist){
748 dprintf(("ntfs_fget: fnode: attrtype: %d, attrname: %s\n",
749 fp->f_attrtype, fp->f_attrname?fp->f_attrname:""));
750
751 if ((attrtype == fp->f_attrtype) &&
752 ((!attrname && !fp->f_attrname) ||
753 (attrname && fp->f_attrname &&
754 !strcmp(attrname,fp->f_attrname)))){
755 dprintf(("ntfs_fget: found existed: %p\n",fp));
756 *fpp = fp;
757 }
758 }
759
760 if (*fpp)
761 return (0);
762
763 fp = malloc(sizeof(struct fnode), M_NTFSFNODE,
764 M_WAITOK | M_ZERO);
765 dprintf(("ntfs_fget: allocating fnode: %p\n",fp));
766
767 fp->f_ip = ip;
768 if (attrname) {
769 fp->f_flag |= FN_AATTRNAME;
770 fp->f_attrname = malloc(strlen(attrname)+1, M_TEMP, M_WAITOK);
771 strcpy(fp->f_attrname, attrname);
772 } else
773 fp->f_attrname = NULL;
774 fp->f_attrtype = attrtype;
775
776 ntfs_ntref(ip);
777
778 LIST_INSERT_HEAD(&ip->i_fnlist, fp, f_fnlist);
779
780 *fpp = fp;
781
782 return (0);
783 }
784
785 /*
786 * Deallocate fnode, remove it from ntnode's fnode list.
787 *
788 * ntnode should be locked.
789 */
790 void
791 ntfs_frele(
792 struct fnode *fp)
793 {
794 struct ntnode *ip = FTONT(fp);
795
796 dprintf(("ntfs_frele: fnode: %p for %d: %p\n", fp, ip->i_number, ip));
797
798 dprintf(("ntfs_frele: deallocating fnode\n"));
799 LIST_REMOVE(fp,f_fnlist);
800 if (fp->f_flag & FN_AATTRNAME)
801 free(fp->f_attrname, M_TEMP);
802 if (fp->f_dirblbuf)
803 free(fp->f_dirblbuf, M_NTFSDIR);
804 free(fp, M_NTFSFNODE);
805 ntfs_ntrele(ip);
806 }
807
808 /*
809 * Lookup attribute name in format: [[:$ATTR_TYPE]:$ATTR_NAME],
810 * $ATTR_TYPE is searched in attrdefs read from $AttrDefs.
811 * If $ATTR_TYPE nott specifed, ATTR_A_DATA assumed.
812 */
813 static int
814 ntfs_ntlookupattr(
815 struct ntfsmount * ntmp,
816 const char * name,
817 int namelen,
818 int *attrtype,
819 char **attrname)
820 {
821 const char *sys;
822 size_t syslen, i;
823 struct ntvattrdef *adp;
824
825 if (namelen == 0)
826 return (0);
827
828 if (name[0] == '$') {
829 sys = name;
830 for (syslen = 0; syslen < namelen; syslen++) {
831 if(sys[syslen] == ':') {
832 name++;
833 namelen--;
834 break;
835 }
836 }
837 name += syslen;
838 namelen -= syslen;
839
840 adp = ntmp->ntm_ad;
841 for (i = 0; i < ntmp->ntm_adnum; i++, adp++){
842 if (syslen != adp->ad_namelen ||
843 strncmp(sys, adp->ad_name, syslen) != 0)
844 continue;
845
846 *attrtype = adp->ad_type;
847 goto out;
848 }
849 return (ENOENT);
850 } else
851 *attrtype = NTFS_A_DATA;
852
853 out:
854 if (namelen) {
855 (*attrname) = malloc(namelen, M_TEMP, M_WAITOK);
856 memcpy((*attrname), name, namelen);
857 (*attrname)[namelen] = '\0';
858 }
859
860 return (0);
861 }
862
863 /*
864 * Lookup specifed node for filename, matching cnp,
865 * return fnode filled.
866 */
867 int
868 ntfs_ntlookupfile(
869 struct ntfsmount * ntmp,
870 struct vnode * vp,
871 struct componentname * cnp,
872 struct vnode ** vpp)
873 {
874 struct fnode *fp = VTOF(vp);
875 struct ntnode *ip = FTONT(fp);
876 struct ntvattr *vap; /* Root attribute */
877 cn_t cn; /* VCN in current attribute */
878 caddr_t rdbuf; /* Buffer to read directory's blocks */
879 u_int32_t blsize;
880 u_int64_t rdsize; /* Length of data to read from current block */
881 struct attr_indexentry *iep;
882 int error, res, anamelen, fnamelen;
883 const char *fname,*aname;
884 u_int32_t aoff;
885 int attrtype = NTFS_A_DATA;
886 char *attrname = NULL;
887 struct fnode *nfp;
888 struct vnode *nvp;
889 enum vtype f_type;
890
891 error = ntfs_ntget(ip);
892 if (error)
893 return (error);
894
895 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
896 if (error || (vap->va_flag & NTFS_AF_INRUN))
897 return (ENOTDIR);
898
899 blsize = vap->va_a_iroot->ir_size;
900 rdsize = vap->va_datalen;
901
902 /*
903 * Divide file name into: foofilefoofilefoofile[:attrspec]
904 * Store like this: fname:fnamelen [aname:anamelen]
905 */
906 fname = cnp->cn_nameptr;
907 aname = NULL;
908 anamelen = 0;
909 for (fnamelen = 0; fnamelen < cnp->cn_namelen; fnamelen++)
910 if(fname[fnamelen] == ':') {
911 aname = fname + fnamelen + 1;
912 anamelen = cnp->cn_namelen - fnamelen - 1;
913 dprintf(("ntfs_ntlookupfile: %s (%d), attr: %s (%d)\n",
914 fname, fnamelen, aname, anamelen));
915 break;
916 }
917
918 dprintf(("ntfs_ntlookupfile: blksz: %d, rdsz: %jd\n", blsize, rdsize));
919
920 rdbuf = malloc(blsize, M_TEMP, M_WAITOK);
921
922 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXROOT, "$I30",
923 0, rdsize, rdbuf, NULL);
924 if (error)
925 goto fail;
926
927 aoff = sizeof(struct attr_indexroot);
928
929 do {
930 iep = (struct attr_indexentry *) (rdbuf + aoff);
931
932 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
933 aoff += iep->reclen,
934 iep = (struct attr_indexentry *) (rdbuf + aoff))
935 {
936 ddprintf(("scan: %d, %d\n",
937 (u_int32_t) iep->ie_number,
938 (u_int32_t) iep->ie_fnametype));
939
940 /* check the name - the case-insensitible check
941 * has to come first, to break from this for loop
942 * if needed, so we can dive correctly */
943 res = NTFS_UASTRICMP(iep->ie_fname, iep->ie_fnamelen,
944 fname, fnamelen);
945 if (res > 0) break;
946 if (res < 0) continue;
947
948 if (iep->ie_fnametype == 0 ||
949 !(ntmp->ntm_flag & NTFS_MFLAG_CASEINS))
950 {
951 res = NTFS_UASTRCMP(iep->ie_fname,
952 iep->ie_fnamelen, fname, fnamelen);
953 if (res != 0) continue;
954 }
955
956 if (aname) {
957 error = ntfs_ntlookupattr(ntmp,
958 aname, anamelen,
959 &attrtype, &attrname);
960 if (error)
961 goto fail;
962 }
963
964 /* Check if we've found ourself */
965 if ((iep->ie_number == ip->i_number) &&
966 (attrtype == fp->f_attrtype) &&
967 ((!attrname && !fp->f_attrname) ||
968 (attrname && fp->f_attrname &&
969 !strcmp(attrname, fp->f_attrname))))
970 {
971 VREF(vp);
972 *vpp = vp;
973 error = 0;
974 goto fail;
975 }
976
977 /* vget node, but don't load it */
978 error = ntfs_vgetex(ntmp->ntm_mountp,
979 iep->ie_number, attrtype, attrname,
980 LK_EXCLUSIVE, VG_DONTLOADIN | VG_DONTVALIDFN,
981 curthread, &nvp);
982
983 /* free the buffer returned by ntfs_ntlookupattr() */
984 if (attrname) {
985 free(attrname, M_TEMP);
986 attrname = NULL;
987 }
988
989 if (error)
990 goto fail;
991
992 nfp = VTOF(nvp);
993
994 if (nfp->f_flag & FN_VALID) {
995 *vpp = nvp;
996 goto fail;
997 }
998
999 nfp->f_fflag = iep->ie_fflag;
1000 nfp->f_pnumber = iep->ie_fpnumber;
1001 nfp->f_times = iep->ie_ftimes;
1002
1003 if((nfp->f_fflag & NTFS_FFLAG_DIR) &&
1004 (nfp->f_attrtype == NTFS_A_DATA) &&
1005 (nfp->f_attrname == NULL))
1006 f_type = VDIR;
1007 else
1008 f_type = VREG;
1009
1010 nvp->v_type = f_type;
1011
1012 if ((nfp->f_attrtype == NTFS_A_DATA) &&
1013 (nfp->f_attrname == NULL))
1014 {
1015 /* Opening default attribute */
1016 nfp->f_size = iep->ie_fsize;
1017 nfp->f_allocated = iep->ie_fallocated;
1018 nfp->f_flag |= FN_PRELOADED;
1019 } else {
1020 error = ntfs_filesize(ntmp, nfp,
1021 &nfp->f_size, &nfp->f_allocated);
1022 if (error) {
1023 vput(nvp);
1024 goto fail;
1025 }
1026 }
1027
1028 nfp->f_flag &= ~FN_VALID;
1029 *vpp = nvp;
1030 goto fail;
1031 }
1032
1033 /* Dive if possible */
1034 if (iep->ie_flag & NTFS_IEFLAG_SUBNODE) {
1035 dprintf(("ntfs_ntlookupfile: diving\n"));
1036
1037 cn = *(cn_t *) (rdbuf + aoff +
1038 iep->reclen - sizeof(cn_t));
1039 rdsize = blsize;
1040
1041 error = ntfs_readattr(ntmp, ip, NTFS_A_INDX, "$I30",
1042 ntfs_cntob(cn), rdsize, rdbuf, NULL);
1043 if (error)
1044 goto fail;
1045
1046 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1047 rdbuf, rdsize);
1048 if (error)
1049 goto fail;
1050
1051 aoff = (((struct attr_indexalloc *) rdbuf)->ia_hdrsize +
1052 0x18);
1053 } else {
1054 dprintf(("ntfs_ntlookupfile: nowhere to dive :-(\n"));
1055 error = ENOENT;
1056 break;
1057 }
1058 } while (1);
1059
1060 dprintf(("finish\n"));
1061
1062 fail:
1063 if (attrname) free(attrname, M_TEMP);
1064 ntfs_ntvattrrele(vap);
1065 ntfs_ntput(ip);
1066 free(rdbuf, M_TEMP);
1067 return (error);
1068 }
1069
1070 /*
1071 * Check if name type is permitted to show.
1072 */
1073 int
1074 ntfs_isnamepermitted(
1075 struct ntfsmount * ntmp,
1076 struct attr_indexentry * iep)
1077 {
1078 if (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)
1079 return 1;
1080
1081 switch (iep->ie_fnametype) {
1082 case 2:
1083 ddprintf(("ntfs_isnamepermitted: skipped DOS name\n"));
1084 return 0;
1085 case 0: case 1: case 3:
1086 return 1;
1087 default:
1088 printf("ntfs_isnamepermitted: " \
1089 "WARNING! Unknown file name type: %d\n",
1090 iep->ie_fnametype);
1091 break;
1092 }
1093 return 0;
1094 }
1095
1096 /*
1097 * Read ntfs dir like stream of attr_indexentry, not like btree of them.
1098 * This is done by scaning $BITMAP:$I30 for busy clusters and reading them.
1099 * Ofcouse $INDEX_ROOT:$I30 is read before. Last read values are stored in
1100 * fnode, so we can skip toward record number num almost immediatly.
1101 * Anyway this is rather slow routine. The problem is that we don't know
1102 * how many records are there in $INDEX_ALLOCATION:$I30 block.
1103 */
1104 int
1105 ntfs_ntreaddir(
1106 struct ntfsmount * ntmp,
1107 struct fnode * fp,
1108 u_int32_t num,
1109 struct attr_indexentry ** riepp)
1110 {
1111 struct ntnode *ip = FTONT(fp);
1112 struct ntvattr *vap = NULL; /* IndexRoot attribute */
1113 struct ntvattr *bmvap = NULL; /* BitMap attribute */
1114 struct ntvattr *iavap = NULL; /* IndexAllocation attribute */
1115 caddr_t rdbuf; /* Buffer to read directory's blocks */
1116 u_int8_t *bmp = NULL; /* Bitmap */
1117 u_int32_t blsize; /* Index allocation size (2048) */
1118 u_int32_t rdsize; /* Length of data to read */
1119 u_int32_t attrnum; /* Current attribute type */
1120 u_int32_t cpbl = 1; /* Clusters per directory block */
1121 u_int32_t blnum;
1122 struct attr_indexentry *iep;
1123 int error = ENOENT;
1124 u_int32_t aoff, cnum;
1125
1126 dprintf(("ntfs_ntreaddir: read ino: %d, num: %d\n", ip->i_number, num));
1127 error = ntfs_ntget(ip);
1128 if (error)
1129 return (error);
1130
1131 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXROOT, "$I30", 0, &vap);
1132 if (error)
1133 return (ENOTDIR);
1134
1135 if (fp->f_dirblbuf == NULL) {
1136 fp->f_dirblsz = vap->va_a_iroot->ir_size;
1137 fp->f_dirblbuf = malloc(max(vap->va_datalen,fp->f_dirblsz),
1138 M_NTFSDIR, M_WAITOK);
1139 }
1140
1141 blsize = fp->f_dirblsz;
1142 rdbuf = fp->f_dirblbuf;
1143
1144 dprintf(("ntfs_ntreaddir: rdbuf: %p, blsize: %d\n", rdbuf, blsize));
1145
1146 if (vap->va_a_iroot->ir_flag & NTFS_IRFLAG_INDXALLOC) {
1147 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDXBITMAP, "$I30",
1148 0, &bmvap);
1149 if (error) {
1150 error = ENOTDIR;
1151 goto fail;
1152 }
1153 bmp = malloc(bmvap->va_datalen, M_TEMP, M_WAITOK);
1154 error = ntfs_readattr(ntmp, ip, NTFS_A_INDXBITMAP, "$I30", 0,
1155 bmvap->va_datalen, bmp, NULL);
1156 if (error)
1157 goto fail;
1158
1159 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_INDX, "$I30",
1160 0, &iavap);
1161 if (error) {
1162 error = ENOTDIR;
1163 goto fail;
1164 }
1165 cpbl = ntfs_btocn(blsize + ntfs_cntob(1) - 1);
1166 dprintf(("ntfs_ntreaddir: indexalloc: %jd, cpbl: %d\n",
1167 iavap->va_datalen, cpbl));
1168 } else {
1169 dprintf(("ntfs_ntreadidir: w/o BitMap and IndexAllocation\n"));
1170 iavap = bmvap = NULL;
1171 bmp = NULL;
1172 }
1173
1174 /* Try use previous values */
1175 if ((fp->f_lastdnum < num) && (fp->f_lastdnum != 0)) {
1176 attrnum = fp->f_lastdattr;
1177 aoff = fp->f_lastdoff;
1178 blnum = fp->f_lastdblnum;
1179 cnum = fp->f_lastdnum;
1180 } else {
1181 attrnum = NTFS_A_INDXROOT;
1182 aoff = sizeof(struct attr_indexroot);
1183 blnum = 0;
1184 cnum = 0;
1185 }
1186
1187 do {
1188 dprintf(("ntfs_ntreaddir: scan: 0x%x, %d, %d, %d, %d\n",
1189 attrnum, (u_int32_t) blnum, cnum, num, aoff));
1190 rdsize = (attrnum == NTFS_A_INDXROOT) ? vap->va_datalen : blsize;
1191 error = ntfs_readattr(ntmp, ip, attrnum, "$I30",
1192 ntfs_cntob(blnum * cpbl), rdsize, rdbuf, NULL);
1193 if (error)
1194 goto fail;
1195
1196 if (attrnum == NTFS_A_INDX) {
1197 error = ntfs_procfixups(ntmp, NTFS_INDXMAGIC,
1198 rdbuf, rdsize);
1199 if (error)
1200 goto fail;
1201 }
1202 if (aoff == 0)
1203 aoff = (attrnum == NTFS_A_INDX) ?
1204 (0x18 + ((struct attr_indexalloc *) rdbuf)->ia_hdrsize) :
1205 sizeof(struct attr_indexroot);
1206
1207 iep = (struct attr_indexentry *) (rdbuf + aoff);
1208 for (; !(iep->ie_flag & NTFS_IEFLAG_LAST) && (rdsize > aoff);
1209 aoff += iep->reclen,
1210 iep = (struct attr_indexentry *) (rdbuf + aoff))
1211 {
1212 if (!ntfs_isnamepermitted(ntmp, iep)) continue;
1213
1214 if (cnum >= num) {
1215 fp->f_lastdnum = cnum;
1216 fp->f_lastdoff = aoff;
1217 fp->f_lastdblnum = blnum;
1218 fp->f_lastdattr = attrnum;
1219
1220 *riepp = iep;
1221
1222 error = 0;
1223 goto fail;
1224 }
1225 cnum++;
1226 }
1227
1228 if (iavap) {
1229 if (attrnum == NTFS_A_INDXROOT)
1230 blnum = 0;
1231 else
1232 blnum++;
1233
1234 while (ntfs_cntob(blnum * cpbl) < iavap->va_datalen) {
1235 if (bmp[blnum >> 3] & (1 << (blnum & 7)))
1236 break;
1237 blnum++;
1238 }
1239
1240 attrnum = NTFS_A_INDX;
1241 aoff = 0;
1242 if (ntfs_cntob(blnum * cpbl) >= iavap->va_datalen)
1243 break;
1244 dprintf(("ntfs_ntreaddir: blnum: %d\n", (u_int32_t) blnum));
1245 }
1246 } while (iavap);
1247
1248 *riepp = NULL;
1249 fp->f_lastdnum = 0;
1250
1251 fail:
1252 if (vap)
1253 ntfs_ntvattrrele(vap);
1254 if (bmvap)
1255 ntfs_ntvattrrele(bmvap);
1256 if (iavap)
1257 ntfs_ntvattrrele(iavap);
1258 if (bmp)
1259 free(bmp, M_TEMP);
1260 ntfs_ntput(ip);
1261 return (error);
1262 }
1263
1264 /*
1265 * Convert NTFS times that are in 100 ns units and begins from
1266 * 1601 Jan 1 into unix times.
1267 */
1268 struct timespec
1269 ntfs_nttimetounix(
1270 u_int64_t nt)
1271 {
1272 struct timespec t;
1273
1274 /* WindowNT times are in 100 ns and from 1601 Jan 1 */
1275 t.tv_nsec = (nt % (1000 * 1000 * 10)) * 100;
1276 t.tv_sec = nt / (1000 * 1000 * 10) -
1277 369LL * 365LL * 24LL * 60LL * 60LL -
1278 89LL * 1LL * 24LL * 60LL * 60LL;
1279 return (t);
1280 }
1281
1282 /*
1283 * Get file times from NTFS_A_NAME attribute.
1284 */
1285 int
1286 ntfs_times(
1287 struct ntfsmount * ntmp,
1288 struct ntnode * ip,
1289 ntfs_times_t * tm)
1290 {
1291 struct ntvattr *vap;
1292 int error;
1293
1294 dprintf(("ntfs_times: ino: %d...\n", ip->i_number));
1295
1296 error = ntfs_ntget(ip);
1297 if (error)
1298 return (error);
1299
1300 error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
1301 if (error) {
1302 ntfs_ntput(ip);
1303 return (error);
1304 }
1305 *tm = vap->va_a_name->n_times;
1306 ntfs_ntvattrrele(vap);
1307 ntfs_ntput(ip);
1308
1309 return (0);
1310 }
1311
1312 /*
1313 * Get file sizes from corresponding attribute.
1314 *
1315 * ntnode under fnode should be locked.
1316 */
1317 int
1318 ntfs_filesize(
1319 struct ntfsmount * ntmp,
1320 struct fnode * fp,
1321 u_int64_t * size,
1322 u_int64_t * bytes)
1323 {
1324 struct ntvattr *vap;
1325 struct ntnode *ip = FTONT(fp);
1326 u_int64_t sz, bn;
1327 int error;
1328
1329 dprintf(("ntfs_filesize: ino: %d\n", ip->i_number));
1330
1331 error = ntfs_ntvattrget(ntmp, ip,
1332 fp->f_attrtype, fp->f_attrname, 0, &vap);
1333 if (error)
1334 return (error);
1335
1336 bn = vap->va_allocated;
1337 sz = vap->va_datalen;
1338
1339 dprintf(("ntfs_filesize: %d bytes (%d bytes allocated)\n",
1340 (u_int32_t) sz, (u_int32_t) bn));
1341
1342 if (size)
1343 *size = sz;
1344 if (bytes)
1345 *bytes = bn;
1346
1347 ntfs_ntvattrrele(vap);
1348
1349 return (0);
1350 }
1351
1352 /*
1353 * This is one of write routine.
1354 */
1355 int
1356 ntfs_writeattr_plain(
1357 struct ntfsmount * ntmp,
1358 struct ntnode * ip,
1359 u_int32_t attrnum,
1360 char *attrname,
1361 off_t roff,
1362 size_t rsize,
1363 void *rdata,
1364 size_t * initp,
1365 struct uio *uio)
1366 {
1367 size_t init;
1368 int error = 0;
1369 off_t off = roff, left = rsize, towrite;
1370 caddr_t data = rdata;
1371 struct ntvattr *vap;
1372 *initp = 0;
1373
1374 while (left) {
1375 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1376 ntfs_btocn(off), &vap);
1377 if (error)
1378 return (error);
1379 towrite = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1380 ddprintf(("ntfs_writeattr_plain: o: %d, s: %d (%d - %d)\n",
1381 (u_int32_t) off, (u_int32_t) towrite,
1382 (u_int32_t) vap->va_vcnstart,
1383 (u_int32_t) vap->va_vcnend));
1384 error = ntfs_writentvattr_plain(ntmp, ip, vap,
1385 off - ntfs_cntob(vap->va_vcnstart),
1386 towrite, data, &init, uio);
1387 if (error) {
1388 printf("ntfs_writeattr_plain: " \
1389 "ntfs_writentvattr_plain failed: o: %d, s: %d\n",
1390 (u_int32_t) off, (u_int32_t) towrite);
1391 printf("ntfs_writeattr_plain: attrib: %d - %d\n",
1392 (u_int32_t) vap->va_vcnstart,
1393 (u_int32_t) vap->va_vcnend);
1394 ntfs_ntvattrrele(vap);
1395 break;
1396 }
1397 ntfs_ntvattrrele(vap);
1398 left -= towrite;
1399 off += towrite;
1400 data = data + towrite;
1401 *initp += init;
1402 }
1403
1404 return (error);
1405 }
1406
1407 /*
1408 * This is one of write routine.
1409 *
1410 * ntnode should be locked.
1411 */
1412 int
1413 ntfs_writentvattr_plain(
1414 struct ntfsmount * ntmp,
1415 struct ntnode * ip,
1416 struct ntvattr * vap,
1417 off_t roff,
1418 size_t rsize,
1419 void *rdata,
1420 size_t * initp,
1421 struct uio *uio)
1422 {
1423 int error = 0;
1424 off_t off;
1425 int cnt;
1426 cn_t ccn, ccl, cn, left, cl;
1427 caddr_t data = rdata;
1428 struct buf *bp;
1429 size_t tocopy;
1430
1431 *initp = 0;
1432
1433 if ((vap->va_flag & NTFS_AF_INRUN) == 0) {
1434 printf("ntfs_writevattr_plain: CAN'T WRITE RES. ATTRIBUTE\n");
1435 return ENOTTY;
1436 }
1437
1438 ddprintf(("ntfs_writentvattr_plain: data in run: %ld chains\n",
1439 vap->va_vruncnt));
1440
1441 off = roff;
1442 left = rsize;
1443 ccl = 0;
1444 ccn = 0;
1445 cnt = 0;
1446 for (; left && (cnt < vap->va_vruncnt); cnt++) {
1447 ccn = vap->va_vruncn[cnt];
1448 ccl = vap->va_vruncl[cnt];
1449
1450 ddprintf(("ntfs_writentvattr_plain: " \
1451 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1452 (u_int32_t) left, (u_int32_t) ccn, \
1453 (u_int32_t) ccl, (u_int32_t) off));
1454
1455 if (ntfs_cntob(ccl) < off) {
1456 off -= ntfs_cntob(ccl);
1457 cnt++;
1458 continue;
1459 }
1460 if (!ccn && ip->i_number != NTFS_BOOTINO)
1461 continue; /* XXX */
1462
1463 ccl -= ntfs_btocn(off);
1464 cn = ccn + ntfs_btocn(off);
1465 off = ntfs_btocnoff(off);
1466
1467 while (left && ccl) {
1468 /*
1469 * Always read and write single clusters at a time -
1470 * we need to avoid requesting differently-sized
1471 * blocks at the same disk offsets to avoid
1472 * confusing the buffer cache.
1473 */
1474 tocopy = MIN(left, ntfs_cntob(1) - off);
1475 cl = ntfs_btocl(tocopy + off);
1476 KASSERT(cl == 1 && tocopy <= ntfs_cntob(1),
1477 ("single cluster limit mistake"));
1478 ddprintf(("ntfs_writentvattr_plain: write: " \
1479 "cn: 0x%x cl: %d, off: %d len: %d, left: %d\n",
1480 (u_int32_t) cn, (u_int32_t) cl,
1481 (u_int32_t) off, (u_int32_t) tocopy,
1482 (u_int32_t) left));
1483 if ((off == 0) && (tocopy == ntfs_cntob(cl)))
1484 {
1485 bp = getblk(ntmp->ntm_devvp, ntfs_cntobn(cn),
1486 ntfs_cntob(cl), 0, 0, 0);
1487 clrbuf(bp);
1488 } else {
1489 error = bread(ntmp->ntm_devvp, ntfs_cntobn(cn),
1490 ntfs_cntob(cl), NOCRED, &bp);
1491 if (error) {
1492 brelse(bp);
1493 return (error);
1494 }
1495 }
1496 if (uio)
1497 uiomove(bp->b_data + off, tocopy, uio);
1498 else
1499 memcpy(bp->b_data + off, data, tocopy);
1500 bawrite(bp);
1501 data = data + tocopy;
1502 *initp += tocopy;
1503 off = 0;
1504 left -= tocopy;
1505 cn += cl;
1506 ccl -= cl;
1507 }
1508 }
1509
1510 if (left) {
1511 printf("ntfs_writentvattr_plain: POSSIBLE RUN ERROR\n");
1512 error = EINVAL;
1513 }
1514
1515 return (error);
1516 }
1517
1518 /*
1519 * This is one of read routines.
1520 *
1521 * ntnode should be locked.
1522 */
1523 int
1524 ntfs_readntvattr_plain(
1525 struct ntfsmount * ntmp,
1526 struct ntnode * ip,
1527 struct ntvattr * vap,
1528 off_t roff,
1529 size_t rsize,
1530 void *rdata,
1531 size_t * initp,
1532 struct uio *uio)
1533 {
1534 int error = 0;
1535 off_t off;
1536
1537 *initp = 0;
1538 if (vap->va_flag & NTFS_AF_INRUN) {
1539 int cnt;
1540 cn_t ccn, ccl, cn, left, cl;
1541 caddr_t data = rdata;
1542 struct buf *bp;
1543 size_t tocopy;
1544
1545 ddprintf(("ntfs_readntvattr_plain: data in run: %ld chains\n",
1546 vap->va_vruncnt));
1547
1548 off = roff;
1549 left = rsize;
1550 ccl = 0;
1551 ccn = 0;
1552 cnt = 0;
1553 while (left && (cnt < vap->va_vruncnt)) {
1554 ccn = vap->va_vruncn[cnt];
1555 ccl = vap->va_vruncl[cnt];
1556
1557 ddprintf(("ntfs_readntvattr_plain: " \
1558 "left %d, cn: 0x%x, cl: %d, off: %d\n", \
1559 (u_int32_t) left, (u_int32_t) ccn, \
1560 (u_int32_t) ccl, (u_int32_t) off));
1561
1562 if (ntfs_cntob(ccl) < off) {
1563 off -= ntfs_cntob(ccl);
1564 cnt++;
1565 continue;
1566 }
1567 if (ccn || ip->i_number == NTFS_BOOTINO) {
1568 ccl -= ntfs_btocn(off);
1569 cn = ccn + ntfs_btocn(off);
1570 off = ntfs_btocnoff(off);
1571
1572 while (left && ccl) {
1573 /*
1574 * Always read single clusters at a
1575 * time - we need to avoid reading
1576 * differently-sized blocks at the
1577 * same disk offsets to avoid
1578 * confusing the buffer cache.
1579 */
1580 tocopy = MIN(left,
1581 ntfs_cntob(1) - off);
1582 cl = ntfs_btocl(tocopy + off);
1583 KASSERT(cl == 1 &&
1584 tocopy <= ntfs_cntob(1),
1585 ("single cluster limit mistake"));
1586
1587 ddprintf(("ntfs_readntvattr_plain: " \
1588 "read: cn: 0x%x cl: %d, " \
1589 "off: %d len: %d, left: %d\n",
1590 (u_int32_t) cn,
1591 (u_int32_t) cl,
1592 (u_int32_t) off,
1593 (u_int32_t) tocopy,
1594 (u_int32_t) left));
1595 error = bread(ntmp->ntm_devvp,
1596 ntfs_cntobn(cn),
1597 ntfs_cntob(cl),
1598 NOCRED, &bp);
1599 if (error) {
1600 brelse(bp);
1601 return (error);
1602 }
1603 if (uio) {
1604 uiomove(bp->b_data + off,
1605 tocopy, uio);
1606 } else {
1607 memcpy(data, bp->b_data + off,
1608 tocopy);
1609 }
1610 brelse(bp);
1611 data = data + tocopy;
1612 *initp += tocopy;
1613 off = 0;
1614 left -= tocopy;
1615 cn += cl;
1616 ccl -= cl;
1617 }
1618 } else {
1619 tocopy = MIN(left, ntfs_cntob(ccl) - off);
1620 ddprintf(("ntfs_readntvattr_plain: "
1621 "hole: ccn: 0x%x ccl: %d, off: %d, " \
1622 " len: %d, left: %d\n",
1623 (u_int32_t) ccn, (u_int32_t) ccl,
1624 (u_int32_t) off, (u_int32_t) tocopy,
1625 (u_int32_t) left));
1626 left -= tocopy;
1627 off = 0;
1628 if (uio) {
1629 size_t remains = tocopy;
1630 for(; remains; remains--)
1631 uiomove("", 1, uio);
1632 } else
1633 bzero(data, tocopy);
1634 data = data + tocopy;
1635 }
1636 cnt++;
1637 }
1638 if (left) {
1639 printf("ntfs_readntvattr_plain: POSSIBLE RUN ERROR\n");
1640 error = E2BIG;
1641 }
1642 } else {
1643 ddprintf(("ntfs_readnvattr_plain: data is in mft record\n"));
1644 if (uio)
1645 uiomove(vap->va_datap + roff, rsize, uio);
1646 else
1647 memcpy(rdata, vap->va_datap + roff, rsize);
1648 *initp += rsize;
1649 }
1650
1651 return (error);
1652 }
1653
1654 /*
1655 * This is one of read routines.
1656 */
1657 int
1658 ntfs_readattr_plain(
1659 struct ntfsmount * ntmp,
1660 struct ntnode * ip,
1661 u_int32_t attrnum,
1662 char *attrname,
1663 off_t roff,
1664 size_t rsize,
1665 void *rdata,
1666 size_t * initp,
1667 struct uio *uio)
1668 {
1669 size_t init;
1670 int error = 0;
1671 off_t off = roff, left = rsize, toread;
1672 caddr_t data = rdata;
1673 struct ntvattr *vap;
1674 *initp = 0;
1675
1676 while (left) {
1677 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname,
1678 ntfs_btocn(off), &vap);
1679 if (error)
1680 return (error);
1681 toread = MIN(left, ntfs_cntob(vap->va_vcnend + 1) - off);
1682 ddprintf(("ntfs_readattr_plain: o: %d, s: %d (%d - %d)\n",
1683 (u_int32_t) off, (u_int32_t) toread,
1684 (u_int32_t) vap->va_vcnstart,
1685 (u_int32_t) vap->va_vcnend));
1686 error = ntfs_readntvattr_plain(ntmp, ip, vap,
1687 off - ntfs_cntob(vap->va_vcnstart),
1688 toread, data, &init, uio);
1689 if (error) {
1690 printf("ntfs_readattr_plain: " \
1691 "ntfs_readntvattr_plain failed: o: %d, s: %d\n",
1692 (u_int32_t) off, (u_int32_t) toread);
1693 printf("ntfs_readattr_plain: attrib: %d - %d\n",
1694 (u_int32_t) vap->va_vcnstart,
1695 (u_int32_t) vap->va_vcnend);
1696 ntfs_ntvattrrele(vap);
1697 break;
1698 }
1699 ntfs_ntvattrrele(vap);
1700 left -= toread;
1701 off += toread;
1702 data = data + toread;
1703 *initp += init;
1704 }
1705
1706 return (error);
1707 }
1708
1709 /*
1710 * This is one of read routines.
1711 */
1712 int
1713 ntfs_readattr(
1714 struct ntfsmount * ntmp,
1715 struct ntnode * ip,
1716 u_int32_t attrnum,
1717 char *attrname,
1718 off_t roff,
1719 size_t rsize,
1720 void *rdata,
1721 struct uio *uio)
1722 {
1723 int error = 0;
1724 struct ntvattr *vap;
1725 size_t init;
1726
1727 ddprintf(("ntfs_readattr: reading %d: 0x%x, from %d size %d bytes\n",
1728 ip->i_number, attrnum, (u_int32_t) roff, (u_int32_t) rsize));
1729
1730 error = ntfs_ntvattrget(ntmp, ip, attrnum, attrname, 0, &vap);
1731 if (error)
1732 return (error);
1733
1734 if ((roff > vap->va_datalen) ||
1735 (roff + rsize > vap->va_datalen)) {
1736 ddprintf(("ntfs_readattr: offset too big\n"));
1737 ntfs_ntvattrrele(vap);
1738 return (E2BIG);
1739 }
1740 if (vap->va_compression && vap->va_compressalg) {
1741 u_int8_t *cup;
1742 u_int8_t *uup;
1743 off_t off = roff, left = rsize, tocopy;
1744 caddr_t data = rdata;
1745 cn_t cn;
1746
1747 ddprintf(("ntfs_ntreadattr: compression: %d\n",
1748 vap->va_compressalg));
1749
1750 cup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1751 M_NTFSDECOMP, M_WAITOK);
1752 uup = malloc(ntfs_cntob(NTFS_COMPUNIT_CL),
1753 M_NTFSDECOMP, M_WAITOK);
1754
1755 cn = (ntfs_btocn(roff)) & (~(NTFS_COMPUNIT_CL - 1));
1756 off = roff - ntfs_cntob(cn);
1757
1758 while (left) {
1759 error = ntfs_readattr_plain(ntmp, ip, attrnum,
1760 attrname, ntfs_cntob(cn),
1761 ntfs_cntob(NTFS_COMPUNIT_CL),
1762 cup, &init, NULL);
1763 if (error)
1764 break;
1765
1766 tocopy = MIN(left, ntfs_cntob(NTFS_COMPUNIT_CL) - off);
1767
1768 if (init == ntfs_cntob(NTFS_COMPUNIT_CL)) {
1769 if (uio)
1770 uiomove(cup + off, tocopy, uio);
1771 else
1772 memcpy(data, cup + off, tocopy);
1773 } else if (init == 0) {
1774 if (uio) {
1775 size_t remains = tocopy;
1776 for(; remains; remains--)
1777 uiomove("", 1, uio);
1778 }
1779 else
1780 bzero(data, tocopy);
1781 } else {
1782 error = ntfs_uncompunit(ntmp, uup, cup);
1783 if (error)
1784 break;
1785 if (uio)
1786 uiomove(uup + off, tocopy, uio);
1787 else
1788 memcpy(data, uup + off, tocopy);
1789 }
1790
1791 left -= tocopy;
1792 data = data + tocopy;
1793 off += tocopy - ntfs_cntob(NTFS_COMPUNIT_CL);
1794 cn += NTFS_COMPUNIT_CL;
1795 }
1796
1797 free(uup, M_NTFSDECOMP);
1798 free(cup, M_NTFSDECOMP);
1799 } else
1800 error = ntfs_readattr_plain(ntmp, ip, attrnum, attrname,
1801 roff, rsize, rdata, &init, uio);
1802 ntfs_ntvattrrele(vap);
1803 return (error);
1804 }
1805
1806 #if 0
1807 int
1808 ntfs_parserun(
1809 cn_t * cn,
1810 cn_t * cl,
1811 u_int8_t * run,
1812 u_long len,
1813 u_long *off)
1814 {
1815 u_int8_t sz;
1816 int i;
1817
1818 if (NULL == run) {
1819 printf("ntfs_parsetun: run == NULL\n");
1820 return (EINVAL);
1821 }
1822 sz = run[(*off)++];
1823 if (0 == sz) {
1824 printf("ntfs_parserun: trying to go out of run\n");
1825 return (E2BIG);
1826 }
1827 *cl = 0;
1828 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1829 printf("ntfs_parserun: " \
1830 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1831 sz, len, *off);
1832 return (EINVAL);
1833 }
1834 for (i = 0; i < (sz & 0xF); i++)
1835 *cl += (u_int32_t) run[(*off)++] << (i << 3);
1836
1837 sz >>= 4;
1838 if ((sz & 0xF) > 8 || (*off) + (sz & 0xF) > len) {
1839 printf("ntfs_parserun: " \
1840 "bad run: length too big: sz: 0x%02x (%ld < %ld + sz)\n",
1841 sz, len, *off);
1842 return (EINVAL);
1843 }
1844 for (i = 0; i < (sz & 0xF); i++)
1845 *cn += (u_int32_t) run[(*off)++] << (i << 3);
1846
1847 return (0);
1848 }
1849 #endif
1850
1851 /*
1852 * Process fixup routine on given buffer.
1853 */
1854 int
1855 ntfs_procfixups(
1856 struct ntfsmount * ntmp,
1857 u_int32_t magic,
1858 caddr_t buf,
1859 size_t len)
1860 {
1861 struct fixuphdr *fhp = (struct fixuphdr *) buf;
1862 int i;
1863 u_int16_t fixup;
1864 u_int16_t *fxp;
1865 u_int16_t *cfxp;
1866
1867 if (fhp->fh_magic != magic) {
1868 printf("ntfs_procfixups: magic doesn't match: %08x != %08x\n",
1869 fhp->fh_magic, magic);
1870 return (EINVAL);
1871 }
1872 if ((fhp->fh_fnum - 1) * ntmp->ntm_bps != len) {
1873 printf("ntfs_procfixups: " \
1874 "bad fixups number: %d for %ld bytes block\n",
1875 fhp->fh_fnum, (long)len); /* XXX printf kludge */
1876 return (EINVAL);
1877 }
1878 if (fhp->fh_foff >= ntmp->ntm_spc * ntmp->ntm_mftrecsz * ntmp->ntm_bps) {
1879 printf("ntfs_procfixups: invalid offset: %x", fhp->fh_foff);
1880 return (EINVAL);
1881 }
1882 fxp = (u_int16_t *) (buf + fhp->fh_foff);
1883 cfxp = (u_int16_t *) (buf + ntmp->ntm_bps - 2);
1884 fixup = *fxp++;
1885 for (i = 1; i < fhp->fh_fnum; i++, fxp++) {
1886 if (*cfxp != fixup) {
1887 printf("ntfs_procfixups: fixup %d doesn't match\n", i);
1888 return (EINVAL);
1889 }
1890 *cfxp = *fxp;
1891 cfxp = (u_int16_t *) ((caddr_t) cfxp + ntmp->ntm_bps);
1892 }
1893 return (0);
1894 }
1895
1896 #if 0
1897 int
1898 ntfs_runtocn(
1899 cn_t * cn,
1900 struct ntfsmount * ntmp,
1901 u_int8_t * run,
1902 u_long len,
1903 cn_t vcn)
1904 {
1905 cn_t ccn = 0;
1906 cn_t ccl = 0;
1907 u_long off = 0;
1908 int error = 0;
1909
1910 #if NTFS_DEBUG
1911 int i;
1912 printf("ntfs_runtocn: run: %p, %ld bytes, vcn:%ld\n",
1913 run, len, (u_long) vcn);
1914 printf("ntfs_runtocn: run: ");
1915 for (i = 0; i < len; i++)
1916 printf("0x%02x ", run[i]);
1917 printf("\n");
1918 #endif
1919
1920 if (NULL == run) {
1921 printf("ntfs_runtocn: run == NULL\n");
1922 return (EINVAL);
1923 }
1924 do {
1925 if (run[off] == 0) {
1926 printf("ntfs_runtocn: vcn too big\n");
1927 return (E2BIG);
1928 }
1929 vcn -= ccl;
1930 error = ntfs_parserun(&ccn, &ccl, run, len, &off);
1931 if (error) {
1932 printf("ntfs_runtocn: ntfs_parserun failed\n");
1933 return (error);
1934 }
1935 } while (ccl <= vcn);
1936 *cn = ccn + vcn;
1937 return (0);
1938 }
1939 #endif
1940
1941 /*
1942 * this initializes toupper table & dependant variables to be ready for
1943 * later work
1944 */
1945 void
1946 ntfs_toupper_init()
1947 {
1948 ntfs_toupper_tab = (wchar *) NULL;
1949 lockinit(&ntfs_toupper_lock, PVFS, "ntfs_toupper", 0, 0);
1950 ntfs_toupper_usecount = 0;
1951 }
1952
1953 void
1954 ntfs_toupper_destroy(void)
1955 {
1956
1957 lockdestroy(&ntfs_toupper_lock);
1958 }
1959
1960 /*
1961 * if the ntfs_toupper_tab[] is filled already, just raise use count;
1962 * otherwise read the data from the filesystem we are currently mounting
1963 */
1964 int
1965 ntfs_toupper_use(mp, ntmp)
1966 struct mount *mp;
1967 struct ntfsmount *ntmp;
1968 {
1969 int error = 0;
1970 struct vnode *vp;
1971
1972 /* get exclusive access */
1973 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
1974
1975 /* only read the translation data from a file if it hasn't been
1976 * read already */
1977 if (ntfs_toupper_tab)
1978 goto out;
1979
1980 /*
1981 * Read in Unicode lowercase -> uppercase translation file.
1982 * XXX for now, just the first 256 entries are used anyway,
1983 * so don't bother reading more
1984 */
1985 ntfs_toupper_tab = malloc(65536 * sizeof(wchar),
1986 M_NTFSRDATA, M_WAITOK);
1987
1988 if ((error = VFS_VGET(mp, NTFS_UPCASEINO, LK_EXCLUSIVE, &vp)))
1989 goto out;
1990 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
1991 0, 65536*sizeof(wchar), (char *) ntfs_toupper_tab, NULL);
1992 vput(vp);
1993
1994 out:
1995 ntfs_toupper_usecount++;
1996 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
1997 return (error);
1998 }
1999
2000 /*
2001 * lower the use count and if it reaches zero, free the memory
2002 * tied by toupper table
2003 */
2004 void
2005 ntfs_toupper_unuse()
2006 {
2007 /* get exclusive access */
2008 lockmgr(&ntfs_toupper_lock, LK_EXCLUSIVE, NULL);
2009
2010 ntfs_toupper_usecount--;
2011 if (ntfs_toupper_usecount == 0) {
2012 free(ntfs_toupper_tab, M_NTFSRDATA);
2013 ntfs_toupper_tab = NULL;
2014 }
2015 #ifdef DIAGNOSTIC
2016 else if (ntfs_toupper_usecount < 0) {
2017 panic("ntfs_toupper_unuse(): use count negative: %d\n",
2018 ntfs_toupper_usecount);
2019 }
2020 #endif
2021
2022 /* release the lock */
2023 lockmgr(&ntfs_toupper_lock, LK_RELEASE, NULL);
2024 }
2025
2026 int
2027 ntfs_u28_init(
2028 struct ntfsmount *ntmp,
2029 wchar *u2w,
2030 char *cs_local,
2031 char *cs_ntfs)
2032 {
2033 char ** u28;
2034 int i, j, h, l;
2035
2036 if (ntfs_iconv && cs_local) {
2037 ntfs_iconv->open(cs_local, cs_ntfs, &ntmp->ntm_ic_u2l);
2038 return (0);
2039 }
2040
2041 u28 = malloc(256 * sizeof(char*), M_TEMP, M_WAITOK | M_ZERO);
2042
2043 for (i=0; i<256; i++) {
2044 h = (u2w[i] >> 8) & 0xFF;
2045 l = (u2w[i]) &0xFF;
2046
2047 if (u28[h] == NULL) {
2048 u28[h] = malloc(256 * sizeof(char), M_TEMP, M_WAITOK);
2049 for (j=0; j<256; j++)
2050 u28[h][j] = '_';
2051 }
2052
2053 u28[h][l] = i & 0xFF;
2054 }
2055
2056 ntmp->ntm_u28 = u28;
2057
2058 return (0);
2059 }
2060
2061 int
2062 ntfs_u28_uninit(struct ntfsmount *ntmp)
2063 {
2064 char ** u28;
2065 int i;
2066
2067 if (ntmp->ntm_u28 == NULL) {
2068 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2069 ntfs_iconv->close(ntmp->ntm_ic_u2l);
2070 }
2071 return (0);
2072 }
2073
2074 u28 = ntmp->ntm_u28;
2075
2076 for (i=0; i<256; i++)
2077 if (u28[i] != NULL)
2078 free(u28[i], M_TEMP);
2079
2080 free(u28, M_TEMP);
2081
2082 return (0);
2083 }
2084
2085 int
2086 ntfs_82u_init(
2087 struct ntfsmount *ntmp,
2088 char *cs_local,
2089 char *cs_ntfs)
2090 {
2091 wchar * _82u;
2092 int i;
2093
2094 if (ntfs_iconv && cs_local) {
2095 ntfs_iconv->open(cs_ntfs, cs_local, &ntmp->ntm_ic_l2u);
2096 return (0);
2097 }
2098
2099 _82u = malloc(256 * sizeof(wchar), M_TEMP, M_WAITOK);
2100
2101 for (i=0; i<256; i++)
2102 _82u[i] = i;
2103
2104 ntmp->ntm_82u = _82u;
2105
2106 return (0);
2107 }
2108
2109 int
2110 ntfs_82u_uninit(struct ntfsmount *ntmp)
2111 {
2112
2113 if (ntmp->ntm_82u == NULL) {
2114 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2115 ntfs_iconv->close(ntmp->ntm_ic_l2u);
2116 }
2117 return (0);
2118 }
2119
2120 free(ntmp->ntm_82u, M_TEMP);
2121 return (0);
2122 }
2123
2124 /*
2125 * maps the Unicode char to local character
2126 */
2127 char *
2128 ntfs_u28(
2129 char *outbuf,
2130 struct ntfsmount *ntmp,
2131 wchar wc)
2132 {
2133 char *p, *outp, inbuf[3];
2134 size_t ilen, olen;
2135
2136 outp = outbuf;
2137 if (ntfs_iconv && ntmp->ntm_ic_u2l) {
2138 ilen = 2;
2139 olen = 4;
2140
2141 inbuf[0] = (char)(wc>>8);
2142 inbuf[1] = (char)wc;
2143 inbuf[2] = '\0';
2144 p = inbuf;
2145 ntfs_iconv->convchr(ntmp->ntm_ic_u2l, (const char **)&p, &ilen,
2146 &outp, &olen);
2147 if (olen == 4)
2148 *outp++ = '?';
2149 *outp = '\0';
2150 outp = outbuf;
2151 return (outp);
2152 }
2153
2154 p = ntmp->ntm_u28[(wc>>8)&0xFF];
2155 outbuf[0] = (p == NULL) ? '_' : p[wc&0xFF] & 0xFF;
2156 outbuf[1] = '\0';
2157 return (outp);
2158 }
2159
2160 wchar
2161 ntfs_82u(
2162 struct ntfsmount *ntmp,
2163 const char *c,
2164 int *len)
2165 {
2166 char *outp, outbuf[3];
2167 wchar uc;
2168 size_t ilen, olen;
2169
2170 if (ntfs_iconv && ntmp->ntm_ic_l2u) {
2171 ilen = (size_t)*len;
2172 olen = 2;
2173
2174 outp = outbuf;
2175 ntfs_iconv->convchr(ntmp->ntm_ic_l2u, &c, &ilen, &outp, &olen);
2176 *len -= (int)ilen;
2177 uc = (wchar)((outbuf[0]<<8) | (outbuf[1]&0xFF));
2178
2179 return (uc);
2180 }
2181
2182 if (ntmp->ntm_82u != NULL)
2183 return (ntmp->ntm_82u[*c&0xFF]);
2184
2185 return ('?');
2186 }
2187
Cache object: 5f99039a8b758716e09d343b3b48d438
|