1 /* gf256_table_gen.c */
3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * \file gf256_table_gen.c
21 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
39 char* fmt_strings[]={"%d", "%3d",
46 int main(int argc, char** argv){
51 static struct option long_options[] =
53 /* These options set a flag. */
54 {"print-header", no_argument, &print_header, 1},
55 {"no-print-header", no_argument, &print_header, 0},
56 {"print-braces", no_argument, &print_braces, 1},
57 {"no-print-braces", no_argument, &print_braces, 0},
58 {"print-dec", no_argument, &print_base, 0},
59 {"print-hex", no_argument, &print_base, 1},
60 {"print-octal", no_argument, &print_base, 2},
61 {"align", no_argument, &print_align, 1},
62 {"no-align", no_argument, &print_align, 0},
63 {"print-prefix", no_argument, &print_prefix, 1},
64 {"no-print-prefix", no_argument, &print_prefix, 0},
66 /* These options don't set a flag.
67 We distinguish them by their indices. */
68 {"reducer", required_argument, 0, 'r'},
69 {"min", required_argument, 0, 'l'},
70 {"max", required_argument, 0, 'u'},
71 {"format", required_argument, 0, 'f'},
79 unsigned long ul_reducer=0x1b, max=0xff, min=0x00;
84 c = getopt_long(argc, argv,"cCbBdhoaApPr:l:u:f:",
85 long_options, &option_index);
87 case 'c': print_header=1; break;
88 case 'C': print_header=0; break;
89 case 'b': print_braces=1; break;
90 case 'B': print_braces=0; break;
91 case 'h': print_base = 1; break;
92 case 'd': print_base = 0; break;
93 case 'o': print_base = 2; break;
94 case 'a': print_align =1; break;
95 case 'A': print_align =0; break;
96 case 'p': print_prefix=1; break;
97 case 'P': print_prefix=0; break;
98 case 'r': ul_reducer = strtoul(optarg, eptr, 0);
100 fprintf(stderr, "Error, invalid reducer value \"%s\"!\n",
105 case 'l': min = strtoul(optarg, eptr, 0);
106 if(((**eptr)!='\0')||min>0xff){
107 fprintf(stderr, "Error, invalid minimum value \"%s\"!\n",
112 case 'u': max = strtoul(optarg, eptr, 0);
113 if(((**eptr)!='\0')||max>0xff){
114 fprintf(stderr, "Error, invalid maximum value \"%s\"!\n",
119 case 'f': fmt = optarg; break;
123 reducer = ul_reducer&0xff;
125 fmt = fmt_strings[print_prefix*6+print_base*2+print_align];
127 for(i=optind; i<argc; ++i){
128 ul_a=strtoul(argv[i], eptr, 0)&0xff;
129 if(((**eptr)!='\0')|| ul_a>0xff){
130 fprintf(stderr, "Error, invalid number \"%s\"!\n", argv[i]);
135 fprintf(of,"\nuint8_t lut_gfmul_%s_%2.2x",argv[i],reducer);
137 fprintf(of," = {\n\t");
139 for(j=min; j<=max-1; ++j){
140 fprintf(of,fmt,gf256mul(a, j, reducer));
141 if(j%columns==columns-1){
142 fprintf(of, ",\n\t");
147 fprintf(of, fmt, gf256mul(a,max, reducer));