forked from sebastianbiallas/ht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endianess.cc
215 lines (207 loc) · 4.95 KB
/
endianess.cc
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* HT Editor
* endianess.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <cstring>
#include "htdebug.h"
#include "endianess.h"
#include "io/types.h"
void createForeignInt(void *buf, int i, int size, Endianess to_endianess)
{
uint8 *b = (uint8*)buf;
switch (size) {
case 1: b[0] = i; break;
case 2:
switch (to_endianess) {
case big_endian:
b[0] = i>>8;
b[1] = i;
break;
case little_endian:
b[0] = i;
b[1] = i>>8;
break;
}
break;
case 4:
switch (to_endianess) {
case big_endian:
b[0] = i>>24;
b[1] = i>>16;
b[2] = i>>8;
b[3] = i;
break;
case little_endian:
b[0] = i;
b[1] = i>>8;
b[2] = i>>16;
b[3] = i>>24;
break;
}
break;
default: ASSERT(0);
}
}
int createHostInt(const void *buf, int size, Endianess from_endianess)
{
uint8 *b = (uint8*)buf;
switch (size) {
case 1:
return b[0];
case 2:
switch (from_endianess) {
case big_endian:
return (b[0]<<8) | b[1];
case little_endian:
return (b[1]<<8) | b[0];
}
break;
case 4:
switch (from_endianess) {
case big_endian:
return (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3];
case little_endian:
return (b[3]<<24) | (b[2]<<16) | (b[1]<<8) | b[0];
}
break;
}
ASSERT(0);
return 0;
}
void createForeignInt64(void *buf, uint64 i, int size, Endianess to_endianess)
{
if (size <= 4) {
return createForeignInt(buf, i, size, to_endianess);
}
uint8 *b = (uint8*)buf;
uint32 hi = i >> 32;
uint32 lo = i;
switch (to_endianess) {
case big_endian:
b[0] = hi>>24;
b[1] = hi>>16;
b[2] = hi>>8;
b[3] = hi;
b[4] = lo>>24;
b[5] = lo>>16;
b[6] = lo>>8;
b[7] = lo;
break;
case little_endian:
b[0] = lo;
b[1] = lo>>8;
b[2] = lo>>16;
b[3] = lo>>24;
b[4] = hi;
b[5] = hi>>8;
b[6] = hi>>16;
b[7] = hi>>24;
break;
default: ASSERT(0);
}
}
uint64 createHostInt64(const void *buf, int size, Endianess from_endianess)
{
if (size <= 4) {
return createHostInt(buf, size, from_endianess);
}
uint8 *b = (uint8*)buf;
uint64 q;
switch (from_endianess) {
case big_endian:
q = uint64(b[0])<<56 | uint64(b[1])<<48 | uint64(b[2])<<40 | uint64(b[3])<<32
| uint64(b[4])<<24 | uint64(b[5])<<16 | uint64(b[6])<<8 | uint64(b[7]);
break;
case little_endian:
q = uint64(b[7])<<56 | uint64(b[6])<<48 | uint64(b[5])<<40 | uint64(b[4])<<32
| uint64(b[3])<<24 | uint64(b[2])<<16 | uint64(b[1])<<8 | uint64(b[0]);
break;
default: ASSERT(0);
}
return q;
}
void createHostStruct(void *buf, const uint8 *table, Endianess from_endianess)
{
uint8 *buf2 = (uint8*)buf;
while (*table) {
int table2 = *table & ~STRUCT_ENDIAN_HOST;
if (*table & STRUCT_ENDIAN_HOST) {
switch (table2) {
case STRUCT_ENDIAN_8: {
uint8 a = createHostInt(buf2, STRUCT_ENDIAN_8, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_8);
break;
}
case STRUCT_ENDIAN_16: {
uint16 a = createHostInt(buf2, STRUCT_ENDIAN_16, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_16);
break;
}
case STRUCT_ENDIAN_32: {
uint32 a = createHostInt(buf2, STRUCT_ENDIAN_32, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_32);
break;
}
case STRUCT_ENDIAN_64: {
uint64 q = createHostInt64(buf2, STRUCT_ENDIAN_64, from_endianess);
memcpy(buf2, &q, STRUCT_ENDIAN_64);
break;
}
default: ASSERT(0);
}
}
buf2 += table2;
table++;
}
}
void createHostStructx(void *buf, uint bufsize, const uint8 *table, Endianess from_endianess)
{
uint8 *buf2 = (uint8*)buf;
while (*table) {
if (bufsize) ASSERT((uint)(buf2 - (uint8*)buf) < bufsize);
int table2 = *table & ~STRUCT_ENDIAN_HOST;
if (*table & STRUCT_ENDIAN_HOST) {
switch (table2) {
case STRUCT_ENDIAN_8: {
uint8 a = createHostInt(buf2, STRUCT_ENDIAN_8, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_8);
break;
}
case STRUCT_ENDIAN_16: {
uint16 a = createHostInt(buf2, STRUCT_ENDIAN_16, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_16);
break;
}
case STRUCT_ENDIAN_32: {
uint32 a = createHostInt(buf2, STRUCT_ENDIAN_32, from_endianess);
memcpy(buf2, &a, STRUCT_ENDIAN_32);
break;
}
case STRUCT_ENDIAN_64: {
uint64 q = createHostInt64(buf2, STRUCT_ENDIAN_64, from_endianess);
memcpy(buf2, &q, STRUCT_ENDIAN_64);
break;
}
default: ASSERT(0);
}
}
buf2 += table2;
table++;
}
if (bufsize) ASSERT((uint)(buf2 - (uint8*)buf) == bufsize);
}