TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
split.c
Go to the documentation of this file.
1 /*
2  * qrencode - QR Code encoder
3  *
4  * Input data splitter.
5  * Copyright (C) 2006-2017 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 #include "config.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include "qrencode.h"
33 #include "qrinput.h"
34 #include "qrspec.h"
35 #include "split.h"
36 
37 #define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
38 #define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
39 
40 #if !HAVE_STRDUP
41 #undef strdup
42 char *strdup(const char *s)
43 {
44  size_t len = strlen(s) + 1;
45  void *newstring = malloc(len);
46  if(newstring == NULL) return NULL;
47  return (char *)memcpy(newstring, s, len);
48 }
49 #endif
50 
51 static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
52 {
53  unsigned char c, d;
54  unsigned int word;
55 
56  c = (unsigned char)string[0];
57 
58  if(c == '\0') return QR_MODE_NUL;
59  if(isdigit(c)) {
60  return QR_MODE_NUM;
61  } else if(isalnum(c)) {
62  return QR_MODE_AN;
63  } else if(hint == QR_MODE_KANJI) {
64  d = (unsigned char)string[1];
65  if(d != '\0') {
66  word = ((unsigned int)c << 8) | d;
67  if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
68  return QR_MODE_KANJI;
69  }
70  }
71  }
72 
73  return QR_MODE_8;
74 }
75 
76 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
77 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
78 
79 static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
80 {
81  const char *p;
82  int ret;
83  int run;
84  int dif;
85  int ln;
87 
89 
90  p = string;
91  while(isdigit(*p)) {
92  p++;
93  }
94  run = (int)(p - string);
95  mode = Split_identifyMode(p, hint);
96  if(mode == QR_MODE_8) {
97  dif = QRinput_estimateBitsModeNum(run) + 4 + ln
98  + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
99  - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
100  if(dif > 0) {
101  return Split_eat8(string, input, hint);
102  }
103  }
104  if(mode == QR_MODE_AN) {
105  dif = QRinput_estimateBitsModeNum(run) + 4 + ln
106  + QRinput_estimateBitsModeAn(1) /* + 4 + la */
107  - QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
108  if(dif > 0) {
109  return Split_eatAn(string, input, hint);
110  }
111  }
112 
113  ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
114  if(ret < 0) return -1;
115 
116  return run;
117 }
118 
119 static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
120 {
121  const char *p, *q;
122  int ret;
123  int run;
124  int dif;
125  int la, ln;
126 
129 
130  p = string;
131  while(isalnum(*p)) {
132  if(isdigit(*p)) {
133  q = p;
134  while(isdigit(*q)) {
135  q++;
136  }
137  dif = QRinput_estimateBitsModeAn((int)(p - string)) /* + 4 + la */
138  + QRinput_estimateBitsModeNum((int)(q - p)) + 4 + ln
139  + (isalnum(*q)?(4 + ln):0)
140  - QRinput_estimateBitsModeAn((int)(q - string)) /* - 4 - la */;
141  if(dif < 0) {
142  break;
143  }
144  p = q;
145  } else {
146  p++;
147  }
148  }
149 
150  run = (int)(p - string);
151 
152  if(*p && !isalnum(*p)) {
153  dif = QRinput_estimateBitsModeAn(run) + 4 + la
154  + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
155  - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
156  if(dif > 0) {
157  return Split_eat8(string, input, hint);
158  }
159  }
160 
161  ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
162  if(ret < 0) return -1;
163 
164  return run;
165 }
166 
167 static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
168 {
169  const char *p;
170  int ret;
171  int run;
172 
173  p = string;
174  while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
175  p += 2;
176  }
177  run = (int)(p - string);
178  ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
179  if(ret < 0) return -1;
180 
181  return run;
182 }
183 
184 static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
185 {
186  const char *p, *q;
188  int ret;
189  int run;
190  int dif;
191  int la, ln, l8;
192  int swcost;
193 
197 
198  p = string + 1;
199  while(*p != '\0') {
200  mode = Split_identifyMode(p, hint);
201  if(mode == QR_MODE_KANJI) {
202  break;
203  }
204  if(mode == QR_MODE_NUM) {
205  q = p;
206  while(isdigit(*q)) {
207  q++;
208  }
209  if(Split_identifyMode(q, hint) == QR_MODE_8) {
210  swcost = 4 + l8;
211  } else {
212  swcost = 0;
213  }
214  dif = QRinput_estimateBitsMode8((int)(p - string)) /* + 4 + l8 */
215  + QRinput_estimateBitsModeNum((int)(q - p)) + 4 + ln
216  + swcost
217  - QRinput_estimateBitsMode8((int)(q - string)) /* - 4 - l8 */;
218  if(dif < 0) {
219  break;
220  }
221  p = q;
222  } else if(mode == QR_MODE_AN) {
223  q = p;
224  while(isalnum(*q)) {
225  q++;
226  }
227  if(Split_identifyMode(q, hint) == QR_MODE_8) {
228  swcost = 4 + l8;
229  } else {
230  swcost = 0;
231  }
232  dif = QRinput_estimateBitsMode8((int)(p - string)) /* + 4 + l8 */
233  + QRinput_estimateBitsModeAn((int)(q - p)) + 4 + la
234  + swcost
235  - QRinput_estimateBitsMode8((int)(q - string)) /* - 4 - l8 */;
236  if(dif < 0) {
237  break;
238  }
239  p = q;
240  } else {
241  p++;
242  }
243  }
244 
245  run = (int)(p - string);
246  ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
247  if(ret < 0) return -1;
248 
249  return run;
250 }
251 
252 static int Split_splitString(const char *string, QRinput *input,
253  QRencodeMode hint)
254 {
255  int length;
257 
258  while(*string != '\0') {
259  mode = Split_identifyMode(string, hint);
260  if(mode == QR_MODE_NUM) {
261  length = Split_eatNum(string, input, hint);
262  } else if(mode == QR_MODE_AN) {
263  length = Split_eatAn(string, input, hint);
264  } else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
265  length = Split_eatKanji(string, input, hint);
266  } else {
267  length = Split_eat8(string, input, hint);
268  }
269  if(length == 0) break;
270  if(length < 0) return -1;
271  string += length;
272  }
273 
274  return 0;
275 }
276 
277 static char *dupAndToUpper(const char *str, QRencodeMode hint)
278 {
279  char *newstr, *p;
281 
282  newstr = strdup(str);
283  if(newstr == NULL) return NULL;
284 
285  p = newstr;
286  while(*p != '\0') {
287  mode = Split_identifyMode(p, hint);
288  if(mode == QR_MODE_KANJI) {
289  p += 2;
290  } else {
291  if (*p >= 'a' && *p <= 'z') {
292  *p = (char)((int)*p - 32);
293  }
294  p++;
295  }
296  }
297 
298  return newstr;
299 }
300 
301 int Split_splitStringToQRinput(const char *string, QRinput *input,
302  QRencodeMode hint, int casesensitive)
303 {
304  char *newstr;
305  int ret;
306 
307  if(string == NULL || *string == '\0') {
308  errno = EINVAL;
309  return -1;
310  }
311  if(!casesensitive) {
312  newstr = dupAndToUpper(string, hint);
313  if(newstr == NULL) return -1;
314  ret = Split_splitString(newstr, input, hint);
315  free(newstr);
316  } else {
317  ret = Split_splitString(string, input, hint);
318  }
319 
320  return ret;
321 }
int QRinput_estimateBitsModeNum(int size)
Estimate the length of the encoded bit stream of numeric data.
Definition: qrinput.c:379
#define isalnum(__c__)
Definition: split.c:38
EXTRAS_EXPORT int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
Append data to an input object.
Definition: qrinput.c:221
static int Split_eatNum(const char *string, QRinput *input, QRencodeMode hint)
Definition: split.c:79
Numeric mode.
Definition: qrencode.h:113
static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
Definition: split.c:184
int version
Definition: qrinput.h:47
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
static int Split_splitString(const char *string, QRinput *input, QRencodeMode hint)
Definition: split.c:252
static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
Definition: split.c:167
8-bit data mode
Definition: qrencode.h:115
int QRinput_estimateBitsModeAn(int size)
Estimate the length of the encoded bit stream of alphabet-numeric data.
Definition: qrinput.c:491
static char * dupAndToUpper(const char *str, QRencodeMode hint)
Definition: split.c:277
QRencodeMode
Encoding mode.
Definition: qrencode.h:111
char * strdup(const char *s)
Definition: split.c:42
int QRspec_lengthIndicator(QRencodeMode mode, int version)
Return the size of length indicator for the mode and version.
Definition: qrspec.c:140
Kanji (shift-jis) mode.
Definition: qrencode.h:116
Alphabet-numeric mode.
Definition: qrencode.h:114
static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
Definition: split.c:51
void free(voidpf ptr)
const char int mode
Definition: ioapi.h:135
int QRinput_estimateBitsMode8(int size)
Estimate the length of the encoded bit stream of 8 bit data.
Definition: qrinput.c:564
static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
Definition: split.c:119
voidp malloc(uInt size)
Terminator (NUL character). Internal use only.
Definition: qrencode.h:112
int Split_splitStringToQRinput(const char *string, QRinput *input, QRencodeMode hint, int casesensitive)
Split the input string (null terminated) into QRinput.
Definition: split.c:301
#define isdigit(__c__)
Definition: split.c:37