]> Devoid-pointer.net GitWeb - libHPCS.git/commitdiff
Check file type description and abort reading if unknown.
authorUser <User>
Tue, 1 Dec 2015 12:46:17 +0000 (13:46 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Tue, 1 Dec 2015 16:59:51 +0000 (17:59 +0100)
Signed-off-by: Michal Malý <madcatxster@devoid-pointer.net>
libhpcs.c
libhpcs_p.h

index 951fa9ff9d3b12025b06f05aaff45baf5c25f103..a621fdd551a65581b8baf5ad3d2bc3f4b89a48ec 100644 (file)
--- 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];
index 1fdf7ccdca14ef1fb036e313e695277224996f01..27d9bb9358b1b1870c2693a822808baea08b8c18 100644 (file)
@@ -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,