]>
git.proxmox.com Git - efi-boot-shim.git/blob - csv.c
f6b37f150f09eaaccd352d1b2df60fbc83b45521
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 dprint(L
"data:0x%lx end:0x%lx n_columns:%lu list:0x%lx\n",
60 data
, end
, n_columns
, list
);
61 return EFI_INVALID_PARAMETER
;
64 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
66 if (line
&& is_utf8_bom(line
, max
))
67 line
+= UTF8_BOM_SIZE
;
69 while (line
&& line
<= data_end
) {
70 size_t entrysz
= sizeof(char *) * n_columns
+ sizeof(struct csv_row
);
71 struct csv_row
*entry
;
72 size_t m_columns
= n_columns
;
77 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
78 while (max
&& found
) {
80 for (delim
= &delims
[0]; max
&& *delim
; delim
++) {
81 if (line
[0] == *delim
) {
88 for (delim
= &delims
[0]; *delim
; delim
++) {
89 char *tmp
= strnchrnul(line
, max
, *delim
);
93 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
96 if (line
== data_end
|| max
== 0) {
101 entry
= AllocateZeroPool(entrysz
);
103 efi_status
= EFI_OUT_OF_RESOURCES
;
107 INIT_LIST_HEAD(&entry
->list
);
108 list_add_tail(&entry
->list
, list
);
110 for (delim
= &delims
[0]; *delim
; delim
++) {
111 char *tmp
= strnchrnul((const char *)line
, max
, *delim
);
116 parse_csv_line(line
, max
, &m_columns
, (const char **)entry
->columns
);
117 entry
->n_columns
= m_columns
;
127 // vim:fenc=utf-8:tw=75:noet