From: Michal MalĂ˝ Date: Wed, 25 Jul 2018 09:02:29 +0000 (+0200) Subject: Treat marker-like segments as regular data if they are not at the X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=0ea935a8cb5466d57a6090da53c9d02f36b7ab42;p=libHPCS.git Treat marker-like segments as regular data if they are not at the expected marker positions and beef up debugging output. --- diff --git a/src/libHPCS.c b/src/libHPCS.c index 76028e4..e1118c3 100644 --- a/src/libHPCS.c +++ b/src/libHPCS.c @@ -319,9 +319,15 @@ out: return PARSE_OK; } -static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* const next_marker_idx) +static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* const next_marker_idx, const size_t segments_read) { - if (segment[0] == BIN_MARKER_A && segment[1] != BIN_MARKER_END) { + if (segment[0] == BIN_MARKER_A) { + if (segments_read != *next_marker_idx) + return DCHECK_NO_MARKER; + + if (segment[1] == BIN_MARKER_END) + return DCHECK_NO_MARKER; + *next_marker_idx += (uint8_t)segment[1] + 1; return DCHECK_GOT_MARKER; } else @@ -959,21 +965,26 @@ static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pair r = fread(raw, SEGMENT_SIZE, 1, datafile); if (r != 1) return PARSE_E_CANT_READ; - segments_read++; - dret = check_for_marker(raw, &next_marker_idx); + dret = check_for_marker(raw, &next_marker_idx, segments_read); switch (dret) { case DCHECK_EOF: + PR_DEBUG("File contains no data\n"); + return PARSE_E_CANT_READ; case DCHECK_NO_MARKER: + PR_DEBUG("Leading marker not present\n"); return PARSE_E_NOT_FOUND; default: break; } + segments_read++; *pairs = malloc(sizeof(struct HPCS_TVPair) * alloc_size); if (*pairs == NULL) return PARSE_E_NO_MEM; + PR_DEBUGF("Reading signal, first mid-marker expected at segment %lu\n", next_marker_idx); + while (read_file) { r = fread(raw, SEGMENT_SIZE, 1, datafile); @@ -1009,22 +1020,29 @@ static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pair } /* Check for markers */ - dret = check_for_marker(raw, &next_marker_idx); + dret = check_for_marker(raw, &next_marker_idx, segments_read); switch (dret) { case DCHECK_GOT_MARKER: - PR_DEBUGF("Got marker at segment %lu, byte 0x%lx\n", segments_read, 2 * segments_read); - continue; +#ifndef NDEBUG + { + const size_t pos = ftell(datafile) - SEGMENT_SIZE; + fprintf(stderr, "Got marker at segment %lu, byte 0x%lx, next marker expected at %lu\n", segments_read, pos, next_marker_idx); + } +#endif + break; case DCHECK_NO_MARKER: #ifndef NDEBUG if (segments_read == next_marker_idx) - PR_DEBUGF("Warning - marker expected but not found at segment %lu", segments_read); + fprintf(stderr, "Warning - marker expected but not found at segment %lu\n", segments_read); #endif /* Check for a sudden jump of value */ if (raw[0] == BIN_MARKER_JUMP && raw[1] == BIN_MARKER_END) { char lraw[4]; int32_t _v; - - PR_DEBUGF("Value has jumped at 0x%lx\n", segments_read); +#ifndef NDEBUG + const size_t pos = ftell(datafile) - SEGMENT_SIZE; + fprintf(stderr, "Value has jumped at %lu, byte 0x%lx\n", segments_read, pos); +#endif r = fread(lraw, LARGE_SEGMENT_SIZE, 1, datafile); if (feof(datafile) || ferror(datafile) || (r != 1)) { free(*pairs); @@ -1044,7 +1062,6 @@ static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pair } (*pairs)[data_segments_read].value = value; - segments_read++; data_segments_read++; break; default: @@ -1053,6 +1070,7 @@ static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pair *pairs = NULL; return PARSE_E_CANT_READ; } + segments_read++; } *pairs_count = data_segments_read; diff --git a/src/libHPCS_p.h b/src/libHPCS_p.h index 7b02556..edd24fb 100644 --- a/src/libHPCS_p.h +++ b/src/libHPCS_p.h @@ -174,7 +174,7 @@ UChar* CR_LF; #endif static enum HPCS_ParseCode autodetect_file_type(FILE* datafile, enum HPCS_FileType* file_type, const bool p_means_pressure, const enum HPCS_GenType gentype); -static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* const next_marker_idx); +static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* const next_marker_idx, const size_t segments_read); static enum HPCS_ChemStationVer detect_chemstation_version(const char*const version_string); static bool gentype_is_readable(const enum HPCS_GenType gentype); static enum HPCS_ParseCode fetch_signal_step(FILE * datafile, double *step, double *shift, bool old_format);