qrinput.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Input data chunk class
  5. * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #if HAVE_CONFIG_H
  22. # include "config.h"
  23. #endif
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include "qrencode.h"
  29. #include "qrspec.h"
  30. #include "mqrspec.h"
  31. #include "bitstream.h"
  32. #include "qrinput.h"
  33. /******************************************************************************
  34. * Utilities
  35. *****************************************************************************/
  36. int QRinput_isSplittableMode(QRencodeMode mode)
  37. {
  38. return (mode >= QR_MODE_NUM && mode <= QR_MODE_KANJI);
  39. }
  40. /******************************************************************************
  41. * Entry of input data
  42. *****************************************************************************/
  43. static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
  44. {
  45. QRinput_List *entry;
  46. if(QRinput_check(mode, size, data)) {
  47. errno = EINVAL;
  48. return NULL;
  49. }
  50. entry = (QRinput_List *)malloc(sizeof(QRinput_List));
  51. if(entry == NULL) return NULL;
  52. entry->mode = mode;
  53. entry->size = size;
  54. entry->data = NULL;
  55. if(size > 0) {
  56. entry->data = (unsigned char *)malloc((size_t)size);
  57. if(entry->data == NULL) {
  58. free(entry);
  59. return NULL;
  60. }
  61. memcpy(entry->data, data, (size_t)size);
  62. }
  63. entry->bstream = NULL;
  64. entry->next = NULL;
  65. return entry;
  66. }
  67. static void QRinput_List_freeEntry(QRinput_List *entry)
  68. {
  69. if(entry != NULL) {
  70. free(entry->data);
  71. BitStream_free(entry->bstream);
  72. free(entry);
  73. }
  74. }
  75. static QRinput_List *QRinput_List_dup(QRinput_List *entry)
  76. {
  77. QRinput_List *n;
  78. n = (QRinput_List *)malloc(sizeof(QRinput_List));
  79. if(n == NULL) return NULL;
  80. n->mode = entry->mode;
  81. n->size = entry->size;
  82. n->data = (unsigned char *)malloc((size_t)n->size);
  83. if(n->data == NULL) {
  84. free(n);
  85. return NULL;
  86. }
  87. memcpy(n->data, entry->data, (size_t)entry->size);
  88. n->bstream = NULL;
  89. n->next = NULL;
  90. return n;
  91. }
  92. /******************************************************************************
  93. * Input Data
  94. *****************************************************************************/
  95. QRinput *QRinput_new(void)
  96. {
  97. return QRinput_new2(0, QR_ECLEVEL_L);
  98. }
  99. QRinput *QRinput_new2(int version, QRecLevel level)
  100. {
  101. QRinput *input;
  102. if(version < 0 || version > QRSPEC_VERSION_MAX || level > QR_ECLEVEL_H) {
  103. errno = EINVAL;
  104. return NULL;
  105. }
  106. input = (QRinput *)malloc(sizeof(QRinput));
  107. if(input == NULL) return NULL;
  108. input->head = NULL;
  109. input->tail = NULL;
  110. input->version = version;
  111. input->level = level;
  112. input->mqr = 0;
  113. input->fnc1 = 0;
  114. return input;
  115. }
  116. QRinput *QRinput_newMQR(int version, QRecLevel level)
  117. {
  118. QRinput *input;
  119. if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
  120. if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
  121. input = QRinput_new2(version, level);
  122. if(input == NULL) return NULL;
  123. input->mqr = 1;
  124. return input;
  125. INVALID:
  126. errno = EINVAL;
  127. return NULL;
  128. }
  129. int QRinput_getVersion(QRinput *input)
  130. {
  131. return input->version;
  132. }
  133. int QRinput_setVersion(QRinput *input, int version)
  134. {
  135. if(input->mqr || version < 0 || version > QRSPEC_VERSION_MAX) {
  136. errno = EINVAL;
  137. return -1;
  138. }
  139. input->version = version;
  140. return 0;
  141. }
  142. QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
  143. {
  144. return input->level;
  145. }
  146. int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
  147. {
  148. if(input->mqr || level > QR_ECLEVEL_H) {
  149. errno = EINVAL;
  150. return -1;
  151. }
  152. input->level = level;
  153. return 0;
  154. }
  155. int QRinput_setVersionAndErrorCorrectionLevel(QRinput *input, int version, QRecLevel level)
  156. {
  157. if(input->mqr) {
  158. if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
  159. if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
  160. } else {
  161. if(version < 0 || version > QRSPEC_VERSION_MAX) goto INVALID;
  162. if(level > QR_ECLEVEL_H) goto INVALID;
  163. }
  164. input->version = version;
  165. input->level = level;
  166. return 0;
  167. INVALID:
  168. errno = EINVAL;
  169. return -1;
  170. }
  171. static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
  172. {
  173. if(input->tail == NULL) {
  174. input->head = entry;
  175. input->tail = entry;
  176. } else {
  177. input->tail->next = entry;
  178. input->tail = entry;
  179. }
  180. entry->next = NULL;
  181. }
  182. int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
  183. {
  184. QRinput_List *entry;
  185. entry = QRinput_List_newEntry(mode, size, data);
  186. if(entry == NULL) {
  187. return -1;
  188. }
  189. QRinput_appendEntry(input, entry);
  190. return 0;
  191. }
  192. /**
  193. * Insert a structured-append header to the head of the input data.
  194. * @param input input data.
  195. * @param size number of structured symbols.
  196. * @param number index number of the symbol. (1 <= number <= size)
  197. * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
  198. * @retval 0 success.
  199. * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
  200. * @throw EINVAL invalid parameter.
  201. * @throw ENOMEM unable to allocate memory.
  202. */
  203. STATIC_IN_RELEASE int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int number, unsigned char parity)
  204. {
  205. QRinput_List *entry;
  206. unsigned char buf[3];
  207. if(size > MAX_STRUCTURED_SYMBOLS) {
  208. errno = EINVAL;
  209. return -1;
  210. }
  211. if(number <= 0 || number > size) {
  212. errno = EINVAL;
  213. return -1;
  214. }
  215. buf[0] = (unsigned char)size;
  216. buf[1] = (unsigned char)number;
  217. buf[2] = parity;
  218. entry = QRinput_List_newEntry(QR_MODE_STRUCTURE, 3, buf);
  219. if(entry == NULL) {
  220. return -1;
  221. }
  222. entry->next = input->head;
  223. input->head = entry;
  224. return 0;
  225. }
  226. int QRinput_appendECIheader(QRinput *input, unsigned int ecinum)
  227. {
  228. unsigned char data[4];
  229. if(ecinum > 999999) {
  230. errno = EINVAL;
  231. return -1;
  232. }
  233. /* We manually create byte array of ecinum because
  234. (unsigned char *)&ecinum may cause bus error on some architectures, */
  235. data[0] = ecinum & 0xff;
  236. data[1] = (ecinum >> 8) & 0xff;
  237. data[2] = (ecinum >> 16) & 0xff;
  238. data[3] = (ecinum >> 24) & 0xff;
  239. return QRinput_append(input, QR_MODE_ECI, 4, data);
  240. }
  241. void QRinput_free(QRinput *input)
  242. {
  243. QRinput_List *list, *next;
  244. if(input != NULL) {
  245. list = input->head;
  246. while(list != NULL) {
  247. next = list->next;
  248. QRinput_List_freeEntry(list);
  249. list = next;
  250. }
  251. free(input);
  252. }
  253. }
  254. static unsigned char QRinput_calcParity(QRinput *input)
  255. {
  256. unsigned char parity = 0;
  257. QRinput_List *list;
  258. int i;
  259. list = input->head;
  260. while(list != NULL) {
  261. if(list->mode != QR_MODE_STRUCTURE) {
  262. for(i = list->size-1; i >= 0; i--) {
  263. parity ^= list->data[i];
  264. }
  265. }
  266. list = list->next;
  267. }
  268. return parity;
  269. }
  270. QRinput *QRinput_dup(QRinput *input)
  271. {
  272. QRinput *n;
  273. QRinput_List *list, *e;
  274. if(input->mqr) {
  275. n = QRinput_newMQR(input->version, input->level);
  276. } else {
  277. n = QRinput_new2(input->version, input->level);
  278. }
  279. if(n == NULL) return NULL;
  280. list = input->head;
  281. while(list != NULL) {
  282. e = QRinput_List_dup(list);
  283. if(e == NULL) {
  284. QRinput_free(n);
  285. return NULL;
  286. }
  287. QRinput_appendEntry(n, e);
  288. list = list->next;
  289. }
  290. return n;
  291. }
  292. /******************************************************************************
  293. * Numeric data
  294. *****************************************************************************/
  295. /**
  296. * Check the input data.
  297. * @param size
  298. * @param data
  299. * @return result
  300. */
  301. static int QRinput_checkModeNum(int size, const char *data)
  302. {
  303. int i;
  304. for(i = 0; i < size; i++) {
  305. if(data[i] < '0' || data[i] > '9')
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * Estimate the length of the encoded bit stream of numeric data.
  312. * @param size
  313. * @return number of bits
  314. */
  315. int QRinput_estimateBitsModeNum(int size)
  316. {
  317. int w;
  318. int bits;
  319. w = size / 3;
  320. bits = w * 10;
  321. switch(size - w * 3) {
  322. case 1:
  323. bits += 4;
  324. break;
  325. case 2:
  326. bits += 7;
  327. break;
  328. default:
  329. break;
  330. }
  331. return bits;
  332. }
  333. /**
  334. * Convert the number data and append to a bit stream.
  335. * @param entry
  336. * @param mqr
  337. * @retval 0 success
  338. * @retval -1 an error occurred and errno is set to indeicate the error.
  339. * See Execptions for the details.
  340. * @throw ENOMEM unable to allocate memory.
  341. */
  342. static int QRinput_encodeModeNum(QRinput_List *entry, BitStream *bstream, int version, int mqr)
  343. {
  344. int words, i, ret;
  345. unsigned int val;
  346. if(mqr) {
  347. if(version > 1) {
  348. ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_NUM);
  349. if(ret < 0) return -1;
  350. }
  351. ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_NUM, version), (unsigned int)entry->size);
  352. if(ret < 0) return -1;
  353. } else {
  354. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_NUM);
  355. if(ret < 0) return -1;
  356. ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_NUM, version), (unsigned int)entry->size);
  357. if(ret < 0) return -1;
  358. }
  359. words = entry->size / 3;
  360. for(i = 0; i < words; i++) {
  361. val = (unsigned int)(entry->data[i*3 ] - '0') * 100;
  362. val += (unsigned int)(entry->data[i*3+1] - '0') * 10;
  363. val += (unsigned int)(entry->data[i*3+2] - '0');
  364. ret = BitStream_appendNum(bstream, 10, val);
  365. if(ret < 0) return -1;
  366. }
  367. if(entry->size - words * 3 == 1) {
  368. val = (unsigned int)(entry->data[words*3] - '0');
  369. ret = BitStream_appendNum(bstream, 4, val);
  370. if(ret < 0) return -1;
  371. } else if(entry->size - words * 3 == 2) {
  372. val = (unsigned int)(entry->data[words*3 ] - '0') * 10;
  373. val += (unsigned int)(entry->data[words*3+1] - '0');
  374. BitStream_appendNum(bstream, 7, val);
  375. if(ret < 0) return -1;
  376. }
  377. return 0;
  378. }
  379. /******************************************************************************
  380. * Alphabet-numeric data
  381. *****************************************************************************/
  382. const signed char QRinput_anTable[128] = {
  383. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  384. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  385. 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
  386. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
  387. -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  388. 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
  389. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  390. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  391. };
  392. /**
  393. * Check the input data.
  394. * @param size
  395. * @param data
  396. * @return result
  397. */
  398. static int QRinput_checkModeAn(int size, const char *data)
  399. {
  400. int i;
  401. for(i = 0; i < size; i++) {
  402. if(QRinput_lookAnTable(data[i]) < 0)
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. /**
  408. * Estimate the length of the encoded bit stream of alphabet-numeric data.
  409. * @param size
  410. * @return number of bits
  411. */
  412. int QRinput_estimateBitsModeAn(int size)
  413. {
  414. int w;
  415. int bits;
  416. w = size / 2;
  417. bits = w * 11;
  418. if(size & 1) {
  419. bits += 6;
  420. }
  421. return bits;
  422. }
  423. /**
  424. * Convert the alphabet-numeric data and append to a bit stream.
  425. * @param entry
  426. * @param mqr
  427. * @retval 0 success
  428. * @retval -1 an error occurred and errno is set to indeicate the error.
  429. * See Execptions for the details.
  430. * @throw ENOMEM unable to allocate memory.
  431. * @throw EINVAL invalid version.
  432. */
  433. static int QRinput_encodeModeAn(QRinput_List *entry, BitStream *bstream, int version, int mqr)
  434. {
  435. int words, i, ret;
  436. unsigned int val;
  437. if(mqr) {
  438. if(version < 2) {
  439. errno = EINVAL;
  440. return -1;
  441. }
  442. ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_AN);
  443. if(ret < 0) return -1;
  444. ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_AN, version), (unsigned int)entry->size);
  445. if(ret < 0) return -1;
  446. } else {
  447. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_AN);
  448. if(ret < 0) return -1;
  449. ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_AN, version), (unsigned int)entry->size);
  450. if(ret < 0) return -1;
  451. }
  452. words = entry->size / 2;
  453. for(i = 0; i < words; i++) {
  454. val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
  455. val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
  456. ret = BitStream_appendNum(bstream, 11, val);
  457. if(ret < 0) return -1;
  458. }
  459. if(entry->size & 1) {
  460. val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
  461. ret = BitStream_appendNum(bstream, 6, val);
  462. if(ret < 0) return -1;
  463. }
  464. return 0;
  465. }
  466. /******************************************************************************
  467. * 8 bit data
  468. *****************************************************************************/
  469. /**
  470. * Estimate the length of the encoded bit stream of 8 bit data.
  471. * @param size
  472. * @return number of bits
  473. */
  474. int QRinput_estimateBitsMode8(int size)
  475. {
  476. return size * 8;
  477. }
  478. /**
  479. * Convert the 8bits data and append to a bit stream.
  480. * @param entry
  481. * @param mqr
  482. * @retval 0 success
  483. * @retval -1 an error occurred and errno is set to indeicate the error.
  484. * See Execptions for the details.
  485. * @throw ENOMEM unable to allocate memory.
  486. */
  487. static int QRinput_encodeMode8(QRinput_List *entry, BitStream *bstream, int version, int mqr)
  488. {
  489. int ret;
  490. if(mqr) {
  491. if(version < 3) {
  492. errno = EINVAL;
  493. return -1;
  494. }
  495. ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_8);
  496. if(ret < 0) return -1;
  497. ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_8, version), (unsigned int)entry->size);
  498. if(ret < 0) return -1;
  499. } else {
  500. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_8);
  501. if(ret < 0) return -1;
  502. ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_8, version), (unsigned int)entry->size);
  503. if(ret < 0) return -1;
  504. }
  505. ret = BitStream_appendBytes(bstream, (size_t)entry->size, entry->data);
  506. if(ret < 0) return -1;
  507. return 0;
  508. }
  509. /******************************************************************************
  510. * Kanji data
  511. *****************************************************************************/
  512. /**
  513. * Estimate the length of the encoded bit stream of kanji data.
  514. * @param size
  515. * @return number of bits
  516. */
  517. int QRinput_estimateBitsModeKanji(int size)
  518. {
  519. return (size / 2) * 13;
  520. }
  521. /**
  522. * Check the input data.
  523. * @param size
  524. * @param data
  525. * @return result
  526. */
  527. static int QRinput_checkModeKanji(int size, const unsigned char *data)
  528. {
  529. int i;
  530. unsigned int val;
  531. if(size & 1)
  532. return -1;
  533. for(i = 0; i < size; i+=2) {
  534. val = ((unsigned int)data[i] << 8) | data[i+1];
  535. if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
  536. return -1;
  537. }
  538. }
  539. return 0;
  540. }
  541. /**
  542. * Convert the kanji data and append to a bit stream.
  543. * @param entry
  544. * @param mqr
  545. * @retval 0 success
  546. * @retval -1 an error occurred and errno is set to indeicate the error.
  547. * See Execptions for the details.
  548. * @throw ENOMEM unable to allocate memory.
  549. * @throw EINVAL invalid version.
  550. */
  551. static int QRinput_encodeModeKanji(QRinput_List *entry, BitStream *bstream, int version, int mqr)
  552. {
  553. int ret, i;
  554. unsigned int val, h;
  555. if(mqr) {
  556. if(version < 2) {
  557. errno = EINVAL;
  558. return -1;
  559. }
  560. ret = BitStream_appendNum(bstream, (size_t)(version - 1), MQRSPEC_MODEID_KANJI);
  561. if(ret < 0) return -1;
  562. ret = BitStream_appendNum(bstream, (size_t)MQRspec_lengthIndicator(QR_MODE_KANJI, version), (unsigned int)entry->size/2);
  563. if(ret < 0) return -1;
  564. } else {
  565. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_KANJI);
  566. if(ret < 0) return -1;
  567. ret = BitStream_appendNum(bstream, (size_t)QRspec_lengthIndicator(QR_MODE_KANJI, version), (unsigned int)entry->size/2);
  568. if(ret < 0) return -1;
  569. }
  570. for(i = 0; i < entry->size; i+=2) {
  571. val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
  572. if(val <= 0x9ffc) {
  573. val -= 0x8140;
  574. } else {
  575. val -= 0xc140;
  576. }
  577. h = (val >> 8) * 0xc0;
  578. val = (val & 0xff) + h;
  579. ret = BitStream_appendNum(bstream, 13, val);
  580. if(ret < 0) return -1;
  581. }
  582. return 0;
  583. }
  584. /******************************************************************************
  585. * Structured Symbol
  586. *****************************************************************************/
  587. /**
  588. * Convert a structure symbol code and append to a bit stream.
  589. * @param entry
  590. * @param mqr
  591. * @retval 0 success
  592. * @retval -1 an error occurred and errno is set to indeicate the error.
  593. * See Execptions for the details.
  594. * @throw ENOMEM unable to allocate memory.
  595. * @throw EINVAL invalid entry.
  596. */
  597. static int QRinput_encodeModeStructure(QRinput_List *entry, BitStream *bstream, int mqr)
  598. {
  599. int ret;
  600. if(mqr) {
  601. errno = EINVAL;
  602. return -1;
  603. }
  604. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_STRUCTURE);
  605. if(ret < 0) return -1;
  606. ret = BitStream_appendNum(bstream, 4, entry->data[1] - 1U);
  607. if(ret < 0) return -1;
  608. ret = BitStream_appendNum(bstream, 4, entry->data[0] - 1U);
  609. if(ret < 0) return -1;
  610. ret = BitStream_appendNum(bstream, 8, entry->data[2]);
  611. if(ret < 0) return -1;
  612. return 0;
  613. }
  614. /******************************************************************************
  615. * FNC1
  616. *****************************************************************************/
  617. static int QRinput_checkModeFNC1Second(int size)
  618. {
  619. if(size != 1) return -1;
  620. /* No data check required. */
  621. return 0;
  622. }
  623. static int QRinput_encodeModeFNC1Second(QRinput_List *entry, BitStream *bstream)
  624. {
  625. int ret;
  626. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_FNC1SECOND);
  627. if(ret < 0) return -1;
  628. ret = BitStream_appendBytes(bstream, 1, entry->data);
  629. if(ret < 0) return -1;
  630. return 0;
  631. }
  632. /******************************************************************************
  633. * ECI header
  634. *****************************************************************************/
  635. static unsigned int QRinput_decodeECIfromByteArray(unsigned char *data)
  636. {
  637. int i;
  638. unsigned int ecinum;
  639. ecinum = 0;
  640. for(i = 0; i < 4; i++) {
  641. ecinum = ecinum << 8;
  642. ecinum |= data[3-i];
  643. }
  644. return ecinum;
  645. }
  646. static int QRinput_estimateBitsModeECI(unsigned char *data)
  647. {
  648. unsigned int ecinum;
  649. ecinum = QRinput_decodeECIfromByteArray(data);
  650. /* See Table 4 of JISX 0510:2004 pp.17. */
  651. if(ecinum < 128) {
  652. return MODE_INDICATOR_SIZE + 8;
  653. } else if(ecinum < 16384) {
  654. return MODE_INDICATOR_SIZE + 16;
  655. } else {
  656. return MODE_INDICATOR_SIZE + 24;
  657. }
  658. }
  659. static int QRinput_encodeModeECI(QRinput_List *entry, BitStream *bstream)
  660. {
  661. int ret, words;
  662. unsigned int ecinum, code;
  663. ecinum = QRinput_decodeECIfromByteArray(entry->data);
  664. /* See Table 4 of JISX 0510:2004 pp.17. */
  665. if(ecinum < 128) {
  666. words = 1;
  667. code = ecinum;
  668. } else if(ecinum < 16384) {
  669. words = 2;
  670. code = 0x8000 + ecinum;
  671. } else {
  672. words = 3;
  673. code = 0xc0000 + ecinum;
  674. }
  675. ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_ECI);
  676. if(ret < 0) return -1;
  677. ret = BitStream_appendNum(bstream, (size_t)words * 8, code);
  678. if(ret < 0) return -1;
  679. return 0;
  680. }
  681. /******************************************************************************
  682. * Validation
  683. *****************************************************************************/
  684. int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
  685. {
  686. if((mode == QR_MODE_FNC1FIRST && size < 0) || size <= 0) return -1;
  687. switch(mode) {
  688. case QR_MODE_NUM:
  689. return QRinput_checkModeNum(size, (const char *)data);
  690. case QR_MODE_AN:
  691. return QRinput_checkModeAn(size, (const char *)data);
  692. case QR_MODE_KANJI:
  693. return QRinput_checkModeKanji(size, data);
  694. case QR_MODE_8:
  695. return 0;
  696. case QR_MODE_STRUCTURE:
  697. return 0;
  698. case QR_MODE_ECI:
  699. return 0;
  700. case QR_MODE_FNC1FIRST:
  701. return 0;
  702. case QR_MODE_FNC1SECOND:
  703. return QRinput_checkModeFNC1Second(size);
  704. case QR_MODE_NUL:
  705. break;
  706. }
  707. return -1;
  708. }
  709. /******************************************************************************
  710. * Estimation of the bit length
  711. *****************************************************************************/
  712. /**
  713. * Estimate the length of the encoded bit stream on the current version.
  714. * @param entry
  715. * @param version version of the symbol
  716. * @param mqr
  717. * @return number of bits
  718. */
  719. static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version, int mqr)
  720. {
  721. int bits = 0;
  722. int l, m;
  723. int num;
  724. if(version == 0) version = 1;
  725. switch(entry->mode) {
  726. case QR_MODE_NUM:
  727. bits = QRinput_estimateBitsModeNum(entry->size);
  728. break;
  729. case QR_MODE_AN:
  730. bits = QRinput_estimateBitsModeAn(entry->size);
  731. break;
  732. case QR_MODE_8:
  733. bits = QRinput_estimateBitsMode8(entry->size);
  734. break;
  735. case QR_MODE_KANJI:
  736. bits = QRinput_estimateBitsModeKanji(entry->size);
  737. break;
  738. case QR_MODE_STRUCTURE:
  739. return STRUCTURE_HEADER_SIZE;
  740. case QR_MODE_ECI:
  741. bits = QRinput_estimateBitsModeECI(entry->data);
  742. break;
  743. case QR_MODE_FNC1FIRST:
  744. return MODE_INDICATOR_SIZE;
  745. case QR_MODE_FNC1SECOND:
  746. return MODE_INDICATOR_SIZE + 8;
  747. default:
  748. return 0;
  749. }
  750. if(mqr) {
  751. l = QRspec_lengthIndicator(entry->mode, version);
  752. m = version - 1;
  753. bits += l + m;
  754. } else {
  755. l = QRspec_lengthIndicator(entry->mode, version);
  756. m = 1 << l;
  757. num = (entry->size + m - 1) / m;
  758. bits += num * (MODE_INDICATOR_SIZE + l);
  759. }
  760. return bits;
  761. }
  762. /**
  763. * Estimate the length of the encoded bit stream of the data.
  764. * @param input input data
  765. * @param version version of the symbol
  766. * @return number of bits
  767. */
  768. STATIC_IN_RELEASE int QRinput_estimateBitStreamSize(QRinput *input, int version)
  769. {
  770. QRinput_List *list;
  771. int bits = 0;
  772. list = input->head;
  773. while(list != NULL) {
  774. bits += QRinput_estimateBitStreamSizeOfEntry(list, version, input->mqr);
  775. list = list->next;
  776. }
  777. return bits;
  778. }
  779. /**
  780. * Estimate the required version number of the symbol.
  781. * @param input input data
  782. * @return required version number
  783. */
  784. static int QRinput_estimateVersion(QRinput *input)
  785. {
  786. int bits;
  787. int version, prev;
  788. version = 0;
  789. do {
  790. prev = version;
  791. bits = QRinput_estimateBitStreamSize(input, prev);
  792. version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
  793. } while (version > prev);
  794. return version;
  795. }
  796. /**
  797. * Return required length in bytes for specified mode, version and bits.
  798. * @param mode
  799. * @param version
  800. * @param bits
  801. * @return required length of code words in bytes.
  802. */
  803. STATIC_IN_RELEASE int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
  804. {
  805. int payload, size, chunks, remain, maxsize;
  806. payload = bits - 4 - QRspec_lengthIndicator(mode, version);
  807. switch(mode) {
  808. case QR_MODE_NUM:
  809. chunks = payload / 10;
  810. remain = payload - chunks * 10;
  811. size = chunks * 3;
  812. if(remain >= 7) {
  813. size += 2;
  814. } else if(remain >= 4) {
  815. size += 1;
  816. }
  817. break;
  818. case QR_MODE_AN:
  819. chunks = payload / 11;
  820. remain = payload - chunks * 11;
  821. size = chunks * 2;
  822. if(remain >= 6) size++;
  823. break;
  824. case QR_MODE_8:
  825. size = payload / 8;
  826. break;
  827. case QR_MODE_KANJI:
  828. size = (payload / 13) * 2;
  829. break;
  830. case QR_MODE_STRUCTURE:
  831. size = payload / 8;
  832. break;
  833. default:
  834. size = 0;
  835. break;
  836. }
  837. maxsize = QRspec_maximumWords(mode, version);
  838. if(size < 0) size = 0;
  839. if(maxsize > 0 && size > maxsize) size = maxsize;
  840. return size;
  841. }
  842. /******************************************************************************
  843. * Data conversion
  844. *****************************************************************************/
  845. /**
  846. * Convert the input data in the data chunk and append to a bit stream.
  847. * @param entry
  848. * @param bstream
  849. * @return number of bits (>0) or -1 for failure.
  850. */
  851. static int QRinput_encodeBitStream(QRinput_List *entry, BitStream *bstream, int version, int mqr)
  852. {
  853. int words, ret;
  854. QRinput_List *st1 = NULL, *st2 = NULL;
  855. int prevsize;
  856. prevsize = (int)BitStream_size(bstream);
  857. words = QRspec_maximumWords(entry->mode, version);
  858. if(words != 0 && entry->size > words) {
  859. st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
  860. if(st1 == NULL) goto ABORT;
  861. st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
  862. if(st2 == NULL) goto ABORT;
  863. ret = QRinput_encodeBitStream(st1, bstream, version, mqr);
  864. if(ret < 0) goto ABORT;
  865. ret = QRinput_encodeBitStream(st2, bstream, version, mqr);
  866. if(ret < 0) goto ABORT;
  867. QRinput_List_freeEntry(st1);
  868. QRinput_List_freeEntry(st2);
  869. } else {
  870. ret = 0;
  871. switch(entry->mode) {
  872. case QR_MODE_NUM:
  873. ret = QRinput_encodeModeNum(entry, bstream, version, mqr);
  874. break;
  875. case QR_MODE_AN:
  876. ret = QRinput_encodeModeAn(entry, bstream, version, mqr);
  877. break;
  878. case QR_MODE_8:
  879. ret = QRinput_encodeMode8(entry, bstream, version, mqr);
  880. break;
  881. case QR_MODE_KANJI:
  882. ret = QRinput_encodeModeKanji(entry, bstream, version, mqr);
  883. break;
  884. case QR_MODE_STRUCTURE:
  885. ret = QRinput_encodeModeStructure(entry, bstream, mqr);
  886. break;
  887. case QR_MODE_ECI:
  888. ret = QRinput_encodeModeECI(entry, bstream);
  889. break;
  890. case QR_MODE_FNC1SECOND:
  891. ret = QRinput_encodeModeFNC1Second(entry, bstream);
  892. break;
  893. default:
  894. break;
  895. }
  896. if(ret < 0) return -1;
  897. }
  898. return (int)BitStream_size(bstream) - prevsize;
  899. ABORT:
  900. QRinput_List_freeEntry(st1);
  901. QRinput_List_freeEntry(st2);
  902. return -1;
  903. }
  904. /**
  905. * Convert the input data to a bit stream.
  906. * @param input input data.
  907. * @retval 0 success
  908. * @retval -1 an error occurred and errno is set to indeicate the error.
  909. * See Execptions for the details.
  910. * @throw ENOMEM unable to allocate memory.
  911. */
  912. static int QRinput_createBitStream(QRinput *input, BitStream *bstream)
  913. {
  914. QRinput_List *list;
  915. int bits, total = 0;
  916. list = input->head;
  917. while(list != NULL) {
  918. bits = QRinput_encodeBitStream(list, bstream, input->version, input->mqr);
  919. if(bits < 0) return -1;
  920. total += bits;
  921. list = list->next;
  922. }
  923. return total;
  924. }
  925. /**
  926. * Convert the input data to a bit stream.
  927. * When the version number is given and that is not sufficient, it is increased
  928. * automatically.
  929. * @param input input data.
  930. * @param bstream where the converted data is stored.
  931. * @retval 0 success
  932. * @retval -1 an error occurred and errno is set to indeicate the error.
  933. * See Execptions for the details.
  934. * @throw ENOMEM unable to allocate memory.
  935. * @throw ERANGE input data is too large.
  936. */
  937. static int QRinput_convertData(QRinput *input, BitStream *bstream)
  938. {
  939. int bits;
  940. int ver;
  941. ver = QRinput_estimateVersion(input);
  942. if(ver > QRinput_getVersion(input)) {
  943. QRinput_setVersion(input, ver);
  944. }
  945. for(;;) {
  946. BitStream_reset(bstream);
  947. bits = QRinput_createBitStream(input, bstream);
  948. if(bits < 0) return -1;
  949. ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
  950. if(ver > QRinput_getVersion(input)) {
  951. QRinput_setVersion(input, ver);
  952. } else {
  953. break;
  954. }
  955. }
  956. return 0;
  957. }
  958. /**
  959. * Append padding bits for the input data.
  960. * @param bstream Bitstream to be appended.
  961. * @param input input data.
  962. * @retval 0 success
  963. * @retval -1 an error occurred and errno is set to indeicate the error.
  964. * See Execptions for the details.
  965. * @throw ERANGE input data is too large.
  966. * @throw ENOMEM unable to allocate memory.
  967. */
  968. static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
  969. {
  970. int bits, maxbits, words, maxwords, i, ret;
  971. int padlen;
  972. bits = (int)BitStream_size(bstream);
  973. maxwords = QRspec_getDataLength(input->version, input->level);
  974. maxbits = maxwords * 8;
  975. if(maxbits < bits) {
  976. errno = ERANGE;
  977. return -1;
  978. }
  979. if(maxbits == bits) {
  980. return 0;
  981. }
  982. if(maxbits - bits <= 4) {
  983. return (int)BitStream_appendNum(bstream, (size_t)(maxbits - bits), 0);
  984. }
  985. words = (bits + 4 + 7) / 8;
  986. ret = (int)BitStream_appendNum(bstream, (size_t)(words * 8 - bits), 0);
  987. if(ret < 0) return ret;
  988. padlen = maxwords - words;
  989. if(padlen > 0) {
  990. for(i = 0; i < padlen; i++) {
  991. ret = (int)BitStream_appendNum(bstream, 8, (i&1)?0x11:0xec);
  992. if(ret < 0) {
  993. return ret;
  994. }
  995. }
  996. }
  997. return 0;
  998. }
  999. /**
  1000. * Append padding bits for the input data - Micro QR Code version.
  1001. * @param bstream Bitstream to be appended.
  1002. * @param input input data.
  1003. * @retval 0 success
  1004. * @retval -1 an error occurred and errno is set to indeicate the error.
  1005. * See Execptions for the details.
  1006. * @throw ERANGE input data is too large.
  1007. * @throw ENOMEM unable to allocate memory.
  1008. */
  1009. static int QRinput_appendPaddingBitMQR(BitStream *bstream, QRinput *input)
  1010. {
  1011. int bits, maxbits, words, maxwords, i, ret, termbits;
  1012. int padlen;
  1013. bits = (int)BitStream_size(bstream);
  1014. maxbits = MQRspec_getDataLengthBit(input->version, input->level);
  1015. maxwords = maxbits / 8;
  1016. if(maxbits < bits) {
  1017. errno = ERANGE;
  1018. return -1;
  1019. }
  1020. if(maxbits == bits) {
  1021. return 0;
  1022. }
  1023. termbits = input->version * 2 + 1;
  1024. if(maxbits - bits <= termbits) {
  1025. return (int)BitStream_appendNum(bstream, (size_t)(maxbits - bits), 0);
  1026. }
  1027. bits += termbits;
  1028. words = (bits + 7) / 8;
  1029. if(maxbits - words * 8 > 0) {
  1030. termbits += words * 8 - bits;
  1031. if(words == maxwords) termbits += maxbits - words * 8;
  1032. } else {
  1033. termbits += words * 8 - bits;
  1034. }
  1035. ret = (int)BitStream_appendNum(bstream, (size_t)termbits, 0);
  1036. if(ret < 0) return ret;
  1037. padlen = maxwords - words;
  1038. if(padlen > 0) {
  1039. for(i = 0; i < padlen; i++) {
  1040. ret = (int)BitStream_appendNum(bstream, 8, (i&1)?0x11:0xec);
  1041. if(ret < 0) return ret;
  1042. }
  1043. termbits = maxbits - maxwords * 8;
  1044. if(termbits > 0) {
  1045. ret = (int)BitStream_appendNum(bstream, (size_t)termbits, 0);
  1046. if(ret < 0) return ret;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. static int QRinput_insertFNC1Header(QRinput *input)
  1052. {
  1053. QRinput_List *entry = NULL;
  1054. if(input->fnc1 == 1) {
  1055. entry = QRinput_List_newEntry(QR_MODE_FNC1FIRST, 0, NULL);
  1056. } else if(input->fnc1 == 2) {
  1057. entry = QRinput_List_newEntry(QR_MODE_FNC1SECOND, 1, &(input->appid));
  1058. }
  1059. if(entry == NULL) {
  1060. return -1;
  1061. }
  1062. if(input->head->mode != QR_MODE_STRUCTURE && input->head->mode != QR_MODE_ECI) {
  1063. entry->next = input->head;
  1064. input->head = entry;
  1065. } else {
  1066. entry->next = input->head->next;
  1067. input->head->next = entry;
  1068. }
  1069. return 0;
  1070. }
  1071. /**
  1072. * Merge all bit streams in the input data.
  1073. * @param input input data.
  1074. * @return merged bit stream
  1075. */
  1076. STATIC_IN_RELEASE int QRinput_mergeBitStream(QRinput *input, BitStream *bstream)
  1077. {
  1078. if(input->mqr) {
  1079. if(QRinput_createBitStream(input, bstream) < 0) {
  1080. return -1;
  1081. }
  1082. } else {
  1083. if(input->fnc1) {
  1084. if(QRinput_insertFNC1Header(input) < 0) {
  1085. return -1;
  1086. }
  1087. }
  1088. if(QRinput_convertData(input, bstream) < 0) {
  1089. return -1;
  1090. }
  1091. }
  1092. return 0;
  1093. }
  1094. /**
  1095. * Merge all bit streams in the input data and append padding bits
  1096. * @param input input data.
  1097. * @return padded merged bit stream
  1098. */
  1099. STATIC_IN_RELEASE int QRinput_getBitStream(QRinput *input, BitStream *bstream)
  1100. {
  1101. int ret;
  1102. ret = QRinput_mergeBitStream(input, bstream);
  1103. if(ret < 0) return -1;
  1104. if(input->mqr) {
  1105. ret = QRinput_appendPaddingBitMQR(bstream, input);
  1106. } else {
  1107. ret = QRinput_appendPaddingBit(bstream, input);
  1108. }
  1109. if(ret < 0) return -1;
  1110. return 0;
  1111. }
  1112. /**
  1113. * Pack all bit streams padding bits into a byte array.
  1114. * @param input input data.
  1115. * @return padded merged byte stream
  1116. */
  1117. unsigned char *QRinput_getByteStream(QRinput *input)
  1118. {
  1119. BitStream *bstream;
  1120. unsigned char *array;
  1121. int ret;
  1122. bstream = BitStream_new();
  1123. if(bstream == NULL) {
  1124. return NULL;
  1125. }
  1126. ret = QRinput_getBitStream(input, bstream);
  1127. if(ret < 0) {
  1128. BitStream_free(bstream);
  1129. return NULL;
  1130. }
  1131. array = BitStream_toByte(bstream);
  1132. BitStream_free(bstream);
  1133. return array;
  1134. }
  1135. /******************************************************************************
  1136. * Structured input data
  1137. *****************************************************************************/
  1138. static QRinput_InputList *QRinput_InputList_newEntry(QRinput *input)
  1139. {
  1140. QRinput_InputList *entry;
  1141. entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
  1142. if(entry == NULL) return NULL;
  1143. entry->input = input;
  1144. entry->next = NULL;
  1145. return entry;
  1146. }
  1147. static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
  1148. {
  1149. if(entry != NULL) {
  1150. QRinput_free(entry->input);
  1151. free(entry);
  1152. }
  1153. }
  1154. QRinput_Struct *QRinput_Struct_new(void)
  1155. {
  1156. QRinput_Struct *s;
  1157. s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
  1158. if(s == NULL) return NULL;
  1159. s->size = 0;
  1160. s->parity = -1;
  1161. s->head = NULL;
  1162. s->tail = NULL;
  1163. return s;
  1164. }
  1165. void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
  1166. {
  1167. s->parity = (int)parity;
  1168. }
  1169. int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
  1170. {
  1171. QRinput_InputList *e;
  1172. if(input->mqr) {
  1173. errno = EINVAL;
  1174. return -1;
  1175. }
  1176. e = QRinput_InputList_newEntry(input);
  1177. if(e == NULL) return -1;
  1178. s->size++;
  1179. if(s->tail == NULL) {
  1180. s->head = e;
  1181. s->tail = e;
  1182. } else {
  1183. s->tail->next = e;
  1184. s->tail = e;
  1185. }
  1186. return s->size;
  1187. }
  1188. void QRinput_Struct_free(QRinput_Struct *s)
  1189. {
  1190. QRinput_InputList *list, *next;
  1191. if(s != NULL) {
  1192. list = s->head;
  1193. while(list != NULL) {
  1194. next = list->next;
  1195. QRinput_InputList_freeEntry(list);
  1196. list = next;
  1197. }
  1198. free(s);
  1199. }
  1200. }
  1201. static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
  1202. {
  1203. QRinput_InputList *list;
  1204. unsigned char parity = 0;
  1205. list = s->head;
  1206. while(list != NULL) {
  1207. parity ^= QRinput_calcParity(list->input);
  1208. list = list->next;
  1209. }
  1210. QRinput_Struct_setParity(s, parity);
  1211. return parity;
  1212. }
  1213. static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
  1214. {
  1215. unsigned char *data;
  1216. data = (unsigned char *)malloc((size_t)bytes);
  1217. if(data == NULL) return -1;
  1218. memcpy(data, entry->data, (size_t)bytes);
  1219. free(entry->data);
  1220. entry->data = data;
  1221. entry->size = bytes;
  1222. return 0;
  1223. }
  1224. STATIC_IN_RELEASE int QRinput_splitEntry(QRinput_List *entry, int bytes)
  1225. {
  1226. QRinput_List *e;
  1227. int ret;
  1228. e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
  1229. if(e == NULL) {
  1230. return -1;
  1231. }
  1232. ret = QRinput_List_shrinkEntry(entry, bytes);
  1233. if(ret < 0) {
  1234. QRinput_List_freeEntry(e);
  1235. return -1;
  1236. }
  1237. e->next = entry->next;
  1238. entry->next = e;
  1239. return 0;
  1240. }
  1241. QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input)
  1242. {
  1243. QRinput *p = NULL;
  1244. QRinput_Struct *s = NULL;
  1245. int bits, maxbits, nextbits, bytes, ret;
  1246. QRinput_List *list, *next, *prev;
  1247. BitStream *bstream = NULL;
  1248. if(input->mqr) {
  1249. errno = EINVAL;
  1250. return NULL;
  1251. }
  1252. s = QRinput_Struct_new();
  1253. if(s == NULL) return NULL;
  1254. input = QRinput_dup(input);
  1255. if(input == NULL) {
  1256. QRinput_Struct_free(s);
  1257. return NULL;
  1258. }
  1259. QRinput_Struct_setParity(s, QRinput_calcParity(input));
  1260. maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_SIZE;
  1261. if(maxbits <= 0) goto ABORT;
  1262. bstream = BitStream_new();
  1263. if(bstream == NULL) goto ABORT;
  1264. bits = 0;
  1265. list = input->head;
  1266. prev = NULL;
  1267. while(list != NULL) {
  1268. nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version, input->mqr);
  1269. if(bits + nextbits <= maxbits) {
  1270. BitStream_reset(bstream);
  1271. ret = QRinput_encodeBitStream(list, bstream, input->version, input->mqr);
  1272. if(ret < 0) goto ABORT;
  1273. bits += ret;
  1274. prev = list;
  1275. list = list->next;
  1276. } else {
  1277. bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
  1278. p = QRinput_new2(input->version, input->level);
  1279. if(p == NULL) goto ABORT;
  1280. if(bytes > 0) {
  1281. /* Splits this entry into 2 entries. */
  1282. ret = QRinput_splitEntry(list, bytes);
  1283. if(ret < 0) {
  1284. QRinput_free(p);
  1285. goto ABORT;
  1286. }
  1287. /* First half is the tail of the current input. */
  1288. next = list->next;
  1289. list->next = NULL;
  1290. /* Second half is the head of the next input, p.*/
  1291. p->head = next;
  1292. /* Renew QRinput.tail. */
  1293. p->tail = input->tail;
  1294. input->tail = list;
  1295. /* Point to the next entry. */
  1296. prev = list;
  1297. list = next;
  1298. } else {
  1299. /* Current entry will go to the next input. */
  1300. prev->next = NULL;
  1301. p->head = list;
  1302. p->tail = input->tail;
  1303. input->tail = prev;
  1304. }
  1305. ret = QRinput_Struct_appendInput(s, input);
  1306. if(ret < 0) {
  1307. QRinput_free(p);
  1308. goto ABORT;
  1309. }
  1310. input = p;
  1311. bits = 0;
  1312. }
  1313. }
  1314. ret = QRinput_Struct_appendInput(s, input);
  1315. if(ret < 0) goto ABORT;
  1316. if(s->size > MAX_STRUCTURED_SYMBOLS) {
  1317. errno = ERANGE;
  1318. QRinput_Struct_free(s);
  1319. BitStream_free(bstream);
  1320. return NULL;
  1321. }
  1322. ret = QRinput_Struct_insertStructuredAppendHeaders(s);
  1323. if(ret < 0) {
  1324. QRinput_Struct_free(s);
  1325. BitStream_free(bstream);
  1326. return NULL;
  1327. }
  1328. BitStream_free(bstream);
  1329. return s;
  1330. ABORT:
  1331. BitStream_free(bstream);
  1332. QRinput_free(input);
  1333. QRinput_Struct_free(s);
  1334. return NULL;
  1335. }
  1336. int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
  1337. {
  1338. int i;
  1339. QRinput_InputList *list;
  1340. if(s->size == 1) {
  1341. return 0;
  1342. }
  1343. if(s->parity < 0) {
  1344. QRinput_Struct_calcParity(s);
  1345. }
  1346. i = 1;
  1347. list = s->head;
  1348. while(list != NULL) {
  1349. if(QRinput_insertStructuredAppendHeader(list->input, s->size, i, s->parity))
  1350. return -1;
  1351. i++;
  1352. list = list->next;
  1353. }
  1354. return 0;
  1355. }
  1356. /******************************************************************************
  1357. * Extended encoding mode (FNC1 and ECI)
  1358. *****************************************************************************/
  1359. int QRinput_setFNC1First(QRinput *input)
  1360. {
  1361. if(input->mqr) {
  1362. errno = EINVAL;
  1363. return -1;
  1364. }
  1365. input->fnc1 = 1;
  1366. return 0;
  1367. }
  1368. int QRinput_setFNC1Second(QRinput *input, unsigned char appid)
  1369. {
  1370. if(input->mqr) {
  1371. errno = EINVAL;
  1372. return -1;
  1373. }
  1374. input->fnc1 = 2;
  1375. input->appid = appid;
  1376. return 0;
  1377. }