Merged bigendian code into array_to_integer master
authorRobert Pengelly <robertapengelly@hotmail.com>
Mon, 1 Sep 2025 15:43:39 +0000 (16:43 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Mon, 1 Sep 2025 15:43:39 +0000 (16:43 +0100)
lib.c
lib.h
ranlib.c

diff --git a/lib.c b/lib.c
index aaf65d6bcb1034e8a7c8887954be4a71f82eb62b..296df34ff50a1b01096f2e549247d24b20a673ff 100644 (file)
--- a/lib.c
+++ b/lib.c
@@ -54,13 +54,25 @@ char *xstrndup (const char *__p, unsigned long __size) {
 
 }
 
-unsigned long array_to_integer (unsigned char *arr, int size) {
+unsigned long array_to_integer (unsigned char *arr, int size, int bigendian) {
 
     unsigned long value = 0;
     int i;
     
-    for (i = 0; i < size; i++) {
-        value |= arr[i] << (CHAR_BIT * i);
+    if (bigendian) {
+    
+        int j;
+        
+        for (i = size, j = 0; i > 0; i--, j++) {
+            value |= arr[j] << (CHAR_BIT * (i - 1));
+        }
+    
+    } else {
+    
+        for (i = 0; i < size; i++) {
+            value |= arr[i] << (CHAR_BIT * i);
+        }
+    
     }
     
     return value;
diff --git a/lib.h b/lib.h
index c8900eba4228422c79b0059eabedaeb36e9def1a..6a7df05d76a09c8d71e7f047c15a91eb49b8cd7d 100644 (file)
--- a/lib.h
+++ b/lib.h
@@ -7,7 +7,7 @@
 char *xstrdup (const char *str);
 char *xstrndup (const char *__p, unsigned long __size);
 
-unsigned long array_to_integer (unsigned char *arr, int size);
+unsigned long array_to_integer (unsigned char *arr, int size, int bigendian);
 
 void *xmalloc (unsigned long size);
 void *xrealloc (void *ptr, unsigned long size);
index a34cefbd824a09d2581cc2a1e2eef2396d678d95..ffc61fca9ada96e19734234ebca82eb421a0ba3b 100644 (file)
--- a/ranlib.c
+++ b/ranlib.c
@@ -470,19 +470,6 @@ static void elf64_get_symbols (void *object, long offset, int endianess) {
 
 }
 
-unsigned long array_to_be_integer (unsigned char *arr, int size) {
-
-    unsigned long value = 0;
-    int i, j;
-    
-    for (i = size, j = 0; i > 0; i--, j++) {
-        value |= arr[j] << (CHAR_BIT * (i - 1));
-    }
-    
-    return value;
-
-}
-
 static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
 
     unsigned char *pos = (unsigned char *) object + 4;
@@ -490,7 +477,7 @@ static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
     
     struct strtab *strtab;
     
-    name_size = array_to_be_integer (pos, 4);
+    name_size = array_to_integer (pos, 4, 1);
     name_size *= 4;
     
     pos = pos + 4 + name_size;
@@ -514,7 +501,7 @@ static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
             
             if (pos[3] != 0xEB) {
             
-                size = array_to_be_integer (pos, 4);
+                size = array_to_integer (pos, 4, 1);
                 size *= 4;
                 
                 pos = pos + 4 + size;
@@ -535,7 +522,7 @@ static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
             
             while (1) {
             
-                name_size = array_to_be_integer (pos, 4);
+                name_size = array_to_integer (pos, 4, 1);
                 pos += 4;
                 
                 if (name_size == 0) {
@@ -572,7 +559,7 @@ static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
                         pos += 4;
                     }
                     
-                    num_ref = array_to_be_integer (pos, 4);
+                    num_ref = array_to_integer (pos, 4, 1);
                     num_ref *= 4;
                     
                     pos = pos + 4 + num_ref;
@@ -593,7 +580,7 @@ static void hunk_get_symbols (void *object, unsigned long bytes, long offset) {
             
             while (1) {
             
-                size = array_to_be_integer (pos, 4) * 4;
+                size = array_to_integer (pos, 4, 1) * 4;
                 pos += 4;
                 
                 if (size == 0) {
@@ -638,7 +625,7 @@ static void omf_get_symbols (void *object, char *filename, unsigned long bytes,
         big_fields = record_type & 1;
         record_type &= ~1;
         
-        record_len = array_to_be_integer (pos + 1, 2);
+        record_len = array_to_integer (pos + 1, 2, 1);
         
         {