1 /* bincfg - Compiler/Decompiler for data blobs with specs */
2 /* SPDX-License-Identifier: GPL-3.0-or-later */
8 #include "bincfg.tab.h"
10 extern struct blob binary;
12 unsigned int parsehex (char *s)
14 unsigned int i, nib, retval = 0;
15 unsigned int nibs = strlen(s) - 2;
17 for (i = 2; i < nibs + 2; i++) {
18 if (s[i] >= '0' && s[i] <= '9') {
20 } else if (s[i] >= 'a' && s[i] <= 'f') {
21 nib = s[i] - 'a' + 10;
22 } else if (s[i] >= 'A' && s[i] <= 'F') {
23 nib = s[i] - 'A' + 10;
27 retval |= nib << (((nibs - 1) - (i - 2)) * 4);
32 char* stripquotes (char *string)
35 unsigned int len = strlen(string);
36 if (len >= 2 && string[0] == '"' && string[len - 1] == '"') {
37 stripped = (char *) malloc (len - 1);
38 if (stripped == NULL) {
39 printf("Out of memory\n");
42 snprintf (stripped, len - 1, "%s", string + 1);
54 INT [-]?{DIGIT}|[-]?[1-9]{DIGIT}+
57 NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
58 HEX [0][x][0-9a-fA-F]+
65 yylval.str = stripquotes(yytext);
70 yylval.u32 = atoi(yytext);
75 yylval.u32 = parsehex(yytext);
107 [ \t\n]+ /* ignore whitespace */;
109 {COMMENT} /* ignore comments */
115 <<EOF>> { return eof; };
119 void set_input_string(char* in) {