-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndianCvt.c
36 lines (32 loc) · 848 Bytes
/
EndianCvt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <stdint.h>
#ifndef _MSC_VER
#include <byteswap.h>
#define BSWAP32 bswap_32
#define BSWAP16 bswap_16
#else // Windows
#define BSWAP32 _byteswap_ulong
#define BSWAP16 _byteswap_ushort
#endif
#ifdef __cplusplus
extern "C" {
#endif
void RomToBigEndian(void* rom, size_t romSize) {
uint8_t firstbyte = ((uint8_t*)rom)[0];
switch (firstbyte) {
case 0x80: // BE
return; // Already BE, no need to swap
case 0x37:
for (size_t pos = 0; pos < (romSize / 2); pos++) {
((uint16_t*)rom)[pos] = BSWAP16(((uint16_t*)rom)[pos]);
}
return;
case 0x40:
for (size_t pos = 0; pos < (romSize / 4); pos++) {
((uint32_t*)rom)[pos] = BSWAP32(((uint32_t*)rom)[pos]);
}
}
}
#ifdef __cplusplus
}
#endif