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.

ZXRSSExpandedFieldParser.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 2012 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 "ZXErrors.h"
  17. #import "ZXRSSExpandedFieldParser.h"
  18. static NSObject *VARIABLE_LENGTH = nil;
  19. static NSArray *TWO_DIGIT_DATA_LENGTH = nil;
  20. static NSArray *THREE_DIGIT_DATA_LENGTH = nil;
  21. static NSArray *THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH = nil;
  22. static NSArray *FOUR_DIGIT_DATA_LENGTH = nil;
  23. @implementation ZXRSSExpandedFieldParser
  24. + (void)initialize {
  25. if ([self class] != [ZXRSSExpandedFieldParser class]) return;
  26. if (VARIABLE_LENGTH == nil) {
  27. VARIABLE_LENGTH = [[NSObject alloc] init];
  28. }
  29. if (TWO_DIGIT_DATA_LENGTH == nil) {
  30. TWO_DIGIT_DATA_LENGTH = @[@[@"00", @18],
  31. @[@"01", @14],
  32. @[@"02", @14],
  33. @[@"10", VARIABLE_LENGTH, @20],
  34. @[@"11", @6],
  35. @[@"12", @6],
  36. @[@"13", @6],
  37. @[@"15", @6],
  38. @[@"17", @6],
  39. @[@"20", @2],
  40. @[@"21", VARIABLE_LENGTH, @20],
  41. @[@"22", VARIABLE_LENGTH, @29],
  42. @[@"30", VARIABLE_LENGTH, @8],
  43. @[@"37", VARIABLE_LENGTH, @8],
  44. //internal company codes
  45. @[@"90", VARIABLE_LENGTH, @30],
  46. @[@"91", VARIABLE_LENGTH, @30],
  47. @[@"92", VARIABLE_LENGTH, @30],
  48. @[@"93", VARIABLE_LENGTH, @30],
  49. @[@"94", VARIABLE_LENGTH, @30],
  50. @[@"95", VARIABLE_LENGTH, @30],
  51. @[@"96", VARIABLE_LENGTH, @30],
  52. @[@"97", VARIABLE_LENGTH, @30],
  53. @[@"98", VARIABLE_LENGTH, @30],
  54. @[@"99", VARIABLE_LENGTH, @30]];
  55. }
  56. if (THREE_DIGIT_DATA_LENGTH == nil) {
  57. THREE_DIGIT_DATA_LENGTH = @[@[@"240", VARIABLE_LENGTH, @30],
  58. @[@"241", VARIABLE_LENGTH, @30],
  59. @[@"242", VARIABLE_LENGTH, @6],
  60. @[@"250", VARIABLE_LENGTH, @30],
  61. @[@"251", VARIABLE_LENGTH, @30],
  62. @[@"253", VARIABLE_LENGTH, @17],
  63. @[@"254", VARIABLE_LENGTH, @20],
  64. @[@"400", VARIABLE_LENGTH, @30],
  65. @[@"401", VARIABLE_LENGTH, @30],
  66. @[@"402", @17],
  67. @[@"403", VARIABLE_LENGTH, @30],
  68. @[@"410", @13],
  69. @[@"411", @13],
  70. @[@"412", @13],
  71. @[@"413", @13],
  72. @[@"414", @13],
  73. @[@"420", VARIABLE_LENGTH, @20],
  74. @[@"421", VARIABLE_LENGTH, @15],
  75. @[@"422", @3],
  76. @[@"423", VARIABLE_LENGTH, @15],
  77. @[@"424", @3],
  78. @[@"425", @3],
  79. @[@"426", @3]];
  80. }
  81. if (THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH == nil) {
  82. THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH = @[@[@"310", @6],
  83. @[@"311", @6],
  84. @[@"312", @6],
  85. @[@"313", @6],
  86. @[@"314", @6],
  87. @[@"315", @6],
  88. @[@"316", @6],
  89. @[@"320", @6],
  90. @[@"321", @6],
  91. @[@"322", @6],
  92. @[@"323", @6],
  93. @[@"324", @6],
  94. @[@"325", @6],
  95. @[@"326", @6],
  96. @[@"327", @6],
  97. @[@"328", @6],
  98. @[@"329", @6],
  99. @[@"330", @6],
  100. @[@"331", @6],
  101. @[@"332", @6],
  102. @[@"333", @6],
  103. @[@"334", @6],
  104. @[@"335", @6],
  105. @[@"336", @6],
  106. @[@"340", @6],
  107. @[@"341", @6],
  108. @[@"342", @6],
  109. @[@"343", @6],
  110. @[@"344", @6],
  111. @[@"345", @6],
  112. @[@"346", @6],
  113. @[@"347", @6],
  114. @[@"348", @6],
  115. @[@"349", @6],
  116. @[@"350", @6],
  117. @[@"351", @6],
  118. @[@"352", @6],
  119. @[@"353", @6],
  120. @[@"354", @6],
  121. @[@"355", @6],
  122. @[@"356", @6],
  123. @[@"357", @6],
  124. @[@"360", @6],
  125. @[@"361", @6],
  126. @[@"362", @6],
  127. @[@"363", @6],
  128. @[@"364", @6],
  129. @[@"365", @6],
  130. @[@"366", @6],
  131. @[@"367", @6],
  132. @[@"368", @6],
  133. @[@"369", @6],
  134. @[@"390", VARIABLE_LENGTH, @15],
  135. @[@"391", VARIABLE_LENGTH, @18],
  136. @[@"392", VARIABLE_LENGTH, @15],
  137. @[@"393", VARIABLE_LENGTH, @18],
  138. @[@"703", VARIABLE_LENGTH, @30]];
  139. }
  140. if (FOUR_DIGIT_DATA_LENGTH == nil) {
  141. FOUR_DIGIT_DATA_LENGTH = @[@[@"7001", @13],
  142. @[@"7002", VARIABLE_LENGTH, @30],
  143. @[@"7003", @10],
  144. @[@"8001", @14],
  145. @[@"8002", VARIABLE_LENGTH, @20],
  146. @[@"8003", VARIABLE_LENGTH, @30],
  147. @[@"8004", VARIABLE_LENGTH, @30],
  148. @[@"8005", @6],
  149. @[@"8006", @18],
  150. @[@"8007", VARIABLE_LENGTH, @30],
  151. @[@"8008", VARIABLE_LENGTH, @12],
  152. @[@"8018", @18],
  153. @[@"8020", VARIABLE_LENGTH, @25],
  154. @[@"8100", @6],
  155. @[@"8101", @10],
  156. @[@"8102", @2],
  157. @[@"8110", VARIABLE_LENGTH, @70],
  158. @[@"8200", VARIABLE_LENGTH, @70]];
  159. }
  160. }
  161. + (NSString *)parseFieldsInGeneralPurpose:(NSString *)rawInformation error:(NSError **)error {
  162. if ([rawInformation length] == 0) {
  163. return @"";
  164. }
  165. if ([rawInformation length] < 2) {
  166. if (error) *error = ZXNotFoundErrorInstance();
  167. return nil;
  168. }
  169. NSString *firstTwoDigits = [rawInformation substringWithRange:NSMakeRange(0, 2)];
  170. for (int i = 0; i < [TWO_DIGIT_DATA_LENGTH count]; ++i) {
  171. if ([TWO_DIGIT_DATA_LENGTH[i][0] isEqualToString:firstTwoDigits]) {
  172. if ([TWO_DIGIT_DATA_LENGTH[i][1] isEqual:VARIABLE_LENGTH]) {
  173. return [self processVariableAI:2
  174. variableFieldSize:[TWO_DIGIT_DATA_LENGTH[i][2] intValue]
  175. rawInformation:rawInformation];
  176. }
  177. NSString *result = [self processFixedAI:2
  178. fieldSize:[TWO_DIGIT_DATA_LENGTH[i][1] intValue]
  179. rawInformation:rawInformation];
  180. if (!result) {
  181. if (error) *error = ZXNotFoundErrorInstance();
  182. return nil;
  183. }
  184. return result;
  185. }
  186. }
  187. if ([rawInformation length] < 3) {
  188. if (error) *error = ZXNotFoundErrorInstance();
  189. return nil;
  190. }
  191. NSString *firstThreeDigits = [rawInformation substringWithRange:NSMakeRange(0, 3)];
  192. for (int i = 0; i < [THREE_DIGIT_DATA_LENGTH count]; ++i) {
  193. if ([THREE_DIGIT_DATA_LENGTH[i][0] isEqualToString:firstThreeDigits]) {
  194. if ([THREE_DIGIT_DATA_LENGTH[i][1] isEqual:VARIABLE_LENGTH]) {
  195. return [self processVariableAI:3
  196. variableFieldSize:[THREE_DIGIT_DATA_LENGTH[i][2] intValue]
  197. rawInformation:rawInformation];
  198. }
  199. NSString *result = [self processFixedAI:3
  200. fieldSize:[THREE_DIGIT_DATA_LENGTH[i][1] intValue]
  201. rawInformation:rawInformation];
  202. if (!result) {
  203. if (error) *error = ZXNotFoundErrorInstance();
  204. return nil;
  205. }
  206. return result;
  207. }
  208. }
  209. for (int i = 0; i < [THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH count]; ++i) {
  210. if ([THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH[i][0] isEqualToString:firstThreeDigits]) {
  211. if ([THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH[i][1] isEqual:VARIABLE_LENGTH]) {
  212. return [self processVariableAI:4
  213. variableFieldSize:[THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH[i][2] intValue]
  214. rawInformation:rawInformation];
  215. }
  216. NSString *result = [self processFixedAI:4
  217. fieldSize:[THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH[i][1] intValue]
  218. rawInformation:rawInformation];
  219. if (!result) {
  220. if (error) *error = ZXNotFoundErrorInstance();
  221. return nil;
  222. }
  223. return result;
  224. }
  225. }
  226. if ([rawInformation length] < 4) {
  227. if (error) *error = ZXNotFoundErrorInstance();
  228. return nil;
  229. }
  230. NSString *firstFourDigits = [rawInformation substringWithRange:NSMakeRange(0, 4)];
  231. for (int i = 0; i < [FOUR_DIGIT_DATA_LENGTH count]; ++i) {
  232. if ([FOUR_DIGIT_DATA_LENGTH[i][0] isEqualToString:firstFourDigits]) {
  233. if ([FOUR_DIGIT_DATA_LENGTH[i][1] isEqual:VARIABLE_LENGTH]) {
  234. NSString *result = [self processVariableAI:4
  235. variableFieldSize:[FOUR_DIGIT_DATA_LENGTH[i][2] intValue]
  236. rawInformation:rawInformation];
  237. if (!result) {
  238. if (error) *error = ZXNotFoundErrorInstance();
  239. return nil;
  240. }
  241. return result;
  242. }
  243. NSString *result = [self processFixedAI:4
  244. fieldSize:[FOUR_DIGIT_DATA_LENGTH[i][1] intValue]
  245. rawInformation:rawInformation];
  246. if (!result) {
  247. if (error) *error = ZXNotFoundErrorInstance();
  248. return nil;
  249. }
  250. return result;
  251. }
  252. }
  253. if (error) *error = ZXNotFoundErrorInstance();
  254. return nil;
  255. }
  256. + (NSString *)processFixedAI:(int)aiSize fieldSize:(int)fieldSize rawInformation:(NSString *)rawInformation {
  257. if ([rawInformation length] < aiSize) {
  258. return nil;
  259. }
  260. NSString *ai = [rawInformation substringWithRange:NSMakeRange(0, aiSize)];
  261. if ([rawInformation length] < aiSize + fieldSize) {
  262. return nil;
  263. }
  264. NSString *field = [rawInformation substringWithRange:NSMakeRange(aiSize, fieldSize)];
  265. NSString *remaining;
  266. if (aiSize + fieldSize == rawInformation.length) {
  267. remaining = @"";
  268. } else {
  269. remaining = [rawInformation substringFromIndex:aiSize + fieldSize];
  270. }
  271. NSString *result = [NSString stringWithFormat:@"(%@)%@", ai, field];
  272. NSString *parsedAI = [self parseFieldsInGeneralPurpose:remaining error:nil];
  273. return parsedAI == nil ? result : [result stringByAppendingString:parsedAI];
  274. }
  275. + (NSString *)processVariableAI:(int)aiSize variableFieldSize:(int)variableFieldSize rawInformation:(NSString *)rawInformation {
  276. NSString *ai = [rawInformation substringWithRange:NSMakeRange(0, aiSize)];
  277. int maxSize;
  278. if ([rawInformation length] < aiSize + variableFieldSize) {
  279. maxSize = (int)[rawInformation length];
  280. } else {
  281. maxSize = aiSize + variableFieldSize;
  282. }
  283. NSString *field = [rawInformation substringWithRange:NSMakeRange(aiSize, maxSize - aiSize)];
  284. NSString *remaining = [rawInformation substringFromIndex:maxSize];
  285. NSString *result = [NSString stringWithFormat:@"(%@)%@", ai, field];
  286. NSString *parsedAI = [self parseFieldsInGeneralPurpose:remaining error:nil];
  287. return parsedAI == nil ? result : [result stringByAppendingString:parsedAI];
  288. }
  289. @end