Skip to content

Commit acacb0a

Browse files
committed
test
1 parent 13067d9 commit acacb0a

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

tools/rimage/src/toml_utils.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,104 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
166166
return (uint32_t)val;
167167
}
168168

169+
/**
170+
* Parse hex value from key in given toml table
171+
* @param table toml table where key is specified
172+
* @param ctx parsing context, key counter will be incremented after successful key parse
173+
* @param key field name
174+
* @param def is default value or -1 when value don't have default value
175+
* @param error code, 0 when success
176+
* @return default, parsed, or UINT16_MAX value for error cases
177+
*/
178+
uint16_t parse_uint16_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
179+
const char *key, int64_t def, int *error)
180+
{
181+
toml_raw_t raw;
182+
char *temp_s;
183+
unsigned long val; /* strtoul return type */
184+
int ret;
185+
186+
raw = toml_raw_in(table, key);
187+
if (!raw) {
188+
if (def < 0 || def > UINT16_MAX) {
189+
*error = err_key_not_found(key);
190+
return UINT16_MAX;
191+
}
192+
*error = 0;
193+
return (uint16_t)def;
194+
}
195+
196+
ret = toml_rtos(raw, &temp_s);
197+
if (ret < 0) {
198+
*error = err_key_parse(key, NULL);
199+
return UINT16_MAX;
200+
}
201+
val = strtoul(temp_s, 0, 0);
202+
free(temp_s);
203+
204+
if (errno < 0) {
205+
*error = err_key_parse(key, "can't convert hex value");
206+
return UINT16_MAX;
207+
}
208+
if (val > UINT16_MAX) {
209+
*error = log_err(-ERANGE, "key %s out of uint16_t hex range", key);
210+
return UINT16_MAX;
211+
}
212+
213+
*error = 0;
214+
++ctx->key_cnt;
215+
return (uint16_t)val;
216+
}
217+
218+
/**
219+
* Parse hex value from key in given toml table
220+
* @param table toml table where key is specified
221+
* @param ctx parsing context, key counter will be incremented after successful key parse
222+
* @param key field name
223+
* @param def is default value or -1 when value don't have default value
224+
* @param error code, 0 when success
225+
* @return default, parsed, or UINT8_MAX value for error cases
226+
*/
227+
uint8_t parse_uint8_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
228+
const char *key, int64_t def, int *error)
229+
{
230+
toml_raw_t raw;
231+
char *temp_s;
232+
unsigned long val;
233+
int ret;
234+
235+
raw = toml_raw_in(table, key);
236+
if (!raw) {
237+
if (def < 0 || def > UINT8_MAX) {
238+
*error = err_key_not_found(key);
239+
return UINT8_MAX;
240+
}
241+
*error = 0;
242+
return (uint8_t)def;
243+
}
244+
245+
ret = toml_rtos(raw, &temp_s);
246+
if (ret < 0) {
247+
*error = err_key_parse(key, NULL);
248+
return UINT8_MAX;
249+
}
250+
val = strtoul(temp_s, 0, 0);
251+
free(temp_s);
252+
253+
if (errno < 0) {
254+
*error = err_key_parse(key, "can't convert hex value");
255+
return UINT8_MAX;
256+
}
257+
if (val > UINT8_MAX) {
258+
*error = log_err(-ERANGE, "key %s out of uint8_t hex range", key);
259+
return UINT8_MAX;
260+
}
261+
262+
*error = 0;
263+
++ctx->key_cnt;
264+
return (uint8_t)val;
265+
}
266+
169267
/**
170268
* Parse integer value from key in given toml table
171269
* @param table toml table where key is specified
@@ -210,6 +308,48 @@ uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, cons
210308
return (uint32_t)val;
211309
}
212310

311+
/**
312+
* Parse unsigned 8-bit integer value from key in given toml table.
313+
* @param table toml table where key is specified
314+
* @param ctx parsing context, key counter will be incremented after successful key parse
315+
* @param key field name
316+
* @param def is default value or -1 when value don't have default value
317+
* @param error code, 0 when success
318+
* @return default, parsed, or UINT8_MAX value for error cases
319+
*/
320+
uint8_t parse_uint8_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
321+
int64_t def, int *error)
322+
{
323+
toml_raw_t raw;
324+
int64_t val;
325+
int ret;
326+
327+
raw = toml_raw_in(table, key);
328+
if (!raw) {
329+
if (def < 0 || def > UINT8_MAX) {
330+
*error = err_key_not_found(key);
331+
return UINT8_MAX;
332+
} else {
333+
*error = 0;
334+
return (uint8_t)def;
335+
}
336+
}
337+
338+
ret = toml_rtoi(raw, &val);
339+
if (ret < 0) {
340+
*error = err_key_parse(key, "can't convert to integer value");
341+
return UINT8_MAX;
342+
}
343+
if (val < 0 || val > UINT8_MAX) {
344+
*error = log_err(-ERANGE, "key %s out of uint8_t range", key);
345+
return UINT8_MAX;
346+
}
347+
348+
*error = 0;
349+
++ctx->key_cnt;
350+
return (uint8_t)val;
351+
}
352+
213353
/**
214354
* Parse string value from key in given toml table to uint8_t array. The
215355
* destination is NOT a string because it is padded with zeros if and

0 commit comments

Comments
 (0)