forked from tpircher-zz/pycrc
/
crc_symtable.py
1364 lines (1224 loc) · 43.8 KB
/
crc_symtable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: Latin-1 -*-
# pycrc -- parameterisable CRC calculation utility and C source code generator
#
# Copyright (c) 2006-2012 Thomas Pircher <tehpeh@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
"""
Symbol table for the macro processor used by pycrc.
use as follows:
from crc_opt import Options
from crc_symtable import SymbolTable
opt = Options("0.6")
sym = SymbolTable(opt)
str = " .... "
terminal = sym.getTerminal(str)
"""
from crc_algorithms import Crc
import time
import os
# Class SymbolTable
###############################################################################
class SymbolTable:
"""
The symbol table class.
"""
opt = None
table = {}
# Class constructor
###############################################################################
def __init__(self, opt):
"""
The class constructor.
"""
self.opt = opt
self.table["nop"] = ""
self.table["datetime"] = time.asctime()
self.table["program_version"] = self.opt.VersionStr
self.table["program_url"] = self.opt.WebAddress
if self.opt.OutputFile == None:
self.table["filename"] = "pycrc_stdout"
else:
self.table["filename"] = os.path.basename(self.opt.OutputFile)
self.table["header_filename"] = self.__pretty_header_filename(self.opt.OutputFile)
self.table["crc_width"] = self.__pretty_str(self.opt.Width)
self.table["crc_poly"] = self.__pretty_hex(self.opt.Poly, self.opt.Width)
self.table["crc_reflect_in"] = self.__pretty_bool(self.opt.ReflectIn)
self.table["crc_xor_in"] = self.__pretty_hex(self.opt.XorIn, self.opt.Width)
self.table["crc_reflect_out"] = self.__pretty_bool(self.opt.ReflectOut)
self.table["crc_xor_out"] = self.__pretty_hex(self.opt.XorOut, self.opt.Width)
self.table["crc_table_idx_width"] = str(self.opt.TableIdxWidth)
self.table["crc_table_width"] = str(1 << self.opt.TableIdxWidth)
self.table["crc_table_mask"] = self.__pretty_hex(self.opt.TableWidth - 1, 8)
self.table["crc_mask"] = self.__pretty_hex(self.opt.Mask, self.opt.Width)
self.table["crc_msb_mask"] = self.__pretty_hex(self.opt.MSB_Mask, self.opt.Width)
if self.opt.Algorithm == self.opt.Algo_Table_Driven and (self.opt.Width == None or self.opt.Width < 8):
self.table["crc_do_shift"] = self.__pretty_bool(True)
if self.opt.Width == None:
self.table["crc_shift"] = self.__pretty_str(None)
else:
self.table["crc_shift"] = self.__pretty_str(8 - self.opt.Width)
else:
self.table["crc_do_shift"] = self.__pretty_bool(False)
self.table["crc_shift"] = self.__pretty_str(0)
self.table["cfg_width"] = "$if ($crc_width != Undefined) {:$crc_width:} $else {:cfg->width:}"
self.table["cfg_poly"] = "$if ($crc_poly != Undefined) {:$crc_poly:} $else {:cfg->poly:}"
self.table["cfg_poly_shifted"] = "$if ($crc_do_shift == True) {:($cfg_poly << $cfg_shift):} $else {:$cfg_poly:}"
self.table["cfg_reflect_in"] = "$if ($crc_reflect_in != Undefined) {:$crc_reflect_in:} $else {:cfg->reflect_in:}"
self.table["cfg_xor_in"] = "$if ($crc_xor_in != Undefined) {:$crc_xor_in:} $else {:cfg->xor_in:}"
self.table["cfg_reflect_out"] = "$if ($crc_reflect_out != Undefined) {:$crc_reflect_out:} $else {:cfg->reflect_out:}"
self.table["cfg_xor_out"] = "$if ($crc_xor_out != Undefined) {:$crc_xor_out:} $else {:cfg->xor_out:}"
self.table["cfg_table_idx_width"] = "$if ($crc_table_idx_width != Undefined) {:$crc_table_idx_width:} $else {:cfg->table_idx_width:}"
self.table["cfg_table_width"] = "$if ($crc_table_width != Undefined) {:$crc_table_width:} $else {:cfg->table_width:}"
self.table["cfg_mask"] = "$if ($crc_mask != Undefined) {:$crc_mask:} $else {:cfg->crc_mask:}"
self.table["cfg_mask_shifted"] = "$if ($crc_do_shift == True) {:($cfg_mask << $cfg_shift):} $else {:$cfg_mask:}"
self.table["cfg_msb_mask"] = "$if ($crc_msb_mask != Undefined) {:$crc_msb_mask:} $else {:cfg->msb_mask:}"
self.table["cfg_msb_mask_shifted"] = "$if ($crc_do_shift == True) {:($cfg_msb_mask << $cfg_shift):} $else {:$cfg_msb_mask:}"
self.table["cfg_shift"] = "$if ($crc_shift != Undefined) {:$crc_shift:} $else {:cfg->crc_shift:}"
self.table["undefined_parameters"] = self.__pretty_bool(self.opt.UndefinedCrcParameters)
self.table["use_cfg_t"] = self.__pretty_bool(self.opt.UndefinedCrcParameters)
self.table["c_std"] = self.opt.CStd
self.table["c_bool"] = "$if ($c_std == C89) {:int:} $else {:bool:}"
self.table["c_true"] = "$if ($c_std == C89) {:1:} $else {:true:}"
self.table["c_false"] = "$if ($c_std == C89) {:0:} $else {:false:}"
self.table["underlying_crc_t"] = self.__get_underlying_crc_t()
self.table["include_file"] = self.__get_include_file()
self.table["crc_prefix"] = self.opt.SymbolPrefix
self.table["crc_t"] = self.opt.SymbolPrefix + "t"
self.table["cfg_t"] = self.opt.SymbolPrefix + "cfg_t"
self.table["crc_reflect_function"] = self.opt.SymbolPrefix + "reflect"
self.table["crc_table_gen_function"] = self.opt.SymbolPrefix + "table_gen"
self.table["crc_init_function"] = self.opt.SymbolPrefix + "init"
self.table["crc_update_function"] = self.opt.SymbolPrefix + "update"
self.table["crc_finalize_function"] = self.opt.SymbolPrefix + "finalize"
# function getTerminal
###############################################################################
def getTerminal(self, id):
"""
Return the expanded terminal, if it exists or None otherwise.
"""
if id != None:
if id == "":
return ""
if id in self.table:
return self.table[id]
key = self.__getTerminal(id)
if key != None:
self.table[id] = key
return key
raise LookupError
# function __getTerminal
###############################################################################
def __getTerminal(self, id):
"""
Return the expanded terminal, if it exists or None otherwise.
"""
if id == "constant_crc_init":
if self.__get_init_value() == None:
return self.__pretty_bool(False)
else:
return self.__pretty_bool(True)
if id == "constant_crc_table":
if self.opt.Width != None and self.opt.Poly != None and self.opt.ReflectIn != None:
return self.__pretty_bool(True)
else:
return self.__pretty_bool(False)
elif id == "simple_crc_update_def":
if self.opt.Algorithm in set([self.opt.Algo_Bit_by_Bit, self.opt.Algo_Bit_by_Bit_Fast]):
if self.opt.Width != None and self.opt.Poly != None and self.opt.ReflectIn != None:
return self.__pretty_bool(True)
elif self.opt.Algorithm == self.opt.Algo_Table_Driven:
if self.opt.Width != None and self.opt.ReflectIn != None:
return self.__pretty_bool(True)
return self.__pretty_bool(False)
elif id == "inline_crc_finalize":
if self.opt.Algorithm in set([self.opt.Algo_Bit_by_Bit_Fast, self.opt.Algo_Table_Driven]) and \
(self.opt.Width != None and self.opt.ReflectIn != None and self.opt.ReflectOut != None and self.opt.XorOut != None):
return self.__pretty_bool(True)
else:
return self.__pretty_bool(False)
elif id == "simple_crc_finalize_def":
if self.opt.Algorithm == self.opt.Algo_Bit_by_Bit:
if self.opt.Width != None and self.opt.Poly != None and self.opt.ReflectOut != None and self.opt.XorOut != None:
return self.__pretty_bool(True)
elif self.opt.Algorithm == self.opt.Algo_Bit_by_Bit_Fast:
if self.opt.Width != None and self.opt.ReflectOut != None and self.opt.XorOut != None:
return self.__pretty_bool(True)
elif self.opt.Algorithm == self.opt.Algo_Table_Driven:
if self.opt.Width != None and self.opt.ReflectIn != None and self.opt.ReflectOut != None and self.opt.XorOut != None:
return self.__pretty_bool(True)
return self.__pretty_bool(False)
elif id == "use_reflect_func":
if self.opt.ReflectOut == False and self.opt.ReflectIn == False:
return self.__pretty_bool(False)
else:
return self.__pretty_bool(True)
elif id == "static_reflect_func":
if self.opt.Algorithm == self.opt.Algo_Table_Driven:
return self.__pretty_bool(False)
elif self.opt.ReflectOut != None and self.opt.Algorithm == self.opt.Algo_Bit_by_Bit_Fast:
return self.__pretty_bool(False)
else:
return self.__pretty_bool(True)
elif id == "crc_algorithm":
if self.opt.Algorithm == self.opt.Algo_Bit_by_Bit:
return "bit-by-bit"
elif self.opt.Algorithm == self.opt.Algo_Bit_by_Bit_Fast:
return "bit-by-bit-fast"
elif self.opt.Algorithm == self.opt.Algo_Table_Driven:
return "table-driven"
else:
return "UNDEFINED"
elif id == "crc_table_init":
return self.__get_table_init()
elif id == "crc_table_core_algorithm_ni":
return self.__get_table_core_algorithm_ni()
elif id == "crc_table_core_algorithm_ri":
return self.__get_table_core_algorithm_ri()
elif id == "header_protection":
return self.__pretty_hdrprotection()
elif id == "crc_init_value":
ret = self.__get_init_value()
if ret == None:
return ""
else:
return ret
elif id == "crc_final_value":
return """\
$if ($crc_algorithm == "table-driven") {:
$if ($crc_reflect_in == $crc_reflect_out) {:
$if ($crc_do_shift == True) {:(crc >> $cfg_shift):} $else {:crc:} ^ $crc_xor_out\
:} $else {:
$crc_reflect_function($if ($crc_do_shift == True) {:(crc >> $cfg_shift):} $else {:crc:}, $crc_width) ^ $crc_xor_out\
:}:} $elif ($crc_reflect_out == True) {:
$crc_reflect_function(crc, $crc_width) ^ $crc_xor_out\
:} $else {:
crc ^ $crc_xor_out\
:}\
"""
elif id == "h_template":
return """\
$source_header
#ifndef $header_protection
#define $header_protection
$if ($include_file != Undefined) {:
#include $include_file
:}
#include <stdlib.h>
$if ($c_std != C89) {:
#include <stdint.h>
:}
$if ($undefined_parameters == True and $c_std != C89) {:
#include <stdbool.h>
:}
#ifdef __cplusplus
extern "C" {
#endif
/**
* The definition of the used algorithm.
*****************************************************************************/
$if ($crc_algorithm == "bit-by-bit") {:
#define CRC_ALGO_BIT_BY_BIT 1
:} $elif ($crc_algorithm == "bit-by-bit-fast") {:
#define CRC_ALGO_BIT_BY_BIT_FAST 1
:} $elif ($crc_algorithm == "table-driven") {:
#define CRC_ALGO_TABLE_DRIVEN 1
:}
/**
* The type of the CRC values.
*
* This type must be big enough to contain at least $cfg_width bits.
*****************************************************************************/
typedef $underlying_crc_t $crc_t;
$if ($undefined_parameters == True) {:
/**
* The configuration type of the CRC algorithm.
*****************************************************************************/
typedef struct {
$if ($crc_width == Undefined) {:
unsigned int width; /*!< The width of the polynomial */
:}
$if ($crc_poly == Undefined) {:
$crc_t poly; /*!< The CRC polynomial */
:}
$if ($crc_reflect_in == Undefined) {:
$c_bool reflect_in; /*!< Whether the input shall be reflected or not */
:}
$if ($crc_xor_in == Undefined) {:
$crc_t xor_in; /*!< The initial value of the algorithm */
:}
$if ($crc_reflect_out == Undefined) {:
$c_bool reflect_out; /*!< Wether the output shall be reflected or not */
:}
$if ($crc_xor_out == Undefined) {:
$crc_t xor_out; /*!< The value which shall be XOR-ed to the final CRC value */
:}
$if ($crc_width == Undefined) {:
/* internal parameters */
$crc_t msb_mask; /*!< a bitmask with the Most Significant Bit set to 1
initialise as 1UL << (width - 1) */
$crc_t crc_mask; /*!< a bitmask with all width bits set to 1
initialise as (cfg->msb_mask - 1) | cfg->msb_mask */
unsigned int crc_shift; /*!< a shift count that is used when width < 8
initialise as cfg->width < 8 ? 8 - cfg->width : 0 */
:}
} $cfg_t;
:}
$if ($use_reflect_func == True and $static_reflect_func != True) {:
$crc_reflect_doc
$crc_reflect_function_def;
:}
$if ($crc_algorithm == "table-driven" and $constant_crc_table != True) {:
$crc_table_gen_doc
$crc_table_gen_function_def;
:}
$crc_init_doc
$if ($constant_crc_init == False) {:
$crc_init_function_def;
:} $elif ($c_std == C89) {:
#define $crc_init_function() ($crc_init_value$if ($crc_do_shift == True) {: << $cfg_shift:})
:} $else {:
static inline $crc_init_function_def$nop
{
return $crc_init_value$if ($crc_do_shift == True) {: << $cfg_shift:};
}
:}
$crc_update_doc
$crc_update_function_def;
$crc_finalize_doc
$if ($inline_crc_finalize == True) {:
$if ($c_std == C89) {:
#define $crc_finalize_function(crc) ($crc_final_value)
:} $else {:
static inline $crc_finalize_function_def$nop
{
return $crc_final_value;
}
:}
:} $else {:
$crc_finalize_function_def;
:}
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
#endif /* $header_protection */
"""
elif id == "source_header":
return """\
/**
* \\file $filename
* Functions and types for CRC checks.
*
* Generated on $datetime,
* by $program_version, $program_url
* using the configuration:
* Width = $crc_width
* Poly = $crc_poly
* XorIn = $crc_xor_in
* ReflectIn = $crc_reflect_in
* XorOut = $crc_xor_out
* ReflectOut = $crc_reflect_out
* Algorithm = $crc_algorithm
*****************************************************************************/\
"""
elif id == "crc_reflect_doc":
return """\
/**
* Reflect all bits of a \\a data word of \\a data_len bytes.
*
* \\param data The data word to be reflected.
* \\param data_len The width of \\a data expressed in number of bits.
* \\return The reflected data.
*****************************************************************************/\
"""
elif id == "crc_reflect_function_def":
return """\
$crc_t $crc_reflect_function($crc_t data, size_t data_len)\
"""
elif id == "crc_reflect_function_body":
return """\
$if ($crc_reflect_in == Undefined or $crc_reflect_in == True or $crc_reflect_out == Undefined or $crc_reflect_out == True) {:
$crc_reflect_doc
$crc_reflect_function_def$nop
{
unsigned int i;
$crc_t ret;
ret = data & 0x01;
for (i = 1; i < data_len; i++) {
data >>= 1;
ret = (ret << 1) | (data & 0x01);
}
return ret;
}
:}\
"""
elif id == "crc_table_gen_doc":
return """\
/**
* Populate the private static crc table.
*
* \\param cfg A pointer to a initialised $cfg_t structure.
* \\return void
*****************************************************************************/\
"""
elif id == "crc_table_gen_function_def":
return """\
void $crc_table_gen_function(const $cfg_t *cfg)\
"""
elif id == "crc_init_doc":
return """\
/**
* Calculate the initial crc value.
*
$if ($use_cfg_t == True) {:
* \\param cfg A pointer to a initialised $cfg_t structure.
:}
* \\return The initial crc value.
*****************************************************************************/\
"""
elif id == "crc_init_function_def":
return """\
$if ($constant_crc_init == False) {:
$crc_t $crc_init_function(const $cfg_t *cfg)\
:} $else {:
$crc_t $crc_init_function(void)\
:}\
"""
elif id == "crc_update_doc":
return """\
/**
* Update the crc value with new data.
*
* \\param crc The current crc value.
$if ($simple_crc_update_def != True) {:
* \\param cfg A pointer to a initialised $cfg_t structure.
:}
* \\param data Pointer to a buffer of \\a data_len bytes.
* \\param data_len Number of bytes in the \\a data buffer.
* \\return The updated crc value.
*****************************************************************************/\
"""
elif id == "crc_update_function_def":
return """\
$if ($simple_crc_update_def != True) {:
$crc_t $crc_update_function(const $cfg_t *cfg, $crc_t crc, const unsigned char *data, size_t data_len)\
:} $else {:
$crc_t $crc_update_function($crc_t crc, const unsigned char *data, size_t data_len)\
:}\
"""
elif id == "crc_finalize_doc":
return """\
/**
* Calculate the final crc value.
*
$if ($simple_crc_finalize_def != True) {:
* \\param cfg A pointer to a initialised $cfg_t structure.
:}
* \\param crc The current crc value.
* \\return The final crc value.
*****************************************************************************/\
"""
elif id == "crc_finalize_function_def":
return """\
$if ($simple_crc_finalize_def != True) {:
$crc_t $crc_finalize_function(const $cfg_t *cfg, $crc_t crc)\
:} $else {:
$crc_t $crc_finalize_function($crc_t crc)\
:}\
"""
elif id == "c_template":
return """\
$source_header
$if ($include_file != Undefined) {:
#include $include_file
:}
#include "$header_filename" /* include the header file generated with pycrc */
#include <stdlib.h>
$if ($c_std != C89) {:
#include <stdint.h>
$if ($undefined_parameters == True or $crc_algorithm == "bit-by-bit" or $crc_algorithm == "bit-by-bit-fast") {:
#include <stdbool.h>
:}
:}
$if ($use_reflect_func == True and $static_reflect_func == True) {:
static $crc_reflect_function_def;
:}
$if ($crc_algorithm == "bit-by-bit") {:
$c_bit_by_bit
:} $elif ($crc_algorithm == "bit-by-bit-fast") {:
$c_bit_by_bit_fast
:} $elif ($crc_algorithm == "table-driven") {:
$c_table_driven
:}
"""
elif id == "c_table":
return """\
/**
* Static table used for the table_driven implementation.
$if ($undefined_parameters == True) {:
* Must be initialised with the $crc_init_function function.
:}
*****************************************************************************/
$if ($constant_crc_table != True) {:
static $crc_t crc_table[$crc_table_width];
:} $else {:
static const $crc_t crc_table[$crc_table_width] = {
$crc_table_init
};
:}
"""
elif id == "c_bit_by_bit":
return """\
$if ($use_reflect_func == True) {:
$crc_reflect_function_body
:}
$if ($constant_crc_init == False) {:
$crc_init_doc
$crc_init_function_def$nop
{
unsigned int i;
$c_bool bit;
$crc_t crc = $cfg_xor_in;
for (i = 0; i < $cfg_width; i++) {
bit = crc & 0x01;
if (bit) {
crc = ((crc ^ $cfg_poly) >> 1) | $cfg_msb_mask;
} else {
crc >>= 1;
}
}
return crc & $cfg_mask;
}
:}
$crc_update_doc
$crc_update_function_def$nop
{
unsigned int i;
$c_bool bit;
unsigned char c;
while (data_len--) {
$if ($crc_reflect_in == Undefined) {:
if ($cfg_reflect_in) {
c = $crc_reflect_function(*data++, 8);
} else {
c = *data++;
}
:} $elif ($crc_reflect_in == True) {:
c = $crc_reflect_function(*data++, 8);
:} $else {:
c = *data++;
:}
for (i = 0; i < 8; i++) {
bit = $if ($c_std == C89) {:!!(crc & $cfg_msb_mask):} $else {:crc & $cfg_msb_mask:};
crc = (crc << 1) | ((c >> (7 - i)) & 0x01);
if (bit) {
crc ^= $cfg_poly;
}
}
crc &= $cfg_mask;
}
return crc & $cfg_mask;
}
$crc_finalize_doc
$crc_finalize_function_def$nop
{
unsigned int i;
$c_bool bit;
for (i = 0; i < $cfg_width; i++) {
bit = $if ($c_std == C89) {:!!(crc & $cfg_msb_mask):} $else {:crc & $cfg_msb_mask:};
crc = (crc << 1) | 0x00;
if (bit) {
crc ^= $cfg_poly;
}
}
$if ($crc_reflect_out == Undefined) {:
if ($cfg_reflect_out) {
crc = $crc_reflect_function(crc, $cfg_width);
}
:} $elif ($crc_reflect_out == True) {:
crc = $crc_reflect_function(crc, $cfg_width);
:}
return (crc ^ $cfg_xor_out) & $cfg_mask;
}
"""
elif id == "c_bit_by_bit_fast":
return """\
$if ($use_reflect_func == True) {:
$crc_reflect_function_body
:}
$if ($constant_crc_init == False) {:
$crc_init_doc
$crc_init_function_def$nop
{
return $cfg_xor_in & $cfg_mask;
}
:}
$crc_update_doc
$crc_update_function_def$nop
{
unsigned int i;
$c_bool bit;
unsigned char c;
while (data_len--) {
$if ($crc_reflect_in == Undefined) {:
if ($cfg_reflect_in) {
c = $crc_reflect_function(*data++, 8);
} else {
c = *data++;
}
:} $else {:
c = *data++;
:}
$if ($crc_reflect_in == True) {:
for (i = 0x01; i & 0xff; i <<= 1) {
:} $else {:
for (i = 0x80; i > 0; i >>= 1) {
:}
bit = $if ($c_std == C89) {:!!(crc & $cfg_msb_mask):} $else {:crc & $cfg_msb_mask:};
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= $cfg_poly;
}
}
crc &= $cfg_mask;
}
return crc & $cfg_mask;
}
$if ($inline_crc_finalize != True) {:
$crc_finalize_doc
$crc_finalize_function_def$nop
{
$if ($crc_reflect_out == Undefined) {:
if (cfg->reflect_out) {
crc = $crc_reflect_function(crc, $cfg_width);
}
:} $elif ($crc_reflect_out == True) {:
crc = $crc_reflect_function(crc, $cfg_width);
:}
return (crc ^ $cfg_xor_out) & $cfg_mask;
}
:}
"""
elif id == "c_table_driven":
return """\
$c_table
$if ($use_reflect_func == True) {:
$crc_reflect_function_body
:}
$if ($constant_crc_init == False) {:
$crc_init_doc
$crc_init_function_def$nop
{
$if ($crc_reflect_in == Undefined) {:
if ($cfg_reflect_in) {
return $crc_reflect_function($cfg_xor_in & $cfg_mask, $cfg_width)$if ($crc_do_shift == True) {: << $cfg_shift:};
} else {
return $cfg_xor_in & $cfg_mask$if ($crc_do_shift == True) {: << $cfg_shift:};
}
:} $elif ($crc_reflect_in == True) {:
return $crc_reflect_function($cfg_xor_in & $cfg_mask, $cfg_width)$if ($crc_do_shift == True) {: << $cfg_shift:};
:} $else {:
return $cfg_xor_in & $cfg_mask$if ($crc_do_shift == True) {: << $cfg_shift:};
:}
}
:}
$if ($constant_crc_table != True) {:
$crc_table_gen_doc
$crc_table_gen_function_def
{
$crc_t crc;
unsigned int i, j;
for (i = 0; i < $cfg_table_width; i++) {
$if ($crc_reflect_in == Undefined) {:
if (cfg->reflect_in) {
crc = $crc_reflect_function(i, $cfg_table_idx_width);
} else {
crc = i;
}
:} $elif ($crc_reflect_in == True) {:
crc = $crc_reflect_function(i, $cfg_table_idx_width);
:} $else {:
crc = i;
:}
$if ($crc_do_shift == True) {:
crc <<= ($cfg_width - $cfg_table_idx_width + $cfg_shift);
:} $else {:
crc <<= ($cfg_width - $cfg_table_idx_width);
:}
for (j = 0; j < $cfg_table_idx_width; j++) {
if (crc & $cfg_msb_mask_shifted) {
crc = (crc << 1) ^ $cfg_poly_shifted;
} else {
crc = crc << 1;
}
}
$if ($crc_reflect_in == Undefined) {:
if (cfg->reflect_in) {
$if ($crc_do_shift == True) {:
crc = $crc_reflect_function(crc >> $cfg_shift, $cfg_width) << $cfg_shift;
:} $else {:
crc = $crc_reflect_function(crc, $cfg_width);
:}
}
:} $elif ($crc_reflect_in == True) {:
$if ($crc_do_shift == True) {:
crc = $crc_reflect_function(crc >> $cfg_shift, $cfg_width) << $cfg_shift;
:} $else {:
crc = $crc_reflect_function(crc, $cfg_width);
:}
:}
crc_table[i] = crc & $cfg_mask_shifted;
}
}
:}
$crc_update_doc
$crc_update_function_def$nop
{
unsigned int tbl_idx;
$if ($crc_reflect_in == Undefined) {:
if (cfg->reflect_in) {
while (data_len--) {
$crc_table_core_algorithm_ri
data++;
}
} else {
while (data_len--) {
$crc_table_core_algorithm_ni
data++;
}
}
:} $else {:
while (data_len--) {
$if ($crc_reflect_in == True) {:
$crc_table_core_algorithm_ri
:} $elif ($crc_reflect_in == False) {:
$crc_table_core_algorithm_ni
:}
data++;
}
:}
return crc & $cfg_mask_shifted;
}
$if ($inline_crc_finalize == False) {:
$crc_finalize_doc
$crc_finalize_function_def$nop
{
$if ($crc_do_shift == True) {:
crc >>= $cfg_shift;
:}
$if ($crc_reflect_in == Undefined or $crc_reflect_out == Undefined) {:
$if ($crc_reflect_in == Undefined and $crc_reflect_out == Undefined) {:
if (cfg->reflect_in == !cfg->reflect_out):}
$elif ($crc_reflect_out == Undefined) {:
if ($if ($crc_reflect_in == True) {:!:}cfg->reflect_out):}
$elif ($crc_reflect_in == Undefined) {:
if ($if ($crc_reflect_out == True) {:!:}cfg->reflect_in):} {
crc = $crc_reflect_function(crc, $cfg_width);
}
:} $elif ($crc_reflect_in != $crc_reflect_out) {:
crc = $crc_reflect_function(crc, $cfg_width);
:}
return (crc ^ $cfg_xor_out) & $cfg_mask;
}
:}
"""
elif id == "main_template":
return """\
$if ($include_file != Undefined) {:
#include $include_file
:}
#include <stdio.h>
#include <getopt.h>
$if ($undefined_parameters == True) {:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
:}
$if ($c_std != C89) {:
#include <stdbool.h>
:}
#include <string.h>
static char str[256] = "123456789";
static $c_bool verbose = $c_false;
void print_params($if ($undefined_parameters == True) {:const $cfg_t *cfg:} $else {:void:});
$getopt_template
void print_params($if ($undefined_parameters == True) {:const $cfg_t *cfg:} $else {:void:})
{
char format[20];
$if ($c_std == C89) {:
sprintf(format, "%%-16s = 0x%%0%dx\\n", (unsigned int)($cfg_width + 3) / 4);
:} $else {:
snprintf(format, sizeof(format), "%%-16s = 0x%%0%dx\\n", (unsigned int)($cfg_width + 3) / 4);
:}
printf("%-16s = %d\\n", "width", (unsigned int)$cfg_width);
printf(format, "poly", (unsigned int)$cfg_poly);
printf("%-16s = %s\\n", "reflect_in", $if ($crc_reflect_in == Undefined) {:$cfg_reflect_in ? "true": "false":} $else {:$if ($crc_reflect_in == True) {:"true":} $else {:"false":}:});
printf(format, "xor_in", $cfg_xor_in);
printf("%-16s = %s\\n", "reflect_out", $if ($crc_reflect_out == Undefined) {:$cfg_reflect_out ? "true": "false":} $else {:$if ($crc_reflect_out == True) {:"true":} $else {:"false":}:});
printf(format, "xor_out", (unsigned int)$cfg_xor_out);
printf(format, "crc_mask", (unsigned int)$cfg_mask);
printf(format, "msb_mask", (unsigned int)$cfg_msb_mask);
}
/**
* C main function.
*
* \\return 0 on success, != 0 on error.
*****************************************************************************/
int main(int argc, char *argv[])
{
$if ($undefined_parameters == True) {:
$cfg_t cfg = {
$if ($crc_width == Undefined) {:
0, /* width */
:}
$if ($crc_poly == Undefined) {:
0, /* poly */
:}
$if ($crc_xor_in == Undefined) {:
0, /* xor_in */
:}
$if ($crc_reflect_in == Undefined) {:
0, /* reflect_in */
:}
$if ($crc_xor_out == Undefined) {:
0, /* xor_out */
:}
$if ($crc_reflect_out == Undefined) {:
0, /* reflect_out */
:}
$if ($crc_width == Undefined) {:
0, /* crc_mask */
0, /* msb_mask */
0, /* crc_shift */
:}
};
:}
$crc_t crc;
$if ($undefined_parameters == True) {:
get_config(argc, argv, &cfg);
:} $else {:
get_config(argc, argv);
:}
$if ($crc_algorithm == "table-driven" and $constant_crc_table != True) {:
$crc_table_gen_function(&cfg);
:}
crc = $crc_init_function($if ($constant_crc_init != True) {:&cfg:});
crc = $crc_update_function($if ($simple_crc_update_def != True) {:&cfg, :}crc, (unsigned char *)str, strlen(str));
crc = $crc_finalize_function($if ($simple_crc_finalize_def != True) {:&cfg, :}crc);
if (verbose) {
print_params($if ($undefined_parameters == True) {:&cfg:});
}
printf("0x%lx\\n", (unsigned long)crc);
return 0;
}
"""
elif id == "getopt_template":
return """\
$if ($crc_reflect_in == Undefined or $crc_reflect_out == Undefined) {:
static $c_bool atob(const char *str);
:}
$if ($crc_poly == Undefined or $crc_xor_in == Undefined or $crc_xor_out == Undefined) {:
static crc_t xtoi(const char *str);
:}
static int get_config(int argc, char *argv[]$if ($undefined_parameters == True) {:, $cfg_t *cfg:});
$if ($crc_reflect_in == Undefined or $crc_reflect_out == Undefined) {:
$c_bool atob(const char *str)
{
if (!str) {
return 0;
}
if (isdigit(str[0])) {
return ($c_bool)atoi(str);
}
if (tolower(str[0]) == 't') {
return $c_true;
}
return $c_false;
}
:}
$if ($crc_poly == Undefined or $crc_xor_in == Undefined or $crc_xor_out == Undefined) {:
crc_t xtoi(const char *str)
{
crc_t ret = 0;
if (!str) {
return 0;
}
if (str[0] == '0' && tolower(str[1]) == 'x') {
str += 2;
while (*str) {
if (isdigit(*str))
ret = 16 * ret + *str - '0';
else if (isxdigit(*str))
ret = 16 * ret + tolower(*str) - 'a' + 10;
else
return ret;
str++;
}
} else if (isdigit(*str)) {
while (*str) {
if (isdigit(*str))
ret = 10 * ret + *str - '0';
else
return ret;
str++;
}
}
return ret;
}
:}
static int get_config(int argc, char *argv[]$if ($undefined_parameters == True) {:, $cfg_t *cfg:})
{
int c;
int option_index;
static struct option long_options[] = {
$if ($crc_width == Undefined) {: