--- /home/icrazy/tt/sac-101.1/utils/sacswap.c 2006-03-27 15:44:54.000000000 -0500 +++ sacswap2.c 2008-08-30 19:50:40.000000000 -0400 @@ -47,9 +47,9 @@ #define HEADER_SIZE (NFLOATS * FLOAT_SIZE) + (NINTS * INT_SIZE) + (NCHARS * CHAR_SIZE) int file_size(char *file); -float float_swap(char cbuf[]); -long long_swap(char cbuf[]); -long long_join(char cbuf[]); +int long_swap(void *swappee); +int float_swap(void *swappee); +int byteswap(void *swappee, int Nbytes); int @@ -59,9 +59,9 @@ float *data; float sacfloat[NFLOATS]; long saclong[NINTS]; - char cbuf[MAXBYTES], sacfile[2048], sacchars[NCHARS]; + unsigned char cbuf[MAXBYTES], sacfile[2048], sacchars[NCHARS]; FILE *fp; - char swap[2048]; + unsigned char swap[2048], *ptr; int size; /* Check usage */ @@ -95,7 +95,9 @@ } /* Swap the floats */ for(j = 0; j< NFLOATS; j++) { - sacfloat[j] = float_swap(cbuf + FLOAT_SIZE * j); + ptr = cbuf + FLOAT_SIZE * j; + float_swap((void *)ptr); + sacfloat[j] = *((float *)ptr); } /* ints */ @@ -105,19 +107,25 @@ } /* Swap the ints */ for(j = 0; j < NINTS; j++) { - saclong[j] = long_swap(cbuf + INT_SIZE * j); + ptr = cbuf + INT_SIZE * j; + long_swap((void *)ptr); + saclong[j] = *((long *)ptr); } /* Figure out which way we're going: To or from native format? */ /* store swapped and unswapped header values */ nvhdr = (int) saclong[LONG_NVHDR]; - nnvhdr = (int) long_join(cbuf + INT_SIZE * LONG_NVHDR); + ptr = cbuf + INT_SIZE * LONG_NVHDR; + long_swap((void *)ptr); + nnvhdr = *((int *)ptr); if ( nvhdr >= HEADER_MIN && nvhdr <= HEADER_MAX ) { npts = (int) saclong[LONG_NPTS]; sprintf(swap, "non-native => native"); } else if ( nnvhdr >= HEADER_MIN && nnvhdr <= HEADER_MAX ) { - npts = (int) long_join(cbuf + INT_SIZE * LONG_NPTS); + ptr = cbuf + INT_SIZE * LONG_NPTS; + long_swap((void *)ptr); + npts = *((int *)ptr); sprintf(swap, "native => non-native"); } else { fprintf(stderr, "%s: %s has bad header (native,nonnative): %d, %d\n", @@ -149,7 +157,8 @@ fprintf(stderr, "%s: exiting\n", PROGRAM); exit(-1); } - data[j] = (float) float_swap(cbuf); + float_swap((void *)cbuf); + data[j] = *((float *)cbuf); } fclose(fp); @@ -192,45 +201,61 @@ return((int)s.st_size); } -long -long_swap(char cbuf[]) { - union { - char cval[4]; - long lval; - } l_union; +int +long_swap(void *swappee) { + return byteswap(swappee, 4); +} - l_union.cval[3] = cbuf[0]; - l_union.cval[2] = cbuf[1]; - l_union.cval[1] = cbuf[2]; - l_union.cval[0] = cbuf[3]; - return(l_union.lval); +int +float_swap(void *swappee) { + return byteswap(swappee, 4); } -float -float_swap(char cbuf[]) { - union { - char cval[4]; - float fval; - } f_union; +/* this routine will reverse the byte order of 2, 4, or 8 byte numeric types. + swapee is the data to be byteswapped. Nbytes is the number of bytes to + swap. byteswap returns 0 for success and -1 for failure. maf 2/5/03 */ - f_union.cval[3] = cbuf[0]; - f_union.cval[2] = cbuf[1]; - f_union.cval[1] = cbuf[2]; - f_union.cval[0] = cbuf[3]; - return(f_union.fval); -} +int byteswap(void *swappee, int Nbytes) +{ + char temp, *ptr = swappee ; -long -long_join(char cbuf[]) { - union { - char cval[4]; - long lval; - } l_union; + if( Nbytes == 2 ){ + temp = ptr[0] ; + ptr[0] = ptr[1] ; + ptr[1] = temp ; + return 0 ; + } - l_union.cval[3] = cbuf[3]; - l_union.cval[2] = cbuf[2]; - l_union.cval[1] = cbuf[1]; - l_union.cval[0] = cbuf[0]; - return(l_union.lval); + if( Nbytes == 4 ){ + temp = ptr[0] ; + ptr[0] = ptr[3] ; + ptr[3] = temp ; + + temp = ptr[1] ; + ptr[1] = ptr[2] ; + ptr[2] = temp ; + return 0 ; + } + + if( Nbytes == 8 ){ + temp = ptr[0] ; + ptr[0] = ptr[7] ; + ptr[7] = temp ; + + temp = ptr[1] ; + ptr[1] = ptr[6] ; + ptr[6] = temp ; + + temp = ptr[2] ; + ptr[2] = ptr[5] ; + ptr[5] = temp ; + + temp = ptr[3] ; + ptr[3] = ptr[4] ; + ptr[4] = temp ; + return 0 ; + } + + return -1 ; }