src/split.c

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 265 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mqrspec.c ↑ Summary src/qrinput.c »
1 /*
2 * qrencode - QR Code encoder
3 *
4 * Input data splitter.
5 * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
6 *
7 * The following data / specifications are taken from
8 * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
9 * or
10 * "Automatic identification and data capture techniques --
11 * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
12 *
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.
17 *
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.
22 *
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
26 */
27  
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "qrencode.h"
35 #include "qrinput.h"
36 #include "qrspec.h"
37 #include "split.h"
38  
39 #define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
40 #define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
41  
42 #if !HAVE_STRDUP
43 #undef strdup
44 char *strdup(const char *s)
45 {
46 size_t len = strlen(s) + 1;
47 void *new = malloc(len);
48 if(new == NULL) return NULL;
49 return (char *)memcpy(new, s, len);
50 }
51 #endif
52  
53 0 static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
54 {
55 unsigned char c, d;
56 unsigned int word;
57  
58 0 c = string[0];
59  
60 0 ▶ if(c == '\0') return QR_MODE_NUL;
Branch: 1 branch
BranchTrueFalseTotal
if(c == '\0') return QR_MODE_NUL; 0 0 0
61 0 ▶ if(isdigit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if(isdigit(c)) { 0 0 0
62 0 return QR_MODE_NUM;
63 0 ▶ } else if(isalnum(c)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(isalnum(c)) { 0 0 0
} else if(isalnum(c)) { 0 0 0
64 0 return QR_MODE_AN;
65 0 ▶ } else if(hint == QR_MODE_KANJI) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(hint == QR_MODE_KANJI) { 0 0 0
66 0 d = string[1];
67 0 ▶ if(d != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if(d != '\0') { 0 0 0
68 0 word = ((unsigned int)c << 8) | d;
69 0 ▶ if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
Branch: 4 branches
BranchTrueFalseTotal
if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) { 0 0 0
if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) { 0 0 0
if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) { 0 0 0
if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) { 0 0 0
70 0 return QR_MODE_KANJI;
71 }
72 }
73 }
74  
75 0 return QR_MODE_8;
76 }
77  
78 static int Split_eatNum(const char *string, QRinput *input, QRencodeMode hint);
79 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
80 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
81 static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint);
82  
83 0 static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
84 {
85 const char *p;
86 int ret;
87 int run;
88 int dif;
89 int ln;
90 QRencodeMode mode;
91  
92 0 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
93  
94 0 p = string;
95 0 ▶ while(isdigit(*p)) {
Branch: 1 branch
BranchTrueFalseTotal
while(isdigit(*p)) { 0 0 0
96 0 p++;
97 }
98 0 run = p - string;
99 0 mode = Split_identifyMode(p, hint);
100 0 ▶ if(mode == QR_MODE_8) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_8) { 0 0 0
101 0 dif = QRinput_estimateBitsModeNum(run) + 4 + ln
102 0 + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
103 0 - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
104 0 ▶ if(dif > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif > 0) { 0 0 0
105 0 return Split_eat8(string, input, hint);
106 }
107 }
108 0 ▶ if(mode == QR_MODE_AN) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_AN) { 0 0 0
109 0 dif = QRinput_estimateBitsModeNum(run) + 4 + ln
110 0 + QRinput_estimateBitsModeAn(1) /* + 4 + la */
111 0 - QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
112 0 ▶ if(dif > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif > 0) { 0 0 0
113 0 return Split_eatAn(string, input, hint);
114 }
115 }
116  
117 0 ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
118 0 ▶ if(ret < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(ret < 0) return -1; 0 0 0
119  
120 0 return run;
121 }
122  
123 0 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
124 {
125 const char *p, *q;
126 int ret;
127 int run;
128 int dif;
129 int la, ln;
130  
131 0 la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
132 0 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
133  
134 0 p = string;
135 0 ▶ while(isalnum(*p)) {
Branch: 2 branches
BranchTrueFalseTotal
while(isalnum(*p)) { 0 0 0
while(isalnum(*p)) { 0 0 0
136 0 ▶ if(isdigit(*p)) {
Branch: 1 branch
BranchTrueFalseTotal
if(isdigit(*p)) { 0 0 0
137 0 q = p;
138 0 ▶ while(isdigit(*q)) {
Branch: 1 branch
BranchTrueFalseTotal
while(isdigit(*q)) { 0 0 0
139 0 q++;
140 }
141 0 dif = QRinput_estimateBitsModeAn(p - string) /* + 4 + la */
142 0 + QRinput_estimateBitsModeNum(q - p) + 4 + ln
143 0 ▶ + (isalnum(*q)?(4 + ln):0)
Branch: 2 branches
BranchTrueFalseTotal
+ (isalnum(*q)?(4 + ln):0) 0 0 0
+ (isalnum(*q)?(4 + ln):0) 0 0 0
144 0 - QRinput_estimateBitsModeAn(q - string) /* - 4 - la */;
145 0 ▶ if(dif < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif < 0) { 0 0 0
146 0 break;
147 } else {
148 0 p = q;
149 }
150 } else {
151 0 p++;
152 }
153 }
154  
155 0 run = p - string;
156  
157 0 ▶ if(*p && !isalnum(*p)) {
Branch: 3 branches
BranchTrueFalseTotal
if(*p && !isalnum(*p)) { 0 0 0
if(*p && !isalnum(*p)) { 0 0 0
if(*p && !isalnum(*p)) { 0 0 0
158 0 dif = QRinput_estimateBitsModeAn(run) + 4 + la
159 0 + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
160 0 - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
161 0 ▶ if(dif > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif > 0) { 0 0 0
162 0 return Split_eat8(string, input, hint);
163 }
164 }
165  
166 0 ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
167 0 ▶ if(ret < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(ret < 0) return -1; 0 0 0
168  
169 0 return run;
170 }
171  
172 0 static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
173 {
174 const char *p;
175 int ret;
176 int run;
177  
178 0 p = string;
179 0 ▶ while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
Branch: 1 branch
BranchTrueFalseTotal
while(Split_identifyMode(p, hint) == QR_MODE_KANJI) { 0 0 0
180 0 p += 2;
181 }
182 0 run = p - string;
183 0 ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
184 0 ▶ if(ret < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(ret < 0) return -1; 0 0 0
185  
186 0 return run;
187 }
188  
189 0 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
190 {
191 const char *p, *q;
192 QRencodeMode mode;
193 int ret;
194 int run;
195 int dif;
196 int la, ln, l8;
197 int swcost;
198  
199 0 la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
200 0 ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
201 0 l8 = QRspec_lengthIndicator(QR_MODE_8, input->version);
202  
203 0 p = string + 1;
204 0 ▶ while(*p != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
while(*p != '\0') { 0 0 0
205 0 mode = Split_identifyMode(p, hint);
206 0 ▶ if(mode == QR_MODE_KANJI) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_KANJI) { 0 0 0
207 0 break;
208 }
209 0 ▶ if(mode == QR_MODE_NUM) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_NUM) { 0 0 0
210 0 q = p;
211 0 ▶ while(isdigit(*q)) {
Branch: 1 branch
BranchTrueFalseTotal
while(isdigit(*q)) { 0 0 0
212 0 q++;
213 }
214 0 ▶ if(Split_identifyMode(q, hint) == QR_MODE_8) {
Branch: 1 branch
BranchTrueFalseTotal
if(Split_identifyMode(q, hint) == QR_MODE_8) { 0 0 0
215 0 swcost = 4 + l8;
216 } else {
217 0 swcost = 0;
218 }
219 0 dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
220 0 + QRinput_estimateBitsModeNum(q - p) + 4 + ln
221 0 + swcost
222 0 - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
223 0 ▶ if(dif < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif < 0) { 0 0 0
224 0 break;
225 } else {
226 0 p = q;
227 }
228 0 ▶ } else if(mode == QR_MODE_AN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(mode == QR_MODE_AN) { 0 0 0
229 0 q = p;
230 0 ▶ while(isalnum(*q)) {
Branch: 2 branches
BranchTrueFalseTotal
while(isalnum(*q)) { 0 0 0
while(isalnum(*q)) { 0 0 0
231 0 q++;
232 }
233 0 ▶ if(Split_identifyMode(q, hint) == QR_MODE_8) {
Branch: 1 branch
BranchTrueFalseTotal
if(Split_identifyMode(q, hint) == QR_MODE_8) { 0 0 0
234 0 swcost = 4 + l8;
235 } else {
236 0 swcost = 0;
237 }
238 0 dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
239 0 + QRinput_estimateBitsModeAn(q - p) + 4 + la
240 0 + swcost
241 0 - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
242 0 ▶ if(dif < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(dif < 0) { 0 0 0
243 0 break;
244 } else {
245 0 p = q;
246 }
247 } else {
248 0 p++;
249 }
250 }
251  
252 0 run = p - string;
253 0 ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
254 0 ▶ if(ret < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(ret < 0) return -1; 0 0 0
255  
256 0 return run;
257 }
258  
259 0 static int Split_splitString(const char *string, QRinput *input,
260 QRencodeMode hint)
261 {
262 int length;
263 QRencodeMode mode;
264  
265 0 ▶ if(*string == '\0') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(*string == '\0') return 0; 0 0 0
266  
267 0 mode = Split_identifyMode(string, hint);
268 0 ▶ if(mode == QR_MODE_NUM) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_NUM) { 0 0 0
269 0 length = Split_eatNum(string, input, hint);
270 0 ▶ } else if(mode == QR_MODE_AN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(mode == QR_MODE_AN) { 0 0 0
271 0 length = Split_eatAn(string, input, hint);
272 0 ▶ } else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) { 0 0 0
} else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) { 0 0 0
273 0 length = Split_eatKanji(string, input, hint);
274 } else {
275 0 length = Split_eat8(string, input, hint);
276 }
277 0 ▶ if(length == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(length == 0) return 0; 0 0 0
278 0 ▶ if(length < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(length < 0) return -1; 0 0 0
279 0 return Split_splitString(&string[length], input, hint);
280 }
281  
282 0 static char *dupAndToUpper(const char *str, QRencodeMode hint)
283 {
284 char *newstr, *p;
285 QRencodeMode mode;
286  
287 0 newstr = strdup(str);
288 0 ▶ if(newstr == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if(newstr == NULL) return NULL; 0 0 0
289  
290 0 p = newstr;
291 0 ▶ while(*p != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
while(*p != '\0') { 0 0 0
292 0 mode = Split_identifyMode(p, hint);
293 0 ▶ if(mode == QR_MODE_KANJI) {
Branch: 1 branch
BranchTrueFalseTotal
if(mode == QR_MODE_KANJI) { 0 0 0
294 0 p += 2;
295 } else {
296 0 ▶ if (*p >= 'a' && *p <= 'z') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p >= 'a' && *p <= 'z') { 0 0 0
if (*p >= 'a' && *p <= 'z') { 0 0 0
297 0 *p = (char)((int)*p - 32);
298 }
299 0 p++;
300 }
301 }
302  
303 0 return newstr;
304 }
305  
306 0 int Split_splitStringToQRinput(const char *string, QRinput *input,
307 QRencodeMode hint, int casesensitive)
308 {
309 char *newstr;
310 int ret;
311  
312 0 ▶ if(string == NULL || *string == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if(string == NULL || *string == '\0') { 0 0 0
if(string == NULL || *string == '\0') { 0 0 0
313 0 errno = EINVAL;
314 0 return -1;
315 }
316 0 ▶ if(!casesensitive) {
Branch: 1 branch
BranchTrueFalseTotal
if(!casesensitive) { 0 0 0
317 0 newstr = dupAndToUpper(string, hint);
318 0 ▶ if(newstr == NULL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if(newstr == NULL) return -1; 0 0 0
319 0 ret = Split_splitString(newstr, input, hint);
320 0 free(newstr);
321 } else {
322 0 ret = Split_splitString(string, input, hint);
323 }
324  
325 0 return ret;
326 }
« src/mqrspec.c ↑ Summary src/qrinput.c »