delta-verifier: Compare variables only form file from delta. 90/285290/5
authorAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Thu, 8 Dec 2022 13:12:42 +0000 (14:12 +0100)
committerAntoni Adaszkiewicz <a.adaszkiewi@samsung.com>
Thu, 8 Dec 2022 17:19:15 +0000 (18:19 +0100)
To allow for "full" type delta to pass verification, only variables
present in "update-info.ini" file from provided delta will be compared
against system variables generated by the device.

Change-Id: I60599e6a2673a4649eb47b88a6ef913b66f9d5e9

delta-verifier/delta-verifier.c

index 1fe459c1ef69fd0294e80eb3e98c883bfd681c76..9beddfad89402b0a93ba276552535134e775b7ab 100644 (file)
@@ -196,18 +196,119 @@ static int write_info_from_file(FILE* read_file, FILE* write_file)
        return 0;
 }
 
+static int get_newline_count(char* buff)
+{
+       int count = 0;
+
+       for (int i = 0; i < strlen(buff); i++)
+       {
+               if (buff[i] == '\n')
+                       count++;
+       }
+
+       return count;
+}
+
+static char** alloc_string_array(const int lines)
+{
+       char** arr = (char**)malloc(sizeof(char*) * lines);
+       if (arr == NULL) {
+               ERR_ERRNO("malloc()");
+               return NULL;
+       }
+
+       for (int i = 0; i < lines; i++)
+       {
+               arr[i] = (char*)malloc(MAX_STRING);
+               if (arr[i] == NULL) {
+                       for (int j = 0; j < i; j++)
+                               free(arr[i]);
+
+                       free(arr);
+                       ERR_ERRNO("malloc()");
+                       return NULL;
+               }
+       }
+
+       return arr;
+}
+
+static void free_string_array(char** arr, const int lines)
+{
+       for (int i = 0; i < lines; i++)
+               free(arr[i]);
+       free(arr);
+}
+
+static char** get_string_array(char* buff, const int lines)
+{
+       char** string_array = alloc_string_array(lines);
+       if (string_array == NULL) {
+               ERR("alloc_string_array() fail");
+               return NULL;
+       }
+
+       const char* separator = "\n";
+       char* curr_line;
+       curr_line = strtok(buff, separator);
+       int id = 0;
+
+       while (curr_line != NULL)
+       {
+               strcpy(string_array[id], curr_line);
+               id++;
+               curr_line = strtok(NULL, separator);
+       }
+
+       if (id != lines) {
+               ERR("unexpected error while creating string array");
+               free_string_array(string_array, lines);
+               return NULL;
+       }
+
+       return string_array;
+}
+
+static int compare_string_arrays(int device_size, char** device, int update_size, char** update)
+{
+       bool found;
+       for (int i = 0; i < update_size; i++)
+       {
+               found = false;
+               for (int j = 0; j < device_size; j++)
+               {
+                       if (strcmp(update[i], device[j]) == 0) {
+                               found = true;
+                               break;
+                       }
+               }
+
+               if (!found) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
 int compare_info_files(FILE* update_info_file, FILE* device_info_file)
 {
        size_t update_info_size, device_info_size;
        update_info_size = get_file_size(update_info_file);
        device_info_size = get_file_size(device_info_file);
 
-       if (device_info_size != update_info_size)
-               return 1;
+       // if there are no variables in update-info.ini file, delta should pass verification
+       if (update_info_size == 0) {
+               return 0;
+       }
 
        int ret = 0;
        char* device_read_buf = NULL;
        char* update_read_buf = NULL;
+       char** device_strings = NULL;
+       char** update_strings = NULL;
+       int device_file_lines = 0;
+       int update_file_lines = 0;
 
        update_read_buf = (char*)malloc(update_info_size + 1);
        if (update_read_buf == NULL) {
@@ -237,7 +338,24 @@ int compare_info_files(FILE* update_info_file, FILE* device_info_file)
        }
        device_read_buf[device_info_size] = '\0';
 
-       if (strcmp(update_read_buf, device_read_buf) != 0) {
+       device_file_lines = NO_OF_API_VARIABLES + NO_OF_CONFIG_VARIABLES;
+       update_file_lines = get_newline_count(update_read_buf);
+
+       device_strings = get_string_array(device_read_buf, device_file_lines);
+       if (device_strings == NULL) {
+               ERR("get_string_array() failed for device generated file");
+               ret = -1;
+               goto cleanup;
+       }
+
+       update_strings = get_string_array(update_read_buf, update_file_lines);
+       if (update_strings == NULL) {
+               ERR("get_string_array() failed for file provided with delta");
+               ret = -1;
+               goto cleanup;
+       }
+
+       if (compare_string_arrays(device_file_lines, device_strings, update_file_lines, update_strings) != 0) {
                ret = 1;
        }
 
@@ -246,6 +364,12 @@ cleanup:
                free(update_read_buf);
        if (device_read_buf)
                free(device_read_buf);
+       if (device_strings) {
+               free_string_array(device_strings, device_file_lines);
+       }
+       if (update_strings) {
+               free_string_array(update_strings, update_file_lines);
+       }
        return ret;
 }