Blob Blame History Raw
From 816fd37ee87127911c61b5464dfdd25145b36e1c Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sun, 10 Mar 2013 18:27:53 +0100
Subject: [PATCH 202/482] 	* grub-core/fs/hfs.c (grub_hfs_read_file):
 Avoid divmod64 since the 	maximum size is 4G - 1 on hfs

---
 ChangeLog          |  5 +++++
 grub-core/fs/hfs.c | 15 ++++++++-------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bc51ae9..931ae8e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	* grub-core/fs/hfs.c (grub_hfs_read_file): Avoid divmod64 since the
+	maximum size is 4G - 1 on hfs
+
+2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	Avoid costly 64-bit division in grub_get_time_ms on most platforms.
 
 2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
diff --git a/grub-core/fs/hfs.c b/grub-core/fs/hfs.c
index 73ac7f9..14520c0 100644
--- a/grub-core/fs/hfs.c
+++ b/grub-core/fs/hfs.c
@@ -244,15 +244,16 @@ grub_hfs_block (struct grub_hfs_data *data, grub_hfs_datarecord_t dat,
 static grub_ssize_t
 grub_hfs_read_file (struct grub_hfs_data *data,
 		    grub_disk_read_hook_t read_hook, void *read_hook_data,
-		    grub_off_t pos, grub_size_t len, char *buf)
+		    grub_uint32_t pos, grub_size_t len, char *buf)
 {
   grub_off_t i;
   grub_off_t blockcnt;
 
-  blockcnt = grub_divmod64 (((len + pos)
-			     + data->blksz - 1), data->blksz, 0);
+  /* Files are at most 2G/4G - 1 bytes on hfs. Avoid 64-bit division.
+     Moreover len > 0 as checked in upper layer.  */
+  blockcnt = (len + pos - 1) / data->blksz + 1;
 
-  for (i = grub_divmod64 (pos, data->blksz, 0); i < blockcnt; i++)
+  for (i = pos / data->blksz; i < blockcnt; i++)
     {
       grub_disk_addr_t blknr;
       grub_off_t blockoff;
@@ -260,7 +261,7 @@ grub_hfs_read_file (struct grub_hfs_data *data,
 
       int skipfirst = 0;
 
-      grub_divmod64 (pos, data->blksz, &blockoff);
+      blockoff = pos % data->blksz;
 
       blknr = grub_hfs_block (data, data->extents, data->fileid, i, 1);
       if (grub_errno)
@@ -269,7 +270,7 @@ grub_hfs_read_file (struct grub_hfs_data *data,
       /* Last block.  */
       if (i == blockcnt - 1)
 	{
-	  grub_divmod64 ((len + pos), data->blksz, &blockend);
+	  blockend = (len + pos) % data->blksz;
 
 	  /* The last portion is exactly EXT2_BLOCK_SIZE (data).  */
 	  if (! blockend)
@@ -277,7 +278,7 @@ grub_hfs_read_file (struct grub_hfs_data *data,
 	}
 
       /* First block.  */
-      if (i == grub_divmod64 (pos, data->blksz, 0))
+      if (i == pos / data->blksz)
 	{
 	  skipfirst = blockoff;
 	  blockend -= skipfirst;
-- 
1.8.2.1