#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mount.h>
#include <ntfs/ntfs.h>
#include <ntfs/ntfs_compr.h>
#define GET_UINT16(addr) (*((u_int16_t *)(addr)))
int
ntfs_uncompblock(u_int8_t *buf, u_int8_t *cbuf)
{
u_int32_t ctag;
int len, dshift, lmask;
int blen, boff;
int i, j;
int pos, cpos;
len = GET_UINT16(cbuf) & 0xFFF;
DPRINTF("ntfs_uncompblock: block length: %d + 3, 0x%x,0x%04x\n",
len, len, GET_UINT16(cbuf));
if (!(GET_UINT16(cbuf) & 0x8000)) {
if ((len + 1) != NTFS_COMPBLOCK_SIZE) {
DPRINTF("ntfs_uncompblock: len: %x instead of %d\n",
len, 0xfff);
}
memcpy(buf, cbuf + 2, len + 1);
bzero(buf + len + 1, NTFS_COMPBLOCK_SIZE - 1 - len);
return (len + 3);
}
cpos = 2;
pos = 0;
while ((cpos < len + 3) && (pos < NTFS_COMPBLOCK_SIZE)) {
ctag = cbuf[cpos++];
for (i = 0; (i < 8) && (pos < NTFS_COMPBLOCK_SIZE); i++) {
if (ctag & 1) {
for (j = pos - 1, lmask = 0xFFF, dshift = 12;
j >= 0x10; j >>= 1) {
dshift--;
lmask >>= 1;
}
boff = -1 - (GET_UINT16(cbuf + cpos) >> dshift);
blen = 3 + (GET_UINT16(cbuf + cpos) & lmask);
for (j = 0; (j < blen) &&
(pos < NTFS_COMPBLOCK_SIZE); j++) {
buf[pos] = buf[pos + boff];
pos++;
}
cpos += 2;
} else {
buf[pos++] = cbuf[cpos++];
}
ctag >>= 1;
}
}
return (len + 3);
}
int
ntfs_uncompunit(struct ntfsmount *ntmp, u_int8_t *uup, u_int8_t *cup)
{
int i;
int off = 0;
int new;
for (i = 0; i * NTFS_COMPBLOCK_SIZE < ntfs_cntob(NTFS_COMPUNIT_CL);
i++) {
new = ntfs_uncompblock(uup + i * NTFS_COMPBLOCK_SIZE,
cup + off);
if (new == 0)
return (EINVAL);
off += new;
}
return (0);
}