]>
git.proxmox.com Git - efi-boot-shim.git/blob - csv.c
d141f03543e5b2e0310aaf1fcbe17ccb60679f7e
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
9 parse_csv_line(char * line
, size_t max
, size_t *n_columns
, const char *columns
[])
12 size_t n
= 0, new_n
= n
;
13 const char * const delims
= ",";
18 for (n
= 0; n
< *n_columns
; n
++) {
21 valid
= strntoken(next
, max
, delims
, &token
, &state
);
24 next
+= strlen(token
) + 1;
25 max
-= strlen(token
) + 1;
37 free_csv_list(list_t
*list
)
39 list_t
*pos
= NULL
, *tmp
= NULL
;
40 list_for_each_safe(pos
, tmp
, list
) {
43 row
= list_entry(pos
, struct csv_row
, list
);
50 parse_csv_data(char *data
, char *data_end
, size_t n_columns
, list_t
*list
)
52 EFI_STATUS efi_status
= EFI_OUT_OF_RESOURCES
;
53 char delims
[] = "\r\n";
58 if (!data
|| !end
|| end
<= data
|| !n_columns
|| !list
)
59 return EFI_INVALID_PARAMETER
;
61 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
63 if (line
&& is_utf8_bom(line
, max
))
64 line
+= UTF8_BOM_SIZE
;
66 while (line
&& line
<= data_end
) {
67 size_t entrysz
= sizeof(char *) * n_columns
+ sizeof(struct csv_row
);
68 struct csv_row
*entry
;
69 size_t m_columns
= n_columns
;
74 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
75 while (max
&& found
) {
77 for (delim
= &delims
[0]; max
&& *delim
; delim
++) {
78 if (line
[0] == *delim
) {
85 for (delim
= &delims
[0]; *delim
; delim
++) {
86 char *tmp
= strnchrnul(line
, max
, *delim
);
90 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
93 if (line
== data_end
|| max
== 0) {
98 entry
= AllocateZeroPool(entrysz
);
100 efi_status
= EFI_OUT_OF_RESOURCES
;
104 INIT_LIST_HEAD(&entry
->list
);
105 list_add_tail(&entry
->list
, list
);
107 for (delim
= &delims
[0]; *delim
; delim
++) {
108 char *tmp
= strnchrnul((const char *)line
, max
, *delim
);
113 parse_csv_line(line
, max
, &m_columns
, (const char **)entry
->columns
);
114 entry
->n_columns
= m_columns
;
124 // vim:fenc=utf-8:tw=75:noet