]>
git.proxmox.com Git - efi-boot-shim.git/blob - csv.c
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
= ",";
19 for (n
= 0; n
< *n_columns
; n
++) {
22 valid
= strntoken(next
, max
, delims
, &token
, &state
);
25 next
+= strlen(token
) + 1;
26 max
-= strlen(token
) + 1;
38 free_csv_list(list_t
*list
)
40 list_t
*pos
= NULL
, *tmp
= NULL
;
41 list_for_each_safe(pos
, tmp
, list
) {
44 row
= list_entry(pos
, struct csv_row
, list
);
51 parse_csv_data(char *data
, char *data_end
, size_t n_columns
, list_t
*list
)
53 EFI_STATUS efi_status
= EFI_OUT_OF_RESOURCES
;
54 char delims
[] = "\r\n";
59 if (!data
|| !end
|| end
<= data
|| !n_columns
|| !list
) {
60 dprint(L
"data:0x%lx end:0x%lx n_columns:%lu list:0x%lx\n",
61 data
, end
, n_columns
, list
);
62 return EFI_INVALID_PARAMETER
;
65 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
66 if (is_utf8_bom(line
, max
))
68 line
+= UTF8_BOM_SIZE
;
70 while (line
<= data_end
&& *line
) {
71 size_t entrysz
= sizeof(char *) * n_columns
+ sizeof(struct csv_row
);
72 struct csv_row
*entry
;
73 size_t m_columns
= n_columns
;
79 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
80 /* Skip the delimiter(s) of the previous line */
81 while (max
&& found
) {
83 for (delim
= &delims
[0]; max
&& *delim
; delim
++) {
84 if (line
[0] == *delim
) {
91 /* Find the first delimiter of the current line */
92 for (delim
= &delims
[0]; *delim
; delim
++) {
93 char *tmp
= strnchrnul(line
, max
, *delim
);
97 max
= (uintptr_t)end
- (uintptr_t)line
+ (end
> line
? 1 : 0);
103 if (line
== data_end
|| max
== 0) {
108 entry
= AllocateZeroPool(entrysz
);
110 efi_status
= EFI_OUT_OF_RESOURCES
;
114 INIT_LIST_HEAD(&entry
->list
);
115 list_add_tail(&entry
->list
, list
);
117 for (delim
= &delims
[0]; *delim
; delim
++) {
118 char *tmp
= strnchrnul((const char *)line
, max
, *delim
);
123 parse_csv_line(line
, max
, &m_columns
, (const char **)entry
->columns
);
124 entry
->n_columns
= m_columns
;
137 // vim:fenc=utf-8:tw=75:noet