選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ZXPDF417HighLevelEncoder.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright 2006 Jeremias Maerki in part, and ZXing Authors in part
  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 "ZXByteArray.h"
  17. #import "ZXCharacterSetECI.h"
  18. #import "ZXErrors.h"
  19. #import "ZXPDF417HighLevelEncoder.h"
  20. /**
  21. * code for Text compaction
  22. */
  23. const int ZX_PDF417_TEXT_COMPACTION = 0;
  24. /**
  25. * code for Byte compaction
  26. */
  27. const int ZX_PDF417_BYTE_COMPACTION = 1;
  28. /**
  29. * code for Numeric compaction
  30. */
  31. const int ZX_PDF417_NUMERIC_COMPACTION = 2;
  32. /**
  33. * Text compaction submode Alpha
  34. */
  35. const int ZX_PDF417_SUBMODE_ALPHA = 0;
  36. /**
  37. * Text compaction submode Lower
  38. */
  39. const int ZX_PDF417_SUBMODE_LOWER = 1;
  40. /**
  41. * Text compaction submode Mixed
  42. */
  43. const int ZX_PDF417_SUBMODE_MIXED = 2;
  44. /**
  45. * Text compaction submode Punctuation
  46. */
  47. const int ZX_PDF417_SUBMODE_PUNCTUATION = 3;
  48. /**
  49. * mode latch to Text Compaction mode
  50. */
  51. const int ZX_PDF417_LATCH_TO_TEXT = 900;
  52. /**
  53. * mode latch to Byte Compaction mode (number of characters NOT a multiple of 6)
  54. */
  55. const int ZX_PDF417_LATCH_TO_BYTE_PADDED = 901;
  56. /**
  57. * mode latch to Numeric Compaction mode
  58. */
  59. const int ZX_PDF417_LATCH_TO_NUMERIC = 902;
  60. /**
  61. * mode shift to Byte Compaction mode
  62. */
  63. const int ZX_PDF417_SHIFT_TO_BYTE = 913;
  64. /**
  65. * mode latch to Byte Compaction mode (number of characters a multiple of 6)
  66. */
  67. const int ZX_PDF417_LATCH_TO_BYTE = 924;
  68. /**
  69. * identifier for a user defined Extended Channel Interpretation (ECI)
  70. */
  71. const int ZX_PDF417_HIGH_LEVEL_ECI_USER_DEFINED = 925;
  72. /**
  73. * identifier for a general purpose ECO format
  74. */
  75. const int ZX_PDF417_HIGH_LEVEL_ECI_GENERAL_PURPOSE = 926;
  76. /**
  77. * identifier for an ECI of a character set of code page
  78. */
  79. const int ZX_PDF417_HIGH_LEVEL_ECI_CHARSET = 927;
  80. /**
  81. * Raw code table for text compaction Mixed sub-mode
  82. */
  83. const int8_t ZX_PDF417_TEXT_MIXED_RAW[] = {
  84. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58,
  85. 35, 45, 46, 36, 47, 43, 37, 42, 61, 94, 0, 32, 0, 0, 0};
  86. /**
  87. * Raw code table for text compaction: Punctuation sub-mode
  88. */
  89. const int8_t ZX_PDF417_TEXT_PUNCTUATION_RAW[] = {
  90. 59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58,
  91. 10, 45, 46, 36, 47, 34, 124, 42, 40, 41, 63, 123, 125, 39, 0};
  92. const int ZX_PDF417_MIXED_TABLE_LEN = 128;
  93. unichar ZX_PDF417_MIXED_TABLE[ZX_PDF417_MIXED_TABLE_LEN];
  94. const int ZX_PDF417_PUNCTUATION_LEN = 128;
  95. unichar ZX_PDF417_PUNCTUATION[ZX_PDF417_PUNCTUATION_LEN];
  96. const NSStringEncoding ZX_PDF417_DEFAULT_ENCODING = NSISOLatin1StringEncoding;
  97. @implementation ZXPDF417HighLevelEncoder
  98. + (void)initialize {
  99. if ([self class] != [ZXPDF417HighLevelEncoder class]) return;
  100. //Construct inverse lookups
  101. for (int i = 0; i < ZX_PDF417_MIXED_TABLE_LEN; i++) {
  102. ZX_PDF417_MIXED_TABLE[i] = 0xFF;
  103. }
  104. for (int8_t i = 0; i < sizeof(ZX_PDF417_TEXT_MIXED_RAW) / sizeof(int8_t); i++) {
  105. int8_t b = ZX_PDF417_TEXT_MIXED_RAW[i];
  106. if (b > 0) {
  107. ZX_PDF417_MIXED_TABLE[b] = i;
  108. }
  109. }
  110. for (int i = 0; i < ZX_PDF417_PUNCTUATION_LEN; i++) {
  111. ZX_PDF417_PUNCTUATION[i] = 0xFF;
  112. }
  113. for (int8_t i = 0; i < sizeof(ZX_PDF417_TEXT_PUNCTUATION_RAW) / sizeof(int8_t); i++) {
  114. int8_t b = ZX_PDF417_TEXT_PUNCTUATION_RAW[i];
  115. if (b > 0) {
  116. ZX_PDF417_PUNCTUATION[b] = i;
  117. }
  118. }
  119. }
  120. + (NSString *)encodeHighLevel:(NSString *)msg compaction:(ZXPDF417Compaction)compaction encoding:(NSStringEncoding)encoding error:(NSError **)error {
  121. //the codewords 0..928 are encoded as Unicode characters
  122. NSMutableString *sb = [NSMutableString stringWithCapacity:msg.length];
  123. if (encoding == 0) {
  124. encoding = ZX_PDF417_DEFAULT_ENCODING;
  125. } else if (ZX_PDF417_DEFAULT_ENCODING != encoding) {
  126. ZXCharacterSetECI *eci = [ZXCharacterSetECI characterSetECIByEncoding:encoding];
  127. if (![self encodingECI:eci.value sb:sb error:error]) {
  128. return nil;
  129. }
  130. }
  131. NSUInteger len = msg.length;
  132. int p = 0;
  133. int textSubMode = ZX_PDF417_SUBMODE_ALPHA;
  134. // User selected encoding mode
  135. ZXByteArray *bytes = nil; //Fill later and only if needed
  136. if (compaction == ZXPDF417CompactionText) {
  137. [self encodeText:msg startpos:p count:(int)len buffer:sb initialSubmode:textSubMode];
  138. } else if (compaction == ZXPDF417CompactionByte) {
  139. bytes = [self bytesForMessage:msg encoding:encoding];
  140. [self encodeBinary:bytes startpos:p count:(int)msg.length startmode:ZX_PDF417_BYTE_COMPACTION buffer:sb];
  141. } else if (compaction == ZXPDF417CompactionNumeric) {
  142. [sb appendFormat:@"%C", (unichar) ZX_PDF417_LATCH_TO_NUMERIC];
  143. [self encodeNumeric:msg startpos:p count:(int)len buffer:sb];
  144. } else {
  145. int encodingMode = ZX_PDF417_TEXT_COMPACTION; //Default mode, see 4.4.2.1
  146. while (p < len) {
  147. int n = [self determineConsecutiveDigitCount:msg startpos:p];
  148. if (n >= 13) {
  149. [sb appendFormat:@"%C", (unichar) ZX_PDF417_LATCH_TO_NUMERIC];
  150. encodingMode = ZX_PDF417_NUMERIC_COMPACTION;
  151. textSubMode = ZX_PDF417_SUBMODE_ALPHA; //Reset after latch
  152. [self encodeNumeric:msg startpos:p count:n buffer:sb];
  153. p += n;
  154. } else {
  155. int t = [self determineConsecutiveTextCount:msg startpos:p];
  156. if (t >= 5 || n == len) {
  157. if (encodingMode != ZX_PDF417_TEXT_COMPACTION) {
  158. [sb appendFormat:@"%C", (unichar) ZX_PDF417_LATCH_TO_TEXT];
  159. encodingMode = ZX_PDF417_TEXT_COMPACTION;
  160. textSubMode = ZX_PDF417_SUBMODE_ALPHA; //start with submode alpha after latch
  161. }
  162. textSubMode = [self encodeText:msg startpos:p count:t buffer:sb initialSubmode:textSubMode];
  163. p += t;
  164. } else {
  165. if (bytes == NULL) {
  166. bytes = [self bytesForMessage:msg encoding:encoding];
  167. }
  168. int b = [self determineConsecutiveBinaryCount:msg bytes:bytes startpos:p error:error];
  169. if (b == -1) {
  170. return nil;
  171. } else if (b == 0) {
  172. b = 1;
  173. }
  174. if (b == 1 && encodingMode == ZX_PDF417_TEXT_COMPACTION) {
  175. //Switch for one byte (instead of latch)
  176. [self encodeBinary:bytes startpos:p count:1 startmode:ZX_PDF417_TEXT_COMPACTION buffer:sb];
  177. } else {
  178. //Mode latch performed by encodeBinary
  179. [self encodeBinary:bytes startpos:p count:b startmode:encodingMode buffer:sb];
  180. encodingMode = ZX_PDF417_BYTE_COMPACTION;
  181. textSubMode = ZX_PDF417_SUBMODE_ALPHA; //Reset after latch
  182. }
  183. p += b;
  184. }
  185. }
  186. }
  187. }
  188. return sb;
  189. }
  190. /**
  191. * Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E),
  192. * chapter 4.4.2.
  193. *
  194. * @param msg the message
  195. * @param startpos the start position within the message
  196. * @param count the number of characters to encode
  197. * @param sb receives the encoded codewords
  198. * @param initialSubmode should normally be SUBMODE_ALPHA
  199. * @return the text submode in which this method ends
  200. */
  201. + (int)encodeText:(NSString *)msg startpos:(int)startpos count:(int)count buffer:(NSMutableString *)sb initialSubmode:(int)initialSubmode {
  202. NSMutableString *tmp = [NSMutableString stringWithCapacity:count];
  203. int submode = initialSubmode;
  204. int idx = 0;
  205. while (true) {
  206. unichar ch = [msg characterAtIndex:startpos + idx];
  207. switch (submode) {
  208. case ZX_PDF417_SUBMODE_ALPHA:
  209. if ([self isAlphaUpper:ch]) {
  210. if (ch == ' ') {
  211. [tmp appendFormat:@"%C", (unichar) 26]; //space
  212. } else {
  213. [tmp appendFormat:@"%C", (unichar) (ch - 65)];
  214. }
  215. } else {
  216. if ([self isAlphaLower:ch]) {
  217. submode = ZX_PDF417_SUBMODE_LOWER;
  218. [tmp appendFormat:@"%C", (unichar) 27]; //ll
  219. continue;
  220. } else if ([self isMixed:ch]) {
  221. submode = ZX_PDF417_SUBMODE_MIXED;
  222. [tmp appendFormat:@"%C", (unichar) 28]; //ml
  223. continue;
  224. } else {
  225. [tmp appendFormat:@"%C", (unichar) 29]; //ps
  226. [tmp appendFormat:@"%C", ZX_PDF417_PUNCTUATION[ch]];
  227. break;
  228. }
  229. }
  230. break;
  231. case ZX_PDF417_SUBMODE_LOWER:
  232. if ([self isAlphaLower:ch]) {
  233. if (ch == ' ') {
  234. [tmp appendFormat:@"%C", (unichar) 26]; //space
  235. } else {
  236. [tmp appendFormat:@"%C", (unichar) (ch - 97)];
  237. }
  238. } else {
  239. if ([self isAlphaUpper:ch]) {
  240. [tmp appendFormat:@"%C", (unichar) 27]; //as
  241. [tmp appendFormat:@"%C", (unichar) (ch - 65)];
  242. //space cannot happen here, it is also in "Lower"
  243. break;
  244. } else if ([self isMixed:ch]) {
  245. submode = ZX_PDF417_SUBMODE_MIXED;
  246. [tmp appendFormat:@"%C", (unichar) 28]; //ml
  247. continue;
  248. } else {
  249. [tmp appendFormat:@"%C", (unichar) 29]; //ps
  250. [tmp appendFormat:@"%C", ZX_PDF417_PUNCTUATION[ch]];
  251. break;
  252. }
  253. }
  254. break;
  255. case ZX_PDF417_SUBMODE_MIXED:
  256. if ([self isMixed:ch]) {
  257. [tmp appendFormat:@"%C", ZX_PDF417_MIXED_TABLE[ch]]; //as
  258. } else {
  259. if ([self isAlphaUpper:ch]) {
  260. submode = ZX_PDF417_SUBMODE_ALPHA;
  261. [tmp appendFormat:@"%C", (unichar) 28]; //al
  262. continue;
  263. } else if ([self isAlphaLower:ch]) {
  264. submode = ZX_PDF417_SUBMODE_LOWER;
  265. [tmp appendFormat:@"%C", (unichar) 27]; //ll
  266. continue;
  267. } else {
  268. if (startpos + idx + 1 < count) {
  269. char next = [msg characterAtIndex:startpos + idx + 1];
  270. if ([self isPunctuation:next]) {
  271. submode = ZX_PDF417_SUBMODE_PUNCTUATION;
  272. [tmp appendFormat:@"%C", (unichar) 25]; //pl
  273. continue;
  274. }
  275. }
  276. [tmp appendFormat:@"%C", (unichar) 29]; //ps
  277. [tmp appendFormat:@"%C", ZX_PDF417_PUNCTUATION[ch]];
  278. }
  279. }
  280. break;
  281. default: //ZX_PDF417_SUBMODE_PUNCTUATION
  282. if ([self isPunctuation:ch]) {
  283. [tmp appendFormat:@"%C", ZX_PDF417_PUNCTUATION[ch]];
  284. } else {
  285. submode = ZX_PDF417_SUBMODE_ALPHA;
  286. [tmp appendFormat:@"%C", (unichar) 29]; //al
  287. continue;
  288. }
  289. }
  290. idx++;
  291. if (idx >= count) {
  292. break;
  293. }
  294. }
  295. unichar h = 0;
  296. NSUInteger len = tmp.length;
  297. for (int i = 0; i < len; i++) {
  298. BOOL odd = (i % 2) != 0;
  299. if (odd) {
  300. h = (unichar) ((h * 30) + [tmp characterAtIndex:i]);
  301. [sb appendFormat:@"%C", h];
  302. } else {
  303. h = [tmp characterAtIndex:i];
  304. }
  305. }
  306. if ((len % 2) != 0) {
  307. [sb appendFormat:@"%C", (unichar) ((h * 30) + 29)]; //ps
  308. }
  309. return submode;
  310. }
  311. /**
  312. * Encode parts of the message using Byte Compaction as described in ISO/IEC 15438:2001(E),
  313. * chapter 4.4.3. The Unicode characters will be converted to binary using the cp437
  314. * codepage.
  315. *
  316. * @param bytes the message converted to a byte array
  317. * @param startpos the start position within the message
  318. * @param count the number of bytes to encode
  319. * @param startmode the mode from which this method starts
  320. * @param sb receives the encoded codewords
  321. */
  322. + (void)encodeBinary:(ZXByteArray *)bytes startpos:(int)startpos count:(int)count startmode:(int)startmode buffer:(NSMutableString *)sb {
  323. if (count == 1 && startmode == ZX_PDF417_TEXT_COMPACTION) {
  324. [sb appendFormat:@"%C", (unichar) ZX_PDF417_SHIFT_TO_BYTE];
  325. } else {
  326. BOOL sixpack = ((count % 6) == 0);
  327. if (sixpack) {
  328. [sb appendFormat:@"%C", (unichar) ZX_PDF417_LATCH_TO_BYTE];
  329. } else {
  330. [sb appendFormat:@"%C", (unichar) ZX_PDF417_LATCH_TO_BYTE_PADDED];
  331. }
  332. }
  333. int idx = startpos;
  334. // Encode sixpacks
  335. if (count >= 6) {
  336. const int charsLen = 5;
  337. unichar chars[charsLen];
  338. memset(chars, 0, charsLen * sizeof(unichar));
  339. while ((startpos + count - idx) >= 6) {
  340. long long t = 0;
  341. for (int i = 0; i < 6; i++) {
  342. t <<= 8;
  343. t += bytes.array[idx + i] & 0xff;
  344. }
  345. for (int i = 0; i < 5; i++) {
  346. chars[i] = (unichar) (t % 900);
  347. t /= 900;
  348. }
  349. for (int i = charsLen - 1; i >= 0; i--) {
  350. [sb appendFormat:@"%C", chars[i]];
  351. }
  352. idx += 6;
  353. }
  354. }
  355. //Encode rest (remaining n<5 bytes if any)
  356. for (int i = idx; i < startpos + count; i++) {
  357. int ch = bytes.array[i] & 0xff;
  358. [sb appendFormat:@"%C", (unichar)ch];
  359. }
  360. }
  361. + (void)encodeNumeric:(NSString *)msg startpos:(int)startpos count:(int)count buffer:(NSMutableString *)sb {
  362. int idx = 0;
  363. NSMutableString *tmp = [NSMutableString stringWithCapacity:count / 3 + 1];
  364. NSDecimalNumber *num900 = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:900] decimalValue]];
  365. NSDecimalNumber *num0 = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:0] decimalValue]];
  366. while (idx < count) {
  367. [tmp setString:@""];
  368. int len = MIN(44, count - idx);
  369. NSString *part = [@"1" stringByAppendingString:[msg substringWithRange:NSMakeRange(startpos + idx, len)]];
  370. NSDecimalNumber *bigint = [NSDecimalNumber decimalNumberWithString:part];
  371. do {
  372. NSRoundingMode roundingMode = ((bigint.floatValue < 0) ^ (num900.floatValue < 0)) ? NSRoundUp : NSRoundDown;
  373. NSDecimalNumber *quotient = [bigint decimalNumberByDividingBy:num900
  374. withBehavior:[NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:roundingMode
  375. scale:0
  376. raiseOnExactness:NO
  377. raiseOnOverflow:NO
  378. raiseOnUnderflow:NO
  379. raiseOnDivideByZero:NO]];
  380. NSDecimalNumber *subtractAmount = [quotient decimalNumberByMultiplyingBy:num900];
  381. NSDecimalNumber *remainder = [bigint decimalNumberBySubtracting:subtractAmount];
  382. [tmp appendFormat:@"%C", (unichar)[remainder longValue]];
  383. bigint = quotient;
  384. } while (![bigint isEqualToNumber:num0]);
  385. //Reverse temporary string
  386. for (int i = (int)tmp.length - 1; i >= 0; i--) {
  387. [sb appendFormat:@"%C", [tmp characterAtIndex:i]];
  388. }
  389. idx += len;
  390. }
  391. }
  392. + (BOOL)isDigit:(unichar)ch {
  393. return ch >= '0' && ch <= '9';
  394. }
  395. + (BOOL)isAlphaUpper:(unichar)ch {
  396. return ch == ' ' || (ch >= 'A' && ch <= 'Z');
  397. }
  398. + (BOOL)isAlphaLower:(unichar)ch {
  399. return ch == ' ' || (ch >= 'a' && ch <= 'z');
  400. }
  401. + (BOOL)isMixed:(unichar)ch {
  402. return ZX_PDF417_MIXED_TABLE[ch] != 0xFF;
  403. }
  404. + (BOOL)isPunctuation:(unichar)ch {
  405. return ZX_PDF417_PUNCTUATION[ch] != 0xFF;
  406. }
  407. + (BOOL)isText:(unichar)ch {
  408. return ch == '\t' || ch == '\n' || ch == '\r' || (ch >= 32 && ch <= 126);
  409. }
  410. /**
  411. * Determines the number of consecutive characters that are encodable using numeric compaction.
  412. *
  413. * @param msg the message
  414. * @param startpos the start position within the message
  415. * @return the requested character count
  416. */
  417. + (int)determineConsecutiveDigitCount:(NSString *)msg startpos:(int)startpos {
  418. int count = 0;
  419. NSUInteger len = msg.length;
  420. int idx = startpos;
  421. if (idx < len) {
  422. char ch = [msg characterAtIndex:idx];
  423. while ([self isDigit:ch] && idx < len) {
  424. count++;
  425. idx++;
  426. if (idx < len) {
  427. ch = [msg characterAtIndex:idx];
  428. }
  429. }
  430. }
  431. return count;
  432. }
  433. /**
  434. * Determines the number of consecutive characters that are encodable using text compaction.
  435. *
  436. * @param msg the message
  437. * @param startpos the start position within the message
  438. * @return the requested character count
  439. */
  440. + (int)determineConsecutiveTextCount:(NSString *)msg startpos:(int)startpos {
  441. NSUInteger len = msg.length;
  442. int idx = startpos;
  443. while (idx < len) {
  444. char ch = [msg characterAtIndex:idx];
  445. int numericCount = 0;
  446. while (numericCount < 13 && [self isDigit:ch] && idx < len) {
  447. numericCount++;
  448. idx++;
  449. if (idx < len) {
  450. ch = [msg characterAtIndex:idx];
  451. }
  452. }
  453. if (numericCount >= 13) {
  454. return idx - startpos - numericCount;
  455. }
  456. if (numericCount > 0) {
  457. //Heuristic: All text-encodable chars or digits are binary encodable
  458. continue;
  459. }
  460. ch = [msg characterAtIndex:idx];
  461. //Check if character is encodable
  462. if (![self isText:ch]) {
  463. break;
  464. }
  465. idx++;
  466. }
  467. return idx - startpos;
  468. }
  469. /**
  470. * Determines the number of consecutive characters that are encodable using binary compaction.
  471. *
  472. * @param msg the message
  473. * @param bytes the message converted to a byte array
  474. * @param startpos the start position within the message
  475. * @return the requested character count
  476. */
  477. + (int)determineConsecutiveBinaryCount:(NSString *)msg bytes:(ZXByteArray *)bytes startpos:(int)startpos error:(NSError **)error {
  478. NSUInteger len = msg.length;
  479. int idx = startpos;
  480. while (idx < len) {
  481. char ch = [msg characterAtIndex:idx];
  482. int numericCount = 0;
  483. while (numericCount < 13 && [self isDigit:ch]) {
  484. numericCount++;
  485. //textCount++;
  486. int i = idx + numericCount;
  487. if (i >= len) {
  488. break;
  489. }
  490. ch = [msg characterAtIndex:i];
  491. }
  492. if (numericCount >= 13) {
  493. return idx - startpos;
  494. }
  495. ch = [msg characterAtIndex:idx];
  496. //Check if character is encodable
  497. //Sun returns a ASCII 63 (?) for a character that cannot be mapped. Let's hope all
  498. //other VMs do the same
  499. if (bytes.array[idx] == 63 && ch != '?') {
  500. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Non-encodable character detected: %c (Unicode: %C)", ch, (unichar)ch]};
  501. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXWriterError userInfo:userInfo];
  502. return -1;
  503. }
  504. idx++;
  505. }
  506. return idx - startpos;
  507. }
  508. + (BOOL)encodingECI:(int)eci sb:(NSMutableString *)sb error:(NSError **)error {
  509. if (eci >= 0 && eci < 900) {
  510. [sb appendFormat:@"%C", (unichar) ZX_PDF417_HIGH_LEVEL_ECI_CHARSET];
  511. [sb appendFormat:@"%C", (unichar) eci];
  512. } else if (eci < 810900) {
  513. [sb appendFormat:@"%C", (unichar) ZX_PDF417_HIGH_LEVEL_ECI_GENERAL_PURPOSE];
  514. [sb appendFormat:@"%C", (unichar) (eci / 900 - 1)];
  515. [sb appendFormat:@"%C", (unichar) (eci % 900)];
  516. } else if (eci < 811800) {
  517. [sb appendFormat:@"%C", (unichar) ZX_PDF417_HIGH_LEVEL_ECI_USER_DEFINED];
  518. [sb appendFormat:@"%C", (unichar) (810900 - eci)];
  519. } else {
  520. NSDictionary *userInfo = @{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"ECI number not in valid range from 0..811799, but was %d", eci]};
  521. if (error) *error = [[NSError alloc] initWithDomain:ZXErrorDomain code:ZXWriterError userInfo:userInfo];
  522. return NO;
  523. }
  524. return YES;
  525. }
  526. + (ZXByteArray *)bytesForMessage:(NSString *)msg encoding:(NSStringEncoding)encoding {
  527. NSData *data = [msg dataUsingEncoding:encoding];
  528. int8_t *bytes = (int8_t *)[data bytes];
  529. ZXByteArray *byteArray = [[ZXByteArray alloc] initWithLength:(unsigned int)[data length]];
  530. memcpy(byteArray.array, bytes, [data length] * sizeof(int8_t));
  531. return byteArray;
  532. }
  533. @end