2 * qrencode - QR Code encoder
5 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
7 * The following data / specifications are taken from
8 * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
10 * "Automatic identification and data capture techniques --
11 * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
37 #define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
39 static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
46 if(c == '\0') return QR_MODE_NUL;
49 } else if(isalnum(c)) {
51 } else if(hint == QR_MODE_KANJI) {
54 word = ((unsigned int)c << 8) | d;
55 if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
64 static int Split_eatNum(const char *string, QRinput *input, QRencodeMode hint);
65 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
66 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
67 static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint);
69 static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
78 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
85 mode = Split_identifyMode(p, hint);
86 if(mode == QR_MODE_8) {
87 dif = QRinput_estimateBitsModeNum(run) + 4 + ln
88 + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
89 - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
91 return Split_eat8(string, input, hint);
94 if(mode == QR_MODE_AN) {
95 dif = QRinput_estimateBitsModeNum(run) + 4 + ln
96 + QRinput_estimateBitsModeAn(1) /* + 4 + la */
97 - QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
99 return Split_eatAn(string, input, hint);
103 ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
104 if(ret < 0) return -1;
109 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
117 la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
118 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
127 dif = QRinput_estimateBitsModeAn(p - string) /* + 4 + la */
128 + QRinput_estimateBitsModeNum(q - p) + 4 + ln
129 - QRinput_estimateBitsModeAn(q - string) /* - 4 - la */;
142 if(*p && !isalnum(*p)) {
143 dif = QRinput_estimateBitsModeAn(run) + 4 + la
144 + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
145 - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
147 return Split_eat8(string, input, hint);
151 ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
152 if(ret < 0) return -1;
157 static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
164 while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
168 ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
169 if(ret < 0) return -1;
174 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
183 la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
184 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
188 mode = Split_identifyMode(p, hint);
189 if(mode == QR_MODE_KANJI) {
192 if(mode == QR_MODE_NUM) {
197 dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
198 + QRinput_estimateBitsModeNum(q - p) + 4 + ln
199 - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
205 } else if(mode == QR_MODE_AN) {
210 dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
211 + QRinput_estimateBitsModeAn(q - p) + 4 + la
212 - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
224 ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
225 if(ret < 0) return -1;
230 static int Split_splitString(const char *string, QRinput *input,
236 if(*string == '\0') return 0;
238 mode = Split_identifyMode(string, hint);
239 if(mode == QR_MODE_NUM) {
240 length = Split_eatNum(string, input, hint);
241 } else if(mode == QR_MODE_AN) {
242 length = Split_eatAn(string, input, hint);
243 } else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
244 length = Split_eatKanji(string, input, hint);
246 length = Split_eat8(string, input, hint);
248 if(length == 0) return 0;
249 if(length < 0) return -1;
250 return Split_splitString(&string[length], input, hint);
253 static char *dupAndToUpper(const char *str, QRencodeMode hint)
258 newstr = strdup(str);
259 if(newstr == NULL) return NULL;
263 mode = Split_identifyMode(p, hint);
264 if(mode == QR_MODE_KANJI) {
267 if (*p >= 'a' && *p <= 'z') {
268 *p = (char)((int)*p - 32);
277 int Split_splitStringToQRinput(const char *string, QRinput *input,
278 QRencodeMode hint, int casesensitive)
283 if(string == NULL || *string == '\0') {
288 newstr = dupAndToUpper(string, hint);
289 if(newstr == NULL) return -1;
290 ret = Split_splitString(newstr, input, hint);
293 ret = Split_splitString(string, input, hint);