/** * This routine maps the SAC file header into memory from * a buffer containing it in disk file format. * In the disk file (deriving from the FORTRAN version of * SAC) these strings are essentially concatenated without * null termination. In memory, they are carried about as * null terminated strings. This routine picks out the * strings and null terminates them, storing them in the * in memory working storage area. * * @param memarray * Output * @param buffer * Input * @param lswap * Swap the buffer if necessary * */ void map_hdr_in(float *memarray, float *buffer, int lswap) { char *ptr1, *ptr2; int idx; /* First get the header values for the non character * fields fhdr, nhdr, ihdr and lhdr. These are copied * straight across. */ ptr1 = (char *)memarray; ptr2 = (char *)buffer; memcpy(ptr1,ptr2, SAC_HEADER_NUMBERS * sizeof(float)); /* byteswap numeric data if necessary. */ if( lswap ){ for( idx = 0 ; idx < SAC_HEADER_NUMBERS ; idx++, ptr1 += SAC_HEADER_SIZEOF_NUMBER ) byteswap( (void *)ptr1, SAC_HEADER_SIZEOF_NUMBER ) ; } /* Now copy the character variables into the memory * buffer, supplying the additional null termination * character. */ map_chdr_in(memarray + SAC_HEADER_NUMBERS, buffer + SAC_HEADER_NUMBERS); return; }
/* * read_head_in: * read sac header in and deal with possible byte swap. * * IN: * const char *name : file name, only for debug * SACHEAD *hd : header to be filled * FILE *strm : file handler * * Return: * 0 : Succeed and no byte swap * 1 : Succeed and byte swap * -1 : fail. */ static int read_head_in(const char *name, SACHEAD *hd, FILE *strm) { char *buffer; int lswap; if (sizeof(float) != SAC_DATA_SIZEOF || sizeof(int) != SAC_DATA_SIZEOF) { fprintf(stderr, "Mismatch in size of basic data type!\n"); return -1; } /* read numeric parts of the SAC header */ if (fread(hd, SAC_HEADER_NUMBERS_SIZE, 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n", name); return -1; } /* Check Header Version and Endian */ lswap = check_sac_nvhdr(hd->nvhdr); if (lswap == -1) { fprintf(stderr, "Warning: %s not in sac format.\n", name); return -1; } else if (lswap == TRUE) { byte_swap((char *)hd, SAC_HEADER_NUMBERS_SIZE); } /* read string parts of the SAC header */ if ((buffer = (char *)malloc(SAC_HEADER_STRINGS_SIZE)) == NULL) { fprintf(stderr, "Error in allocating memory %s\n", name); return -1; } if (fread(buffer, SAC_HEADER_STRINGS_SIZE, 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n", name); free(buffer); return -1; } map_chdr_in((char *)(hd)+SAC_HEADER_NUMBERS_SIZE, buffer); free(buffer); return lswap; }