barcode_validator.c 14 KB

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