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