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