From 6fa626ee31760f1cccc20d0897be9f4b09e84a91 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Tue, 23 May 2017 11:21:12 +0200 Subject: [PATCH] C unpacker: count_events and extract_info --- normalmode/c_standalone/.gitignore | 3 + normalmode/c_standalone/Makefile.count_events | 21 ++++ normalmode/c_standalone/Makefile.extract_info | 21 ++++ normalmode/c_standalone/README.md | 15 +++ normalmode/c_standalone/count_events.c | 51 ++++++++++ normalmode/c_standalone/count_events.h | 14 +++ normalmode/c_standalone/extract_info.c | 80 +++++++++++++++ normalmode/c_standalone/extract_info.h | 14 +++ normalmode/c_standalone/hld.c | 97 +++++++++++++++++++ normalmode/c_standalone/hld.h | 44 +++++++++ 10 files changed, 360 insertions(+) create mode 100644 normalmode/c_standalone/.gitignore create mode 100644 normalmode/c_standalone/Makefile.count_events create mode 100644 normalmode/c_standalone/Makefile.extract_info create mode 100644 normalmode/c_standalone/README.md create mode 100644 normalmode/c_standalone/count_events.c create mode 100644 normalmode/c_standalone/count_events.h create mode 100644 normalmode/c_standalone/extract_info.c create mode 100644 normalmode/c_standalone/extract_info.h create mode 100644 normalmode/c_standalone/hld.c create mode 100644 normalmode/c_standalone/hld.h diff --git a/normalmode/c_standalone/.gitignore b/normalmode/c_standalone/.gitignore new file mode 100644 index 0000000..32fc39b --- /dev/null +++ b/normalmode/c_standalone/.gitignore @@ -0,0 +1,3 @@ +extract_info +count_events +*.o diff --git a/normalmode/c_standalone/Makefile.count_events b/normalmode/c_standalone/Makefile.count_events new file mode 100644 index 0000000..2a0592b --- /dev/null +++ b/normalmode/c_standalone/Makefile.count_events @@ -0,0 +1,21 @@ + +# http://mrbook.org/blog/tutorials/make/ + +CC=gcc +CFLAGS=-c -Wall -O3 -std=c11 +LDFLAGS= +SOURCES=count_events.c hld.c math_helpers.c +OBJECTS=$(SOURCES:.c=.o) +EXECUTABLE=count_events + +all: $(SOURCES) $(EXECUTABLE) + +$(EXECUTABLE): $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) -o $@ + +.cpp.o: + $(CC) $(CFLAGS) $< -o $@ + +clean: + -rm $(OBJECTS) $(EXECUTABLE) + diff --git a/normalmode/c_standalone/Makefile.extract_info b/normalmode/c_standalone/Makefile.extract_info new file mode 100644 index 0000000..6104598 --- /dev/null +++ b/normalmode/c_standalone/Makefile.extract_info @@ -0,0 +1,21 @@ + +# http://mrbook.org/blog/tutorials/make/ + +CC=gcc +CFLAGS=-c -Wall -O3 -std=c11 +LDFLAGS= +SOURCES=extract_info.c hld.c math_helpers.c +OBJECTS=$(SOURCES:.c=.o) +EXECUTABLE=extract_info + +all: $(SOURCES) $(EXECUTABLE) + +$(EXECUTABLE): $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) -o $@ + +.cpp.o: + $(CC) $(CFLAGS) $< -o $@ + +clean: + -rm $(OBJECTS) $(EXECUTABLE) + diff --git a/normalmode/c_standalone/README.md b/normalmode/c_standalone/README.md new file mode 100644 index 0000000..f01a410 --- /dev/null +++ b/normalmode/c_standalone/README.md @@ -0,0 +1,15 @@ + +### Unpacker + +This is an unpacker for HLD files written in plain C. +It is fast and versatile. + +#### Programming Conventions + +* Use tabs to indent + +#### Author + +* Philipp Klaus + + diff --git a/normalmode/c_standalone/count_events.c b/normalmode/c_standalone/count_events.c new file mode 100644 index 0000000..8c47287 --- /dev/null +++ b/normalmode/c_standalone/count_events.c @@ -0,0 +1,51 @@ +#include +#include +#include +//#include +#include +//#include +//#include +//#include + +#include "math_helpers.h" +#include "count_events.h" +#include "hld.h" + +uint32_t num_events = 0; + +// callback function to be passed to the HLD read_event() function +int read_frame(unsigned long *pos, FILE *ptr_file) +{ + return 0; +} + +int main(int argc, char **argv) +{ + unsigned long pos; + FILE *ptr_myfile; + + if (argc < 2) + { + printf("Missing HLD file as argument\n"); + return 1; + } + char *hld_filename = *++argv; + ptr_myfile = fopen(hld_filename, "rb"); + if (!ptr_myfile) + { + printf("Unable to open file!\n"); + return 1; + } + + print1("seqnr trignr mkd_timestamp frame_length sensor_id sensor_frame_cnt senlen\n"); + pos = 0; + while (read_event(&pos, ptr_myfile, &read_frame) == 0) + { + num_events++; + } // end while read_event() + printf("\nTotal number of events: %lu\n\n", num_events); + + fclose(ptr_myfile); + + return 0; +} diff --git a/normalmode/c_standalone/count_events.h b/normalmode/c_standalone/count_events.h new file mode 100644 index 0000000..85084be --- /dev/null +++ b/normalmode/c_standalone/count_events.h @@ -0,0 +1,14 @@ + +// for the FILE typedef +#include +#include "hld.h" + +#define N_BYTES 32 + +int read_frame(unsigned long *pos, FILE *ptr_file); +int main(int argc, char **argv); + +uint32_t extern event_header[EVENT_HEADERSIZE]; +uint32_t extern subevent_header[SUBEVENT_HEADERSIZE]; +uint16_t extern subsubevent_header[SUBSUBEVENT_HEADERSIZE*2]; +uint32_t extern marker_timestamp, frame_timestamp; diff --git a/normalmode/c_standalone/extract_info.c b/normalmode/c_standalone/extract_info.c new file mode 100644 index 0000000..8899582 --- /dev/null +++ b/normalmode/c_standalone/extract_info.c @@ -0,0 +1,80 @@ +#include +#include +#include +//#include +#include +//#include +//#include +//#include +#include + +#include "math_helpers.h" +#include "count_events.h" +#include "hld.h" + +uint32_t num_events = 0; + +// callback function to be passed to the HLD read_event() function +int read_frame(unsigned long *pos, FILE *ptr_file) +{ + uint16_t frame_length, sensor_id; + uint32_t frame_start, frame_counter; + uint16_t senlen; + fread(&frame_length, sizeof(uint16_t), 1, ptr_file); + frame_length = ntohs(frame_length); + fread(&sensor_id, sizeof(uint16_t), 1, ptr_file); + sensor_id = ntohs(sensor_id); + fseek(ptr_file, 12, SEEK_CUR); + fread(&frame_start, sizeof(uint32_t), 1, ptr_file); + if (frame_start != 0x55555555) + { + print1("Bad header!\n"); + return 1; + } + fread(&frame_counter, sizeof(uint32_t), 1, ptr_file); + frame_counter = ntohs(frame_counter); + fread(&senlen, sizeof(uint16_t), 1, ptr_file); + senlen = ntohs(senlen); + // from global state: + uint32_t seqnr = event_header[3]; + uint32_t trignr = subevent_header[3]; + uint16_t roc = ntohs(subsubevent_header[1]); + trignr = ntohl(trignr); + // output + //print1("seqnr: 0x%08X trignr: 0x%08X mkd_timestamp: 0x%08X frame_length: 0x%04X sensor_id: 0x%02X sensor_frame_cnt: 0x%08X senlen: 0x%04X\n", + // seqnr, trignr, marker_timestamp, frame_length, sensor_id, frame_counter, senlen); + print1("%"PRIu32" %"PRIu32" %"PRIu16" %"PRIu32" %"PRIu16" %"PRIu16" %"PRIu32" %"PRIu16"\n", + seqnr, trignr, roc, marker_timestamp, frame_length, sensor_id, frame_counter, senlen); + return 0; +} + +int main(int argc, char **argv) +{ + unsigned long pos; + FILE *ptr_myfile; + + if (argc < 2) + { + printf("Missing HLD file as argument\n"); + return 1; + } + char *hld_filename = *++argv; + ptr_myfile = fopen(hld_filename, "rb"); + if (!ptr_myfile) + { + printf("Unable to open file!\n"); + return 1; + } + + print1("seqnr trignr roc mkd_timestamp frame_length sensor_id sensor_frame_cnt senlen\n"); + pos = 0; + while (read_event(&pos, ptr_myfile, &read_frame) == 0) + { + num_events++; + } // end while read_event() + //printf("\nTotal number of events: %lu\n\n", num_events); + + fclose(ptr_myfile); + + return 0; +} diff --git a/normalmode/c_standalone/extract_info.h b/normalmode/c_standalone/extract_info.h new file mode 100644 index 0000000..85084be --- /dev/null +++ b/normalmode/c_standalone/extract_info.h @@ -0,0 +1,14 @@ + +// for the FILE typedef +#include +#include "hld.h" + +#define N_BYTES 32 + +int read_frame(unsigned long *pos, FILE *ptr_file); +int main(int argc, char **argv); + +uint32_t extern event_header[EVENT_HEADERSIZE]; +uint32_t extern subevent_header[SUBEVENT_HEADERSIZE]; +uint16_t extern subsubevent_header[SUBSUBEVENT_HEADERSIZE*2]; +uint32_t extern marker_timestamp, frame_timestamp; diff --git a/normalmode/c_standalone/hld.c b/normalmode/c_standalone/hld.c new file mode 100644 index 0000000..56386f9 --- /dev/null +++ b/normalmode/c_standalone/hld.c @@ -0,0 +1,97 @@ +#include +#include +#include + +#include "hld.h" + +uint32_t event_header[EVENT_HEADERSIZE]; +uint32_t subevent_header[SUBEVENT_HEADERSIZE]; +uint16_t subsubevent_header[SUBSUBEVENT_HEADERSIZE*2]; +uint32_t marker_timestamp, frame_timestamp; + +int read_event(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)) +{ + unsigned long start_pos = *pos; + if (!fread(event_header, WORDSIZE, EVENT_HEADERSIZE, ptr_file)) return 1; + uint32_t size = event_header[0]; + print2("EVENT Pos: 0x%lX Size: 0x%X\n", *pos, size); + *pos += EVENT_HEADERSIZE*WORDSIZE; + while (*pos - start_pos < size) + if (read_subevent(pos, ptr_file, sensor_callback)) break; + *pos = start_pos + size + size % BLOCKSIZE; + if (size % BLOCKSIZE != 0) + fseek(ptr_file, *pos, SEEK_SET); + return 0; +} + +int read_subevent(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)) +{ + unsigned long start_pos = *pos; + if (!fread(subevent_header, WORDSIZE, SUBEVENT_HEADERSIZE, ptr_file)) return 1; + uint32_t size = ntohl(subevent_header[0]); + print2("SUBEVENT Pos: 0x%lX Size: 0x%X\n", *pos, size); + *pos += SUBEVENT_HEADERSIZE*WORDSIZE; + while (*pos - start_pos < size) + if (read_subsubevent(pos, ptr_file, sensor_callback)) break; + *pos = start_pos + size + size % BLOCKSIZE; + if (size % BLOCKSIZE != 0) + fseek(ptr_file, *pos, SEEK_SET); + return 0; +} + +int read_subsubevent(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)) +{ + unsigned long start_pos = *pos; + if (!fread(subsubevent_header, WORDSIZE/2, SUBSUBEVENT_HEADERSIZE*2, ptr_file)) return 1; + uint16_t size = ntohs(subsubevent_header[0]); + uint16_t address = ntohs(subsubevent_header[1]); + print2("SUBSUBEVENT Pos: 0x%lX Size: 0x%X Adress: 0x%X\n", *pos, size, address); + if (address >= 0xD000 && address < 0xE000) + { + uint8_t roc_header[4]; + uint8_t header_version, data_version, header_size; + if (!fread(roc_header, 1, 4, ptr_file)) return 1; + header_version = roc_header[0]; + data_version = roc_header[1]; + header_size = roc_header[3]; + BOOL frame_marked = FALSE; + marker_timestamp = 0; + frame_timestamp = 0; + if (header_size >= 1) + { + if (!fread(&marker_timestamp, 4, 1, ptr_file)) return 1; + marker_timestamp = ntohl(marker_timestamp); + frame_marked = marker_timestamp & 0x80000000; + marker_timestamp &= 0x00FFFFFF; + } + if (header_size >= 2) + { + if (!fread(&frame_timestamp, 4, 1, ptr_file)) return 1; + frame_timestamp = ntohl(frame_timestamp); + } + if (header_size >= 3) + { + print1("Header length not yet implemented.\n"); + return 2; + } + print2("Marker: %d, Marker Timestamp: 0x%08X, Frame Timestamp: 0x%08X\n", frame_marked, marker_timestamp, frame_timestamp); + // finished reading the ROC header + + // Starting to look at frame header now + uint16_t frame_size; + *pos += (SUBSUBEVENT_HEADERSIZE + header_size + 1) * WORDSIZE; + while (*pos - start_pos < (SUBSUBEVENT_HEADERSIZE + size) * WORDSIZE) + { + fseek(ptr_file, *pos, SEEK_SET); + if (!fread(&frame_size, 2, 1, ptr_file)) return 1; + frame_size = ntohs(frame_size); + fseek(ptr_file, *pos, SEEK_SET); + sensor_callback(pos, ptr_file); + *pos += (1 + frame_size) * WORDSIZE; + } + } + + *pos = start_pos + (size + SUBSUBEVENT_HEADERSIZE) * WORDSIZE; + fseek(ptr_file, *pos, SEEK_SET); + return 0; +} diff --git a/normalmode/c_standalone/hld.h b/normalmode/c_standalone/hld.h new file mode 100644 index 0000000..89612c1 --- /dev/null +++ b/normalmode/c_standalone/hld.h @@ -0,0 +1,44 @@ + +// HLD file format properties + +// size of a data word (32 bits) in bytes +#define WORDSIZE 4 +// size of a data block (64 bits) in bytes +#define BLOCKSIZE (WORDSIZE*2) +// size of the headers in 32bit words +#define EVENT_HEADERSIZE 8 +// size of the headers in 32bit words +#define SUBEVENT_HEADERSIZE 4 +// size of the headers in 32bit words +#define SUBSUBEVENT_HEADERSIZE 1 + +// substituting the missing bool type +#define FALSE 0 +#define TRUE 1 +#define BOOL uint8_t + +// debugging features +#ifndef DEBUGLEVEL + #define DEBUGLEVEL 1 + + #if (DEBUGLEVEL >= 1) + # define print1 printf + #else + # define print1(...) (0) + #endif + + #if (DEBUGLEVEL >= 2) + # define print2 printf + #else + # define print2(...) (0) + #endif +#endif + +// for the FILE typedef +#include + +int read_event(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)); +int read_subevent(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)); +int read_subsubevent(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)); +int read_roc(unsigned long *pos, FILE *ptr_file, int (*sensor_callback)(unsigned long *pos, FILE *ptr_file)); + -- 2.43.0