qrencode.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /**
  2. * qrencode - QR Code encoder
  3. *
  4. * Copyright (C) 2006-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /** \mainpage
  21. * Libqrencode is a library for encoding data in a QR Code symbol, a kind of 2D
  22. * symbology.
  23. *
  24. * \section encoding Encoding
  25. *
  26. * There are two methods to encode data: <b>encoding a string/data</b> or
  27. * <b>encoding a structured data</b>.
  28. *
  29. * \subsection encoding-string Encoding a string/data
  30. * You can encode a string by calling QRcode_encodeString().
  31. * The given string is parsed automatically and encoded. If you want to encode
  32. * data that can be represented as a C string style (NUL terminated), you can
  33. * simply use this way.
  34. *
  35. * If the input data contains Kanji (Shift-JIS) characters and you want to
  36. * encode them as Kanji in QR Code, you should give QR_MODE_KANJI as a hint.
  37. * Otherwise, all of non-alphanumeric characters are encoded as 8 bit data.
  38. * If you want to encode a whole string in 8 bit mode, you can use
  39. * QRcode_encodeString8bit() instead.
  40. *
  41. * Please note that a C string can not contain NUL characters. If your data
  42. * contains NUL, you must use QRcode_encodeData().
  43. *
  44. * \subsection encoding-input Encoding a structured data
  45. * You can construct a structured input data manually. If the structure of the
  46. * input data is known, you can use this way.
  47. * At first, create a ::QRinput object by QRinput_new(). Then add input data
  48. * to the QRinput object by QRinput_append(). Finally call QRcode_encodeInput()
  49. * to encode the QRinput data.
  50. * You can reuse the QRinput data again to encode it in other symbols with
  51. * different parameters.
  52. *
  53. * \section result Result
  54. * The encoded symbol is resulted as a ::QRcode object. It will contain
  55. * its version number, width of the symbol and an array represents the symbol.
  56. * See ::QRcode for the details. You can free the object by QRcode_free().
  57. *
  58. * Please note that the version of the result may be larger than specified.
  59. * In such cases, the input data would be too large to be encoded in a
  60. * symbol of the specified version.
  61. *
  62. * \section structured Structured append
  63. * Libqrencode can generate "Structured-appended" symbols that enables to split
  64. * a large data set into mulitple QR codes. A QR code reader concatenates
  65. * multiple QR code symbols into a string.
  66. * Just like QRcode_encodeString(), you can use QRcode_encodeStringStructured()
  67. * to generate structured-appended symbols. This functions returns an instance
  68. * of ::QRcode_List. The returned list is a singly-linked list of QRcode: you
  69. * can retrieve each QR code in this way:
  70. *
  71. * \code
  72. * QRcode_List *qrcodes;
  73. * QRcode_List *entry;
  74. * QRcode *qrcode;
  75. *
  76. * qrcodes = QRcode_encodeStringStructured(...);
  77. * entry = qrcodes;
  78. * while(entry != NULL) {
  79. * qrcode = entry->code;
  80. * // do something
  81. * entry = entry->next;
  82. * }
  83. * QRcode_List_free(entry);
  84. * \endcode
  85. *
  86. * Instead of using auto-parsing functions, you can construct your own
  87. * structured input. At first, instantiate an object of ::QRinput_Struct
  88. * by calling QRinput_Struct_new(). This object can hold multiple ::QRinput,
  89. * and one QR code is generated for a ::QRinput.
  90. * QRinput_Struct_appendInput() appends a ::QRinput to a ::QRinput_Struct
  91. * object. In order to generate structured-appended symbols, it is required to
  92. * embed headers to each symbol. You can use
  93. * QRinput_Struct_insertStructuredAppendHeaders() to insert appropriate
  94. * headers to each symbol. You should call this function just once before
  95. * encoding symbols.
  96. */
  97. #ifndef QRENCODE_H
  98. #define QRENCODE_H
  99. #if defined(__cplusplus)
  100. extern "C" {
  101. #endif
  102. /**
  103. * Encoding mode.
  104. */
  105. typedef enum {
  106. QR_MODE_NUL = -1, ///< Terminator (NUL character). Internal use only
  107. QR_MODE_NUM = 0, ///< Numeric mode
  108. QR_MODE_AN, ///< Alphabet-numeric mode
  109. QR_MODE_8, ///< 8-bit data mode
  110. QR_MODE_KANJI, ///< Kanji (shift-jis) mode
  111. QR_MODE_STRUCTURE, ///< Internal use only
  112. QR_MODE_ECI, ///< ECI mode
  113. QR_MODE_FNC1FIRST, ///< FNC1, first position
  114. QR_MODE_FNC1SECOND, ///< FNC1, second position
  115. } QRencodeMode;
  116. /**
  117. * Level of error correction.
  118. */
  119. typedef enum {
  120. QR_ECLEVEL_L = 0, ///< lowest
  121. QR_ECLEVEL_M,
  122. QR_ECLEVEL_Q,
  123. QR_ECLEVEL_H ///< highest
  124. } QRecLevel;
  125. /**
  126. * Maximum version (size) of QR-code symbol.
  127. */
  128. #define QRSPEC_VERSION_MAX 40
  129. /**
  130. * Maximum version (size) of QR-code symbol.
  131. */
  132. #define MQRSPEC_VERSION_MAX 4
  133. /******************************************************************************
  134. * Input data (qrinput.c)
  135. *****************************************************************************/
  136. /**
  137. * Singly linked list to contain input strings. An instance of this class
  138. * contains its version and error correction level too. It is required to
  139. * set them by QRinput_setVersion() and QRinput_setErrorCorrectionLevel(),
  140. * or use QRinput_new2() to instantiate an object.
  141. */
  142. typedef struct _QRinput QRinput;
  143. /**
  144. * Instantiate an input data object. The version is set to 0 (auto-select)
  145. * and the error correction level is set to QR_ECLEVEL_L.
  146. * @return an input object (initialized). On error, NULL is returned and errno
  147. * is set to indicate the error.
  148. * @throw ENOMEM unable to allocate memory.
  149. */
  150. extern QRinput* QRinput_new(void);
  151. /**
  152. * Instantiate an input data object.
  153. * @param version version number.
  154. * @param level Error correction level.
  155. * @return an input object (initialized). On error, NULL is returned and errno
  156. * is set to indicate the error.
  157. * @throw ENOMEM unable to allocate memory for input objects.
  158. * @throw EINVAL invalid arguments.
  159. */
  160. extern QRinput* QRinput_new2(int version, QRecLevel level);
  161. /**
  162. * Instantiate an input data object. Object's Micro QR Code flag is set.
  163. * Unlike with full-sized QR Code, version number must be specified (>0).
  164. * @param version version number (1--4).
  165. * @param level Error correction level.
  166. * @return an input object (initialized). On error, NULL is returned and errno
  167. * is set to indicate the error.
  168. * @throw ENOMEM unable to allocate memory for input objects.
  169. * @throw EINVAL invalid arguments.
  170. */
  171. extern QRinput* QRinput_newMQR(int version, QRecLevel level);
  172. /**
  173. * Append data to an input object.
  174. * The data is copied and appended to the input object.
  175. * @param input input object.
  176. * @param mode encoding mode.
  177. * @param size size of data (byte).
  178. * @param data a pointer to the memory area of the input data.
  179. * @retval 0 success.
  180. * @retval -1 an error occurred and errno is set to indeicate the error.
  181. * See Execptions for the details.
  182. * @throw ENOMEM unable to allocate memory.
  183. * @throw EINVAL input data is invalid.
  184. *
  185. */
  186. extern int QRinput_append(QRinput* input, QRencodeMode mode, int size, const unsigned char* data);
  187. /**
  188. * Append ECI header.
  189. * @param input input object.
  190. * @param ecinum ECI indicator number (0 - 999999)
  191. * @retval 0 success.
  192. * @retval -1 an error occurred and errno is set to indeicate the error.
  193. * See Execptions for the details.
  194. * @throw ENOMEM unable to allocate memory.
  195. * @throw EINVAL input data is invalid.
  196. *
  197. */
  198. extern int QRinput_appendECIheader(QRinput* input, unsigned int ecinum);
  199. /**
  200. * Get current version.
  201. * @param input input object.
  202. * @return current version.
  203. */
  204. extern int QRinput_getVersion(QRinput* input);
  205. /**
  206. * Set version of the QR code that is to be encoded.
  207. * This function cannot be applied to Micro QR Code.
  208. * @param input input object.
  209. * @param version version number (0 = auto)
  210. * @retval 0 success.
  211. * @retval -1 invalid argument.
  212. */
  213. extern int QRinput_setVersion(QRinput* input, int version);
  214. /**
  215. * Get current error correction level.
  216. * @param input input object.
  217. * @return Current error correcntion level.
  218. */
  219. extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput* input);
  220. /**
  221. * Set error correction level of the QR code that is to be encoded.
  222. * This function cannot be applied to Micro QR Code.
  223. * @param input input object.
  224. * @param level Error correction level.
  225. * @retval 0 success.
  226. * @retval -1 invalid argument.
  227. */
  228. extern int QRinput_setErrorCorrectionLevel(QRinput* input, QRecLevel level);
  229. /**
  230. * Set version and error correction level of the QR code at once.
  231. * This function is recommened for Micro QR Code.
  232. * @param input input object.
  233. * @param version version number (0 = auto)
  234. * @param level Error correction level.
  235. * @retval 0 success.
  236. * @retval -1 invalid argument.
  237. */
  238. extern int QRinput_setVersionAndErrorCorrectionLevel(QRinput* input, int version, QRecLevel level);
  239. /**
  240. * Free the input object.
  241. * All of data chunks in the input object are freed too.
  242. * @param input input object.
  243. */
  244. extern void QRinput_free(QRinput* input);
  245. /**
  246. * Validate the input data.
  247. * @param mode encoding mode.
  248. * @param size size of data (byte).
  249. * @param data a pointer to the memory area of the input data.
  250. * @retval 0 success.
  251. * @retval -1 invalid arguments.
  252. */
  253. extern int QRinput_check(QRencodeMode mode, int size, const unsigned char* data);
  254. /**
  255. * Set of QRinput for structured symbols.
  256. */
  257. typedef struct _QRinput_Struct QRinput_Struct;
  258. /**
  259. * Instantiate a set of input data object.
  260. * @return an instance of QRinput_Struct. On error, NULL is returned and errno
  261. * is set to indicate the error.
  262. * @throw ENOMEM unable to allocate memory.
  263. */
  264. extern QRinput_Struct* QRinput_Struct_new(void);
  265. /**
  266. * Set parity of structured symbols.
  267. * @param s structured input object.
  268. * @param parity parity of s.
  269. */
  270. extern void QRinput_Struct_setParity(QRinput_Struct* s, unsigned char parity);
  271. /**
  272. * Append a QRinput object to the set. QRinput created by QRinput_newMQR()
  273. * will be rejected.
  274. * @warning never append the same QRinput object twice or more.
  275. * @param s structured input object.
  276. * @param input an input object.
  277. * @retval >0 number of input objects in the structure.
  278. * @retval -1 an error occurred. See Exceptions for the details.
  279. * @throw ENOMEM unable to allocate memory.
  280. * @throw EINVAL invalid arguments.
  281. */
  282. extern int QRinput_Struct_appendInput(QRinput_Struct* s, QRinput* input);
  283. /**
  284. * Free all of QRinput in the set.
  285. * @param s a structured input object.
  286. */
  287. extern void QRinput_Struct_free(QRinput_Struct* s);
  288. /**
  289. * Split a QRinput to QRinput_Struct. It calculates a parity, set it, then
  290. * insert structured-append headers. QRinput created by QRinput_newMQR() will
  291. * be rejected.
  292. * @param input input object. Version number and error correction level must be
  293. * set.
  294. * @return a set of input data. On error, NULL is returned, and errno is set
  295. * to indicate the error. See Exceptions for the details.
  296. * @throw ERANGE input data is too large.
  297. * @throw EINVAL invalid input data.
  298. * @throw ENOMEM unable to allocate memory.
  299. */
  300. extern QRinput_Struct* QRinput_splitQRinputToStruct(QRinput* input);
  301. /**
  302. * Insert structured-append headers to the input structure. It calculates
  303. * a parity and set it if the parity is not set yet.
  304. * @param s input structure
  305. * @retval 0 success.
  306. * @retval -1 an error occurred and errno is set to indeicate the error.
  307. * See Execptions for the details.
  308. * @throw EINVAL invalid input object.
  309. * @throw ENOMEM unable to allocate memory.
  310. */
  311. extern int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct* s);
  312. /**
  313. * Set FNC1-1st position flag.
  314. */
  315. extern int QRinput_setFNC1First(QRinput* input);
  316. /**
  317. * Set FNC1-2nd position flag and application identifier.
  318. */
  319. extern int QRinput_setFNC1Second(QRinput* input, unsigned char appid);
  320. /******************************************************************************
  321. * QRcode output (qrencode.c)
  322. *****************************************************************************/
  323. /**
  324. * QRcode class.
  325. * Symbol data is represented as an array contains width*width uchars.
  326. * Each uchar represents a module (dot). If the less significant bit of
  327. * the uchar is 1, the corresponding module is black. The other bits are
  328. * meaningless for usual applications, but here its specification is described.
  329. *
  330. * @verbatim
  331. MSB 76543210 LSB
  332. |||||||`- 1=black/0=white
  333. ||||||`-- 1=ecc/0=data code area
  334. |||||`--- format information
  335. ||||`---- version information
  336. |||`----- timing pattern
  337. ||`------ alignment pattern
  338. |`------- finder pattern and separator
  339. `-------- non-data modules (format, timing, etc.)
  340. @endverbatim
  341. */
  342. typedef struct {
  343. int version; ///< version of the symbol
  344. int width; ///< width of the symbol
  345. unsigned char* data; ///< symbol data
  346. } QRcode;
  347. /**
  348. * Singly-linked list of QRcode. Used to represent a structured symbols.
  349. * A list is terminated with NULL.
  350. */
  351. typedef struct _QRcode_List {
  352. QRcode* code;
  353. struct _QRcode_List* next;
  354. } QRcode_List;
  355. /**
  356. * Create a symbol from the input data.
  357. * @warning This function is THREAD UNSAFE when pthread is disabled.
  358. * @param input input data.
  359. * @return an instance of QRcode class. The version of the result QRcode may
  360. * be larger than the designated version. On error, NULL is returned,
  361. * and errno is set to indicate the error. See Exceptions for the
  362. * details.
  363. * @throw EINVAL invalid input object.
  364. * @throw ENOMEM unable to allocate memory for input objects.
  365. */
  366. extern QRcode* QRcode_encodeInput(QRinput* input);
  367. /**
  368. * Create a symbol from the string. The library automatically parses the input
  369. * string and encodes in a QR Code symbol.
  370. * @warning This function is THREAD UNSAFE when pthread is disabled.
  371. * @param string input string. It must be NUL terminated.
  372. * @param version version of the symbol. If 0, the library chooses the minimum
  373. * version for the given input data.
  374. * @param level error correction level.
  375. * @param hint tell the library how Japanese Kanji characters should be
  376. * encoded. If QR_MODE_KANJI is given, the library assumes that the
  377. * given string contains Shift-JIS characters and encodes them in
  378. * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
  379. * characters will be encoded as is. If you want to embed UTF-8
  380. * string, choose this. Other mode will cause EINVAL error.
  381. * @param casesensitive case-sensitive(1) or not(0).
  382. * @return an instance of QRcode class. The version of the result QRcode may
  383. * be larger than the designated version. On error, NULL is returned,
  384. * and errno is set to indicate the error. See Exceptions for the
  385. * details.
  386. * @throw EINVAL invalid input object.
  387. * @throw ENOMEM unable to allocate memory for input objects.
  388. * @throw ERANGE input data is too large.
  389. */
  390. extern QRcode* QRcode_encodeString(const char* string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
  391. /**
  392. * Same to QRcode_encodeString(), but encode whole data in 8-bit mode.
  393. * @warning This function is THREAD UNSAFE when pthread is disabled.
  394. */
  395. extern QRcode* QRcode_encodeString8bit(const char* string, int version, QRecLevel level);
  396. /**
  397. * Micro QR Code version of QRcode_encodeString().
  398. * @warning This function is THREAD UNSAFE when pthread is disabled.
  399. */
  400. extern QRcode* QRcode_encodeStringMQR(const char* string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
  401. /**
  402. * Micro QR Code version of QRcode_encodeString8bit().
  403. * @warning This function is THREAD UNSAFE when pthread is disabled.
  404. */
  405. extern QRcode* QRcode_encodeString8bitMQR(const char* string, int version, QRecLevel level);
  406. /**
  407. * Encode byte stream (may include '\0') in 8-bit mode.
  408. * @warning This function is THREAD UNSAFE when pthread is disabled.
  409. * @param size size of the input data.
  410. * @param data input data.
  411. * @param version version of the symbol. If 0, the library chooses the minimum
  412. * version for the given input data.
  413. * @param level error correction level.
  414. * @throw EINVAL invalid input object.
  415. * @throw ENOMEM unable to allocate memory for input objects.
  416. * @throw ERANGE input data is too large.
  417. */
  418. extern QRcode* QRcode_encodeData(int size, const unsigned char* data, int version, QRecLevel level);
  419. /**
  420. * Micro QR Code version of QRcode_encodeData().
  421. * @warning This function is THREAD UNSAFE when pthread is disabled.
  422. */
  423. extern QRcode* QRcode_encodeDataMQR(int size, const unsigned char* data, int version, QRecLevel level);
  424. /**
  425. * Free the instance of QRcode class.
  426. * @param qrcode an instance of QRcode class.
  427. */
  428. extern void QRcode_free(QRcode* qrcode);
  429. /**
  430. * Create structured symbols from the input data.
  431. * @warning This function is THREAD UNSAFE when pthread is disabled.
  432. * @param s input data, structured.
  433. * @return a singly-linked list of QRcode.
  434. */
  435. extern QRcode_List* QRcode_encodeInputStructured(QRinput_Struct* s);
  436. /**
  437. * Create structured symbols from the string. The library automatically parses
  438. * the input string and encodes in a QR Code symbol.
  439. * @warning This function is THREAD UNSAFE when pthread is disabled.
  440. * @param string input string. It must be NUL terminated.
  441. * @param version version of the symbol.
  442. * @param level error correction level.
  443. * @param hint tell the library how Japanese Kanji characters should be
  444. * encoded. If QR_MODE_KANJI is given, the library assumes that the
  445. * given string contains Shift-JIS characters and encodes them in
  446. * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
  447. * characters will be encoded as is. If you want to embed UTF-8
  448. * string, choose this. Other mode will cause EINVAL error.
  449. * @param casesensitive case-sensitive(1) or not(0).
  450. * @return a singly-linked list of QRcode. On error, NULL is returned, and
  451. * errno is set to indicate the error. See Exceptions for the details.
  452. * @throw EINVAL invalid input object.
  453. * @throw ENOMEM unable to allocate memory for input objects.
  454. */
  455. extern QRcode_List* QRcode_encodeStringStructured(const char* string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
  456. /**
  457. * Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.
  458. * @warning This function is THREAD UNSAFE when pthread is disabled.
  459. */
  460. extern QRcode_List* QRcode_encodeString8bitStructured(const char* string, int version, QRecLevel level);
  461. /**
  462. * Create structured symbols from byte stream (may include '\0'). Wholde data
  463. * are encoded in 8-bit mode.
  464. * @warning This function is THREAD UNSAFE when pthread is disabled.
  465. * @param size size of the input data.
  466. * @param data input dat.
  467. * @param version version of the symbol.
  468. * @param level error correction level.
  469. * @return a singly-linked list of QRcode. On error, NULL is returned, and
  470. * errno is set to indicate the error. See Exceptions for the details.
  471. * @throw EINVAL invalid input object.
  472. * @throw ENOMEM unable to allocate memory for input objects.
  473. */
  474. extern QRcode_List* QRcode_encodeDataStructured(int size, const unsigned char* data, int version, QRecLevel level);
  475. /**
  476. * Return the number of symbols included in a QRcode_List.
  477. * @param qrlist a head entry of a QRcode_List.
  478. * @return number of symbols in the list.
  479. */
  480. extern int QRcode_List_size(QRcode_List* qrlist);
  481. /**
  482. * Free the QRcode_List.
  483. * @param qrlist a head entry of a QRcode_List.
  484. */
  485. extern void QRcode_List_free(QRcode_List* qrlist);
  486. /******************************************************************************
  487. * System utilities
  488. *****************************************************************************/
  489. /**
  490. * Return a string that identifies the library version.
  491. * @param major_version major version number
  492. * @param minor_version minor version number
  493. * @param micro_version micro version number
  494. */
  495. extern void QRcode_APIVersion(int* major_version, int* minor_version, int* micro_version);
  496. /**
  497. * Return a string that identifies the library version.
  498. * @return a string identifies the library version. The string is held by the
  499. * library. Do NOT free it.
  500. */
  501. extern char* QRcode_APIVersionString(void);
  502. /**
  503. * @deprecated
  504. */
  505. #ifndef _MSC_VER
  506. extern void QRcode_clearCache(void) __attribute__((deprecated));
  507. #else
  508. extern void QRcode_clearCache(void);
  509. #endif
  510. #ifndef STATIC_IN_RELEASE
  511. #define STATIC_IN_RELEASE
  512. #endif
  513. #ifndef MAJOR_VERSION
  514. #define MAJOR_VERSION 1
  515. #endif
  516. #ifndef MINOR_VERSION
  517. #define MINOR_VERSION 1
  518. #endif
  519. #ifndef MICRO_VERSION
  520. #define MICRO_VERSION 1
  521. #endif
  522. #ifndef VERSION
  523. #define VERSION ""
  524. #endif
  525. #if defined(__cplusplus)
  526. }
  527. #endif
  528. #endif /* QRENCODE_H */