ntfscomp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* ntfscomp.c - compression support for the NTFS filesystem */
  2. /*
  3. * Copyright (C) 2007 Free Software Foundation, Inc.
  4. *
  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.
  9. *
  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.
  14. *
  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/>.
  17. */
  18. #include <grub/file.h>
  19. #include <grub/mm.h>
  20. #include <grub/misc.h>
  21. #include <grub/disk.h>
  22. #include <grub/dl.h>
  23. #include <grub/ntfs.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. static grub_err_t
  26. decomp_nextvcn (struct grub_ntfs_comp *cc)
  27. {
  28. if (cc->comp_head >= cc->comp_tail)
  29. return grub_error (GRUB_ERR_BAD_FS, "compression block overflown");
  30. if (grub_disk_read
  31. (cc->disk,
  32. (cc->comp_table[cc->comp_head].next_lcn -
  33. (cc->comp_table[cc->comp_head].next_vcn - cc->cbuf_vcn)) << cc->log_spc,
  34. 0,
  35. 1 << (cc->log_spc + GRUB_NTFS_BLK_SHR), cc->cbuf))
  36. return grub_errno;
  37. cc->cbuf_vcn++;
  38. if ((cc->cbuf_vcn >= cc->comp_table[cc->comp_head].next_vcn))
  39. cc->comp_head++;
  40. cc->cbuf_ofs = 0;
  41. return 0;
  42. }
  43. static grub_err_t
  44. decomp_getch (struct grub_ntfs_comp *cc, grub_uint8_t *res)
  45. {
  46. if (cc->cbuf_ofs >= (1U << (cc->log_spc + GRUB_NTFS_BLK_SHR)))
  47. {
  48. if (decomp_nextvcn (cc))
  49. return grub_errno;
  50. }
  51. *res = cc->cbuf[cc->cbuf_ofs++];
  52. return 0;
  53. }
  54. static grub_err_t
  55. decomp_get16 (struct grub_ntfs_comp *cc, grub_uint16_t * res)
  56. {
  57. grub_uint8_t c1 = 0, c2 = 0;
  58. if ((decomp_getch (cc, &c1)) || (decomp_getch (cc, &c2)))
  59. return grub_errno;
  60. *res = ((grub_uint16_t) c2) * 256 + ((grub_uint16_t) c1);
  61. return 0;
  62. }
  63. /* Decompress a block (4096 bytes) */
  64. static grub_err_t
  65. decomp_block (struct grub_ntfs_comp *cc, grub_uint8_t *dest)
  66. {
  67. grub_uint16_t flg, cnt;
  68. if (decomp_get16 (cc, &flg))
  69. return grub_errno;
  70. cnt = (flg & 0xFFF) + 1;
  71. if (dest)
  72. {
  73. if (flg & 0x8000)
  74. {
  75. grub_uint8_t tag;
  76. grub_uint32_t bits, copied;
  77. bits = copied = tag = 0;
  78. while (cnt > 0)
  79. {
  80. if (copied > GRUB_NTFS_COM_LEN)
  81. return grub_error (GRUB_ERR_BAD_FS,
  82. "compression block too large");
  83. if (!bits)
  84. {
  85. if (decomp_getch (cc, &tag))
  86. return grub_errno;
  87. bits = 8;
  88. cnt--;
  89. if (cnt <= 0)
  90. break;
  91. }
  92. if (tag & 1)
  93. {
  94. grub_uint32_t i, len, delta, code, lmask, dshift;
  95. grub_uint16_t word = 0;
  96. if (decomp_get16 (cc, &word))
  97. return grub_errno;
  98. code = word;
  99. cnt -= 2;
  100. if (!copied)
  101. {
  102. grub_error (GRUB_ERR_BAD_FS, "nontext window empty");
  103. return 0;
  104. }
  105. for (i = copied - 1, lmask = 0xFFF, dshift = 12; i >= 0x10;
  106. i >>= 1)
  107. {
  108. lmask >>= 1;
  109. dshift--;
  110. }
  111. delta = code >> dshift;
  112. len = (code & lmask) + 3;
  113. for (i = 0; i < len; i++)
  114. {
  115. dest[copied] = dest[copied - delta - 1];
  116. copied++;
  117. }
  118. }
  119. else
  120. {
  121. grub_uint8_t ch = 0;
  122. if (decomp_getch (cc, &ch))
  123. return grub_errno;
  124. dest[copied++] = ch;
  125. cnt--;
  126. }
  127. tag >>= 1;
  128. bits--;
  129. }
  130. return 0;
  131. }
  132. else
  133. {
  134. if (cnt != GRUB_NTFS_COM_LEN)
  135. return grub_error (GRUB_ERR_BAD_FS,
  136. "invalid compression block size");
  137. }
  138. }
  139. while (cnt > 0)
  140. {
  141. int n;
  142. n = (1 << (cc->log_spc + GRUB_NTFS_BLK_SHR)) - cc->cbuf_ofs;
  143. if (n > cnt)
  144. n = cnt;
  145. if ((dest) && (n))
  146. {
  147. grub_memcpy (dest, &cc->cbuf[cc->cbuf_ofs], n);
  148. dest += n;
  149. }
  150. cnt -= n;
  151. cc->cbuf_ofs += n;
  152. if ((cnt) && (decomp_nextvcn (cc)))
  153. return grub_errno;
  154. }
  155. return 0;
  156. }
  157. static grub_err_t
  158. read_block (struct grub_ntfs_rlst *ctx, grub_uint8_t *buf, grub_size_t num)
  159. {
  160. int log_cpb = GRUB_NTFS_LOG_COM_SEC - ctx->comp.log_spc;
  161. while (num)
  162. {
  163. grub_size_t nn;
  164. if ((ctx->target_vcn & 0xF) == 0)
  165. {
  166. if (ctx->comp.comp_head != ctx->comp.comp_tail
  167. && !(ctx->flags & GRUB_NTFS_RF_BLNK))
  168. return grub_error (GRUB_ERR_BAD_FS, "invalid compression block");
  169. ctx->comp.comp_head = ctx->comp.comp_tail = 0;
  170. ctx->comp.cbuf_vcn = ctx->target_vcn;
  171. ctx->comp.cbuf_ofs = (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR));
  172. if (ctx->target_vcn >= ctx->next_vcn)
  173. {
  174. if (grub_ntfs_read_run_list (ctx))
  175. return grub_errno;
  176. }
  177. while (ctx->target_vcn + 16 > ctx->next_vcn)
  178. {
  179. if (ctx->flags & GRUB_NTFS_RF_BLNK)
  180. break;
  181. ctx->comp.comp_table[ctx->comp.comp_tail].next_vcn = ctx->next_vcn;
  182. ctx->comp.comp_table[ctx->comp.comp_tail].next_lcn =
  183. ctx->curr_lcn + ctx->next_vcn - ctx->curr_vcn;
  184. ctx->comp.comp_tail++;
  185. if (grub_ntfs_read_run_list (ctx))
  186. return grub_errno;
  187. }
  188. }
  189. nn = (16 - (unsigned) (ctx->target_vcn & 0xF)) >> log_cpb;
  190. if (nn > num)
  191. nn = num;
  192. num -= nn;
  193. if (ctx->flags & GRUB_NTFS_RF_BLNK)
  194. {
  195. ctx->target_vcn += nn << log_cpb;
  196. if (ctx->comp.comp_tail == 0)
  197. {
  198. if (buf)
  199. {
  200. grub_memset (buf, 0, nn * GRUB_NTFS_COM_LEN);
  201. buf += nn * GRUB_NTFS_COM_LEN;
  202. if (grub_file_progress_hook && ctx->file)
  203. grub_file_progress_hook (0, 0, nn * GRUB_NTFS_COM_LEN, NULL,
  204. ctx->file);
  205. }
  206. }
  207. else
  208. {
  209. while (nn)
  210. {
  211. if (decomp_block (&ctx->comp, buf))
  212. return grub_errno;
  213. if (buf)
  214. buf += GRUB_NTFS_COM_LEN;
  215. if (grub_file_progress_hook && ctx->file)
  216. grub_file_progress_hook (0, 0, GRUB_NTFS_COM_LEN, NULL,
  217. ctx->file);
  218. nn--;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. nn <<= log_cpb;
  225. while ((ctx->comp.comp_head < ctx->comp.comp_tail) && (nn))
  226. {
  227. grub_disk_addr_t tt;
  228. tt =
  229. ctx->comp.comp_table[ctx->comp.comp_head].next_vcn -
  230. ctx->target_vcn;
  231. if (tt > nn)
  232. tt = nn;
  233. ctx->target_vcn += tt;
  234. if (buf)
  235. {
  236. if (grub_disk_read
  237. (ctx->comp.disk,
  238. (ctx->comp.comp_table[ctx->comp.comp_head].next_lcn -
  239. (ctx->comp.comp_table[ctx->comp.comp_head].next_vcn -
  240. ctx->target_vcn)) << ctx->comp.log_spc, 0,
  241. tt << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR), buf))
  242. return grub_errno;
  243. if (grub_file_progress_hook && ctx->file)
  244. grub_file_progress_hook (0, 0,
  245. tt << (ctx->comp.log_spc
  246. + GRUB_NTFS_BLK_SHR), NULL,
  247. ctx->file);
  248. buf += tt << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
  249. }
  250. nn -= tt;
  251. if (ctx->target_vcn >=
  252. ctx->comp.comp_table[ctx->comp.comp_head].next_vcn)
  253. ctx->comp.comp_head++;
  254. }
  255. if (nn)
  256. {
  257. if (buf)
  258. {
  259. if (grub_disk_read
  260. (ctx->comp.disk,
  261. (ctx->target_vcn - ctx->curr_vcn +
  262. ctx->curr_lcn) << ctx->comp.log_spc, 0,
  263. nn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR), buf))
  264. return grub_errno;
  265. buf += nn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
  266. if (grub_file_progress_hook && ctx->file)
  267. grub_file_progress_hook (0, 0,
  268. nn << (ctx->comp.log_spc
  269. + GRUB_NTFS_BLK_SHR), NULL,
  270. ctx->file);
  271. }
  272. ctx->target_vcn += nn;
  273. }
  274. }
  275. }
  276. return 0;
  277. }
  278. static grub_err_t
  279. ntfscomp (grub_uint8_t *dest, grub_disk_addr_t ofs,
  280. grub_size_t len, struct grub_ntfs_rlst *ctx)
  281. {
  282. grub_err_t ret;
  283. grub_disk_addr_t vcn;
  284. if (ctx->attr->sbuf)
  285. {
  286. if ((ofs & (~(GRUB_NTFS_COM_LEN - 1))) == ctx->attr->save_pos)
  287. {
  288. grub_disk_addr_t n;
  289. n = GRUB_NTFS_COM_LEN - (ofs - ctx->attr->save_pos);
  290. if (n > len)
  291. n = len;
  292. grub_memcpy (dest, ctx->attr->sbuf + ofs - ctx->attr->save_pos, n);
  293. if (grub_file_progress_hook && ctx->file)
  294. grub_file_progress_hook (0, 0, n, NULL, ctx->file);
  295. if (n == len)
  296. return 0;
  297. dest += n;
  298. len -= n;
  299. ofs += n;
  300. }
  301. }
  302. else
  303. {
  304. ctx->attr->sbuf = grub_malloc (GRUB_NTFS_COM_LEN);
  305. if (ctx->attr->sbuf == NULL)
  306. return grub_errno;
  307. ctx->attr->save_pos = 1;
  308. }
  309. vcn = ctx->target_vcn = (ofs >> GRUB_NTFS_COM_LOG_LEN) * (GRUB_NTFS_COM_SEC >> ctx->comp.log_spc);
  310. ctx->target_vcn &= ~0xFULL;
  311. while (ctx->next_vcn <= ctx->target_vcn)
  312. {
  313. if (grub_ntfs_read_run_list (ctx))
  314. return grub_errno;
  315. }
  316. ctx->comp.comp_head = ctx->comp.comp_tail = 0;
  317. ctx->comp.cbuf = grub_malloc (1 << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR));
  318. if (!ctx->comp.cbuf)
  319. return 0;
  320. ret = 0;
  321. //ctx->comp.disk->read_hook = read_hook;
  322. //ctx->comp.disk->read_hook_data = read_hook_data;
  323. if ((vcn > ctx->target_vcn) &&
  324. (read_block
  325. (ctx, NULL, (vcn - ctx->target_vcn) >> (GRUB_NTFS_LOG_COM_SEC - ctx->comp.log_spc))))
  326. {
  327. ret = grub_errno;
  328. goto quit;
  329. }
  330. if (ofs % GRUB_NTFS_COM_LEN)
  331. {
  332. grub_uint32_t t, n, o;
  333. void *file = ctx->file;
  334. ctx->file = 0;
  335. t = ctx->target_vcn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
  336. if (read_block (ctx, ctx->attr->sbuf, 1))
  337. {
  338. ret = grub_errno;
  339. goto quit;
  340. }
  341. ctx->file = file;
  342. ctx->attr->save_pos = t;
  343. o = ofs % GRUB_NTFS_COM_LEN;
  344. n = GRUB_NTFS_COM_LEN - o;
  345. if (n > len)
  346. n = len;
  347. grub_memcpy (dest, &ctx->attr->sbuf[o], n);
  348. if (grub_file_progress_hook && ctx->file)
  349. grub_file_progress_hook (0, 0, n, NULL, ctx->file);
  350. if (n == len)
  351. goto quit;
  352. dest += n;
  353. len -= n;
  354. }
  355. if (read_block (ctx, dest, len / GRUB_NTFS_COM_LEN))
  356. {
  357. ret = grub_errno;
  358. goto quit;
  359. }
  360. dest += (len / GRUB_NTFS_COM_LEN) * GRUB_NTFS_COM_LEN;
  361. len = len % GRUB_NTFS_COM_LEN;
  362. if (len)
  363. {
  364. grub_uint32_t t;
  365. void *file = ctx->file;
  366. ctx->file = 0;
  367. t = ctx->target_vcn << (ctx->comp.log_spc + GRUB_NTFS_BLK_SHR);
  368. if (read_block (ctx, ctx->attr->sbuf, 1))
  369. {
  370. ret = grub_errno;
  371. goto quit;
  372. }
  373. ctx->attr->save_pos = t;
  374. grub_memcpy (dest, ctx->attr->sbuf, len);
  375. if (grub_file_progress_hook && file)
  376. grub_file_progress_hook (0, 0, len, NULL, file);
  377. }
  378. quit:
  379. //ctx->comp.disk->read_hook = 0;
  380. if (ctx->comp.cbuf)
  381. grub_free (ctx->comp.cbuf);
  382. return ret;
  383. }
  384. GRUB_MOD_INIT (ntfscomp)
  385. {
  386. grub_ntfscomp_func = ntfscomp;
  387. }
  388. GRUB_MOD_FINI (ntfscomp)
  389. {
  390. grub_ntfscomp_func = NULL;
  391. }