| 1 |
|
| /* |
| 2 |
|
| * qrencode - QR Code encoder |
| 3 |
|
| * |
| 4 |
|
| * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org> |
| 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 |
|
| |
| 21 |
|
| #if HAVE_CONFIG_H |
| 22 |
|
| # include "config.h" |
| 23 |
|
| #endif |
| 24 |
|
| #include <stdio.h> |
| 25 |
|
| #include <stdlib.h> |
| 26 |
|
| #include <string.h> |
| 27 |
|
| #include <errno.h> |
| 28 |
|
| |
| 29 |
|
| #include "qrencode.h" |
| 30 |
|
| #include "qrspec.h" |
| 31 |
|
| #include "mqrspec.h" |
| 32 |
|
| #include "bitstream.h" |
| 33 |
|
| #include "qrinput.h" |
| 34 |
|
| #include "rscode.h" |
| 35 |
|
| #include "split.h" |
| 36 |
|
| #include "mask.h" |
| 37 |
|
| #include "mmask.h" |
| 38 |
|
| |
| 39 |
|
| /****************************************************************************** |
| 40 |
|
| * Raw code |
| 41 |
|
| *****************************************************************************/ |
| 42 |
|
| |
| 43 |
|
| typedef struct { |
| 44 |
|
| int dataLength; |
| 45 |
|
| unsigned char *data; |
| 46 |
|
| int eccLength; |
| 47 |
|
| unsigned char *ecc; |
| 48 |
|
| } RSblock; |
| 49 |
|
| |
| 50 |
|
| typedef struct { |
| 51 |
|
| int version; |
| 52 |
|
| int dataLength; |
| 53 |
|
| int eccLength; |
| 54 |
|
| unsigned char *datacode; |
| 55 |
|
| unsigned char *ecccode; |
| 56 |
|
| int b1; |
| 57 |
|
| int blocks; |
| 58 |
|
| RSblock *rsblock; |
| 59 |
|
| int count; |
| 60 |
|
| } QRRawCode; |
| 61 |
|
| |
| 62 |
13 |
| static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs) |
| 63 |
|
| { |
| 64 |
13 |
| block->dataLength = dl; |
| 65 |
13 |
| block->data = data; |
| 66 |
13 |
| block->eccLength = el; |
| 67 |
13 |
| block->ecc = ecc; |
| 68 |
|
| |
| 69 |
13 |
| encode_rs_char(rs, data, ecc); |
| 70 |
13 |
| } |
| 71 |
|
| |
| 72 |
6 |
| static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc) |
| 73 |
|
| { |
| 74 |
|
| int i; |
| 75 |
|
| RSblock *block; |
| 76 |
|
| unsigned char *dp, *ep; |
| 77 |
|
| RS *rs; |
| 78 |
|
| int el, dl; |
| 79 |
|
| |
| 80 |
6 |
| dl = QRspec_rsDataCodes1(spec); |
| 81 |
6 |
| el = QRspec_rsEccCodes1(spec); |
| 82 |
6 |
| rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el); |
| 83 |
6 |
▶ | if(rs == NULL) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if(rs == NULL) return -1; |
0 |
6 |
6 |
|
| 84 |
|
| |
| 85 |
6 |
| block = blocks; |
| 86 |
6 |
| dp = data; |
| 87 |
6 |
| ep = ecc; |
| 88 |
15 |
▶ | for(i=0; i<QRspec_rsBlockNum1(spec); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<QRspec_rsBlockNum1(spec); i++) { |
9 |
6 |
15 |
|
| 89 |
9 |
| RSblock_initBlock(block, dl, dp, el, ep, rs); |
| 90 |
9 |
| dp += dl; |
| 91 |
9 |
| ep += el; |
| 92 |
9 |
| block++; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
6 |
▶ | if(QRspec_rsBlockNum2(spec) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if(QRspec_rsBlockNum2(spec) == 0) return 0; |
4 |
2 |
6 |
|
| 96 |
|
| |
| 97 |
2 |
| dl = QRspec_rsDataCodes2(spec); |
| 98 |
2 |
| el = QRspec_rsEccCodes2(spec); |
| 99 |
2 |
| rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el); |
| 100 |
2 |
▶ | if(rs == NULL) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if(rs == NULL) return -1; |
0 |
2 |
2 |
|
| 101 |
6 |
▶ | for(i=0; i<QRspec_rsBlockNum2(spec); i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<QRspec_rsBlockNum2(spec); i++) { |
4 |
2 |
6 |
|
| 102 |
4 |
| RSblock_initBlock(block, dl, dp, el, ep, rs); |
| 103 |
4 |
| dp += dl; |
| 104 |
4 |
| ep += el; |
| 105 |
4 |
| block++; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
2 |
| return 0; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| __STATIC void QRraw_free(QRRawCode *raw); |
| 112 |
6 |
| __STATIC QRRawCode *QRraw_new(QRinput *input) |
| 113 |
|
| { |
| 114 |
|
| QRRawCode *raw; |
| 115 |
|
| int spec[5], ret; |
| 116 |
|
| |
| 117 |
6 |
| raw = (QRRawCode *)malloc(sizeof(QRRawCode)); |
| 118 |
6 |
▶ | if(raw == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw == NULL) return NULL; |
0 |
6 |
6 |
|
| 119 |
|
| |
| 120 |
6 |
| raw->datacode = QRinput_getByteStream(input); |
| 121 |
6 |
▶ | if(raw->datacode == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->datacode == NULL) { |
0 |
6 |
6 |
|
| 122 |
0 |
| free(raw); |
| 123 |
0 |
| return NULL; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
6 |
| QRspec_getEccSpec(input->version, input->level, spec); |
| 127 |
|
| |
| 128 |
6 |
| raw->version = input->version; |
| 129 |
6 |
| raw->b1 = QRspec_rsBlockNum1(spec); |
| 130 |
6 |
| raw->dataLength = QRspec_rsDataLength(spec); |
| 131 |
6 |
| raw->eccLength = QRspec_rsEccLength(spec); |
| 132 |
6 |
| raw->ecccode = (unsigned char *)malloc(raw->eccLength); |
| 133 |
6 |
▶ | if(raw->ecccode == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->ecccode == NULL) { |
0 |
6 |
6 |
|
| 134 |
0 |
| free(raw->datacode); |
| 135 |
0 |
| free(raw); |
| 136 |
0 |
| return NULL; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
6 |
| raw->blocks = QRspec_rsBlockNum(spec); |
| 140 |
6 |
| raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock)); |
| 141 |
6 |
▶ | if(raw->rsblock == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->rsblock == NULL) { |
0 |
6 |
6 |
|
| 142 |
0 |
| QRraw_free(raw); |
| 143 |
0 |
| return NULL; |
| 144 |
|
| } |
| 145 |
6 |
| ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode); |
| 146 |
6 |
▶ | if(ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret < 0) { |
0 |
6 |
6 |
|
| 147 |
0 |
| QRraw_free(raw); |
| 148 |
0 |
| return NULL; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
6 |
| raw->count = 0; |
| 152 |
|
| |
| 153 |
6 |
| return raw; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| /** |
| 157 |
|
| * Return a code (byte). |
| 158 |
|
| * This function can be called iteratively. |
| 159 |
|
| * @param raw raw code. |
| 160 |
|
| * @return code |
| 161 |
|
| */ |
| 162 |
758 |
| __STATIC unsigned char QRraw_getCode(QRRawCode *raw) |
| 163 |
|
| { |
| 164 |
|
| int col, row; |
| 165 |
|
| unsigned char ret; |
| 166 |
|
| |
| 167 |
758 |
▶ | if(raw->count < raw->dataLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->count < raw->dataLength) { |
515 |
243 |
758 |
|
| 168 |
515 |
| row = raw->count % raw->blocks; |
| 169 |
515 |
| col = raw->count / raw->blocks; |
| 170 |
515 |
▶ | if(col >= raw->rsblock[0].dataLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(col >= raw->rsblock[0].dataLength) { |
4 |
511 |
515 |
|
| 171 |
4 |
| row += raw->b1; |
| 172 |
|
| } |
| 173 |
515 |
| ret = raw->rsblock[row].data[col]; |
| 174 |
243 |
▶ | } else if(raw->count < raw->dataLength + raw->eccLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(raw->count < raw->dataLength + raw->eccLength) { |
243 |
0 |
243 |
|
| 175 |
243 |
| row = (raw->count - raw->dataLength) % raw->blocks; |
| 176 |
243 |
| col = (raw->count - raw->dataLength) / raw->blocks; |
| 177 |
243 |
| ret = raw->rsblock[row].ecc[col]; |
| 178 |
|
| } else { |
| 179 |
0 |
| return 0; |
| 180 |
|
| } |
| 181 |
758 |
| raw->count++; |
| 182 |
758 |
| return ret; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
12 |
| __STATIC void QRraw_free(QRRawCode *raw) |
| 186 |
|
| { |
| 187 |
12 |
▶ | if(raw != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw != NULL) { |
6 |
6 |
12 |
|
| 188 |
6 |
| free(raw->datacode); |
| 189 |
6 |
| free(raw->ecccode); |
| 190 |
6 |
| free(raw->rsblock); |
| 191 |
6 |
| free(raw); |
| 192 |
|
| } |
| 193 |
12 |
| } |
| 194 |
|
| |
| 195 |
|
| /****************************************************************************** |
| 196 |
|
| * Raw code for Micro QR Code |
| 197 |
|
| *****************************************************************************/ |
| 198 |
|
| |
| 199 |
|
| typedef struct { |
| 200 |
|
| int version; |
| 201 |
|
| int dataLength; |
| 202 |
|
| int eccLength; |
| 203 |
|
| unsigned char *datacode; |
| 204 |
|
| unsigned char *ecccode; |
| 205 |
|
| RSblock *rsblock; |
| 206 |
|
| int oddbits; |
| 207 |
|
| int count; |
| 208 |
|
| } MQRRawCode; |
| 209 |
|
| |
| 210 |
|
| __STATIC void MQRraw_free(MQRRawCode *raw); |
| 211 |
0 |
| __STATIC MQRRawCode *MQRraw_new(QRinput *input) |
| 212 |
|
| { |
| 213 |
|
| MQRRawCode *raw; |
| 214 |
|
| RS *rs; |
| 215 |
|
| |
| 216 |
0 |
| raw = (MQRRawCode *)malloc(sizeof(MQRRawCode)); |
| 217 |
0 |
▶ | if(raw == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw == NULL) return NULL; |
0 |
0 |
0 |
|
| 218 |
|
| |
| 219 |
0 |
| raw->version = input->version; |
| 220 |
0 |
| raw->dataLength = MQRspec_getDataLength(input->version, input->level); |
| 221 |
0 |
| raw->eccLength = MQRspec_getECCLength(input->version, input->level); |
| 222 |
0 |
| raw->oddbits = raw->dataLength * 8 - MQRspec_getDataLengthBit(input->version, input->level); |
| 223 |
0 |
| raw->datacode = QRinput_getByteStream(input); |
| 224 |
0 |
▶ | if(raw->datacode == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->datacode == NULL) { |
0 |
0 |
0 |
|
| 225 |
0 |
| free(raw); |
| 226 |
0 |
| return NULL; |
| 227 |
|
| } |
| 228 |
0 |
| raw->ecccode = (unsigned char *)malloc(raw->eccLength); |
| 229 |
0 |
▶ | if(raw->ecccode == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->ecccode == NULL) { |
0 |
0 |
0 |
|
| 230 |
0 |
| free(raw->datacode); |
| 231 |
0 |
| free(raw); |
| 232 |
0 |
| return NULL; |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
0 |
| raw->rsblock = (RSblock *)calloc(1, sizeof(RSblock)); |
| 236 |
0 |
▶ | if(raw->rsblock == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->rsblock == NULL) { |
0 |
0 |
0 |
|
| 237 |
0 |
| MQRraw_free(raw); |
| 238 |
0 |
| return NULL; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
0 |
| rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - raw->dataLength - raw->eccLength); |
| 242 |
0 |
▶ | if(rs == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(rs == NULL) { |
0 |
0 |
0 |
|
| 243 |
0 |
| MQRraw_free(raw); |
| 244 |
0 |
| return NULL; |
| 245 |
|
| } |
| 246 |
|
| |
| 247 |
0 |
| RSblock_initBlock(raw->rsblock, raw->dataLength, raw->datacode, raw->eccLength, raw->ecccode, rs); |
| 248 |
|
| |
| 249 |
0 |
| raw->count = 0; |
| 250 |
|
| |
| 251 |
0 |
| return raw; |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
|
| /** |
| 255 |
|
| * Return a code (byte). |
| 256 |
|
| * This function can be called iteratively. |
| 257 |
|
| * @param raw raw code. |
| 258 |
|
| * @return code |
| 259 |
|
| */ |
| 260 |
0 |
| __STATIC unsigned char MQRraw_getCode(MQRRawCode *raw) |
| 261 |
|
| { |
| 262 |
|
| unsigned char ret; |
| 263 |
|
| |
| 264 |
0 |
▶ | if(raw->count < raw->dataLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw->count < raw->dataLength) { |
0 |
0 |
0 |
|
| 265 |
0 |
| ret = raw->datacode[raw->count]; |
| 266 |
0 |
▶ | } else if(raw->count < raw->dataLength + raw->eccLength) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(raw->count < raw->dataLength + raw->eccLength) { |
0 |
0 |
0 |
|
| 267 |
0 |
| ret = raw->ecccode[raw->count - raw->dataLength]; |
| 268 |
|
| } else { |
| 269 |
0 |
| return 0; |
| 270 |
|
| } |
| 271 |
0 |
| raw->count++; |
| 272 |
0 |
| return ret; |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
0 |
| __STATIC void MQRraw_free(MQRRawCode *raw) |
| 276 |
|
| { |
| 277 |
0 |
▶ | if(raw != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw != NULL) { |
0 |
0 |
0 |
|
| 278 |
0 |
| free(raw->datacode); |
| 279 |
0 |
| free(raw->ecccode); |
| 280 |
0 |
| free(raw->rsblock); |
| 281 |
0 |
| free(raw); |
| 282 |
|
| } |
| 283 |
0 |
| } |
| 284 |
|
| |
| 285 |
|
| |
| 286 |
|
| /****************************************************************************** |
| 287 |
|
| * Frame filling |
| 288 |
|
| *****************************************************************************/ |
| 289 |
|
| |
| 290 |
|
| typedef struct { |
| 291 |
|
| int width; |
| 292 |
|
| unsigned char *frame; |
| 293 |
|
| int x, y; |
| 294 |
|
| int dir; |
| 295 |
|
| int bit; |
| 296 |
|
| int mqr; |
| 297 |
|
| } FrameFiller; |
| 298 |
|
| |
| 299 |
6 |
| static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr) |
| 300 |
|
| { |
| 301 |
|
| FrameFiller *filler; |
| 302 |
|
| |
| 303 |
6 |
| filler = (FrameFiller *)malloc(sizeof(FrameFiller)); |
| 304 |
6 |
▶ | if(filler == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler == NULL) return NULL; |
0 |
6 |
6 |
|
| 305 |
6 |
| filler->width = width; |
| 306 |
6 |
| filler->frame = frame; |
| 307 |
6 |
| filler->x = width - 1; |
| 308 |
6 |
| filler->y = width - 1; |
| 309 |
6 |
| filler->dir = -1; |
| 310 |
6 |
| filler->bit = -1; |
| 311 |
6 |
| filler->mqr = mqr; |
| 312 |
|
| |
| 313 |
6 |
| return filler; |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
7722 |
| static unsigned char *FrameFiller_next(FrameFiller *filler) |
| 317 |
|
| { |
| 318 |
|
| unsigned char *p; |
| 319 |
|
| int x, y, w; |
| 320 |
|
| |
| 321 |
7722 |
▶ | if(filler->bit == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler->bit == -1) { |
6 |
7716 |
7722 |
|
| 322 |
6 |
| filler->bit = 0; |
| 323 |
6 |
| return filler->frame + filler->y * filler->width + filler->x; |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
7716 |
| x = filler->x; |
| 327 |
7716 |
| y = filler->y; |
| 328 |
7716 |
| p = filler->frame; |
| 329 |
7716 |
| w = filler->width; |
| 330 |
|
| |
| 331 |
7716 |
▶ | if(filler->bit == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler->bit == 0) { |
3861 |
3855 |
7716 |
|
| 332 |
3861 |
| x--; |
| 333 |
3861 |
| filler->bit++; |
| 334 |
|
| } else { |
| 335 |
3855 |
| x++; |
| 336 |
3855 |
| y += filler->dir; |
| 337 |
3855 |
| filler->bit--; |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
7716 |
▶ | if(filler->dir < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler->dir < 0) { |
3972 |
3744 |
7716 |
|
| 341 |
3972 |
▶ | if(y < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(y < 0) { |
50 |
3922 |
3972 |
|
| 342 |
50 |
| y = 0; |
| 343 |
50 |
| x -= 2; |
| 344 |
50 |
| filler->dir = 1; |
| 345 |
50 |
▶ | if(!filler->mqr && x == 6) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(!filler->mqr && x == 6) { |
50 |
0 |
50 |
| if(!filler->mqr && x == 6) { |
6 |
44 |
50 |
|
| 346 |
6 |
| x--; |
| 347 |
6 |
| y = 9; |
| 348 |
|
| } |
| 349 |
|
| } |
| 350 |
|
| } else { |
| 351 |
3744 |
▶ | if(y == w) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(y == w) { |
44 |
3700 |
3744 |
|
| 352 |
44 |
| y = w - 1; |
| 353 |
44 |
| x -= 2; |
| 354 |
44 |
| filler->dir = -1; |
| 355 |
44 |
▶ | if(!filler->mqr && x == 6) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(!filler->mqr && x == 6) { |
44 |
0 |
44 |
| if(!filler->mqr && x == 6) { |
0 |
44 |
44 |
|
| 356 |
0 |
| x--; |
| 357 |
0 |
| y -= 8; |
| 358 |
|
| } |
| 359 |
|
| } |
| 360 |
|
| } |
| 361 |
7716 |
▶ | if(x < 0 || y < 0) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if(x < 0 || y < 0) return NULL; |
7716 |
0 |
7716 |
| if(x < 0 || y < 0) return NULL; |
0 |
7716 |
7716 |
|
| 362 |
|
| |
| 363 |
7716 |
| filler->x = x; |
| 364 |
7716 |
| filler->y = y; |
| 365 |
|
| |
| 366 |
7716 |
▶ | if(p[y * w + x] & 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(p[y * w + x] & 0x80) { |
1658 |
6058 |
7716 |
|
| 367 |
|
| // This tail recursion could be optimized. |
| 368 |
1658 |
| return FrameFiller_next(filler); |
| 369 |
|
| } |
| 370 |
6058 |
| return &p[y * w + x]; |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
|
| #ifdef WITH_TESTS |
| 374 |
|
| extern unsigned char *FrameFiller_test(int version) |
| 375 |
|
| { |
| 376 |
|
| int width; |
| 377 |
|
| unsigned char *frame, *p; |
| 378 |
|
| FrameFiller *filler; |
| 379 |
|
| int i, length; |
| 380 |
|
| |
| 381 |
|
| width = QRspec_getWidth(version); |
| 382 |
|
| frame = QRspec_newFrame(version); |
| 383 |
|
| if(frame == NULL) return NULL; |
| 384 |
|
| filler = FrameFiller_new(width, frame, 0); |
| 385 |
|
| if(filler == NULL) { |
| 386 |
|
| free(frame); |
| 387 |
|
| return NULL; |
| 388 |
|
| } |
| 389 |
|
| length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8 |
| 390 |
|
| + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8 |
| 391 |
|
| + QRspec_getRemainder(version); |
| 392 |
|
| for(i=0; i<length; i++) { |
| 393 |
|
| p = FrameFiller_next(filler); |
| 394 |
|
| if(p == NULL) { |
| 395 |
|
| free(filler); |
| 396 |
|
| free(frame); |
| 397 |
|
| return NULL; |
| 398 |
|
| } |
| 399 |
|
| *p = (unsigned char)(i & 0x7f) | 0x80; |
| 400 |
|
| } |
| 401 |
|
| free(filler); |
| 402 |
|
| return frame; |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| extern unsigned char *FrameFiller_testMQR(int version) |
| 406 |
|
| { |
| 407 |
|
| int width; |
| 408 |
|
| unsigned char *frame, *p; |
| 409 |
|
| FrameFiller *filler; |
| 410 |
|
| int i, length; |
| 411 |
|
| |
| 412 |
|
| width = MQRspec_getWidth(version); |
| 413 |
|
| frame = MQRspec_newFrame(version); |
| 414 |
|
| if(frame == NULL) return NULL; |
| 415 |
|
| filler = FrameFiller_new(width, frame, 1); |
| 416 |
|
| if(filler == NULL) { |
| 417 |
|
| free(frame); |
| 418 |
|
| return NULL; |
| 419 |
|
| } |
| 420 |
|
| length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L) |
| 421 |
|
| + MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8; |
| 422 |
|
| for(i=0; i<length; i++) { |
| 423 |
|
| p = FrameFiller_next(filler); |
| 424 |
|
| if(p == NULL) { |
| 425 |
|
| fprintf(stderr, "Frame filler run over the frame!\n"); |
| 426 |
|
| free(filler); |
| 427 |
|
| return frame; |
| 428 |
|
| } |
| 429 |
|
| *p = (unsigned char)(i & 0x7f) | 0x80; |
| 430 |
|
| } |
| 431 |
|
| free(filler); |
| 432 |
|
| return frame; |
| 433 |
|
| } |
| 434 |
|
| #endif |
| 435 |
|
| |
| 436 |
|
| |
| 437 |
|
| /****************************************************************************** |
| 438 |
|
| * QR-code encoding |
| 439 |
|
| *****************************************************************************/ |
| 440 |
|
| |
| 441 |
6 |
| __STATIC QRcode *QRcode_new(int version, int width, unsigned char *data) |
| 442 |
|
| { |
| 443 |
|
| QRcode *qrcode; |
| 444 |
|
| |
| 445 |
6 |
| qrcode = (QRcode *)malloc(sizeof(QRcode)); |
| 446 |
6 |
▶ | if(qrcode == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(qrcode == NULL) return NULL; |
0 |
6 |
6 |
|
| 447 |
|
| |
| 448 |
6 |
| qrcode->version = version; |
| 449 |
6 |
| qrcode->width = width; |
| 450 |
6 |
| qrcode->data = data; |
| 451 |
|
| |
| 452 |
6 |
| return qrcode; |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
6 |
| void QRcode_free(QRcode *qrcode) |
| 456 |
|
| { |
| 457 |
6 |
▶ | if(qrcode != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(qrcode != NULL) { |
6 |
0 |
6 |
|
| 458 |
6 |
| free(qrcode->data); |
| 459 |
6 |
| free(qrcode); |
| 460 |
|
| } |
| 461 |
6 |
| } |
| 462 |
|
| |
| 463 |
6 |
| __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask) |
| 464 |
|
| { |
| 465 |
|
| int width, version; |
| 466 |
|
| QRRawCode *raw; |
| 467 |
|
| unsigned char *frame, *masked, *p, code, bit; |
| 468 |
|
| FrameFiller *filler; |
| 469 |
|
| int i, j; |
| 470 |
6 |
| QRcode *qrcode = NULL; |
| 471 |
|
| |
| 472 |
6 |
▶ | if(input->mqr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input->mqr) { |
0 |
6 |
6 |
|
| 473 |
0 |
| errno = EINVAL; |
| 474 |
0 |
| return NULL; |
| 475 |
|
| } |
| 476 |
6 |
▶ | if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) { |
6 |
0 |
6 |
| if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) { |
0 |
6 |
6 |
|
| 477 |
0 |
| errno = EINVAL; |
| 478 |
0 |
| return NULL; |
| 479 |
|
| } |
| 480 |
6 |
▶ | if(input->level > QR_ECLEVEL_H) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input->level > QR_ECLEVEL_H) { |
0 |
6 |
6 |
|
| 481 |
0 |
| errno = EINVAL; |
| 482 |
0 |
| return NULL; |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
6 |
| raw = QRraw_new(input); |
| 486 |
6 |
▶ | if(raw == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw == NULL) return NULL; |
0 |
6 |
6 |
|
| 487 |
|
| |
| 488 |
6 |
| version = raw->version; |
| 489 |
6 |
| width = QRspec_getWidth(version); |
| 490 |
6 |
| frame = QRspec_newFrame(version); |
| 491 |
6 |
▶ | if(frame == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(frame == NULL) { |
0 |
6 |
6 |
|
| 492 |
0 |
| QRraw_free(raw); |
| 493 |
0 |
| return NULL; |
| 494 |
|
| } |
| 495 |
6 |
| filler = FrameFiller_new(width, frame, 0); |
| 496 |
6 |
▶ | if(filler == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler == NULL) { |
0 |
6 |
6 |
|
| 497 |
0 |
| QRraw_free(raw); |
| 498 |
0 |
| free(frame); |
| 499 |
0 |
| return NULL; |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
|
| /* inteleaved data and ecc codes */ |
| 503 |
764 |
▶ | for(i=0; i<raw->dataLength + raw->eccLength; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<raw->dataLength + raw->eccLength; i++) { |
758 |
6 |
764 |
|
| 504 |
758 |
| code = QRraw_getCode(raw); |
| 505 |
758 |
| bit = 0x80; |
| 506 |
6822 |
▶ | for(j=0; j<8; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<8; j++) { |
6064 |
758 |
6822 |
|
| 507 |
6064 |
| p = FrameFiller_next(filler); |
| 508 |
6064 |
▶ | if(p == NULL) goto EXIT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p == NULL) goto EXIT; |
0 |
6064 |
6064 |
|
| 509 |
6064 |
▶ | *p = 0x02 | ((bit & code) != 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *p = 0x02 | ((bit & code) != 0); |
2708 |
3356 |
6064 |
|
| 510 |
6064 |
| bit = bit >> 1; |
| 511 |
|
| } |
| 512 |
|
| } |
| 513 |
6 |
| QRraw_free(raw); |
| 514 |
6 |
| raw = NULL; |
| 515 |
|
| /* remainder bits */ |
| 516 |
6 |
| j = QRspec_getRemainder(version); |
| 517 |
6 |
▶ | for(i=0; i<j; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<j; i++) { |
0 |
6 |
6 |
|
| 518 |
0 |
| p = FrameFiller_next(filler); |
| 519 |
0 |
▶ | if(p == NULL) goto EXIT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p == NULL) goto EXIT; |
0 |
0 |
0 |
|
| 520 |
0 |
| *p = 0x02; |
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| /* masking */ |
| 524 |
6 |
▶ | if(mask == -2) { // just for debug purpose |
Branch: 1 branch
| Branch | True | False | Total |
| if(mask == -2) { // just for debug purpose |
0 |
6 |
6 |
|
| 525 |
0 |
| masked = (unsigned char *)malloc(width * width); |
| 526 |
0 |
| memcpy(masked, frame, width * width); |
| 527 |
6 |
▶ | } else if(mask < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(mask < 0) { |
6 |
0 |
6 |
|
| 528 |
6 |
| masked = Mask_mask(width, frame, input->level); |
| 529 |
|
| } else { |
| 530 |
0 |
| masked = Mask_makeMask(width, frame, mask, input->level); |
| 531 |
|
| } |
| 532 |
6 |
▶ | if(masked == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(masked == NULL) { |
0 |
6 |
6 |
|
| 533 |
0 |
| goto EXIT; |
| 534 |
|
| } |
| 535 |
6 |
| qrcode = QRcode_new(version, width, masked); |
| 536 |
|
| |
| 537 |
6 |
| EXIT: |
| 538 |
6 |
| QRraw_free(raw); |
| 539 |
6 |
| free(filler); |
| 540 |
6 |
| free(frame); |
| 541 |
6 |
| return qrcode; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
0 |
| __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) |
| 545 |
|
| { |
| 546 |
|
| int width, version; |
| 547 |
|
| MQRRawCode *raw; |
| 548 |
|
| unsigned char *frame, *masked, *p, code, bit; |
| 549 |
|
| FrameFiller *filler; |
| 550 |
|
| int i, j; |
| 551 |
0 |
| QRcode *qrcode = NULL; |
| 552 |
|
| |
| 553 |
0 |
▶ | if(!input->mqr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!input->mqr) { |
0 |
0 |
0 |
|
| 554 |
0 |
| errno = EINVAL; |
| 555 |
0 |
| return NULL; |
| 556 |
|
| } |
| 557 |
0 |
▶ | if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) { |
0 |
0 |
0 |
| if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) { |
0 |
0 |
0 |
|
| 558 |
0 |
| errno = EINVAL; |
| 559 |
0 |
| return NULL; |
| 560 |
|
| } |
| 561 |
0 |
▶ | if(input->level > QR_ECLEVEL_Q) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input->level > QR_ECLEVEL_Q) { |
0 |
0 |
0 |
|
| 562 |
0 |
| errno = EINVAL; |
| 563 |
0 |
| return NULL; |
| 564 |
|
| } |
| 565 |
|
| |
| 566 |
0 |
| raw = MQRraw_new(input); |
| 567 |
0 |
▶ | if(raw == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(raw == NULL) return NULL; |
0 |
0 |
0 |
|
| 568 |
|
| |
| 569 |
0 |
| version = raw->version; |
| 570 |
0 |
| width = MQRspec_getWidth(version); |
| 571 |
0 |
| frame = MQRspec_newFrame(version); |
| 572 |
0 |
▶ | if(frame == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(frame == NULL) { |
0 |
0 |
0 |
|
| 573 |
0 |
| MQRraw_free(raw); |
| 574 |
0 |
| return NULL; |
| 575 |
|
| } |
| 576 |
0 |
| filler = FrameFiller_new(width, frame, 1); |
| 577 |
0 |
▶ | if(filler == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(filler == NULL) { |
0 |
0 |
0 |
|
| 578 |
0 |
| MQRraw_free(raw); |
| 579 |
0 |
| free(frame); |
| 580 |
0 |
| return NULL; |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| /* inteleaved data and ecc codes */ |
| 584 |
0 |
▶ | for(i=0; i<raw->dataLength + raw->eccLength; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<raw->dataLength + raw->eccLength; i++) { |
0 |
0 |
0 |
|
| 585 |
0 |
| code = MQRraw_getCode(raw); |
| 586 |
0 |
▶ | if(raw->oddbits && i == raw->dataLength - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(raw->oddbits && i == raw->dataLength - 1) { |
0 |
0 |
0 |
| if(raw->oddbits && i == raw->dataLength - 1) { |
0 |
0 |
0 |
|
| 587 |
0 |
| bit = 1 << (raw->oddbits - 1); |
| 588 |
0 |
▶ | for(j=0; j<raw->oddbits; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<raw->oddbits; j++) { |
0 |
0 |
0 |
|
| 589 |
0 |
| p = FrameFiller_next(filler); |
| 590 |
0 |
▶ | if(p == NULL) goto EXIT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p == NULL) goto EXIT; |
0 |
0 |
0 |
|
| 591 |
0 |
▶ | *p = 0x02 | ((bit & code) != 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *p = 0x02 | ((bit & code) != 0); |
0 |
0 |
0 |
|
| 592 |
0 |
| bit = bit >> 1; |
| 593 |
|
| } |
| 594 |
|
| } else { |
| 595 |
0 |
| bit = 0x80; |
| 596 |
0 |
▶ | for(j=0; j<8; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(j=0; j<8; j++) { |
0 |
0 |
0 |
|
| 597 |
0 |
| p = FrameFiller_next(filler); |
| 598 |
0 |
▶ | if(p == NULL) goto EXIT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(p == NULL) goto EXIT; |
0 |
0 |
0 |
|
| 599 |
0 |
▶ | *p = 0x02 | ((bit & code) != 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *p = 0x02 | ((bit & code) != 0); |
0 |
0 |
0 |
|
| 600 |
0 |
| bit = bit >> 1; |
| 601 |
|
| } |
| 602 |
|
| } |
| 603 |
|
| } |
| 604 |
0 |
| MQRraw_free(raw); |
| 605 |
0 |
| raw = NULL; |
| 606 |
|
| |
| 607 |
|
| /* masking */ |
| 608 |
0 |
▶ | if(mask < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mask < 0) { |
0 |
0 |
0 |
|
| 609 |
0 |
| masked = MMask_mask(version, frame, input->level); |
| 610 |
|
| } else { |
| 611 |
0 |
| masked = MMask_makeMask(version, frame, mask, input->level); |
| 612 |
|
| } |
| 613 |
0 |
▶ | if(masked == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(masked == NULL) { |
0 |
0 |
0 |
|
| 614 |
0 |
| goto EXIT; |
| 615 |
|
| } |
| 616 |
|
| |
| 617 |
0 |
| qrcode = QRcode_new(version, width, masked); |
| 618 |
|
| |
| 619 |
0 |
| EXIT: |
| 620 |
0 |
| MQRraw_free(raw); |
| 621 |
0 |
| free(filler); |
| 622 |
0 |
| free(frame); |
| 623 |
0 |
| return qrcode; |
| 624 |
|
| } |
| 625 |
|
| |
| 626 |
6 |
| QRcode *QRcode_encodeInput(QRinput *input) |
| 627 |
|
| { |
| 628 |
6 |
▶ | if(input->mqr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(input->mqr) { |
0 |
6 |
6 |
|
| 629 |
0 |
| return QRcode_encodeMaskMQR(input, -1); |
| 630 |
|
| } else { |
| 631 |
6 |
| return QRcode_encodeMask(input, -1); |
| 632 |
|
| } |
| 633 |
|
| } |
| 634 |
|
| |
| 635 |
0 |
| static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive) |
| 636 |
|
| { |
| 637 |
|
| QRinput *input; |
| 638 |
|
| QRcode *code; |
| 639 |
|
| int ret; |
| 640 |
|
| |
| 641 |
0 |
▶ | if(string == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(string == NULL) { |
0 |
0 |
0 |
|
| 642 |
0 |
| errno = EINVAL; |
| 643 |
0 |
| return NULL; |
| 644 |
|
| } |
| 645 |
0 |
▶ | if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) { |
0 |
0 |
0 |
| if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) { |
0 |
0 |
0 |
|
| 646 |
0 |
| errno = EINVAL; |
| 647 |
0 |
| return NULL; |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
0 |
▶ | if(mqr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mqr) { |
0 |
0 |
0 |
|
| 651 |
0 |
| input = QRinput_newMQR(version, level); |
| 652 |
|
| } else { |
| 653 |
0 |
| input = QRinput_new2(version, level); |
| 654 |
|
| } |
| 655 |
0 |
▶ | if(input == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(input == NULL) return NULL; |
0 |
0 |
0 |
|
| 656 |
|
| |
| 657 |
0 |
| ret = Split_splitStringToQRinput(string, input, hint, casesensitive); |
| 658 |
0 |
▶ | if(ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret < 0) { |
0 |
0 |
0 |
|
| 659 |
0 |
| QRinput_free(input); |
| 660 |
0 |
| return NULL; |
| 661 |
|
| } |
| 662 |
0 |
| code = QRcode_encodeInput(input); |
| 663 |
0 |
| QRinput_free(input); |
| 664 |
|
| |
| 665 |
0 |
| return code; |
| 666 |
|
| } |
| 667 |
|
| |
| 668 |
0 |
| QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive) |
| 669 |
|
| { |
| 670 |
0 |
| return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive); |
| 671 |
|
| } |
| 672 |
|
| |
| 673 |
0 |
| QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive) |
| 674 |
|
| { |
| 675 |
0 |
| return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive); |
| 676 |
|
| } |
| 677 |
|
| |
| 678 |
6 |
| static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr) |
| 679 |
|
| { |
| 680 |
|
| QRinput *input; |
| 681 |
|
| QRcode *code; |
| 682 |
|
| int ret; |
| 683 |
|
| |
| 684 |
6 |
▶ | if(data == NULL || length == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(data == NULL || length == 0) { |
6 |
0 |
6 |
| if(data == NULL || length == 0) { |
0 |
6 |
6 |
|
| 685 |
0 |
| errno = EINVAL; |
| 686 |
0 |
| return NULL; |
| 687 |
|
| } |
| 688 |
|
| |
| 689 |
6 |
▶ | if(mqr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mqr) { |
0 |
6 |
6 |
|
| 690 |
0 |
| input = QRinput_newMQR(version, level); |
| 691 |
|
| } else { |
| 692 |
6 |
| input = QRinput_new2(version, level); |
| 693 |
|
| } |
| 694 |
6 |
▶ | if(input == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(input == NULL) return NULL; |
0 |
6 |
6 |
|
| 695 |
|
| |
| 696 |
6 |
| ret = QRinput_append(input, QR_MODE_8, length, data); |
| 697 |
6 |
▶ | if(ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret < 0) { |
0 |
6 |
6 |
|
| 698 |
0 |
| QRinput_free(input); |
| 699 |
0 |
| return NULL; |
| 700 |
|
| } |
| 701 |
6 |
| code = QRcode_encodeInput(input); |
| 702 |
6 |
| QRinput_free(input); |
| 703 |
|
| |
| 704 |
6 |
| return code; |
| 705 |
|
| } |
| 706 |
|
| |
| 707 |
0 |
| QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level) |
| 708 |
|
| { |
| 709 |
0 |
| return QRcode_encodeDataReal(data, size, version, level, 0); |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
6 |
| QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level) |
| 713 |
|
| { |
| 714 |
6 |
▶ | if(string == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(string == NULL) { |
0 |
6 |
6 |
|
| 715 |
0 |
| errno = EINVAL; |
| 716 |
0 |
| return NULL; |
| 717 |
|
| } |
| 718 |
6 |
| return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 0); |
| 719 |
|
| } |
| 720 |
|
| |
| 721 |
0 |
| QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level) |
| 722 |
|
| { |
| 723 |
0 |
| return QRcode_encodeDataReal(data, size, version, level, 1); |
| 724 |
|
| } |
| 725 |
|
| |
| 726 |
0 |
| QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level) |
| 727 |
|
| { |
| 728 |
0 |
▶ | if(string == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(string == NULL) { |
0 |
0 |
0 |
|
| 729 |
0 |
| errno = EINVAL; |
| 730 |
0 |
| return NULL; |
| 731 |
|
| } |
| 732 |
0 |
| return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 1); |
| 733 |
|
| } |
| 734 |
|
| |
| 735 |
|
| |
| 736 |
|
| /****************************************************************************** |
| 737 |
|
| * Structured QR-code encoding |
| 738 |
|
| *****************************************************************************/ |
| 739 |
|
| |
| 740 |
0 |
| static QRcode_List *QRcode_List_newEntry(void) |
| 741 |
|
| { |
| 742 |
|
| QRcode_List *entry; |
| 743 |
|
| |
| 744 |
0 |
| entry = (QRcode_List *)malloc(sizeof(QRcode_List)); |
| 745 |
0 |
▶ | if(entry == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(entry == NULL) return NULL; |
0 |
0 |
0 |
|
| 746 |
|
| |
| 747 |
0 |
| entry->next = NULL; |
| 748 |
0 |
| entry->code = NULL; |
| 749 |
|
| |
| 750 |
0 |
| return entry; |
| 751 |
|
| } |
| 752 |
|
| |
| 753 |
0 |
| static void QRcode_List_freeEntry(QRcode_List *entry) |
| 754 |
|
| { |
| 755 |
0 |
▶ | if(entry != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(entry != NULL) { |
0 |
0 |
0 |
|
| 756 |
0 |
| QRcode_free(entry->code); |
| 757 |
0 |
| free(entry); |
| 758 |
|
| } |
| 759 |
0 |
| } |
| 760 |
|
| |
| 761 |
0 |
| void QRcode_List_free(QRcode_List *qrlist) |
| 762 |
|
| { |
| 763 |
0 |
| QRcode_List *list = qrlist, *next; |
| 764 |
|
| |
| 765 |
0 |
▶ | while(list != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(list != NULL) { |
0 |
0 |
0 |
|
| 766 |
0 |
| next = list->next; |
| 767 |
0 |
| QRcode_List_freeEntry(list); |
| 768 |
0 |
| list = next; |
| 769 |
|
| } |
| 770 |
0 |
| } |
| 771 |
|
| |
| 772 |
0 |
| int QRcode_List_size(QRcode_List *qrlist) |
| 773 |
|
| { |
| 774 |
0 |
| QRcode_List *list = qrlist; |
| 775 |
0 |
| int size = 0; |
| 776 |
|
| |
| 777 |
0 |
▶ | while(list != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(list != NULL) { |
0 |
0 |
0 |
|
| 778 |
0 |
| size++; |
| 779 |
0 |
| list = list->next; |
| 780 |
|
| } |
| 781 |
|
| |
| 782 |
0 |
| return size; |
| 783 |
|
| } |
| 784 |
|
| |
| 785 |
|
| #if 0 |
| 786 |
|
| static unsigned char QRcode_parity(const char *str, int size) |
| 787 |
|
| { |
| 788 |
|
| unsigned char parity = 0; |
| 789 |
|
| int i; |
| 790 |
|
| |
| 791 |
|
| for(i=0; i<size; i++) { |
| 792 |
|
| parity ^= str[i]; |
| 793 |
|
| } |
| 794 |
|
| |
| 795 |
|
| return parity; |
| 796 |
|
| } |
| 797 |
|
| #endif |
| 798 |
|
| |
| 799 |
0 |
| QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s) |
| 800 |
|
| { |
| 801 |
0 |
| QRcode_List *head = NULL; |
| 802 |
0 |
| QRcode_List *tail = NULL; |
| 803 |
|
| QRcode_List *entry; |
| 804 |
0 |
| QRinput_InputList *list = s->head; |
| 805 |
|
| |
| 806 |
0 |
▶ | while(list != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(list != NULL) { |
0 |
0 |
0 |
|
| 807 |
0 |
▶ | if(head == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(head == NULL) { |
0 |
0 |
0 |
|
| 808 |
0 |
| entry = QRcode_List_newEntry(); |
| 809 |
0 |
▶ | if(entry == NULL) goto ABORT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(entry == NULL) goto ABORT; |
0 |
0 |
0 |
|
| 810 |
0 |
| head = entry; |
| 811 |
0 |
| tail = head; |
| 812 |
|
| } else { |
| 813 |
0 |
| entry = QRcode_List_newEntry(); |
| 814 |
0 |
▶ | if(entry == NULL) goto ABORT; |
Branch: 1 branch
| Branch | True | False | Total |
| if(entry == NULL) goto ABORT; |
0 |
0 |
0 |
|
| 815 |
0 |
| tail->next = entry; |
| 816 |
0 |
| tail = tail->next; |
| 817 |
|
| } |
| 818 |
0 |
| tail->code = QRcode_encodeInput(list->input); |
| 819 |
0 |
▶ | if(tail->code == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tail->code == NULL) { |
0 |
0 |
0 |
|
| 820 |
0 |
| goto ABORT; |
| 821 |
|
| } |
| 822 |
0 |
| list = list->next; |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
0 |
| return head; |
| 826 |
0 |
| ABORT: |
| 827 |
0 |
| QRcode_List_free(head); |
| 828 |
0 |
| return NULL; |
| 829 |
|
| } |
| 830 |
|
| |
| 831 |
0 |
| static QRcode_List *QRcode_encodeInputToStructured(QRinput *input) |
| 832 |
|
| { |
| 833 |
|
| QRinput_Struct *s; |
| 834 |
|
| QRcode_List *codes; |
| 835 |
|
| |
| 836 |
0 |
| s = QRinput_splitQRinputToStruct(input); |
| 837 |
0 |
▶ | if(s == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(s == NULL) return NULL; |
0 |
0 |
0 |
|
| 838 |
|
| |
| 839 |
0 |
| codes = QRcode_encodeInputStructured(s); |
| 840 |
0 |
| QRinput_Struct_free(s); |
| 841 |
|
| |
| 842 |
0 |
| return codes; |
| 843 |
|
| } |
| 844 |
|
| |
| 845 |
0 |
| static QRcode_List *QRcode_encodeDataStructuredReal( |
| 846 |
|
| int size, const unsigned char *data, |
| 847 |
|
| int version, QRecLevel level, |
| 848 |
|
| int eightbit, QRencodeMode hint, int casesensitive) |
| 849 |
|
| { |
| 850 |
|
| QRinput *input; |
| 851 |
|
| QRcode_List *codes; |
| 852 |
|
| int ret; |
| 853 |
|
| |
| 854 |
0 |
▶ | if(version <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(version <= 0) { |
0 |
0 |
0 |
|
| 855 |
0 |
| errno = EINVAL; |
| 856 |
0 |
| return NULL; |
| 857 |
|
| } |
| 858 |
0 |
▶ | if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) { |
0 |
0 |
0 |
| if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) { |
0 |
0 |
0 |
| if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) { |
0 |
0 |
0 |
|
| 859 |
0 |
| errno = EINVAL; |
| 860 |
0 |
| return NULL; |
| 861 |
|
| } |
| 862 |
|
| |
| 863 |
0 |
| input = QRinput_new2(version, level); |
| 864 |
0 |
▶ | if(input == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if(input == NULL) return NULL; |
0 |
0 |
0 |
|
| 865 |
|
| |
| 866 |
0 |
▶ | if(eightbit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(eightbit) { |
0 |
0 |
0 |
|
| 867 |
0 |
| ret = QRinput_append(input, QR_MODE_8, size, data); |
| 868 |
|
| } else { |
| 869 |
0 |
| ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive); |
| 870 |
|
| } |
| 871 |
0 |
▶ | if(ret < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret < 0) { |
0 |
0 |
0 |
|
| 872 |
0 |
| QRinput_free(input); |
| 873 |
0 |
| return NULL; |
| 874 |
|
| } |
| 875 |
0 |
| codes = QRcode_encodeInputToStructured(input); |
| 876 |
0 |
| QRinput_free(input); |
| 877 |
|
| |
| 878 |
0 |
| return codes; |
| 879 |
|
| } |
| 880 |
|
| |
| 881 |
0 |
| QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) { |
| 882 |
0 |
| return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0); |
| 883 |
|
| } |
| 884 |
|
| |
| 885 |
0 |
| QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) { |
| 886 |
0 |
▶ | if(string == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(string == NULL) { |
0 |
0 |
0 |
|
| 887 |
0 |
| errno = EINVAL; |
| 888 |
0 |
| return NULL; |
| 889 |
|
| } |
| 890 |
0 |
| return QRcode_encodeDataStructured(strlen(string), (unsigned char *)string, version, level); |
| 891 |
|
| } |
| 892 |
|
| |
| 893 |
0 |
| QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive) |
| 894 |
|
| { |
| 895 |
0 |
▶ | if(string == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(string == NULL) { |
0 |
0 |
0 |
|
| 896 |
0 |
| errno = EINVAL; |
| 897 |
0 |
| return NULL; |
| 898 |
|
| } |
| 899 |
0 |
| return QRcode_encodeDataStructuredReal(strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive); |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
|
| /****************************************************************************** |
| 903 |
|
| * System utilities |
| 904 |
|
| *****************************************************************************/ |
| 905 |
|
| |
| 906 |
0 |
| void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version) |
| 907 |
|
| { |
| 908 |
0 |
▶ | if(major_version != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(major_version != NULL) { |
0 |
0 |
0 |
|
| 909 |
0 |
| *major_version = MAJOR_VERSION; |
| 910 |
|
| } |
| 911 |
0 |
▶ | if(minor_version != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(minor_version != NULL) { |
0 |
0 |
0 |
|
| 912 |
0 |
| *minor_version = MINOR_VERSION; |
| 913 |
|
| } |
| 914 |
0 |
▶ | if(micro_version != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(micro_version != NULL) { |
0 |
0 |
0 |
|
| 915 |
0 |
| *micro_version = MICRO_VERSION; |
| 916 |
|
| } |
| 917 |
0 |
| } |
| 918 |
|
| |
| 919 |
0 |
| char *QRcode_APIVersionString(void) |
| 920 |
|
| { |
| 921 |
0 |
| return VERSION; |
| 922 |
|
| } |
| 923 |
|
| |
| 924 |
0 |
| void QRcode_clearCache(void) |
| 925 |
|
| { |
| 926 |
0 |
| QRspec_clearCache(); |
| 927 |
0 |
| MQRspec_clearCache(); |
| 928 |
0 |
| free_rs_cache(); |
| 929 |
0 |
| } |