2 * qrencode - QR Code encoder
4 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
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.
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.
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
22 * Libqrencode is a library for encoding data in a QR Code symbol, a kind of 2D
25 * \section encoding Encoding
27 * There are two ways to encode data: <b>encoding a string</b> or
28 * <b>encoding a structured data</b>.
30 * \subsection encoding-string Encoding a string
31 * You can encode a string by calling QRcode_encodeString().
32 * The given string is parsed automatically and encoded. If you want to encode
33 * data that can be represented as a C string style (NUL terminated), you can
34 * simply use this way.
36 * If the input data contains Kanji (Shift-JIS) characters and you want to
37 * encode them as Kanji in QR Code, you should give QR_MODE_KANJI as a hint.
38 * Otherwise, all of non-alphanumeric characters are encoded as 8 bit data.
39 * If you want to encode a whole string in 8 bit mode, use
40 * QRcode_encodeString8bit() instead.
42 * Please note that a C string can not contain NUL character. If your data
43 * contains NUL, you should chose the second way.
45 * \subsection encoding-input Encoding a structured data
46 * You can construct a structured input data manually. If the structure of the
47 * input data is known, you can use this way.
48 * At first, create a ::QRinput object by QRinput_new(). Then add input data
49 * to the QRinput object by QRinput_append(). Finally call QRcode_encodeInput()
50 * to encode the QRinput data.
51 * You can reuse the QRinput data again to encode it in other symbols with
52 * different parameters.
54 * \section result Result
55 * The encoded symbol is resulted as a ::QRcode object. It will contain
56 * its version number, width of the symbol and an array represents the symbol.
57 * See ::QRcode for the details. You can free the object by QRcode_free().
59 * Please note that the version of the result may be larger than specified.
60 * In such cases, the input data would be too large to be encoded in a
61 * symbol of the specified version.
63 * \section structured Structured append
64 * Libqrencode can generate "Structured-appended" symbols that enables to split
65 * a large data set into mulitple QR codes. A QR code reader concatenates
66 * multiple QR code symbols into a string.
67 * Just like QRcode_encodeString(), you can use QRcode_encodeStringStructured()
68 * to generate structured-appended symbols. This functions returns an instance
69 * of ::QRcode_List. The returned list is a singly-linked list of QRcode: you
70 * can retrieve each QR code in this way:
73 * QRcode_List *qrcodes;
77 * qrcodes = QRcode_encodeStringStructured(...);
79 * while(entry != NULL) {
80 * qrcode = entry->code;
82 * entry = entry->next;
84 * QRcode_List_free(entry);
87 * Instead of using auto-parsing functions, you can construct your own
88 * structured input. At first, instantiate an object of ::QRinput_Struct
89 * by calling QRinput_Struct_new(). This object can hold multiple ::QRinput,
90 * and one QR code is generated for a ::QRinput.
91 * QRinput_Struct_appendInput() appends a ::QRinput to a ::QRinput_Struct
92 * object. In order to generate structured-appended symbols, it is required to
93 * embed headers to each symbol. You can use
94 * QRinput_Struct_insertStructuredAppendHeaders() to insert appropriate
95 * headers to each symbol. You should call this function just once before
99 #ifndef __QRENCODE_H__
100 #define __QRENCODE_H__
102 #if defined(__cplusplus)
110 QR_MODE_NUL = -1, ///< Terminator (NUL character). Internal use only
111 QR_MODE_NUM = 0, ///< Numeric mode
112 QR_MODE_AN, ///< Alphabet-numeric mode
113 QR_MODE_8, ///< 8-bit data mode
114 QR_MODE_KANJI, ///< Kanji (shift-jis) mode
115 QR_MODE_STRUCTURE, ///< Internal use only
119 * Level of error correction.
122 QR_ECLEVEL_L = 0, ///< lowest
125 QR_ECLEVEL_H ///< highest
128 /******************************************************************************
129 * Input data (qrinput.c)
130 *****************************************************************************/
133 * Singly linked list to contain input strings. An instance of this class
134 * contains its version and error correction level too. It is required to
135 * set them by QRinput_setVersion() and QRinput_setErrorCorrectionLevel(),
136 * or use QRinput_new2() to instantiate an object.
138 typedef struct _QRinput QRinput;
141 * Instantiate an input data object. The version is set to 0 (auto-select)
142 * and the error correction level is set to QR_ECLEVEL_L.
143 * @return an input object (initialized). On error, NULL is returned and errno
144 * is set to indicate the error.
145 * @throw ENOMEM unable to allocate memory.
147 extern QRinput *QRinput_new(void);
150 * Instantiate an input data object.
151 * @param version version number.
152 * @param level Error correction level.
153 * @return an input object (initialized). On error, NULL is returned and errno
154 * is set to indicate the error.
155 * @throw ENOMEM unable to allocate memory for input objects.
156 * @throw EINVAL invalid arguments.
158 extern QRinput *QRinput_new2(int version, QRecLevel level);
161 * Append data to an input object.
162 * The data is copied and appended to the input object.
163 * @param input input object.
164 * @param mode encoding mode.
165 * @param size size of data (byte).
166 * @param data a pointer to the memory area of the input data.
168 * @retval -1 an error occurred and errno is set to indeicate the error.
169 * See Execptions for the details.
170 * @throw ENOMEM unable to allocate memory.
171 * @throw EINVAL input data is invalid.
174 extern int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data);
177 * Get current version.
178 * @param input input object.
179 * @return current version.
181 extern int QRinput_getVersion(QRinput *input);
184 * Set version of the QR-code that is to be encoded.
185 * @param input input object.
186 * @param version version number (0 = auto)
188 * @retval -1 invalid argument.
190 extern int QRinput_setVersion(QRinput *input, int version);
193 * Get current error correction level.
194 * @param input input object.
195 * @return Current error correcntion level.
197 extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input);
200 * Set error correction level of the QR-code that is to be encoded.
201 * @param input input object.
202 * @param level Error correction level.
204 * @retval -1 invalid argument.
206 extern int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level);
209 * Free the input object.
210 * All of data chunks in the input object are freed too.
211 * @param input input object.
213 extern void QRinput_free(QRinput *input);
216 * Validate the input data.
217 * @param mode encoding mode.
218 * @param size size of data (byte).
219 * @param data a pointer to the memory area of the input data.
221 * @retval -1 invalid arguments.
223 extern int QRinput_check(QRencodeMode mode, int size, const unsigned char *data);
226 * Set of QRinput for structured symbols.
228 typedef struct _QRinput_Struct QRinput_Struct;
231 * Instantiate a set of input data object.
232 * @return an instance of QRinput_Struct. On error, NULL is returned and errno
233 * is set to indicate the error.
234 * @throw ENOMEM unable to allocate memory.
236 extern QRinput_Struct *QRinput_Struct_new(void);
239 * Set parity of structured symbols.
240 * @param s structured input object.
241 * @param parity parity of s.
243 extern void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity);
246 * Append a QRinput object to the set.
247 * @warning never append the same QRinput object twice or more.
248 * @param s structured input object.
249 * @param input an input object.
250 * @retval >0 number of input objects in the structure.
251 * @retval -1 an error occurred. See Exceptions for the details.
252 * @throw ENOMEM unable to allocate memory.
254 extern int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input);
257 * Free all of QRinput in the set.
258 * @param s a structured input object.
260 extern void QRinput_Struct_free(QRinput_Struct *s);
263 * Split a QRinput to QRinput_Struct. It calculates a parity, set it, then
264 * insert structured-append headers.
265 * @param input input object. Version number and error correction level must be
267 * @return a set of input data. On error, NULL is returned, and errno is set
268 * to indicate the error. See Exceptions for the details.
269 * @throw ERANGE input data is too large.
270 * @throw EINVAL invalid input data.
271 * @throw ENOMEM unable to allocate memory.
273 extern QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input);
276 * Insert structured-append headers to the input structure. It calculates
277 * a parity and set it if the parity is not set yet.
278 * @param s input structure
280 * @retval -1 an error occurred and errno is set to indeicate the error.
281 * See Execptions for the details.
282 * @throw EINVAL invalid input object.
283 * @throw ENOMEM unable to allocate memory.
285 extern int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s);
287 /******************************************************************************
288 * QRcode output (qrencode.c)
289 *****************************************************************************/
293 * Symbol data is represented as an array contains width*width uchars.
294 * Each uchar represents a module (dot). If the less significant bit of
295 * the uchar is 1, the corresponding module is black. The other bits are
296 * meaningless for usual applications, but here its specification is described.
300 * |||||||`- 1=black/0=white
301 * ||||||`-- data and ecc code area
302 * |||||`--- format information
303 * ||||`---- version information
304 * |||`----- timing pattern
305 * ||`------ alignment pattern
306 * |`------- finder pattern and separator
307 * `-------- non-data modules (format, timing, etc.)
311 int version; ///< version of the symbol
312 int width; ///< width of the symbol
313 unsigned char *data; ///< symbol data
317 * Singly-linked list of QRcode. Used to represent a structured symbols.
318 * A list is terminated with NULL.
320 typedef struct _QRcode_List QRcode_List;
322 struct _QRcode_List {
328 * Create a symbol from the input data.
329 * @warning This function is THREAD UNSAFE.
330 * @param input input data.
331 * @return an instance of QRcode class. The version of the result QRcode may
332 * be larger than the designated version. On error, NULL is returned,
333 * and errno is set to indicate the error. See Exceptions for the
335 * @throw EINVAL invalid input object.
336 * @throw ENOMEM unable to allocate memory for input objects.
338 extern QRcode *QRcode_encodeInput(QRinput *input);
341 * Create a symbol from the string. The library automatically parses the input
342 * string and encodes in a QR Code symbol.
343 * @warning This function is THREAD UNSAFE.
344 * @param string input string. It must be NULL terminated.
345 * @param version version of the symbol. If 0, the library chooses the minimum
346 * version for the given input data.
347 * @param level error correction level.
348 * @param hint tell the library how non-alphanumerical characters should be
349 * encoded. If QR_MODE_KANJI is given, kanji characters will be
350 * encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
351 * non-alphanumerical characters will be encoded as is. If you want
352 * to embed UTF-8 string, choose this.
353 * @param casesensitive case-sensitive(1) or not(0).
354 * @return an instance of QRcode class. The version of the result QRcode may
355 * be larger than the designated version. On error, NULL is returned,
356 * and errno is set to indicate the error. See Exceptions for the
358 * @throw EINVAL invalid input object.
359 * @throw ENOMEM unable to allocate memory for input objects.
361 extern QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
364 * Same to QRcode_encodeString(), but encode whole data in 8-bit mode.
365 * @warning This function is THREAD UNSAFE.
367 extern QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level);
370 * Free the instance of QRcode class.
371 * @param qrcode an instance of QRcode class.
373 extern void QRcode_free(QRcode *qrcode);
376 * Create structured symbols from the input data.
377 * @warning This function is THREAD UNSAFE.
379 * @return a singly-linked list of QRcode.
381 extern QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s);
384 * Create structured symbols from the string. The library automatically parses
385 * the input string and encodes in a QR Code symbol.
386 * @warning This function is THREAD UNSAFE.
387 * @param string input string. It should be NULL terminated.
388 * @param version version of the symbol.
389 * @param level error correction level.
390 * @param hint tell the library how non-alphanumerical characters should be
391 * encoded. If QR_MODE_KANJI is given, kanji characters will be
392 * encoded as Shif-JIS characters. If QR_MODE_8 is given, all of
393 * non-alphanumerical characters will be encoded as is. If you want
394 * to embed UTF-8 string, choose this.
395 * @param casesensitive case-sensitive(1) or not(0).
396 * @return a singly-linked list of QRcode. On error, NULL is returned, and
397 * errno is set to indicate the error. See Exceptions for the details.
398 * @throw EINVAL invalid input object.
399 * @throw ENOMEM unable to allocate memory for input objects.
401 extern QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
404 * Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.
405 * @warning This function is THREAD UNSAFE.
407 extern QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level);
410 * Return the number of symbols included in a QRcode_List.
411 * @param qrlist a head entry of a QRcode_List.
412 * @return number of symbols in the list.
414 extern int QRcode_List_size(QRcode_List *qrlist);
417 * Free the QRcode_List.
418 * @param qrlist a head entry of a QRcode_List.
420 extern void QRcode_List_free(QRcode_List *qrlist);
422 #if defined(__cplusplus)
426 #endif /* __QRENCODE_H__ */