Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/utils/ini.c
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2024 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <string.h>
19
20
#include "ini.h"
21
#include <libs/fatfs/ff.h>
22
#include <mem/heap.h>
23
#include <utils/dirlist.h>
24
#include <utils/util.h>
25
26
u32 _find_section_name(char *lbuf, u32 lblen, char schar)
27
{
28
u32 i;
29
// Depends on 'FF_USE_STRFUNC 2' that removes \r.
30
for (i = 0; i < lblen && lbuf[i] != schar && lbuf[i] != '\n'; i++)
31
;
32
lbuf[i] = 0;
33
34
return i;
35
}
36
37
ini_sec_t *_ini_create_section(link_t *dst, ini_sec_t *csec, char *name, u8 type)
38
{
39
if (csec)
40
list_append(dst, &csec->link);
41
42
// Calculate total allocation size.
43
u32 len = name ? strlen(name) + 1 : 0;
44
char *buf = zalloc(sizeof(ini_sec_t) + len);
45
46
csec = (ini_sec_t *)buf;
47
csec->name = strcpy_ns(buf + sizeof(ini_sec_t), name);
48
csec->type = type;
49
50
// Initialize list.
51
list_init(&csec->kvs);
52
53
return csec;
54
}
55
56
int ini_parse(link_t *dst, const char *ini_path, bool is_dir)
57
{
58
FIL fp;
59
u32 lblen;
60
u32 k = 0;
61
u32 pathlen = strlen(ini_path);
62
ini_sec_t *csec = NULL;
63
64
char *lbuf = NULL;
65
dirlist_t *filelist = NULL;
66
char *filename = (char *)malloc(256);
67
68
strcpy(filename, ini_path);
69
70
// Get all ini filenames.
71
if (is_dir)
72
{
73
filelist = dirlist(filename, "*.ini", false, false);
74
if (!filelist)
75
{
76
free(filename);
77
return 0;
78
}
79
strcpy(filename + pathlen, "/");
80
pathlen++;
81
}
82
83
do
84
{
85
// Copy ini filename in path string.
86
if (is_dir)
87
{
88
if (filelist->name[k])
89
{
90
strcpy(filename + pathlen, filelist->name[k]);
91
k++;
92
}
93
else
94
break;
95
}
96
97
// Open ini.
98
if (f_open(&fp, filename, FA_READ) != FR_OK)
99
{
100
free(filelist);
101
free(filename);
102
103
return 0;
104
}
105
106
lbuf = malloc(512);
107
108
do
109
{
110
// Fetch one line.
111
lbuf[0] = 0;
112
f_gets(lbuf, 512, &fp);
113
lblen = strlen(lbuf);
114
115
// Remove trailing newline. Depends on 'FF_USE_STRFUNC 2' that removes \r.
116
if (lblen && lbuf[lblen - 1] == '\n')
117
lbuf[lblen - 1] = 0;
118
119
if (lblen > 2 && lbuf[0] == '[') // Create new section.
120
{
121
_find_section_name(lbuf, lblen, ']');
122
123
csec = _ini_create_section(dst, csec, &lbuf[1], INI_CHOICE);
124
}
125
else if (lblen > 1 && lbuf[0] == '{') // Create new caption. Support empty caption '{}'.
126
{
127
_find_section_name(lbuf, lblen, '}');
128
129
csec = _ini_create_section(dst, csec, &lbuf[1], INI_CAPTION);
130
csec->color = 0xFF0AB9E6;
131
}
132
else if (lblen > 2 && lbuf[0] == '#') // Create comment.
133
{
134
csec = _ini_create_section(dst, csec, &lbuf[1], INI_COMMENT);
135
}
136
else if (lblen < 2) // Create empty line.
137
{
138
csec = _ini_create_section(dst, csec, NULL, INI_NEWLINE);
139
}
140
else if (csec && csec->type == INI_CHOICE) // Extract key/value.
141
{
142
u32 i = _find_section_name(lbuf, lblen, '=');
143
144
// Calculate total allocation size.
145
u32 klen = strlen(&lbuf[0]) + 1;
146
u32 vlen = strlen(&lbuf[i + 1]) + 1;
147
char *buf = zalloc(sizeof(ini_kv_t) + klen + vlen);
148
149
ini_kv_t *kv = (ini_kv_t *)buf;
150
buf += sizeof(ini_kv_t);
151
kv->key = strcpy_ns(buf, &lbuf[0]);
152
buf += klen;
153
kv->val = strcpy_ns(buf, &lbuf[i + 1]);
154
list_append(&csec->kvs, &kv->link);
155
}
156
} while (!f_eof(&fp));
157
158
free(lbuf);
159
160
f_close(&fp);
161
162
if (csec)
163
{
164
list_append(dst, &csec->link);
165
if (is_dir)
166
csec = NULL;
167
}
168
} while (is_dir);
169
170
free(filename);
171
free(filelist);
172
173
return 1;
174
}
175
176
char *ini_check_special_section(ini_sec_t *cfg)
177
{
178
if (cfg == NULL)
179
return NULL;
180
181
LIST_FOREACH_ENTRY(ini_kv_t, kv, &cfg->kvs, link)
182
{
183
if (!strcmp("l4t", kv->key))
184
return ((kv->val[0] == '1') ? (char *)-1 : NULL);
185
else if (!strcmp("payload", kv->key))
186
return kv->val;
187
}
188
189
return NULL;
190
}
191
192
void ini_free(link_t *src)
193
{
194
ini_sec_t *prev_sec = NULL;
195
196
// Parse and free all ini sections.
197
LIST_FOREACH_ENTRY(ini_sec_t, ini_sec, src, link)
198
{
199
ini_kv_t *prev_kv = NULL;
200
201
// Free all ini key allocations if they exist.
202
LIST_FOREACH_ENTRY(ini_kv_t, kv, &ini_sec->kvs, link)
203
{
204
// Free previous key.
205
if (prev_kv)
206
free(prev_kv);
207
208
// Set next key to free.
209
prev_kv = kv;
210
}
211
212
// Free last key.
213
if (prev_kv)
214
free(prev_kv);
215
216
// Free previous section.
217
if (prev_sec)
218
free(prev_sec);
219
220
// Set next section to free.
221
prev_sec = ini_sec;
222
}
223
224
// Free last section.
225
if (prev_sec)
226
free(prev_sec);
227
}
228
229