Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 1 | /* |
Denis 'GNUtoo' Carikli | 780e931 | 2018-01-10 14:35:55 +0100 | [diff] [blame] | 2 | * bincfg - Compiler/Decompiler for data blobs with specs |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 3 | * 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' Carikli | 780e931 | 2018-01-10 14:35:55 +0100 | [diff] [blame] | 20 | #include "bincfg.tab.h" |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 21 | |
| 22 | extern struct blob binary; |
| 23 | |
| 24 | unsigned 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 | |
| 44 | char* stripquotes (char *string) |
| 45 | { |
| 46 | char *stripped; |
| 47 | unsigned int len = strlen(string); |
Martin Roth | 6d189cc | 2017-04-08 17:05:23 -0600 | [diff] [blame] | 48 | 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 Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 55 | return stripped; |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 56 | } |
Martin Roth | 6d189cc | 2017-04-08 17:05:23 -0600 | [diff] [blame] | 57 | return NULL; |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | %} |
| 61 | |
| 62 | %option noyywrap |
| 63 | %option nounput |
| 64 | |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 65 | DIGIT [0-9] |
Martin Roth | 6d189cc | 2017-04-08 17:05:23 -0600 | [diff] [blame] | 66 | INT [-]?{DIGIT}|[-]?[1-9]{DIGIT}+ |
| 67 | FRAC [.]{DIGIT}+ |
| 68 | EXP [eE][+-]?{DIGIT}+ |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 69 | NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP} |
Martin Roth | 6d189cc | 2017-04-08 17:05:23 -0600 | [diff] [blame] | 70 | HEX [0][x][0-9a-fA-F]+ |
| 71 | STRING ["][^"]*["] |
| 72 | COMMENT [#][^\n]*$ |
Damien Zammit | 0685322 | 2016-11-16 21:06:54 +1100 | [diff] [blame] | 73 | |
| 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 | |
| 131 | void set_input_string(char* in) { |
| 132 | yy_scan_string(in); |
| 133 | } |