Przeglądaj źródła

fix: issue where check digit gets wrong encoding for code128

Alan Tsui 1 rok temu
rodzic
commit
974b033eca
2 zmienionych plików z 212 dodań i 210 usunięć
  1. 196 195
      barcode_encoding_files/code128_encodings.txt
  2. 16 15
      barcode_validator.c

+ 196 - 195
barcode_encoding_files/code128_encodings.txt

@@ -1,199 +1,200 @@
- : 00
-!: 01
-": 02
-H#: 03
-$: 04
-%: 05
-&: 06
-': 07
-(: 08
-): 09
-*: 10
-+: 11
-,: 12
--: 13
-.: 14
-/: 15
-0: 16
-1: 17
-2: 18
-3: 19
-4: 20
-5: 21
-6: 22
-7: 23
-8: 24
-9: 25
-:: 26
-;: 27
-<: 28
-=: 29
->: 30
-?: 31
-@: 32
-A: 33
-B: 34
-C: 35
-D: 36
-E: 37
-F: 38
-G: 39
-H: 40
-I: 41
-J: 42
-K: 43
-L: 44
-M: 45
-N: 46
-O: 47
-P: 48
-Q: 49
-R: 50
-S: 51
-T: 52
-U: 53
-V: 54
-W: 55
-X: 56
-Y: 57
-Z: 58
-[: 59
-\: 60
-]: 61
-^: 62
-_: 63
-`: 64
-a: 65
-b: 66
-c: 67
-d: 68
-e: 69
-f: 70
-g: 71
-h: 72
-i: 73
-j: 74
-k: 75
-l: 76
-m: 77
-n: 78
-o: 79
-p: 80
-q: 81
-r: 82
-s: 83
-t: 84
-u: 85
-v: 86
-w: 87
-x: 88
-y: 89
-z: 90
-{: 91
-|: 92
-}: 93
-~: 94
+ : 000
+!: 001
+": 002
+H#: 003
+$: 004
+%: 005
+&: 006
+': 007
+(: 008
+): 009
+*: 010
++: 011
+,: 012
+-: 013
+.: 014
+/: 015
+0: 016
+1: 017
+2: 018
+3: 019
+4: 020
+5: 021
+6: 022
+7: 023
+8: 024
+9: 025
+:: 026
+;: 027
+<: 028
+=: 029
+>: 030
+?: 031
+@: 032
+A: 033
+B: 034
+C: 035
+D: 036
+E: 037
+F: 038
+G: 039
+H: 040
+I: 041
+J: 042
+K: 043
+L: 044
+M: 045
+N: 046
+O: 047
+P: 048
+Q: 049
+R: 050
+S: 051
+T: 052
+U: 053
+V: 054
+W: 055
+X: 056
+Y: 057
+Z: 058
+[: 059
+\: 060
+]: 061
+^: 062
+_: 063
+`: 064
+a: 065
+b: 066
+c: 067
+d: 068
+e: 069
+f: 070
+g: 071
+h: 072
+i: 073
+j: 074
+k: 075
+l: 076
+m: 077
+n: 078
+o: 079
+p: 080
+q: 081
+r: 082
+s: 083
+t: 084
+u: 085
+v: 086
+w: 087
+x: 088
+y: 089
+z: 090
+{: 091
+|: 092
+}: 093
+~: 094
 
-00: 11011001100
-01: 11001101100
-02: 11001100110
-03: 10010011000
-04: 10010001100
-05: 10001001100
-06: 10011001000
-07: 10011000100
-08: 10001100100
-09: 11001001000
-10: 11001000100
-11: 11000100100
-12: 10110011100
-13: 10011011100
-14: 10011001110
-15: 10111001100
-16: 10011101100
-17: 10011100110
-18: 11001110010
-19: 11001011100
-20: 11001001110
-21: 11011100100
-22: 11001110100
-23: 11101101110
-24: 11101001100
-25: 11100101100
-26: 11100100110
-27: 11101100100
-28: 11100110100
-29: 11100110010
-30: 11011011000
-31: 11011000110
-32: 11000110110
-33: 10100011000
-34: 10001011000
-35: 10001000110
-36: 10110001000
-37: 10001101000
-38: 10001100010
-39: 11010001000
-40: 11000101000
-41: 11000100010
-42: 10110111000
-43: 10110001110
-44: 10001101110
-45: 10111011000
-46: 10111000110
-47: 10001110110
-48: 11101110110
-49: 11010001110
-50: 11000101110
-51: 11011101000
-52: 11011100010
-53: 11011101110
-54: 11101011000
-55: 11101000110
-56: 11100010110
-57: 11101101000
-58: 11101100010
-59: 11100011010
-60: 11101111010
-61: 11001000010
-62: 11110001010
-63: 10100110000
-64: 10100001100
-65: 10010110000
-66: 10010000110
-67: 10000101100
-68: 10000100110
-69: 10110010000
-70: 10110000100
-71: 10011010000
-72: 10011000010
-73: 10000110100
-74: 10000110010
-75: 11000010010
-76: 11001010000
-77: 11110111010
-78: 11000010100
-79: 10001111010
-80: 10100111100
-81: 10010111100
-82: 10010011110
-83: 10111100100
-84: 10011110100
-85: 10011110010
-86: 11110100100
-87: 11110010100
-88: 11110010010
-89: 11011011110
-90: 11011110110
-91: 11110110110
-92: 10101111000
-93: 10100011110
-94: 10001011110
-95: 10111101000
-96: 10111100010
-97: 11110101000
-98: 11110100010
-99: 10111011110
+ENCODINGS: 000
+000: 11011001100
+001: 11001101100
+002: 11001100110
+003: 10010011000
+004: 10010001100
+005: 10001001100
+006: 10011001000
+007: 10011000100
+008: 10001100100
+009: 11001001000
+010: 11001000100
+011: 11000100100
+012: 10110011100
+013: 10011011100
+014: 10011001110
+015: 10111001100
+016: 10011101100
+017: 10011100110
+018: 11001110010
+019: 11001011100
+020: 11001001110
+021: 11011100100
+022: 11001110100
+023: 11101101110
+024: 11101001100
+025: 11100101100
+026: 11100100110
+027: 11101100100
+028: 11100110100
+029: 11100110010
+030: 11011011000
+031: 11011000110
+032: 11000110110
+033: 10100011000
+034: 10001011000
+035: 10001000110
+036: 10110001000
+037: 10001101000
+038: 10001100010
+039: 11010001000
+040: 11000101000
+041: 11000100010
+042: 10110111000
+043: 10110001110
+044: 10001101110
+045: 10111011000
+046: 10111000110
+047: 10001110110
+048: 11101110110
+049: 11010001110
+050: 11000101110
+051: 11011101000
+052: 11011100010
+053: 11011101110
+054: 11101011000
+055: 11101000110
+056: 11100010110
+057: 11101101000
+058: 11101100010
+059: 11100011010
+060: 11101111010
+061: 11001000010
+062: 11110001010
+063: 10100110000
+064: 10100001100
+065: 10010110000
+066: 10010000110
+067: 10000101100
+068: 10000100110
+069: 10110010000
+070: 10110000100
+071: 10011010000
+072: 10011000010
+073: 10000110100
+074: 10000110010
+075: 11000010010
+076: 11001010000
+077: 11110111010
+078: 11000010100
+079: 10001111010
+080: 10100111100
+081: 10010111100
+082: 10010011110
+083: 10111100100
+084: 10011110100
+085: 10011110010
+086: 11110100100
+087: 11110010100
+088: 11110010010
+089: 11011011110
+090: 11011110110
+091: 11110110110
+092: 10101111000
+093: 10100011110
+094: 10001011110
+095: 10111101000
+096: 10111100010
+097: 11110101000
+098: 11110100010
+099: 10111011110
 100: 10111101110
 101: 11101011110
 102: 11110101110

+ 16 - 15
barcode_validator.c

@@ -291,6 +291,7 @@ void code_128_loader(BarcodeData* barcode_data) {
                 barcode_data->valid = false;
                 break;
             }
+
             //using the value of the character, get the characters bits
             if(!flipper_format_read_string(ff, furi_string_get_cstr(value), char_bits)) {
                 FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
@@ -321,12 +322,13 @@ void code_128_loader(BarcodeData* barcode_data) {
 
         //calculate the check digit and convert it into a c string for lookup in the encoding table
         final_check_digit = checksum_adder % 103;
-        int length = snprintf(NULL, 0, "%d", final_check_digit);
-        char* final_check_digit_string = malloc(length + 1);
-        snprintf(final_check_digit_string, length + 1, "%d", final_check_digit);
+        // int length = snprintf(NULL, 0, "%d", final_check_digit);
+        char* final_check_digit_string = malloc(5);
+        snprintf(final_check_digit_string, 5, "%03d", final_check_digit);
 
         //after the checksum has been calculated, add the bits to the full barcode
-        if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
+        if(!flipper_format_read_string(ff, "ENCODINGS", char_bits) ||
+           !flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
             FURI_LOG_E(TAG, "Could not read \"%s\" string", final_check_digit_string);
             barcode_data->reason = EncodingTableError;
             barcode_data->valid = false;
@@ -363,7 +365,7 @@ void code_128_loader(BarcodeData* barcode_data) {
 void code_128c_loader(BarcodeData* barcode_data) {
     int barcode_length = furi_string_size(barcode_data->raw_data);
 
-    //the start code for character set C 
+    //the start code for character set C
     int start_code_value = 105;
 
     //The bits for the start code
@@ -404,14 +406,14 @@ void code_128c_loader(BarcodeData* barcode_data) {
     } else {
         FuriString* value = furi_string_alloc();
         FuriString* char_bits = furi_string_alloc();
-        for(int i = 0; i < barcode_length; i+=2) {
+        for(int i = 0; i < barcode_length; i += 2) {
             char barcode_char1 = furi_string_get_char(barcode_data->raw_data, i);
-            char barcode_char2 = furi_string_get_char(barcode_data->raw_data, i+1);
+            char barcode_char2 = furi_string_get_char(barcode_data->raw_data, i + 1);
             FURI_LOG_I(TAG, "c128c bc1='%c' bc2='%c'", barcode_char1, barcode_char2);
 
             char current_chars[4];
             snprintf(current_chars, 3, "%c%c", barcode_char1, barcode_char2);
-	    FURI_LOG_I(TAG, "c128c current_chars='%s'", current_chars);
+            FURI_LOG_I(TAG, "c128c current_chars='%s'", current_chars);
 
             //using the value of the characters, get the characters bits
             if(!flipper_format_read_string(ff, current_chars, char_bits)) {
@@ -442,13 +444,12 @@ void code_128c_loader(BarcodeData* barcode_data) {
         }
         //calculate the check digit and convert it into a c string for lookup in the encoding table
         final_check_digit = checksum_adder % 103;
-	FURI_LOG_I(TAG, "c128c finale_check_digit=%d", final_check_digit);
+        FURI_LOG_I(TAG, "c128c finale_check_digit=%d", final_check_digit);
 
-	int length = snprintf(NULL, 0, "%d", final_check_digit);
-	if (final_check_digit < 100)
-            length=2;
-        char* final_check_digit_string = malloc(length+1);
-	snprintf(final_check_digit_string, length + 1, "%02d", final_check_digit);
+        int length = snprintf(NULL, 0, "%d", final_check_digit);
+        if(final_check_digit < 100) length = 2;
+        char* final_check_digit_string = malloc(length + 1);
+        snprintf(final_check_digit_string, length + 1, "%02d", final_check_digit);
 
         //after the checksum has been calculated, add the bits to the full barcode
         if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
@@ -483,7 +484,7 @@ void code_128c_loader(BarcodeData* barcode_data) {
     furi_string_free(barcode_bits);
 }
 
-void codabar_loader(BarcodeData* barcode_data){
+void codabar_loader(BarcodeData* barcode_data) {
     int barcode_length = furi_string_size(barcode_data->raw_data);
 
     int min_digits = barcode_data->type_obj->min_digits;