Skip to content

Commit aa7c55a

Browse files
committed
Refactor: internalize endianness macros and simplify MD5 implementation
- oapv_port.h: Introduce cross-platform endianness swap and conversion macros (OAPV_SWAP*, OAPV_LE32_TO_CPU). - oapv_util.c: Replace manual byte swapping with new macros in MD5 transform to improve readability and maintainability. - Standardize endianness handling across the codebase. Signed-off-by: Anemptyship <hanbin1931@gmail.com>
1 parent 2d70d27 commit aa7c55a

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

src/oapv_port.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,51 @@ static __inline void oapv_mset_16b(s16 *dst, s16 v, int cnt)
203203
/* CPU information */
204204
int oapv_get_num_cpu_cores(void);
205205

206+
/* Endianness handling */
207+
#if defined(__GNUC__) || defined(__clang__)
208+
#define OAPV_SWAP16(x) __builtin_bswap16(x)
209+
#define OAPV_SWAP32(x) __builtin_bswap32(x)
210+
#define OAPV_SWAP64(x) __builtin_bswap64(x)
211+
#elif defined(_MSC_VER)
212+
#define OAPV_SWAP16(x) _byteswap_ushort(x)
213+
#define OAPV_SWAP32(x) _byteswap_ulong(x)
214+
#define OAPV_SWAP64(x) _byteswap_uint64(x)
215+
#else
216+
#define OAPV_SWAP16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
217+
#define OAPV_SWAP32(x) ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
218+
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
219+
#define OAPV_SWAP64(x) ((((x) & 0xff00000000000000ull) >> 56) | \
220+
(((x) & 0x00ff000000000000ull) >> 40) | \
221+
(((x) & 0x0000ff0000000000ull) >> 24) | \
222+
(((x) & 0x000000ff00000000ull) >> 8) | \
223+
(((x) & 0x00000000ff000000ull) << 8) | \
224+
(((x) & 0x0000000000ff0000ull) << 24) | \
225+
(((x) & 0x000000000000ff00ull) << 40) | \
226+
(((x) & 0x00000000000000ffull) << 56))
227+
#endif
228+
229+
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
230+
#define OAPV_BIG_ENDIAN 1
231+
#define OAPV_CPU_TO_LE32(x) OAPV_SWAP32(x)
232+
#define OAPV_CPU_TO_LE64(x) OAPV_SWAP64(x)
233+
#define OAPV_LE32_TO_CPU(x) OAPV_SWAP32(x)
234+
#define OAPV_LE64_TO_CPU(x) OAPV_SWAP64(x)
235+
#define OAPV_CPU_TO_BE32(x) (x)
236+
#define OAPV_CPU_TO_BE64(x) (x)
237+
#define OAPV_BE32_TO_CPU(x) (x)
238+
#define OAPV_BE64_TO_CPU(x) (x)
239+
#else
240+
// Little Endian (Default for x86/ARM)
241+
#define OAPV_LITTLE_ENDIAN 1
242+
#define OAPV_CPU_TO_LE32(x) (x)
243+
#define OAPV_CPU_TO_LE64(x) (x)
244+
#define OAPV_LE32_TO_CPU(x) (x)
245+
#define OAPV_LE64_TO_CPU(x) (x)
246+
#define OAPV_CPU_TO_BE32(x) OAPV_SWAP32(x)
247+
#define OAPV_CPU_TO_BE64(x) OAPV_SWAP64(x)
248+
#define OAPV_BE32_TO_CPU(x) OAPV_SWAP32(x)
249+
#define OAPV_BE64_TO_CPU(x) OAPV_SWAP64(x)
250+
#endif
251+
206252
#endif /* _OAPV_PORT_H_ */
207253

src/oapv_util.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,16 @@ static void md5_trans(u32 *buf, const u8 *msg)
4444
{
4545
register u32 a, b, c, d;
4646

47-
#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
48-
const u32 *blk = (const u32 *)msg;
49-
#else
47+
#if OAPV_BIG_ENDIAN
5048
u32 x[16];
5149
int i;
52-
50+
const u32 *ptr = (const u32 *)msg;
5351
for (i = 0; i < 16; i++) {
54-
x[i] = ((u32)msg[i*4+0]) | (((u32)msg[i*4+1]) << 8) |
55-
(((u32)msg[i*4+2]) << 16) | (((u32)msg[i*4+3]) << 24);
52+
x[i] = OAPV_LE32_TO_CPU(ptr[i]);
5653
}
5754
const u32 *blk = x;
55+
#else // Little Endian
56+
const u32 *blk = (const u32 *)msg;
5857
#endif
5958

6059
a = buf[0];

0 commit comments

Comments
 (0)