2N/A/* ntfscomp.c - compression support for the NTFS filesystem */
2N/A/*
2N/A * Copyright (C) 2007 Free Software Foundation, Inc.
2N/A *
2N/A * This program is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * This program is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with this program. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/file.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/ntfs.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic grub_err_t
2N/Adecomp_nextvcn (struct grub_ntfs_comp *cc)
2N/A{
2N/A if (cc->comp_head >= cc->comp_tail)
2N/A return grub_error (GRUB_ERR_BAD_FS, "compression block overflown");
2N/A if (grub_disk_read
2N/A (cc->disk,
2N/A (cc->comp_table[cc->comp_head][1] -
2N/A (cc->comp_table[cc->comp_head][0] - cc->cbuf_vcn)) * cc->spc, 0,
2N/A cc->spc << GRUB_NTFS_BLK_SHR, cc->cbuf))
2N/A return grub_errno;
2N/A cc->cbuf_vcn++;
2N/A if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head][0]))
2N/A cc->comp_head++;
2N/A cc->cbuf_ofs = 0;
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Adecomp_getch (struct grub_ntfs_comp *cc, unsigned char *res)
2N/A{
2N/A if (cc->cbuf_ofs >= (cc->spc << GRUB_NTFS_BLK_SHR))
2N/A {
2N/A if (decomp_nextvcn (cc))
2N/A return grub_errno;
2N/A }
2N/A *res = (unsigned char) cc->cbuf[cc->cbuf_ofs++];
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Adecomp_get16 (struct grub_ntfs_comp *cc, grub_uint16_t * res)
2N/A{
2N/A unsigned char c1 = 0, c2 = 0;
2N/A
2N/A if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2)))
2N/A return grub_errno;
2N/A *res = ((grub_uint16_t) c2) * 256 + ((grub_uint16_t) c1);
2N/A return 0;
2N/A}
2N/A
2N/A/* Decompress a block (4096 bytes) */
2N/Astatic grub_err_t
2N/Adecomp_block (struct grub_ntfs_comp *cc, char *dest)
2N/A{
2N/A grub_uint16_t flg, cnt;
2N/A
2N/A if (decomp_get16 (cc, &flg))
2N/A return grub_errno;
2N/A cnt = (flg & 0xFFF) + 1;
2N/A
2N/A if (dest)
2N/A {
2N/A if (flg & 0x8000)
2N/A {
2N/A unsigned char tag;
2N/A grub_uint32_t bits, copied;
2N/A
2N/A bits = copied = tag = 0;
2N/A while (cnt > 0)
2N/A {
2N/A if (copied > GRUB_NTFS_COM_LEN)
2N/A return grub_error (GRUB_ERR_BAD_FS,
2N/A "compression block too large");
2N/A
2N/A if (!bits)
2N/A {
2N/A if (decomp_getch (cc, &tag))
2N/A return grub_errno;
2N/A
2N/A bits = 8;
2N/A cnt--;
2N/A if (cnt <= 0)
2N/A break;
2N/A }
2N/A if (tag & 1)
2N/A {
2N/A grub_uint32_t i, len, delta, code, lmask, dshift;
2N/A grub_uint16_t word;
2N/A
2N/A if (decomp_get16 (cc, &word))
2N/A return grub_errno;
2N/A
2N/A code = word;
2N/A cnt -= 2;
2N/A
2N/A if (!copied)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FS, "nontext window empty");
2N/A return 0;
2N/A }
2N/A
2N/A for (i = copied - 1, lmask = 0xFFF, dshift = 12; i >= 0x10;
2N/A i >>= 1)
2N/A {
2N/A lmask >>= 1;
2N/A dshift--;
2N/A }
2N/A
2N/A delta = code >> dshift;
2N/A len = (code & lmask) + 3;
2N/A
2N/A for (i = 0; i < len; i++)
2N/A {
2N/A dest[copied] = dest[copied - delta - 1];
2N/A copied++;
2N/A }
2N/A }
2N/A else
2N/A {
2N/A unsigned char ch = 0;
2N/A
2N/A if (decomp_getch (cc, &ch))
2N/A return grub_errno;
2N/A dest[copied++] = ch;
2N/A cnt--;
2N/A }
2N/A tag >>= 1;
2N/A bits--;
2N/A }
2N/A return 0;
2N/A }
2N/A else
2N/A {
2N/A if (cnt != GRUB_NTFS_COM_LEN)
2N/A return grub_error (GRUB_ERR_BAD_FS,
2N/A "invalid compression block size");
2N/A }
2N/A }
2N/A
2N/A while (cnt > 0)
2N/A {
2N/A int n;
2N/A
2N/A n = (cc->spc << GRUB_NTFS_BLK_SHR) - cc->cbuf_ofs;
2N/A if (n > cnt)
2N/A n = cnt;
2N/A if ((dest) && (n))
2N/A {
2N/A grub_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n);
2N/A dest += n;
2N/A }
2N/A cnt -= n;
2N/A cc->cbuf_ofs += n;
2N/A if ((cnt) && (decomp_nextvcn (cc)))
2N/A return grub_errno;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Aread_block (struct grub_ntfs_rlst *ctx, char *buf, int num)
2N/A{
2N/A int cpb = GRUB_NTFS_COM_SEC / ctx->comp.spc;
2N/A
2N/A while (num)
2N/A {
2N/A int nn;
2N/A
2N/A if ((ctx->target_vcn & 0xF) == 0)
2N/A {
2N/A
2N/A if (ctx->comp.comp_head != ctx->comp.comp_tail)
2N/A return grub_error (GRUB_ERR_BAD_FS, "invalid compression block");
2N/A ctx->comp.comp_head = ctx->comp.comp_tail = 0;
2N/A ctx->comp.cbuf_vcn = ctx->target_vcn;
2N/A ctx->comp.cbuf_ofs = (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
2N/A if (ctx->target_vcn >= ctx->next_vcn)
2N/A {
2N/A if (grub_ntfs_read_run_list (ctx))
2N/A return grub_errno;
2N/A }
2N/A while (ctx->target_vcn + 16 > ctx->next_vcn)
2N/A {
2N/A if (ctx->flags & GRUB_NTFS_RF_BLNK)
2N/A break;
2N/A ctx->comp.comp_table[ctx->comp.comp_tail][0] = ctx->next_vcn;
2N/A ctx->comp.comp_table[ctx->comp.comp_tail][1] =
2N/A ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn;
2N/A ctx->comp.comp_tail++;
2N/A if (grub_ntfs_read_run_list (ctx))
2N/A return grub_errno;
2N/A }
2N/A }
2N/A
2N/A nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) / cpb;
2N/A if (nn > num)
2N/A nn = num;
2N/A num -= nn;
2N/A
2N/A if (ctx->flags & GRUB_NTFS_RF_BLNK)
2N/A {
2N/A ctx->target_vcn += nn * cpb;
2N/A if (ctx->comp.comp_tail == 0)
2N/A {
2N/A if (buf)
2N/A {
2N/A grub_memset (buf, 0, nn * GRUB_NTFS_COM_LEN);
2N/A buf += nn * GRUB_NTFS_COM_LEN;
2N/A }
2N/A }
2N/A else
2N/A {
2N/A while (nn)
2N/A {
2N/A if (decomp_block (&ctx->comp, buf))
2N/A return grub_errno;
2N/A if (buf)
2N/A buf += GRUB_NTFS_COM_LEN;
2N/A nn--;
2N/A }
2N/A }
2N/A }
2N/A else
2N/A {
2N/A nn *= cpb;
2N/A while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn))
2N/A {
2N/A int tt;
2N/A
2N/A tt =
2N/A ctx->comp.comp_table[ctx->comp.comp_head][0] -
2N/A ctx->target_vcn;
2N/A if (tt > nn)
2N/A tt = nn;
2N/A ctx->target_vcn += tt;
2N/A if (buf)
2N/A {
2N/A if (grub_disk_read
2N/A (ctx->comp.disk,
2N/A (ctx->comp.comp_table[ctx->comp.comp_head][1] -
2N/A (ctx->comp.comp_table[ctx->comp.comp_head][0] -
2N/A ctx->target_vcn)) * ctx->comp.spc, 0,
2N/A tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
2N/A return grub_errno;
2N/A buf += tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
2N/A }
2N/A nn -= tt;
2N/A if (ctx->target_vcn >=
2N/A ctx->comp.comp_table[ctx->comp.comp_head][0])
2N/A ctx->comp.comp_head++;
2N/A }
2N/A if (nn)
2N/A {
2N/A if (buf)
2N/A {
2N/A if (grub_disk_read
2N/A (ctx->comp.disk,
2N/A (ctx->target_vcn - ctx->curr_vcn +
2N/A ctx->curr_lcn) * ctx->comp.spc, 0,
2N/A nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
2N/A return grub_errno;
2N/A buf += nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
2N/A }
2N/A ctx->target_vcn += nn;
2N/A }
2N/A }
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Antfscomp (struct grub_ntfs_attr *at, char *dest, grub_uint32_t ofs,
2N/A grub_uint32_t len, struct grub_ntfs_rlst *ctx, grub_uint32_t vcn)
2N/A{
2N/A grub_err_t ret;
2N/A
2N/A ctx->comp.comp_head = ctx->comp.comp_tail = 0;
2N/A ctx->comp.cbuf = grub_malloc ((ctx->comp.spc) << GRUB_NTFS_BLK_SHR);
2N/A if (!ctx->comp.cbuf)
2N/A return 0;
2N/A
2N/A ret = 0;
2N/A
2N/A //ctx->comp.disk->read_hook = read_hook;
2N/A
2N/A if ((vcn > ctx->target_vcn) &&
2N/A (read_block
2N/A (ctx, NULL, ((vcn - ctx->target_vcn) * ctx->comp.spc) / GRUB_NTFS_COM_SEC)))
2N/A {
2N/A ret = grub_errno;
2N/A goto quit;
2N/A }
2N/A
2N/A if (ofs % GRUB_NTFS_COM_LEN)
2N/A {
2N/A grub_uint32_t t, n, o;
2N/A
2N/A t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
2N/A if (read_block (ctx, at->sbuf, 1))
2N/A {
2N/A ret = grub_errno;
2N/A goto quit;
2N/A }
2N/A
2N/A at->save_pos = t;
2N/A
2N/A o = ofs % GRUB_NTFS_COM_LEN;
2N/A n = GRUB_NTFS_COM_LEN - o;
2N/A if (n > len)
2N/A n = len;
2N/A grub_memcpy (dest, &at->sbuf[o], n);
2N/A if (n == len)
2N/A goto quit;
2N/A dest += n;
2N/A len -= n;
2N/A }
2N/A
2N/A if (read_block (ctx, dest, len / GRUB_NTFS_COM_LEN))
2N/A {
2N/A ret = grub_errno;
2N/A goto quit;
2N/A }
2N/A
2N/A dest += (len / GRUB_NTFS_COM_LEN) * GRUB_NTFS_COM_LEN;
2N/A len = len % GRUB_NTFS_COM_LEN;
2N/A if (len)
2N/A {
2N/A grub_uint32_t t;
2N/A
2N/A t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
2N/A if (read_block (ctx, at->sbuf, 1))
2N/A {
2N/A ret = grub_errno;
2N/A goto quit;
2N/A }
2N/A
2N/A at->save_pos = t;
2N/A
2N/A grub_memcpy (dest, at->sbuf, len);
2N/A }
2N/A
2N/Aquit:
2N/A //ctx->comp.disk->read_hook = 0;
2N/A if (ctx->comp.cbuf)
2N/A grub_free (ctx->comp.cbuf);
2N/A return ret;
2N/A}
2N/A
2N/AGRUB_MOD_INIT (ntfscomp)
2N/A{
2N/A grub_ntfscomp_func = ntfscomp;
2N/A}
2N/A
2N/AGRUB_MOD_FINI (ntfscomp)
2N/A{
2N/A grub_ntfscomp_func = NULL;
2N/A}