ntfs_compr.c revision 1.5.22.1 1 1.5.22.1 jdolecek /* $NetBSD: ntfs_compr.c,v 1.5.22.1 2017/12/03 11:38:43 jdolecek 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.5.22.1 jdolecek __KERNEL_RCSID(0, "$NetBSD: ntfs_compr.c,v 1.5.22.1 2017/12/03 11:38:43 jdolecek 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/kernel.h>
38 1.1 jdolecek #include <sys/vnode.h>
39 1.1 jdolecek #include <sys/mount.h>
40 1.1 jdolecek #include <sys/malloc.h>
41 1.1 jdolecek
42 1.1 jdolecek #include <fs/ntfs/ntfs.h>
43 1.1 jdolecek #include <fs/ntfs/ntfs_compr.h>
44 1.1 jdolecek
45 1.1 jdolecek #define GET_UINT16(addr) (*((u_int16_t *)(addr)))
46 1.1 jdolecek
47 1.1 jdolecek int
48 1.5.22.1 jdolecek ntfs_uncompblock(u_int8_t *dbuf, u_int8_t *cbuf)
49 1.1 jdolecek {
50 1.5.22.1 jdolecek u_int32_t ctag;
51 1.5.22.1 jdolecek int len, dshift, lmask;
52 1.5.22.1 jdolecek int blen, boff;
53 1.5.22.1 jdolecek int i, j;
54 1.5.22.1 jdolecek int pos, cpos;
55 1.1 jdolecek
56 1.1 jdolecek len = GET_UINT16(cbuf) & 0xFFF;
57 1.1 jdolecek dprintf(("ntfs_uncompblock: block length: %d + 3, 0x%x,0x%04x\n",
58 1.1 jdolecek len, len, GET_UINT16(cbuf)));
59 1.1 jdolecek
60 1.1 jdolecek if (!(GET_UINT16(cbuf) & 0x8000)) {
61 1.1 jdolecek if ((len + 1) != NTFS_COMPBLOCK_SIZE) {
62 1.1 jdolecek dprintf(("ntfs_uncompblock: len: %x instead of %d\n",
63 1.1 jdolecek len, 0xfff));
64 1.1 jdolecek }
65 1.2 christos memcpy(dbuf, cbuf + 2, len + 1);
66 1.5 cegger memset(dbuf + len + 1, 0, NTFS_COMPBLOCK_SIZE - 1 - len);
67 1.1 jdolecek return len + 3;
68 1.1 jdolecek }
69 1.1 jdolecek cpos = 2;
70 1.1 jdolecek pos = 0;
71 1.1 jdolecek while ((cpos < len + 3) && (pos < NTFS_COMPBLOCK_SIZE)) {
72 1.1 jdolecek ctag = cbuf[cpos++];
73 1.1 jdolecek for (i = 0; (i < 8) && (pos < NTFS_COMPBLOCK_SIZE); i++) {
74 1.1 jdolecek if (ctag & 1) {
75 1.1 jdolecek for (j = pos - 1, lmask = 0xFFF, dshift = 12;
76 1.1 jdolecek j >= 0x10; j >>= 1) {
77 1.1 jdolecek dshift--;
78 1.1 jdolecek lmask >>= 1;
79 1.1 jdolecek }
80 1.1 jdolecek boff = -1 - (GET_UINT16(cbuf + cpos) >> dshift);
81 1.1 jdolecek blen = 3 + (GET_UINT16(cbuf + cpos) & lmask);
82 1.1 jdolecek for (j = 0; (j < blen) && (pos < NTFS_COMPBLOCK_SIZE); j++) {
83 1.2 christos dbuf[pos] = dbuf[pos + boff];
84 1.1 jdolecek pos++;
85 1.1 jdolecek }
86 1.1 jdolecek cpos += 2;
87 1.1 jdolecek } else {
88 1.2 christos dbuf[pos++] = cbuf[cpos++];
89 1.1 jdolecek }
90 1.1 jdolecek ctag >>= 1;
91 1.1 jdolecek }
92 1.1 jdolecek }
93 1.1 jdolecek return len + 3;
94 1.1 jdolecek }
95 1.1 jdolecek
96 1.1 jdolecek int
97 1.5.22.1 jdolecek ntfs_uncompunit(struct ntfsmount *ntmp, u_int8_t *uup, u_int8_t *cup)
98 1.1 jdolecek {
99 1.5.22.1 jdolecek int i;
100 1.5.22.1 jdolecek int off = 0;
101 1.5.22.1 jdolecek int new;
102 1.1 jdolecek
103 1.1 jdolecek for (i = 0; i * NTFS_COMPBLOCK_SIZE < ntfs_cntob(NTFS_COMPUNIT_CL); i++) {
104 1.1 jdolecek new = ntfs_uncompblock(uup + i * NTFS_COMPBLOCK_SIZE, cup + off);
105 1.1 jdolecek if (new == 0)
106 1.1 jdolecek return (EINVAL);
107 1.1 jdolecek off += new;
108 1.1 jdolecek }
109 1.1 jdolecek return (0);
110 1.1 jdolecek }
111