You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2014 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXVINParsedResult.h"
  17. #import "ZXVINResultParser.h"
  18. static NSRegularExpression *ZX_IOQ = nil;
  19. static NSRegularExpression *ZX_AZ09 = nil;
  20. @implementation ZXVINResultParser
  21. + (void)initialize {
  22. if ([self class] != [ZXVINResultParser class]) return;
  23. ZX_IOQ = [[NSRegularExpression alloc] initWithPattern:@"[IOQ]" options:0 error:nil];
  24. ZX_AZ09 = [[NSRegularExpression alloc] initWithPattern:@"[A-Z0-9]{17}" options:0 error:nil];
  25. }
  26. - (ZXVINParsedResult *)parse:(ZXResult *)result {
  27. if (result.barcodeFormat != kBarcodeFormatCode39) {
  28. return nil;
  29. }
  30. NSString *rawText = result.text;
  31. rawText = [[ZX_IOQ stringByReplacingMatchesInString:rawText options:0 range:NSMakeRange(0, rawText.length) withTemplate:@""] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  32. if ([ZX_AZ09 matchesInString:rawText options:0 range:NSMakeRange(0, rawText.length)] == 0) {
  33. return nil;
  34. }
  35. if (![self checkChecksum:rawText]) {
  36. return nil;
  37. }
  38. int modelYear = [self modelYear:[rawText characterAtIndex:9]];
  39. if (modelYear == -1) {
  40. return nil;
  41. }
  42. NSString *wmi = [rawText substringToIndex:3];
  43. return [[ZXVINParsedResult alloc] initWithVIN:rawText
  44. worldManufacturerID:wmi
  45. vehicleDescriptorSection:[rawText substringWithRange:NSMakeRange(3, 6)]
  46. vehicleIdentifierSection:[rawText substringWithRange:NSMakeRange(9, 8)]
  47. countryCode:[self countryCode:wmi]
  48. vehicleAttributes:[rawText substringWithRange:NSMakeRange(3, 5)]
  49. modelYear:modelYear
  50. plantCode:[rawText characterAtIndex:10]
  51. sequentialNumber:[rawText substringFromIndex:11]];
  52. }
  53. - (BOOL)checkChecksum:(NSString *)vin {
  54. int sum = 0;
  55. for (int i = 0; i < [vin length]; i++) {
  56. int vinPositionWeight = [self vinPositionWeight:i + 1];
  57. if (vinPositionWeight == -1) {
  58. return NO;
  59. }
  60. int vinCharValue = [self vinCharValue:[vin characterAtIndex:i]];
  61. if (vinCharValue == -1) {
  62. return NO;
  63. }
  64. sum += vinPositionWeight * vinCharValue;
  65. }
  66. unichar checkChar = [vin characterAtIndex:8];
  67. if (checkChar == '\0') {
  68. return NO;
  69. }
  70. unichar expectedCheckChar = [self checkChar:sum % 11];
  71. return checkChar == expectedCheckChar;
  72. }
  73. - (int)vinCharValue:(unichar)c {
  74. if (c >= 'A' && c <= 'I') {
  75. return (c - 'A') + 1;
  76. }
  77. if (c >= 'J' && c <= 'R') {
  78. return (c - 'J') + 1;
  79. }
  80. if (c >= 'S' && c <= 'Z') {
  81. return (c - 'S') + 2;
  82. }
  83. if (c >= '0' && c <= '9') {
  84. return c - '0';
  85. }
  86. return -1;
  87. }
  88. - (int)vinPositionWeight:(int)position {
  89. if (position >= 1 && position <= 7) {
  90. return 9 - position;
  91. }
  92. if (position == 8) {
  93. return 10;
  94. }
  95. if (position == 9) {
  96. return 0;
  97. }
  98. if (position >= 10 && position <= 17) {
  99. return 19 - position;
  100. }
  101. return -1;
  102. }
  103. - (unichar)checkChar:(int)remainder {
  104. if (remainder < 10) {
  105. return (unichar) ('0' + remainder);
  106. }
  107. if (remainder == 10) {
  108. return 'X';
  109. }
  110. return '\0';
  111. }
  112. - (int)modelYear:(unichar)c {
  113. if (c >= 'E' && c <= 'H') {
  114. return (c - 'E') + 1984;
  115. }
  116. if (c >= 'J' && c <= 'N') {
  117. return (c - 'J') + 1988;
  118. }
  119. if (c == 'P') {
  120. return 1993;
  121. }
  122. if (c >= 'R' && c <= 'T') {
  123. return (c - 'R') + 1994;
  124. }
  125. if (c >= 'V' && c <= 'Y') {
  126. return (c - 'V') + 1997;
  127. }
  128. if (c >= '1' && c <= '9') {
  129. return (c - '1') + 2001;
  130. }
  131. if (c >= 'A' && c <= 'D') {
  132. return (c - 'A') + 2010;
  133. }
  134. return -1;
  135. }
  136. - (NSString *)countryCode:(NSString *)wmi {
  137. unichar c1 = [wmi characterAtIndex:0];
  138. unichar c2 = [wmi characterAtIndex:1];
  139. switch (c1) {
  140. case '1':
  141. case '4':
  142. case '5':
  143. return @"US";
  144. case '2':
  145. return @"CA";
  146. case '3':
  147. if (c2 >= 'A' && c2 <= 'W') {
  148. return @"MX";
  149. }
  150. break;
  151. case '9':
  152. if ((c2 >= 'A' && c2 <= 'E') || (c2 >= '3' && c2 <= '9')) {
  153. return @"BR";
  154. }
  155. break;
  156. case 'J':
  157. if (c2 >= 'A' && c2 <= 'T') {
  158. return @"JP";
  159. }
  160. break;
  161. case 'K':
  162. if (c2 >= 'L' && c2 <= 'R') {
  163. return @"KO";
  164. }
  165. break;
  166. case 'L':
  167. return @"CN";
  168. case 'M':
  169. if (c2 >= 'A' && c2 <= 'E') {
  170. return @"IN";
  171. }
  172. break;
  173. case 'S':
  174. if (c2 >= 'A' && c2 <= 'M') {
  175. return @"UK";
  176. }
  177. if (c2 >= 'N' && c2 <= 'T') {
  178. return @"DE";
  179. }
  180. break;
  181. case 'V':
  182. if (c2 >= 'F' && c2 <= 'R') {
  183. return @"FR";
  184. }
  185. if (c2 >= 'S' && c2 <= 'W') {
  186. return @"ES";
  187. }
  188. break;
  189. case 'W':
  190. return @"DE";
  191. case 'X':
  192. if (c2 == '0' || (c2 >= '3' && c2 <= '9')) {
  193. return @"RU";
  194. }
  195. break;
  196. case 'Z':
  197. if (c2 >= 'A' && c2 <= 'R') {
  198. return @"IT";
  199. }
  200. break;
  201. }
  202. return nil;
  203. }
  204. @end