Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

byteorder.h

Go to the documentation of this file.
00001 #ifndef BYTEORDER_H
00002 #define BYTEORDER_H
00003 
00004 #include <sys/types.h>
00005 
00006 inline int get_byte_order()
00007 {                // 1 if little endian
00008         return (*(u_int32_t*)"a   ") & 0x00000001;
00009 }
00010 
00011 #define SWAP_ITERATE \
00012                                         byte1 = buffer1[i]; \
00013                                 byte2 = buffer2[i]; \
00014                                 buffer1[i] = byte2; \
00015                                 buffer2[i] = byte1; \
00016                                         i += 2;
00017                                                                                                  
00018 #define SWAP_24BIT_ITERATE \
00019                                         byte1 = buffer1[i]; \
00020                                 byte2 = buffer2[i]; \
00021                                 byte3 = buffer3[i]; \
00022                                 buffer1[i] = byte3; \
00023                                 buffer2[i] = byte2; \
00024                                 buffer3[i] = byte1; \
00025                                 i += 3;
00026 
00027 #define SWAP_32BIT_ITERATE \
00028                                         byte1 = buffer1[i]; \
00029                                 byte2 = buffer2[i]; \
00030                                 byte3 = buffer3[i]; \
00031                                 byte4 = buffer4[i]; \
00032                                 buffer1[i] = byte4; \
00033                                 buffer2[i] = byte1; \
00034                                 buffer3[i] = byte2; \
00035                                 buffer4[i] = byte3; \
00036                                 i += 4;
00037 
00038 inline int swap_bytes(int wordsize, unsigned char *buffer, long len)
00039 {
00040         register unsigned char byte1, byte2, byte3, byte4;
00041         register unsigned char *buffer1 = buffer;
00042         register unsigned char *buffer2 = buffer + 1;
00043         register unsigned char *buffer3 = buffer + 2;
00044         register unsigned char *buffer4 = buffer + 3;
00045 
00046         register long i = 0, j = 0, k = 0;
00047 
00048 //printf("swap bytes\n");
00049 
00050         switch(wordsize)
00051         {
00052                 case 1:
00053                         return 0;
00054                         break;
00055                 
00056                 case 2:
00057                         len -= 8;
00058                         while(i < len){
00059                         SWAP_ITERATE
00060                         SWAP_ITERATE
00061                         SWAP_ITERATE
00062                         SWAP_ITERATE
00063                         }
00064 
00065                         len += 8;
00066                         while(i < len){
00067                         SWAP_ITERATE
00068                         }
00069                         return 0;
00070                         break;
00071 
00072                 case 3:
00073                         len -= 12;
00074                         while(i < len){
00075                         SWAP_24BIT_ITERATE
00076                         SWAP_24BIT_ITERATE
00077                         SWAP_24BIT_ITERATE
00078                         SWAP_24BIT_ITERATE
00079                         }
00080 
00081                         len += 12;
00082                         while(i < len){
00083                         SWAP_24BIT_ITERATE
00084                         }
00085                         return 0;
00086                         break;
00087 
00088                 case 4:
00089                         len -= 16;
00090                         while(i < len)
00091                         {
00092                                 SWAP_32BIT_ITERATE
00093                                 SWAP_32BIT_ITERATE
00094                                 SWAP_32BIT_ITERATE
00095                                 SWAP_32BIT_ITERATE
00096                         }
00097 
00098                         len += 16;
00099                         while(i < len)
00100                         {
00101                                 SWAP_32BIT_ITERATE
00102                         }
00103                         return 0;
00104                         break;
00105         }
00106         return 1;
00107 }
00108 
00109 #endif

Generated on Sun Jan 8 13:38:50 2006 for Cinelerra-svn by  doxygen 1.4.4