blob: e55d6c609a10a36b66fc8720dd3f5cb45f11feb9 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* bincfg - Compiler/Decompiler for data blobs with specs */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-3.0-or-later */
Damien Zammit06853222016-11-16 21:06:54 +11003
4%{
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
Denis 'GNUtoo' Carikli780e9312018-01-10 14:35:55 +01008#include "bincfg.tab.h"
Damien Zammit06853222016-11-16 21:06:54 +11009
10extern struct blob binary;
11
12unsigned int parsehex (char *s)
13{
Martin Rothdb4719f2021-02-13 23:19:51 -070014 unsigned int i, nib, retval = 0;
Damien Zammit06853222016-11-16 21:06:54 +110015 unsigned int nibs = strlen(s) - 2;
16
17 for (i = 2; i < nibs + 2; i++) {
18 if (s[i] >= '0' && s[i] <= '9') {
19 nib = s[i] - '0';
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;
24 } else {
25 return 0;
26 }
Martin Rothdb4719f2021-02-13 23:19:51 -070027 retval |= nib << (((nibs - 1) - (i - 2)) * 4);
Damien Zammit06853222016-11-16 21:06:54 +110028 }
Martin Rothdb4719f2021-02-13 23:19:51 -070029 return retval;
Damien Zammit06853222016-11-16 21:06:54 +110030}
31
32char* stripquotes (char *string)
33{
34 char *stripped;
35 unsigned int len = strlen(string);
Martin Roth6d189cc2017-04-08 17:05:23 -060036 if (len >= 2 && string[0] == '"' && string[len - 1] == '"') {
37 stripped = (char *) malloc (len - 1);
38 if (stripped == NULL) {
39 printf("Out of memory\n");
40 exit(1);
41 }
42 snprintf (stripped, len - 1, "%s", string + 1);
Damien Zammit06853222016-11-16 21:06:54 +110043 return stripped;
Damien Zammit06853222016-11-16 21:06:54 +110044 }
Martin Roth6d189cc2017-04-08 17:05:23 -060045 return NULL;
Damien Zammit06853222016-11-16 21:06:54 +110046}
47
48%}
49
50%option noyywrap
51%option nounput
52
Damien Zammit06853222016-11-16 21:06:54 +110053DIGIT [0-9]
Martin Roth6d189cc2017-04-08 17:05:23 -060054INT [-]?{DIGIT}|[-]?[1-9]{DIGIT}+
55FRAC [.]{DIGIT}+
56EXP [eE][+-]?{DIGIT}+
Damien Zammit06853222016-11-16 21:06:54 +110057NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
Martin Roth6d189cc2017-04-08 17:05:23 -060058HEX [0][x][0-9a-fA-F]+
59STRING ["][^"]*["]
60COMMENT [#][^\n]*$
Damien Zammit06853222016-11-16 21:06:54 +110061
62%%
63
64{STRING} {
65 yylval.str = stripquotes(yytext);
66 return name;
67};
68
69{NUMBER} {
70 yylval.u32 = atoi(yytext);
71 return val;
72};
73
74{HEX} {
75 yylval.u32 = parsehex(yytext);
76 return val;
77};
78
79\{ {
80 return '{';
81};
82
83\} {
84 return '}';
85};
86
87\[ {
88 return '[';
89};
90
91\] {
92 return ']';
93};
94
95, {
96 return ',';
97};
98
99: {
100 return ':';
101};
102
103= {
104 return '=';
105};
106
107[ \t\n]+ /* ignore whitespace */;
108
109{COMMENT} /* ignore comments */
110
111\% {
112 return '%';
113};
114
115<<EOF>> { return eof; };
116
117%%
118
119void set_input_string(char* in) {
120 yy_scan_string(in);
121}