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

htendian.cc

Go to the documentation of this file.
00001 /* 
00002  *      HT Editor
00003  *      htendian.cc
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "htdebug.h"
00022 #include "htendian.h"
00023 #include "global.h"
00024 #include <string.h>
00025 
00026 void create_foreign_int(void *buf, int i, int size, endianess to_endianess)
00027 {
00028         byte *b=(byte*)buf;
00029         switch (size) {
00030                 case 1:
00031                         b[0]=i;
00032                         break;
00033                 case 2:
00034                         switch (to_endianess) {
00035                                 case big_endian:
00036                                         b[0]=i>>8;
00037                                         b[1]=i;
00038                                         break;
00039                                 case little_endian:
00040                                         b[0]=i;
00041                                         b[1]=i>>8;
00042                                         break;
00043                         }
00044                         break;
00045                 case 4:
00046                         switch (to_endianess) {
00047                                 case big_endian:
00048                                         b[0]=i>>24;
00049                                         b[1]=i>>16;
00050                                         b[2]=i>>8;
00051                                         b[3]=i;
00052                                         break;
00053                                 case little_endian:
00054                                         b[0]=i;
00055                                         b[1]=i>>8;
00056                                         b[2]=i>>16;
00057                                         b[3]=i>>24;
00058                                         break;
00059                         }
00060                         break;
00061         }
00062 }
00063 
00064 int create_host_int(const void *buf, int size, endianess from_endianess)
00065 {
00066         byte *b=(byte*)buf;
00067         switch (size) {
00068                 case 1:
00069                         return b[0];
00070                 case 2:
00071                         switch (from_endianess) {
00072                                 case big_endian:
00073                                         return (b[0]<<8) | b[1];
00074                                 case little_endian:
00075                                         return (b[1]<<8) | b[0];
00076                         }
00077                         break;
00078                 case 4:
00079                         switch (from_endianess) {
00080                                 case big_endian:
00081                                         return (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3];
00082                                 case little_endian:
00083                                         return (b[3]<<24) | (b[2]<<16) | (b[1]<<8) | b[0];
00084                         }
00085                         break;
00086         }
00087         assert(0);
00088         return 0;
00089 }
00090 
00091 void create_foreign_int64(void *buf, const qword i, int size, endianess to_endianess)
00092 {
00093         byte *b = (byte*)buf;
00094         dword hi = QWORD_GET_HI(i);
00095         dword lo = QWORD_GET_LO(i);
00096         switch (to_endianess) {
00097                 case big_endian:
00098                         b[0]=hi>>24;
00099                         b[1]=hi>>16;
00100                         b[2]=hi>>8;
00101                         b[3]=hi;
00102                         b[4]=lo>>24;
00103                         b[5]=lo>>16;
00104                         b[6]=lo>>8;
00105                         b[7]=lo;
00106                         break;
00107                 case little_endian:
00108                         b[0]=lo;
00109                         b[1]=lo>>8;
00110                         b[2]=lo>>16;
00111                         b[3]=lo>>24;
00112                         b[4]=hi;
00113                         b[5]=hi>>8;
00114                         b[6]=hi>>16;
00115                         b[7]=hi>>24;
00116                         break;
00117         }
00118 }
00119 
00120 qword create_host_int64(const void *buf, endianess from_endianess)
00121 {
00122         byte *b = (byte*)buf;
00123         qword q;
00124         switch (from_endianess) {
00125                 case big_endian:
00126                         QWORD_SET_HI(q, (b[0]<<24) | (b[1]<<16) | (b[2]<<8) | b[3]);
00127                         QWORD_SET_LO(q, (b[4]<<24) | (b[5]<<16) | (b[6]<<8) | b[7]);
00128                         break;
00129                 case little_endian:
00130                         QWORD_SET_HI(q, (b[7]<<24) | (b[6]<<16) | (b[5]<<8) | b[4]);
00131                         QWORD_SET_LO(q, (b[3]<<24) | (b[2]<<16) | (b[1]<<8) | b[0]);
00132                         break;
00133         }
00134         return q;
00135 }
00136 
00137 void create_host_struct(void *buf, const byte *table, endianess from)
00138 {
00139         byte *buf2 = (byte *)buf;
00140         while (*table) {
00141                 int table2 = *table & ~STRUCT_ENDIAN_HOST;
00142                 if (*table & STRUCT_ENDIAN_HOST) {
00143                         switch (table2) {
00144                                 case STRUCT_ENDIAN_BYTE: {
00145                                         byte a = create_host_int(buf2, STRUCT_ENDIAN_BYTE, from);
00146                                         memcpy(buf2, &a, STRUCT_ENDIAN_BYTE);
00147                                         break;
00148                                 }
00149                                 case STRUCT_ENDIAN_WORD: {
00150                                         word a = create_host_int(buf2, STRUCT_ENDIAN_WORD, from);
00151                                         memcpy(buf2, &a, STRUCT_ENDIAN_WORD);
00152                                         break;
00153                                 }
00154                                 case STRUCT_ENDIAN_DWORD: {
00155                                         dword a = create_host_int(buf2, STRUCT_ENDIAN_DWORD, from);
00156                                         memcpy(buf2, &a, STRUCT_ENDIAN_DWORD);
00157                                         break;
00158                                 }
00159                                 case STRUCT_ENDIAN_QWORD: {
00160                                         qword q = create_host_int64(buf2, from);
00161                                         memcpy(buf2, &q, STRUCT_ENDIAN_QWORD);
00162                                         break;
00163                                 }
00164                                 default: {
00165                                         assert(0);
00166                                 }
00167                         }
00168                 }
00169                 buf2+=table2;
00170                 table++;
00171         }
00172 }
00173 

Generated on Fri May 7 21:15:33 2004 by doxygen 1.3.5