blob: f9e880f333b8a98aec4f09f671e2a15dc7e2139f [file] [log] [blame]
Damien Zammit06853222016-11-16 21:06:54 +11001/*
Denis 'GNUtoo' Carikli780e9312018-01-10 14:35:55 +01002 * bincfg - Compiler/Decompiler for data blobs with specs
Damien Zammit06853222016-11-16 21:06:54 +11003 * Copyright (C) 2017 Damien Zammit <damien@zamaudio.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16%{
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Denis 'GNUtoo' Carikli780e9312018-01-10 14:35:55 +010020#include "bincfg.tab.h"
Damien Zammit06853222016-11-16 21:06:54 +110021
22extern struct blob binary;
23
24unsigned int parsehex (char *s)
25{
26 unsigned int i, nib, val = 0;
27 unsigned int nibs = strlen(s) - 2;
28
29 for (i = 2; i < nibs + 2; i++) {
30 if (s[i] >= '0' && s[i] <= '9') {
31 nib = s[i] - '0';
32 } else if (s[i] >= 'a' && s[i] <= 'f') {
33 nib = s[i] - 'a' + 10;
34 } else if (s[i] >= 'A' && s[i] <= 'F') {
35 nib = s[i] - 'A' + 10;
36 } else {
37 return 0;
38 }
39 val |= nib << (((nibs - 1) - (i - 2)) * 4);
40 }
41 return val;
42}
43
44char* stripquotes (char *string)
45{
46 char *stripped;
47 unsigned int len = strlen(string);
Martin Roth6d189cc2017-04-08 17:05:23 -060048 if (len >= 2 && string[0] == '"' && string[len - 1] == '"') {
49 stripped = (char *) malloc (len - 1);
50 if (stripped == NULL) {
51 printf("Out of memory\n");
52 exit(1);
53 }
54 snprintf (stripped, len - 1, "%s", string + 1);
Damien Zammit06853222016-11-16 21:06:54 +110055 return stripped;
Damien Zammit06853222016-11-16 21:06:54 +110056 }
Martin Roth6d189cc2017-04-08 17:05:23 -060057 return NULL;
Damien Zammit06853222016-11-16 21:06:54 +110058}
59
60%}
61
62%option noyywrap
63%option nounput
64
Damien Zammit06853222016-11-16 21:06:54 +110065DIGIT [0-9]
Martin Roth6d189cc2017-04-08 17:05:23 -060066INT [-]?{DIGIT}|[-]?[1-9]{DIGIT}+
67FRAC [.]{DIGIT}+
68EXP [eE][+-]?{DIGIT}+
Damien Zammit06853222016-11-16 21:06:54 +110069NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
Martin Roth6d189cc2017-04-08 17:05:23 -060070HEX [0][x][0-9a-fA-F]+
71STRING ["][^"]*["]
72COMMENT [#][^\n]*$
Damien Zammit06853222016-11-16 21:06:54 +110073
74%%
75
76{STRING} {
77 yylval.str = stripquotes(yytext);
78 return name;
79};
80
81{NUMBER} {
82 yylval.u32 = atoi(yytext);
83 return val;
84};
85
86{HEX} {
87 yylval.u32 = parsehex(yytext);
88 return val;
89};
90
91\{ {
92 return '{';
93};
94
95\} {
96 return '}';
97};
98
99\[ {
100 return '[';
101};
102
103\] {
104 return ']';
105};
106
107, {
108 return ',';
109};
110
111: {
112 return ':';
113};
114
115= {
116 return '=';
117};
118
119[ \t\n]+ /* ignore whitespace */;
120
121{COMMENT} /* ignore comments */
122
123\% {
124 return '%';
125};
126
127<<EOF>> { return eof; };
128
129%%
130
131void set_input_string(char* in) {
132 yy_scan_string(in);
133}