grub/grub-core/fs/ntfscomp.c

377 lines
8.2 KiB
C
Raw Normal View History

/* ntfscomp.c - compression support for the NTFS filesystem */
/*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/file.h>
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/disk.h>
#include <grub/dl.h>
#include <grub/ntfs.h>
GRUB_MOD_LICENSE ("GPLv3+");
static grub_err_t
decomp_nextvcn (struct grub_ntfs_comp *cc)
{
if (cc->comp_head >= cc->comp_tail)
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
return grub_error (GRUB_ERR_BAD_FS, "compression block overflown");
if (grub_disk_read
(cc->disk,
(cc->comp_table[cc->comp_head][1] -
(cc->comp_table[cc->comp_head][0] - cc->cbuf_vcn)) * cc->spc, 0,
cc->spc << GRUB_NTFS_BLK_SHR, cc->cbuf))
return grub_errno;
cc->cbuf_vcn++;
if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head][0]))
cc->comp_head++;
cc->cbuf_ofs = 0;
return 0;
}
static grub_err_t
decomp_getch (struct grub_ntfs_comp *cc, unsigned char *res)
{
if (cc->cbuf_ofs >= (cc->spc << GRUB_NTFS_BLK_SHR))
{
if (decomp_nextvcn (cc))
return grub_errno;
}
*res = (unsigned char) cc->cbuf[cc->cbuf_ofs++];
return 0;
}
static grub_err_t
decomp_get16 (struct grub_ntfs_comp *cc, grub_uint16_t * res)
{
unsigned char c1 = 0, c2 = 0;
if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2)))
return grub_errno;
*res = ((grub_uint16_t) c2) * 256 + ((grub_uint16_t) c1);
return 0;
}
/* Decompress a block (4096 bytes) */
static grub_err_t
decomp_block (struct grub_ntfs_comp *cc, char *dest)
{
grub_uint16_t flg, cnt;
if (decomp_get16 (cc, &flg))
return grub_errno;
cnt = (flg & 0xFFF) + 1;
if (dest)
{
if (flg & 0x8000)
{
unsigned char tag;
grub_uint32_t bits, copied;
bits = copied = tag = 0;
while (cnt > 0)
{
if (copied > GRUB_NTFS_COM_LEN)
return grub_error (GRUB_ERR_BAD_FS,
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
"compression block too large");
if (!bits)
{
if (decomp_getch (cc, &tag))
return grub_errno;
bits = 8;
cnt--;
if (cnt <= 0)
break;
}
if (tag & 1)
{
grub_uint32_t i, len, delta, code, lmask, dshift;
grub_uint16_t word;
if (decomp_get16 (cc, &word))
return grub_errno;
code = word;
cnt -= 2;
if (!copied)
{
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
grub_error (GRUB_ERR_BAD_FS, "nontext window empty");
return 0;
}
for (i = copied - 1, lmask = 0xFFF, dshift = 12; i >= 0x10;
i >>= 1)
{
lmask >>= 1;
dshift--;
}
delta = code >> dshift;
len = (code & lmask) + 3;
for (i = 0; i < len; i++)
{
dest[copied] = dest[copied - delta - 1];
copied++;
}
}
else
{
unsigned char ch = 0;
if (decomp_getch (cc, &ch))
return grub_errno;
dest[copied++] = ch;
cnt--;
}
tag >>= 1;
bits--;
}
return 0;
}
else
{
if (cnt != GRUB_NTFS_COM_LEN)
return grub_error (GRUB_ERR_BAD_FS,
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
"invalid compression block size");
}
}
while (cnt > 0)
{
int n;
n = (cc->spc << GRUB_NTFS_BLK_SHR) - cc->cbuf_ofs;
if (n > cnt)
n = cnt;
if ((dest) && (n))
{
grub_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n);
dest += n;
}
cnt -= n;
cc->cbuf_ofs += n;
if ((cnt) && (decomp_nextvcn (cc)))
return grub_errno;
}
return 0;
}
static grub_err_t
read_block (struct grub_ntfs_rlst *ctx, char *buf, grub_size_t num)
{
int cpb = GRUB_NTFS_COM_SEC / ctx->comp.spc;
while (num)
{
grub_size_t nn;
if ((ctx->target_vcn & 0xF) == 0)
{
if (ctx->comp.comp_head != ctx->comp.comp_tail
&& !(ctx->flags & GRUB_NTFS_RF_BLNK))
2009-12-24 Carles Pina i Estany <carles@pina.cat> * bus/usb/usbhub.c: Fix capitalization, fullstop and newlines in grub_errno calls. * commands/acpi.c: Likewise. * commands/blocklist.c: Likewise. * commands/efi/loadbios.c: Likewise. * commands/i386/pc/drivemap.c: Likewise. * commands/loadenv.c: Likewise. * commands/memrw.c: Likewise. * commands/password.c: Likewise. * commands/videotest.c: Likewise. * disk/ata.c: Likewise. * disk/ata_pthru.c: Likewise. * disk/dmraid_nvidia.c: Likewise. * disk/ieee1275/nand.c: Likewise. * disk/ieee1275/ofdisk.c: Likewise. * disk/loopback.c: Likewise. * disk/lvm.c: Likewise. * disk/mdraid_linux.c: Likewise. * disk/raid.c: Likewise. * disk/raid6_recover.c: Likewise. * disk/scsi.c: Likewise. * efiemu/main.c: Likewise. * efiemu/mm.c: Likewise. * efiemu/pnvram.c: Likewise. * efiemu/symbols.c: Likewise. * font/font.c: Likewise. * fs/cpio.c: Likewise. * fs/hfsplus.c: Likewise. * fs/iso9660.c: Likewise. * fs/jfs.c: Likewise. * fs/minix.c: Likewise. * fs/ntfs.c: Likewise. * fs/ntfscomp.c: Likewise. * fs/reiserfs.c: Likewise. * fs/ufs.c: Likewise. * fs/xfs.c: Likewise. * gettext/gettext.c: Likewise. * include/grub/auth.h: Likewise. * kern/elf.c: Likewise. * kern/file.c: Likewise. * kern/ieee1275/init.c: Likewise. * kern/ieee1275/mmap.c: Likewise. * kern/ieee1275/openfw.c: Likewise. * kern/powerpc/dl.c: Likewise. * kern/sparc64/dl.c: Likewise. * lib/arg.c: Likewise. * loader/i386/bsd.c: Likewise. * loader/i386/bsdXX.c: Likewise. * loader/i386/efi/linux.c: Likewise. * loader/i386/efi/xnu.c: Likewise. * loader/i386/ieee1275/linux.c: Likewise. * loader/i386/linux.c: Likewise. * loader/i386/multiboot.c: Likewise. * loader/i386/pc/linux.c: Likewise. * loader/i386/pc/multiboot2.c: Likewise. * loader/i386/xnu.c: Likewise. * loader/ieee1275/multiboot2.c: Likewise. * loader/macho.c: Likewise. * loader/machoXX.c: Likewise. * loader/multiboot2.c: Likewise. * loader/multiboot_loader.c: Likewise. * loader/powerpc/ieee1275/linux.c: Likewise. * loader/sparc64/ieee1275/linux.c: Likewise. * loader/xnu.c: Likewise. * loader/xnu_resume.c: Likewise. * mmap/i386/pc/mmap.c: Likewise. * normal/menu_viewer.c: Likewise. * partmap/acorn.c: Likewise. * partmap/amiga.c: Likewise. * partmap/apple.c: Likewise. * script/lexer.c: Likewise. * term/gfxterm.c: Likewise. * term/i386/pc/serial.c: Likewise. * term/i386/pc/vga.c: Likewise. * term/ieee1275/ofconsole.c: Likewise. * term/terminfo.c: Likewise. * video/bitmap.c: Likewise. * video/efi_gop.c: Likewise. * video/efi_uga.c: Likewise. * video/fb/video_fb.c: Likewise. * video/i386/pc/vbe.c: Likewise. * video/readers/tga.c: Likewise. * video/video.c: Likewise.
2009-12-24 22:53:05 +00:00
return grub_error (GRUB_ERR_BAD_FS, "invalid compression block");
ctx->comp.comp_head = ctx->comp.comp_tail = 0;
ctx->comp.cbuf_vcn = ctx->target_vcn;
ctx->comp.cbuf_ofs = (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
if (ctx->target_vcn >= ctx->next_vcn)
{
if (grub_ntfs_read_run_list (ctx))
return grub_errno;
}
while (ctx->target_vcn + 16 > ctx->next_vcn)
{
if (ctx->flags & GRUB_NTFS_RF_BLNK)
break;
ctx->comp.comp_table[ctx->comp.comp_tail][0] = ctx->next_vcn;
ctx->comp.comp_table[ctx->comp.comp_tail][1] =
ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn;
ctx->comp.comp_tail++;
if (grub_ntfs_read_run_list (ctx))
return grub_errno;
}
}
nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) / cpb;
if (nn > num)
nn = num;
num -= nn;
if (ctx->flags & GRUB_NTFS_RF_BLNK)
{
ctx->target_vcn += nn * cpb;
if (ctx->comp.comp_tail == 0)
{
if (buf)
{
grub_memset (buf, 0, nn * GRUB_NTFS_COM_LEN);
buf += nn * GRUB_NTFS_COM_LEN;
}
}
else
{
while (nn)
{
if (decomp_block (&ctx->comp, buf))
return grub_errno;
if (buf)
buf += GRUB_NTFS_COM_LEN;
nn--;
}
}
}
else
{
nn *= cpb;
while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn))
{
grub_disk_addr_t tt;
tt =
ctx->comp.comp_table[ctx->comp.comp_head][0] -
ctx->target_vcn;
if (tt > nn)
tt = nn;
ctx->target_vcn += tt;
if (buf)
{
if (grub_disk_read
(ctx->comp.disk,
(ctx->comp.comp_table[ctx->comp.comp_head][1] -
(ctx->comp.comp_table[ctx->comp.comp_head][0] -
ctx->target_vcn)) * ctx->comp.spc, 0,
tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
return grub_errno;
buf += tt * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
}
nn -= tt;
if (ctx->target_vcn >=
ctx->comp.comp_table[ctx->comp.comp_head][0])
ctx->comp.comp_head++;
}
if (nn)
{
if (buf)
{
if (grub_disk_read
(ctx->comp.disk,
(ctx->target_vcn - ctx->curr_vcn +
ctx->curr_lcn) * ctx->comp.spc, 0,
nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR), buf))
return grub_errno;
buf += nn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
}
ctx->target_vcn += nn;
}
}
}
return 0;
}
static grub_err_t
ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_disk_addr_t ofs,
grub_size_t len, struct grub_ntfs_rlst *ctx, grub_disk_addr_t vcn)
{
grub_err_t ret;
ctx->comp.comp_head = ctx->comp.comp_tail = 0;
ctx->comp.cbuf = grub_malloc ((ctx->comp.spc) << GRUB_NTFS_BLK_SHR);
if (!ctx->comp.cbuf)
return 0;
ret = 0;
//ctx->comp.disk->read_hook = read_hook;
if ((vcn > ctx->target_vcn) &&
(read_block
(ctx, NULL, ((vcn - ctx->target_vcn) * ctx->comp.spc) / GRUB_NTFS_COM_SEC)))
{
ret = grub_errno;
goto quit;
}
if (ofs % GRUB_NTFS_COM_LEN)
{
grub_uint32_t t, n, o;
t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
if (read_block (ctx, at->sbuf, 1))
{
ret = grub_errno;
goto quit;
}
at->save_pos = t;
o = ofs % GRUB_NTFS_COM_LEN;
n = GRUB_NTFS_COM_LEN - o;
if (n > len)
n = len;
grub_memcpy (dest, &at->sbuf[o], n);
if (n == len)
goto quit;
dest += n;
len -= n;
}
if (read_block (ctx, dest, len / GRUB_NTFS_COM_LEN))
{
ret = grub_errno;
goto quit;
}
dest += (len / GRUB_NTFS_COM_LEN) * GRUB_NTFS_COM_LEN;
len = len % GRUB_NTFS_COM_LEN;
if (len)
{
grub_uint32_t t;
t = ctx->target_vcn * (ctx->comp.spc << GRUB_NTFS_BLK_SHR);
if (read_block (ctx, at->sbuf, 1))
{
ret = grub_errno;
goto quit;
}
at->save_pos = t;
grub_memcpy (dest, at->sbuf, len);
}
quit:
//ctx->comp.disk->read_hook = 0;
if (ctx->comp.cbuf)
grub_free (ctx->comp.cbuf);
return ret;
}
GRUB_MOD_INIT (ntfscomp)
{
grub_ntfscomp_func = ntfscomp;
}
GRUB_MOD_FINI (ntfscomp)
{
grub_ntfscomp_func = NULL;
}