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