barcode_validator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "barcode_validator.h"
  2. void barcode_loader(BarcodeData* barcode_data) {
  3. switch(barcode_data->type_obj->type) {
  4. case UPCA:
  5. case EAN8:
  6. case EAN13:
  7. ean_upc_loader(barcode_data);
  8. break;
  9. case CODE39:
  10. code_39_loader(barcode_data);
  11. break;
  12. case CODE128:
  13. code_128_loader(barcode_data);
  14. break;
  15. case UNKNOWN:
  16. barcode_data->reason = UnsupportedType;
  17. barcode_data->valid = false;
  18. default:
  19. break;
  20. }
  21. }
  22. /**
  23. * Calculates the check digit of a barcode if they have one
  24. * @param barcode_data the barcode data
  25. * @returns a check digit or -1 for either an invalid
  26. */
  27. int calculate_check_digit(BarcodeData* barcode_data) {
  28. int check_digit = -1;
  29. switch(barcode_data->type_obj->type) {
  30. case UPCA:
  31. case EAN8:
  32. case EAN13:
  33. check_digit = calculate_ean_upc_check_digit(barcode_data);
  34. break;
  35. case CODE39:
  36. case CODE128:
  37. case UNKNOWN:
  38. default:
  39. break;
  40. }
  41. return check_digit;
  42. }
  43. /**
  44. * Calculates the check digit of barcode types UPC-A, EAN-8, & EAN-13
  45. */
  46. int calculate_ean_upc_check_digit(BarcodeData* barcode_data) {
  47. int check_digit = 0;
  48. int odd = 0;
  49. int even = 0;
  50. int length = barcode_data->type_obj->min_digits;
  51. //Get sum of odd digits
  52. for(int i = 0; i < length; i += 2) {
  53. odd += furi_string_get_char(barcode_data->raw_data, i) - '0';
  54. }
  55. //Get sum of even digits
  56. for(int i = 1; i < length; i += 2) {
  57. even += furi_string_get_char(barcode_data->raw_data, i) - '0';
  58. }
  59. if(barcode_data->type_obj->type == EAN13) {
  60. check_digit = even * 3 + odd;
  61. } else {
  62. check_digit = odd * 3 + even;
  63. }
  64. check_digit = check_digit % 10;
  65. return (10 - check_digit) % 10;
  66. }
  67. /**
  68. * Loads and validates Barcode Types EAN-8, EAN-13, and UPC-A
  69. * barcode_data and its strings should already be allocated;
  70. */
  71. void ean_upc_loader(BarcodeData* barcode_data) {
  72. int barcode_length = furi_string_size(barcode_data->raw_data);
  73. int min_digits = barcode_data->type_obj->min_digits;
  74. int max_digit = barcode_data->type_obj->max_digits;
  75. //check the length of the barcode
  76. if(barcode_length < min_digits || barcode_length > max_digit) {
  77. barcode_data->reason = WrongNumberOfDigits;
  78. barcode_data->valid = false;
  79. return;
  80. }
  81. //checks if the barcode contains any characters that aren't a number
  82. for(int i = 0; i < barcode_length; i++) {
  83. char character = furi_string_get_char(barcode_data->raw_data, i);
  84. int digit = character - '0'; //convert the number into an int (also the index)
  85. if(digit < 0 || digit > 9) {
  86. barcode_data->reason = InvalidCharacters;
  87. barcode_data->valid = false;
  88. return;
  89. }
  90. }
  91. int check_digit = calculate_check_digit(barcode_data);
  92. char check_digit_char = check_digit + '0';
  93. barcode_data->check_digit = check_digit;
  94. //if the barcode length is at max length then we will verify if the check digit is correct
  95. if(barcode_length == max_digit) {
  96. //append the raw_data to the correct data string
  97. furi_string_cat(barcode_data->correct_data, barcode_data->raw_data);
  98. //append the check digit to the correct data string
  99. furi_string_set_char(barcode_data->correct_data, min_digits, check_digit_char);
  100. }
  101. //if the barcode length is at min length, we will calculate the check digit
  102. if(barcode_length == min_digits) {
  103. //append the raw_data to the correct data string
  104. furi_string_cat(barcode_data->correct_data, barcode_data->raw_data);
  105. //append the check digit to the correct data string
  106. furi_string_push_back(barcode_data->correct_data, check_digit_char);
  107. }
  108. }
  109. void code_39_loader(BarcodeData* barcode_data) {
  110. int barcode_length = furi_string_size(barcode_data->raw_data);
  111. int min_digits = barcode_data->type_obj->min_digits;
  112. //check the length of the barcode, must contain atleast a character,
  113. //this can have as many characters as it wants, it might not fit on the screen
  114. if(barcode_length < min_digits) {
  115. barcode_data->reason = WrongNumberOfDigits;
  116. barcode_data->valid = false;
  117. return;
  118. }
  119. FuriString* barcode_bits = furi_string_alloc();
  120. FuriString* temp_string = furi_string_alloc();
  121. //add starting and ending *
  122. if(!furi_string_start_with(barcode_data->raw_data, "*")) {
  123. furi_string_push_back(temp_string, '*');
  124. furi_string_cat(temp_string, barcode_data->raw_data);
  125. furi_string_set(barcode_data->raw_data, temp_string);
  126. }
  127. if(!furi_string_end_with(barcode_data->raw_data, "*")) {
  128. furi_string_push_back(barcode_data->raw_data, '*');
  129. }
  130. furi_string_free(temp_string);
  131. barcode_length = furi_string_size(barcode_data->raw_data);
  132. //Open Storage
  133. Storage* storage = furi_record_open(RECORD_STORAGE);
  134. FlipperFormat* ff = flipper_format_file_alloc(storage);
  135. if(!flipper_format_file_open_existing(ff, CODE39_DICT_FILE_PATH)) {
  136. FURI_LOG_E(TAG, "Could not open file %s", CODE39_DICT_FILE_PATH);
  137. barcode_data->reason = MissingEncodingTable;
  138. barcode_data->valid = false;
  139. } else {
  140. FuriString* char_bits = furi_string_alloc();
  141. for(int i = 0; i < barcode_length; i++) {
  142. char barcode_char = toupper(furi_string_get_char(barcode_data->raw_data, i));
  143. //convert a char into a string so it used in flipper_format_read_string
  144. char current_character[2];
  145. snprintf(current_character, 2, "%c", barcode_char);
  146. if(!flipper_format_read_string(ff, current_character, char_bits)) {
  147. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  148. barcode_data->reason = InvalidCharacters;
  149. barcode_data->valid = false;
  150. break;
  151. } else {
  152. FURI_LOG_I(
  153. TAG, "\"%c\" string: %s", barcode_char, furi_string_get_cstr(char_bits));
  154. furi_string_cat(barcode_bits, char_bits);
  155. }
  156. flipper_format_rewind(ff);
  157. }
  158. furi_string_free(char_bits);
  159. }
  160. //Close Storage
  161. flipper_format_free(ff);
  162. furi_record_close(RECORD_STORAGE);
  163. furi_string_cat(barcode_data->correct_data, barcode_bits);
  164. furi_string_free(barcode_bits);
  165. }
  166. /**
  167. * Loads a code 128 barcode
  168. *
  169. * Only supports character set B
  170. */
  171. void code_128_loader(BarcodeData* barcode_data) {
  172. int barcode_length = furi_string_size(barcode_data->raw_data);
  173. //the start code for character set B
  174. int start_code_value = 104;
  175. //The bits for the start code
  176. const char* start_code_bits = "11010010000";
  177. //The bits for the stop code
  178. const char* stop_code_bits = "1100011101011";
  179. int min_digits = barcode_data->type_obj->min_digits;
  180. /**
  181. * A sum of all of the characters values
  182. * Ex:
  183. * Barcode Data : ABC
  184. * A has a value of 33
  185. * B has a value of 34
  186. * C has a value of 35
  187. *
  188. * the checksum_adder would be (33 * 1) + (34 * 2) + (35 * 3) + 104 = 310
  189. *
  190. * Add 104 since we are using set B
  191. */
  192. int checksum_adder = start_code_value;
  193. /**
  194. * Checksum digits is the number of characters it has read so far
  195. * In the above example the checksum_digits would be 3
  196. */
  197. int checksum_digits = 0;
  198. //the calculated check digit
  199. int final_check_digit = 0;
  200. //check the length of the barcode, must contain atleast a character,
  201. //this can have as many characters as it wants, it might not fit on the screen
  202. if(barcode_length < min_digits) {
  203. barcode_data->reason = WrongNumberOfDigits;
  204. barcode_data->valid = false;
  205. return;
  206. }
  207. //Open Storage
  208. Storage* storage = furi_record_open(RECORD_STORAGE);
  209. FlipperFormat* ff = flipper_format_file_alloc(storage);
  210. FuriString* barcode_bits = furi_string_alloc();
  211. //add the start code
  212. furi_string_cat(barcode_bits, start_code_bits);
  213. if(!flipper_format_file_open_existing(ff, CODE128_DICT_FILE_PATH)) {
  214. FURI_LOG_E(TAG, "Could not open file %s", CODE128_DICT_FILE_PATH);
  215. barcode_data->reason = MissingEncodingTable;
  216. barcode_data->valid = false;
  217. } else {
  218. FuriString* value = furi_string_alloc();
  219. FuriString* char_bits = furi_string_alloc();
  220. for(int i = 0; i < barcode_length; i++) {
  221. char barcode_char = furi_string_get_char(barcode_data->raw_data, i);
  222. //convert a char into a string so it used in flipper_format_read_string
  223. char current_character[2];
  224. snprintf(current_character, 2, "%c", barcode_char);
  225. //get the value of the character
  226. if(!flipper_format_read_string(ff, current_character, value)) {
  227. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  228. barcode_data->reason = InvalidCharacters;
  229. barcode_data->valid = false;
  230. break;
  231. }
  232. //using the value of the character, get the characters bits
  233. if(!flipper_format_read_string(ff, furi_string_get_cstr(value), char_bits)) {
  234. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  235. barcode_data->reason = EncodingTableError;
  236. barcode_data->valid = false;
  237. break;
  238. } else {
  239. //add the bits to the full barcode
  240. furi_string_cat(barcode_bits, char_bits);
  241. //calculate the checksum
  242. checksum_digits += 1;
  243. checksum_adder += (atoi(furi_string_get_cstr(value)) * checksum_digits);
  244. FURI_LOG_D(
  245. TAG,
  246. "\"%c\" string: %s : %s : %d : %d : %d",
  247. barcode_char,
  248. furi_string_get_cstr(char_bits),
  249. furi_string_get_cstr(value),
  250. checksum_digits,
  251. (atoi(furi_string_get_cstr(value)) * checksum_digits),
  252. checksum_adder);
  253. }
  254. //bring the file pointer back to the beginning
  255. flipper_format_rewind(ff);
  256. }
  257. //calculate the check digit and convert it into a c string for lookup in the encoding table
  258. final_check_digit = checksum_adder % 103;
  259. int length = snprintf(NULL, 0, "%d", final_check_digit);
  260. char* final_check_digit_string = malloc(length + 1);
  261. snprintf(final_check_digit_string, length + 1, "%d", final_check_digit);
  262. //after the checksum has been calculated, add the bits to the full barcode
  263. if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
  264. FURI_LOG_E(TAG, "Could not read \"%s\" string", final_check_digit_string);
  265. barcode_data->reason = EncodingTableError;
  266. barcode_data->valid = false;
  267. } else {
  268. //add the check digit bits to the full barcode
  269. furi_string_cat(barcode_bits, char_bits);
  270. FURI_LOG_D(
  271. TAG,
  272. "\"%s\" string: %s",
  273. final_check_digit_string,
  274. furi_string_get_cstr(char_bits));
  275. }
  276. free(final_check_digit_string);
  277. furi_string_free(value);
  278. furi_string_free(char_bits);
  279. }
  280. //add the stop code
  281. furi_string_cat(barcode_bits, stop_code_bits);
  282. //Close Storage
  283. flipper_format_free(ff);
  284. furi_record_close(RECORD_STORAGE);
  285. furi_string_cat(barcode_data->correct_data, barcode_bits);
  286. furi_string_free(barcode_bits);
  287. }