qrencode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * qrencode - QR Code encoder
  3. *
  4. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  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 ways to encode data: <b>encoding a string</b> or
  27. * <b>encoding a structured data</b>.
  28. *
  29. * \subsection encoding-string Encoding a string
  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, use
  39. * QRcode_encodeString8bit() instead.
  40. *
  41. * Please note that a C string can not contain NUL character. If your data
  42. * contains NUL, you should chose the second way.
  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. } QRencodeMode;
  113. /**
  114. * Level of error correction.
  115. */
  116. typedef enum {
  117. QR_ECLEVEL_L = 0, ///< lowest
  118. QR_ECLEVEL_M,
  119. QR_ECLEVEL_Q,
  120. QR_ECLEVEL_H ///< highest
  121. } QRecLevel;
  122. /******************************************************************************
  123. * Input data (qrinput.c)
  124. *****************************************************************************/
  125. /**
  126. * Singly linked list to contain input strings. An instance of this class
  127. * contains its version and error correction level too. It is required to
  128. * set them by QRinput_setVersion() and QRinput_setErrorCorrectionLevel(),
  129. * or use QRinput_new2() to instantiate an object.
  130. */
  131. typedef struct _QRinput QRinput;
  132. /**
  133. * Instantiate an input data object. The version is set to 0 (auto-select)
  134. * and the error correction level is set to QR_ECLEVEL_L.
  135. * @return an input object (initialized). On error, NULL is returned and errno
  136. * is set to indicate the error.
  137. * @throw ENOMEM unable to allocate memory.
  138. */
  139. extern QRinput *QRinput_new(void);
  140. /**
  141. * Instantiate an input data object.
  142. * @param version version number.
  143. * @param level Error correction level.
  144. * @return an input object (initialized). On error, NULL is returned and errno
  145. * is set to indicate the error.
  146. * @throw ENOMEM unable to allocate memory for input objects.
  147. * @throw EINVAL invalid arguments.
  148. */
  149. extern QRinput *QRinput_new2(int version, QRecLevel level);
  150. /**
  151. * Append data to an input object.
  152. * The data is copied and appended to the input object.
  153. * @param input input object.
  154. * @param mode encoding mode.
  155. * @param size size of data (byte).
  156. * @param data a pointer to the memory area of the input data.
  157. * @retval 0 success.
  158. * @retval -1 an error occurred and errno is set to indeicate the error.
  159. * See Execptions for the details.
  160. * @throw ENOMEM unable to allocate memory.
  161. * @throw EINVAL input data is invalid.
  162. *
  163. */
  164. extern int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data);
  165. /**
  166. * Get current version.
  167. * @param input input object.
  168. * @return current version.
  169. */
  170. extern int QRinput_getVersion(QRinput *input);
  171. /**
  172. * Set version of the QR-code that is to be encoded.
  173. * @param input input object.
  174. * @param version version number (0 = auto)
  175. * @retval 0 success.
  176. * @retval -1 invalid argument.
  177. */
  178. extern int QRinput_setVersion(QRinput *input, int version);
  179. /**
  180. * Get current error correction level.
  181. * @param input input object.
  182. * @return Current error correcntion level.
  183. */
  184. extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input);
  185. /**
  186. * Set error correction level of the QR-code that is to be encoded.
  187. * @param input input object.
  188. * @param level Error correction level.
  189. * @retval 0 success.
  190. * @retval -1 invalid argument.
  191. */
  192. extern int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level);
  193. /**
  194. * Free the input object.
  195. * All of data chunks in the input object are freed too.
  196. * @param input input object.
  197. */
  198. extern void QRinput_free(QRinput *input);
  199. /**
  200. * Validate the input data.
  201. * @param mode encoding mode.
  202. * @param size size of data (byte).
  203. * @param data a pointer to the memory area of the input data.
  204. * @retval 0 success.
  205. * @retval -1 invalid arguments.
  206. */
  207. extern int QRinput_check(QRencodeMode mode, int size, const unsigned char *data);
  208. /**
  209. * Set of QRinput for structured symbols.
  210. */
  211. typedef struct _QRinput_Struct QRinput_Struct;
  212. /**
  213. * Instantiate a set of input data object.
  214. * @return an instance of QRinput_Struct. On error, NULL is returned and errno
  215. * is set to indicate the error.
  216. * @throw ENOMEM unable to allocate memory.
  217. */
  218. extern QRinput_Struct *QRinput_Struct_new(void);
  219. /**
  220. * Set parity of structured symbols.
  221. * @param s structured input object.
  222. * @param parity parity of s.
  223. */
  224. extern void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity);
  225. /**
  226. * Append a QRinput object to the set.
  227. * @warning never append the same QRinput object twice or more.
  228. * @param s structured input object.
  229. * @param input an input object.
  230. * @retval >0 number of input objects in the structure.
  231. * @retval -1 an error occurred. See Exceptions for the details.
  232. * @throw ENOMEM unable to allocate memory.
  233. */
  234. extern int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input);
  235. /**
  236. * Free all of QRinput in the set.
  237. * @param s a structured input object.
  238. */
  239. extern void QRinput_Struct_free(QRinput_Struct *s);
  240. /**
  241. * Split a QRinput to QRinput_Struct. It calculates a parity, set it, then
  242. * insert structured-append headers.
  243. * @param input input object. Version number and error correction level must be
  244. * set.
  245. * @return a set of input data. On error, NULL is returned, and errno is set
  246. * to indicate the error. See Exceptions for the details.
  247. * @throw ERANGE input data is too large.
  248. * @throw EINVAL invalid input data.
  249. * @throw ENOMEM unable to allocate memory.
  250. */
  251. extern QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input);
  252. /**
  253. * Insert structured-append headers to the input structure. It calculates
  254. * a parity and set it if the parity is not set yet.
  255. * @param s input structure
  256. * @retval 0 success.
  257. * @retval -1 an error occurred and errno is set to indeicate the error.
  258. * See Execptions for the details.
  259. * @throw EINVAL invalid input object.
  260. * @throw ENOMEM unable to allocate memory.
  261. */
  262. extern int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s);
  263. /******************************************************************************
  264. * QRcode output (qrencode.c)
  265. *****************************************************************************/
  266. /**
  267. * QRcode class.
  268. * Symbol data is represented as an array contains width*width uchars.
  269. * Each uchar represents a module (dot). If the less significant bit of
  270. * the uchar is 1, the corresponding module is black. The other bits are
  271. * meaningless for usual applications, but here its specification is described.
  272. *
  273. * <pre>
  274. * MSB 76543210 LSB
  275. * |||||||`- 1=black/0=white
  276. * ||||||`-- data and ecc code area
  277. * |||||`--- format information
  278. * ||||`---- version information
  279. * |||`----- timing pattern
  280. * ||`------ alignment pattern
  281. * |`------- finder pattern and separator
  282. * `-------- non-data modules (format, timing, etc.)
  283. * </pre>
  284. */
  285. typedef struct {
  286. int version; ///< version of the symbol
  287. int width; ///< width of the symbol
  288. unsigned char *data; ///< symbol data
  289. } QRcode;
  290. /**
  291. * Singly-linked list of QRcode. Used to represent a structured symbols.
  292. * A list is terminated with NULL.
  293. */
  294. typedef struct _QRcode_List QRcode_List;
  295. struct _QRcode_List {
  296. QRcode *code;
  297. QRcode_List *next;
  298. };
  299. /**
  300. * Create a symbol from the input data.
  301. * @warning This function is THREAD UNSAFE.
  302. * @param input input data.
  303. * @return an instance of QRcode class. The version of the result QRcode may
  304. * be larger than the designated version. On error, NULL is returned,
  305. * and errno is set to indicate the error. See Exceptions for the
  306. * details.
  307. * @throw EINVAL invalid input object.
  308. * @throw ENOMEM unable to allocate memory for input objects.
  309. */
  310. extern QRcode *QRcode_encodeInput(QRinput *input);
  311. /**
  312. * Create a symbol from the string. The library automatically parses the input
  313. * string and encodes in a QR Code symbol.
  314. * @warning This function is THREAD UNSAFE.
  315. * @param string input string. It must be NULL terminated.
  316. * @param version version of the symbol. If 0, the library chooses the minimum
  317. * version for the given input data.
  318. * @param level error correction level.
  319. * @param hint tell the library how non-alphanumerical characters should be
  320. * encoded. If QR_MODE_KANJI is given, kanji characters will be
  321. * encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
  322. * non-alphanumerical characters will be encoded as is. If you want
  323. * to embed UTF-8 string, choose this.
  324. * @param casesensitive case-sensitive(1) or not(0).
  325. * @return an instance of QRcode class. The version of the result QRcode may
  326. * be larger than the designated version. On error, NULL is returned,
  327. * and errno is set to indicate the error. See Exceptions for the
  328. * details.
  329. * @throw EINVAL invalid input object.
  330. * @throw ENOMEM unable to allocate memory for input objects.
  331. */
  332. extern QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
  333. /**
  334. * Same to QRcode_encodeString(), but encode whole data in 8-bit mode.
  335. * @warning This function is THREAD UNSAFE.
  336. */
  337. extern QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level);
  338. /**
  339. * Free the instance of QRcode class.
  340. * @param qrcode an instance of QRcode class.
  341. */
  342. extern void QRcode_free(QRcode *qrcode);
  343. /**
  344. * Create structured symbols from the input data.
  345. * @warning This function is THREAD UNSAFE.
  346. * @param s
  347. * @return a singly-linked list of QRcode.
  348. */
  349. extern QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s);
  350. /**
  351. * Create structured symbols from the string. The library automatically parses
  352. * the input string and encodes in a QR Code symbol.
  353. * @warning This function is THREAD UNSAFE.
  354. * @param string input string. It should be NULL terminated.
  355. * @param version version of the symbol.
  356. * @param level error correction level.
  357. * @param hint tell the library how non-alphanumerical characters should be
  358. * encoded. If QR_MODE_KANJI is given, kanji characters will be
  359. * encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
  360. * non-alphanumerical characters will be encoded as is. If you want
  361. * to embed UTF-8 string, choose this.
  362. * @param casesensitive case-sensitive(1) or not(0).
  363. * @return a singly-linked list of QRcode. On error, NULL is returned, and
  364. * errno is set to indicate the error. See Exceptions for the details.
  365. * @throw EINVAL invalid input object.
  366. * @throw ENOMEM unable to allocate memory for input objects.
  367. */
  368. extern QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
  369. /**
  370. * Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.
  371. * @warning This function is THREAD UNSAFE.
  372. */
  373. extern QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level);
  374. /**
  375. * Return the number of symbols included in a QRcode_List.
  376. * @param qrlist a head entry of a QRcode_List.
  377. * @return number of symbols in the list.
  378. */
  379. extern int QRcode_List_size(QRcode_List *qrlist);
  380. /**
  381. * Free the QRcode_List.
  382. * @param qrlist a head entry of a QRcode_List.
  383. */
  384. extern void QRcode_List_free(QRcode_List *qrlist);
  385. #if defined(__cplusplus)
  386. }
  387. #endif
  388. #endif /* __QRENCODE_H__ */