Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ZXDataMatrixDecodedBitStreamParser.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 "ZXBitSource.h"
  17. #import "ZXByteArray.h"
  18. #import "ZXDataMatrixDecodedBitStreamParser.h"
  19. #import "ZXDecoderResult.h"
  20. #import "ZXErrors.h"
  21. /**
  22. * See ISO 16022:2006, Annex C Table C.1
  23. * The C40 Basic Character Set (*'s used for placeholders for the shift values)
  24. */
  25. static const unichar C40_BASIC_SET_CHARS[40] = {
  26. '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  27. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  28. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  29. };
  30. static const unichar C40_SHIFT2_SET_CHARS[40] = {
  31. '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
  32. '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'
  33. };
  34. /**
  35. * See ISO 16022:2006, Annex C Table C.2
  36. * The Text Basic Character Set (*'s used for placeholders for the shift values)
  37. */
  38. static const unichar TEXT_BASIC_SET_CHARS[40] = {
  39. '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  40. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  41. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  42. };
  43. // Shift 2 for Text is the same encoding as C40
  44. static unichar TEXT_SHIFT2_SET_CHARS[40];
  45. static const unichar TEXT_SHIFT3_SET_CHARS[32] = {
  46. '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  47. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', (unichar) 127
  48. };
  49. enum {
  50. PAD_ENCODE = 0, // Not really a mode
  51. ASCII_ENCODE,
  52. C40_ENCODE,
  53. TEXT_ENCODE,
  54. ANSIX12_ENCODE,
  55. EDIFACT_ENCODE,
  56. BASE256_ENCODE
  57. };
  58. @implementation ZXDataMatrixDecodedBitStreamParser
  59. + (void)initialize {
  60. if ([self class] != [ZXDataMatrixDecodedBitStreamParser class]) return;
  61. memcpy(TEXT_SHIFT2_SET_CHARS, C40_SHIFT2_SET_CHARS, sizeof(C40_SHIFT2_SET_CHARS));
  62. }
  63. + (ZXDecoderResult *)decode:(ZXByteArray *)bytes error:(NSError **)error {
  64. ZXBitSource *bits = [[ZXBitSource alloc] initWithBytes:bytes];
  65. NSMutableString *result = [NSMutableString stringWithCapacity:100];
  66. NSMutableString *resultTrailer = [NSMutableString string];
  67. NSMutableArray *byteSegments = [NSMutableArray arrayWithCapacity:1];
  68. int mode = ASCII_ENCODE;
  69. do {
  70. if (mode == ASCII_ENCODE) {
  71. mode = [self decodeAsciiSegment:bits result:result resultTrailer:resultTrailer];
  72. if (mode == -1) {
  73. if (error) *error = ZXFormatErrorInstance();
  74. return nil;
  75. }
  76. } else {
  77. switch (mode) {
  78. case C40_ENCODE:
  79. if (![self decodeC40Segment:bits result:result]) {
  80. if (error) *error = ZXFormatErrorInstance();
  81. return nil;
  82. }
  83. break;
  84. case TEXT_ENCODE:
  85. if (![self decodeTextSegment:bits result:result]) {
  86. if (error) *error = ZXFormatErrorInstance();
  87. return nil;
  88. }
  89. break;
  90. case ANSIX12_ENCODE:
  91. if (![self decodeAnsiX12Segment:bits result:result]) {
  92. if (error) *error = ZXFormatErrorInstance();
  93. return nil;
  94. }
  95. break;
  96. case EDIFACT_ENCODE:
  97. [self decodeEdifactSegment:bits result:result];
  98. break;
  99. case BASE256_ENCODE:
  100. if (![self decodeBase256Segment:bits result:result byteSegments:byteSegments]) {
  101. if (error) *error = ZXFormatErrorInstance();
  102. return nil;
  103. }
  104. break;
  105. default:
  106. if (error) *error = ZXFormatErrorInstance();
  107. return nil;
  108. }
  109. mode = ASCII_ENCODE;
  110. }
  111. } while (mode != PAD_ENCODE && bits.available > 0);
  112. if ([resultTrailer length] > 0) {
  113. [result appendString:resultTrailer];
  114. }
  115. return [[ZXDecoderResult alloc] initWithRawBytes:bytes
  116. text:result
  117. byteSegments:[byteSegments count] == 0 ? nil : byteSegments
  118. ecLevel:nil];
  119. }
  120. /**
  121. * See ISO 16022:2006, 5.2.3 and Annex C, Table C.2
  122. */
  123. + (int)decodeAsciiSegment:(ZXBitSource *)bits result:(NSMutableString *)result resultTrailer:(NSMutableString *)resultTrailer {
  124. BOOL upperShift = NO;
  125. do {
  126. int oneByte = [bits readBits:8];
  127. if (oneByte == 0) {
  128. return -1;
  129. } else if (oneByte <= 128) { // ASCII data (ASCII value + 1)
  130. if (upperShift) {
  131. oneByte += 128;
  132. //upperShift = NO;
  133. }
  134. [result appendFormat:@"%C", (unichar)(oneByte - 1)];
  135. return ASCII_ENCODE;
  136. } else if (oneByte == 129) { // Pad
  137. return PAD_ENCODE;
  138. } else if (oneByte <= 229) { // 2-digit data 00-99 (Numeric Value + 130)
  139. int value = oneByte - 130;
  140. if (value < 10) { // pad with '0' for single digit values
  141. [result appendString:@"0"];
  142. }
  143. [result appendFormat:@"%d", value];
  144. } else if (oneByte == 230) { // Latch to C40 encodation
  145. return C40_ENCODE;
  146. } else if (oneByte == 231) { // Latch to Base 256 encodation
  147. return BASE256_ENCODE;
  148. } else if (oneByte == 232) {
  149. // FNC1
  150. [result appendFormat:@"%C", (unichar)29]; // translate as ASCII 29
  151. } else if (oneByte == 233 || oneByte == 234) {
  152. // Structured Append, Reader Programming
  153. // Ignore these symbols for now
  154. //return -1;
  155. } else if (oneByte == 235) { // Upper Shift (shift to Extended ASCII)
  156. upperShift = YES;
  157. } else if (oneByte == 236) { // 05 Macro
  158. [result appendFormat:@"[)>%C%C", (unichar)0x001E05, (unichar)0x001D];
  159. [resultTrailer insertString:[NSString stringWithFormat:@"%C%C", (unichar)0x001E, (unichar)0x0004] atIndex:0];
  160. } else if (oneByte == 237) { // 06 Macro
  161. [result appendFormat:@"[)>%C%C", (unichar)0x001E06, (unichar)0x001D];
  162. [resultTrailer insertString:[NSString stringWithFormat:@"%C%C", (unichar)0x001E, (unichar)0x0004] atIndex:0];
  163. } else if (oneByte == 238) { // Latch to ANSI X12 encodation
  164. return ANSIX12_ENCODE;
  165. } else if (oneByte == 239) { // Latch to Text encodation
  166. return TEXT_ENCODE;
  167. } else if (oneByte == 240) { // Latch to EDIFACT encodation
  168. return EDIFACT_ENCODE;
  169. } else if (oneByte == 241) { // ECI Character
  170. // TODO(bbrown): I think we need to support ECI
  171. // Ignore this symbol for now
  172. } else if (oneByte >= 242) { // Not to be used in ASCII encodation
  173. // ... but work around encoders that end with 254, latch back to ASCII
  174. if (oneByte != 254 || bits.available != 0) {
  175. return -1;
  176. }
  177. }
  178. } while (bits.available > 0);
  179. return ASCII_ENCODE;
  180. }
  181. /**
  182. * See ISO 16022:2006, 5.2.5 and Annex C, Table C.1
  183. */
  184. + (BOOL)decodeC40Segment:(ZXBitSource *)bits result:(NSMutableString *)result {
  185. // Three C40 values are encoded in a 16-bit value as
  186. // (1600 * C1) + (40 * C2) + C3 + 1
  187. // TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
  188. BOOL upperShift = NO;
  189. int cValues[3] = {0};
  190. int shift = 0;
  191. do {
  192. // If there is only one byte left then it will be encoded as ASCII
  193. if ([bits available] == 8) {
  194. return YES;
  195. }
  196. int firstByte = [bits readBits:8];
  197. if (firstByte == 254) { // Unlatch codeword
  198. return YES;
  199. }
  200. [self parseTwoBytes:firstByte secondByte:[bits readBits:8] result:cValues];
  201. for (int i = 0; i < 3; i++) {
  202. int cValue = cValues[i];
  203. switch (shift) {
  204. case 0:
  205. if (cValue < 3) {
  206. shift = cValue + 1;
  207. } else if (cValue < sizeof(C40_BASIC_SET_CHARS) / sizeof(char)) {
  208. unichar c40char = C40_BASIC_SET_CHARS[cValue];
  209. if (upperShift) {
  210. [result appendFormat:@"%C", (unichar)(c40char + 128)];
  211. upperShift = NO;
  212. } else {
  213. [result appendFormat:@"%C", c40char];
  214. }
  215. } else {
  216. return NO;
  217. }
  218. break;
  219. case 1:
  220. if (upperShift) {
  221. [result appendFormat:@"%C", (unichar)(cValue + 128)];
  222. upperShift = NO;
  223. } else {
  224. [result appendFormat:@"%C", (unichar)cValue];
  225. }
  226. shift = 0;
  227. break;
  228. case 2:
  229. if (cValue < sizeof(C40_SHIFT2_SET_CHARS) / sizeof(char)) {
  230. unichar c40char = C40_SHIFT2_SET_CHARS[cValue];
  231. if (upperShift) {
  232. [result appendFormat:@"%C", (unichar)(c40char + 128)];
  233. upperShift = NO;
  234. } else {
  235. [result appendFormat:@"%C", c40char];
  236. }
  237. } else if (cValue == 27) { // FNC1
  238. [result appendFormat:@"%C", (unichar)29]; // translate as ASCII 29
  239. } else if (cValue == 30) { // Upper Shift
  240. upperShift = YES;
  241. } else {
  242. return NO;
  243. }
  244. shift = 0;
  245. break;
  246. case 3:
  247. if (upperShift) {
  248. [result appendFormat:@"%C", (unichar)(cValue + 224)];
  249. upperShift = NO;
  250. } else {
  251. [result appendFormat:@"%C", (unichar)(cValue + 96)];
  252. }
  253. shift = 0;
  254. break;
  255. default:
  256. return NO;
  257. }
  258. }
  259. } while (bits.available > 0);
  260. return YES;
  261. }
  262. /**
  263. * See ISO 16022:2006, 5.2.6 and Annex C, Table C.2
  264. */
  265. + (BOOL)decodeTextSegment:(ZXBitSource *)bits result:(NSMutableString *)result {
  266. // Three Text values are encoded in a 16-bit value as
  267. // (1600 * C1) + (40 * C2) + C3 + 1
  268. // TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
  269. BOOL upperShift = NO;
  270. int cValues[3] = {0};
  271. int shift = 0;
  272. do {
  273. // If there is only one byte left then it will be encoded as ASCII
  274. if (bits.available == 8) {
  275. return YES;
  276. }
  277. int firstByte = [bits readBits:8];
  278. if (firstByte == 254) { // Unlatch codeword
  279. return YES;
  280. }
  281. [self parseTwoBytes:firstByte secondByte:[bits readBits:8] result:cValues];
  282. for (int i = 0; i < 3; i++) {
  283. int cValue = cValues[i];
  284. switch (shift) {
  285. case 0:
  286. if (cValue < 3) {
  287. shift = cValue + 1;
  288. } else if (cValue < sizeof(TEXT_BASIC_SET_CHARS) / sizeof(char)) {
  289. unichar textChar = TEXT_BASIC_SET_CHARS[cValue];
  290. if (upperShift) {
  291. [result appendFormat:@"%C", (unichar)(textChar + 128)];
  292. upperShift = NO;
  293. } else {
  294. [result appendFormat:@"%C", textChar];
  295. }
  296. } else {
  297. return NO;
  298. }
  299. break;
  300. case 1:
  301. if (upperShift) {
  302. [result appendFormat:@"%C", (unichar)(cValue + 128)];
  303. upperShift = NO;
  304. } else {
  305. [result appendFormat:@"%C", (unichar)cValue];
  306. }
  307. shift = 0;
  308. break;
  309. case 2:
  310. // Shift 2 for Text is the same encoding as C40
  311. if (cValue < sizeof(TEXT_SHIFT2_SET_CHARS) / sizeof(unichar)) {
  312. unichar textChar = TEXT_SHIFT2_SET_CHARS[cValue];
  313. if (upperShift) {
  314. [result appendFormat:@"%C", (unichar)(textChar + 128)];
  315. upperShift = NO;
  316. } else {
  317. [result appendFormat:@"%C", textChar];
  318. }
  319. } else if (cValue == 27) {
  320. [result appendFormat:@"%C", (unichar)29]; // translate as ASCII 29
  321. } else if (cValue == 30) { // Upper Shift
  322. upperShift = YES;
  323. } else {
  324. return NO;
  325. }
  326. shift = 0;
  327. break;
  328. case 3:
  329. if (cValue < sizeof(TEXT_SHIFT3_SET_CHARS) / sizeof(char)) {
  330. unichar textChar = TEXT_SHIFT3_SET_CHARS[cValue];
  331. if (upperShift) {
  332. [result appendFormat:@"%C", (unichar)(textChar + 128)];
  333. upperShift = NO;
  334. } else {
  335. [result appendFormat:@"%C", textChar];
  336. }
  337. shift = 0;
  338. } else {
  339. return NO;
  340. }
  341. break;
  342. default:
  343. return NO;
  344. }
  345. }
  346. } while (bits.available > 0);
  347. return YES;
  348. }
  349. /**
  350. * See ISO 16022:2006, 5.2.7
  351. */
  352. + (BOOL)decodeAnsiX12Segment:(ZXBitSource *)bits result:(NSMutableString *)result {
  353. // Three ANSI X12 values are encoded in a 16-bit value as
  354. // (1600 * C1) + (40 * C2) + C3 + 1
  355. int cValues[3] = {0};
  356. do {
  357. // If there is only one byte left then it will be encoded as ASCII
  358. if (bits.available == 8) {
  359. return YES;
  360. }
  361. int firstByte = [bits readBits:8];
  362. if (firstByte == 254) { // Unlatch codeword
  363. return YES;
  364. }
  365. [self parseTwoBytes:firstByte secondByte:[bits readBits:8] result:cValues];
  366. for (int i = 0; i < 3; i++) {
  367. int cValue = cValues[i];
  368. if (cValue == 0) { // X12 segment terminator <CR>
  369. [result appendString:@"\r"];
  370. } else if (cValue == 1) { // X12 segment separator *
  371. [result appendString:@"*"];
  372. } else if (cValue == 2) { // X12 sub-element separator >
  373. [result appendString:@">"];
  374. } else if (cValue == 3) { // space
  375. [result appendString:@" "];
  376. } else if (cValue < 14) { // 0 - 9
  377. [result appendFormat:@"%C", (unichar)(cValue + 44)];
  378. } else if (cValue < 40) { // A - Z
  379. [result appendFormat:@"%C", (unichar)(cValue + 51)];
  380. } else {
  381. return NO;
  382. }
  383. }
  384. } while (bits.available > 0);
  385. return YES;
  386. }
  387. + (void)parseTwoBytes:(int)firstByte secondByte:(int)secondByte result:(int[])result {
  388. int fullBitValue = (firstByte << 8) + secondByte - 1;
  389. int temp = fullBitValue / 1600;
  390. result[0] = temp;
  391. fullBitValue -= temp * 1600;
  392. temp = fullBitValue / 40;
  393. result[1] = temp;
  394. result[2] = fullBitValue - temp * 40;
  395. }
  396. /**
  397. * See ISO 16022:2006, 5.2.8 and Annex C Table C.3
  398. */
  399. + (void)decodeEdifactSegment:(ZXBitSource *)bits result:(NSMutableString *)result {
  400. do {
  401. // If there is only two or less bytes left then it will be encoded as ASCII
  402. if (bits.available <= 16) {
  403. return;
  404. }
  405. for (int i = 0; i < 4; i++) {
  406. int edifactValue = [bits readBits:6];
  407. // Check for the unlatch character
  408. if (edifactValue == 0x1F) { // 011111
  409. // Read rest of byte, which should be 0, and stop
  410. int bitsLeft = 8 - bits.bitOffset;
  411. if (bitsLeft != 8) {
  412. [bits readBits:bitsLeft];
  413. }
  414. return;
  415. }
  416. if ((edifactValue & 0x20) == 0) { // no 1 in the leading (6th) bit
  417. edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
  418. }
  419. [result appendFormat:@"%c", (char)edifactValue];
  420. }
  421. } while (bits.available > 0);
  422. }
  423. /**
  424. * See ISO 16022:2006, 5.2.9 and Annex B, B.2
  425. */
  426. + (BOOL)decodeBase256Segment:(ZXBitSource *)bits result:(NSMutableString *)result byteSegments:(NSMutableArray *)byteSegments {
  427. int codewordPosition = 1 + bits.byteOffset; // position is 1-indexed
  428. int d1 = [self unrandomize255State:[bits readBits:8] base256CodewordPosition:codewordPosition++];
  429. int count;
  430. if (d1 == 0) {
  431. count = [bits available] / 8;
  432. } else if (d1 < 250) {
  433. count = d1;
  434. } else {
  435. count = 250 * (d1 - 249) + [self unrandomize255State:[bits readBits:8] base256CodewordPosition:codewordPosition++];
  436. }
  437. if (count < 0) {
  438. return NO;
  439. }
  440. ZXByteArray *bytes = [[ZXByteArray alloc] initWithLength:count];
  441. for (int i = 0; i < count; i++) {
  442. if ([bits available] < 8) {
  443. return NO;
  444. }
  445. bytes.array[i] = (int8_t)[self unrandomize255State:[bits readBits:8] base256CodewordPosition:codewordPosition++];
  446. }
  447. [byteSegments addObject:bytes];
  448. [result appendString:[[NSString alloc] initWithBytes:bytes.array length:bytes.length encoding:NSISOLatin1StringEncoding]];
  449. return YES;
  450. }
  451. /**
  452. * See ISO 16022:2006, Annex B, B.2
  453. */
  454. + (int)unrandomize255State:(int)randomizedBase256Codeword base256CodewordPosition:(int)base256CodewordPosition {
  455. int pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1;
  456. int tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
  457. return tempVariable >= 0 ? tempVariable : tempVariable + 256;
  458. }
  459. @end