1 | #include "../include/Endianness.h" |
---|
2 | #include <iostream> |
---|
3 | |
---|
4 | namespace environment { |
---|
5 | namespace endianness { |
---|
6 | |
---|
7 | /* |
---|
8 | * swapBytes -> change the endiannes |
---|
9 | * exemple for 16 and size_access = 4, [0123456789abcdef] -> [32107654ba98fedc] |
---|
10 | */ |
---|
11 | char * swapBytes (char * string_src , uint32_t size_string, uint32_t size_access) |
---|
12 | { |
---|
13 | char string_tmp [size_string]; |
---|
14 | |
---|
15 | if ( (size_access == 1) or |
---|
16 | (size_access > size_string)) |
---|
17 | return string_src; |
---|
18 | |
---|
19 | // if (size_string % size_access != 0) |
---|
20 | // { |
---|
21 | // std::cerr << "<swapBytes> : Size of string must be a multiple of size_access." << std::endl; |
---|
22 | // exit(1); |
---|
23 | // } |
---|
24 | |
---|
25 | // size_access : |
---|
26 | // 1 -> bytes |
---|
27 | // 2 -> half |
---|
28 | // 4 -> word |
---|
29 | // 8 -> doubleword |
---|
30 | |
---|
31 | memcpy (string_tmp,string_src,size_string); |
---|
32 | |
---|
33 | // Loop to reorganize the string |
---|
34 | for (uint32_t i = 0 ; i < size_string; i += size_access) |
---|
35 | { |
---|
36 | switch (size_access) |
---|
37 | { |
---|
38 | case 2 : |
---|
39 | { |
---|
40 | string_src[i+0] = string_tmp[i+1]; |
---|
41 | string_src[i+1] = string_tmp[i+0]; |
---|
42 | break; |
---|
43 | } |
---|
44 | case 4 : |
---|
45 | { |
---|
46 | string_src[i+0] = string_tmp[i+3]; |
---|
47 | string_src[i+1] = string_tmp[i+2]; |
---|
48 | string_src[i+2] = string_tmp[i+1]; |
---|
49 | string_src[i+3] = string_tmp[i+0]; |
---|
50 | break; |
---|
51 | } |
---|
52 | case 8 : |
---|
53 | { |
---|
54 | string_src[i+0] = string_tmp[i+7]; |
---|
55 | string_src[i+1] = string_tmp[i+6]; |
---|
56 | string_src[i+2] = string_tmp[i+5]; |
---|
57 | string_src[i+3] = string_tmp[i+4]; |
---|
58 | string_src[i+4] = string_tmp[i+3]; |
---|
59 | string_src[i+5] = string_tmp[i+2]; |
---|
60 | string_src[i+6] = string_tmp[i+1]; |
---|
61 | string_src[i+7] = string_tmp[i+0]; |
---|
62 | break; |
---|
63 | } |
---|
64 | default : |
---|
65 | { |
---|
66 | std::cerr << "<swapBytes> : Illegal size of access (size_access = " << size_access << ")" << std::endl; |
---|
67 | exit(1); |
---|
68 | break; |
---|
69 | } |
---|
70 | }//end switch(size_access) |
---|
71 | }//end i |
---|
72 | |
---|
73 | return string_src; |
---|
74 | } |
---|
75 | |
---|
76 | }; |
---|
77 | }; |
---|