/* 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 . */ #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); static VasEBoot_err_t decomp_nextvcn (struct VasEBoot_ntfs_comp *cc) { if (cc->comp_head >= cc->comp_tail) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "compression block overflow"); if (VasEBoot_disk_read (cc->disk, (cc->comp_table[cc->comp_head].next_lcn - (cc->comp_table[cc->comp_head].next_vcn - cc->cbuf_vcn)) << cc->log_spc, 0, 1 << (cc->log_spc + VAS_EBOOT_NTFS_BLK_SHR), cc->cbuf)) return VasEBoot_errno; cc->cbuf_vcn++; if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head].next_vcn)) cc->comp_head++; cc->cbuf_ofs = 0; return 0; } static VasEBoot_err_t decomp_getch (struct VasEBoot_ntfs_comp *cc, VasEBoot_uint8_t *res) { if (cc->cbuf_ofs >= (1U << (cc->log_spc + VAS_EBOOT_NTFS_BLK_SHR))) { if (decomp_nextvcn (cc)) return VasEBoot_errno; } *res = cc->cbuf[cc->cbuf_ofs++]; return 0; } static VasEBoot_err_t decomp_get16 (struct VasEBoot_ntfs_comp *cc, VasEBoot_uint16_t * res) { VasEBoot_uint8_t c1 = 0, c2 = 0; if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2))) return VasEBoot_errno; *res = ((VasEBoot_uint16_t) c2) * 256 + ((VasEBoot_uint16_t) c1); return 0; } /* Decompress a block (4096 bytes) */ static VasEBoot_err_t decomp_block (struct VasEBoot_ntfs_comp *cc, VasEBoot_uint8_t *dest) { VasEBoot_uint16_t flg, cnt; if (decomp_get16 (cc, &flg)) return VasEBoot_errno; cnt = (flg & 0xFFF) + 1; if (dest) { if (flg & 0x8000) { VasEBoot_uint8_t tag; VasEBoot_uint32_t bits, copied; bits = copied = tag = 0; while (cnt > 0) { if (copied > VAS_EBOOT_NTFS_COM_LEN) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "compression block too large"); if (!bits) { if (decomp_getch (cc, &tag)) return VasEBoot_errno; bits = 8; cnt--; if (cnt <= 0) break; } if (tag & 1) { VasEBoot_uint32_t i, len, delta, code, lmask, dshift; VasEBoot_uint16_t word = 0; if (decomp_get16 (cc, &word)) return VasEBoot_errno; code = word; cnt -= 2; if (!copied) { VasEBoot_error (VAS_EBOOT_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 { VasEBoot_uint8_t ch = 0; if (decomp_getch (cc, &ch)) return VasEBoot_errno; dest[copied++] = ch; cnt--; } tag >>= 1; bits--; } return 0; } else { if (cnt != VAS_EBOOT_NTFS_COM_LEN) return VasEBoot_error (VAS_EBOOT_ERR_BAD_FS, "invalid compression block size"); } } while (cnt > 0) { int n; n = (1 << (cc->log_spc + VAS_EBOOT_NTFS_BLK_SHR)) - cc->cbuf_ofs; if (n > cnt) n = cnt; if ((dest) && (n)) { VasEBoot_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n); dest += n; } cnt -= n; cc->cbuf_ofs += n; if ((cnt) && (decomp_nextvcn (cc))) return VasEBoot_errno; } return 0; } static VasEBoot_err_t read_block (struct VasEBoot_ntfs_rlst *ctx, VasEBoot_uint8_t *buf, VasEBoot_size_t num) { int log_cpb = VAS_EBOOT_NTFS_LOG_COM_SEC - ctx->comp.log_spc; while (num) { VasEBoot_size_t nn; if ((ctx->target_vcn & 0xF) == 0) { if (ctx->comp.comp_head != ctx->comp.comp_tail && !(ctx->flags & VAS_EBOOT_NTFS_RF_BLNK)) return VasEBoot_error (VAS_EBOOT_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 = (1 << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR)); if (ctx->target_vcn >= ctx->next_vcn) { if (VasEBoot_ntfs_read_run_list (ctx)) return VasEBoot_errno; } while (ctx->target_vcn + 16 > ctx->next_vcn) { if (ctx->flags & VAS_EBOOT_NTFS_RF_BLNK) break; ctx->comp.comp_table[ctx->comp.comp_tail].next_vcn = ctx->next_vcn; ctx->comp.comp_table[ctx->comp.comp_tail].next_lcn = ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn; ctx->comp.comp_tail++; if (VasEBoot_ntfs_read_run_list (ctx)) return VasEBoot_errno; } } nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) >> log_cpb; if (nn > num) nn = num; num -= nn; if (ctx->flags & VAS_EBOOT_NTFS_RF_BLNK) { ctx->target_vcn += nn << log_cpb; if (ctx->comp.comp_tail == 0) { if (buf) { VasEBoot_memset (buf, 0, nn * VAS_EBOOT_NTFS_COM_LEN); buf += nn * VAS_EBOOT_NTFS_COM_LEN; if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, nn * VAS_EBOOT_NTFS_COM_LEN, NULL, ctx->file); } } else { while (nn) { if (decomp_block (&ctx->comp, buf)) return VasEBoot_errno; if (buf) buf += VAS_EBOOT_NTFS_COM_LEN; if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, VAS_EBOOT_NTFS_COM_LEN, NULL, ctx->file); nn--; } } } else { nn <<= log_cpb; while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn)) { VasEBoot_disk_addr_t tt; tt = ctx->comp.comp_table[ctx->comp.comp_head].next_vcn - ctx->target_vcn; if (tt > nn) tt = nn; ctx->target_vcn += tt; if (buf) { if (VasEBoot_disk_read (ctx->comp.disk, (ctx->comp.comp_table[ctx->comp.comp_head].next_lcn - (ctx->comp.comp_table[ctx->comp.comp_head].next_vcn - ctx->target_vcn)) << ctx->comp.log_spc, 0, tt << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR), buf)) return VasEBoot_errno; if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, tt << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR), NULL, ctx->file); buf += tt << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR); } nn -= tt; if (ctx->target_vcn >= ctx->comp.comp_table[ctx->comp.comp_head].next_vcn) ctx->comp.comp_head++; } if (nn) { if (buf) { if (VasEBoot_disk_read (ctx->comp.disk, (ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc, 0, nn << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR), buf)) return VasEBoot_errno; buf += nn << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR); if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, nn << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR), NULL, ctx->file); } ctx->target_vcn += nn; } } } return 0; } static VasEBoot_err_t ntfscomp (VasEBoot_uint8_t *dest, VasEBoot_disk_addr_t ofs, VasEBoot_size_t len, struct VasEBoot_ntfs_rlst *ctx) { VasEBoot_err_t ret; VasEBoot_disk_addr_t vcn; int log_sz; if (ctx->attr->sbuf) { if ((ofs & (~(VAS_EBOOT_NTFS_COM_LEN - 1))) == ctx->attr->save_pos) { VasEBoot_disk_addr_t n; n = VAS_EBOOT_NTFS_COM_LEN - (ofs - ctx->attr->save_pos); if (n > len) n = len; VasEBoot_memcpy (dest, ctx->attr->sbuf + ofs - ctx->attr->save_pos, n); if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, n, NULL, ctx->file); if (n == len) return 0; dest += n; len -= n; ofs += n; } } else { ctx->attr->sbuf = VasEBoot_malloc (VAS_EBOOT_NTFS_COM_LEN); if (ctx->attr->sbuf == NULL) return VasEBoot_errno; ctx->attr->save_pos = 1; } vcn = ctx->target_vcn = (ofs >> VAS_EBOOT_NTFS_COM_LOG_LEN) * (VAS_EBOOT_NTFS_COM_SEC >> ctx->comp.log_spc); ctx->target_vcn &= ~0xFULL; while (ctx->next_vcn <= ctx->target_vcn) { if (VasEBoot_ntfs_read_run_list (ctx)) return VasEBoot_errno; } ctx->comp.comp_head = ctx->comp.comp_tail = 0; if (VasEBoot_add (ctx->comp.log_spc, VAS_EBOOT_NTFS_BLK_SHR, &log_sz)) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("compression buffer size overflow")); return 0; } ctx->comp.cbuf = VasEBoot_malloc (1 << log_sz); if (!ctx->comp.cbuf) return 0; ret = 0; //ctx->comp.disk->read_hook = read_hook; //ctx->comp.disk->read_hook_data = read_hook_data; if ((vcn > ctx->target_vcn) && (read_block (ctx, NULL, (vcn - ctx->target_vcn) >> (VAS_EBOOT_NTFS_LOG_COM_SEC - ctx->comp.log_spc)))) { ret = VasEBoot_errno; goto quit; } if (ofs % VAS_EBOOT_NTFS_COM_LEN) { VasEBoot_uint32_t t, n, o; void *file = ctx->file; ctx->file = 0; t = ctx->target_vcn << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR); if (read_block (ctx, ctx->attr->sbuf, 1)) { ret = VasEBoot_errno; goto quit; } ctx->file = file; ctx->attr->save_pos = t; o = ofs % VAS_EBOOT_NTFS_COM_LEN; n = VAS_EBOOT_NTFS_COM_LEN - o; if (n > len) n = len; VasEBoot_memcpy (dest, &ctx->attr->sbuf[o], n); if (VasEBoot_file_progress_hook && ctx->file) VasEBoot_file_progress_hook (0, 0, n, NULL, ctx->file); if (n == len) goto quit; dest += n; len -= n; } if (read_block (ctx, dest, len / VAS_EBOOT_NTFS_COM_LEN)) { ret = VasEBoot_errno; goto quit; } dest += (len / VAS_EBOOT_NTFS_COM_LEN) * VAS_EBOOT_NTFS_COM_LEN; len = len % VAS_EBOOT_NTFS_COM_LEN; if (len) { VasEBoot_uint32_t t; void *file = ctx->file; ctx->file = 0; t = ctx->target_vcn << (ctx->comp.log_spc + VAS_EBOOT_NTFS_BLK_SHR); if (read_block (ctx, ctx->attr->sbuf, 1)) { ret = VasEBoot_errno; goto quit; } ctx->attr->save_pos = t; VasEBoot_memcpy (dest, ctx->attr->sbuf, len); if (VasEBoot_file_progress_hook && file) VasEBoot_file_progress_hook (0, 0, len, NULL, file); } quit: //ctx->comp.disk->read_hook = 0; if (ctx->comp.cbuf) VasEBoot_free (ctx->comp.cbuf); return ret; } VAS_EBOOT_MOD_INIT (ntfscomp) { VasEBoot_ntfscomp_func = ntfscomp; } VAS_EBOOT_MOD_FINI (ntfscomp) { VasEBoot_ntfscomp_func = NULL; }