Annotation of sys/ntfs/ntfs_compr.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: ntfs_compr.c,v 1.2 2003/05/20 03:23:11 mickey 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/cdefs.h>
! 33: #ifdef __KERNEL_RCSID
! 34: __KERNEL_RCSID(0, "$NetBSD: ntfs_compr.c,v 1.1 2002/12/23 17:38:31 jdolecek Exp $");
! 35: #endif
! 36:
! 37: #include <sys/param.h>
! 38: #include <sys/systm.h>
! 39: #include <sys/namei.h>
! 40: #include <sys/proc.h>
! 41: #include <sys/kernel.h>
! 42: #include <sys/vnode.h>
! 43: #include <sys/mount.h>
! 44: #include <sys/buf.h>
! 45: #include <sys/file.h>
! 46: #include <sys/malloc.h>
! 47: #ifdef __FreeBSD__
! 48: #include <machine/clock.h>
! 49: #endif
! 50:
! 51: #include <miscfs/specfs/specdev.h>
! 52:
! 53: #if defined(__FreeBSD__) || defined(__NetBSD__)
! 54: #include <fs/ntfs/ntfs.h>
! 55: #include <fs/ntfs/ntfs_compr.h>
! 56: #else
! 57: #include <ntfs/ntfs.h>
! 58: #include <ntfs/ntfs_compr.h>
! 59: #endif
! 60:
! 61: #define GET_UINT16(addr) (*((u_int16_t *)(addr)))
! 62:
! 63: int
! 64: ntfs_uncompblock(
! 65: u_int8_t * buf,
! 66: u_int8_t * cbuf)
! 67: {
! 68: u_int32_t ctag;
! 69: int len, dshift, lmask;
! 70: int blen, boff;
! 71: int i, j;
! 72: int pos, cpos;
! 73:
! 74: len = GET_UINT16(cbuf) & 0xFFF;
! 75: dprintf(("ntfs_uncompblock: block length: %d + 3, 0x%x,0x%04x\n",
! 76: len, len, GET_UINT16(cbuf)));
! 77:
! 78: if (!(GET_UINT16(cbuf) & 0x8000)) {
! 79: if ((len + 1) != NTFS_COMPBLOCK_SIZE) {
! 80: dprintf(("ntfs_uncompblock: len: %x instead of %d\n",
! 81: len, 0xfff));
! 82: }
! 83: memcpy(buf, cbuf + 2, len + 1);
! 84: bzero(buf + len + 1, NTFS_COMPBLOCK_SIZE - 1 - len);
! 85: return len + 3;
! 86: }
! 87: cpos = 2;
! 88: pos = 0;
! 89: while ((cpos < len + 3) && (pos < NTFS_COMPBLOCK_SIZE)) {
! 90: ctag = cbuf[cpos++];
! 91: for (i = 0; (i < 8) && (pos < NTFS_COMPBLOCK_SIZE); i++) {
! 92: if (ctag & 1) {
! 93: for (j = pos - 1, lmask = 0xFFF, dshift = 12;
! 94: j >= 0x10; j >>= 1) {
! 95: dshift--;
! 96: lmask >>= 1;
! 97: }
! 98: boff = -1 - (GET_UINT16(cbuf + cpos) >> dshift);
! 99: blen = 3 + (GET_UINT16(cbuf + cpos) & lmask);
! 100: for (j = 0; (j < blen) && (pos < NTFS_COMPBLOCK_SIZE); j++) {
! 101: buf[pos] = buf[pos + boff];
! 102: pos++;
! 103: }
! 104: cpos += 2;
! 105: } else {
! 106: buf[pos++] = cbuf[cpos++];
! 107: }
! 108: ctag >>= 1;
! 109: }
! 110: }
! 111: return len + 3;
! 112: }
! 113:
! 114: int
! 115: ntfs_uncompunit(
! 116: struct ntfsmount * ntmp,
! 117: u_int8_t * uup,
! 118: u_int8_t * cup)
! 119: {
! 120: int i;
! 121: int off = 0;
! 122: int new;
! 123:
! 124: for (i = 0; i * NTFS_COMPBLOCK_SIZE < ntfs_cntob(NTFS_COMPUNIT_CL); i++) {
! 125: new = ntfs_uncompblock(uup + i * NTFS_COMPBLOCK_SIZE, cup + off);
! 126: if (new == 0)
! 127: return (EINVAL);
! 128: off += new;
! 129: }
! 130: return (0);
! 131: }
CVSweb