From: User Date: Tue, 1 Dec 2015 12:46:17 +0000 (+0100) Subject: Check file type description and abort reading if unknown. X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=dc5a11635063c54634a0699cc1411e3e9cc95b2d;p=libHPCS.git Check file type description and abort reading if unknown. Signed-off-by: Michal MalĂ˝ --- diff --git a/libhpcs.c b/libhpcs.c index 951fa9f..a621fdd 100644 --- a/libhpcs.c +++ b/libhpcs.c @@ -133,6 +133,16 @@ enum HPCS_RetCode hpcs_read_mdata(const char* filename, struct HPCS_MeasuredData goto out; } + pret = read_file_type_description(datafile, &mdata->file_description); + if (pret != PARSE_OK) + goto out; + + if (!file_type_description_is_readable(mdata->file_description)) { + PR_DEBUGF("Incompatible file description: %s\n", mdata->file_description); + ret = HPCS_E_INCOMPATIBLE_FILE; + goto out; + } + pret = read_file_header(datafile, mdata); if (pret != PARSE_OK) { PR_DEBUG("Cannot read the header\n"); @@ -203,6 +213,16 @@ enum HPCS_RetCode hpcs_read_mheader(const char* filename, struct HPCS_MeasuredDa goto out; } + pret = read_file_type_description(datafile, &mdata->file_description); + if (pret != PARSE_OK) + goto out; + + if (!file_type_description_is_readable(mdata->file_description)) { + PR_DEBUGF("Incompatible file description: %s\n", mdata->file_description); + ret = HPCS_E_INCOMPATIBLE_FILE; + goto out; + } + pret = read_file_header(datafile, mdata); if (pret != PARSE_OK) ret = HPCS_E_PARSE_ERROR; @@ -278,6 +298,14 @@ static enum HPCS_DataCheckCode check_for_marker(const char* segment, size_t* con return DCHECK_NO_MARKER; } +static bool file_type_description_is_readable(const char*const description) +{ + if (!strcmp(FILE_DESC_LC_DATA_FILE, description)) + return true; + else + return false; +} + static bool gentype_is_readable(const enum HPCS_GenType gentype) { switch (gentype) { @@ -624,11 +652,6 @@ static enum HPCS_ParseCode read_file_header(FILE* datafile, struct HPCS_Measured { enum HPCS_ParseCode pret; - pret = read_string_at_offset(datafile, DATA_OFFSET_FILE_DESC, &mdata->file_description); - if (pret != PARSE_OK) { - PR_DEBUG("Cannot read file description\n"); - return pret; - } pret = read_string_at_offset(datafile, DATA_OFFSET_SAMPLE_INFO, &mdata->sample_info); if (pret != PARSE_OK) { PR_DEBUG("Cannot read sample info\n"); @@ -687,6 +710,17 @@ static enum HPCS_ParseCode read_file_header(FILE* datafile, struct HPCS_Measured return PARSE_OK; } +static enum HPCS_ParseCode read_file_type_description(FILE* datafile, char** const description) +{ + enum HPCS_ParseCode pret; + + pret = read_string_at_offset(datafile, DATA_OFFSET_FILE_DESC, description); + if (pret != PARSE_OK) + PR_DEBUG("Cannot read file description\n"); + + return pret; +} + static enum HPCS_ParseCode read_method_info_file(HPCS_UFH fh, struct HPCS_MethodInfo* minfo) { HPCS_NChar line[64]; diff --git a/libhpcs_p.h b/libhpcs_p.h index 1fdf7cc..27d9bb9 100644 --- a/libhpcs_p.h +++ b/libhpcs_p.h @@ -118,6 +118,9 @@ enum HPCS_GenType { GENTYPE_ADC_UV2 = 131 }; +/* Known file descriptions */ +const char FILE_DESC_LC_DATA_FILE[] = "LC DATA FILE"; + /* Known ChemStation format versions */ const char CHEMSTAT_VER_B0625[] = "B.06.25 [0003]"; @@ -153,6 +156,7 @@ static HPCS_step guess_current_step(const struct HPCS_MeasuredData* mdata); static HPCS_step guess_elec_sigstep(const struct HPCS_MeasuredData *mdata); static bool guess_p_meaning(const struct HPCS_MeasuredData* mdata); static void guess_sampling_rate(struct HPCS_MeasuredData* mdata); +static bool file_type_description_is_readable(const char*const description); static enum HPCS_ParseCode next_native_line(HPCS_UFH fh, HPCS_NChar* line, int32_t length); static HPCS_UFH open_data_file(const char* filename); static enum HPCS_ParseCode parse_native_method_info_line(char** name, char** value, HPCS_NChar* line); @@ -160,6 +164,7 @@ static enum HPCS_ParseCode read_dad_wavelength(FILE* datafile, struct HPCS_Wavel static uint8_t month_to_number(const char* month); static enum HPCS_ParseCode read_date(FILE* datafile, struct HPCS_Date* date); static enum HPCS_ParseCode read_file_header(FILE* datafile, struct HPCS_MeasuredData* mdata); +static enum HPCS_ParseCode read_file_type_description(FILE* datafile, char** const description); static enum HPCS_ParseCode read_generic_type(FILE* datafile, enum HPCS_GenType* gentype); static enum HPCS_ParseCode read_method_info_file(HPCS_UFH fh, struct HPCS_MethodInfo* minfo); static enum HPCS_ParseCode read_signal(FILE* datafile, struct HPCS_TVPair** pairs, size_t* pairs_count,