2009-08-23 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / fs / ntfscomp.c
blob20c79ac07d876bdff1492f2f8efc94b0322b21c7
1 /* ntfscomp.c - compression support for the NTFS filesystem */
2 /*
3 * Copyright (C) 2007 Free Software Foundation, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/file.h>
20 #include <grub/mm.h>
21 #include <grub/misc.h>
22 #include <grub/disk.h>
23 #include <grub/dl.h>
24 #include <grub/fshelp.h>
25 #include <grub/ntfs.h>
27 static grub_err_t
28 decomp_nextvcn (struct grub_ntfs_comp *cc)
30 if (cc->comp_head >= cc->comp_tail)
31 return grub_error (GRUB_ERR_BAD_FS, "Compression block overflown");
32 if (grub_disk_read
33 (cc->disk,
34 (cc->comp_table[cc->comp_head][1] -
35 (cc->comp_table[cc->comp_head][0] - cc->cbuf_vcn)) * cc->spc, 0,
36 cc->spc << BLK_SHR, cc->cbuf))
37 return grub_errno;
38 cc->cbuf_vcn++;
39 if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head][0]))
40 cc->comp_head++;
41 cc->cbuf_ofs = 0;
42 return 0;
45 static grub_err_t
46 decomp_getch (struct grub_ntfs_comp *cc, unsigned char *res)
48 if (cc->cbuf_ofs >= (cc->spc << BLK_SHR))
50 if (decomp_nextvcn (cc))
51 return grub_errno;
53 *res = (unsigned char) cc->cbuf[cc->cbuf_ofs++];
54 return 0;
57 static grub_err_t
58 decomp_get16 (struct grub_ntfs_comp *cc, grub_uint16_t * res)
60 unsigned char c1 = 0, c2 = 0;
62 if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2)))
63 return grub_errno;
64 *res = ((grub_uint16_t) c2) * 256 + ((grub_uint16_t) c1);
65 return 0;
68 /* Decompress a block (4096 bytes) */
69 static grub_err_t
70 decomp_block (struct grub_ntfs_comp *cc, char *dest)
72 grub_uint16_t flg, cnt;
74 if (decomp_get16 (cc, &flg))
75 return grub_errno;
76 cnt = (flg & 0xFFF) + 1;
78 if (dest)
80 if (flg & 0x8000)
82 unsigned char tag;
83 grub_uint32_t bits, copied;
85 bits = copied = tag = 0;
86 while (cnt > 0)
88 if (copied > COM_LEN)
89 return grub_error (GRUB_ERR_BAD_FS,
90 "Compression block too large");
92 if (!bits)
94 if (decomp_getch (cc, &tag))
95 return grub_errno;
97 bits = 8;
98 cnt--;
99 if (cnt <= 0)
100 break;
102 if (tag & 1)
104 grub_uint32_t i, len, delta, code, lmask, dshift;
105 grub_uint16_t word;
107 if (decomp_get16 (cc, &word))
108 return grub_errno;
110 code = word;
111 cnt -= 2;
113 if (!copied)
115 grub_error (GRUB_ERR_BAD_FS, "Context window empty");
116 return 0;
119 for (i = copied - 1, lmask = 0xFFF, dshift = 12; i >= 0x10;
120 i >>= 1)
122 lmask >>= 1;
123 dshift--;
126 delta = code >> dshift;
127 len = (code & lmask) + 3;
129 for (i = 0; i < len; i++)
131 dest[copied] = dest[copied - delta - 1];
132 copied++;
135 else
137 unsigned char ch = 0;
139 if (decomp_getch (cc, &ch))
140 return grub_errno;
141 dest[copied++] = ch;
142 cnt--;
144 tag >>= 1;
145 bits--;
147 return 0;
149 else
151 if (cnt != COM_LEN)
152 return grub_error (GRUB_ERR_BAD_FS,
153 "Invalid compression block size");
157 while (cnt > 0)
159 int n;
161 n = (cc->spc << BLK_SHR) - cc->cbuf_ofs;
162 if (n > cnt)
163 n = cnt;
164 if ((dest) && (n))
166 grub_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n);
167 dest += n;
169 cnt -= n;
170 cc->cbuf_ofs += n;
171 if ((cnt) && (decomp_nextvcn (cc)))
172 return grub_errno;
174 return 0;
177 static grub_err_t
178 read_block (struct grub_ntfs_rlst *ctx, char *buf, int num)
180 int cpb = COM_SEC / ctx->comp.spc;
182 while (num)
184 int nn;
186 if ((ctx->target_vcn & 0xF) == 0)
189 if (ctx->comp.comp_head != ctx->comp.comp_tail)
190 return grub_error (GRUB_ERR_BAD_FS, "Invalid compression block");
191 ctx->comp.comp_head = ctx->comp.comp_tail = 0;
192 ctx->comp.cbuf_vcn = ctx->target_vcn;
193 ctx->comp.cbuf_ofs = (ctx->comp.spc << BLK_SHR);
194 if (ctx->target_vcn >= ctx->next_vcn)
196 if (grub_ntfs_read_run_list (ctx))
197 return grub_errno;
199 while (ctx->target_vcn + 16 > ctx->next_vcn)
201 if (ctx->flags & RF_BLNK)
202 break;
203 ctx->comp.comp_table[ctx->comp.comp_tail][0] = ctx->next_vcn;
204 ctx->comp.comp_table[ctx->comp.comp_tail][1] =
205 ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn;
206 ctx->comp.comp_tail++;
207 if (grub_ntfs_read_run_list (ctx))
208 return grub_errno;
212 nn = (16 - (ctx->target_vcn & 0xF)) / cpb;
213 if (nn > num)
214 nn = num;
215 num -= nn;
217 if (ctx->flags & RF_BLNK)
219 ctx->target_vcn += nn * cpb;
220 if (ctx->comp.comp_tail == 0)
222 if (buf)
224 grub_memset (buf, 0, nn * COM_LEN);
225 buf += nn * COM_LEN;
228 else
230 while (nn)
232 if (decomp_block (&ctx->comp, buf))
233 return grub_errno;
234 if (buf)
235 buf += COM_LEN;
236 nn--;
240 else
242 nn *= cpb;
243 while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn))
245 int tt;
247 tt =
248 ctx->comp.comp_table[ctx->comp.comp_head][0] -
249 ctx->target_vcn;
250 if (tt > nn)
251 tt = nn;
252 ctx->target_vcn += tt;
253 if (buf)
255 if (grub_disk_read
256 (ctx->comp.disk,
257 (ctx->comp.comp_table[ctx->comp.comp_head][1] -
258 (ctx->comp.comp_table[ctx->comp.comp_head][0] -
259 ctx->target_vcn)) * ctx->comp.spc, 0,
260 tt * (ctx->comp.spc << BLK_SHR), buf))
261 return grub_errno;
262 buf += tt * (ctx->comp.spc << BLK_SHR);
264 nn -= tt;
265 if (ctx->target_vcn >=
266 ctx->comp.comp_table[ctx->comp.comp_head][0])
267 ctx->comp.comp_head++;
269 if (nn)
271 if (buf)
273 if (grub_disk_read
274 (ctx->comp.disk,
275 (ctx->target_vcn - ctx->curr_vcn +
276 ctx->curr_lcn) * ctx->comp.spc, 0,
277 nn * (ctx->comp.spc << BLK_SHR), buf))
278 return grub_errno;
279 buf += nn * (ctx->comp.spc << BLK_SHR);
281 ctx->target_vcn += nn;
285 return 0;
288 static grub_err_t
289 ntfscomp (struct grub_ntfs_attr *at, char *dest, grub_uint32_t ofs,
290 grub_uint32_t len, struct grub_ntfs_rlst *ctx, grub_uint32_t vcn)
292 grub_err_t ret;
294 ctx->comp.comp_head = ctx->comp.comp_tail = 0;
295 ctx->comp.cbuf = grub_malloc ((ctx->comp.spc) << BLK_SHR);
296 if (!ctx->comp.cbuf)
297 return 0;
299 ret = 0;
301 //ctx->comp.disk->read_hook = read_hook;
303 if ((vcn > ctx->target_vcn) &&
304 (read_block
305 (ctx, NULL, ((vcn - ctx->target_vcn) * ctx->comp.spc) / COM_SEC)))
307 ret = grub_errno;
308 goto quit;
311 if (ofs % COM_LEN)
313 grub_uint32_t t, n, o;
315 t = ctx->target_vcn * (ctx->comp.spc << BLK_SHR);
316 if (read_block (ctx, at->sbuf, 1))
318 ret = grub_errno;
319 goto quit;
322 at->save_pos = t;
324 o = ofs % COM_LEN;
325 n = COM_LEN - o;
326 if (n > len)
327 n = len;
328 grub_memcpy (dest, &at->sbuf[o], n);
329 if (n == len)
330 goto quit;
331 dest += n;
332 len -= n;
335 if (read_block (ctx, dest, len / COM_LEN))
337 ret = grub_errno;
338 goto quit;
341 dest += (len / COM_LEN) * COM_LEN;
342 len = len % COM_LEN;
343 if (len)
345 grub_uint32_t t;
347 t = ctx->target_vcn * (ctx->comp.spc << BLK_SHR);
348 if (read_block (ctx, at->sbuf, 1))
350 ret = grub_errno;
351 goto quit;
354 at->save_pos = t;
356 grub_memcpy (dest, at->sbuf, len);
359 quit:
360 //ctx->comp.disk->read_hook = 0;
361 if (ctx->comp.cbuf)
362 grub_free (ctx->comp.cbuf);
363 return ret;
366 GRUB_MOD_INIT (ntfscomp)
368 grub_ntfscomp_func = ntfscomp;
371 GRUB_MOD_FINI (ntfscomp)
373 grub_ntfscomp_func = NULL;