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) {
}
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;
}
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;
}