barcode_validator.c 19 KB

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