00001 00002 /* 00003 * THIS FILE IS UNDER RCS - DO NOT MODIFY UNLESS YOU HAVE 00004 * CHECKED IT OUT USING THE COMMAND CHECKOUT. 00005 * 00006 * $Id: swap_8c-source.html 2161 2006-05-19 16:55:03Z paulf $ 00007 * 00008 * Revision history: 00009 * $Log$ 00009 * Revision 1.1 2006/05/19 16:55:02 paulf 00009 * first inclusion 00009 * 00010 * Revision 1.3 2002/03/20 22:13:28 davidk 00011 * Modified WaveMsgMakeLocal(): 00012 * Function now converts the header, performs a checksum on the header, 00013 * and then if successful converts the binary data portion of the tracebuf. 00014 * Function now returns -2 if the header checksum conversion fails. 00015 * Function now returns -1 if _INTEL or _SPARC is not defined. 00016 * (Formerly, no conversion was done and success was returned.) 00017 * 00018 * Revision 1.2 2000/06/22 17:49:33 kohler 00019 * Modified by WMK to allow in-place swapping of non-byte-alligned data. 00020 * 00021 * Revision 1.1 2000/02/14 18:51:48 lucky 00022 * Initial revision 00023 * 00024 * 00025 */ 00026 00027 /* 00028 * SWAP.C 00029 * 00030 * Byte swapping functions 00031 */ 00032 00033 #include <string.h> 00034 #include <swap.h> 00035 00036 void SwapShort( short *data ) 00037 { 00038 char temp; 00039 00040 union { 00041 char c[2]; 00042 } dat; 00043 00044 memcpy( &dat, data, sizeof(short) ); 00045 temp = dat.c[0]; 00046 dat.c[0] = dat.c[1]; 00047 dat.c[1] = temp; 00048 memcpy( data, &dat, sizeof(short) ); 00049 return; 00050 } 00051 00052 void SwapInt( int *data ) 00053 { 00054 char temp; 00055 00056 union { 00057 char c[4]; 00058 } dat; 00059 00060 memcpy( &dat, data, sizeof(int) ); 00061 temp = dat.c[0]; 00062 dat.c[0] = dat.c[3]; 00063 dat.c[3] = temp; 00064 temp = dat.c[1]; 00065 dat.c[1] = dat.c[2]; 00066 dat.c[2] = temp; 00067 memcpy( data, &dat, sizeof(int) ); 00068 return; 00069 } 00070 00071 00072 void SwapLong( long *data ) 00073 { 00074 char temp; 00075 00076 union { 00077 char c[4]; 00078 } dat; 00079 00080 memcpy( &dat, data, sizeof(long) ); 00081 temp = dat.c[0]; 00082 dat.c[0] = dat.c[3]; 00083 dat.c[3] = temp; 00084 temp = dat.c[1]; 00085 dat.c[1] = dat.c[2]; 00086 dat.c[2] = temp; 00087 memcpy( data, &dat, sizeof(long) ); 00088 return; 00089 } 00090 00091 void SwapDouble( double *data ) 00092 { 00093 char temp; 00094 00095 union { 00096 char c[8]; 00097 } dat; 00098 00099 memcpy( &dat, data, sizeof(double) ); 00100 temp = dat.c[0]; 00101 dat.c[0] = dat.c[7]; 00102 dat.c[7] = temp; 00103 00104 temp = dat.c[1]; 00105 dat.c[1] = dat.c[6]; 00106 dat.c[6] = temp; 00107 00108 temp = dat.c[2]; 00109 dat.c[2] = dat.c[5]; 00110 dat.c[5] = temp; 00111 00112 temp = dat.c[3]; 00113 dat.c[3] = dat.c[4]; 00114 dat.c[4] = temp; 00115 memcpy( data, &dat, sizeof(double) ); 00116 return; 00117 } 00118 00119 00120 /**************************** swapWaveMsg *************************** 00121 * Byte-swap a univerals Waveform message in place. * 00122 * Changes the 'datatype' field in the message header * 00123 * Returns -1 if unknown data type. * 00124 * Returns -1 if _SPARC or _INTEL not defined. * 00125 * Returns -2 if checksumish calculation of header fails. * 00126 * Elsewise (SUCCESS) returns 0. * 00127 *********************************************************************/ 00128 00129 int WaveMsgMakeLocal( TRACE_HEADER* wvmsg ) 00130 { 00131 int dataSize; /* flag telling us how many bytes in the data */ 00132 char byteOrder; 00133 long* longPtr; 00134 short* shortPtr; 00135 int i; 00136 int nsamp; 00137 double tShouldEnd; 00138 double dFudgeFactor; 00139 00140 /* See what sort of data it carries 00141 **********************************/ 00142 dataSize=0; 00143 if ( strcmp(wvmsg->datatype, "s4")==0) 00144 { 00145 dataSize=4; byteOrder='s'; 00146 } 00147 else if ( strcmp(wvmsg->datatype, "i4")==0) 00148 { 00149 dataSize=4; byteOrder='i'; 00150 } 00151 else if ( strcmp(wvmsg->datatype, "s2")==0) 00152 { 00153 dataSize=2; byteOrder='s'; 00154 } 00155 else if ( strcmp(wvmsg->datatype, "i2")==0) 00156 { 00157 dataSize=2; byteOrder='i'; 00158 } 00159 else 00160 return(-1); /* We don't know this message type*/ 00161 00162 /* SWAP the header (if neccessary) */ 00163 #if defined( _SPARC ) 00164 if (byteOrder =='i') 00165 { 00166 /* swap the header 00167 *****************/ 00168 SwapInt( &(wvmsg->pinno) ); 00169 SwapInt( &(wvmsg->nsamp) ); 00170 SwapDouble( &(wvmsg->starttime) ); 00171 SwapDouble( &(wvmsg->endtime) ); 00172 SwapDouble( &(wvmsg->samprate) ); 00173 } 00174 00175 #elif defined( _INTEL ) 00176 if (byteOrder =='s') 00177 { 00178 /* swap the header 00179 *****************/ 00180 SwapInt( &(wvmsg->pinno) ); 00181 SwapInt( &(wvmsg->nsamp) ); 00182 SwapDouble( &(wvmsg->starttime) ); 00183 SwapDouble( &(wvmsg->endtime) ); 00184 SwapDouble( &(wvmsg->samprate) ); 00185 } 00186 #else 00187 printf( "WaveMsgMakeLocal warning: _INTEL and _SPARC are both undefined." ); 00188 return(-1); 00189 #endif 00190 00191 00192 /* perform a CheckSumish kind of calculation on the header 00193 ensure that the tracebuf ends within 5 samples of the given endtime. 00194 DK 2002/03/18 00195 *******************************************************************/ 00196 tShouldEnd = wvmsg->starttime + ((wvmsg->nsamp - 1) / wvmsg->samprate); 00197 dFudgeFactor = 5.0 / wvmsg->samprate; 00198 if(tShouldEnd != wvmsg->endtime) 00199 { 00200 if(tShouldEnd > wvmsg->endtime + dFudgeFactor && tShouldEnd < wvmsg->endtime + dFudgeFactor) 00201 { 00202 /* logit("","WaveMsgMakeLocal(): found funky packet with suspect header values!!\n"); */ 00203 return(-2); 00204 } 00205 } 00206 00207 00208 00209 /* SWAP the data (if neccessary) */ 00210 #if defined( _SPARC ) 00211 00212 if (byteOrder =='i') 00213 { 00214 /* Swap the data. Copy nsamp to a local variable 00215 to avoid byte misallignment problems. 00216 **********************************************/ 00217 longPtr=(long*) ((char*)wvmsg + sizeof(TRACE_HEADER) ); 00218 shortPtr=(short*) ((char*)wvmsg + sizeof(TRACE_HEADER) ); 00219 memcpy( &nsamp, &wvmsg->nsamp, sizeof(int) ); 00220 for( i=0; i<nsamp; i++) 00221 { 00222 if(dataSize==2) SwapShort( &shortPtr[i] ); 00223 if(dataSize==4) SwapLong(&(longPtr[i]) ); 00224 } 00225 /* Re-write the data type field in the message 00226 **********************************************/ 00227 if(dataSize==2) strcpy(wvmsg->datatype,"s2"); 00228 if(dataSize==4) strcpy(wvmsg->datatype,"s4"); 00229 } 00230 00231 #elif defined( _INTEL ) 00232 00233 if (byteOrder =='s') 00234 { 00235 /* Swap the data. Copy nsamp to a local variable 00236 to avoid byte misallignment problems. 00237 **********************************************/ 00238 longPtr=(long*) ((char*)wvmsg + sizeof(TRACE_HEADER) ); 00239 shortPtr=(short*) ((char*)wvmsg + sizeof(TRACE_HEADER) ); 00240 memcpy( &nsamp, &wvmsg->nsamp, sizeof(int) ); 00241 for( i=0; i<nsamp; i++) 00242 { 00243 if(dataSize==2) SwapShort( &shortPtr[i] ); 00244 if(dataSize==4) SwapLong(&(longPtr[i]) ); 00245 } 00246 /* Re-write the data type field in the message 00247 **********************************************/ 00248 if(dataSize==2) strcpy(wvmsg->datatype,"i2"); 00249 if(dataSize==4) strcpy(wvmsg->datatype,"i4"); 00250 } 00251 #else 00252 printf( "WaveMsgMakeLocal warning: _INTEL and _SPARC are both undefined." ); 00253 #endif 00254 00255 return(0); 00256 } 00257 00258 00259 void SwapFloat( float *data ) 00260 { 00261 char temp; 00262 00263 union { 00264 char c[4]; 00265 } dat; 00266 00267 memcpy( &dat, data, sizeof(float) ); 00268 temp = dat.c[0]; 00269 dat.c[0] = dat.c[3]; 00270 dat.c[3] = temp; 00271 temp = dat.c[1]; 00272 dat.c[1] = dat.c[2]; 00273 dat.c[2] = temp; 00274 memcpy( data, &dat, sizeof(float) ); 00275 return; 00276 } 00277