ntfs_compr.c revision 1.2 1 1.2 christos /* $NetBSD: ntfs_compr.c,v 1.2 2005/05/29 21:00:29 christos Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*-
4 1.1 jdolecek * Copyright (c) 1998, 1999 Semen Ustimenko
5 1.1 jdolecek * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.1 jdolecek * modification, are permitted provided that the following conditions
9 1.1 jdolecek * are met:
10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.1 jdolecek * documentation and/or other materials provided with the distribution.
15 1.1 jdolecek *
16 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jdolecek * SUCH DAMAGE.
27 1.1 jdolecek *
28 1.1 jdolecek * Id: ntfs_compr.c,v 1.4 1999/05/12 09:42:54 semenu Exp
29 1.1 jdolecek */
30 1.1 jdolecek
31 1.1 jdolecek #include <sys/cdefs.h>
32 1.2 christos __KERNEL_RCSID(0, "$NetBSD: ntfs_compr.c,v 1.2 2005/05/29 21:00:29 christos Exp $");
33 1.1 jdolecek
34 1.1 jdolecek #include <sys/param.h>
35 1.1 jdolecek #include <sys/systm.h>
36 1.1 jdolecek #include <sys/namei.h>
37 1.1 jdolecek #include <sys/proc.h>
38 1.1 jdolecek #include <sys/kernel.h>
39 1.1 jdolecek #include <sys/vnode.h>
40 1.1 jdolecek #include <sys/mount.h>
41 1.1 jdolecek #include <sys/buf.h>
42 1.1 jdolecek #include <sys/file.h>
43 1.1 jdolecek #include <sys/malloc.h>
44 1.1 jdolecek #ifdef __FreeBSD__
45 1.1 jdolecek #include <machine/clock.h>
46 1.1 jdolecek #endif
47 1.1 jdolecek
48 1.1 jdolecek #include <miscfs/specfs/specdev.h>
49 1.1 jdolecek
50 1.1 jdolecek #include <fs/ntfs/ntfs.h>
51 1.1 jdolecek #include <fs/ntfs/ntfs_compr.h>
52 1.1 jdolecek
53 1.1 jdolecek #define GET_UINT16(addr) (*((u_int16_t *)(addr)))
54 1.1 jdolecek
55 1.1 jdolecek int
56 1.1 jdolecek ntfs_uncompblock(
57 1.2 christos u_int8_t * dbuf,
58 1.1 jdolecek u_int8_t * cbuf)
59 1.1 jdolecek {
60 1.1 jdolecek u_int32_t ctag;
61 1.1 jdolecek int len, dshift, lmask;
62 1.1 jdolecek int blen, boff;
63 1.1 jdolecek int i, j;
64 1.1 jdolecek int pos, cpos;
65 1.1 jdolecek
66 1.1 jdolecek len = GET_UINT16(cbuf) & 0xFFF;
67 1.1 jdolecek dprintf(("ntfs_uncompblock: block length: %d + 3, 0x%x,0x%04x\n",
68 1.1 jdolecek len, len, GET_UINT16(cbuf)));
69 1.1 jdolecek
70 1.1 jdolecek if (!(GET_UINT16(cbuf) & 0x8000)) {
71 1.1 jdolecek if ((len + 1) != NTFS_COMPBLOCK_SIZE) {
72 1.1 jdolecek dprintf(("ntfs_uncompblock: len: %x instead of %d\n",
73 1.1 jdolecek len, 0xfff));
74 1.1 jdolecek }
75 1.2 christos memcpy(dbuf, cbuf + 2, len + 1);
76 1.2 christos bzero(dbuf + len + 1, NTFS_COMPBLOCK_SIZE - 1 - len);
77 1.1 jdolecek return len + 3;
78 1.1 jdolecek }
79 1.1 jdolecek cpos = 2;
80 1.1 jdolecek pos = 0;
81 1.1 jdolecek while ((cpos < len + 3) && (pos < NTFS_COMPBLOCK_SIZE)) {
82 1.1 jdolecek ctag = cbuf[cpos++];
83 1.1 jdolecek for (i = 0; (i < 8) && (pos < NTFS_COMPBLOCK_SIZE); i++) {
84 1.1 jdolecek if (ctag & 1) {
85 1.1 jdolecek for (j = pos - 1, lmask = 0xFFF, dshift = 12;
86 1.1 jdolecek j >= 0x10; j >>= 1) {
87 1.1 jdolecek dshift--;
88 1.1 jdolecek lmask >>= 1;
89 1.1 jdolecek }
90 1.1 jdolecek boff = -1 - (GET_UINT16(cbuf + cpos) >> dshift);
91 1.1 jdolecek blen = 3 + (GET_UINT16(cbuf + cpos) & lmask);
92 1.1 jdolecek for (j = 0; (j < blen) && (pos < NTFS_COMPBLOCK_SIZE); j++) {
93 1.2 christos dbuf[pos] = dbuf[pos + boff];
94 1.1 jdolecek pos++;
95 1.1 jdolecek }
96 1.1 jdolecek cpos += 2;
97 1.1 jdolecek } else {
98 1.2 christos dbuf[pos++] = cbuf[cpos++];
99 1.1 jdolecek }
100 1.1 jdolecek ctag >>= 1;
101 1.1 jdolecek }
102 1.1 jdolecek }
103 1.1 jdolecek return len + 3;
104 1.1 jdolecek }
105 1.1 jdolecek
106 1.1 jdolecek int
107 1.1 jdolecek ntfs_uncompunit(
108 1.1 jdolecek struct ntfsmount * ntmp,
109 1.1 jdolecek u_int8_t * uup,
110 1.1 jdolecek u_int8_t * cup)
111 1.1 jdolecek {
112 1.1 jdolecek int i;
113 1.1 jdolecek int off = 0;
114 1.1 jdolecek int new;
115 1.1 jdolecek
116 1.1 jdolecek for (i = 0; i * NTFS_COMPBLOCK_SIZE < ntfs_cntob(NTFS_COMPUNIT_CL); i++) {
117 1.1 jdolecek new = ntfs_uncompblock(uup + i * NTFS_COMPBLOCK_SIZE, cup + off);
118 1.1 jdolecek if (new == 0)
119 1.1 jdolecek return (EINVAL);
120 1.1 jdolecek off += new;
121 1.1 jdolecek }
122 1.1 jdolecek return (0);
123 1.1 jdolecek }
124