Improve the speed of computing checksums for COFF binaries.
authorOleg Tolmatcev <oleg.tolmatcev@gmail.com>
Wed, 3 May 2023 14:36:43 +0000 (15:36 +0100)
committerNick Clifton <nickc@redhat.com>
Wed, 3 May 2023 14:36:43 +0000 (15:36 +0100)
 * coffcode.h (coff_read_word_from_buffer): New function.
 * coffcode.h (COFF_CHECKSUM_BUFFER_SIZE): New constant.
 * coffcode.h (coff_compute_checksum): Improve speed by reducing the number of seeks and reads used.

bfd/ChangeLog
bfd/coffcode.h

index 31e810e06256b5cb68c04318001f2ac96c1d98b3..61a488c348bc6f9c496dd51c67465aa12ae9ea18 100644 (file)
@@ -1,3 +1,10 @@
+2023-05-03  Oleg Tolmatcev  <oleg.tolmatcev@gmail.com>
+
+       * coffcode.h (coff_read_word_from_buffer): New function.
+       * coffcode.h (COFF_CHECKSUM_BUFFER_SIZE): New constant.
+       * coffcode.h (coff_compute_checksum): Improve speed by reducing
+       the number of seeks and reads used.
+
 2023-04-24  Nick Clifton  <nickc@redhat.com>
 
        * po/la.po: New Georgian translation.
index 96c53aa2b3447b39fc9b9ee41ab4b1a07a1ca532..974c8ad98543f7a5a1be2b2bc862ffc4d54bbe91 100644 (file)
@@ -3367,29 +3367,83 @@ coff_read_word (bfd *abfd, unsigned int *value, unsigned int *pelength)
   return true;
 }
 
+/* Read a two byte number from buffer B returning the result in VALUE.
+   No more than BUF_SIZE bytes will be read.
+   Returns true upobn success, false otherwise.
+   If successful, increases the value stored in PELENGTH by the number
+   of bytes read.  */
+
+static bool
+coff_read_word_from_buffer (unsigned char *  b,
+                           int              buf_size,
+                            unsigned int *   value,
+                           unsigned int *   pelength)
+{
+  if (buf_size < 1)
+    {
+      *value = 0;
+      return false;
+    }
+
+  if (buf_size == 1)
+    {
+      *value = (unsigned int)b[0];
+      *pelength += 1;
+    }
+  else
+    {
+      *value = (unsigned int)(b[0] + (b[1] << 8));
+      *pelength += 2;
+    }
+
+  return true;
+}
+
+#define COFF_CHECKSUM_BUFFER_SIZE 0x800000
+
 static unsigned int
 coff_compute_checksum (bfd *abfd, unsigned int *pelength)
 {
-  bool more_data;
   file_ptr filepos;
   unsigned int value;
   unsigned int total;
+  unsigned char *buf;
+  int buf_size;
 
   total = 0;
   *pelength = 0;
   filepos = (file_ptr) 0;
+  buf = (unsigned char *) bfd_malloc (COFF_CHECKSUM_BUFFER_SIZE);
+  if (buf == NULL)
+    return 0;
+  buf_size = 0;
 
   do
     {
+      unsigned char *cur_buf;
+      int cur_buf_size;
+
       if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
        return 0;
 
-      more_data = coff_read_word (abfd, &value, pelength);
-      total += value;
-      total = 0xffff & (total + (total >> 0x10));
-      filepos += 2;
+      buf_size = bfd_bread (buf, COFF_CHECKSUM_BUFFER_SIZE, abfd);
+      cur_buf_size = buf_size;
+      cur_buf = buf;
+
+      while (cur_buf_size > 0)
+        {
+          coff_read_word_from_buffer (cur_buf, cur_buf_size, &value, pelength);
+          cur_buf += 2;
+          cur_buf_size -= 2;
+          total += value;
+          total = 0xffff & (total + (total >> 0x10));
+        }
+
+      filepos += buf_size;
     }
-  while (more_data);
+  while (buf_size > 0);
+
+  free (buf);
 
   return (0xffff & (total + (total >> 0x10)));
 }