zbyszek / rpms / grub2

Forked from rpms/grub2 5 years ago
Clone
31004e6
From 14aa7e045965ce95779e17e20503191f8f6bc160 Mon Sep 17 00:00:00 2001
31004e6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
31004e6
Date: Tue, 7 May 2013 15:46:17 +0200
f74b50e
Subject: [PATCH 441/482] 	Compressed HFS+ support.
31004e6
31004e6
---
31004e6
 ChangeLog                   |   4 +
31004e6
 Makefile.util.def           |   1 +
31004e6
 grub-core/Makefile.core.def |   5 +
31004e6
 grub-core/fs/hfsplus.c      | 272 +++++++++++++--------------------------
31004e6
 grub-core/fs/hfspluscomp.c  | 302 ++++++++++++++++++++++++++++++++++++++++++++
31004e6
 include/grub/hfsplus.h      | 217 +++++++++++++++++++++++++++++++
31004e6
 6 files changed, 618 insertions(+), 183 deletions(-)
31004e6
 create mode 100644 grub-core/fs/hfspluscomp.c
31004e6
 create mode 100644 include/grub/hfsplus.h
31004e6
31004e6
diff --git a/ChangeLog b/ChangeLog
31004e6
index 8b77759..0430249 100644
31004e6
--- a/ChangeLog
31004e6
+++ b/ChangeLog
31004e6
@@ -1,5 +1,9 @@
31004e6
 2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
31004e6
 
31004e6
+	Compressed HFS+ support.
31004e6
+
31004e6
+2013-05-07  Vladimir Serbinenko  <phcoder@gmail.com>
31004e6
+
31004e6
 	* grub-core/commands/videoinfo.c: Use "paletted" rather than "packed
31004e6
 	pixel".
31004e6
 
31004e6
diff --git a/Makefile.util.def b/Makefile.util.def
31004e6
index 5888314..dc621db 100644
31004e6
--- a/Makefile.util.def
31004e6
+++ b/Makefile.util.def
31004e6
@@ -85,6 +85,7 @@ library = {
31004e6
   common = grub-core/fs/fshelp.c;
31004e6
   common = grub-core/fs/hfs.c;
31004e6
   common = grub-core/fs/hfsplus.c;
31004e6
+  common = grub-core/fs/hfspluscomp.c;
31004e6
   common = grub-core/fs/iso9660.c;
31004e6
   common = grub-core/fs/jfs.c;
31004e6
   common = grub-core/fs/minix.c;
31004e6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
31004e6
index 7dc106e..bb62dce 100644
31004e6
--- a/grub-core/Makefile.core.def
31004e6
+++ b/grub-core/Makefile.core.def
31004e6
@@ -1186,6 +1186,11 @@ module = {
31004e6
 };
31004e6
 
31004e6
 module = {
31004e6
+  name = hfspluscomp;
31004e6
+  common = fs/hfspluscomp.c;
31004e6
+};
31004e6
+
31004e6
+module = {
31004e6
   name = iso9660;
31004e6
   common = fs/iso9660.c;
31004e6
 };
31004e6
diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c
31004e6
index a507c0f..7905624 100644
31004e6
--- a/grub-core/fs/hfsplus.c
31004e6
+++ b/grub-core/fs/hfsplus.c
31004e6
@@ -19,6 +19,7 @@
31004e6
 
31004e6
 /* HFS+ is documented at http://developer.apple.com/technotes/tn/tn1150.html */
31004e6
 
31004e6
+#define grub_fshelp_node grub_hfsplus_file 
31004e6
 #include <grub/err.h>
31004e6
 #include <grub/file.h>
31004e6
 #include <grub/mm.h>
31004e6
@@ -29,6 +30,7 @@
31004e6
 #include <grub/fshelp.h>
31004e6
 #include <grub/hfs.h>
31004e6
 #include <grub/charset.h>
31004e6
+#include <grub/hfsplus.h>
31004e6
 
31004e6
 GRUB_MOD_LICENSE ("GPLv3+");
31004e6
 
31004e6
@@ -36,42 +38,6 @@ GRUB_MOD_LICENSE ("GPLv3+");
31004e6
 #define GRUB_HFSPLUSX_MAGIC 0x4858
31004e6
 #define GRUB_HFSPLUS_SBLOCK 2
31004e6
 
31004e6
-/* A HFS+ extent.  */
31004e6
-struct grub_hfsplus_extent
31004e6
-{
31004e6
-  /* The first block of a file on disk.  */
31004e6
-  grub_uint32_t start;
31004e6
-  /* The amount of blocks described by this extent.  */
31004e6
-  grub_uint32_t count;
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
-/* The descriptor of a fork.  */
31004e6
-struct grub_hfsplus_forkdata
31004e6
-{
31004e6
-  grub_uint64_t size;
31004e6
-  grub_uint32_t clumpsize;
31004e6
-  grub_uint32_t blocks;
31004e6
-  struct grub_hfsplus_extent extents[8];
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
-/* The HFS+ Volume Header.  */
31004e6
-struct grub_hfsplus_volheader
31004e6
-{
31004e6
-  grub_uint16_t magic;
31004e6
-  grub_uint16_t version;
31004e6
-  grub_uint32_t attributes;
31004e6
-  grub_uint8_t unused1[12];
31004e6
-  grub_uint32_t utime;
31004e6
-  grub_uint8_t unused2[16];
31004e6
-  grub_uint32_t blksize;
31004e6
-  grub_uint8_t unused3[60];
31004e6
-  grub_uint64_t num_serial;
31004e6
-  struct grub_hfsplus_forkdata allocations_file;
31004e6
-  struct grub_hfsplus_forkdata extents_file;
31004e6
-  struct grub_hfsplus_forkdata catalog_file;
31004e6
-  struct grub_hfsplus_forkdata attrib_file;
31004e6
-  struct grub_hfsplus_forkdata startup_file;
31004e6
-} __attribute__ ((packed));
31004e6
 
31004e6
 /* The type of node.  */
31004e6
 enum grub_hfsplus_btnode_type
31004e6
@@ -82,16 +48,6 @@ enum grub_hfsplus_btnode_type
31004e6
     GRUB_HFSPLUS_BTNODE_TYPE_MAP = 2,
31004e6
   };
31004e6
 
31004e6
-struct grub_hfsplus_btnode
31004e6
-{
31004e6
-  grub_uint32_t next;
31004e6
-  grub_uint32_t prev;
31004e6
-  grub_int8_t type;
31004e6
-  grub_uint8_t height;
31004e6
-  grub_uint16_t count;
31004e6
-  grub_uint16_t unused;
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
 /* The header of a HFS+ B+ Tree.  */
31004e6
 struct grub_hfsplus_btheader
31004e6
 {
31004e6
@@ -111,35 +67,6 @@ struct grub_hfsplus_btheader
31004e6
   grub_uint32_t attributes;
31004e6
 } __attribute__ ((packed));
31004e6
 
31004e6
-/* The on disk layout of a catalog key.  */
31004e6
-struct grub_hfsplus_catkey
31004e6
-{
31004e6
-  grub_uint16_t keylen;
31004e6
-  grub_uint32_t parent;
31004e6
-  grub_uint16_t namelen;
31004e6
-  grub_uint16_t name[30];
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
-/* The on disk layout of an extent overflow file key.  */
31004e6
-struct grub_hfsplus_extkey
31004e6
-{
31004e6
-  grub_uint16_t keylen;
31004e6
-  grub_uint8_t type;
31004e6
-  grub_uint8_t unused;
31004e6
-  grub_uint32_t fileid;
31004e6
-  grub_uint32_t start;
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
-struct grub_hfsplus_key
31004e6
-{
31004e6
-  union
31004e6
-  {
31004e6
-    struct grub_hfsplus_extkey extkey;
31004e6
-    struct grub_hfsplus_catkey catkey;
31004e6
-    grub_uint16_t keylen;
31004e6
-  };
31004e6
-} __attribute__ ((packed));
31004e6
-
31004e6
 struct grub_hfsplus_catfile
31004e6
 {
31004e6
   grub_uint16_t type;
31004e6
@@ -162,9 +89,13 @@ struct grub_hfsplus_catfile
31004e6
 #define GRUB_HFSPLUS_FILEMODE_SYMLINK	0120000
31004e6
 
31004e6
 /* Some pre-defined file IDs.  */
31004e6
-#define GRUB_HFSPLUS_FILEID_ROOTDIR	2
31004e6
-#define GRUB_HFSPLUS_FILEID_OVERFLOW	3
31004e6
-#define GRUB_HFSPLUS_FILEID_CATALOG	4
31004e6
+enum
31004e6
+  {
31004e6
+    GRUB_HFSPLUS_FILEID_ROOTDIR = 2,
31004e6
+    GRUB_HFSPLUS_FILEID_OVERFLOW = 3,
31004e6
+    GRUB_HFSPLUS_FILEID_CATALOG	= 4,
31004e6
+    GRUB_HFSPLUS_FILEID_ATTR	= 8
31004e6
+  };
31004e6
 
31004e6
 enum grub_hfsplus_filetype
31004e6
   {
31004e6
@@ -177,98 +108,15 @@ enum grub_hfsplus_filetype
31004e6
 #define GRUB_HFSPLUSX_BINARYCOMPARE	0xBC
31004e6
 #define GRUB_HFSPLUSX_CASEFOLDING	0xCF
31004e6
 
31004e6
-/* Internal representation of a catalog key.  */
31004e6
-struct grub_hfsplus_catkey_internal
31004e6
-{
31004e6
-  grub_uint32_t parent;
31004e6
-  const grub_uint16_t *name;
31004e6
-  grub_size_t namelen;
31004e6
-};
31004e6
-
31004e6
-/* Internal representation of an extent overflow key.  */
31004e6
-struct grub_hfsplus_extkey_internal
31004e6
-{
31004e6
-  grub_uint32_t fileid;
31004e6
-  grub_uint8_t type;
31004e6
-  grub_uint32_t start;
31004e6
-};
31004e6
-
31004e6
-struct grub_hfsplus_key_internal
31004e6
-{
31004e6
-  union
31004e6
-  {
31004e6
-    struct grub_hfsplus_extkey_internal extkey;
31004e6
-    struct grub_hfsplus_catkey_internal catkey;
31004e6
-  };
31004e6
-};
31004e6
-
31004e6
-
31004e6
-
31004e6
-struct grub_fshelp_node
31004e6
-{
31004e6
-  struct grub_hfsplus_data *data;
31004e6
-  struct grub_hfsplus_extent extents[8];
31004e6
-  grub_uint64_t size;
31004e6
-  grub_uint32_t fileid;
31004e6
-  grub_int32_t mtime;
31004e6
-};
31004e6
-
31004e6
-struct grub_hfsplus_btree
31004e6
-{
31004e6
-  grub_uint32_t root;
31004e6
-  grub_size_t nodesize;
31004e6
-
31004e6
-  /* Catalog file node.  */
31004e6
-  struct grub_fshelp_node file;
31004e6
-};
31004e6
-
31004e6
-/* Information about a "mounted" HFS+ filesystem.  */
31004e6
-struct grub_hfsplus_data
31004e6
-{
31004e6
-  struct grub_hfsplus_volheader volheader;
31004e6
-  grub_disk_t disk;
31004e6
-
31004e6
-  unsigned int log2blksize;
31004e6
-
31004e6
-  struct grub_hfsplus_btree catalog_tree;
31004e6
-  struct grub_hfsplus_btree extoverflow_tree;
31004e6
-
31004e6
-  struct grub_fshelp_node dirroot;
31004e6
-  struct grub_fshelp_node opened_file;
31004e6
-
31004e6
-  /* This is the offset into the physical disk for an embedded HFS+
31004e6
-     filesystem (one inside a plain HFS wrapper).  */
31004e6
-  grub_disk_addr_t embedded_offset;
31004e6
-  int case_sensitive;
31004e6
-};
31004e6
-
31004e6
 static grub_dl_t my_mod;
31004e6
 
31004e6
 
31004e6
-/* Return the offset of the record with the index INDEX, in the node
31004e6
-   NODE which is part of the B+ tree BTREE.  */
31004e6
-static inline grub_off_t
31004e6
-grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree *btree,
31004e6
-			   struct grub_hfsplus_btnode *node, int index)
31004e6
-{
31004e6
-  char *cnode = (char *) node;
31004e6
-  void *recptr;
31004e6
-  recptr = (&cnode[btree->nodesize - index * sizeof (grub_uint16_t) - 2]);
31004e6
-  return grub_be_to_cpu16 (grub_get_unaligned16 (recptr));
31004e6
-}
31004e6
-
31004e6
-/* Return a pointer to the record with the index INDEX, in the node
31004e6
-   NODE which is part of the B+ tree BTREE.  */
31004e6
-static inline struct grub_hfsplus_key *
31004e6
-grub_hfsplus_btree_recptr (struct grub_hfsplus_btree *btree,
31004e6
-			   struct grub_hfsplus_btnode *node, int index)
31004e6
-{
31004e6
-  char *cnode = (char *) node;
31004e6
-  grub_off_t offset;
31004e6
-  offset = grub_hfsplus_btree_recoffset (btree, node, index);
31004e6
-  return (struct grub_hfsplus_key *) &cnode[offset];
31004e6
-}
31004e6
 
31004e6
+grub_err_t (*grub_hfsplus_open_compressed) (struct grub_fshelp_node *node);
31004e6
+grub_ssize_t (*grub_hfsplus_read_compressed) (struct grub_hfsplus_file *node,
31004e6
+					      grub_off_t pos,
31004e6
+					      grub_size_t len,
31004e6
+					      char *buf);
31004e6
 
31004e6
 /* Find the extent that points to FILEBLOCK.  If it is not in one of
31004e6
    the 8 extents described by EXTENT, return -1.  In that case set
31004e6
@@ -292,14 +140,6 @@ grub_hfsplus_find_block (struct grub_hfsplus_extent *extent,
31004e6
   return 0xffffffffffffffffULL;
31004e6
 }
31004e6
 
31004e6
-static grub_err_t
31004e6
-grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
31004e6
-			   struct grub_hfsplus_key_internal *key,
31004e6
-			   int (*compare_keys) (struct grub_hfsplus_key *keya,
31004e6
-						struct grub_hfsplus_key_internal *keyb),
31004e6
-			   struct grub_hfsplus_btnode **matchnode, 
31004e6
-			   grub_off_t *keyoffset);
31004e6
-
31004e6
 static int grub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya,
31004e6
 				    struct grub_hfsplus_key_internal *keyb);
31004e6
 
31004e6
@@ -310,7 +150,8 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
31004e6
 {
31004e6
   struct grub_hfsplus_btnode *nnode = 0;
31004e6
   grub_disk_addr_t blksleft = fileblock;
31004e6
-  struct grub_hfsplus_extent *extents = &node->extents[0];
31004e6
+  struct grub_hfsplus_extent *extents = node->compressed 
31004e6
+    ? &node->resource_extents[0] : &node->extents[0];
31004e6
 
31004e6
   while (1)
31004e6
     {
31004e6
@@ -344,10 +185,11 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
31004e6
       extoverflow.extkey.fileid = node->fileid;
31004e6
       extoverflow.extkey.type = 0;
31004e6
       extoverflow.extkey.start = fileblock - blksleft;
31004e6
-
31004e6
+      extoverflow.extkey.type = node->compressed ? 0xff : 0;
31004e6
       if (grub_hfsplus_btree_search (&node->data->extoverflow_tree,
31004e6
 				     &extoverflow,
31004e6
-				     grub_hfsplus_cmp_extkey, &nnode, &ptr))
31004e6
+				     grub_hfsplus_cmp_extkey, &nnode, &ptr)
31004e6
+	  || !nnode)
31004e6
 	{
31004e6
 	  grub_error (GRUB_ERR_READ_ERROR,
31004e6
 		      "no block found for the file id 0x%x and the block offset 0x%x",
31004e6
@@ -373,7 +215,7 @@ grub_hfsplus_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
31004e6
 
31004e6
 /* Read LEN bytes from the file described by DATA starting with byte
31004e6
    POS.  Return the amount of read bytes in READ.  */
31004e6
-static grub_ssize_t
31004e6
+grub_ssize_t
31004e6
 grub_hfsplus_read_file (grub_fshelp_node_t node,
31004e6
 			grub_disk_read_hook_t read_hook, void *read_hook_data,
31004e6
 			grub_off_t pos, grub_size_t len, char *buf)
31004e6
@@ -460,15 +302,27 @@ grub_hfsplus_mount (grub_disk_t disk)
31004e6
   /* Make a new node for the catalog tree.  */
31004e6
   data->catalog_tree.file.data = data;
31004e6
   data->catalog_tree.file.fileid = GRUB_HFSPLUS_FILEID_CATALOG;
31004e6
+  data->catalog_tree.file.compressed = 0;
31004e6
   grub_memcpy (&data->catalog_tree.file.extents,
31004e6
 	       data->volheader.catalog_file.extents,
31004e6
 	       sizeof data->volheader.catalog_file.extents);
31004e6
   data->catalog_tree.file.size =
31004e6
     grub_be_to_cpu64 (data->volheader.catalog_file.size);
31004e6
 
31004e6
+  data->attr_tree.file.data = data;
31004e6
+  data->attr_tree.file.fileid = GRUB_HFSPLUS_FILEID_ATTR;
31004e6
+  grub_memcpy (&data->attr_tree.file.extents,
31004e6
+	       data->volheader.attr_file.extents,
31004e6
+	       sizeof data->volheader.attr_file.extents);
31004e6
+
31004e6
+  data->attr_tree.file.size =
31004e6
+    grub_be_to_cpu64 (data->volheader.attr_file.size);
31004e6
+  data->attr_tree.file.compressed = 0;
31004e6
+
31004e6
   /* Make a new node for the extent overflow file.  */
31004e6
   data->extoverflow_tree.file.data = data;
31004e6
   data->extoverflow_tree.file.fileid = GRUB_HFSPLUS_FILEID_OVERFLOW;
31004e6
+  data->extoverflow_tree.file.compressed = 0;
31004e6
   grub_memcpy (&data->extoverflow_tree.file.extents,
31004e6
 	       data->volheader.extents_file.extents,
31004e6
 	       sizeof data->volheader.catalog_file.extents);
31004e6
@@ -501,6 +355,20 @@ grub_hfsplus_mount (grub_disk_t disk)
31004e6
   data->extoverflow_tree.root = grub_be_to_cpu32 (header.root);
31004e6
   data->extoverflow_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
31004e6
 
31004e6
+  if (grub_hfsplus_read_file (&data->attr_tree.file, 0, 0,
31004e6
+			      sizeof (struct grub_hfsplus_btnode),
31004e6
+			      sizeof (header), (char *) &header) <= 0)
31004e6
+    {
31004e6
+      grub_errno = 0;
31004e6
+      data->attr_tree.root = 0;
31004e6
+      data->attr_tree.nodesize = 0;
31004e6
+    }
31004e6
+  else
31004e6
+    {
31004e6
+      data->attr_tree.root = grub_be_to_cpu32 (header.root);
31004e6
+      data->attr_tree.nodesize = grub_be_to_cpu16 (header.nodesize);
31004e6
+    }
31004e6
+
31004e6
   data->dirroot.data = data;
31004e6
   data->dirroot.fileid = GRUB_HFSPLUS_FILEID_ROOTDIR;
31004e6
 
31004e6
@@ -586,6 +454,12 @@ grub_hfsplus_cmp_extkey (struct grub_hfsplus_key *keya,
31004e6
     return 1;
31004e6
   if (extkey_a->type < extkey_b->type)
31004e6
     return -1;
31004e6
+
31004e6
+  if (extkey_a->type > extkey_b->type)
31004e6
+    return +1;
31004e6
+
31004e6
+  if (extkey_a->type < extkey_b->type)
31004e6
+    return -1;
31004e6
   
31004e6
   akey = grub_be_to_cpu32 (extkey_a->start);
31004e6
   if (akey > extkey_b->start)
31004e6
@@ -668,7 +542,7 @@ grub_hfsplus_btree_iterate_node (struct grub_hfsplus_btree *btree,
31004e6
    keys using the function COMPARE_KEYS.  When a match is found,
31004e6
    return the node in MATCHNODE and a pointer to the data in this node
31004e6
    in KEYOFFSET.  MATCHNODE should be freed by the caller.  */
31004e6
-static grub_err_t
31004e6
+grub_err_t
31004e6
 grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
31004e6
 			   struct grub_hfsplus_key_internal *key,
31004e6
 			   int (*compare_keys) (struct grub_hfsplus_key *keya,
31004e6
@@ -683,6 +557,12 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
31004e6
   grub_uint64_t save_node;
31004e6
   grub_uint64_t node_count = 0;
31004e6
 
31004e6
+  if (!btree->nodesize)
31004e6
+    {
31004e6
+      *matchnode = 0;
31004e6
+      return 0;
31004e6
+    }
31004e6
+
31004e6
   node = grub_malloc (btree->nodesize);
31004e6
   if (! node)
31004e6
     return grub_errno;
31004e6
@@ -760,7 +640,7 @@ grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
31004e6
 	{
31004e6
 	  *matchnode = 0;
31004e6
 	  grub_free (node);
31004e6
-	  return 1;
31004e6
+	  return 0;
31004e6
 	}
31004e6
     }
31004e6
 }
31004e6
@@ -872,11 +752,17 @@ list_nodes (void *record, void *hook_arg)
31004e6
   if (!node)
31004e6
     return 1;
31004e6
   node->data = ctx->dir->data;
31004e6
+  node->compressed = 0;
31004e6
+  node->cbuf = 0;
31004e6
+  node->compress_index = 0;
31004e6
 
31004e6
   grub_memcpy (node->extents, fileinfo->data.extents,
31004e6
 	       sizeof (node->extents));
31004e6
+  grub_memcpy (node->resource_extents, fileinfo->resource.extents,
31004e6
+	       sizeof (node->resource_extents));
31004e6
   node->mtime = grub_be_to_cpu32 (fileinfo->mtime) - 2082844800;
31004e6
   node->size = grub_be_to_cpu64 (fileinfo->data.size);
31004e6
+  node->resource_size = grub_be_to_cpu64 (fileinfo->resource.size);
31004e6
   node->fileid = grub_be_to_cpu32 (fileinfo->fileid);
31004e6
 
31004e6
   ctx->ret = ctx->hook (filename, type, node, ctx->hook_data);
31004e6
@@ -919,7 +805,8 @@ grub_hfsplus_iterate_dir (grub_fshelp_node_t dir,
31004e6
 
31004e6
   /* First lookup the first entry.  */
31004e6
   if (grub_hfsplus_btree_search (&dir->data->catalog_tree, &intern,
31004e6
-				 grub_hfsplus_cmp_catkey, &node, &ptr))
31004e6
+				 grub_hfsplus_cmp_catkey, &node, &ptr)
31004e6
+      || !node)
31004e6
     return 0;
31004e6
 
31004e6
   /* Iterate over all entries in this directory.  */
31004e6
@@ -950,6 +837,14 @@ grub_hfsplus_open (struct grub_file *file, const char *name)
31004e6
   if (grub_errno)
31004e6
     goto fail;
31004e6
 
31004e6
+  if (grub_hfsplus_open_compressed)
31004e6
+    {
31004e6
+      grub_err_t err;
31004e6
+      err = grub_hfsplus_open_compressed (fdiro);
31004e6
+      if (err)
31004e6
+	goto fail;
31004e6
+    }
31004e6
+
31004e6
   file->size = fdiro->size;
31004e6
   data->opened_file = *fdiro;
31004e6
   grub_free (fdiro);
31004e6
@@ -973,7 +868,13 @@ grub_hfsplus_open (struct grub_file *file, const char *name)
31004e6
 static grub_err_t
31004e6
 grub_hfsplus_close (grub_file_t file)
31004e6
 {
31004e6
-  grub_free (file->data);
31004e6
+  struct grub_hfsplus_data *data =
31004e6
+    (struct grub_hfsplus_data *) file->data;
31004e6
+
31004e6
+  grub_free (data->opened_file.cbuf);
31004e6
+  grub_free (data->opened_file.compress_index);
31004e6
+
31004e6
+  grub_free (data);
31004e6
 
31004e6
   grub_dl_unref (my_mod);
31004e6
 
31004e6
@@ -987,6 +888,10 @@ grub_hfsplus_read (grub_file_t file, char *buf, grub_size_t len)
31004e6
   struct grub_hfsplus_data *data =
31004e6
     (struct grub_hfsplus_data *) file->data;
31004e6
 
31004e6
+  if (grub_hfsplus_read_compressed && data->opened_file.compressed)
31004e6
+    return grub_hfsplus_read_compressed (&data->opened_file,
31004e6
+					 file->offset, len, buf);
31004e6
+
31004e6
   return grub_hfsplus_read_file (&data->opened_file,
31004e6
 				 file->read_hook, file->read_hook_data,
31004e6
 				 file->offset, len, buf);
31004e6
@@ -1076,7 +981,8 @@ grub_hfsplus_label (grub_device_t device, char **label)
31004e6
 
31004e6
   /* First lookup the first entry.  */
31004e6
   if (grub_hfsplus_btree_search (&data->catalog_tree, &intern,
31004e6
-				 grub_hfsplus_cmp_catkey_id, &node, &ptr))
31004e6
+				 grub_hfsplus_cmp_catkey_id, &node, &ptr)
31004e6
+      || !node)
31004e6
     {
31004e6
       grub_free (data);
31004e6
       return 0;
31004e6
diff --git a/grub-core/fs/hfspluscomp.c b/grub-core/fs/hfspluscomp.c
31004e6
new file mode 100644
31004e6
index 0000000..b343441
31004e6
--- /dev/null
31004e6
+++ b/grub-core/fs/hfspluscomp.c
31004e6
@@ -0,0 +1,302 @@
31004e6
+/*
31004e6
+ *  GRUB  --  GRand Unified Bootloader
31004e6
+ *  Copyright (C) 2012  Free Software Foundation, Inc.
31004e6
+ *
31004e6
+ *  GRUB is free software: you can redistribute it and/or modify
31004e6
+ *  it under the terms of the GNU General Public License as published by
31004e6
+ *  the Free Software Foundation, either version 3 of the License, or
31004e6
+ *  (at your option) any later version.
31004e6
+ *
31004e6
+ *  GRUB is distributed in the hope that it will be useful,
31004e6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
31004e6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31004e6
+ *  GNU General Public License for more details.
31004e6
+ *
31004e6
+ *  You should have received a copy of the GNU General Public License
31004e6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
31004e6
+ */
31004e6
+
31004e6
+/* HFS+ is documented at http://developer.apple.com/technotes/tn/tn1150.html */
31004e6
+
31004e6
+#include <grub/hfsplus.h>
31004e6
+#include <grub/dl.h>
31004e6
+#include <grub/misc.h>
31004e6
+#include <grub/mm.h>
31004e6
+#include <grub/deflate.h>
31004e6
+
31004e6
+GRUB_MOD_LICENSE ("GPLv3+");
31004e6
+
31004e6
+/* big-endian.  */
31004e6
+struct grub_hfsplus_compress_header1
31004e6
+{
31004e6
+  grub_uint32_t header_size;
31004e6
+  grub_uint32_t end_descriptor_offset;
31004e6
+  grub_uint32_t total_compressed_size_including_seek_blocks_and_header2;
31004e6
+  grub_uint32_t value_0x32;
31004e6
+  grub_uint8_t unused[0xf0];
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* big-endian.  */
31004e6
+struct grub_hfsplus_compress_header2
31004e6
+{
31004e6
+  grub_uint32_t total_compressed_size_including_seek_blocks;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* little-endian.  */
31004e6
+struct grub_hfsplus_compress_header3
31004e6
+{
31004e6
+  grub_uint32_t num_chunks;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* little-endian.  */
31004e6
+struct grub_hfsplus_compress_block_descriptor
31004e6
+{
31004e6
+  grub_uint32_t offset;
31004e6
+  grub_uint32_t size;
31004e6
+};
31004e6
+
31004e6
+struct grub_hfsplus_compress_end_descriptor
31004e6
+{
31004e6
+  grub_uint8_t always_the_same[50];
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_attr_header
31004e6
+{
31004e6
+  grub_uint8_t unused[3];
31004e6
+  grub_uint8_t type;
31004e6
+  grub_uint32_t unknown[1];
31004e6
+  grub_uint64_t size;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_compress_attr
31004e6
+{
31004e6
+  grub_uint32_t magic;
31004e6
+  grub_uint32_t type;
31004e6
+  grub_uint32_t uncompressed_inline_size;
31004e6
+  grub_uint32_t always_0;
31004e6
+};
31004e6
+
31004e6
+enum
31004e6
+  {
31004e6
+    HFSPLUS_COMPRESSION_INLINE = 3,
31004e6
+    HFSPLUS_COMPRESSION_RESOURCE = 4
31004e6
+  };
31004e6
+
31004e6
+static int
31004e6
+grub_hfsplus_cmp_attrkey (struct grub_hfsplus_key *keya,
31004e6
+			  struct grub_hfsplus_key_internal *keyb)
31004e6
+{
31004e6
+  struct grub_hfsplus_attrkey *attrkey_a = &keya->attrkey;
31004e6
+  struct grub_hfsplus_attrkey_internal *attrkey_b = &keyb->attrkey;
31004e6
+  grub_uint32_t aparent = grub_be_to_cpu32 (attrkey_a->cnid);
31004e6
+  grub_size_t len;
31004e6
+  int diff;
31004e6
+
31004e6
+  if (aparent > attrkey_b->cnid)
31004e6
+    return 1;
31004e6
+  if (aparent < attrkey_b->cnid)
31004e6
+    return -1;
31004e6
+
31004e6
+  len = grub_be_to_cpu16 (attrkey_a->namelen);
31004e6
+  if (len > attrkey_b->namelen)
31004e6
+    len = attrkey_b->namelen;
31004e6
+  /* Since it's big-endian memcmp gives the same result as manually comparing
31004e6
+     uint16_t but may be faster.  */
31004e6
+  diff = grub_memcmp (attrkey_a->name, attrkey_b->name,
31004e6
+		      len * sizeof (attrkey_a->name[0]));
31004e6
+  if (diff == 0)
31004e6
+    diff = grub_be_to_cpu16 (attrkey_a->namelen) - attrkey_b->namelen;
31004e6
+  return diff;
31004e6
+}
31004e6
+
31004e6
+#define HFSPLUS_COMPRESS_BLOCK_SIZE 65536
31004e6
+
31004e6
+static grub_ssize_t
31004e6
+hfsplus_read_compressed_real (struct grub_hfsplus_file *node,
31004e6
+			      grub_off_t pos, grub_size_t len, char *buf)
31004e6
+{
31004e6
+  char *tmp_buf = 0;
31004e6
+  grub_size_t len0 = len;
31004e6
+
31004e6
+  if (node->compressed == 1)
31004e6
+    {
31004e6
+      grub_memcpy (buf, node->cbuf + pos, len);
31004e6
+      return len;
31004e6
+    }
31004e6
+
31004e6
+  while (len)
31004e6
+    {
31004e6
+      grub_uint32_t block = pos / HFSPLUS_COMPRESS_BLOCK_SIZE;
31004e6
+      grub_size_t curlen = HFSPLUS_COMPRESS_BLOCK_SIZE
31004e6
+	- (pos % HFSPLUS_COMPRESS_BLOCK_SIZE);
31004e6
+
31004e6
+      if (curlen > len)
31004e6
+	curlen = len;
31004e6
+
31004e6
+      if (node->cbuf_block != block)
31004e6
+	{
31004e6
+	  grub_uint32_t sz = grub_le_to_cpu32 (node->compress_index[block].size);
31004e6
+	  grub_size_t ts;
31004e6
+	  if (!tmp_buf)
31004e6
+	    tmp_buf = grub_malloc (HFSPLUS_COMPRESS_BLOCK_SIZE);
31004e6
+	  if (!tmp_buf)
31004e6
+	    return -1;
31004e6
+	  if (grub_hfsplus_read_file (node, 0, 0,
31004e6
+				      grub_le_to_cpu32 (node->compress_index[block].start) + 0x104,
31004e6
+				      sz, tmp_buf)
31004e6
+	      != (grub_ssize_t) sz)
31004e6
+	    {
31004e6
+	      grub_free (tmp_buf);
31004e6
+	      return -1;
31004e6
+	    }
31004e6
+	  ts = HFSPLUS_COMPRESS_BLOCK_SIZE;
31004e6
+	  if (ts > node->size - (pos & ~(HFSPLUS_COMPRESS_BLOCK_SIZE)))
31004e6
+	    ts = node->size - (pos & ~(HFSPLUS_COMPRESS_BLOCK_SIZE));
31004e6
+	  if (grub_zlib_decompress (tmp_buf, sz, 0,
31004e6
+				    node->cbuf, ts) < 0)
31004e6
+	    {
31004e6
+	      grub_free (tmp_buf);
31004e6
+	      return -1;
31004e6
+	    }
31004e6
+	  node->cbuf_block = block;
31004e6
+	}
31004e6
+      grub_memcpy (buf, node->cbuf + (pos % HFSPLUS_COMPRESS_BLOCK_SIZE),
31004e6
+		   curlen);
31004e6
+      buf += curlen;
31004e6
+      pos += curlen;
31004e6
+      len -= curlen;
31004e6
+    }
31004e6
+  grub_free (tmp_buf);
31004e6
+  return len0;
31004e6
+}
31004e6
+
31004e6
+static grub_err_t 
31004e6
+hfsplus_open_compressed_real (struct grub_hfsplus_file *node)
31004e6
+{
31004e6
+  grub_err_t err;
31004e6
+  struct grub_hfsplus_btnode *attr_node;
31004e6
+  grub_off_t attr_off;
31004e6
+  struct grub_hfsplus_key_internal key;
31004e6
+  struct grub_hfsplus_attr_header *attr_head;
31004e6
+  struct grub_hfsplus_compress_attr *cmp_head;
31004e6
+#define c grub_cpu_to_be16_compile_time
31004e6
+  const grub_uint16_t compress_attr_name[] =
31004e6
+    {
31004e6
+      c('c'), c('o'), c('m'), c('.'), c('a'), c('p'), c('p'), c('l'), c('e'),
31004e6
+      c('.'), c('d'), c('e'), c('c'), c('m'), c('p'), c('f'), c('s') };
31004e6
+#undef c
31004e6
+  if (node->size)
31004e6
+    return 0;
31004e6
+
31004e6
+  key.attrkey.cnid = node->fileid;
31004e6
+  key.attrkey.namelen = sizeof (compress_attr_name) / sizeof (compress_attr_name[0]);
31004e6
+  key.attrkey.name = compress_attr_name;
31004e6
+
31004e6
+  err = grub_hfsplus_btree_search (&node->data->attr_tree, &key,
31004e6
+				   grub_hfsplus_cmp_attrkey,
31004e6
+				   &attr_node, &attr_off);
31004e6
+  if (err || !attr_node)
31004e6
+    {
31004e6
+      grub_errno = 0;
31004e6
+      return 0;
31004e6
+    }
31004e6
+
31004e6
+  attr_head = (struct grub_hfsplus_attr_header *)
31004e6
+    ((char *) grub_hfsplus_btree_recptr (&node->data->attr_tree,
31004e6
+					 attr_node, attr_off)
31004e6
+     + sizeof (struct grub_hfsplus_attrkey) + sizeof (compress_attr_name));
31004e6
+  if (attr_head->type != 0x10
31004e6
+      || !(attr_head->size & grub_cpu_to_be64_compile_time(~0xfULL)))
31004e6
+    {
31004e6
+      grub_free (attr_node);
31004e6
+      return 0;
31004e6
+    }
31004e6
+  cmp_head = (struct grub_hfsplus_compress_attr *) (attr_head + 1);
31004e6
+  if (cmp_head->magic != grub_cpu_to_be32_compile_time (0x66706d63))
31004e6
+    {
31004e6
+      grub_free (attr_node);
31004e6
+      return 0;
31004e6
+    }
31004e6
+  node->size = grub_le_to_cpu32 (cmp_head->uncompressed_inline_size);
31004e6
+
31004e6
+  if (cmp_head->type == grub_cpu_to_le32_compile_time (HFSPLUS_COMPRESSION_RESOURCE))
31004e6
+    {
31004e6
+      grub_uint32_t index_size;
31004e6
+      node->compressed = 2;
31004e6
+
31004e6
+      if (grub_hfsplus_read_file (node, 0, 0,
31004e6
+				  0x104, sizeof (index_size),
31004e6
+				  (char *) &index_size)
31004e6
+	  != 4)
31004e6
+	{
31004e6
+	  node->compressed = 0;
31004e6
+	  grub_free (attr_node);
31004e6
+	  grub_errno = 0;
31004e6
+	  return 0;
31004e6
+	}
31004e6
+      node->compress_index_size = grub_le_to_cpu32 (index_size);
31004e6
+      node->compress_index = grub_malloc (node->compress_index_size
31004e6
+					  * sizeof (node->compress_index[0]));
31004e6
+      if (!node->compress_index)
31004e6
+	{
31004e6
+	  node->compressed = 0;
31004e6
+	  grub_free (attr_node);
31004e6
+	  return grub_errno;
31004e6
+	}
31004e6
+      if (grub_hfsplus_read_file (node, 0, 0,
31004e6
+				  0x104 + sizeof (index_size),
31004e6
+				  node->compress_index_size
31004e6
+				  * sizeof (node->compress_index[0]),
31004e6
+				  (char *) node->compress_index)
31004e6
+	  != (grub_ssize_t) (node->compress_index_size
31004e6
+			     * sizeof (node->compress_index[0])))
31004e6
+	{
31004e6
+	  node->compressed = 0;
31004e6
+	  grub_free (attr_node);
31004e6
+	  grub_free (node->compress_index);
31004e6
+	  grub_errno = 0;
31004e6
+	  return 0;
31004e6
+	}
31004e6
+
31004e6
+      node->cbuf_block = -1;
31004e6
+
31004e6
+      node->cbuf = grub_malloc (HFSPLUS_COMPRESS_BLOCK_SIZE);
31004e6
+      grub_free (attr_node);
31004e6
+      if (!node->cbuf)
31004e6
+	{
31004e6
+	  node->compressed = 0;
31004e6
+	  grub_free (node->compress_index);
31004e6
+	  return grub_errno;
31004e6
+	}
31004e6
+      return 0;
31004e6
+    }
31004e6
+  if (cmp_head->type != HFSPLUS_COMPRESSION_INLINE)
31004e6
+    {
31004e6
+      grub_free (attr_node);
31004e6
+      return 0;
31004e6
+    }
31004e6
+
31004e6
+  node->cbuf = grub_malloc (node->size);
31004e6
+  if (!node->cbuf)
31004e6
+    return grub_errno;
31004e6
+
31004e6
+  if (grub_zlib_decompress ((char *) (cmp_head + 1),
31004e6
+			    grub_cpu_to_be64 (attr_head->size)
31004e6
+			    - sizeof (*cmp_head), 0,
31004e6
+			    node->cbuf, node->size) < 0)
31004e6
+    return grub_errno;
31004e6
+  node->compressed = 1;
31004e6
+  return 0;
31004e6
+}
31004e6
+
31004e6
+GRUB_MOD_INIT(hfspluscomp)
31004e6
+{
31004e6
+  grub_hfsplus_open_compressed = hfsplus_open_compressed_real;  
31004e6
+  grub_hfsplus_read_compressed = hfsplus_read_compressed_real;  
31004e6
+}
31004e6
+
31004e6
+GRUB_MOD_FINI(hfspluscomp)
31004e6
+{
31004e6
+  grub_hfsplus_open_compressed = 0;
31004e6
+  grub_hfsplus_read_compressed = 0;
31004e6
+}
31004e6
diff --git a/include/grub/hfsplus.h b/include/grub/hfsplus.h
31004e6
new file mode 100644
31004e6
index 0000000..0defd35
31004e6
--- /dev/null
31004e6
+++ b/include/grub/hfsplus.h
31004e6
@@ -0,0 +1,217 @@
31004e6
+
31004e6
+#include <grub/types.h>
31004e6
+#include <grub/disk.h>
31004e6
+
31004e6
+/* A HFS+ extent.  */
31004e6
+struct grub_hfsplus_extent
31004e6
+{
31004e6
+  /* The first block of a file on disk.  */
31004e6
+  grub_uint32_t start;
31004e6
+  /* The amount of blocks described by this extent.  */
31004e6
+  grub_uint32_t count;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* The descriptor of a fork.  */
31004e6
+struct grub_hfsplus_forkdata
31004e6
+{
31004e6
+  grub_uint64_t size;
31004e6
+  grub_uint32_t clumpsize;
31004e6
+  grub_uint32_t blocks;
31004e6
+  struct grub_hfsplus_extent extents[8];
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* The HFS+ Volume Header.  */
31004e6
+struct grub_hfsplus_volheader
31004e6
+{
31004e6
+  grub_uint16_t magic;
31004e6
+  grub_uint16_t version;
31004e6
+  grub_uint32_t attributes;
31004e6
+  grub_uint8_t unused1[12];
31004e6
+  grub_uint32_t utime;
31004e6
+  grub_uint8_t unused2[16];
31004e6
+  grub_uint32_t blksize;
31004e6
+  grub_uint8_t unused3[60];
31004e6
+  grub_uint64_t num_serial;
31004e6
+  struct grub_hfsplus_forkdata allocations_file;
31004e6
+  struct grub_hfsplus_forkdata extents_file;
31004e6
+  struct grub_hfsplus_forkdata catalog_file;
31004e6
+  struct grub_hfsplus_forkdata attr_file;
31004e6
+  struct grub_hfsplus_forkdata startup_file;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_compress_index
31004e6
+{
31004e6
+  grub_uint32_t start;
31004e6
+  grub_uint32_t size;
31004e6
+};
31004e6
+
31004e6
+struct grub_hfsplus_file
31004e6
+{
31004e6
+  struct grub_hfsplus_data *data;
31004e6
+  struct grub_hfsplus_extent extents[8];
31004e6
+  struct grub_hfsplus_extent resource_extents[8];
31004e6
+  grub_uint64_t size;
31004e6
+  grub_uint64_t resource_size;
31004e6
+  grub_uint32_t fileid;
31004e6
+  grub_int32_t mtime;
31004e6
+  int compressed;
31004e6
+  char *cbuf;
31004e6
+  struct grub_hfsplus_compress_index *compress_index;
31004e6
+  grub_uint32_t cbuf_block;
31004e6
+  grub_uint32_t compress_index_size;
31004e6
+};
31004e6
+
31004e6
+struct grub_hfsplus_btree
31004e6
+{
31004e6
+  grub_uint32_t root;
31004e6
+  grub_size_t nodesize;
31004e6
+
31004e6
+  /* Catalog file node.  */
31004e6
+  struct grub_hfsplus_file file;
31004e6
+};
31004e6
+
31004e6
+/* Information about a "mounted" HFS+ filesystem.  */
31004e6
+struct grub_hfsplus_data
31004e6
+{
31004e6
+  struct grub_hfsplus_volheader volheader;
31004e6
+  grub_disk_t disk;
31004e6
+
31004e6
+  unsigned int log2blksize;
31004e6
+
31004e6
+  struct grub_hfsplus_btree catalog_tree;
31004e6
+  struct grub_hfsplus_btree extoverflow_tree;
31004e6
+  struct grub_hfsplus_btree attr_tree;
31004e6
+
31004e6
+  struct grub_hfsplus_file dirroot;
31004e6
+  struct grub_hfsplus_file opened_file;
31004e6
+
31004e6
+  /* This is the offset into the physical disk for an embedded HFS+
31004e6
+     filesystem (one inside a plain HFS wrapper).  */
31004e6
+  grub_disk_addr_t embedded_offset;
31004e6
+  int case_sensitive;
31004e6
+};
31004e6
+
31004e6
+/* Internal representation of a catalog key.  */
31004e6
+struct grub_hfsplus_catkey_internal
31004e6
+{
31004e6
+  grub_uint32_t parent;
31004e6
+  const grub_uint16_t *name;
31004e6
+  grub_size_t namelen;
31004e6
+};
31004e6
+
31004e6
+/* Internal representation of an extent overflow key.  */
31004e6
+struct grub_hfsplus_extkey_internal
31004e6
+{
31004e6
+  grub_uint32_t fileid;
31004e6
+  grub_uint32_t start;
31004e6
+  grub_uint8_t type;
31004e6
+};
31004e6
+
31004e6
+struct grub_hfsplus_attrkey
31004e6
+{
31004e6
+  grub_uint16_t keylen;
31004e6
+  grub_uint16_t unknown1[1];
31004e6
+  grub_uint32_t cnid;
31004e6
+  grub_uint16_t unknown2[2];
31004e6
+  grub_uint16_t namelen;
31004e6
+  grub_uint16_t name[0];
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_attrkey_internal
31004e6
+{
31004e6
+  grub_uint32_t cnid;
31004e6
+  const grub_uint16_t *name;
31004e6
+  grub_size_t namelen;
31004e6
+};
31004e6
+
31004e6
+struct grub_hfsplus_key_internal
31004e6
+{
31004e6
+  union
31004e6
+  {
31004e6
+    struct grub_hfsplus_extkey_internal extkey;
31004e6
+    struct grub_hfsplus_catkey_internal catkey;
31004e6
+    struct grub_hfsplus_attrkey_internal attrkey;
31004e6
+  };
31004e6
+};
31004e6
+
31004e6
+/* The on disk layout of a catalog key.  */
31004e6
+struct grub_hfsplus_catkey
31004e6
+{
31004e6
+  grub_uint16_t keylen;
31004e6
+  grub_uint32_t parent;
31004e6
+  grub_uint16_t namelen;
31004e6
+  grub_uint16_t name[30];
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* The on disk layout of an extent overflow file key.  */
31004e6
+struct grub_hfsplus_extkey
31004e6
+{
31004e6
+  grub_uint16_t keylen;
31004e6
+  grub_uint8_t type;
31004e6
+  grub_uint8_t unused;
31004e6
+  grub_uint32_t fileid;
31004e6
+  grub_uint32_t start;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_key
31004e6
+{
31004e6
+  union
31004e6
+  {
31004e6
+    struct grub_hfsplus_extkey extkey;
31004e6
+    struct grub_hfsplus_catkey catkey;
31004e6
+    struct grub_hfsplus_attrkey attrkey;
31004e6
+    grub_uint16_t keylen;
31004e6
+  };
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+struct grub_hfsplus_btnode
31004e6
+{
31004e6
+  grub_uint32_t next;
31004e6
+  grub_uint32_t prev;
31004e6
+  grub_int8_t type;
31004e6
+  grub_uint8_t height;
31004e6
+  grub_uint16_t count;
31004e6
+  grub_uint16_t unused;
31004e6
+} __attribute__ ((packed));
31004e6
+
31004e6
+/* Return the offset of the record with the index INDEX, in the node
31004e6
+   NODE which is part of the B+ tree BTREE.  */
31004e6
+static inline grub_off_t
31004e6
+grub_hfsplus_btree_recoffset (struct grub_hfsplus_btree *btree,
31004e6
+			   struct grub_hfsplus_btnode *node, int index)
31004e6
+{
31004e6
+  char *cnode = (char *) node;
31004e6
+  void *recptr;
31004e6
+  recptr = (&cnode[btree->nodesize - index * sizeof (grub_uint16_t) - 2]);
31004e6
+  return grub_be_to_cpu16 (grub_get_unaligned16 (recptr));
31004e6
+}
31004e6
+
31004e6
+/* Return a pointer to the record with the index INDEX, in the node
31004e6
+   NODE which is part of the B+ tree BTREE.  */
31004e6
+static inline struct grub_hfsplus_key *
31004e6
+grub_hfsplus_btree_recptr (struct grub_hfsplus_btree *btree,
31004e6
+			   struct grub_hfsplus_btnode *node, int index)
31004e6
+{
31004e6
+  char *cnode = (char *) node;
31004e6
+  grub_off_t offset;
31004e6
+  offset = grub_hfsplus_btree_recoffset (btree, node, index);
31004e6
+  return (struct grub_hfsplus_key *) &cnode[offset];
31004e6
+}
31004e6
+
31004e6
+extern grub_err_t (*grub_hfsplus_open_compressed) (struct grub_hfsplus_file *node);
31004e6
+extern grub_ssize_t (*grub_hfsplus_read_compressed) (struct grub_hfsplus_file *node,
31004e6
+						     grub_off_t pos,
31004e6
+						     grub_size_t len,
31004e6
+						     char *buf);
31004e6
+
31004e6
+grub_ssize_t
31004e6
+grub_hfsplus_read_file (struct grub_hfsplus_file *node,
31004e6
+			grub_disk_read_hook_t read_hook, void *read_hook_data,
31004e6
+			grub_off_t pos, grub_size_t len, char *buf);
31004e6
+grub_err_t
31004e6
+grub_hfsplus_btree_search (struct grub_hfsplus_btree *btree,
31004e6
+			   struct grub_hfsplus_key_internal *key,
31004e6
+			   int (*compare_keys) (struct grub_hfsplus_key *keya,
31004e6
+						struct grub_hfsplus_key_internal *keyb),
31004e6
+			   struct grub_hfsplus_btnode **matchnode, 
31004e6
+			   grub_off_t *keyoffset);
31004e6
-- 
31004e6
1.8.2.1
31004e6