diff options
author | Steve McIntyre <steve@einval.com> | 2021-03-23 23:49:46 +0000 |
---|---|---|
committer | Steve McIntyre <steve@einval.com> | 2021-03-23 23:49:46 +0000 |
commit | 1251a7ba86fc40a6aad8b4fecdbca2b61808d9fa (patch) | |
tree | 2125fda549aaca55cb49a48d54be77dec7fbf3df /csv.c | |
parent | 85b409232ce89b34626df9d72abedf5d4f5ccef6 (diff) | |
parent | 031e5cce385d3f96b1caa1d53495332a7eb03749 (diff) | |
download | efi-boot-shim-debian/15.3-1.tar.gz efi-boot-shim-debian/15.3-1.zip |
Update upstream source from tag 'upstream/15.3'debian/15.3-1
Update to upstream version '15.3'
with Debian dir 1b484f1c1ac270604a5a1451b34de4b0865c6211
Diffstat (limited to 'csv.c')
-rw-r--r-- | csv.c | 124 |
1 files changed, 124 insertions, 0 deletions
@@ -0,0 +1,124 @@ +// SPDX-License-Identifier: BSD-2-Clause-Patent +/* + * csv.c - CSV parser + */ + +#include "shim.h" + +void NONNULL(1, 3, 4) +parse_csv_line(char * line, size_t max, size_t *n_columns, const char *columns[]) +{ + char *next = line; + size_t n = 0, new_n = n; + const char * const delims = ","; + char state = 0; + char *token = NULL; + + bool valid = true; + for (n = 0; n < *n_columns; n++) { + + if (valid) { + valid = strntoken(next, max, delims, &token, &state); + } + if (valid) { + next += strlen(token) + 1; + max -= strlen(token) + 1; + columns[n] = token; + new_n = n + 1; + } else { + columns[n] = NULL; + continue; + } + } + *n_columns = new_n; +} + +void +free_csv_list(list_t *list) +{ + list_t *pos = NULL, *tmp = NULL; + list_for_each_safe(pos, tmp, list) { + struct csv_row *row; + + row = list_entry(pos, struct csv_row, list); + list_del(&row->list); + FreePool(row); + } +} + +EFI_STATUS +parse_csv_data(char *data, char *data_end, size_t n_columns, list_t *list) +{ + EFI_STATUS efi_status = EFI_OUT_OF_RESOURCES; + char delims[] = "\r\n"; + char *line = data; + size_t max = 0; + char *end = data_end; + + if (!data || !end || end <= data || !n_columns || !list) + return EFI_INVALID_PARAMETER; + + max = (uintptr_t)end - (uintptr_t)line + (end > line ? 1 : 0); + + if (line && is_utf8_bom(line, max)) + line += UTF8_BOM_SIZE; + + while (line && line <= data_end) { + size_t entrysz = sizeof(char *) * n_columns + sizeof(struct csv_row); + struct csv_row *entry; + size_t m_columns = n_columns; + char *delim; + bool found = true; + + end = data_end; + max = (uintptr_t)end - (uintptr_t)line + (end > line ? 1 : 0); + while (max && found) { + found = false; + for (delim = &delims[0]; max && *delim; delim++) { + if (line[0] == *delim) { + line++; + max--; + found = true; + } + } + } + for (delim = &delims[0]; *delim; delim++) { + char *tmp = strnchrnul(line, max, *delim); + if (tmp < end) + end = tmp; + } + max = (uintptr_t)end - (uintptr_t)line + (end > line ? 1 : 0); + *end = '\0'; + + if (line == data_end || max == 0) { + line = end + 1; + continue; + } + + entry = AllocateZeroPool(entrysz); + if (!entry) { + efi_status = EFI_OUT_OF_RESOURCES; + goto err_oom; + } + + INIT_LIST_HEAD(&entry->list); + list_add_tail(&entry->list, list); + + for (delim = &delims[0]; *delim; delim++) { + char *tmp = strnchrnul((const char *)line, max, *delim); + if (tmp < end) + end = tmp; + } + + parse_csv_line(line, max, &m_columns, (const char **)entry->columns); + entry->n_columns = m_columns; + line = end + 1; + } + + return EFI_SUCCESS; +err_oom: + free_csv_list(list); + return efi_status; +} + +// vim:fenc=utf-8:tw=75:noet |