00001 #include "my-endian.h" 00002 00003 /* rotates 2 bytes */ 00004 unsigned short swap2(u_2bytes In) { 00005 u_2bytes Out; 00006 00007 ((char*)&(Out))[0] = ((char*)&(In))[1]; 00008 ((char*)&(Out))[1] = ((char*)&(In))[0]; 00009 00010 return Out; 00011 } 00012 00013 /* rotates 4 bytes */ 00014 u_4bytes rotate4(u_4bytes In) { 00015 u_4bytes Out; 00016 00017 ((u_2bytes*)&Out)[0] = swap2(((u_2bytes*)&In)[1]); 00018 ((u_2bytes*)&Out)[1] = swap2(((u_2bytes*)&In)[0]); 00019 00020 return Out; 00021 } 00022 00023 /* detects endian-ness 00024 * Note: will not work if sizeof(unsigned long)==1 00025 */ 00026 int IsLittleEndian(void) { 00027 static const unsigned long long_number = 1; 00028 return *(const unsigned char *)&long_number; 00029 } 00030 00031 /* changes endian-ness */ 00032 void ToOtherEndian(TEntry *e) { 00033 00034 /* unroll this loop if you want to enumerate u_4bytes members of TEntry_v2 */ 00035 u_4bytes *p; 00036 for (p = &(e -> head.event_duration); p <= &(e -> url); p++) 00037 *p = rotate4(*p); 00038 00039 e -> tail.status = swap2(e -> tail.status); 00040 00041 if (sizeof(method_t) == 2) 00042 e -> tail.method = swap2(e -> tail.method); 00043 else 00044 if (sizeof(method_t) == 4) 00045 e -> tail.method = rotate4(e -> tail.method); 00046 00047 if (sizeof(protocol_t) == 2) 00048 e -> tail.protocol = swap2(e -> tail.protocol); 00049 else 00050 if (sizeof(protocol_t) == 4) 00051 e -> tail.protocol = rotate4(e -> tail.protocol); 00052 }
1.4.6