xtf_funcs
Description
xtf_funcs is a C utility library providing functions for reading and parsing data from XTF (Extended Triton Format) binary files. It enables the interpretation of XTF file headers and individual data packets (pings), supporting various HeaderType identifiers defined within the XTF specification. The library manages file pointers to navigate through the XTF stream and handles scenarios where expected magic numbers or record lengths are invalid, attempting to resynchronize the file read. It leverages bytes2func.c for byte-level conversions and chxtfhdr.c for structured header conversions.
Functions
void readXTFheader(FILE *inf, XTFFILEHEADER *FileHead)
Reads the initial XTF file header from the input file stream and converts it into a structured XTFFILEHEADER object.
inf: Pointer to the inputFILEstream (XTF file).FileHead: Pointer to theXTFFILEHEADERstructure to populate.
int FindNextXTFPing(FILE *inf, unsigned char *buffer)
Searches for the next XTF ping (data packet) by looking for the 0xFACE magic number within a buffer.
inf: Pointer to the inputFILEstream.buffer: Anunsigned charbuffer (BUFFER_SIZEbytes) where data is read for searching.- Returns:
0on success (magic number found),0xFE0Fif EOF is reached,0xFFFFif magic number is not found within the buffer and new data is read.
int readXTFdata(FILE *inf, unsigned char *buffer)
Reads an XTF data packet (ping) from the input file stream into a buffer, handling various HeaderTypes and potential read errors.
inf: Pointer to the inputFILEstream.buffer: Anunsigned charbuffer where the entire XTF record will be stored.- Returns: The
HeaderTypeof the read record on success,0xFE0Fif EOF is reached or a read error occurs,0xFFFFif an invalid header is found and an attempt to resynchronize the file pointer is made.
How It Works
-
readXTFheader: This function directly readssizeof(cXTFFILEHEADER)bytes from the input file into a rawcXTFFILEHEADERbuffer. It then callscvtFileHeader(fromchxtfhdr.c) to convert this raw buffer into the properly structured and byte-swappedXTFFILEHEADER. -
FindNextXTFPing: This function is a low-level search utility. Itβs used when the expected XTF structure is broken (e.g., an invalid record length or header type). It iteratively scans through the providedbuffer(or reads newBUFFER_SIZEblocks from the file if the magic number isnβt found in the current buffer) looking for the0xFACEmagic number that signifies the start of a valid XTF record. Once found, it repositions the file pointer (fseek) to the beginning of the detected XTF record. -
readXTFdata: This is the core function for reading subsequent XTF records after the file header.- It first attempts to read 64 bytes, which is sufficient to determine the
HeaderTypeandNumBytesThisRecord(total record length). - It uses
bytes2intto get theRecLen, applying byte-swapping if necessary. - Error Handling and Resynchronization: If
RecLenis invalid (zero or too large), it assumes a corrupted record, backs up the file pointer by 64 bytes, reads a largerBUFFER_SIZEblock, and callsFindNextXTFPingto try and locate the next valid0xFACEmagic number. This mechanism helps to recover from corrupted XTF streams. - Full Record Read: If
RecLenis valid, it reads the remainingRecLen - 64bytes of the record into the buffer. - Return Header Type: It returns the
HeaderTypeof the successfully read record, allowing the caller to then interpret the contents of thebufferbased on that type.
- It first attempts to read 64 bytes, which is sufficient to determine the