TPCCLIB
Loading...
Searching...
No Matches
endian.c
Go to the documentation of this file.
1
5/*****************************************************************************/
6
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <time.h>
13#include <string.h>
14#include <ctype.h>
15#include <unistd.h>
16#include <math.h>
17#include "tpcextensions.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
27{
28 union {
29 uint32_t value;
30 uint8_t data[sizeof(uint32_t)];
31 } number;
32
33 number.data[0] = 0x00;
34 number.data[1] = 0x01;
35 number.data[2] = 0x02;
36 number.data[3] = 0x03;
37
38 switch (number.value) {
39 case UINT32_C(0x00010203): return ENDIAN_BIG;
40 case UINT32_C(0x03020100): return ENDIAN_LITTLE;
41 case UINT32_C(0x02030001): return ENDIAN_BIG_WORD;
42 case UINT32_C(0x01000302): return ENDIAN_LITTLE_WORD;
43 default: return ENDIAN_UNKNOWN;
44 }
45}
46/*****************************************************************************/
47
48/*****************************************************************************/
54{
55 //int x=1;
56 //if(*(char *)&x==1) return(1); else return(0);
57 if(endianness()==ENDIAN_LITTLE) return(1); else return(0);
58}
59/*****************************************************************************/
60
61/*****************************************************************************/
69void swap(
71 void *from,
73 void *to,
75 int size
76) {
77 if(from==NULL || to==NULL) return;
78
79 unsigned char c;
80 unsigned short int s;
81 unsigned long l;
82
83 switch(size) {
84 case 1:
85 *(char *)to=*(char *)from;
86 break;
87 case 2:
88 c=*(unsigned char *)from;
89 *(unsigned char *)to = *((unsigned char *)from+1);
90 *((unsigned char *)to+1) = c;
91 /*swab(from, to, size); // NOT ANSI */
92 break;
93 case 4:
94 s=*(unsigned short *)from;
95 *(unsigned short *)to = *((unsigned short *)from+1);
96 *((unsigned short *)to+1) = s;
97 swap((char*)to, (char*)to, 2);
98 swap((char*)((unsigned short *)to+1), (char*)((unsigned short *)to+1), 2);
99 break;
100 case 8:
101 l=*(unsigned long *)from;
102 *(unsigned long *)to = *((unsigned long *)from+1);
103 *((unsigned long *)to+1) = l;
104 swap((char *)to, (char *)to, 4);
105 swap((char*)((unsigned long *)to+1), (char*)((unsigned long *)to+1), 4);
106 break;
107 }
108}
109/*****************************************************************************/
110
111/*****************************************************************************/
117 void *buf,
119 int size
120) {
121 if(buf==NULL) return;
122
123 int i;
124 unsigned char c;
125
126 for(i=1; i<size; i+=2) {
127 c=*((unsigned char *)buf+i);
128 *((unsigned char *)buf+i)=*((unsigned char *)buf+(i-1));
129 *((unsigned char *)buf+(i-1))=c;
130 }
131}
132/*****************************************************************************/
133
134/*****************************************************************************/
140 void *buf,
142 int size
143) {
144 if(buf==NULL) return;
145
146 int i;
147 unsigned char c, *cptr;
148
149 cptr=(unsigned char*)buf;
150 for(i=0; i<size; i+=4, cptr+=4) {
151 c=cptr[0]; cptr[0]=cptr[3]; cptr[3]=c;
152 c=cptr[1]; cptr[1]=cptr[2]; cptr[2]=c;
153 }
154}
155/*****************************************************************************/
156
157/*****************************************************************************/
163 void *buf,
165 int size
166) {
167 if(buf==NULL) return;
168
169 int i;
170 unsigned short int s, *sptr;
171
172 sptr=(unsigned short int*)buf;
173 for(i=0; i<size; i+=4, sptr+=2) {
174 s=sptr[0]; sptr[0]=sptr[1]; sptr[1]=s;
175 }
176}
177/*****************************************************************************/
178
179/*****************************************************************************/
186 void *buf,
188 unsigned long long size
189) {
190 if(buf==NULL || size==0) return;
191
192 unsigned long long int i;
193 unsigned char c, *cptr;
194
195 cptr=(unsigned char*)buf;
196 for(i=0; i<size; i++, cptr+=8) {
197 c=cptr[0]; cptr[0]=cptr[7]; cptr[7]=c;
198 c=cptr[1]; cptr[1]=cptr[6]; cptr[6]=c;
199 c=cptr[2]; cptr[2]=cptr[5]; cptr[5]=c;
200 c=cptr[3]; cptr[3]=cptr[4]; cptr[4]=c;
201 }
202}
203/*****************************************************************************/
204
205/*****************************************************************************/
212 void *buf,
214 unsigned long long size
215) {
216 if(buf==NULL || size==0) return;
217
218 unsigned long long int i;
219 unsigned char c, *cptr;
220
221 cptr=(unsigned char*)buf;
222 for(i=0; i<size; i++, cptr+=4) {
223 c=cptr[0]; cptr[0]=cptr[3]; cptr[3]=c;
224 c=cptr[1]; cptr[1]=cptr[2]; cptr[2]=c;
225 }
226}
227/*****************************************************************************/
228
229/*****************************************************************************/
236 void *buf,
238 unsigned long long size
239) {
240 if(buf==NULL || size==0) return;
241
242 unsigned long long int i;
243 unsigned char c, *cptr;
244
245 cptr=(unsigned char*)buf;
246 for(i=0; i<size; i++, cptr+=2) {
247 c=cptr[0]; cptr[0]=cptr[1]; cptr[1]=c;
248 }
249}
250/*****************************************************************************/
251
252/*****************************************************************************/
void swap32ip(void *buf, unsigned long long size)
Definition endian.c:210
void swap64ip(void *buf, unsigned long long size)
Definition endian.c:184
void swabip(void *buf, int size)
Definition endian.c:115
int endianLittle()
Definition endian.c:53
void swawip(void *buf, int size)
Definition endian.c:161
void swap16ip(void *buf, unsigned long long size)
Definition endian.c:234
int endianness()
Definition endian.c:26
void swap(void *from, void *to, int size)
Definition endian.c:69
void swawbip(void *buf, int size)
Definition endian.c:138
Header file for library libtpcextensions.