barcode_validator.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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[2];
  232. snprintf(current_character, 2, "%c", barcode_char);
  233. //get the value of the character
  234. if(!flipper_format_read_string(ff, current_character, value)) {
  235. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  236. barcode_data->reason = InvalidCharacters;
  237. barcode_data->valid = false;
  238. break;
  239. }
  240. //using the value of the character, get the characters bits
  241. if(!flipper_format_read_string(ff, furi_string_get_cstr(value), char_bits)) {
  242. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  243. barcode_data->reason = EncodingTableError;
  244. barcode_data->valid = false;
  245. break;
  246. } else {
  247. //add the bits to the full barcode
  248. furi_string_cat(barcode_bits, char_bits);
  249. //calculate the checksum
  250. checksum_digits += 1;
  251. checksum_adder += (atoi(furi_string_get_cstr(value)) * checksum_digits);
  252. FURI_LOG_D(
  253. TAG,
  254. "\"%c\" string: %s : %s : %d : %d : %d",
  255. barcode_char,
  256. furi_string_get_cstr(char_bits),
  257. furi_string_get_cstr(value),
  258. checksum_digits,
  259. (atoi(furi_string_get_cstr(value)) * checksum_digits),
  260. checksum_adder);
  261. }
  262. //bring the file pointer back to the beginning
  263. flipper_format_rewind(ff);
  264. }
  265. //calculate the check digit and convert it into a c string for lookup in the encoding table
  266. final_check_digit = checksum_adder % 103;
  267. int length = snprintf(NULL, 0, "%d", final_check_digit);
  268. char* final_check_digit_string = malloc(length + 1);
  269. snprintf(final_check_digit_string, length + 1, "%d", final_check_digit);
  270. //after the checksum has been calculated, add the bits to the full barcode
  271. if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
  272. FURI_LOG_E(TAG, "Could not read \"%s\" string", final_check_digit_string);
  273. barcode_data->reason = EncodingTableError;
  274. barcode_data->valid = false;
  275. } else {
  276. //add the check digit bits to the full barcode
  277. furi_string_cat(barcode_bits, char_bits);
  278. FURI_LOG_D(
  279. TAG,
  280. "\"%s\" string: %s",
  281. final_check_digit_string,
  282. furi_string_get_cstr(char_bits));
  283. }
  284. free(final_check_digit_string);
  285. furi_string_free(value);
  286. furi_string_free(char_bits);
  287. }
  288. //add the stop code
  289. furi_string_cat(barcode_bits, stop_code_bits);
  290. //Close Storage
  291. flipper_format_free(ff);
  292. furi_record_close(RECORD_STORAGE);
  293. furi_string_cat(barcode_data->correct_data, barcode_bits);
  294. furi_string_free(barcode_bits);
  295. }
  296. /**
  297. * Loads a code 128 C barcode
  298. */
  299. void code_128c_loader(BarcodeData* barcode_data) {
  300. int barcode_length = furi_string_size(barcode_data->raw_data);
  301. //the start code for character set C
  302. int start_code_value = 105;
  303. //The bits for the start code
  304. const char* start_code_bits = "11010011100";
  305. //The bits for the stop code
  306. const char* stop_code_bits = "1100011101011";
  307. int min_digits = barcode_data->type_obj->min_digits;
  308. int checksum_adder = start_code_value;
  309. int checksum_digits = 0;
  310. //the calculated check digit
  311. int final_check_digit = 0;
  312. // check the length of the barcode, must contain atleast 2 character,
  313. // this can have as many characters as it wants, it might not fit on the screen
  314. // code 128 C: the length must be even
  315. if((barcode_length < min_digits) || (barcode_length & 1)) {
  316. barcode_data->reason = WrongNumberOfDigits;
  317. barcode_data->valid = false;
  318. return;
  319. }
  320. //Open Storage
  321. Storage* storage = furi_record_open(RECORD_STORAGE);
  322. FlipperFormat* ff = flipper_format_file_alloc(storage);
  323. FuriString* barcode_bits = furi_string_alloc();
  324. //add the start code
  325. furi_string_cat(barcode_bits, start_code_bits);
  326. if(!flipper_format_file_open_existing(ff, CODE128C_DICT_FILE_PATH)) {
  327. FURI_LOG_E(TAG, "c128c Could not open file %s", CODE128C_DICT_FILE_PATH);
  328. barcode_data->reason = MissingEncodingTable;
  329. barcode_data->valid = false;
  330. } else {
  331. FuriString* value = furi_string_alloc();
  332. FuriString* char_bits = furi_string_alloc();
  333. for(int i = 0; i < barcode_length; i += 2) {
  334. char barcode_char1 = furi_string_get_char(barcode_data->raw_data, i);
  335. char barcode_char2 = furi_string_get_char(barcode_data->raw_data, i + 1);
  336. FURI_LOG_I(TAG, "c128c bc1='%c' bc2='%c'", barcode_char1, barcode_char2);
  337. char current_chars[4];
  338. snprintf(current_chars, 3, "%c%c", barcode_char1, barcode_char2);
  339. FURI_LOG_I(TAG, "c128c current_chars='%s'", current_chars);
  340. //using the value of the characters, get the characters bits
  341. if(!flipper_format_read_string(ff, current_chars, char_bits)) {
  342. FURI_LOG_E(TAG, "c128c Could not read \"%s\" string", current_chars);
  343. barcode_data->reason = EncodingTableError;
  344. barcode_data->valid = false;
  345. break;
  346. } else {
  347. //add the bits to the full barcode
  348. furi_string_cat(barcode_bits, char_bits);
  349. // calculate the checksum
  350. checksum_digits += 1;
  351. checksum_adder += (atoi(current_chars) * checksum_digits);
  352. FURI_LOG_I(
  353. TAG,
  354. "c128c \"%s\" string: %s : %s : %d : %d : %d",
  355. current_chars,
  356. furi_string_get_cstr(char_bits),
  357. furi_string_get_cstr(value),
  358. checksum_digits,
  359. (atoi(furi_string_get_cstr(value)) * checksum_digits),
  360. checksum_adder);
  361. }
  362. //bring the file pointer back to the begining
  363. flipper_format_rewind(ff);
  364. }
  365. //calculate the check digit and convert it into a c string for lookup in the encoding table
  366. final_check_digit = checksum_adder % 103;
  367. FURI_LOG_I(TAG, "c128c finale_check_digit=%d", final_check_digit);
  368. int length = snprintf(NULL, 0, "%d", final_check_digit);
  369. if(final_check_digit < 100) length = 2;
  370. char* final_check_digit_string = malloc(length + 1);
  371. snprintf(final_check_digit_string, length + 1, "%02d", final_check_digit);
  372. //after the checksum has been calculated, add the bits to the full barcode
  373. if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
  374. FURI_LOG_E(TAG, "c128c cksum Could not read \"%s\" string", final_check_digit_string);
  375. barcode_data->reason = EncodingTableError;
  376. barcode_data->valid = false;
  377. } else {
  378. //add the check digit bits to the full barcode
  379. furi_string_cat(barcode_bits, char_bits);
  380. FURI_LOG_I(
  381. TAG,
  382. "check digit \"%s\" string: %s",
  383. final_check_digit_string,
  384. furi_string_get_cstr(char_bits));
  385. }
  386. free(final_check_digit_string);
  387. furi_string_free(value);
  388. furi_string_free(char_bits);
  389. }
  390. //add the stop code
  391. furi_string_cat(barcode_bits, stop_code_bits);
  392. //Close Storage
  393. flipper_format_free(ff);
  394. furi_record_close(RECORD_STORAGE);
  395. FURI_LOG_I(TAG, "c128c %s", furi_string_get_cstr(barcode_bits));
  396. furi_string_cat(barcode_data->correct_data, barcode_bits);
  397. furi_string_free(barcode_bits);
  398. }
  399. void codabar_loader(BarcodeData* barcode_data) {
  400. int barcode_length = furi_string_size(barcode_data->raw_data);
  401. int min_digits = barcode_data->type_obj->min_digits;
  402. //check the length of the barcode, must contain atleast a character,
  403. //this can have as many characters as it wants, it might not fit on the screen
  404. if(barcode_length < min_digits) {
  405. barcode_data->reason = WrongNumberOfDigits;
  406. barcode_data->valid = false;
  407. return;
  408. }
  409. FuriString* barcode_bits = furi_string_alloc();
  410. barcode_length = furi_string_size(barcode_data->raw_data);
  411. //Open Storage
  412. Storage* storage = furi_record_open(RECORD_STORAGE);
  413. FlipperFormat* ff = flipper_format_file_alloc(storage);
  414. if(!flipper_format_file_open_existing(ff, CODABAR_DICT_FILE_PATH)) {
  415. FURI_LOG_E(TAG, "Could not open file %s", CODABAR_DICT_FILE_PATH);
  416. barcode_data->reason = MissingEncodingTable;
  417. barcode_data->valid = false;
  418. } else {
  419. FuriString* char_bits = furi_string_alloc();
  420. for(int i = 0; i < barcode_length; i++) {
  421. char barcode_char = toupper(furi_string_get_char(barcode_data->raw_data, i));
  422. //convert a char into a string so it used in flipper_format_read_string
  423. char current_character[2];
  424. snprintf(current_character, 2, "%c", barcode_char);
  425. if(!flipper_format_read_string(ff, current_character, char_bits)) {
  426. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  427. barcode_data->reason = InvalidCharacters;
  428. barcode_data->valid = false;
  429. break;
  430. } else {
  431. FURI_LOG_I(
  432. TAG, "\"%c\" string: %s", barcode_char, furi_string_get_cstr(char_bits));
  433. furi_string_cat(barcode_bits, char_bits);
  434. }
  435. flipper_format_rewind(ff);
  436. }
  437. furi_string_free(char_bits);
  438. }
  439. //Close Storage
  440. flipper_format_free(ff);
  441. furi_record_close(RECORD_STORAGE);
  442. furi_string_cat(barcode_data->correct_data, barcode_bits);
  443. furi_string_free(barcode_bits);
  444. }