hfspluscomp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2012 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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. * GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* HFS+ is documented at http://developer.apple.com/technotes/tn/tn1150.html */
  19. #include <grub/hfsplus.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/deflate.h>
  24. #include <grub/file.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. /* big-endian. */
  27. struct grub_hfsplus_compress_header1
  28. {
  29. grub_uint32_t header_size;
  30. grub_uint32_t end_descriptor_offset;
  31. grub_uint32_t total_compressed_size_including_seek_blocks_and_header2;
  32. grub_uint32_t value_0x32;
  33. grub_uint8_t unused[0xf0];
  34. } GRUB_PACKED;
  35. /* big-endian. */
  36. struct grub_hfsplus_compress_header2
  37. {
  38. grub_uint32_t total_compressed_size_including_seek_blocks;
  39. } GRUB_PACKED;
  40. /* little-endian. */
  41. struct grub_hfsplus_compress_header3
  42. {
  43. grub_uint32_t num_chunks;
  44. } GRUB_PACKED;
  45. /* little-endian. */
  46. struct grub_hfsplus_compress_block_descriptor
  47. {
  48. grub_uint32_t offset;
  49. grub_uint32_t size;
  50. };
  51. struct grub_hfsplus_compress_end_descriptor
  52. {
  53. grub_uint8_t always_the_same[50];
  54. } GRUB_PACKED;
  55. struct grub_hfsplus_attr_header
  56. {
  57. grub_uint8_t unused[3];
  58. grub_uint8_t type;
  59. grub_uint32_t unknown[1];
  60. grub_uint64_t size;
  61. } GRUB_PACKED;
  62. struct grub_hfsplus_compress_attr
  63. {
  64. grub_uint32_t magic;
  65. grub_uint32_t type;
  66. grub_uint32_t uncompressed_inline_size;
  67. grub_uint32_t always_0;
  68. } GRUB_PACKED;
  69. enum
  70. {
  71. HFSPLUS_COMPRESSION_INLINE = 3,
  72. HFSPLUS_COMPRESSION_RESOURCE = 4
  73. };
  74. static int
  75. grub_hfsplus_cmp_attrkey (struct grub_hfsplus_key *keya,
  76. struct grub_hfsplus_key_internal *keyb)
  77. {
  78. struct grub_hfsplus_attrkey *attrkey_a = &keya->attrkey;
  79. struct grub_hfsplus_attrkey_internal *attrkey_b = &keyb->attrkey;
  80. grub_uint32_t aparent = grub_be_to_cpu32 (attrkey_a->cnid);
  81. grub_size_t len;
  82. int diff;
  83. if (aparent > attrkey_b->cnid)
  84. return 1;
  85. if (aparent < attrkey_b->cnid)
  86. return -1;
  87. len = grub_be_to_cpu16 (attrkey_a->namelen);
  88. if (len > attrkey_b->namelen)
  89. len = attrkey_b->namelen;
  90. /* Since it's big-endian memcmp gives the same result as manually comparing
  91. uint16_t but may be faster. */
  92. diff = grub_memcmp (attrkey_a->name, attrkey_b->name,
  93. len * sizeof (attrkey_a->name[0]));
  94. if (diff == 0)
  95. diff = grub_be_to_cpu16 (attrkey_a->namelen) - attrkey_b->namelen;
  96. return diff;
  97. }
  98. #define HFSPLUS_COMPRESS_BLOCK_SIZE 65536
  99. static grub_ssize_t
  100. hfsplus_read_compressed_real (struct grub_hfsplus_file *node,
  101. grub_off_t pos, grub_size_t len, char *buf)
  102. {
  103. char *tmp_buf = 0;
  104. grub_size_t len0 = len;
  105. if (node->compressed == 1)
  106. {
  107. grub_memcpy (buf, node->cbuf + pos, len);
  108. if (grub_file_progress_hook && node->file)
  109. grub_file_progress_hook (0, 0, len, NULL, node->file);
  110. return len;
  111. }
  112. while (len)
  113. {
  114. grub_uint32_t block = pos / HFSPLUS_COMPRESS_BLOCK_SIZE;
  115. grub_size_t curlen = HFSPLUS_COMPRESS_BLOCK_SIZE
  116. - (pos % HFSPLUS_COMPRESS_BLOCK_SIZE);
  117. if (curlen > len)
  118. curlen = len;
  119. if (node->cbuf_block != block)
  120. {
  121. grub_uint32_t sz = grub_le_to_cpu32 (node->compress_index[block].size);
  122. grub_size_t ts;
  123. if (!tmp_buf)
  124. tmp_buf = grub_malloc (HFSPLUS_COMPRESS_BLOCK_SIZE);
  125. if (!tmp_buf)
  126. return -1;
  127. if (grub_hfsplus_read_file (node, 0, 0,
  128. grub_le_to_cpu32 (node->compress_index[block].start) + 0x104,
  129. sz, tmp_buf)
  130. != (grub_ssize_t) sz)
  131. {
  132. grub_free (tmp_buf);
  133. return -1;
  134. }
  135. ts = HFSPLUS_COMPRESS_BLOCK_SIZE;
  136. if (ts > node->size - (pos & ~(HFSPLUS_COMPRESS_BLOCK_SIZE)))
  137. ts = node->size - (pos & ~(HFSPLUS_COMPRESS_BLOCK_SIZE));
  138. if (grub_zlib_decompress (tmp_buf, sz, 0,
  139. node->cbuf, ts) != (grub_ssize_t) ts)
  140. {
  141. if (!grub_errno)
  142. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  143. "premature end of compressed");
  144. grub_free (tmp_buf);
  145. return -1;
  146. }
  147. node->cbuf_block = block;
  148. }
  149. grub_memcpy (buf, node->cbuf + (pos % HFSPLUS_COMPRESS_BLOCK_SIZE),
  150. curlen);
  151. if (grub_file_progress_hook && node->file)
  152. grub_file_progress_hook (0, 0, curlen, NULL, node->file);
  153. buf += curlen;
  154. pos += curlen;
  155. len -= curlen;
  156. }
  157. grub_free (tmp_buf);
  158. return len0;
  159. }
  160. static grub_err_t
  161. hfsplus_open_compressed_real (struct grub_hfsplus_file *node)
  162. {
  163. grub_err_t err;
  164. struct grub_hfsplus_btnode *attr_node;
  165. grub_off_t attr_off;
  166. struct grub_hfsplus_key_internal key;
  167. struct grub_hfsplus_attr_header *attr_head;
  168. struct grub_hfsplus_compress_attr *cmp_head;
  169. #define c grub_cpu_to_be16_compile_time
  170. const grub_uint16_t compress_attr_name[] =
  171. {
  172. c('c'), c('o'), c('m'), c('.'), c('a'), c('p'), c('p'), c('l'), c('e'),
  173. c('.'), c('d'), c('e'), c('c'), c('m'), c('p'), c('f'), c('s') };
  174. #undef c
  175. if (node->size)
  176. return 0;
  177. key.attrkey.cnid = node->fileid;
  178. key.attrkey.namelen = sizeof (compress_attr_name) / sizeof (compress_attr_name[0]);
  179. key.attrkey.name = compress_attr_name;
  180. err = grub_hfsplus_btree_search (&node->data->attr_tree, &key,
  181. grub_hfsplus_cmp_attrkey,
  182. &attr_node, &attr_off);
  183. if (err || !attr_node)
  184. {
  185. grub_errno = 0;
  186. return 0;
  187. }
  188. attr_head = (struct grub_hfsplus_attr_header *)
  189. ((char *) grub_hfsplus_btree_recptr (&node->data->attr_tree,
  190. attr_node, attr_off)
  191. + sizeof (struct grub_hfsplus_attrkey) + sizeof (compress_attr_name));
  192. if (attr_head->type != 0x10
  193. || !(attr_head->size & grub_cpu_to_be64_compile_time(~0xfULL)))
  194. {
  195. grub_free (attr_node);
  196. return 0;
  197. }
  198. cmp_head = (struct grub_hfsplus_compress_attr *) (attr_head + 1);
  199. if (cmp_head->magic != grub_cpu_to_be32_compile_time (0x66706d63))
  200. {
  201. grub_free (attr_node);
  202. return 0;
  203. }
  204. node->size = grub_le_to_cpu32 (cmp_head->uncompressed_inline_size);
  205. if (cmp_head->type == grub_cpu_to_le32_compile_time (HFSPLUS_COMPRESSION_RESOURCE))
  206. {
  207. grub_uint32_t index_size;
  208. node->compressed = 2;
  209. if (grub_hfsplus_read_file (node, 0, 0,
  210. 0x104, sizeof (index_size),
  211. (char *) &index_size)
  212. != 4)
  213. {
  214. node->compressed = 0;
  215. grub_free (attr_node);
  216. grub_errno = 0;
  217. return 0;
  218. }
  219. node->compress_index_size = grub_le_to_cpu32 (index_size);
  220. node->compress_index = grub_malloc (node->compress_index_size
  221. * sizeof (node->compress_index[0]));
  222. if (!node->compress_index)
  223. {
  224. node->compressed = 0;
  225. grub_free (attr_node);
  226. return grub_errno;
  227. }
  228. if (grub_hfsplus_read_file (node, 0, 0,
  229. 0x104 + sizeof (index_size),
  230. node->compress_index_size
  231. * sizeof (node->compress_index[0]),
  232. (char *) node->compress_index)
  233. != (grub_ssize_t) (node->compress_index_size
  234. * sizeof (node->compress_index[0])))
  235. {
  236. node->compressed = 0;
  237. grub_free (attr_node);
  238. grub_free (node->compress_index);
  239. grub_errno = 0;
  240. return 0;
  241. }
  242. node->cbuf_block = -1;
  243. node->cbuf = grub_malloc (HFSPLUS_COMPRESS_BLOCK_SIZE);
  244. grub_free (attr_node);
  245. if (!node->cbuf)
  246. {
  247. node->compressed = 0;
  248. grub_free (node->compress_index);
  249. return grub_errno;
  250. }
  251. return 0;
  252. }
  253. if (cmp_head->type != HFSPLUS_COMPRESSION_INLINE)
  254. {
  255. grub_free (attr_node);
  256. return 0;
  257. }
  258. node->cbuf = grub_malloc (node->size);
  259. if (!node->cbuf)
  260. return grub_errno;
  261. if (grub_zlib_decompress ((char *) (cmp_head + 1),
  262. grub_cpu_to_be64 (attr_head->size)
  263. - sizeof (*cmp_head), 0,
  264. node->cbuf, node->size)
  265. != (grub_ssize_t) node->size)
  266. {
  267. if (!grub_errno)
  268. grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
  269. "premature end of compressed");
  270. return grub_errno;
  271. }
  272. node->compressed = 1;
  273. return 0;
  274. }
  275. GRUB_MOD_INIT(hfspluscomp)
  276. {
  277. grub_hfsplus_open_compressed = hfsplus_open_compressed_real;
  278. grub_hfsplus_read_compressed = hfsplus_read_compressed_real;
  279. }
  280. GRUB_MOD_FINI(hfspluscomp)
  281. {
  282. grub_hfsplus_open_compressed = 0;
  283. grub_hfsplus_read_compressed = 0;
  284. }