TPCCLIB
Loading...
Searching...
No Matches
endian.c File Reference

Byte swapping between little and big endian. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int endianness ()
 
int endianLittle ()
 
void swap (void *from, void *to, int size)
 
void swabip (void *buf, int size)
 
void swawbip (void *buf, int size)
 
void swawip (void *buf, int size)
 
void swap64ip (void *buf, unsigned long long size)
 
void swap32ip (void *buf, unsigned long long size)
 
void swap16ip (void *buf, unsigned long long size)
 

Detailed Description

Byte swapping between little and big endian.

Author
Vesa Oikonen

Definition in file endian.c.

Function Documentation

◆ endianLittle()

int endianLittle ( )

Check whether current platform uses little endian byte order.

See also
swap, swabip, swawbip, swawip
Returns
Returns 1, if current platform is little endian, and 0 if not.

Definition at line 53 of file endian.c.

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}
int endianness()
Definition endian.c:26

Referenced by anaReadHeader(), dcmFileWrite(), dcmitemGetInt(), dcmitemGetReal(), dcmReadFileTag(), dcmReadFileVL(), dcmReadFileVRVL(), dcmValueString(), dcmWriteFileTag(), dcmWriteFileVRVL(), ecatMListRead(), ecatReadMainheader(), ecatWriteMainheader(), imgReadNifti(), imgSetNiftiHeader(), niftiReadHeader(), and niftiWriteHeader().

◆ endianness()

int endianness ( )

Detect endianness at run-time. From https://sourceforge.net/p/predef/wiki/Endianness/

Returns
Returns ENDIAN_UNKNOWN, ENDIAN_BIG, ENDIAN_LITTLE, ENDIAN_BIG_WORD, or ENDIAN_LITTLE_WORD.

Definition at line 26 of file endian.c.

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}

Referenced by endianLittle().

◆ swabip()

void swabip ( void * buf,
int size )

In-place swab, replaces the non-ANSI function swab(), which may not work if data is overlapping.

See also
swawbip, swawip, swap, endianLittle
Parameters
bufPointer to memory
sizeSize of buf in bytes

Definition at line 115 of file endian.c.

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}

Referenced by anaReadHeader(), dcmFileWrite(), dcmWriteFileTag(), niftiReadHeader(), and niftiWriteHeader().

◆ swap()

void swap ( void * from,
void * to,
int size )

Swap the specified short int, int, long int, float, or double from little endian to big endian or vice versa.

Arguments are allowed to overlap.

See also
swawbip, swabip, swawip, endianLittle
Parameters
fromPointer to a short int, int, long int, float, or double variable.
toPointer to a short int, int, long int, float, or double variable.
sizeSize of from and to (byte nr) must be 1, 2, 4 or 8.

Definition at line 69 of file endian.c.

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}
void swap(void *from, void *to, int size)
Definition endian.c:69

Referenced by dcmitemGetInt(), dcmitemGetReal(), dcmReadFileTag(), dcmReadFileVL(), dcmReadFileVRVL(), dcmValueString(), dcmWriteFileVRVL(), and swap().

◆ swap16ip()

void swap16ip ( void * buf,
unsigned long long size )

In-place big-little endian swapping of an array of 16 bit (2 byte) integers.

See also
swap32ip, swap64ip, endianLittle
Parameters
bufPointer to memory.
sizeNr of 16 bit values in the array.

Definition at line 234 of file endian.c.

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}

Referenced by imgReadNifti(), niftiReadHeader(), and niftiWriteHeader().

◆ swap32ip()

void swap32ip ( void * buf,
unsigned long long size )

In-place big-little endian swapping of an array of 32 bit (4 byte) integers or floating point values.

See also
swap16ip, swap64ip, endianLittle
Parameters
bufPointer to memory.
sizeNr of 32 bit values in the array.

Definition at line 210 of file endian.c.

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}

Referenced by imgReadNifti(), niftiReadHeader(), and niftiWriteHeader().

◆ swap64ip()

void swap64ip ( void * buf,
unsigned long long size )

In-place big-little endian swapping of an array of 64 bit (8 byte) integers or floating point values.

See also
swap32ip, swap16ip, endianLittle
Parameters
bufPointer to memory.
sizeNr of 64 bit values in the array.

Definition at line 184 of file endian.c.

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}

Referenced by imgReadNifti(), niftiReadHeader(), and niftiWriteHeader().

◆ swawbip()

void swawbip ( void * buf,
int size )

In-place swab and swaw, switches words and bytes from an array of 4-byte ints or floats.

See also
swawip, swabip, swap, endianLittle
Parameters
bufPointer to memory
sizeSize of buf in bytes

Definition at line 138 of file endian.c.

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}

Referenced by anaReadHeader(), ecatMListRead(), niftiReadHeader(), and niftiWriteHeader().

◆ swawip()

void swawip ( void * buf,
int size )

In-place swaw, switches words (but not bytes) from an array of 4-byte ints or floats.

See also
swawbip, swabip, swap, endianLittle
Parameters
bufPointer to memory
sizeSize of buf in bytes

Definition at line 161 of file endian.c.

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}