From 56b35046fce8fcad05e77af062e949e091c19c4c Mon Sep 17 00:00:00 2001 From: Antoni Adaszkiewicz Date: Thu, 8 Dec 2022 14:12:42 +0100 Subject: [PATCH] delta-verifier: Compare variables only form file from delta. 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 | 130 +++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 3 deletions(-) diff --git a/delta-verifier/delta-verifier.c b/delta-verifier/delta-verifier.c index 1fe459c..9beddfa 100644 --- a/delta-verifier/delta-verifier.c +++ b/delta-verifier/delta-verifier.c @@ -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; } -- 2.34.1