barcode_validator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. int max_digit = barcode_data->type_obj->max_digits;
  113. //check the length of the barcode, must contain atleast a character,
  114. //this can have as many characters as it wants, it might not fit on the screen
  115. if(barcode_length < min_digits) {
  116. barcode_data->reason = WrongNumberOfDigits;
  117. barcode_data->valid = false;
  118. return;
  119. }
  120. FuriString* barcode_bits = furi_string_alloc();
  121. FuriString* temp_string = furi_string_alloc();
  122. //add starting and ending *
  123. if(!furi_string_start_with(barcode_data->raw_data, "*")) {
  124. furi_string_push_back(temp_string, '*');
  125. furi_string_cat(temp_string, barcode_data->raw_data);
  126. furi_string_set(barcode_data->raw_data, temp_string);
  127. }
  128. if(!furi_string_end_with(barcode_data->raw_data, "*")) {
  129. furi_string_push_back(barcode_data->raw_data, '*');
  130. }
  131. furi_string_free(temp_string);
  132. barcode_length = furi_string_size(barcode_data->raw_data);
  133. //Open Storage
  134. Storage* storage = furi_record_open(RECORD_STORAGE);
  135. FlipperFormat* ff = flipper_format_file_alloc(storage);
  136. if(!flipper_format_file_open_existing(ff, CODE39_DICT_FILE_PATH)) {
  137. FURI_LOG_E(TAG, "Could not open file %s", CODE39_DICT_FILE_PATH);
  138. barcode_data->reason = MissingEncodingTable;
  139. barcode_data->valid = false;
  140. } else {
  141. FuriString* char_bits = furi_string_alloc();
  142. for(int i = 0; i < barcode_length; i++) {
  143. char barcode_char = toupper(furi_string_get_char(barcode_data->raw_data, i));
  144. //convert a char into a string so it used in flipper_format_read_string
  145. char current_character[2];
  146. snprintf(current_character, 2, "%c", barcode_char);
  147. if(!flipper_format_read_string(ff, current_character, char_bits)) {
  148. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  149. barcode_data->reason = InvalidCharacters;
  150. barcode_data->valid = false;
  151. break;
  152. } else {
  153. FURI_LOG_I(
  154. TAG, "\"%c\" string: %s", barcode_char, furi_string_get_cstr(char_bits));
  155. furi_string_cat(barcode_bits, char_bits);
  156. }
  157. flipper_format_rewind(ff);
  158. }
  159. furi_string_free(char_bits);
  160. }
  161. //Close Storage
  162. flipper_format_free(ff);
  163. furi_record_close(RECORD_STORAGE);
  164. furi_string_cat(barcode_data->correct_data, barcode_bits);
  165. furi_string_free(barcode_bits);
  166. }
  167. /**
  168. * Loads a code 128 barcode
  169. *
  170. * Only supports character set B
  171. */
  172. void code_128_loader(BarcodeData* barcode_data) {
  173. int barcode_length = furi_string_size(barcode_data->raw_data);
  174. //the start code for character set B
  175. int start_code_value = 104;
  176. //The bits for the start code
  177. const char* start_code_bits = "11010010000";
  178. //The bits for the stop code
  179. const char* stop_code_bits = "1100011101011";
  180. int min_digits = barcode_data->type_obj->min_digits;
  181. int max_digit = barcode_data->type_obj->max_digits;
  182. /**
  183. * A sum of all of the characters values
  184. * Ex:
  185. * Barcode Data : ABC
  186. * A has a value of 33
  187. * B has a value of 34
  188. * C has a value of 35
  189. *
  190. * the checksum_adder would be (33 * 1) + (34 * 2) + (35 * 3) + 104 = 310
  191. *
  192. * Add 104 since we are using set B
  193. */
  194. int checksum_adder = start_code_value;
  195. /**
  196. * Checksum digits is the number of characters it has read so far
  197. * In the above example the checksum_digits would be 3
  198. */
  199. int checksum_digits = 0;
  200. //the calculated check digit
  201. int final_check_digit = 0;
  202. //check the length of the barcode, must contain atleast a character,
  203. //this can have as many characters as it wants, it might not fit on the screen
  204. if(barcode_length < min_digits) {
  205. barcode_data->reason = WrongNumberOfDigits;
  206. barcode_data->valid = false;
  207. return;
  208. }
  209. //Open Storage
  210. Storage* storage = furi_record_open(RECORD_STORAGE);
  211. FlipperFormat* ff = flipper_format_file_alloc(storage);
  212. FuriString* barcode_bits = furi_string_alloc();
  213. //add the start code
  214. furi_string_cat(barcode_bits, start_code_bits);
  215. if(!flipper_format_file_open_existing(ff, CODE128_DICT_FILE_PATH)) {
  216. FURI_LOG_E(TAG, "Could not open file %s", CODE128_DICT_FILE_PATH);
  217. barcode_data->reason = MissingEncodingTable;
  218. barcode_data->valid = false;
  219. } else {
  220. FuriString* value = furi_string_alloc();
  221. FuriString* char_bits = furi_string_alloc();
  222. for(int i = 0; i < barcode_length; i++) {
  223. char barcode_char = furi_string_get_char(barcode_data->raw_data, i);
  224. //convert a char into a string so it used in flipper_format_read_string
  225. char current_character[2];
  226. snprintf(current_character, 2, "%c", barcode_char);
  227. //get the value of the character
  228. if(!flipper_format_read_string(ff, current_character, value)) {
  229. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  230. barcode_data->reason = InvalidCharacters;
  231. barcode_data->valid = false;
  232. break;
  233. }
  234. //using the value of the character, get the characters bits
  235. if(!flipper_format_read_string(ff, furi_string_get_cstr(value), char_bits)) {
  236. FURI_LOG_E(TAG, "Could not read \"%c\" string", barcode_char);
  237. barcode_data->reason = EncodingTableError;
  238. barcode_data->valid = false;
  239. break;
  240. } else {
  241. //add the bits to the full barcode
  242. furi_string_cat(barcode_bits, char_bits);
  243. //calculate the checksum
  244. checksum_digits += 1;
  245. checksum_adder += (atoi(furi_string_get_cstr(value)) * checksum_digits);
  246. FURI_LOG_D(
  247. TAG,
  248. "\"%c\" string: %s : %s : %d : %d : %d",
  249. barcode_char,
  250. furi_string_get_cstr(char_bits),
  251. furi_string_get_cstr(value),
  252. checksum_digits,
  253. (atoi(furi_string_get_cstr(value)) * checksum_digits),
  254. checksum_adder);
  255. }
  256. //bring the file pointer back to the beginning
  257. flipper_format_rewind(ff);
  258. }
  259. //calculate the check digit and convert it into a c string for lookup in the encoding table
  260. final_check_digit = checksum_adder % 103;
  261. int length = snprintf(NULL, 0, "%d", final_check_digit);
  262. char* final_check_digit_string = malloc(length + 1);
  263. snprintf(final_check_digit_string, length + 1, "%d", final_check_digit);
  264. //after the checksum has been calculated, add the bits to the full barcode
  265. if(!flipper_format_read_string(ff, final_check_digit_string, char_bits)) {
  266. FURI_LOG_E(TAG, "Could not read \"%s\" string", final_check_digit_string);
  267. barcode_data->reason = EncodingTableError;
  268. barcode_data->valid = false;
  269. } else {
  270. //add the check digit bits to the full barcode
  271. furi_string_cat(barcode_bits, char_bits);
  272. FURI_LOG_D(
  273. TAG,
  274. "\"%s\" string: %s",
  275. final_check_digit_string,
  276. furi_string_get_cstr(char_bits));
  277. }
  278. free(final_check_digit_string);
  279. furi_string_free(value);
  280. furi_string_free(char_bits);
  281. }
  282. //add the stop code
  283. furi_string_cat(barcode_bits, stop_code_bits);
  284. //Close Storage
  285. flipper_format_free(ff);
  286. furi_record_close(RECORD_STORAGE);
  287. furi_string_cat(barcode_data->correct_data, barcode_bits);
  288. furi_string_free(barcode_bits);
  289. }