Home | History | Annotate | Line # | Download | only in ntfs
ntfs_compr.c revision 1.2.2.1
      1  1.2.2.1      yamt /*	$NetBSD: ntfs_compr.c,v 1.2.2.1 2008/02/04 09:23:47 yamt 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.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: ntfs_compr.c,v 1.2.2.1 2008/02/04 09:23:47 yamt 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 
     45      1.1  jdolecek #include <miscfs/specfs/specdev.h>
     46      1.1  jdolecek 
     47      1.1  jdolecek #include <fs/ntfs/ntfs.h>
     48      1.1  jdolecek #include <fs/ntfs/ntfs_compr.h>
     49      1.1  jdolecek 
     50      1.1  jdolecek #define GET_UINT16(addr)	(*((u_int16_t *)(addr)))
     51      1.1  jdolecek 
     52      1.1  jdolecek int
     53      1.1  jdolecek ntfs_uncompblock(
     54      1.2  christos 	u_int8_t * dbuf,
     55      1.1  jdolecek 	u_int8_t * cbuf)
     56      1.1  jdolecek {
     57      1.1  jdolecek 	u_int32_t       ctag;
     58      1.1  jdolecek 	int             len, dshift, lmask;
     59      1.1  jdolecek 	int             blen, boff;
     60      1.1  jdolecek 	int             i, j;
     61      1.1  jdolecek 	int             pos, cpos;
     62      1.1  jdolecek 
     63      1.1  jdolecek 	len = GET_UINT16(cbuf) & 0xFFF;
     64      1.1  jdolecek 	dprintf(("ntfs_uncompblock: block length: %d + 3, 0x%x,0x%04x\n",
     65      1.1  jdolecek 	    len, len, GET_UINT16(cbuf)));
     66      1.1  jdolecek 
     67      1.1  jdolecek 	if (!(GET_UINT16(cbuf) & 0x8000)) {
     68      1.1  jdolecek 		if ((len + 1) != NTFS_COMPBLOCK_SIZE) {
     69      1.1  jdolecek 			dprintf(("ntfs_uncompblock: len: %x instead of %d\n",
     70      1.1  jdolecek 			    len, 0xfff));
     71      1.1  jdolecek 		}
     72      1.2  christos 		memcpy(dbuf, cbuf + 2, len + 1);
     73      1.2  christos 		bzero(dbuf + len + 1, NTFS_COMPBLOCK_SIZE - 1 - len);
     74      1.1  jdolecek 		return len + 3;
     75      1.1  jdolecek 	}
     76      1.1  jdolecek 	cpos = 2;
     77      1.1  jdolecek 	pos = 0;
     78      1.1  jdolecek 	while ((cpos < len + 3) && (pos < NTFS_COMPBLOCK_SIZE)) {
     79      1.1  jdolecek 		ctag = cbuf[cpos++];
     80      1.1  jdolecek 		for (i = 0; (i < 8) && (pos < NTFS_COMPBLOCK_SIZE); i++) {
     81      1.1  jdolecek 			if (ctag & 1) {
     82      1.1  jdolecek 				for (j = pos - 1, lmask = 0xFFF, dshift = 12;
     83      1.1  jdolecek 				     j >= 0x10; j >>= 1) {
     84      1.1  jdolecek 					dshift--;
     85      1.1  jdolecek 					lmask >>= 1;
     86      1.1  jdolecek 				}
     87      1.1  jdolecek 				boff = -1 - (GET_UINT16(cbuf + cpos) >> dshift);
     88      1.1  jdolecek 				blen = 3 + (GET_UINT16(cbuf + cpos) & lmask);
     89      1.1  jdolecek 				for (j = 0; (j < blen) && (pos < NTFS_COMPBLOCK_SIZE); j++) {
     90      1.2  christos 					dbuf[pos] = dbuf[pos + boff];
     91      1.1  jdolecek 					pos++;
     92      1.1  jdolecek 				}
     93      1.1  jdolecek 				cpos += 2;
     94      1.1  jdolecek 			} else {
     95      1.2  christos 				dbuf[pos++] = cbuf[cpos++];
     96      1.1  jdolecek 			}
     97      1.1  jdolecek 			ctag >>= 1;
     98      1.1  jdolecek 		}
     99      1.1  jdolecek 	}
    100      1.1  jdolecek 	return len + 3;
    101      1.1  jdolecek }
    102      1.1  jdolecek 
    103      1.1  jdolecek int
    104      1.1  jdolecek ntfs_uncompunit(
    105      1.1  jdolecek 	struct ntfsmount * ntmp,
    106      1.1  jdolecek 	u_int8_t * uup,
    107      1.1  jdolecek 	u_int8_t * cup)
    108      1.1  jdolecek {
    109      1.1  jdolecek 	int             i;
    110      1.1  jdolecek 	int             off = 0;
    111      1.1  jdolecek 	int             new;
    112      1.1  jdolecek 
    113      1.1  jdolecek 	for (i = 0; i * NTFS_COMPBLOCK_SIZE < ntfs_cntob(NTFS_COMPUNIT_CL); i++) {
    114      1.1  jdolecek 		new = ntfs_uncompblock(uup + i * NTFS_COMPBLOCK_SIZE, cup + off);
    115      1.1  jdolecek 		if (new == 0)
    116      1.1  jdolecek 			return (EINVAL);
    117      1.1  jdolecek 		off += new;
    118      1.1  jdolecek 	}
    119      1.1  jdolecek 	return (0);
    120      1.1  jdolecek }
    121