Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | pmbaty | 1 | #include "bswap.h" |
2 | #include "harness/trace.h" |
||
3 | |||
4 | #define SWAP(A, B) do { (A) ^= (B); (B) ^= (A); (A) ^= (B); } while (0) |
||
5 | |||
6 | |||
7 | // IDA: br_uint_32 __cdecl BrSwap32(br_uint_32 l) |
||
8 | br_uint_32 BrSwap32(br_uint_32 l) { |
||
9 | union { // size: 0x4 |
||
10 | unsigned long l; // @0x0 |
||
11 | unsigned char c[4]; // @0x0 |
||
12 | } u; |
||
13 | LOG_TRACE10("(%d)", l); |
||
14 | |||
15 | u.l = l; |
||
16 | SWAP(u.c[0], u.c[3]); |
||
17 | SWAP(u.c[1], u.c[2]); |
||
18 | return u.l; |
||
19 | } |
||
20 | |||
21 | // IDA: br_uint_16 __cdecl BrSwap16(br_uint_16 s) |
||
22 | br_uint_16 BrSwap16(br_uint_16 s) { |
||
23 | union { // size: 0x2 |
||
24 | unsigned short s; // @0x0 |
||
25 | unsigned char c[2]; // @0x0 |
||
26 | } u; |
||
27 | LOG_TRACE("(%d)", s); |
||
28 | |||
29 | u.s = s; |
||
30 | SWAP(u.c[0], u.c[1]); |
||
31 | return u.s; |
||
32 | } |
||
33 | |||
34 | // IDA: br_float __cdecl BrSwapFloat(br_float f) |
||
35 | br_float BrSwapFloat(br_float f) { |
||
36 | union { // size: 0x4 |
||
37 | br_float f; // @0x0 |
||
38 | unsigned char c[4]; // @0x0 |
||
39 | } u; |
||
40 | LOG_TRACE("(%f)", f); |
||
41 | |||
42 | u.f = f; |
||
43 | SWAP(u.c[0], u.c[3]); |
||
44 | SWAP(u.c[1], u.c[2]); |
||
45 | return u.f; |
||
46 | } |
||
47 | |||
48 | |||
49 | // IDA: void* __cdecl BrSwapBlock(void *block, int count, int size) |
||
50 | void* BrSwapBlock(void* block, int count, int size) { |
||
51 | br_uint_8* cp; |
||
52 | int i; |
||
53 | int k; |
||
54 | LOG_TRACE9("(%p, %d, %d)", block, count, size); |
||
55 | |||
56 | cp = (br_uint_8*)block; |
||
57 | switch (size) { |
||
58 | case 1: |
||
59 | break; |
||
60 | case 2: |
||
61 | for (i = 0; i < count; i++) { |
||
62 | SWAP(cp[0], cp[1]); |
||
63 | cp += 2; |
||
64 | } |
||
65 | break; |
||
66 | case 4: |
||
67 | for (i = 0; i < count; i++) { |
||
68 | SWAP(cp[0], cp[3]); |
||
69 | SWAP(cp[1], cp[2]); |
||
70 | cp += 4; |
||
71 | } |
||
72 | break; |
||
73 | |||
74 | default: |
||
75 | for (i = 0; i < count; i++) { |
||
76 | for (k = 0; k < size / 2; k++) { |
||
77 | SWAP(cp[k], cp[size - k - 1]); |
||
78 | } |
||
79 | cp += size; |
||
80 | } |
||
81 | break; |
||
82 | } |
||
83 | return block; |
||
84 | } |