d2fcd91
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d2fcd91
From: Maxim Suhanov <dfirblog@gmail.com>
d2fcd91
Date: Tue, 3 Oct 2023 19:12:28 +0200
d2fcd91
Subject: [PATCH] fs/ntfs: Make code more readable
d2fcd91
d2fcd91
Move some calls used to access NTFS attribute header fields into
d2fcd91
functions with human-readable names.
d2fcd91
d2fcd91
Suggested-by: Daniel Kiper <daniel.kiper@oracle.com>
d2fcd91
Signed-off-by: Maxim Suhanov <dfirblog@gmail.com>
d2fcd91
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
d2fcd91
---
d2fcd91
 grub-core/fs/ntfs.c | 48 +++++++++++++++++++++++++++++++++---------------
d2fcd91
 1 file changed, 33 insertions(+), 15 deletions(-)
d2fcd91
d2fcd91
diff --git a/grub-core/fs/ntfs.c b/grub-core/fs/ntfs.c
d2fcd91
index 32ba8276dd8d..991b1c2094f5 100644
d2fcd91
--- a/grub-core/fs/ntfs.c
d2fcd91
+++ b/grub-core/fs/ntfs.c
d2fcd91
@@ -52,6 +52,24 @@ u64at (void *ptr, grub_size_t ofs)
d2fcd91
   return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
d2fcd91
 }
d2fcd91
 
d2fcd91
+static grub_uint16_t
d2fcd91
+first_attr_off (void *mft_buf_ptr)
d2fcd91
+{
d2fcd91
+  return u16at (mft_buf_ptr, 0x14);
d2fcd91
+}
d2fcd91
+
d2fcd91
+static grub_uint16_t
d2fcd91
+res_attr_data_off (void *res_attr_ptr)
d2fcd91
+{
d2fcd91
+  return u16at (res_attr_ptr, 0x14);
d2fcd91
+}
d2fcd91
+
d2fcd91
+static grub_uint32_t
d2fcd91
+res_attr_data_len (void *res_attr_ptr)
d2fcd91
+{
d2fcd91
+  return u32at (res_attr_ptr, 0x10);
d2fcd91
+}
d2fcd91
+
d2fcd91
 grub_ntfscomp_func_t grub_ntfscomp_func;
d2fcd91
 
d2fcd91
 static grub_err_t
d2fcd91
@@ -106,7 +124,7 @@ init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
d2fcd91
 {
d2fcd91
   at->mft = mft;
d2fcd91
   at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
d2fcd91
-  at->attr_nxt = mft->buf + u16at (mft->buf, 0x14);
d2fcd91
+  at->attr_nxt = mft->buf + first_attr_off (mft->buf);
d2fcd91
   at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
d2fcd91
 }
d2fcd91
 
d2fcd91
@@ -154,7 +172,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
d2fcd91
 		    return NULL;
d2fcd91
 		}
d2fcd91
 
d2fcd91
-	      new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)];
d2fcd91
+	      new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
d2fcd91
 	      while (*new_pos != 0xFF)
d2fcd91
 		{
d2fcd91
 		  if ((*new_pos == *at->attr_cur)
d2fcd91
@@ -213,7 +231,7 @@ find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
d2fcd91
 	}
d2fcd91
       else
d2fcd91
 	{
d2fcd91
-	  at->attr_nxt = at->attr_end + u16at (pa, 0x14);
d2fcd91
+	  at->attr_nxt = at->attr_end + res_attr_data_off (pa);
d2fcd91
 	  at->attr_end = at->attr_end + u32at (pa, 4);
d2fcd91
 	  pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
d2fcd91
 	}
d2fcd91
@@ -399,20 +417,20 @@ read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
d2fcd91
 
d2fcd91
   if (pa[8] == 0)
d2fcd91
     {
d2fcd91
-      if (ofs + len > u32at (pa, 0x10))
d2fcd91
+      if (ofs + len > res_attr_data_len (pa))
d2fcd91
 	return grub_error (GRUB_ERR_BAD_FS, "read out of range");
d2fcd91
 
d2fcd91
-      if (u32at (pa, 0x10) > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
d2fcd91
+      if (res_attr_data_len (pa) > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
d2fcd91
 	return grub_error (GRUB_ERR_BAD_FS, "resident attribute too large");
d2fcd91
 
d2fcd91
       if (pa >= at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
d2fcd91
 	return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
d2fcd91
 
d2fcd91
-      if (u16at (pa, 0x14) + u32at (pa, 0x10) >
d2fcd91
+      if (res_attr_data_off (pa) + res_attr_data_len (pa) >
d2fcd91
 	  (grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) pa)
d2fcd91
 	return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
d2fcd91
 
d2fcd91
-      grub_memcpy (dest, pa + u16at (pa, 0x14) + ofs, len);
d2fcd91
+      grub_memcpy (dest, pa + res_attr_data_off (pa) + ofs, len);
d2fcd91
       return 0;
d2fcd91
     }
d2fcd91
 
d2fcd91
@@ -556,7 +574,7 @@ init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
d2fcd91
 			   (unsigned long long) mftno);
d2fcd91
 
d2fcd91
       if (!pa[8])
d2fcd91
-	mft->size = u32at (pa, 0x10);
d2fcd91
+	mft->size = res_attr_data_len (pa);
d2fcd91
       else
d2fcd91
 	mft->size = u64at (pa, 0x30);
d2fcd91
 
d2fcd91
@@ -801,7 +819,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
d2fcd91
 	  (u32at (cur_pos, 0x18) != 0x490024) ||
d2fcd91
 	  (u32at (cur_pos, 0x1C) != 0x300033))
d2fcd91
 	continue;
d2fcd91
-      cur_pos += u16at (cur_pos, 0x14);
d2fcd91
+      cur_pos += res_attr_data_off (cur_pos);
d2fcd91
       if (*cur_pos != 0x30)	/* Not filename index */
d2fcd91
 	continue;
d2fcd91
       break;
d2fcd91
@@ -830,7 +848,7 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
d2fcd91
 	{
d2fcd91
           int is_resident = (cur_pos[8] == 0);
d2fcd91
 
d2fcd91
-          bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) :
d2fcd91
+          bitmap_len = ((is_resident) ? res_attr_data_len (cur_pos) :
d2fcd91
                         u32at (cur_pos, 0x28));
d2fcd91
 
d2fcd91
           bmp = grub_malloc (bitmap_len);
d2fcd91
@@ -851,14 +869,14 @@ grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
d2fcd91
 		  goto done;
d2fcd91
 		}
d2fcd91
 
d2fcd91
-              if (u16at (cur_pos, 0x14) + u32at (cur_pos, 0x10) >
d2fcd91
+              if (res_attr_data_off (cur_pos) + res_attr_data_len (cur_pos) >
d2fcd91
 		  (grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) cur_pos)
d2fcd91
 		{
d2fcd91
 		  grub_error (GRUB_ERR_BAD_FS, "resident bitmap out of range");
d2fcd91
 		  goto done;
d2fcd91
 		}
d2fcd91
 
d2fcd91
-              grub_memcpy (bmp, cur_pos + u16at (cur_pos, 0x14),
d2fcd91
+              grub_memcpy (bmp, cur_pos + res_attr_data_off (cur_pos),
d2fcd91
                            bitmap_len);
d2fcd91
 	    }
d2fcd91
           else
d2fcd91
@@ -1222,12 +1240,12 @@ grub_ntfs_label (grub_device_t device, char **label)
d2fcd91
       goto fail;
d2fcd91
     }
d2fcd91
 
d2fcd91
-  if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
d2fcd91
+  if ((pa) && (pa[8] == 0) && (res_attr_data_len (pa)))
d2fcd91
     {
d2fcd91
       int len;
d2fcd91
 
d2fcd91
-      len = u32at (pa, 0x10) / 2;
d2fcd91
-      pa += u16at (pa, 0x14);
d2fcd91
+      len = res_attr_data_len (pa) / 2;
d2fcd91
+      pa += res_attr_data_off (pa);
d2fcd91
       if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa >= 2 * len)
d2fcd91
         *label = get_utf8 (pa, len);
d2fcd91
       else