From b272392b5aaac606f658e995efebdc3923ceda20 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Mon, 1 Sep 2025 16:43:39 +0100 Subject: [PATCH] Merged bigendian code into array_to_integer --- lib.c | 18 +++++++++++++++--- lib.h | 2 +- ranlib.c | 25 ++++++------------------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib.c b/lib.c index aaf65d6..296df34 100644 --- 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 c8900eb..6a7df05 100644 --- 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); diff --git a/ranlib.c b/ranlib.c index a34cefb..ffc61fc 100644 --- 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); { -- 2.34.1