/[pearpc]/src/tools/endianess.cc
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /src/tools/endianess.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Wed Sep 5 17:11:21 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 4265 byte(s)
import upstream CVS
1 /*
2 * HT Editor
3 * endianess.cc
4 *
5 * Copyright (C) 1999-2002 Stefan Weyergraf
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <cstring>
22
23 #include "debug.h"
24 #include "endianess.h"
25 #include "system/types.h"
26
27 extern "C" void createForeignInt(void *buf, int i, int size, Endianess to_endianess)
28 {
29 uint8 *b = (uint8*)buf;
30 switch (size) {
31 case 1: b[0] = i; break;
32 case 2:
33 switch (to_endianess) {
34 case big_endian:
35 b[0] = i>>8;
36 b[1] = i;
37 break;
38 case little_endian:
39 b[0] = i;
40 b[1] = i>>8;
41 break;
42 }
43 break;
44 case 4:
45 switch (to_endianess) {
46 case big_endian:
47 b[0] = i>>24;
48 b[1] = i>>16;
49 b[2] = i>>8;
50 b[3] = i;
51 break;
52 case little_endian:
53 b[0] = i;
54 b[1] = i>>8;
55 b[2] = i>>16;
56 b[3] = i>>24;
57 break;
58 }
59 break;
60 default: ASSERT(0);
61 }
62 }
63
64 extern "C" int createHostInt(const void *buf, int size, Endianess from_endianess)
65 {
66 uint8 *b = (uint8*)buf;
67 switch (size) {
68 case 1:
69 return b[0];
70 case 2:
71 switch (from_endianess) {
72 case big_endian:
73 return (b[0]<<8) | b[1];
74 case little_endian:
75 return (b[1]<<8) | b[0];
76 }
77 break;
78 case 4:
79 switch (from_endianess) {
80 case big_endian:
81 return (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3];
82 case little_endian:
83 return (b[3]<<24) | (b[2]<<16) | (b[1]<<8) | b[0];
84 }
85 break;
86 }
87 ASSERT(0);
88 return 0;
89 }
90
91 extern "C" void createForeignInt64(void *buf, uint64 i, int size, Endianess to_endianess)
92 {
93 if (size <= 4) {
94 return createForeignInt(buf, i, size, to_endianess);
95 }
96 uint8 *b = (uint8*)buf;
97 uint32 hi = i >> 32;
98 uint32 lo = i;
99 switch (to_endianess) {
100 case big_endian:
101 b[0] = hi>>24;
102 b[1] = hi>>16;
103 b[2] = hi>>8;
104 b[3] = hi;
105 b[4] = lo>>24;
106 b[5] = lo>>16;
107 b[6] = lo>>8;
108 b[7] = lo;
109 break;
110 case little_endian:
111 b[0] = lo;
112 b[1] = lo>>8;
113 b[2] = lo>>16;
114 b[3] = lo>>24;
115 b[4] = hi;
116 b[5] = hi>>8;
117 b[6] = hi>>16;
118 b[7] = hi>>24;
119 break;
120 default: ASSERT(0);
121 }
122 }
123
124 extern "C" uint64 createHostInt64(const void *buf, int size, Endianess from_endianess)
125 {
126 if (size <= 4) {
127 return createHostInt(buf, size, from_endianess);
128 }
129 uint8 *b = (uint8*)buf;
130 uint64 q;
131 switch (from_endianess) {
132 case big_endian:
133 q = (((uint64)(b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]) << 32) |
134 (b[4]<<24) | (b[5]<<16) | (b[6]<<8) | b[7];
135 break;
136 case little_endian:
137 q = (((uint64)(b[7]<<24) | (b[6]<<16) | (b[5]<<8) | b[4]) << 32) |
138 (b[3]<<24) | (b[2]<<16) | (b[1]<<8) | b[0];
139 default: ASSERT(0);
140 }
141 return q;
142 }
143
144 extern "C" void createHostStructx(void *buf, uint bufsize, const uint8 *table, Endianess from_endianess)
145 {
146 uint8 *buf2 = (uint8*)buf;
147 while (*table) {
148 if (bufsize) ASSERT(buf2 - (uint8*)buf < bufsize);
149 int table2 = *table & ~STRUCT_ENDIAN_HOST;
150 if (*table & STRUCT_ENDIAN_HOST) {
151 switch (table2) {
152 case STRUCT_ENDIAN_8: {
153 uint8 a = createHostInt(buf2, STRUCT_ENDIAN_8, from_endianess);
154 memcpy(buf2, &a, STRUCT_ENDIAN_8);
155 break;
156 }
157 case STRUCT_ENDIAN_16: {
158 uint16 a = createHostInt(buf2, STRUCT_ENDIAN_16, from_endianess);
159 memcpy(buf2, &a, STRUCT_ENDIAN_16);
160 break;
161 }
162 case STRUCT_ENDIAN_32: {
163 uint32 a = createHostInt(buf2, STRUCT_ENDIAN_32, from_endianess);
164 memcpy(buf2, &a, STRUCT_ENDIAN_32);
165 break;
166 }
167 case STRUCT_ENDIAN_64: {
168 uint64 q = createHostInt64(buf2, STRUCT_ENDIAN_64, from_endianess);
169 memcpy(buf2, &q, STRUCT_ENDIAN_64);
170 break;
171 }
172 default: ASSERT(0);
173 }
174 }
175 buf2 += table2;
176 table++;
177 }
178 if (bufsize) ASSERT(buf2 - (uint8*)buf == bufsize);
179 }

  ViewVC Help
Powered by ViewVC 1.1.26