[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit
Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/app/qrencode/Makefile b/ap/app/qrencode/Makefile
new file mode 100644
index 0000000..a78c42a
--- /dev/null
+++ b/ap/app/qrencode/Makefile
@@ -0,0 +1,44 @@
+#*******************************************************************************
+# include ZTE application makefile
+#*******************************************************************************
+include $(COMMON_MK)
+
+##############USER COMIZE BEGIN################
+EXEC1 = qrencode
+EXEC2 = png2bmp
+OBJS1 = bitstream.o mask.o mmask.o mqrspec.o qrenc.o qrencode.o \
+ qrinput.o qrspec.o rscode.o split.o
+OBJS2 = common.o png2bmp.o
+
+
+LDLIBS += -lpng -L$(zte_lib_path)/libpng
+LDLIBS += -L$(zte_lib_path)/zlib/build -lz -lm
+CFLAGS += -I./
+CFLAGS += -I$(zte_lib_path)/libpng
+CFLAGS += -I$(zte_lib_path)/zlib/zlib-1.2.11
+
+
+
+##############USER COMIZE END##################
+
+#*******************************************************************************
+# targets
+#*******************************************************************************
+all: $(EXEC1) $(EXEC2)
+
+$(EXEC1): $(OBJS1)
+ $(CC) $(LDFLAGS) -o $@ $^ -Wl,--start-group $(LDLIBS) -Wl,--end-group
+ @cp $@ $@.elf
+
+$(EXEC2): $(OBJS2)
+ $(CC) $(LDFLAGS) -o $@ $^ -Wl,--start-group $(LDLIBS) -Wl,--end-group
+ @cp $@ $@.elf
+
+romfs:
+ $(ROMFSINST) $(EXEC1) /sbin/$(EXEC1)
+ $(ROMFSINST) $(EXEC2) /sbin/$(EXEC2)
+
+clean:
+ -rm -f $(EXEC1) *.elf *.gdb *.o
+ -rm -f $(EXEC2) *.elf *.gdb *.o
+
diff --git a/ap/app/qrencode/bitstream.c b/ap/app/qrencode/bitstream.c
new file mode 100644
index 0000000..a0b9283
--- /dev/null
+++ b/ap/app/qrencode/bitstream.c
@@ -0,0 +1,238 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Binary sequence class.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "bitstream.h"
+
+BitStream *BitStream_new(void)
+{
+ BitStream *bstream;
+
+ bstream = (BitStream *)malloc(sizeof(BitStream));
+ if(bstream == NULL) return NULL;
+
+ bstream->length = 0;
+ bstream->data = NULL;
+
+ return bstream;
+}
+
+static int BitStream_allocate(BitStream *bstream, int length)
+{
+ unsigned char *data;
+
+ if(bstream == NULL) {
+ return -1;
+ }
+
+ data = (unsigned char *)malloc(length);
+ if(data == NULL) {
+ return -1;
+ }
+
+ if(bstream->data) {
+ free(bstream->data);
+ }
+ bstream->length = length;
+ bstream->data = data;
+
+ return 0;
+}
+
+static BitStream *BitStream_newFromNum(int bits, unsigned int num)
+{
+ unsigned int mask;
+ int i;
+ unsigned char *p;
+ BitStream *bstream;
+
+ bstream = BitStream_new();
+ if(bstream == NULL) return NULL;
+
+ if(BitStream_allocate(bstream, bits)) {
+ BitStream_free(bstream);
+ return NULL;
+ }
+
+ p = bstream->data;
+ mask = 1 << (bits - 1);
+ for(i=0; i<bits; i++) {
+ if(num & mask) {
+ *p = 1;
+ } else {
+ *p = 0;
+ }
+ p++;
+ mask = mask >> 1;
+ }
+
+ return bstream;
+}
+
+static BitStream *BitStream_newFromBytes(int size, unsigned char *data)
+{
+ unsigned char mask;
+ int i, j;
+ unsigned char *p;
+ BitStream *bstream;
+
+ bstream = BitStream_new();
+ if(bstream == NULL) return NULL;
+
+ if(BitStream_allocate(bstream, size * 8)) {
+ BitStream_free(bstream);
+ return NULL;
+ }
+
+ p = bstream->data;
+ for(i=0; i<size; i++) {
+ mask = 0x80;
+ for(j=0; j<8; j++) {
+ if(data[i] & mask) {
+ *p = 1;
+ } else {
+ *p = 0;
+ }
+ p++;
+ mask = mask >> 1;
+ }
+ }
+
+ return bstream;
+}
+
+int BitStream_append(BitStream *bstream, BitStream *arg)
+{
+ unsigned char *data;
+
+ if(arg == NULL) {
+ return -1;
+ }
+ if(arg->length == 0) {
+ return 0;
+ }
+ if(bstream->length == 0) {
+ if(BitStream_allocate(bstream, arg->length)) {
+ return -1;
+ }
+ memcpy(bstream->data, arg->data, arg->length);
+ return 0;
+ }
+
+ data = (unsigned char *)malloc(bstream->length + arg->length);
+ if(data == NULL) {
+ return -1;
+ }
+ memcpy(data, bstream->data, bstream->length);
+ memcpy(data + bstream->length, arg->data, arg->length);
+
+ free(bstream->data);
+ bstream->length += arg->length;
+ bstream->data = data;
+
+ return 0;
+}
+
+int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num)
+{
+ BitStream *b;
+ int ret;
+
+ if(bits == 0) return 0;
+
+ b = BitStream_newFromNum(bits, num);
+ if(b == NULL) return -1;
+
+ ret = BitStream_append(bstream, b);
+ BitStream_free(b);
+
+ return ret;
+}
+
+int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data)
+{
+ BitStream *b;
+ int ret;
+
+ if(size == 0) return 0;
+
+ b = BitStream_newFromBytes(size, data);
+ if(b == NULL) return -1;
+
+ ret = BitStream_append(bstream, b);
+ BitStream_free(b);
+
+ return ret;
+}
+
+unsigned char *BitStream_toByte(BitStream *bstream)
+{
+ int i, j, size, bytes;
+ unsigned char *data, v;
+ unsigned char *p;
+
+ size = BitStream_size(bstream);
+ if(size == 0) {
+ return NULL;
+ }
+ data = (unsigned char *)malloc((size + 7) / 8);
+ if(data == NULL) {
+ return NULL;
+ }
+
+ bytes = size / 8;
+
+ p = bstream->data;
+ for(i=0; i<bytes; i++) {
+ v = 0;
+ for(j=0; j<8; j++) {
+ v = v << 1;
+ v |= *p;
+ p++;
+ }
+ data[i] = v;
+ }
+ if(size & 7) {
+ v = 0;
+ for(j=0; j<(size & 7); j++) {
+ v = v << 1;
+ v |= *p;
+ p++;
+ }
+ data[bytes] = v;
+ }
+
+ return data;
+}
+
+void BitStream_free(BitStream *bstream)
+{
+ if(bstream != NULL) {
+ free(bstream->data);
+ free(bstream);
+ }
+}
diff --git a/ap/app/qrencode/bitstream.h b/ap/app/qrencode/bitstream.h
new file mode 100644
index 0000000..ffe743c
--- /dev/null
+++ b/ap/app/qrencode/bitstream.h
@@ -0,0 +1,38 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Binary sequence class.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __BITSTREAM_H__
+#define __BITSTREAM_H__
+
+typedef struct {
+ int length;
+ unsigned char *data;
+} BitStream;
+
+extern BitStream *BitStream_new(void);
+extern int BitStream_append(BitStream *bstream, BitStream *arg);
+extern int BitStream_appendNum(BitStream *bstream, int bits, unsigned int num);
+extern int BitStream_appendBytes(BitStream *bstream, int size, unsigned char *data);
+#define BitStream_size(__bstream__) (__bstream__->length)
+extern unsigned char *BitStream_toByte(BitStream *bstream);
+extern void BitStream_free(BitStream *bstream);
+
+#endif /* __BITSTREAM_H__ */
diff --git a/ap/app/qrencode/bmphed.h b/ap/app/qrencode/bmphed.h
new file mode 100755
index 0000000..3379c76
--- /dev/null
+++ b/ap/app/qrencode/bmphed.h
@@ -0,0 +1,71 @@
+#ifndef BMPHED_H
+#define BMPHED_H
+
+#ifndef BI_RGB
+#define BI_RGB 0L /* Uncompressed */
+#define BI_RLE8 1L /* RLE (8 bits/pixel) */
+#define BI_RLE4 2L /* RLE (4 bits/pixel) */
+#define BI_BITFIELDS 3L /* Bitfield */
+#define BI_JPEG 4L /* JPEG Extension */
+#define BI_PNG 5L /* PNG Extension */
+#endif
+
+#define BIH_DSIZE 0 /* DWORD biSize; */
+#define BIH_LWIDTH 4 /* LONG biWidth; */
+#define BIH_LHEIGHT 8 /* LONG biHeight; */
+#define BIH_WPLANES 12 /* WORD biPlanes; */
+#define BIH_WBITCOUNT 14 /* WORD biBitCount; */
+#define BIH_DCOMPRESSION 16 /* DWORD biCompression; */
+#define BIH_DSIZEIMAGE 20 /* DWORD biSizeImage; */
+#define BIH_LXPELSPERMETER 24 /* LONG biXPelsPerMeter; */
+#define BIH_LYPELSPERMETER 28 /* LONG biYPelsPerMeter; */
+#define BIH_DCLRUSED 32 /* DWORD biClrUsed; */
+#define BIH_DCLRIMPORANT 36 /* DWORD biClrImportant; */
+#define B4H_DREDMASK 40 /* DWORD bV4RedMask; */
+#define B4H_DGREENMASK 44 /* DWORD bV4GreenMask; */
+#define B4H_DBLUEMASK 48 /* DWORD bV4BlueMask; */
+#define B4H_DALPHAMASK 52 /* DWORD bV4AlphaMask; */
+#define B4H_DCSTYPE 56 /* DWORD bV4CSType; */
+#define B4H_XENDPOINTS 60 /* CIEXYZTRIPLE bV4Endpoints; */
+#define B4H_DGAMMARED 96 /* DWORD bV4GammaRed; */
+#define B4H_DGAMMAGREEN 100 /* DWORD bV4GammaGreen; */
+#define B4H_DGAMMABLUE 104 /* DWORD bV4GammaBlue; */
+#define B5H_DINTENT 108 /* DWORD bV5Intent; */
+#define B5H_DPROFILEDATA 112 /* DWORD bV5ProfileData; */
+#define B5H_DPROFILESIZE 116 /* DWORD bV5ProfileSize; */
+#define B5H_DRESERVED 120 /* DWORD bV5Reserved; */
+#define INFOHED_SIZE 40 /* sizeof(BITMAPINFOHEADER) */
+#define BMPV4HED_SIZE 108 /* sizeof(BITMAPV4HEADER) */
+#define BMPV5HED_SIZE 124 /* sizeof(BITMAPV5HEADER) */
+
+#define BCH_DSIZE 0 /* DWORD bcSize; */
+#define BCH_WWIDTH 4 /* WORD bcWidth; */
+#define BCH_WHEIGHT 6 /* WORD bcHeight; */
+#define BCH_WPLANES 8 /* WORD bcPlanes; */
+#define BCH_WBITCOUNT 10 /* WORD bcBitCount; */
+#define COREHED_SIZE 12 /* sizeof(BITMAPCOREHEADER) */
+
+#define RGBQ_BLUE 0 /* BYTE rgbBlue; */
+#define RGBQ_GREEN 1 /* BYTE rgbGreen; */
+#define RGBQ_RED 2 /* BYTE rgbRed; */
+#define RGBQ_RESERVED 3 /* BYTE rgbReserved; */
+#define RGBQUAD_SIZE 4 /* sizeof(RGBQUAD) */
+
+#define RGBT_BLUE 0 /* BYTE rgbtBlue; */
+#define RGBT_GREEN 1 /* BYTE rgbtGreen; */
+#define RGBT_RED 2 /* BYTE rgbtRed; */
+#define RGBTRIPLE_SIZE 3 /* sizeof(RGBTRIPLE) */
+
+#define BMP_SIGNATURE 0x4D42
+#define BMP_SIG_BYTES 2
+
+#define BFH_WTYPE 0 /* WORD bfType; */
+#define BFH_DSIZE 2 /* DWORD bfSize; */
+#define BFH_WRESERVED1 6 /* WORD bfReserved1; */
+#define BFH_WRESERVED2 8 /* WORD bfReserved2; */
+#define BFH_DOFFBITS 10 /* DWORD bfOffBits; */
+#define BFH_DBIHSIZE 14 /* DWORD biSize; */
+#define FILEHED_SIZE 14 /* sizeof(BITMAPFILEHEADER) */
+#define BIHSIZE_SIZE 4 /* sizeof(biSize) */
+
+#endif /* BMPHED_H */
diff --git a/ap/app/qrencode/common.c b/ap/app/qrencode/common.c
new file mode 100755
index 0000000..c010f4c
--- /dev/null
+++ b/ap/app/qrencode/common.c
@@ -0,0 +1,512 @@
+#include "common.h"
+
+#define LINE_LEN 79
+#define STATUS_LEN 22
+#define PROGBAR_MAX (LINE_LEN - STATUS_LEN - 1)
+
+static char status_msg[128];
+static int progbar_scale = 0;
+static int progbar_len = 0;
+static int progbar_pos = -1;
+
+int quietmode = 0;
+int errorlog = 0;
+
+static png_uint_32 counter;
+static png_uint_32 maxcount;
+static int barlen;
+
+#define isoption(p) (IsOptChar((p)[0]) && (p)[1]!='\0')
+
+static void print_status(void)
+{
+ fprintf(stderr, "\r%-*.*s ", STATUS_LEN, STATUS_LEN, status_msg);
+ fflush(stderr);
+ progbar_pos = 0;
+}
+
+static void put_dots(int dotchar, int num)
+{
+ int i;
+
+ if (num > PROGBAR_MAX) num = PROGBAR_MAX;
+ if (progbar_pos == -1) print_status();
+
+ for (i = progbar_pos; i < num; i++)
+ fputc(dotchar, stderr);
+
+ if (progbar_pos < num) {
+ progbar_pos = num;
+ fflush(stderr);
+ }
+}
+
+static void print_scale(void)
+{
+ if (progbar_pos != 0) print_status();
+ put_dots('.', progbar_len);
+ print_status();
+ progbar_scale = 1;
+}
+
+static void init_progress_bar(int max)
+{
+ if (quietmode) return;
+
+ progbar_len = max;
+ print_scale();
+}
+
+static void update_progress_bar(int num)
+{
+ if (quietmode) return;
+
+ if (!progbar_scale) print_scale();
+ put_dots('o', num);
+}
+
+static void clear_line(void)
+{
+ if (quietmode) return;
+
+ fprintf(stderr, "\r%*c\r", LINE_LEN, ' ');
+ progbar_scale = 0;
+ progbar_pos = -1;
+}
+
+void xxprintf(const char *fmt, ...)
+{
+ va_list ap;
+ FILE *f;
+
+ va_start(ap, fmt);
+
+ clear_line();
+ vfprintf(stderr, fmt, ap);
+ fflush(stderr);
+
+ if (errorlog && (f = fopen(errlogfile, "a")) != NULL) {
+ vfprintf(f, fmt, ap);
+ fclose(f);
+ }
+
+ va_end(ap);
+}
+
+void set_status(const char *fmt, ...)
+{
+ va_list ap;
+
+ if (quietmode) return;
+
+ va_start(ap, fmt);
+ vsprintf(status_msg, fmt, ap);
+ va_end(ap);
+
+ print_status();
+}
+
+void feed_line(void)
+{
+ if (quietmode) return;
+
+ fputc('\n', stderr);
+ fflush(stderr);
+ progbar_scale = 0;
+ progbar_pos = -1;
+}
+
+static png_uint_32 maxcount_adam7(png_uint_32 width, png_uint_32 height)
+{
+ png_uint_32 c = 0;
+
+ if ( 1 ) c += ((height - 0 + 7) / 8) * 1;
+ if (width > 4) c += ((height - 0 + 7) / 8) * 1;
+ if ( 1 ) c += ((height - 4 + 7) / 8) * 2;
+ if (width > 2) c += ((height - 0 + 3) / 4) * 2;
+ if ( 1 ) c += ((height - 2 + 3) / 4) * 4;
+ if (width > 1) c += ((height - 0 + 1) / 2) * 4;
+ if ( 1 ) c += ((height - 1 + 1) / 2) * 8;
+
+ return c;
+}
+
+void init_progress_meter(png_structp png_ptr, png_uint_32 width,
+ png_uint_32 height)
+{
+ enum { W = 1024, H = 768 };
+
+ if (png_set_interlace_handling(png_ptr) == 7) {
+ maxcount = maxcount_adam7(width, height);
+ } else {
+ maxcount = height;
+ }
+ if (height > ((png_uint_32)W * H) / width) {
+ barlen = PROGBAR_MAX;
+ } else {
+ barlen = (PROGBAR_MAX * width * height + (W * H - 1)) / (W * H);
+ }
+ counter = 0;
+ init_progress_bar(barlen);
+}
+
+void row_callback(png_structp png_ptr, png_uint_32 row, int pass)
+{
+
+ if (row == 0) pass--;
+
+
+ counter += (1 << (pass >> 1)); /* step[pass]; */
+ update_progress_bar(barlen * counter / maxcount);
+}
+
+FILE *binary_stdio(int fd)
+{
+ FILE *fp;
+
+ if (fd != 0 && fd != 1)
+ return NULL;
+
+ printf("---------binary_stdio----else ------\n");
+ fp = (fd == 0) ? stdin : stdout;
+
+ return fp;
+}
+
+char *suffix(const char *path)
+{
+ char c, *p, *q, *r;
+
+ for (r = q = p = basname(path); (c = *p) != '\0'; p++)
+ if (c == '.') q = p;
+ if (q == r) q = p; /* dotfile with no suffix */
+
+ return q;
+}
+
+char *basname(const char *path)
+{
+ const char *p, *q;
+
+ for (p = path_skiproot(path);
+ *(q = path_nextslash(p)) != '\0'; p = q + 1) ;
+
+ return (char *)p;
+}
+
+char *addslash(char *path)
+{
+ char *p, *q;
+
+ for (p = path_skiproot(path);
+ *(q = path_nextslash(p)) != '\0'; p = q + 1) ;
+
+ if (q != p) {
+ *q++ = PATHDELIM;
+ *q = '\0';
+ }
+
+ return path;
+}
+
+
+char *path_skiproot(const char *path)
+{
+ if (IsPathDelim(path[0])) path++;
+ return (char *)path;
+}
+
+char *path_nextslash(const char *path)
+{
+ char c;
+
+ for (; (c = *path) != '\0'; path++) {
+ if (IsDBCSLead((unsigned char)c)) {
+ if (*(++path) == '\0') break;
+ continue;
+ }
+ if (IsPathDelim(c)) break;
+ }
+ return (char *)path;
+}
+
+char *delslash(char *path)
+{
+ char *p, *q, *s;
+
+ for (p = s = path_skiproot(path);
+ *(q = path_nextslash(p)) != '\0'; p = q + 1) ;
+
+ if (q == s) {
+ *q++ = '.';
+ *q = '\0';
+ } else if (q == p) {
+ *--q = '\0';
+ }
+
+ return path;
+}
+
+
+void png_my_error(png_structp png_ptr, png_const_charp message)
+{
+ xxprintf("ERROR(libpng): %s - %s\n", message,
+ (char *)png_get_error_ptr(png_ptr));
+ longjmp(png_jmpbuf(png_ptr), 1);
+}
+
+void png_my_warning(png_structp png_ptr, png_const_charp message)
+{
+ xxprintf("WARNING(libpng): %s - %s\n", message,
+ (char *)png_get_error_ptr(png_ptr));
+}
+
+BOOL imgbuf_alloc(IMAGE *img)
+{
+ BYTE *bp, **rp;
+ LONG n;
+
+ if (img->palnum > 0) {
+ img->palette = malloc((size_t)img->palnum * sizeof(PALETTE));
+ if (img->palette == NULL) { imgbuf_init(img); return FALSE; }
+ } else {
+ img->palette = NULL;
+ }
+ img->rowbytes = ((DWORD)img->width * img->pixdepth + 31) / 32 * 4;
+ img->imgbytes = img->rowbytes * img->height;
+ img->rowptr = malloc((size_t)img->height * sizeof(BYTE *));
+ img->bmpbits = malloc((size_t)img->imgbytes);
+
+ if (img->rowptr == NULL || img->bmpbits == NULL) {
+ imgbuf_free(img); imgbuf_init(img); return FALSE;
+ }
+
+ n = img->height;
+ rp = img->rowptr;
+ bp = img->bmpbits;
+
+ if (img->topdown) {
+ while (--n >= 0) {
+ *(rp++) = bp;
+ bp += img->rowbytes;
+
+ }
+ } else { /* bottom-up */
+ bp += img->imgbytes;
+ while (--n >= 0) {
+
+ ((DWORD *)bp)[-1] = 0;
+ bp -= img->rowbytes;
+ *(rp++) = bp;
+ }
+ }
+
+ return TRUE;
+}
+
+void imgbuf_free(IMAGE *img)
+{
+ free(img->palette);
+ free(img->rowptr);
+ free(img->bmpbits);
+}
+
+void imgbuf_init(IMAGE *img)
+{
+ img->palette = NULL;
+ img->rowptr = NULL;
+ img->bmpbits = NULL;
+}
+
+int parsearg(int *opt, char **arg, int argc, char **argv, char *aopts)
+{
+ static int agi = 1;
+ static char *agp = NULL;
+ char *p;
+ int c, i;
+
+ if (agp != NULL && *agp == '\0') {
+ agp = NULL;
+ agi++;
+ }
+ if (agi >= argc)
+ return 0;
+
+ if (p = argv[agi], agp == NULL && !isoption(p)) {
+
+ c = 0;
+ agi++;
+ } else {
+ if (agp == NULL) agp = p + 1;
+ if (c = (*agp & 0xFF), strchr(aopts, c) != NULL) {
+
+ if (p = agp + 1, *p != '\0') {
+ /*NULL*/;
+ } else if (i = agi + 1, p = argv[i], i < argc && !isoption(p)) {
+ agi = i;
+ } else {
+ p = NULL;
+ }
+ agp = NULL;
+ agi++;
+ } else {
+
+ p = NULL;
+ agp++;
+ }
+ }
+ *opt = c;
+ *arg = p;
+
+ return 1;
+}
+
+char **envargv(int *argcp, char ***argvp, const char *envn)
+{
+ int argc, nagc, envc, i;
+ char **argv, **nagv, *envs, *ep;
+
+ ep = getenv(envn);
+ if (ep == NULL || ep[0] == '\0') return NULL;
+
+ envs = malloc(strlen(ep) + 1);
+ if (envs == NULL) return NULL;
+ strcpy(envs, ep);
+
+ envc = tokenize(envs, envs);
+ if (envc == 0) { free(envs); return NULL; }
+
+ argc = *argcp;
+ argv = *argvp;
+ nagv = malloc((argc + envc + 1) * sizeof(char *));
+ if (nagv == NULL) { free(envs); return NULL; }
+
+ nagc = 1;
+ nagv[0] = argv[0];
+
+ for (i = 0; i < envc; i++) {
+ nagv[nagc++] = envs;
+ while (*(envs++) != '\0') ;
+ }
+ for (i = 1; i < argc; i++) {
+ nagv[nagc++] = argv[i];
+ }
+ nagv[nagc] = NULL;
+
+ *argcp = nagc;
+ *argvp = nagv;
+
+ return argv;
+}
+
+int tokenize(char *buf, const char *str)
+{
+ enum { STR = 0x01, QUOTE = 0x02 };
+ int flag = 0;
+ int num = 0;
+ char c;
+ int i;
+
+ while ((c = *str++) != '\0') {
+ if (!(flag & QUOTE) &&
+ (c == ' ' || c == '\t' || c == '\n' || c == '\r')) {
+ if (flag & STR) {
+ flag &= ~STR;
+ *buf++ = '\0';
+ }
+ } else {
+ if (!(flag & STR)) {
+ flag |= STR;
+ num++;
+ }
+ switch (c) {
+ case '\\':
+
+ for (i = 1; (c = *str) == '\\'; str++, i++) ;
+ if (c == '"') {
+ while ((i -= 2) >= 0)
+ *buf++ = '\\';
+ if (i == -1) {
+ *buf++ = '"';
+ str++;
+ }
+ } else {
+ while ((--i) >= 0)
+ *buf++ = '\\';
+ }
+ break;
+
+ case '"':
+ flag ^= QUOTE;
+ break;
+
+ default:
+ *buf++ = c;
+ }
+ }
+ }
+ if (flag & STR) *buf = '\0';
+
+ return num;
+}
+
+int makedir(const char *path)
+{
+ char dir[FILENAME_MAX];
+ struct stat sbuf;
+ char *p, c;
+ int r;
+
+ delslash(strcpy(dir, path));
+ if (stat(dir, &sbuf) == 0) {
+ if ((sbuf.st_mode & S_IFMT) == S_IFDIR) return 0;
+
+ return -1;
+ }
+ p = path_skiproot(dir);
+ do {
+ p = path_nextslash(p);
+ c = *p; *p = '\0';
+ r = MKDIR(dir, 0777);
+ *p++ = c;
+ } while (c != '\0');
+
+ return r;
+}
+
+int renbak(const char *path)
+{
+ char bak[FILENAME_MAX];
+ struct stat sbuf;
+ char *sfx;
+ int i;
+
+ strcpy(bak, path);
+ if (stat(bak, &sbuf) != 0) return 0;
+
+ sfx = bak + strlen(bak);
+
+ strcpy(sfx, ".bak");
+ i = 0;
+ while (1) {
+ if (stat(bak, &sbuf) != 0 && rename(path, bak) == 0) return 0;
+ if (i >= 1000) break;
+ sprintf(sfx, ".%03d", i++);
+ }
+ return -1;
+}
+
+int cpyftime(const char *srcf, const char *dstf)
+{
+ struct stat sbuf;
+ struct utimbuf ubuf;
+
+ if (stat(srcf, &sbuf) != 0) return -1;
+
+ ubuf.actime = sbuf.st_atime;
+ ubuf.modtime = sbuf.st_mtime;
+
+ return utime(dstf, &ubuf);
+}
+
+
diff --git a/ap/app/qrencode/common.h b/ap/app/qrencode/common.h
new file mode 100755
index 0000000..b6b081b
--- /dev/null
+++ b/ap/app/qrencode/common.h
@@ -0,0 +1,104 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdarg.h>
+
+#include <utime.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "png.h"
+
+#define MKDIR(d,m) mkdir(d,m)
+
+#if (PNG_LIBPNG_VER < 10004)
+# error libpng version 1.0.4 or later is required.
+#endif
+
+#if (PNG_LIBPNG_VER == 10207) || (PNG_LIBPNG_VER == 10206) || \
+ (PNG_LIBPNG_VER == 10017) || (PNG_LIBPNG_VER == 10016)
+# error Libpng versions 1.2.7, 1.2.6, 1.0.17, and 1.0.16
+# error have a bug that will cause png2bmp to crash.
+# error Update your libpng to latest version.
+# error "http://www.libpng.org/pub/png/libpng.html"
+#endif
+
+#if !defined(PNG_READ_tRNS_SUPPORTED) || !defined(PNG_WRITE_tRNS_SUPPORTED)
+# error This software requires tRNS chunk support.
+#endif
+
+#ifndef png_jmpbuf /* pngconf.h (libpng 1.0.6 or later) */
+# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
+#endif
+
+
+#define PATHDELIM '/'
+#define IsPathDelim(c) ((c)==PATHDELIM)
+#define IsOptChar(c) ((c)=='-')
+#define IsDBCSLead(c) (0)
+
+typedef char CHAR;
+typedef unsigned char BYTE;
+typedef short SHORT;
+typedef unsigned short WORD;
+typedef int INT;
+typedef unsigned int UINT;
+typedef long LONG;
+typedef unsigned long DWORD;
+typedef enum { FALSE = 0, TRUE = 1 } BOOL;
+
+typedef png_color PALETTE;
+typedef struct tagIMAGE {
+ LONG width;
+ LONG height;
+ UINT pixdepth;
+ UINT palnum;
+ BOOL topdown;
+ BOOL alpha;
+ /* ----------- */
+ DWORD rowbytes;
+ DWORD imgbytes;
+ PALETTE *palette;
+ BYTE **rowptr;
+ BYTE *bmpbits;
+ /* ----------- */
+ png_color_8 sigbit;
+} IMAGE;
+
+extern int quietmode;
+extern int errorlog;
+extern const char errlogfile[];
+
+void xxprintf(const char *, ...);
+void set_status(const char *, ...);
+void feed_line(void);
+void init_progress_meter(png_structp, png_uint_32, png_uint_32);
+void row_callback(png_structp, png_uint_32, int);
+
+FILE *binary_stdio(int);
+char *suffix(const char *);
+char *basname(const char *);
+char *addslash(char *);
+char *delslash(char *);
+char *path_skiproot(const char *);
+char *path_nextslash(const char *);
+
+void png_my_error(png_structp, png_const_charp);
+void png_my_warning(png_structp, png_const_charp);
+BOOL imgbuf_alloc(IMAGE *);
+void imgbuf_free(IMAGE *);
+void imgbuf_init(IMAGE *);
+int parsearg(int *, char **, int, char **, char *);
+char **envargv(int *, char ***, const char *);
+int tokenize(char *, const char *);
+int makedir(const char *);
+int renbak(const char *);
+int cpyftime(const char *, const char *);
+
+
+#endif /* COMMON_H */
diff --git a/ap/app/qrencode/config.h b/ap/app/qrencode/config.h
new file mode 100644
index 0000000..09e32fd
--- /dev/null
+++ b/ap/app/qrencode/config.h
@@ -0,0 +1,95 @@
+/* config.h. Generated from config.h.in by configure. */
+/* config.h.in. Generated from configure.ac by autoheader. */
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#define HAVE_DLFCN_H 1
+
+/* Define if you have the iconv() function and it works. */
+/* #undef HAVE_ICONV */
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#define HAVE_INTTYPES_H 1
+
+/* Define to 1 if you have the <memory.h> header file. */
+#define HAVE_MEMORY_H 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#define HAVE_STDINT_H 1
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#define HAVE_STDLIB_H 1
+
+/* Define to 1 if you have the `strdup' function. */
+#define HAVE_STRDUP 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#define HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#define HAVE_STRING_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#define HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#define HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#define HAVE_UNISTD_H 1
+
+/* Define to the sub-directory in which libtool stores uninstalled libraries.
+ */
+#define LT_OBJDIR ".libs/"
+
+/* Major version number */
+#define MAJOR_VERSION 3
+
+/* Micro version number */
+#define MICRO_VERSION 1
+
+/* Minor version number */
+#define MINOR_VERSION 3
+
+/* Define to 1 if your C compiler doesn't accept -c and -o together. */
+/* #undef NO_MINUS_C_MINUS_O */
+
+/* Name of package */
+#define PACKAGE "qrencode"
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT ""
+
+/* Define to the full name of this package. */
+#define PACKAGE_NAME ""
+
+/* Define to the full name and version of this package. */
+#define PACKAGE_STRING ""
+
+/* Define to the one symbol short name of this package. */
+#define PACKAGE_TARNAME ""
+
+/* Define to the home page for this package. */
+#define PACKAGE_URL ""
+
+/* Define to the version of this package. */
+#define PACKAGE_VERSION ""
+
+/* Define to 1 if you have the ANSI C header files. */
+#define STDC_HEADERS 1
+
+/* Version number of package */
+#define VERSION "3.3.1"
+
+/* Define to empty if `const' does not conform to ANSI C. */
+/* #undef const */
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+ calls it, or to nothing if 'inline' is not supported under any name. */
+#ifndef __cplusplus
+/* #undef inline */
+#endif
+
+/* Define to 'static' if no test programs will be compiled. */
+#define __STATIC static
+/* #undef WITH_TESTS */
+
diff --git a/ap/app/qrencode/mask.c b/ap/app/qrencode/mask.c
new file mode 100644
index 0000000..ccef810
--- /dev/null
+++ b/ap/app/qrencode/mask.c
@@ -0,0 +1,330 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Masking.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+
+#include "qrencode.h"
+#include "qrspec.h"
+#include "mask.h"
+
+static int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level)
+{
+ unsigned int format;
+ unsigned char v;
+ int i;
+ int blacks = 0;
+
+ format = QRspec_getFormatInfo(mask, level);
+
+ for(i=0; i<8; i++) {
+ if(format & 1) {
+ blacks += 2;
+ v = 0x85;
+ } else {
+ v = 0x84;
+ }
+ frame[width * 8 + width - 1 - i] = v;
+ if(i < 6) {
+ frame[width * i + 8] = v;
+ } else {
+ frame[width * (i + 1) + 8] = v;
+ }
+ format= format >> 1;
+ }
+ for(i=0; i<7; i++) {
+ if(format & 1) {
+ blacks += 2;
+ v = 0x85;
+ } else {
+ v = 0x84;
+ }
+ frame[width * (width - 7 + i) + 8] = v;
+ if(i == 0) {
+ frame[width * 8 + 7] = v;
+ } else {
+ frame[width * 8 + 6 - i] = v;
+ }
+ format= format >> 1;
+ }
+
+ return blacks;
+}
+
+/**
+ * Demerit coefficients.
+ * See Section 8.8.2, pp.45, JIS X0510:2004.
+ */
+#define N1 (3)
+#define N2 (3)
+#define N3 (40)
+#define N4 (10)
+
+#define MASKMAKER(__exp__) \
+ int x, y;\
+ int b = 0;\
+\
+ for(y=0; y<width; y++) {\
+ for(x=0; x<width; x++) {\
+ if(*s & 0x80) {\
+ *d = *s;\
+ } else {\
+ *d = *s ^ ((__exp__) == 0);\
+ }\
+ b += (int)(*d & 1);\
+ s++; d++;\
+ }\
+ }\
+ return b;
+
+static int Mask_mask0(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((x+y)&1)
+}
+
+static int Mask_mask1(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(y&1)
+}
+
+static int Mask_mask2(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(x%3)
+}
+
+static int Mask_mask3(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((x+y)%3)
+}
+
+static int Mask_mask4(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(((y/2)+(x/3))&1)
+}
+
+static int Mask_mask5(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(((x*y)&1)+(x*y)%3)
+}
+
+static int Mask_mask6(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((((x*y)&1)+(x*y)%3)&1)
+}
+
+static int Mask_mask7(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((((x*y)%3)+((x+y)&1))&1)
+}
+
+#define maskNum (8)
+typedef int MaskMaker(int, const unsigned char *, unsigned char *);
+static MaskMaker *maskMakers[maskNum] = {
+ Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3,
+ Mask_mask4, Mask_mask5, Mask_mask6, Mask_mask7
+};
+
+#ifdef WITH_TESTS
+unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask)
+{
+ unsigned char *masked;
+
+ masked = (unsigned char *)malloc(width * width);
+ if(masked == NULL) return NULL;
+
+ maskMakers[mask](width, frame, masked);
+
+ return masked;
+}
+#endif
+
+unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask, QRecLevel level)
+{
+ unsigned char *masked;
+
+ if(mask < 0 || mask >= maskNum) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ masked = (unsigned char *)malloc(width * width);
+ if(masked == NULL) return NULL;
+
+ maskMakers[mask](width, frame, masked);
+ Mask_writeFormatInformation(width, masked, mask, level);
+
+ return masked;
+}
+
+
+//static int n1;
+//static int n2;
+//static int n3;
+//static int n4;
+
+static int Mask_calcN1N3(int length, int *runLength)
+{
+ int i;
+ int demerit = 0;
+ int fact;
+
+ for(i=0; i<length; i++) {
+ if(runLength[i] >= 5) {
+ demerit += N1 + (runLength[i] - 5);
+ //n1 += N1 + (runLength[i] - 5);
+ }
+ if((i & 1)) {
+ if(i >= 3 && i < length-2 && (runLength[i] % 3) == 0) {
+ fact = runLength[i] / 3;
+ if(runLength[i-2] == fact &&
+ runLength[i-1] == fact &&
+ runLength[i+1] == fact &&
+ runLength[i+2] == fact) {
+ if(i == 3 || runLength[i-3] >= 4 * fact) {
+ demerit += N3;
+ //n3 += N3;
+ } else if(i+4 >= length || runLength[i+3] >= 4 * fact) {
+ demerit += N3;
+ //n3 += N3;
+ }
+ }
+ }
+ }
+ }
+
+ return demerit;
+}
+
+static int Mask_calcN2(int width, unsigned char *frame)
+{
+ int x, y;
+ unsigned char *p;
+ unsigned char b22, w22;
+ int demerit = 0;
+
+ p = frame + width + 1;
+ for(y=1; y<width; y++) {
+ for(x=1; x<width; x++) {
+ b22 = p[0] & p[-1] & p[-width] & p [-width-1];
+ w22 = p[0] | p[-1] | p[-width] | p [-width-1];
+ if((b22 | (w22 ^ 1))&1) {
+ demerit += N2;
+ }
+ p++;
+ }
+ p++;
+ }
+
+ return demerit;
+}
+
+static int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength)
+{
+ int head;
+ int i;
+ unsigned char *p;
+ int pitch;
+
+ pitch = (dir==0)?1:width;
+ if(frame[0] & 1) {
+ runLength[0] = -1;
+ head = 1;
+ } else {
+ head = 0;
+ }
+ runLength[head] = 1;
+ p = frame + pitch;
+
+ for(i=1; i<width; i++) {
+ if((p[0] ^ p[-pitch]) & 1) {
+ head++;
+ runLength[head] = 1;
+ } else {
+ runLength[head]++;
+ }
+ p += pitch;
+ }
+
+ return head + 1;
+}
+
+static int Mask_evaluateSymbol(int width, unsigned char *frame)
+{
+ int x, y;
+ int demerit = 0;
+ int runLength[QRSPEC_WIDTH_MAX + 1];
+ int length;
+
+ demerit += Mask_calcN2(width, frame);
+
+ for(y=0; y<width; y++) {
+ length = Mask_calcRunLength(width, frame + y * width, 0, runLength);
+ demerit += Mask_calcN1N3(length, runLength);
+ }
+
+ for(x=0; x<width; x++) {
+ length = Mask_calcRunLength(width, frame + x, 1, runLength);
+ demerit += Mask_calcN1N3(length, runLength);
+ }
+
+ return demerit;
+}
+
+unsigned char *Mask_mask(int width, unsigned char *frame, QRecLevel level)
+{
+ int i;
+ unsigned char *mask, *bestMask;
+ int minDemerit = INT_MAX;
+ int blacks;
+ int bratio;
+ int demerit;
+ int w2 = width * width;
+
+ mask = (unsigned char *)malloc(w2);
+ if(mask == NULL) return NULL;
+ bestMask = NULL;
+
+ for(i=0; i<maskNum; i++) {
+// n1 = n2 = n3 = n4 = 0;
+ demerit = 0;
+ blacks = maskMakers[i](width, frame, mask);
+ blacks += Mask_writeFormatInformation(width, mask, i, level);
+ bratio = (200 * blacks + w2) / w2 / 2; /* (int)(100*blacks/w2+0.5) */
+ demerit = (abs(bratio - 50) / 5) * N4;
+// n4 = demerit;
+ demerit += Mask_evaluateSymbol(width, mask);
+// printf("(%d,%d,%d,%d)=%d\n", n1, n2, n3 ,n4, demerit);
+ if(demerit < minDemerit) {
+ minDemerit = demerit;
+ free(bestMask);
+ bestMask = mask;
+ mask = (unsigned char *)malloc(w2);
+ if(mask == NULL) break;
+ }
+ }
+ free(mask);
+ return bestMask;
+}
diff --git a/ap/app/qrencode/mask.h b/ap/app/qrencode/mask.h
new file mode 100644
index 0000000..d9e1e5d
--- /dev/null
+++ b/ap/app/qrencode/mask.h
@@ -0,0 +1,37 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Masking.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __MASK_H__
+#define __MASK_H__
+
+extern unsigned char *Mask_makeMask(int width, unsigned char *frame, int mask, QRecLevel level);
+extern unsigned char *Mask_mask(int width, unsigned char *frame, QRecLevel level);
+
+#ifdef WITH_TESTS
+extern int Mask_calcN2(int width, unsigned char *frame);
+extern int Mask_calcN1N3(int length, int *runLength);
+extern int Mask_calcRunLength(int width, unsigned char *frame, int dir, int *runLength);
+extern int Mask_evaluateSymbol(int width, unsigned char *frame);
+extern int Mask_writeFormatInformation(int width, unsigned char *frame, int mask, QRecLevel level);
+extern unsigned char *Mask_makeMaskedFrame(int width, unsigned char *frame, int mask);
+#endif
+
+#endif /* __MASK_H__ */
diff --git a/ap/app/qrencode/mmask.c b/ap/app/qrencode/mmask.c
new file mode 100644
index 0000000..8d14235
--- /dev/null
+++ b/ap/app/qrencode/mmask.c
@@ -0,0 +1,177 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Masking for Micro QR Code.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include <errno.h>
+
+#include "qrencode.h"
+#include "mqrspec.h"
+#include "mmask.h"
+
+static void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level)
+{
+ unsigned int format;
+ unsigned char v;
+ int i;
+
+ format = MQRspec_getFormatInfo(mask, version, level);
+
+ for(i=0; i<8; i++) {
+ v = 0x84 | (format & 1);
+ frame[width * (i + 1) + 8] = v;
+ format = format >> 1;
+ }
+ for(i=0; i<7; i++) {
+ v = 0x84 | (format & 1);
+ frame[width * 8 + 7 - i] = v;
+ format = format >> 1;
+ }
+}
+
+#define MASKMAKER(__exp__) \
+ int x, y;\
+\
+ for(y=0; y<width; y++) {\
+ for(x=0; x<width; x++) {\
+ if(*s & 0x80) {\
+ *d = *s;\
+ } else {\
+ *d = *s ^ ((__exp__) == 0);\
+ }\
+ s++; d++;\
+ }\
+ }
+
+static void Mask_mask0(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(y&1)
+}
+
+static void Mask_mask1(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER(((y/2)+(x/3))&1)
+}
+
+static void Mask_mask2(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((((x*y)&1)+(x*y)%3)&1)
+}
+
+static void Mask_mask3(int width, const unsigned char *s, unsigned char *d)
+{
+ MASKMAKER((((x+y)&1)+((x*y)%3))&1)
+}
+
+#define maskNum (4)
+typedef void MaskMaker(int, const unsigned char *, unsigned char *);
+static MaskMaker *maskMakers[maskNum] = {
+ Mask_mask0, Mask_mask1, Mask_mask2, Mask_mask3
+};
+
+#ifdef WITH_TESTS
+unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask)
+{
+ unsigned char *masked;
+
+ masked = (unsigned char *)malloc(width * width);
+ if(masked == NULL) return NULL;
+
+ maskMakers[mask](width, frame, masked);
+
+ return masked;
+}
+#endif
+
+unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level)
+{
+ unsigned char *masked;
+ int width;
+
+ if(mask < 0 || mask >= maskNum) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ width = MQRspec_getWidth(version);
+ masked = (unsigned char *)malloc(width * width);
+ if(masked == NULL) return NULL;
+
+ maskMakers[mask](width, frame, masked);
+ MMask_writeFormatInformation(version, width, masked, mask, level);
+
+ return masked;
+}
+
+static int MMask_evaluateSymbol(int width, unsigned char *frame)
+{
+ int x, y;
+ unsigned char *p;
+ int sum1 = 0, sum2 = 0;
+
+ p = frame + width * (width - 1);
+ for(x=1; x<width; x++) {
+ sum1 += (p[x] & 1);
+ }
+
+ p = frame + width * 2 - 1;
+ for(y=1; y<width; y++) {
+ sum2 += (*p & 1);
+ p += width;
+ }
+
+ return (sum1 <= sum2)?(sum1 * 16 + sum2):(sum2 * 16 + sum1);
+}
+
+unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level)
+{
+ int i;
+ unsigned char *mask, *bestMask;
+ int maxScore = 0;
+ int score;
+ int width;
+
+ width = MQRspec_getWidth(version);
+
+ mask = (unsigned char *)malloc(width * width);
+ if(mask == NULL) return NULL;
+ bestMask = NULL;
+
+ for(i=0; i<maskNum; i++) {
+ score = 0;
+ maskMakers[i](width, frame, mask);
+ MMask_writeFormatInformation(version, width, mask, i, level);
+ score = MMask_evaluateSymbol(width, mask);
+ if(score > maxScore) {
+ maxScore = score;
+ free(bestMask);
+ bestMask = mask;
+ mask = (unsigned char *)malloc(width * width);
+ if(mask == NULL) break;
+ }
+ }
+ free(mask);
+ return bestMask;
+}
diff --git a/ap/app/qrencode/mmask.h b/ap/app/qrencode/mmask.h
new file mode 100644
index 0000000..f6556e8
--- /dev/null
+++ b/ap/app/qrencode/mmask.h
@@ -0,0 +1,34 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Masking for Micro QR Code.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __MMASK_H__
+#define __MMASK_H__
+
+extern unsigned char *MMask_makeMask(int version, unsigned char *frame, int mask, QRecLevel level);
+extern unsigned char *MMask_mask(int version, unsigned char *frame, QRecLevel level);
+
+#ifdef WITH_TESTS
+extern int MMask_evaluateSymbol(int width, unsigned char *frame);
+extern void MMask_writeFormatInformation(int version, int width, unsigned char *frame, int mask, QRecLevel level);
+extern unsigned char *MMask_makeMaskedFrame(int width, unsigned char *frame, int mask);
+#endif
+
+#endif /* __MMASK_H__ */
diff --git a/ap/app/qrencode/mqrspec.c b/ap/app/qrencode/mqrspec.c
new file mode 100644
index 0000000..76d2d26
--- /dev/null
+++ b/ap/app/qrencode/mqrspec.c
@@ -0,0 +1,280 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Micor QR Code specification in convenient format.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * The following data / specifications are taken from
+ * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
+ * or
+ * "Automatic identification and data capture techniques --
+ * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#ifdef HAVE_LIBPTHREAD
+#include <pthread.h>
+#endif
+
+#include "mqrspec.h"
+
+/******************************************************************************
+ * Version and capacity
+ *****************************************************************************/
+
+typedef struct {
+ int width; //< Edge length of the symbol
+ int ec[4]; //< Number of ECC code (bytes)
+} MQRspec_Capacity;
+
+/**
+ * Table of the capacity of symbols
+ * See Table 1 (pp.106) and Table 8 (pp.113) of Appendix 1, JIS X0510:2004.
+ */
+static const MQRspec_Capacity mqrspecCapacity[MQRSPEC_VERSION_MAX + 1] = {
+ { 0, {0, 0, 0, 0}},
+ { 11, {2, 0, 0, 0}},
+ { 13, {5, 6, 0, 0}},
+ { 15, {6, 8, 0, 0}},
+ { 17, {8, 10, 14, 0}}
+};
+
+int MQRspec_getDataLengthBit(int version, QRecLevel level)
+{
+ int w;
+ int ecc;
+
+ w = mqrspecCapacity[version].width - 1;
+ ecc = mqrspecCapacity[version].ec[level];
+ if(ecc == 0) return 0;
+ return w * w - 64 - ecc * 8;
+}
+
+int MQRspec_getDataLength(int version, QRecLevel level)
+{
+ return (MQRspec_getDataLengthBit(version, level) + 4) / 8;
+}
+
+int MQRspec_getECCLength(int version, QRecLevel level)
+{
+ return mqrspecCapacity[version].ec[level];
+}
+
+int MQRspec_getWidth(int version)
+{
+ return mqrspecCapacity[version].width;
+}
+
+/******************************************************************************
+ * Length indicator
+ *****************************************************************************/
+
+/**
+ * See Table 3 (pp.107) of Appendix 1, JIS X0510:2004.
+ */
+static const int lengthTableBits[4][4] = {
+ { 3, 4, 5, 6},
+ { 0, 3, 4, 5},
+ { 0, 0, 4, 5},
+ { 0, 0, 3, 4}
+};
+
+int MQRspec_lengthIndicator(QRencodeMode mode, int version)
+{
+ return lengthTableBits[mode][version - 1];
+}
+
+int MQRspec_maximumWords(QRencodeMode mode, int version)
+{
+ int bits;
+ int words;
+
+ bits = lengthTableBits[mode][version - 1];
+ words = (1 << bits) - 1;
+ if(mode == QR_MODE_KANJI) {
+ words *= 2; // the number of bytes is required
+ }
+
+ return words;
+}
+
+/******************************************************************************
+ * Format information
+ *****************************************************************************/
+
+/* See calcFormatInfo in tests/test_mqrspec.c */
+static const unsigned int formatInfo[4][8] = {
+ {0x4445, 0x55ae, 0x6793, 0x7678, 0x06de, 0x1735, 0x2508, 0x34e3},
+ {0x4172, 0x5099, 0x62a4, 0x734f, 0x03e9, 0x1202, 0x203f, 0x31d4},
+ {0x4e2b, 0x5fc0, 0x6dfd, 0x7c16, 0x0cb0, 0x1d5b, 0x2f66, 0x3e8d},
+ {0x4b1c, 0x5af7, 0x68ca, 0x7921, 0x0987, 0x186c, 0x2a51, 0x3bba}
+};
+
+/* See Table 10 of Appendix 1. (pp.115) */
+static const int typeTable[MQRSPEC_VERSION_MAX + 1][3] = {
+ {-1, -1, -1},
+ { 0, -1, -1},
+ { 1, 2, -1},
+ { 3, 4, -1},
+ { 5, 6, 7}
+};
+
+unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
+{
+ int type;
+
+ if(mask < 0 || mask > 3) return 0;
+ if(version <= 0 || version > MQRSPEC_VERSION_MAX) return 0;
+ if(level == QR_ECLEVEL_H) return 0;
+ type = typeTable[version][level];
+ if(type < 0) return 0;
+
+ return formatInfo[mask][type];
+}
+
+/******************************************************************************
+ * Frame
+ *****************************************************************************/
+
+/**
+ * Cache of initial frames.
+ */
+/* C99 says that static storage shall be initialized to a null pointer
+ * by compiler. */
+static unsigned char *frames[MQRSPEC_VERSION_MAX + 1];
+#ifdef HAVE_LIBPTHREAD
+static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+/**
+ * Put a finder pattern.
+ * @param frame
+ * @param width
+ * @param ox,oy upper-left coordinate of the pattern
+ */
+static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
+{
+ static const unsigned char finder[] = {
+ 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
+ 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
+ 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
+ };
+ int x, y;
+ const unsigned char *s;
+
+ frame += oy * width + ox;
+ s = finder;
+ for(y=0; y<7; y++) {
+ for(x=0; x<7; x++) {
+ frame[x] = s[x];
+ }
+ frame += width;
+ s += 7;
+ }
+}
+
+static unsigned char *MQRspec_createFrame(int version)
+{
+ unsigned char *frame, *p, *q;
+ int width;
+ int x, y;
+
+ width = mqrspecCapacity[version].width;
+ frame = (unsigned char *)malloc(width * width);
+ if(frame == NULL) return NULL;
+
+ memset(frame, 0, width * width);
+ /* Finder pattern */
+ putFinderPattern(frame, width, 0, 0);
+ /* Separator */
+ p = frame;
+ for(y=0; y<7; y++) {
+ p[7] = 0xc0;
+ p += width;
+ }
+ memset(frame + width * 7, 0xc0, 8);
+ /* Mask format information area */
+ memset(frame + width * 8 + 1, 0x84, 8);
+ p = frame + width + 8;
+ for(y=0; y<7; y++) {
+ *p = 0x84;
+ p += width;
+ }
+ /* Timing pattern */
+ p = frame + 8;
+ q = frame + width * 8;
+ for(x=1; x<width-7; x++) {
+ *p = 0x90 | (x & 1);
+ *q = 0x90 | (x & 1);
+ p++;
+ q += width;
+ }
+
+ return frame;
+}
+
+unsigned char *MQRspec_newFrame(int version)
+{
+ unsigned char *frame;
+ int width;
+
+ if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&frames_mutex);
+#endif
+ if(frames[version] == NULL) {
+ frames[version] = MQRspec_createFrame(version);
+ }
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&frames_mutex);
+#endif
+ if(frames[version] == NULL) return NULL;
+
+ width = mqrspecCapacity[version].width;
+ frame = (unsigned char *)malloc(width * width);
+ if(frame == NULL) return NULL;
+ memcpy(frame, frames[version], width * width);
+
+ return frame;
+}
+
+void MQRspec_clearCache(void)
+{
+ int i;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&frames_mutex);
+#endif
+ for(i=1; i<=MQRSPEC_VERSION_MAX; i++) {
+ free(frames[i]);
+ frames[i] = NULL;
+ }
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&frames_mutex);
+#endif
+}
diff --git a/ap/app/qrencode/mqrspec.h b/ap/app/qrencode/mqrspec.h
new file mode 100644
index 0000000..2d4b90d
--- /dev/null
+++ b/ap/app/qrencode/mqrspec.h
@@ -0,0 +1,157 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Micro QR Code specification in convenient format.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __MQRSPEC_H__
+#define __MQRSPEC_H__
+
+#include "qrencode.h"
+
+/******************************************************************************
+ * Version and capacity
+ *****************************************************************************/
+
+/**
+ * Maximum width of a symbol
+ */
+#define MQRSPEC_WIDTH_MAX 17
+
+/**
+ * Return maximum data code length (bits) for the version.
+ * @param version
+ * @param level
+ * @return maximum size (bits)
+ */
+extern int MQRspec_getDataLengthBit(int version, QRecLevel level);
+
+/**
+ * Return maximum data code length (bytes) for the version.
+ * @param version
+ * @param level
+ * @return maximum size (bytes)
+ */
+extern int MQRspec_getDataLength(int version, QRecLevel level);
+
+/**
+ * Return maximum error correction code length (bytes) for the version.
+ * @param version
+ * @param level
+ * @return ECC size (bytes)
+ */
+extern int MQRspec_getECCLength(int version, QRecLevel level);
+
+/**
+ * Return a version number that satisfies the input code length.
+ * @param size input code length (byte)
+ * @param level
+ * @return version number
+ */
+extern int MQRspec_getMinimumVersion(int size, QRecLevel level);
+
+/**
+ * Return the width of the symbol for the version.
+ * @param version
+ * @return width
+ */
+extern int MQRspec_getWidth(int version);
+
+/**
+ * Return the numer of remainder bits.
+ * @param version
+ * @return number of remainder bits
+ */
+extern int MQRspec_getRemainder(int version);
+
+/******************************************************************************
+ * Length indicator
+ *****************************************************************************/
+
+/**
+ * Return the size of lenght indicator for the mode and version.
+ * @param mode
+ * @param version
+ * @return the size of the appropriate length indicator (bits).
+ */
+extern int MQRspec_lengthIndicator(QRencodeMode mode, int version);
+
+/**
+ * Return the maximum length for the mode and version.
+ * @param mode
+ * @param version
+ * @return the maximum length (bytes)
+ */
+extern int MQRspec_maximumWords(QRencodeMode mode, int version);
+
+/******************************************************************************
+ * Version information pattern
+ *****************************************************************************/
+
+/**
+ * Return BCH encoded version information pattern that is used for the symbol
+ * of version 7 or greater. Use lower 18 bits.
+ * @param version
+ * @return BCH encoded version information pattern
+ */
+extern unsigned int MQRspec_getVersionPattern(int version);
+
+/******************************************************************************
+ * Format information
+ *****************************************************************************/
+
+/**
+ * Return BCH encoded format information pattern.
+ * @param mask
+ * @param version
+ * @param level
+ * @return BCH encoded format information pattern
+ */
+extern unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level);
+
+/******************************************************************************
+ * Frame
+ *****************************************************************************/
+
+/**
+ * Return a copy of initialized frame.
+ * When the same version is requested twice or more, a copy of cached frame
+ * is returned.
+ * @param version
+ * @return Array of unsigned char. You can free it by free().
+ */
+extern unsigned char *MQRspec_newFrame(int version);
+
+/**
+ * Clear the frame cache. Typically for debug.
+ */
+extern void MQRspec_clearCache(void);
+
+/******************************************************************************
+ * Mode indicator
+ *****************************************************************************/
+
+/**
+ * Mode indicator. See Table 2 in Appendix 1 of JIS X0510:2004, pp.107.
+ */
+#define MQRSPEC_MODEID_NUM 0
+#define MQRSPEC_MODEID_AN 1
+#define MQRSPEC_MODEID_8 2
+#define MQRSPEC_MODEID_KANJI 3
+
+#endif /* __MQRSPEC_H__ */
diff --git a/ap/app/qrencode/png2bmp.c b/ap/app/qrencode/png2bmp.c
new file mode 100755
index 0000000..8f82f98
--- /dev/null
+++ b/ap/app/qrencode/png2bmp.c
@@ -0,0 +1,526 @@
+#include "common.h"
+#include "bmphed.h"
+
+#define PNG2BMP_VERSION "1.62"
+#define PNG2BMP_COPYRIGHT "Copyright"
+
+#define ZLIB_VERSION "1.2.3"
+
+char outnam[FILENAME_MAX];
+char outdir[FILENAME_MAX];
+int deletesrc = 0;
+int copytime = 0;
+
+#define P2B_ALPHABMP_NONE 0
+#define P2B_ALPHABMP_ARGB 1
+#define P2B_ALPHABMP_BITFIELD 2
+
+int alpha_format = P2B_ALPHABMP_NONE;
+int expand_trans = 0;
+
+const char errlogfile[] = "./p2berror.log";
+
+
+const char wrn_mkdirfail[] =
+ "WARNING: Can not create directory - %s\n"
+ "WARNING: Output directory specified by '-%c' will be ignored.\n";
+
+const char err_ropenfail[] = "SKIPPED: No such file or directory - %s\n";
+const char err_wopenfail[] = "SKIPPED: Cannot create - %s\n";
+const char err_outofmemory[] = "SKIPPED: Out of memory - %s\n";
+
+const char err_writeerr[] = "SKIPPED: Write operation failed - %s\n";
+const char err_not_a_png[] = "SKIPPED: Not a PNG file - %s\n";
+
+static BOOL read_png(char *, IMAGE *);
+static int skip_macbinary(png_structp);
+static void to4bpp(png_structp, png_row_infop, png_bytep);
+static BOOL write_bmp(char *, IMAGE *);
+static const char *write_rgb_bits(IMAGE *, FILE *);
+static void mputdwl(void *, unsigned long);
+static void mputwl(void *, unsigned int);
+static void usage_exit(char *, int);
+
+#define ERROR_ABORT(s) do { errmsg = (s); goto error_abort; } while (0)
+
+static BOOL read_png(char *fn, IMAGE *img)
+{
+ png_structp png_ptr;
+ png_infop info_ptr, end_info;
+ png_uint_32 width, height;
+ int bit_depth, color_type;
+ int xbit_depth, xcolor_type, xchannels;
+ const char *errmsg;
+ FILE *fp;
+
+ imgbuf_init(img);
+
+ if (fn == NULL) {
+ fn = " (stdin)";
+ fp = binary_stdio(fileno(stdin));
+ } else {
+ fp = fopen(fn, "rb");
+ }
+ if (fp == NULL) ERROR_ABORT(err_ropenfail);
+
+ set_status("Reading %.80s", basname(fn));
+
+ /* ------------------------------------------------------ */
+
+ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, fn,
+ png_my_error, png_my_warning);
+ if (png_ptr == NULL) {
+ ERROR_ABORT(err_outofmemory);
+ }
+ info_ptr = png_create_info_struct(png_ptr);
+ end_info = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL || end_info == NULL) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ ERROR_ABORT(err_outofmemory);
+ }
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ /* If we get here, we had a problem writing the file */
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ ERROR_ABORT(NULL);
+ }
+ png_init_io(png_ptr, fp);
+ png_set_sig_bytes(png_ptr, skip_macbinary(png_ptr));
+
+ /* ------------------------------------------------------ */
+
+ png_read_info(png_ptr, info_ptr);
+
+ png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
+ &color_type, NULL, NULL, NULL);
+
+ /* ------------------------------------------------------ */
+
+ if (color_type & PNG_COLOR_MASK_ALPHA) {
+ if (alpha_format == P2B_ALPHABMP_NONE) {
+ png_set_strip_alpha(png_ptr);
+ color_type &= ~PNG_COLOR_MASK_ALPHA;
+ }
+ } else if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
+ if (alpha_format != P2B_ALPHABMP_NONE && expand_trans) {
+ png_set_tRNS_to_alpha(png_ptr);
+ color_type |= PNG_COLOR_MASK_ALPHA;
+ color_type &= ~PNG_COLOR_MASK_PALETTE;
+ }
+ }
+ if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
+ png_set_gray_to_rgb(png_ptr);
+ }
+ if (color_type == PNG_COLOR_TYPE_RGB ||
+ color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
+ png_set_bgr(png_ptr);
+ }
+ if (!(color_type & PNG_COLOR_MASK_ALPHA) && bit_depth == 2) {
+ png_set_user_transform_info(png_ptr, NULL, 4, 1);
+ png_set_read_user_transform_fn(png_ptr, to4bpp);
+ }
+ if (bit_depth == 16)
+ png_set_strip_16(png_ptr);
+
+ png_read_update_info(png_ptr, info_ptr);
+
+ /* ------------------------------------------------------ */
+
+ png_get_IHDR(png_ptr, info_ptr, &width, &height, &xbit_depth,
+ &xcolor_type, NULL, NULL, NULL);
+ xchannels = png_get_channels(png_ptr, info_ptr);
+
+ img->width = (LONG)width;
+ img->height = (LONG)height;
+ img->pixdepth = (UINT)xbit_depth * xchannels;
+ img->palnum = (img->pixdepth <= 8) ? (1 << img->pixdepth) : 0;
+ img->topdown = FALSE;
+ img->alpha = (xcolor_type & PNG_COLOR_MASK_ALPHA) ? TRUE : FALSE;
+
+ if (!imgbuf_alloc(img)) {
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+ ERROR_ABORT(err_outofmemory);
+ }
+
+ if (img->palnum > 0) {
+ if (xcolor_type == PNG_COLOR_TYPE_PALETTE) {
+ png_colorp palette;
+ int num_palette;
+ png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+ if (num_palette > (int)img->palnum) num_palette = img->palnum;
+ memset(img->palette, 0, img->palnum * sizeof(png_color));
+ memcpy(img->palette, palette, num_palette * sizeof(png_color));
+ } else {
+ int depth = (bit_depth == 16) ? 8 : bit_depth;
+ memset(img->palette, 0, img->palnum * sizeof(png_color));
+ png_build_grayscale_palette(depth, img->palette);
+ }
+ }
+
+ /* ------------------------------------------------------ */
+
+ png_set_read_status_fn(png_ptr, row_callback);
+ init_progress_meter(png_ptr, img->width, img->height);
+
+ png_read_image(png_ptr, img->rowptr);
+
+ png_read_end(png_ptr, end_info);
+ png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
+
+ /* ------------------------------------------------------ */
+
+ set_status("Read OK %.80s", basname(fn));
+
+ if (fp != stdin) fclose(fp);
+
+ return TRUE;
+
+error_abort: /* error */
+ if (errmsg != NULL) xxprintf(errmsg, fn);
+ if (fp != stdin && fp != NULL) fclose(fp);
+ imgbuf_free(img);
+
+ return FALSE;
+}
+
+static int skip_macbinary(png_structp png_ptr)
+{
+ enum { PNG_BYTES_TO_CHECK = 8, MACBIN_SIZE = 128 };
+ png_byte buf[MACBIN_SIZE];
+ png_bytep sig;
+ png_size_t check;
+ png_FILE_p fp = (png_FILE_p)png_get_io_ptr(png_ptr);
+
+ check = (png_size_t)fread(buf, 1, PNG_BYTES_TO_CHECK, fp);
+ if (check != PNG_BYTES_TO_CHECK) png_error(png_ptr, "Read Error");
+
+ if (png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK) == 0)
+ return PNG_BYTES_TO_CHECK;
+
+ check = (png_size_t)fread(buf, 1, MACBIN_SIZE, fp);
+ if (check != MACBIN_SIZE) png_error(png_ptr, "Read Error");
+
+ sig = buf + MACBIN_SIZE - PNG_BYTES_TO_CHECK;
+ if (png_sig_cmp(sig, 0, PNG_BYTES_TO_CHECK) == 0)
+ return PNG_BYTES_TO_CHECK;
+
+ xxprintf(err_not_a_png, (char *)png_get_error_ptr(png_ptr));
+ longjmp(png_jmpbuf(png_ptr), 1);
+
+ return 0; /* to quiet compiler warnings */
+}
+
+static void to4bpp(png_structp png_ptr, png_row_infop row_info, png_bytep data)
+{
+ static const png_byte pix[] = {
+ 0x00, 0x01, 0x02, 0x03, 0x10, 0x11, 0x12, 0x13,
+ 0x20, 0x21, 0x22, 0x23, 0x30, 0x31, 0x32, 0x33,
+ };
+ png_uint_32 rowb;
+ png_bytep p, q;
+ png_byte c;
+
+ rowb = (row_info->width + 1) / 2;
+ q = data + rowb;
+ p = data + rowb / 2;
+
+ if (rowb % 2 == 1) {
+ c = *p;
+ *(--q) = pix[c >> 4];
+ }
+ while (p > data) {
+ c = *(--p);
+ *(--q) = pix[c & 0x0F];
+ *(--q) = pix[c >> 4];
+ }
+ row_info->bit_depth = 4;
+ row_info->pixel_depth = 4;
+ row_info->rowbytes = rowb;
+}
+
+static BOOL write_bmp(char *fn, IMAGE *img)
+{
+ BYTE bfh[FILEHED_SIZE + BMPV4HED_SIZE];
+ BYTE *const bih = bfh + FILEHED_SIZE;
+ BYTE rgbq[RGBQUAD_SIZE];
+ BOOL alpha_bitfield;
+ DWORD bihsize, offbits, filesize;
+ PALETTE *pal;
+ const char *errmsg;
+ FILE *fp;
+ UINT i;
+
+ if (fn == NULL) {
+ fn = " (stdout)";
+ fp = binary_stdio(fileno(stdout));
+ } else {
+ fp = fopen(fn, "wb");
+ }
+ if (fp == NULL) ERROR_ABORT(err_wopenfail);
+
+ set_status("Writing %.80s", basname(fn));
+
+ /* ------------------------------------------------------ */
+
+ alpha_bitfield = (img->alpha && alpha_format == P2B_ALPHABMP_BITFIELD);
+ bihsize = (alpha_bitfield) ? BMPV4HED_SIZE : INFOHED_SIZE;
+ offbits = FILEHED_SIZE + bihsize + RGBQUAD_SIZE * img->palnum;
+ filesize = offbits + img->imgbytes;
+
+ memset(bfh, 0, sizeof(bfh));
+
+ mputwl( bfh + BFH_WTYPE , BMP_SIGNATURE);
+ mputdwl(bfh + BFH_DSIZE , filesize);
+ mputdwl(bfh + BFH_DOFFBITS, offbits);
+
+ mputdwl(bih + BIH_DSIZE , bihsize);
+ mputdwl(bih + BIH_LWIDTH , (DWORD)img->width);
+ mputdwl(bih + BIH_LHEIGHT , (DWORD)img->height);
+ mputwl( bih + BIH_WPLANES , 1);
+ mputwl( bih + BIH_WBITCOUNT , img->pixdepth);
+ mputdwl(bih + BIH_DSIZEIMAGE, img->imgbytes);
+
+ if (alpha_bitfield) {
+ mputdwl(bih + BIH_DCOMPRESSION, BI_BITFIELDS);
+ mputdwl(bih + B4H_DALPHAMASK, 0xFF000000);
+ mputdwl(bih + B4H_DREDMASK , 0x00FF0000);
+ mputdwl(bih + B4H_DGREENMASK, 0x0000FF00);
+ mputdwl(bih + B4H_DBLUEMASK , 0x000000FF);
+ }
+
+ if (fwrite(bfh, (FILEHED_SIZE + bihsize), 1, fp) != 1)
+ ERROR_ABORT(err_writeerr);
+
+ /* ------------------------------------------------------ */
+
+ memset(rgbq, 0, sizeof(rgbq));
+
+ for (pal = img->palette, i = img->palnum; i > 0; i--, pal++) {
+ rgbq[RGBQ_RED] = pal->red;
+ rgbq[RGBQ_GREEN] = pal->green;
+ rgbq[RGBQ_BLUE] = pal->blue;
+ if (fwrite(rgbq, RGBQUAD_SIZE, 1, fp) != 1)
+ ERROR_ABORT(err_writeerr);
+ }
+
+ /* ------------------------------------------------------ */
+
+ if ((errmsg = write_rgb_bits(img, fp)) != NULL) ERROR_ABORT(errmsg);
+
+ /* ------------------------------------------------------ */
+
+ set_status("OK %.80s", basname(fn));
+ feed_line();
+
+ fflush(fp);
+ if (fp != stdout) fclose(fp);
+ imgbuf_free(img);
+
+ return TRUE;
+
+error_abort: /* error */
+ xxprintf(errmsg, fn);
+ if (fp != stdout && fp != NULL) fclose(fp);
+ imgbuf_free(img);
+
+ return FALSE;
+}
+
+static const char *write_rgb_bits(IMAGE *img, FILE *fp)
+{
+ DWORD wr = 16*1024*1024;
+ DWORD num = img->imgbytes;
+ BYTE *ptr = img->bmpbits;
+
+ while (num > 0) {
+ if (wr > num) wr = num;
+
+ if (fwrite(ptr, wr, 1, fp) != 1)
+ return err_writeerr;
+
+ ptr += wr; num -= wr;
+ }
+
+ return NULL;
+}
+
+static void mputdwl(void *ptr, unsigned long val)
+{
+ unsigned char *p = ptr;
+
+ p[0] = (unsigned char)(val & 0xFF);
+ p[1] = (unsigned char)(val >> 8 & 0xFF);
+ p[2] = (unsigned char)(val >> 16 & 0xFF);
+ p[3] = (unsigned char)(val >> 24 & 0xFF);
+}
+
+static void mputwl(void *ptr, unsigned int val)
+{
+ unsigned char *p = ptr;
+
+ p[0] = (unsigned char)(val & 0xFF);
+ p[1] = (unsigned char)(val >> 8 & 0xFF);
+}
+
+static void usage_exit(char *argv0, int status)
+{
+ static const char str_usage[] =
+ "png2bmp\n";
+
+ fprintf(stdout, str_usage, argv0, argv0, errlogfile);
+
+ exit(status);
+}
+
+int main(int argc, char *argv[])
+{
+ char outf[FILENAME_MAX];
+ IMAGE image;
+ int opt;
+ char *arg;
+ char *p, c;
+ int r_stdin, w_stdout;
+ int failure = 0, success = 0;
+
+ envargv(&argc, &argv, "B2P");
+
+ r_stdin = !isatty(fileno(stdin));
+ w_stdout = !isatty(fileno(stdout));
+
+ while (parsearg(&opt, &arg, argc, argv, "DdOoFfPp")) {
+ switch (toupper(opt)) {
+ case 'E': deletesrc ^= 1; break;
+ case 'T': copytime ^= 1; break;
+ case 'Q': quietmode ^= 1; break;
+ case 'L': errorlog ^= 1; break;
+
+ case 'X':
+ r_stdin = 0;
+ w_stdout = 0;
+ break;
+
+ case 'A':
+ alpha_format = (alpha_format == P2B_ALPHABMP_ARGB) ?
+ P2B_ALPHABMP_NONE : P2B_ALPHABMP_ARGB;
+ break;
+
+ case 'B':
+ alpha_format = (alpha_format == P2B_ALPHABMP_BITFIELD) ?
+ P2B_ALPHABMP_NONE : P2B_ALPHABMP_BITFIELD;
+ break;
+
+ case 'R':
+ expand_trans ^= 1;
+ break;
+
+ case 'F':
+ /* '-F' option of bmp2png (ignored on png2bmp) */
+ break;
+
+ case 'P':
+ /* '-P' option of bmp2png (ignored on png2bmp) */
+ break;
+
+ case 'D': /* output directory */
+ if (*arg == '-') arg = NULL;
+ if (arg == NULL) {
+ outdir[0] = '\0';
+ } else {
+ strcpy(outdir, arg);
+ addslash(outdir);
+ if (makedir(outdir) != 0) {
+ xxprintf(wrn_mkdirfail, outdir, 'D');
+ outdir[0] = '\0';
+ }
+ }
+ break;
+
+ case 'O': /* output filename */
+ if (arg == NULL) {
+ outnam[0] = '\0';
+ } else {
+ strcpy(outnam, arg);
+ p = basname(outnam);
+ c = *p; *p = '\0';
+ if (makedir(outnam) != 0) {
+ xxprintf(wrn_mkdirfail, outnam, 'O');
+ outnam[0] = '\0';
+ } else {
+ *p = c;
+ }
+ }
+ break;
+
+ case 0x00: /* input file spec */
+ if (outnam[0] != '\0') {
+ strcpy(outf, outnam);
+ outnam[0] = '\0';
+ } else if (w_stdout) {
+ if (!read_png(arg, &image)) return 1;
+ if (!write_bmp(NULL, &image)) return 1;
+ if (deletesrc) remove(arg);
+ return 0;
+ } else {
+ if (outdir[0] != '\0') {
+ strcat(strcpy(outf, outdir), basname(arg));
+ } else {
+ strcpy(outf, arg);
+ }
+
+ strcpy(suffix(outf), ".bmp");
+ }
+ /* ---------------------- */
+ printf("png2bmp read_png 2222222\n");
+ if (!read_png(arg, &image)) {
+ printf("png2bmp read_png arg image\n");
+ failure++;
+ break;
+ }
+ renbak(outf);
+ printf("png2bmp read_png 3333333\n");
+ if (!write_bmp(outf, &image)) {
+ printf("png2bmp read_png outf image\n");
+ failure++;
+ break;
+ }
+ /* ---------------------- */
+ if (copytime) cpyftime(arg, outf);
+ if (deletesrc) remove(arg);
+ /* ---------------------- */
+ success++;
+ break;
+
+ default:
+ ; /* Ignore unknown option */
+ }
+ }
+ printf("png2bmp failure is %d, success is %d, r_stdin is %d, w_stdout is %d \n", failure, success, r_stdin, w_stdout);
+ if (failure == 0 && success == 0) {
+ if (!r_stdin)
+ {
+ printf("png2bmp usage_exit\n");
+ usage_exit(argv[0], 255);
+ }
+ printf("png2bmp 111111111\n");
+ if (!read_png(NULL, &image))
+ {
+ printf("png2bmp read_png NULL\n");
+ return 1;
+ }
+ if (outnam[0] != '\0') {
+ printf("png2bmp write_bmp outnam is %s\n", outnam);
+ renbak(outnam);
+ return !write_bmp(outnam, &image);
+ } else if (w_stdout) {
+ printf("png2bmp write_bmp w_stdout\n");
+ return !write_bmp(NULL, &image);
+ } else {
+ printf("png2bmp write_bmp outf is %s\n", outf);
+ strcat(strcpy(outf, outdir), "___stdin.bmp");
+ renbak(outf);
+ return !write_bmp(outf, &image);
+ }
+ }
+ printf("png2bmp end failure is %d\n", failure);
+ return (failure > 255) ? 255 : failure;
+}
+
diff --git a/ap/app/qrencode/qrenc.c b/ap/app/qrencode/qrenc.c
new file mode 100644
index 0000000..f4b2c46
--- /dev/null
+++ b/ap/app/qrencode/qrenc.c
@@ -0,0 +1,1142 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
+
+/**
+ * qrencode - QR Code encoder
+ *
+ * QR Code encoding tool
+ * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <png.h>
+#include <getopt.h>
+
+#include "qrencode.h"
+
+#define INCHES_PER_METER (100.0/2.54)
+
+static int casesensitive = 1;
+static int eightbit = 0;
+static int version = 0;
+static int size = 3;
+static int margin = -1;
+static int dpi = 72;
+static int structured = 0;
+static int micro = 0;
+static QRecLevel level = QR_ECLEVEL_L;
+static QRencodeMode hint = QR_MODE_8;
+static unsigned int fg_color[4] = {0, 0, 0, 255};
+static unsigned int bg_color[4] = {255, 255, 255, 255};
+
+enum imageType {
+ PNG_TYPE,
+ EPS_TYPE,
+ SVG_TYPE,
+ ANSI_TYPE,
+ ANSI256_TYPE,
+ ASCII_TYPE,
+ ASCIIi_TYPE,
+ UTF8_TYPE,
+ ANSIUTF8_TYPE
+};
+
+static enum imageType image_type = PNG_TYPE;
+
+static const struct option options[] = {
+ {"help" , no_argument , NULL, 'h'},
+ {"output" , required_argument, NULL, 'o'},
+ {"level" , required_argument, NULL, 'l'},
+ {"size" , required_argument, NULL, 's'},
+ {"symversion" , required_argument, NULL, 'v'},
+ {"margin" , required_argument, NULL, 'm'},
+ {"dpi" , required_argument, NULL, 'd'},
+ {"type" , required_argument, NULL, 't'},
+ {"structured" , no_argument , NULL, 'S'},
+ {"kanji" , no_argument , NULL, 'k'},
+ {"casesensitive", no_argument , NULL, 'c'},
+ {"ignorecase" , no_argument , NULL, 'i'},
+ {"8bit" , no_argument , NULL, '8'},
+ {"micro" , no_argument , NULL, 'M'},
+ {"foreground" , required_argument, NULL, 'f'},
+ {"background" , required_argument, NULL, 'b'},
+ {"version" , no_argument , NULL, 'V'},
+ {NULL, 0, NULL, 0}
+};
+
+static char* optstring = "ho:l:s:v:m:d:t:Skci8MV";
+
+static void usage(int help, int longopt)
+{
+ fprintf(stderr,
+"qrencode version %s\n"
+"Copyright (C) 2006-2012 Kentaro Fukuchi\n", QRcode_APIVersionString());
+ if(help) {
+ if(longopt) {
+ fprintf(stderr,
+"Usage: qrencode [OPTION]... [STRING]\n"
+"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
+" -h, --help display the help message. -h displays only the help of short\n"
+" options.\n\n"
+" -o FILENAME, --output=FILENAME\n"
+" write image to FILENAME. If '-' is specified, the result\n"
+" will be output to standard output. If -S is given, structured\n"
+" symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
+" (suffix is removed from FILENAME, if specified)\n"
+" -s NUMBER, --size=NUMBER\n"
+" specify module size in dots (pixels). (default=3)\n\n"
+" -l {LMQH}, --level={LMQH}\n"
+" specify error correction level from L (lowest) to H (highest).\n"
+" (default=L)\n\n"
+" -v NUMBER, --symversion=NUMBER\n"
+" specify the version of the symbol. (default=auto)\n\n"
+" -m NUMBER, --margin=NUMBER\n"
+" specify the width of the margins. (default=4 (2 for Micro)))\n\n"
+" -d NUMBER, --dpi=NUMBER\n"
+" specify the DPI of the generated PNG. (default=72)\n\n"
+" -t {PNG,EPS,SVG,ANSI,ANSI256,ASCII,ASCIIi,UTF8,ANSIUTF8}, --type={PNG,EPS,\n"
+" SVG,ANSI,ANSI256,ASCII,ASCIIi,UTF8,ANSIUTF8}\n"
+" specify the type of the generated image. (default=PNG)\n\n"
+" -S, --structured\n"
+" make structured symbols. Version must be specified.\n\n"
+" -k, --kanji assume that the input text contains kanji (shift-jis).\n\n"
+" -c, --casesensitive\n"
+" encode lower-case alphabet characters in 8-bit mode. (default)\n\n"
+" -i, --ignorecase\n"
+" ignore case distinctions and use only upper-case characters.\n\n"
+" -8, --8bit encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n\n"
+" -M, --micro encode in a Micro QR Code. (experimental)\n\n"
+" --foreground=RRGGBB[AA]\n"
+" --background=RRGGBB[AA]\n"
+" specify foreground/background color in hexadecimal notation.\n"
+" 6-digit (RGB) or 8-digit (RGBA) form are supported.\n"
+" Color output support available only in PNG and SVG.\n"
+" -V, --version\n"
+" display the version number and copyrights of the qrencode.\n\n"
+" [STRING] input data. If it is not specified, data will be taken from\n"
+" standard input.\n"
+ );
+ } else {
+ fprintf(stderr,
+"Usage: qrencode [OPTION]... [STRING]\n"
+"Encode input data in a QR Code and save as a PNG or EPS image.\n\n"
+" -h display this message.\n"
+" --help display the usage of long options.\n"
+" -o FILENAME write image to FILENAME. If '-' is specified, the result\n"
+" will be output to standard output. If -S is given, structured\n"
+" symbols are written to FILENAME-01.png, FILENAME-02.png, ...\n"
+" (suffix is removed from FILENAME, if specified)\n"
+" -s NUMBER specify module size in dots (pixels). (default=3)\n"
+" -l {LMQH} specify error correction level from L (lowest) to H (highest).\n"
+" (default=L)\n"
+" -v NUMBER specify the version of the symbol. (default=auto)\n"
+" -m NUMBER specify the width of the margins. (default=4 (2 for Micro))\n"
+" -d NUMBER specify the DPI of the generated PNG. (default=72)\n"
+" -t {PNG,EPS,SVG,ANSI,ANSI256,ASCII,ASCIIi,UTF8,ANSIUTF8}\n"
+" specify the type of the generated image. (default=PNG)\n"
+" -S make structured symbols. Version must be specified.\n"
+" -k assume that the input text contains kanji (shift-jis).\n"
+" -c encode lower-case alphabet characters in 8-bit mode. (default)\n"
+" -i ignore case distinctions and use only upper-case characters.\n"
+" -8 encode entire data in 8-bit mode. -k, -c and -i will be ignored.\n"
+" -M encode in a Micro QR Code.\n"
+" --foreground=RRGGBB[AA]\n"
+" --background=RRGGBB[AA]\n"
+" specify foreground/background color in hexadecimal notation.\n"
+" 6-digit (RGB) or 8-digit (RGBA) form are supported.\n"
+" Color output support available only in PNG and SVG.\n"
+" -V display the version number and copyrights of the qrencode.\n"
+" [STRING] input data. If it is not specified, data will be taken from\n"
+" standard input.\n"
+ );
+ }
+ }
+}
+
+static int color_set(unsigned int color[4], const char *value)
+{
+ int len = strlen(value);
+ int count;
+ if(len == 6) {
+ count = sscanf(value, "%02x%02x%02x%n", &color[0], &color[1], &color[2], &len);
+ if(count < 3 || len != 6) {
+ return -1;
+ }
+ color[3] = 255;
+ } else if(len == 8) {
+ count = sscanf(value, "%02x%02x%02x%02x%n", &color[0], &color[1], &color[2], &color[3], &len);
+ if(count < 4 || len != 8) {
+ return -1;
+ }
+ } else {
+ return -1;
+ }
+ return 0;
+}
+
+#define MAX_DATA_SIZE (7090 * 16) /* from the specification */
+static unsigned char *readStdin(int *length)
+{
+ unsigned char *buffer;
+ int ret;
+
+ buffer = (unsigned char *)malloc(MAX_DATA_SIZE + 1);
+ if(buffer == NULL) {
+ fprintf(stderr, "Memory allocation failed.\n");
+ exit(EXIT_FAILURE);
+ }
+ ret = fread(buffer, 1, MAX_DATA_SIZE, stdin);
+ if(ret == 0) {
+ fprintf(stderr, "No input data.\n");
+ exit(EXIT_FAILURE);
+ }
+ if(feof(stdin) == 0) {
+ fprintf(stderr, "Input data is too large.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ buffer[ret] = '\0';
+ *length = ret;
+
+ return buffer;
+}
+
+static FILE *openFile(const char *outfile)
+{
+ FILE *fp;
+
+ if(outfile == NULL || (outfile[0] == '-' && outfile[1] == '\0')) {
+ fp = stdout;
+ } else {
+ fp = fopen(outfile, "wb");
+ if(fp == NULL) {
+ fprintf(stderr, "Failed to create file: %s\n", outfile);
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ return fp;
+}
+
+static int writePNG(QRcode *qrcode, const char *outfile)
+{
+ static FILE *fp; // avoid clobbering by setjmp.
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_colorp palette;
+ png_byte alpha_values[2];
+ unsigned char *row, *p, *q;
+ int x, y, xx, yy, bit;
+ int realwidth;
+
+ realwidth = (qrcode->width + margin * 2) * size;
+ row = (unsigned char *)malloc((realwidth + 7) / 8);
+ if(row == NULL) {
+ fprintf(stderr, "Failed to allocate memory.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if(outfile[0] == '-' && outfile[1] == '\0') {
+ fp = stdout;
+ } else {
+ fp = fopen(outfile, "wb");
+ if(fp == NULL) {
+ fprintf(stderr, "Failed to create file: %s\n", outfile);
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if(png_ptr == NULL) {
+ fprintf(stderr, "Failed to initialize PNG writer.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if(info_ptr == NULL) {
+ fprintf(stderr, "Failed to initialize PNG write.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if(setjmp(png_jmpbuf(png_ptr))) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ fprintf(stderr, "Failed to write PNG image.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ palette = (png_colorp) malloc(sizeof(png_color) * 2);
+ palette[0].red = fg_color[0];
+ palette[0].green = fg_color[1];
+ palette[0].blue = fg_color[2];
+ palette[1].red = bg_color[0];
+ palette[1].green = bg_color[1];
+ palette[1].blue = bg_color[2];
+ alpha_values[0] = fg_color[3];
+ alpha_values[1] = bg_color[3];
+ png_set_PLTE(png_ptr, info_ptr, palette, 2);
+ png_set_tRNS(png_ptr, info_ptr, alpha_values, 2, NULL);
+
+ png_init_io(png_ptr, fp);
+ png_set_IHDR(png_ptr, info_ptr,
+ realwidth, realwidth,
+ 1,
+ PNG_COLOR_TYPE_PALETTE,
+ PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+ png_set_pHYs(png_ptr, info_ptr,
+ dpi * INCHES_PER_METER,
+ dpi * INCHES_PER_METER,
+ PNG_RESOLUTION_METER);
+ png_write_info(png_ptr, info_ptr);
+
+ /* top margin */
+ memset(row, 0xff, (realwidth + 7) / 8);
+ for(y=0; y<margin * size; y++) {
+ png_write_row(png_ptr, row);
+ }
+
+ /* data */
+ p = qrcode->data;
+ for(y=0; y<qrcode->width; y++) {
+ bit = 7;
+ memset(row, 0xff, (realwidth + 7) / 8);
+ q = row;
+ q += margin * size / 8;
+ bit = 7 - (margin * size % 8);
+ for(x=0; x<qrcode->width; x++) {
+ for(xx=0; xx<size; xx++) {
+ *q ^= (*p & 1) << bit;
+ bit--;
+ if(bit < 0) {
+ q++;
+ bit = 7;
+ }
+ }
+ p++;
+ }
+ for(yy=0; yy<size; yy++) {
+ png_write_row(png_ptr, row);
+ }
+ }
+ /* bottom margin */
+ memset(row, 0xff, (realwidth + 7) / 8);
+ for(y=0; y<margin * size; y++) {
+ png_write_row(png_ptr, row);
+ }
+
+ png_write_end(png_ptr, info_ptr);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+
+ fclose(fp);
+ free(row);
+ if(palette != NULL)
+ {
+ free(palette);
+ }
+ return 0;
+}
+
+static int writeEPS(QRcode *qrcode, const char *outfile)
+{
+ FILE *fp;
+ unsigned char *row, *p;
+ int x, y, yy;
+ int realwidth;
+
+ fp = openFile(outfile);
+
+ realwidth = (qrcode->width + margin * 2) * size;
+ /* EPS file header */
+ fprintf(fp, "%%!PS-Adobe-2.0 EPSF-1.2\n"
+ "%%%%BoundingBox: 0 0 %d %d\n"
+ "%%%%Pages: 1 1\n"
+ "%%%%EndComments\n", realwidth, realwidth);
+ /* draw point */
+ fprintf(fp, "/p { "
+ "moveto "
+ "0 1 rlineto "
+ "1 0 rlineto "
+ "0 -1 rlineto "
+ "fill "
+ "} bind def "
+ "%d %d scale ", size, size);
+
+ /* data */
+ p = qrcode->data;
+ for(y=0; y<qrcode->width; y++) {
+ row = (p+(y*qrcode->width));
+ yy = (margin + qrcode->width - y - 1);
+
+ for(x=0; x<qrcode->width; x++) {
+ if(*(row+x)&0x1) {
+ fprintf(fp, "%d %d p ", margin + x, yy);
+ }
+ }
+ }
+
+ fprintf(fp, "\n%%%%EOF\n");
+ fclose(fp);
+
+ return 0;
+}
+
+static void writeSVG_writeRect(FILE *fp, int x, int y, int width, char* col, float opacity)
+{
+ if(fg_color[3] != 255) {
+ fprintf(fp, "\t\t\t<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"1\" "\
+ "fill=\"#%s\" fill-opacity=\"%f\" />\n",
+ x, y, width, col, opacity );
+ } else {
+ fprintf(fp, "\t\t\t<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"1\" "\
+ "fill=\"#%s\" />\n",
+ x, y, width, col );
+ }
+}
+
+static int writeSVG( QRcode *qrcode, const char *outfile )
+{
+ FILE *fp;
+ unsigned char *row, *p;
+ int x, y, x0, pen;
+ int symwidth, realwidth;
+ float scale;
+ char fg[7], bg[7];
+ float fg_opacity;
+ float bg_opacity;
+
+ fp = openFile(outfile);
+
+ scale = dpi * INCHES_PER_METER / 100.0;
+
+ symwidth = qrcode->width + margin * 2;
+ realwidth = symwidth * size;
+
+ snprintf(fg, 7, "%02x%02x%02x", fg_color[0], fg_color[1], fg_color[2]);
+ snprintf(bg, 7, "%02x%02x%02x", bg_color[0], bg_color[1], bg_color[2]);
+ fg_opacity = (float)fg_color[3] / 255;
+ bg_opacity = (float)bg_color[3] / 255;
+
+ /* XML declaration */
+ fputs( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n", fp );
+
+ /* DTD
+ No document type specified because "while a DTD is provided in [the SVG]
+ specification, the use of DTDs for validating XML documents is known to be
+ problematic. In particular, DTDs do not handle namespaces gracefully. It
+ is *not* recommended that a DOCTYPE declaration be included in SVG
+ documents."
+ http://www.w3.org/TR/2003/REC-SVG11-20030114/intro.html#Namespace
+ */
+
+ /* Vanity remark */
+ fprintf( fp, "<!-- Created with qrencode %s (http://fukuchi.org/works/qrencode/index.html.en) -->\n",
+ QRcode_APIVersionString() );
+
+ /* SVG code start */
+ fprintf( fp, "<svg width=\"%0.2fcm\" height=\"%0.2fcm\" viewBox=\"0 0 %d %d\""\
+ " preserveAspectRatio=\"none\" version=\"1.1\""\
+ " xmlns=\"http://www.w3.org/2000/svg\">\n",
+ realwidth / scale, realwidth / scale, symwidth, symwidth
+ );
+
+ /* Make named group */
+ fputs( "\t<g id=\"QRcode\">\n", fp );
+
+ /* Make solid background */
+ if(bg_color[3] != 255) {
+ fprintf(fp, "\t\t<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" fill-opacity=\"%f\" />\n", symwidth, symwidth, bg, bg_opacity);
+ } else {
+ fprintf(fp, "\t\t<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", symwidth, symwidth, bg);
+ }
+
+ /* Create new viewbox for QR data */
+ fputs( "\t\t<g id=\"Pattern\">\n", fp);
+
+ /* Write data */
+ p = qrcode->data;
+ for(y=0; y<qrcode->width; y++) {
+ row = (p+(y*qrcode->width));
+
+ /* simple RLE */
+ pen = 0;
+ x0 = 0;
+ for(x=0; x<qrcode->width; x++) {
+ if( !pen ) {
+ pen = *(row+x)&0x1;
+ x0 = x;
+ } else {
+ if(!(*(row+x)&0x1)) {
+ writeSVG_writeRect(fp, x0 + margin, y + margin, x-x0, fg, fg_opacity);
+ pen = 0;
+ }
+ }
+ }
+ if( pen ) {
+ writeSVG_writeRect(fp, x0 + margin, y + margin, qrcode->width - x0, fg, fg_opacity);
+ }
+ }
+
+ /* Close QR data viewbox */
+ fputs( "\t\t</g>\n", fp );
+
+ /* Close group */
+ fputs( "\t</g>\n", fp );
+
+ /* Close SVG code */
+ fputs( "</svg>\n", fp );
+ fclose( fp );
+
+ return 0;
+}
+
+static void writeANSI_margin(FILE* fp, int realwidth,
+ char* buffer, int buffer_s,
+ char* white, int white_s )
+{
+ int y;
+
+ strncpy(buffer, white, white_s);
+ memset(buffer + white_s, ' ', realwidth * 2);
+ strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
+ for(y=0; y<margin; y++ ){
+ fputs(buffer, fp);
+ }
+}
+
+static int writeANSI(QRcode *qrcode, const char *outfile)
+{
+ FILE *fp;
+ unsigned char *row, *p;
+ int x, y;
+ int realwidth;
+ int last;
+
+ char *white, *black, *buffer;
+ int white_s, black_s, buffer_s;
+
+ if( image_type == ANSI256_TYPE ){
+ /* codes for 256 color compatible terminals */
+ white = "\033[48;5;231m";
+ white_s = 11;
+ black = "\033[48;5;16m";
+ black_s = 10;
+ } else {
+ white = "\033[47m";
+ white_s = 5;
+ black = "\033[40m";
+ black_s = 5;
+ }
+
+ size = 1;
+
+ fp = openFile(outfile);
+
+ realwidth = (qrcode->width + margin * 2) * size;
+ buffer_s = ( realwidth * white_s ) * 2;
+ buffer = (char *)malloc( buffer_s );
+ if(buffer == NULL) {
+ fprintf(stderr, "Failed to allocate memory.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* top margin */
+ writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
+
+ /* data */
+ p = qrcode->data;
+ for(y=0; y<qrcode->width; y++) {
+ row = (p+(y*qrcode->width));
+
+ bzero( buffer, buffer_s );
+ strncpy( buffer, white, white_s );
+ for(x=0; x<margin; x++ ){
+ strncat( buffer, " ", 2 );
+ }
+ last = 0;
+
+ for(x=0; x<qrcode->width; x++) {
+ if(*(row+x)&0x1) {
+ if( last != 1 ){
+ strncat( buffer, black, black_s );
+ last = 1;
+ }
+ } else {
+ if( last != 0 ){
+ strncat( buffer, white, white_s );
+ last = 0;
+ }
+ }
+ strncat( buffer, " ", 2 );
+ }
+
+ if( last != 0 ){
+ strncat( buffer, white, white_s );
+ }
+ for(x=0; x<margin; x++ ){
+ strncat( buffer, " ", 2 );
+ }
+ strncat( buffer, "\033[0m\n", 5 );
+ fputs( buffer, fp );
+ }
+
+ /* bottom margin */
+ writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);
+
+ fclose(fp);
+ free(buffer);
+
+ return 0;
+}
+
+static void writeUTF8_margin(FILE* fp, int realwidth,
+ const char* white, const char *reset,
+ int use_ansi)
+{
+ int x, y;
+
+ for (y = 0; y < margin/2; y++) {
+ fputs(white, fp);
+ for (x = 0; x < realwidth; x++)
+ fputs("\342\226\210", fp);
+ fputs(reset, fp);
+ fputc('\n', fp);
+ }
+}
+
+static int writeUTF8(QRcode *qrcode, const char *outfile, int use_ansi)
+{
+ FILE *fp;
+ int x, y;
+ int realwidth;
+ unsigned char *p;
+ const char *white, *reset;
+
+ if (use_ansi){
+ white = "\033[40;37;1m";
+ reset = "\033[0m";
+ } else {
+ white = "";
+ reset = "";
+ }
+
+ fp = openFile(outfile);
+
+ realwidth = (qrcode->width + margin * 2);
+
+ /* top margin */
+ writeUTF8_margin(fp, realwidth, white, reset, use_ansi);
+
+ /* data */
+ p = qrcode->data;
+ for(y = 0; y < qrcode->width; y += 2) {
+ unsigned char *row1, *row2;
+ row1 = p + y*qrcode->width;
+ row2 = p + y*qrcode->width + qrcode->width;
+
+ fputs(white, fp);
+
+ for (x = 0; x < margin; x++)
+ fputs("\342\226\210", fp);
+
+ for (x = 0; x < qrcode->width; x++) {
+ if ((*(row1 + x) & 1) && (*(row2 + x) & 1))
+ fputc(' ', fp);
+ else if (*(row1 + x) & 1)
+ fputs("\342\226\204", fp);
+ else if (*(row2 + x) & 1)
+ fputs("\342\226\200", fp);
+ else
+ fputs("\342\226\210", fp);
+ }
+
+ for (x = 0; x < margin; x++)
+ fputs("\342\226\210", fp);
+
+ fputs(reset, fp);
+ fputc('\n', fp);
+ }
+
+ /* bottom margin */
+ writeUTF8_margin(fp, realwidth, white, reset, use_ansi);
+
+ fclose(fp);
+
+ return 0;
+}
+
+static void writeASCII_margin(FILE* fp, int realwidth, char* buffer, int buffer_s, int invert)
+{
+ int y, h;
+
+ h = margin;
+
+ memset(buffer, (invert?'#':' '), realwidth);
+ buffer[realwidth] = '\n';
+ buffer[realwidth + 1] = '\0';
+ for(y=0; y<h; y++ ){
+ fputs(buffer, fp);
+ }
+}
+
+static int writeASCII(QRcode *qrcode, const char *outfile, int invert)
+{
+ FILE *fp;
+ unsigned char *row;
+ int x, y;
+ int realwidth;
+ char *buffer, *p;
+ int buffer_s;
+ char black = '#';
+ char white = ' ';
+
+ if(invert) {
+ black = ' ';
+ white = '#';
+ }
+
+ size = 1;
+
+ fp = openFile(outfile);
+
+ realwidth = (qrcode->width + margin * 2) * 2;
+ buffer_s = realwidth + 1;
+ buffer = (char *)malloc( buffer_s );
+ if(buffer == NULL) {
+ fprintf(stderr, "Failed to allocate memory.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* top margin */
+ writeASCII_margin(fp, realwidth, buffer, buffer_s, invert);
+
+ /* data */
+ for(y=0; y<qrcode->width; y++) {
+ row = qrcode->data+(y*qrcode->width);
+ p = buffer;
+
+ memset(p, white, margin * 2);
+ p += margin * 2;
+
+ for(x=0; x<qrcode->width; x++) {
+ if(row[x]&0x1) {
+ *p++ = black;
+ *p++ = black;
+ } else {
+ *p++ = white;
+ *p++ = white;
+ }
+ }
+
+ memset(p, white, margin * 2);
+ p += margin * 2;
+ *p++ = '\n';
+ *p++ = '\0';
+ fputs( buffer, fp );
+ }
+
+ /* bottom margin */
+ writeASCII_margin(fp, realwidth, buffer, buffer_s, invert);
+
+ fclose(fp);
+ free(buffer);
+
+ return 0;
+}
+
+static QRcode *encode(const unsigned char *intext, int length)
+{
+ QRcode *code;
+
+ if(micro) {
+ if(eightbit) {
+ code = QRcode_encodeDataMQR(length, intext, version, level);
+ } else {
+ code = QRcode_encodeStringMQR((char *)intext, version, level, hint, casesensitive);
+ }
+ } else {
+ if(eightbit) {
+ code = QRcode_encodeData(length, intext, version, level);
+ } else {
+ code = QRcode_encodeString((char *)intext, version, level, hint, casesensitive);
+ }
+ }
+
+ return code;
+}
+
+static void qrencode(const unsigned char *intext, int length, const char *outfile)
+{
+ QRcode *qrcode;
+
+ qrcode = encode(intext, length);
+ if(qrcode == NULL) {
+ perror("Failed to encode the input data");
+ exit(EXIT_FAILURE);
+ }
+ switch(image_type) {
+ case PNG_TYPE:
+ writePNG(qrcode, outfile);
+ break;
+ case EPS_TYPE:
+ writeEPS(qrcode, outfile);
+ break;
+ case SVG_TYPE:
+ writeSVG(qrcode, outfile);
+ break;
+ case ANSI_TYPE:
+ case ANSI256_TYPE:
+ writeANSI(qrcode, outfile);
+ break;
+ case ASCIIi_TYPE:
+ writeASCII(qrcode, outfile, 1);
+ break;
+ case ASCII_TYPE:
+ writeASCII(qrcode, outfile, 0);
+ break;
+ case UTF8_TYPE:
+ writeUTF8(qrcode, outfile, 0);
+ break;
+ case ANSIUTF8_TYPE:
+ writeUTF8(qrcode, outfile, 1);
+ break;
+ default:
+ fprintf(stderr, "Unknown image type.\n");
+ exit(EXIT_FAILURE);
+ }
+ QRcode_free(qrcode);
+}
+
+static QRcode_List *encodeStructured(const unsigned char *intext, int length)
+{
+ QRcode_List *list;
+
+ if(eightbit) {
+ list = QRcode_encodeDataStructured(length, intext, version, level);
+ } else {
+ list = QRcode_encodeStringStructured((char *)intext, version, level, hint, casesensitive);
+ }
+
+ return list;
+}
+
+static void qrencodeStructured(const unsigned char *intext, int length, const char *outfile)
+{
+ QRcode_List *qrlist, *p;
+ char filename[FILENAME_MAX];
+ char *base, *q, *suffix = NULL;
+ const char *type_suffix;
+ int i = 1;
+ size_t suffix_size;
+
+ switch(image_type) {
+ case PNG_TYPE:
+ type_suffix = ".png";
+ break;
+ case EPS_TYPE:
+ type_suffix = ".eps";
+ break;
+ case SVG_TYPE:
+ type_suffix = ".svg";
+ break;
+ case ANSI_TYPE:
+ case ANSI256_TYPE:
+ case ASCII_TYPE:
+ case UTF8_TYPE:
+ case ANSIUTF8_TYPE:
+ type_suffix = ".txt";
+ break;
+ default:
+ fprintf(stderr, "Unknown image type.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if(outfile == NULL) {
+ fprintf(stderr, "An output filename must be specified to store the structured images.\n");
+ exit(EXIT_FAILURE);
+ }
+ base = strdup(outfile);
+ if(base == NULL) {
+ fprintf(stderr, "Failed to allocate memory.\n");
+ exit(EXIT_FAILURE);
+ }
+ suffix_size = strlen(type_suffix);
+ if(strlen(base) > suffix_size) {
+ q = base + strlen(base) - suffix_size;
+ if(strcasecmp(type_suffix, q) == 0) {
+ suffix = strdup(q);
+ *q = '\0';
+ }
+ }
+
+ qrlist = encodeStructured(intext, length);
+ if(qrlist == NULL) {
+ perror("Failed to encode the input data");
+ exit(EXIT_FAILURE);
+ }
+
+ for(p = qrlist; p != NULL; p = p->next) {
+ if(p->code == NULL) {
+ fprintf(stderr, "Failed to encode the input data.\n");
+ exit(EXIT_FAILURE);
+ }
+ if(suffix) {
+ snprintf(filename, FILENAME_MAX, "%s-%02d%s", base, i, suffix);
+ } else {
+ snprintf(filename, FILENAME_MAX, "%s-%02d", base, i);
+ }
+ switch(image_type) {
+ case PNG_TYPE:
+ writePNG(p->code, filename);
+ break;
+ case EPS_TYPE:
+ writeEPS(p->code, filename);
+ break;
+ case SVG_TYPE:
+ writeSVG(p->code, filename);
+ break;
+ case ANSI_TYPE:
+ case ANSI256_TYPE:
+ writeANSI(p->code, filename);
+ break;
+ case ASCIIi_TYPE:
+ writeASCII(p->code, filename, 1);
+ break;
+ case ASCII_TYPE:
+ writeASCII(p->code, filename, 0);
+ break;
+ case UTF8_TYPE:
+ writeUTF8(p->code, filename, 0);
+ break;
+ case ANSIUTF8_TYPE:
+ writeUTF8(p->code, filename, 0);
+ break;
+
+ default:
+ fprintf(stderr, "Unknown image type.\n");
+ exit(EXIT_FAILURE);
+ }
+ i++;
+ }
+
+ free(base);
+ if(suffix) {
+ free(suffix);
+ }
+
+ QRcode_List_free(qrlist);
+}
+
+int main(int argc, char **argv)
+{
+ int opt, lindex = -1;
+ char *outfile = NULL;
+ unsigned char *intext = NULL;
+ int length = 0;
+
+ while((opt = getopt_long(argc, argv, optstring, options, &lindex)) != -1) {
+ switch(opt) {
+ case 'h':
+ if(lindex == 0) {
+ usage(1, 1);
+ } else {
+ usage(1, 0);
+ }
+ exit(EXIT_SUCCESS);
+ break;
+ case 'o':
+ outfile = optarg;
+ break;
+ case 's':
+ size = atoi(optarg);
+ if(size <= 0) {
+ fprintf(stderr, "Invalid size: %d\n", size);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'v':
+ version = atoi(optarg);
+ if(version < 0) {
+ fprintf(stderr, "Invalid version: %d\n", version);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'l':
+ switch(*optarg) {
+ case 'l':
+ case 'L':
+ level = QR_ECLEVEL_L;
+ break;
+ case 'm':
+ case 'M':
+ level = QR_ECLEVEL_M;
+ break;
+ case 'q':
+ case 'Q':
+ level = QR_ECLEVEL_Q;
+ break;
+ case 'h':
+ case 'H':
+ level = QR_ECLEVEL_H;
+ break;
+ default:
+ fprintf(stderr, "Invalid level: %s\n", optarg);
+ exit(EXIT_FAILURE);
+ break;
+ }
+ break;
+ case 'm':
+ margin = atoi(optarg);
+ if(margin < 0) {
+ fprintf(stderr, "Invalid margin: %d\n", margin);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'd':
+ dpi = atoi(optarg);
+ if( dpi < 0 ) {
+ fprintf(stderr, "Invalid DPI: %d\n", dpi);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 't':
+ if(strcasecmp(optarg, "png") == 0) {
+ image_type = PNG_TYPE;
+ } else if(strcasecmp(optarg, "eps") == 0) {
+ image_type = EPS_TYPE;
+ } else if(strcasecmp(optarg, "svg") == 0) {
+ image_type = SVG_TYPE;
+ } else if(strcasecmp(optarg, "ansi") == 0) {
+ image_type = ANSI_TYPE;
+ } else if(strcasecmp(optarg, "ansi256") == 0) {
+ image_type = ANSI256_TYPE;
+ } else if(strcasecmp(optarg, "asciii") == 0) {
+ image_type = ASCIIi_TYPE;
+ } else if(strcasecmp(optarg, "ascii") == 0) {
+ image_type = ASCII_TYPE;
+ } else if(strcasecmp(optarg, "utf8") == 0) {
+ image_type = UTF8_TYPE;
+ } else if(strcasecmp(optarg, "ansiutf8") == 0) {
+ image_type = ANSIUTF8_TYPE;
+ } else {
+ fprintf(stderr, "Invalid image type: %s\n", optarg);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'S':
+ structured = 1;
+ case 'k':
+ hint = QR_MODE_KANJI;
+ break;
+ case 'c':
+ casesensitive = 1;
+ break;
+ case 'i':
+ casesensitive = 0;
+ break;
+ case '8':
+ eightbit = 1;
+ break;
+ case 'M':
+ micro = 1;
+ break;
+ case 'f':
+ if(color_set(fg_color, optarg)) {
+ fprintf(stderr, "Invalid foreground color value.\n");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'b':
+ if(color_set(bg_color, optarg)) {
+ fprintf(stderr, "Invalid background color value.\n");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'V':
+ usage(0, 0);
+ exit(EXIT_SUCCESS);
+ break;
+ default:
+ fprintf(stderr, "Try `qrencode --help' for more information.\n");
+ exit(EXIT_FAILURE);
+ break;
+ }
+ }
+
+ if(argc == 1) {
+ usage(1, 0);
+ exit(EXIT_SUCCESS);
+ }
+
+ if(outfile == NULL && image_type == PNG_TYPE) {
+ fprintf(stderr, "No output filename is given.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if(optind < argc) {
+ intext = (unsigned char *)argv[optind];
+ length = strlen((char *)intext);
+ }
+ if(intext == NULL) {
+ intext = readStdin(&length);
+ }
+
+ if(micro && version > MQRSPEC_VERSION_MAX) {
+ fprintf(stderr, "Version should be less or equal to %d.\n", MQRSPEC_VERSION_MAX);
+ exit(EXIT_FAILURE);
+ } else if(!micro && version > QRSPEC_VERSION_MAX) {
+ fprintf(stderr, "Version should be less or equal to %d.\n", QRSPEC_VERSION_MAX);
+ exit(EXIT_FAILURE);
+ }
+
+ if(margin < 0) {
+ if(micro) {
+ margin = 2;
+ } else {
+ margin = 4;
+ }
+ }
+
+ if(micro) {
+ if(version == 0) {
+ fprintf(stderr, "Version must be specified to encode a Micro QR Code symbol.\n");
+ exit(EXIT_FAILURE);
+ }
+ if(structured) {
+ fprintf(stderr, "Micro QR Code does not support structured symbols.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ if(structured) {
+ if(version == 0) {
+ fprintf(stderr, "Version must be specified to encode structured symbols.\n");
+ exit(EXIT_FAILURE);
+ }
+ qrencodeStructured(intext, length, outfile);
+ } else {
+ qrencode(intext, length, outfile);
+ }
+
+ return 0;
+}
diff --git a/ap/app/qrencode/qrencode.c b/ap/app/qrencode/qrencode.c
new file mode 100644
index 0000000..466c02a
--- /dev/null
+++ b/ap/app/qrencode/qrencode.c
@@ -0,0 +1,927 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "qrencode.h"
+#include "qrspec.h"
+#include "mqrspec.h"
+#include "bitstream.h"
+#include "qrinput.h"
+#include "rscode.h"
+#include "split.h"
+#include "mask.h"
+#include "mmask.h"
+
+/******************************************************************************
+ * Raw code
+ *****************************************************************************/
+
+typedef struct {
+ int dataLength;
+ unsigned char *data;
+ int eccLength;
+ unsigned char *ecc;
+} RSblock;
+
+typedef struct {
+ int version;
+ int dataLength;
+ int eccLength;
+ unsigned char *datacode;
+ unsigned char *ecccode;
+ int b1;
+ int blocks;
+ RSblock *rsblock;
+ int count;
+} QRRawCode;
+
+static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs)
+{
+ block->dataLength = dl;
+ block->data = data;
+ block->eccLength = el;
+ block->ecc = ecc;
+
+ encode_rs_char(rs, data, ecc);
+}
+
+static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
+{
+ int i;
+ RSblock *block;
+ unsigned char *dp, *ep;
+ RS *rs;
+ int el, dl;
+
+ dl = QRspec_rsDataCodes1(spec);
+ el = QRspec_rsEccCodes1(spec);
+ rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
+ if(rs == NULL) return -1;
+
+ block = blocks;
+ dp = data;
+ ep = ecc;
+ for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
+ RSblock_initBlock(block, dl, dp, el, ep, rs);
+ dp += dl;
+ ep += el;
+ block++;
+ }
+
+ if(QRspec_rsBlockNum2(spec) == 0) return 0;
+
+ dl = QRspec_rsDataCodes2(spec);
+ el = QRspec_rsEccCodes2(spec);
+ rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
+ if(rs == NULL) return -1;
+ for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
+ RSblock_initBlock(block, dl, dp, el, ep, rs);
+ dp += dl;
+ ep += el;
+ block++;
+ }
+
+ return 0;
+}
+
+static void QRraw_free(QRRawCode *raw);
+static QRRawCode *QRraw_new(QRinput *input)
+{
+ QRRawCode *raw;
+ int spec[5], ret;
+
+ raw = (QRRawCode *)malloc(sizeof(QRRawCode));
+ if(raw == NULL) return NULL;
+
+ raw->datacode = QRinput_getByteStream(input);
+ if(raw->datacode == NULL) {
+ free(raw);
+ return NULL;
+ }
+
+ QRspec_getEccSpec(input->version, input->level, spec);
+
+ raw->version = input->version;
+ raw->b1 = QRspec_rsBlockNum1(spec);
+ raw->dataLength = QRspec_rsDataLength(spec);
+ raw->eccLength = QRspec_rsEccLength(spec);
+ raw->ecccode = (unsigned char *)malloc(raw->eccLength);
+ if(raw->ecccode == NULL) {
+ free(raw->datacode);
+ free(raw);
+ return NULL;
+ }
+
+ raw->blocks = QRspec_rsBlockNum(spec);
+ raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock));
+ if(raw->rsblock == NULL) {
+ QRraw_free(raw);
+ return NULL;
+ }
+ ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
+ if(ret < 0) {
+ QRraw_free(raw);
+ return NULL;
+ }
+
+ raw->count = 0;
+
+ return raw;
+}
+
+/**
+ * Return a code (byte).
+ * This function can be called iteratively.
+ * @param raw raw code.
+ * @return code
+ */
+static unsigned char QRraw_getCode(QRRawCode *raw)
+{
+ int col, row;
+ unsigned char ret;
+
+ if(raw->count < raw->dataLength) {
+ row = raw->count % raw->blocks;
+ col = raw->count / raw->blocks;
+ if(col >= raw->rsblock[0].dataLength) {
+ row += raw->b1;
+ }
+ ret = raw->rsblock[row].data[col];
+ } else if(raw->count < raw->dataLength + raw->eccLength) {
+ row = (raw->count - raw->dataLength) % raw->blocks;
+ col = (raw->count - raw->dataLength) / raw->blocks;
+ ret = raw->rsblock[row].ecc[col];
+ } else {
+ return 0;
+ }
+ raw->count++;
+ return ret;
+}
+
+static void QRraw_free(QRRawCode *raw)
+{
+ if(raw != NULL) {
+ free(raw->datacode);
+ free(raw->ecccode);
+ free(raw->rsblock);
+ free(raw);
+ }
+}
+
+/******************************************************************************
+ * Raw code for Micro QR Code
+ *****************************************************************************/
+
+typedef struct {
+ int version;
+ int dataLength;
+ int eccLength;
+ unsigned char *datacode;
+ unsigned char *ecccode;
+ RSblock *rsblock;
+ int oddbits;
+ int count;
+} MQRRawCode;
+
+static void MQRraw_free(MQRRawCode *raw);
+static MQRRawCode *MQRraw_new(QRinput *input)
+{
+ MQRRawCode *raw;
+ RS *rs;
+
+ raw = (MQRRawCode *)malloc(sizeof(MQRRawCode));
+ if(raw == NULL) return NULL;
+
+ raw->version = input->version;
+ raw->dataLength = MQRspec_getDataLength(input->version, input->level);
+ raw->eccLength = MQRspec_getECCLength(input->version, input->level);
+ raw->oddbits = raw->dataLength * 8 - MQRspec_getDataLengthBit(input->version, input->level);
+ raw->datacode = QRinput_getByteStream(input);
+ if(raw->datacode == NULL) {
+ free(raw);
+ return NULL;
+ }
+ raw->ecccode = (unsigned char *)malloc(raw->eccLength);
+ if(raw->ecccode == NULL) {
+ free(raw->datacode);
+ free(raw);
+ return NULL;
+ }
+
+ raw->rsblock = (RSblock *)calloc(1, sizeof(RSblock));
+ if(raw->rsblock == NULL) {
+ MQRraw_free(raw);
+ return NULL;
+ }
+
+ rs = init_rs(8, 0x11d, 0, 1, raw->eccLength, 255 - raw->dataLength - raw->eccLength);
+ if(rs == NULL) {
+ MQRraw_free(raw);
+ return NULL;
+ }
+
+ RSblock_initBlock(raw->rsblock, raw->dataLength, raw->datacode, raw->eccLength, raw->ecccode, rs);
+
+ raw->count = 0;
+
+ return raw;
+}
+
+/**
+ * Return a code (byte).
+ * This function can be called iteratively.
+ * @param raw raw code.
+ * @return code
+ */
+static unsigned char MQRraw_getCode(MQRRawCode *raw)
+{
+ unsigned char ret;
+
+ if(raw->count < raw->dataLength) {
+ ret = raw->datacode[raw->count];
+ } else if(raw->count < raw->dataLength + raw->eccLength) {
+ ret = raw->ecccode[raw->count - raw->dataLength];
+ } else {
+ return 0;
+ }
+ raw->count++;
+ return ret;
+}
+
+static void MQRraw_free(MQRRawCode *raw)
+{
+ if(raw != NULL) {
+ free(raw->datacode);
+ free(raw->ecccode);
+ free(raw->rsblock);
+ free(raw);
+ }
+}
+
+
+/******************************************************************************
+ * Frame filling
+ *****************************************************************************/
+
+typedef struct {
+ int width;
+ unsigned char *frame;
+ int x, y;
+ int dir;
+ int bit;
+ int mqr;
+} FrameFiller;
+
+static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr)
+{
+ FrameFiller *filler;
+
+ filler = (FrameFiller *)malloc(sizeof(FrameFiller));
+ if(filler == NULL) return NULL;
+ filler->width = width;
+ filler->frame = frame;
+ filler->x = width - 1;
+ filler->y = width - 1;
+ filler->dir = -1;
+ filler->bit = -1;
+ filler->mqr = mqr;
+
+ return filler;
+}
+
+static unsigned char *FrameFiller_next(FrameFiller *filler)
+{
+ unsigned char *p;
+ int x, y, w;
+
+ if(filler->bit == -1) {
+ filler->bit = 0;
+ return filler->frame + filler->y * filler->width + filler->x;
+ }
+
+ x = filler->x;
+ y = filler->y;
+ p = filler->frame;
+ w = filler->width;
+
+ if(filler->bit == 0) {
+ x--;
+ filler->bit++;
+ } else {
+ x++;
+ y += filler->dir;
+ filler->bit--;
+ }
+
+ if(filler->dir < 0) {
+ if(y < 0) {
+ y = 0;
+ x -= 2;
+ filler->dir = 1;
+ if(!filler->mqr && x == 6) {
+ x--;
+ y = 9;
+ }
+ }
+ } else {
+ if(y == w) {
+ y = w - 1;
+ x -= 2;
+ filler->dir = -1;
+ if(!filler->mqr && x == 6) {
+ x--;
+ y -= 8;
+ }
+ }
+ }
+ if(x < 0 || y < 0) return NULL;
+
+ filler->x = x;
+ filler->y = y;
+
+ if(p[y * w + x] & 0x80) {
+ // This tail recursion could be optimized.
+ return FrameFiller_next(filler);
+ }
+ return &p[y * w + x];
+}
+
+#ifdef WITH_TESTS
+extern unsigned char *FrameFiller_test(int version)
+{
+ int width;
+ unsigned char *frame, *p;
+ FrameFiller *filler;
+ int i, length;
+
+ width = QRspec_getWidth(version);
+ frame = QRspec_newFrame(version);
+ if(frame == NULL) return NULL;
+ filler = FrameFiller_new(width, frame, 0);
+ if(filler == NULL) {
+ free(frame);
+ return NULL;
+ }
+ length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
+ + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
+ + QRspec_getRemainder(version);
+ for(i=0; i<length; i++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) {
+ free(filler);
+ free(frame);
+ return NULL;
+ }
+ *p = (unsigned char)(i & 0x7f) | 0x80;
+ }
+ free(filler);
+ return frame;
+}
+
+extern unsigned char *FrameFiller_testMQR(int version)
+{
+ int width;
+ unsigned char *frame, *p;
+ FrameFiller *filler;
+ int i, length;
+
+ width = MQRspec_getWidth(version);
+ frame = MQRspec_newFrame(version);
+ if(frame == NULL) return NULL;
+ filler = FrameFiller_new(width, frame, 1);
+ if(filler == NULL) {
+ free(frame);
+ return NULL;
+ }
+ length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
+ + MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
+ for(i=0; i<length; i++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) {
+ fprintf(stderr, "Frame filler run over the frame!\n");
+ free(filler);
+ return frame;
+ }
+ *p = (unsigned char)(i & 0x7f) | 0x80;
+ }
+ free(filler);
+ return frame;
+}
+#endif
+
+
+/******************************************************************************
+ * QR-code encoding
+ *****************************************************************************/
+
+static QRcode *QRcode_new(int version, int width, unsigned char *data)
+{
+ QRcode *qrcode;
+
+ qrcode = (QRcode *)malloc(sizeof(QRcode));
+ if(qrcode == NULL) return NULL;
+
+ qrcode->version = version;
+ qrcode->width = width;
+ qrcode->data = data;
+
+ return qrcode;
+}
+
+void QRcode_free(QRcode *qrcode)
+{
+ if(qrcode != NULL) {
+ free(qrcode->data);
+ free(qrcode);
+ }
+}
+
+static QRcode *QRcode_encodeMask(QRinput *input, int mask)
+{
+ int width, version;
+ QRRawCode *raw;
+ unsigned char *frame, *masked, *p, code, bit;
+ FrameFiller *filler;
+ int i, j;
+ QRcode *qrcode = NULL;
+
+ if(input->mqr) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(input->version < 0 || input->version > QRSPEC_VERSION_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(input->level > QR_ECLEVEL_H) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ raw = QRraw_new(input);
+ if(raw == NULL) return NULL;
+
+ version = raw->version;
+ width = QRspec_getWidth(version);
+ frame = QRspec_newFrame(version);
+ if(frame == NULL) {
+ QRraw_free(raw);
+ return NULL;
+ }
+ filler = FrameFiller_new(width, frame, 0);
+ if(filler == NULL) {
+ QRraw_free(raw);
+ free(frame);
+ return NULL;
+ }
+
+ /* inteleaved data and ecc codes */
+ for(i=0; i<raw->dataLength + raw->eccLength; i++) {
+ code = QRraw_getCode(raw);
+ bit = 0x80;
+ for(j=0; j<8; j++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) goto EXIT;
+ *p = 0x02 | ((bit & code) != 0);
+ bit = bit >> 1;
+ }
+ }
+ QRraw_free(raw);
+ raw = NULL;
+ /* remainder bits */
+ j = QRspec_getRemainder(version);
+ for(i=0; i<j; i++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) goto EXIT;
+ *p = 0x02;
+ }
+
+ /* masking */
+ if(mask == -2) { // just for debug purpose
+ masked = (unsigned char *)malloc(width * width);
+ memcpy(masked, frame, width * width);
+ } else if(mask < 0) {
+ masked = Mask_mask(width, frame, input->level);
+ } else {
+ masked = Mask_makeMask(width, frame, mask, input->level);
+ }
+ if(masked == NULL) {
+ goto EXIT;
+ }
+ qrcode = QRcode_new(version, width, masked);
+
+EXIT:
+ QRraw_free(raw);
+ free(filler);
+ free(frame);
+ return qrcode;
+}
+
+static QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
+{
+ int width, version;
+ MQRRawCode *raw;
+ unsigned char *frame, *masked, *p, code, bit;
+ FrameFiller *filler;
+ int i, j;
+ QRcode *qrcode = NULL;
+
+ if(!input->mqr) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(input->level > QR_ECLEVEL_Q) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ raw = MQRraw_new(input);
+ if(raw == NULL) return NULL;
+
+ version = raw->version;
+ width = MQRspec_getWidth(version);
+ frame = MQRspec_newFrame(version);
+ if(frame == NULL) {
+ MQRraw_free(raw);
+ return NULL;
+ }
+ filler = FrameFiller_new(width, frame, 1);
+ if(filler == NULL) {
+ MQRraw_free(raw);
+ free(frame);
+ return NULL;
+ }
+
+ /* inteleaved data and ecc codes */
+ for(i=0; i<raw->dataLength + raw->eccLength; i++) {
+ code = MQRraw_getCode(raw);
+ if(raw->oddbits && i == raw->dataLength - 1) {
+ bit = 1 << (raw->oddbits - 1);
+ for(j=0; j<raw->oddbits; j++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) goto EXIT;
+ *p = 0x02 | ((bit & code) != 0);
+ bit = bit >> 1;
+ }
+ } else {
+ bit = 0x80;
+ for(j=0; j<8; j++) {
+ p = FrameFiller_next(filler);
+ if(p == NULL) goto EXIT;
+ *p = 0x02 | ((bit & code) != 0);
+ bit = bit >> 1;
+ }
+ }
+ }
+ MQRraw_free(raw);
+ raw = NULL;
+
+ /* masking */
+ if(mask < 0) {
+ masked = MMask_mask(version, frame, input->level);
+ } else {
+ masked = MMask_makeMask(version, frame, mask, input->level);
+ }
+ if(masked == NULL) {
+ goto EXIT;
+ }
+
+ qrcode = QRcode_new(version, width, masked);
+
+EXIT:
+ MQRraw_free(raw);
+ free(filler);
+ free(frame);
+ return qrcode;
+}
+
+QRcode *QRcode_encodeInput(QRinput *input)
+{
+ if(input->mqr) {
+ return QRcode_encodeMaskMQR(input, -1);
+ } else {
+ return QRcode_encodeMask(input, -1);
+ }
+}
+
+static QRcode *QRcode_encodeStringReal(const char *string, int version, QRecLevel level, int mqr, QRencodeMode hint, int casesensitive)
+{
+ QRinput *input;
+ QRcode *code;
+ int ret;
+
+ if(string == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(hint != QR_MODE_8 && hint != QR_MODE_KANJI) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if(mqr) {
+ input = QRinput_newMQR(version, level);
+ } else {
+ input = QRinput_new2(version, level);
+ }
+ if(input == NULL) return NULL;
+
+ ret = Split_splitStringToQRinput(string, input, hint, casesensitive);
+ if(ret < 0) {
+ QRinput_free(input);
+ return NULL;
+ }
+ code = QRcode_encodeInput(input);
+ QRinput_free(input);
+
+ return code;
+}
+
+QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
+{
+ return QRcode_encodeStringReal(string, version, level, 0, hint, casesensitive);
+}
+
+QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
+{
+ return QRcode_encodeStringReal(string, version, level, 1, hint, casesensitive);
+}
+
+static QRcode *QRcode_encodeDataReal(const unsigned char *data, int length, int version, QRecLevel level, int mqr)
+{
+ QRinput *input;
+ QRcode *code;
+ int ret;
+
+ if(data == NULL || length == 0) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if(mqr) {
+ input = QRinput_newMQR(version, level);
+ } else {
+ input = QRinput_new2(version, level);
+ }
+ if(input == NULL) return NULL;
+
+ ret = QRinput_append(input, QR_MODE_8, length, data);
+ if(ret < 0) {
+ QRinput_free(input);
+ return NULL;
+ }
+ code = QRcode_encodeInput(input);
+ QRinput_free(input);
+
+ return code;
+}
+
+QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level)
+{
+ return QRcode_encodeDataReal(data, size, version, level, 0);
+}
+
+QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level)
+{
+ if(string == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 0);
+}
+
+QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level)
+{
+ return QRcode_encodeDataReal(data, size, version, level, 1);
+}
+
+QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level)
+{
+ if(string == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return QRcode_encodeDataReal((unsigned char *)string, strlen(string), version, level, 1);
+}
+
+
+/******************************************************************************
+ * Structured QR-code encoding
+ *****************************************************************************/
+
+static QRcode_List *QRcode_List_newEntry(void)
+{
+ QRcode_List *entry;
+
+ entry = (QRcode_List *)malloc(sizeof(QRcode_List));
+ if(entry == NULL) return NULL;
+
+ entry->next = NULL;
+ entry->code = NULL;
+
+ return entry;
+}
+
+static void QRcode_List_freeEntry(QRcode_List *entry)
+{
+ if(entry != NULL) {
+ QRcode_free(entry->code);
+ free(entry);
+ }
+}
+
+void QRcode_List_free(QRcode_List *qrlist)
+{
+ QRcode_List *list = qrlist, *next;
+
+ while(list != NULL) {
+ next = list->next;
+ QRcode_List_freeEntry(list);
+ list = next;
+ }
+}
+
+int QRcode_List_size(QRcode_List *qrlist)
+{
+ QRcode_List *list = qrlist;
+ int size = 0;
+
+ while(list != NULL) {
+ size++;
+ list = list->next;
+ }
+
+ return size;
+}
+
+#if 0
+static unsigned char QRcode_parity(const char *str, int size)
+{
+ unsigned char parity = 0;
+ int i;
+
+ for(i=0; i<size; i++) {
+ parity ^= str[i];
+ }
+
+ return parity;
+}
+#endif
+
+QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s)
+{
+ QRcode_List *head = NULL;
+ QRcode_List *tail = NULL;
+ QRcode_List *entry;
+ QRinput_InputList *list = s->head;
+
+ while(list != NULL) {
+ if(head == NULL) {
+ entry = QRcode_List_newEntry();
+ if(entry == NULL) goto ABORT;
+ head = entry;
+ tail = head;
+ } else {
+ entry = QRcode_List_newEntry();
+ if(entry == NULL) goto ABORT;
+ tail->next = entry;
+ tail = tail->next;
+ }
+ tail->code = QRcode_encodeInput(list->input);
+ if(tail->code == NULL) {
+ goto ABORT;
+ }
+ list = list->next;
+ }
+
+ return head;
+ABORT:
+ QRcode_List_free(head);
+ return NULL;
+}
+
+static QRcode_List *QRcode_encodeInputToStructured(QRinput *input)
+{
+ QRinput_Struct *s;
+ QRcode_List *codes;
+
+ s = QRinput_splitQRinputToStruct(input);
+ if(s == NULL) return NULL;
+
+ codes = QRcode_encodeInputStructured(s);
+ QRinput_Struct_free(s);
+
+ return codes;
+}
+
+static QRcode_List *QRcode_encodeDataStructuredReal(
+ int size, const unsigned char *data,
+ int version, QRecLevel level,
+ int eightbit, QRencodeMode hint, int casesensitive)
+{
+ QRinput *input;
+ QRcode_List *codes;
+ int ret;
+
+ if(version <= 0) {
+ errno = EINVAL;
+ return NULL;
+ }
+ if(!eightbit && (hint != QR_MODE_8 && hint != QR_MODE_KANJI)) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ input = QRinput_new2(version, level);
+ if(input == NULL) return NULL;
+
+ if(eightbit) {
+ ret = QRinput_append(input, QR_MODE_8, size, data);
+ } else {
+ ret = Split_splitStringToQRinput((char *)data, input, hint, casesensitive);
+ }
+ if(ret < 0) {
+ QRinput_free(input);
+ return NULL;
+ }
+ codes = QRcode_encodeInputToStructured(input);
+ QRinput_free(input);
+
+ return codes;
+}
+
+QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level) {
+ return QRcode_encodeDataStructuredReal(size, data, version, level, 1, QR_MODE_NUL, 0);
+}
+
+QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level) {
+ if(string == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return QRcode_encodeDataStructured(strlen(string), (unsigned char *)string, version, level);
+}
+
+QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive)
+{
+ if(string == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+ return QRcode_encodeDataStructuredReal(strlen(string), (unsigned char *)string, version, level, 0, hint, casesensitive);
+}
+
+/******************************************************************************
+ * System utilities
+ *****************************************************************************/
+
+void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version)
+{
+ if(major_version != NULL) {
+ *major_version = MAJOR_VERSION;
+ }
+ if(minor_version != NULL) {
+ *minor_version = MINOR_VERSION;
+ }
+ if(micro_version != NULL) {
+ *micro_version = MICRO_VERSION;
+ }
+}
+
+char *QRcode_APIVersionString(void)
+{
+ return VERSION;
+}
+
+void QRcode_clearCache(void)
+{
+ QRspec_clearCache();
+ MQRspec_clearCache();
+ free_rs_cache();
+}
diff --git a/ap/app/qrencode/qrencode.h b/ap/app/qrencode/qrencode.h
new file mode 100644
index 0000000..e554bb9
--- /dev/null
+++ b/ap/app/qrencode/qrencode.h
@@ -0,0 +1,566 @@
+/**
+ * qrencode - QR Code encoder
+ *
+ * Copyright (C) 2006-2012 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/** \mainpage
+ * Libqrencode is a library for encoding data in a QR Code symbol, a kind of 2D
+ * symbology.
+ *
+ * \section encoding Encoding
+ *
+ * There are two methods to encode data: <b>encoding a string/data</b> or
+ * <b>encoding a structured data</b>.
+ *
+ * \subsection encoding-string Encoding a string/data
+ * You can encode a string by calling QRcode_encodeString().
+ * The given string is parsed automatically and encoded. If you want to encode
+ * data that can be represented as a C string style (NUL terminated), you can
+ * simply use this way.
+ *
+ * If the input data contains Kanji (Shift-JIS) characters and you want to
+ * encode them as Kanji in QR Code, you should give QR_MODE_KANJI as a hint.
+ * Otherwise, all of non-alphanumeric characters are encoded as 8 bit data.
+ * If you want to encode a whole string in 8 bit mode, you can use
+ * QRcode_encodeString8bit() instead.
+ *
+ * Please note that a C string can not contain NUL characters. If your data
+ * contains NUL, you must use QRcode_encodeData().
+ *
+ * \subsection encoding-input Encoding a structured data
+ * You can construct a structured input data manually. If the structure of the
+ * input data is known, you can use this way.
+ * At first, create a ::QRinput object by QRinput_new(). Then add input data
+ * to the QRinput object by QRinput_append(). Finally call QRcode_encodeInput()
+ * to encode the QRinput data.
+ * You can reuse the QRinput data again to encode it in other symbols with
+ * different parameters.
+ *
+ * \section result Result
+ * The encoded symbol is resulted as a ::QRcode object. It will contain
+ * its version number, width of the symbol and an array represents the symbol.
+ * See ::QRcode for the details. You can free the object by QRcode_free().
+ *
+ * Please note that the version of the result may be larger than specified.
+ * In such cases, the input data would be too large to be encoded in a
+ * symbol of the specified version.
+ *
+ * \section structured Structured append
+ * Libqrencode can generate "Structured-appended" symbols that enables to split
+ * a large data set into mulitple QR codes. A QR code reader concatenates
+ * multiple QR code symbols into a string.
+ * Just like QRcode_encodeString(), you can use QRcode_encodeStringStructured()
+ * to generate structured-appended symbols. This functions returns an instance
+ * of ::QRcode_List. The returned list is a singly-linked list of QRcode: you
+ * can retrieve each QR code in this way:
+ *
+ * \code
+ * QRcode_List *qrcodes;
+ * QRcode_List *entry;
+ * QRcode *qrcode;
+ *
+ * qrcodes = QRcode_encodeStringStructured(...);
+ * entry = qrcodes;
+ * while(entry != NULL) {
+ * qrcode = entry->code;
+ * // do something
+ * entry = entry->next;
+ * }
+ * QRcode_List_free(entry);
+ * \endcode
+ *
+ * Instead of using auto-parsing functions, you can construct your own
+ * structured input. At first, instantiate an object of ::QRinput_Struct
+ * by calling QRinput_Struct_new(). This object can hold multiple ::QRinput,
+ * and one QR code is generated for a ::QRinput.
+ * QRinput_Struct_appendInput() appends a ::QRinput to a ::QRinput_Struct
+ * object. In order to generate structured-appended symbols, it is required to
+ * embed headers to each symbol. You can use
+ * QRinput_Struct_insertStructuredAppendHeaders() to insert appropriate
+ * headers to each symbol. You should call this function just once before
+ * encoding symbols.
+ */
+
+#ifndef __QRENCODE_H__
+#define __QRENCODE_H__
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/**
+ * Encoding mode.
+ */
+typedef enum {
+ QR_MODE_NUL = -1, ///< Terminator (NUL character). Internal use only
+ QR_MODE_NUM = 0, ///< Numeric mode
+ QR_MODE_AN, ///< Alphabet-numeric mode
+ QR_MODE_8, ///< 8-bit data mode
+ QR_MODE_KANJI, ///< Kanji (shift-jis) mode
+ QR_MODE_STRUCTURE, ///< Internal use only
+ QR_MODE_ECI, ///< ECI mode
+ QR_MODE_FNC1FIRST, ///< FNC1, first position
+ QR_MODE_FNC1SECOND, ///< FNC1, second position
+} QRencodeMode;
+
+/**
+ * Level of error correction.
+ */
+typedef enum {
+ QR_ECLEVEL_L = 0, ///< lowest
+ QR_ECLEVEL_M,
+ QR_ECLEVEL_Q,
+ QR_ECLEVEL_H ///< highest
+} QRecLevel;
+
+/**
+ * Maximum version (size) of QR-code symbol.
+ */
+#define QRSPEC_VERSION_MAX 40
+
+/**
+ * Maximum version (size) of QR-code symbol.
+ */
+#define MQRSPEC_VERSION_MAX 4
+
+
+/******************************************************************************
+ * Input data (qrinput.c)
+ *****************************************************************************/
+
+/**
+ * Singly linked list to contain input strings. An instance of this class
+ * contains its version and error correction level too. It is required to
+ * set them by QRinput_setVersion() and QRinput_setErrorCorrectionLevel(),
+ * or use QRinput_new2() to instantiate an object.
+ */
+typedef struct _QRinput QRinput;
+
+/**
+ * Instantiate an input data object. The version is set to 0 (auto-select)
+ * and the error correction level is set to QR_ECLEVEL_L.
+ * @return an input object (initialized). On error, NULL is returned and errno
+ * is set to indicate the error.
+ * @throw ENOMEM unable to allocate memory.
+ */
+extern QRinput *QRinput_new(void);
+
+/**
+ * Instantiate an input data object.
+ * @param version version number.
+ * @param level Error correction level.
+ * @return an input object (initialized). On error, NULL is returned and errno
+ * is set to indicate the error.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ * @throw EINVAL invalid arguments.
+ */
+extern QRinput *QRinput_new2(int version, QRecLevel level);
+
+/**
+ * Instantiate an input data object. Object's Micro QR Code flag is set.
+ * Unlike with full-sized QR Code, version number must be specified (>0).
+ * @param version version number (1--4).
+ * @param level Error correction level.
+ * @return an input object (initialized). On error, NULL is returned and errno
+ * is set to indicate the error.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ * @throw EINVAL invalid arguments.
+ */
+extern QRinput *QRinput_newMQR(int version, QRecLevel level);
+
+/**
+ * Append data to an input object.
+ * The data is copied and appended to the input object.
+ * @param input input object.
+ * @param mode encoding mode.
+ * @param size size of data (byte).
+ * @param data a pointer to the memory area of the input data.
+ * @retval 0 success.
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL input data is invalid.
+ *
+ */
+extern int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data);
+
+/**
+ * Append ECI header.
+ * @param input input object.
+ * @param ecinum ECI indicator number (0 - 999999)
+ * @retval 0 success.
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL input data is invalid.
+ *
+ */
+extern int QRinput_appendECIheader(QRinput *input, unsigned int ecinum);
+
+/**
+ * Get current version.
+ * @param input input object.
+ * @return current version.
+ */
+extern int QRinput_getVersion(QRinput *input);
+
+/**
+ * Set version of the QR code that is to be encoded.
+ * This function cannot be applied to Micro QR Code.
+ * @param input input object.
+ * @param version version number (0 = auto)
+ * @retval 0 success.
+ * @retval -1 invalid argument.
+ */
+extern int QRinput_setVersion(QRinput *input, int version);
+
+/**
+ * Get current error correction level.
+ * @param input input object.
+ * @return Current error correcntion level.
+ */
+extern QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input);
+
+/**
+ * Set error correction level of the QR code that is to be encoded.
+ * This function cannot be applied to Micro QR Code.
+ * @param input input object.
+ * @param level Error correction level.
+ * @retval 0 success.
+ * @retval -1 invalid argument.
+ */
+extern int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level);
+
+/**
+ * Set version and error correction level of the QR code at once.
+ * This function is recommened for Micro QR Code.
+ * @param input input object.
+ * @param version version number (0 = auto)
+ * @param level Error correction level.
+ * @retval 0 success.
+ * @retval -1 invalid argument.
+ */
+extern int QRinput_setVersionAndErrorCorrectionLevel(QRinput *input, int version, QRecLevel level);
+
+/**
+ * Free the input object.
+ * All of data chunks in the input object are freed too.
+ * @param input input object.
+ */
+extern void QRinput_free(QRinput *input);
+
+/**
+ * Validate the input data.
+ * @param mode encoding mode.
+ * @param size size of data (byte).
+ * @param data a pointer to the memory area of the input data.
+ * @retval 0 success.
+ * @retval -1 invalid arguments.
+ */
+extern int QRinput_check(QRencodeMode mode, int size, const unsigned char *data);
+
+/**
+ * Set of QRinput for structured symbols.
+ */
+typedef struct _QRinput_Struct QRinput_Struct;
+
+/**
+ * Instantiate a set of input data object.
+ * @return an instance of QRinput_Struct. On error, NULL is returned and errno
+ * is set to indicate the error.
+ * @throw ENOMEM unable to allocate memory.
+ */
+extern QRinput_Struct *QRinput_Struct_new(void);
+
+/**
+ * Set parity of structured symbols.
+ * @param s structured input object.
+ * @param parity parity of s.
+ */
+extern void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity);
+
+/**
+ * Append a QRinput object to the set. QRinput created by QRinput_newMQR()
+ * will be rejected.
+ * @warning never append the same QRinput object twice or more.
+ * @param s structured input object.
+ * @param input an input object.
+ * @retval >0 number of input objects in the structure.
+ * @retval -1 an error occurred. See Exceptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL invalid arguments.
+ */
+extern int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input);
+
+/**
+ * Free all of QRinput in the set.
+ * @param s a structured input object.
+ */
+extern void QRinput_Struct_free(QRinput_Struct *s);
+
+/**
+ * Split a QRinput to QRinput_Struct. It calculates a parity, set it, then
+ * insert structured-append headers. QRinput created by QRinput_newMQR() will
+ * be rejected.
+ * @param input input object. Version number and error correction level must be
+ * set.
+ * @return a set of input data. On error, NULL is returned, and errno is set
+ * to indicate the error. See Exceptions for the details.
+ * @throw ERANGE input data is too large.
+ * @throw EINVAL invalid input data.
+ * @throw ENOMEM unable to allocate memory.
+ */
+extern QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input);
+
+/**
+ * Insert structured-append headers to the input structure. It calculates
+ * a parity and set it if the parity is not set yet.
+ * @param s input structure
+ * @retval 0 success.
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory.
+ */
+extern int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s);
+
+/**
+ * Set FNC1-1st position flag.
+ */
+extern int QRinput_setFNC1First(QRinput *input);
+
+/**
+ * Set FNC1-2nd position flag and application identifier.
+ */
+extern int QRinput_setFNC1Second(QRinput *input, unsigned char appid);
+
+/******************************************************************************
+ * QRcode output (qrencode.c)
+ *****************************************************************************/
+
+/**
+ * QRcode class.
+ * Symbol data is represented as an array contains width*width uchars.
+ * Each uchar represents a module (dot). If the less significant bit of
+ * the uchar is 1, the corresponding module is black. The other bits are
+ * meaningless for usual applications, but here its specification is described.
+ *
+ * <pre>
+ * MSB 76543210 LSB
+ * |||||||`- 1=black/0=white
+ * ||||||`-- data and ecc code area
+ * |||||`--- format information
+ * ||||`---- version information
+ * |||`----- timing pattern
+ * ||`------ alignment pattern
+ * |`------- finder pattern and separator
+ * `-------- non-data modules (format, timing, etc.)
+ * </pre>
+ */
+typedef struct {
+ int version; ///< version of the symbol
+ int width; ///< width of the symbol
+ unsigned char *data; ///< symbol data
+} QRcode;
+
+/**
+ * Singly-linked list of QRcode. Used to represent a structured symbols.
+ * A list is terminated with NULL.
+ */
+typedef struct _QRcode_List {
+ QRcode *code;
+ struct _QRcode_List *next;
+} QRcode_List;
+
+/**
+ * Create a symbol from the input data.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param input input data.
+ * @return an instance of QRcode class. The version of the result QRcode may
+ * be larger than the designated version. On error, NULL is returned,
+ * and errno is set to indicate the error. See Exceptions for the
+ * details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ */
+extern QRcode *QRcode_encodeInput(QRinput *input);
+
+/**
+ * Create a symbol from the string. The library automatically parses the input
+ * string and encodes in a QR Code symbol.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param string input string. It must be NUL terminated.
+ * @param version version of the symbol. If 0, the library chooses the minimum
+ * version for the given input data.
+ * @param level error correction level.
+ * @param hint tell the library how Japanese Kanji characters should be
+ * encoded. If QR_MODE_KANJI is given, the library assumes that the
+ * given string contains Shift-JIS characters and encodes them in
+ * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
+ * characters will be encoded as is. If you want to embed UTF-8
+ * string, choose this. Other mode will cause EINVAL error.
+ * @param casesensitive case-sensitive(1) or not(0).
+ * @return an instance of QRcode class. The version of the result QRcode may
+ * be larger than the designated version. On error, NULL is returned,
+ * and errno is set to indicate the error. See Exceptions for the
+ * details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ * @throw ERANGE input data is too large.
+ */
+extern QRcode *QRcode_encodeString(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
+
+/**
+ * Same to QRcode_encodeString(), but encode whole data in 8-bit mode.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern QRcode *QRcode_encodeString8bit(const char *string, int version, QRecLevel level);
+
+/**
+ * Micro QR Code version of QRcode_encodeString().
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern QRcode *QRcode_encodeStringMQR(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
+
+/**
+ * Micro QR Code version of QRcode_encodeString8bit().
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern QRcode *QRcode_encodeString8bitMQR(const char *string, int version, QRecLevel level);
+
+/**
+ * Encode byte stream (may include '\0') in 8-bit mode.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param size size of the input data.
+ * @param data input data.
+ * @param version version of the symbol. If 0, the library chooses the minimum
+ * version for the given input data.
+ * @param level error correction level.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ * @throw ERANGE input data is too large.
+ */
+extern QRcode *QRcode_encodeData(int size, const unsigned char *data, int version, QRecLevel level);
+
+/**
+ * Micro QR Code version of QRcode_encodeData().
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern QRcode *QRcode_encodeDataMQR(int size, const unsigned char *data, int version, QRecLevel level);
+
+/**
+ * Free the instance of QRcode class.
+ * @param qrcode an instance of QRcode class.
+ */
+extern void QRcode_free(QRcode *qrcode);
+
+/**
+ * Create structured symbols from the input data.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param s
+ * @return a singly-linked list of QRcode.
+ */
+extern QRcode_List *QRcode_encodeInputStructured(QRinput_Struct *s);
+
+/**
+ * Create structured symbols from the string. The library automatically parses
+ * the input string and encodes in a QR Code symbol.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param string input string. It must be NUL terminated.
+ * @param version version of the symbol.
+ * @param level error correction level.
+ * @param hint tell the library how Japanese Kanji characters should be
+ * encoded. If QR_MODE_KANJI is given, the library assumes that the
+ * given string contains Shift-JIS characters and encodes them in
+ * Kanji-mode. If QR_MODE_8 is given, all of non-alphanumerical
+ * characters will be encoded as is. If you want to embed UTF-8
+ * string, choose this. Other mode will cause EINVAL error.
+ * @param casesensitive case-sensitive(1) or not(0).
+ * @return a singly-linked list of QRcode. On error, NULL is returned, and
+ * errno is set to indicate the error. See Exceptions for the details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ */
+extern QRcode_List *QRcode_encodeStringStructured(const char *string, int version, QRecLevel level, QRencodeMode hint, int casesensitive);
+
+/**
+ * Same to QRcode_encodeStringStructured(), but encode whole data in 8-bit mode.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern QRcode_List *QRcode_encodeString8bitStructured(const char *string, int version, QRecLevel level);
+
+/**
+ * Create structured symbols from byte stream (may include '\0'). Wholde data
+ * are encoded in 8-bit mode.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ * @param size size of the input data.
+ * @param data input dat.
+ * @param version version of the symbol.
+ * @param level error correction level.
+ * @return a singly-linked list of QRcode. On error, NULL is returned, and
+ * errno is set to indicate the error. See Exceptions for the details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ */
+extern QRcode_List *QRcode_encodeDataStructured(int size, const unsigned char *data, int version, QRecLevel level);
+
+/**
+ * Return the number of symbols included in a QRcode_List.
+ * @param qrlist a head entry of a QRcode_List.
+ * @return number of symbols in the list.
+ */
+extern int QRcode_List_size(QRcode_List *qrlist);
+
+/**
+ * Free the QRcode_List.
+ * @param qrlist a head entry of a QRcode_List.
+ */
+extern void QRcode_List_free(QRcode_List *qrlist);
+
+
+/******************************************************************************
+ * System utilities
+ *****************************************************************************/
+
+/**
+ * Return a string that identifies the library version.
+ * @param major_version
+ * @param minor_version
+ * @param micro_version
+ */
+extern void QRcode_APIVersion(int *major_version, int *minor_version, int *micro_version);
+
+/**
+ * Return a string that identifies the library version.
+ * @return a string identifies the library version. The string is held by the
+ * library. Do NOT free it.
+ */
+extern char *QRcode_APIVersionString(void);
+
+/**
+ * Clear all caches. This is only for debug purpose. If you are attacking a
+ * complicated memory leak bug, try this to reduce the reachable blocks record.
+ * @warning This function is THREAD UNSAFE when pthread is disabled.
+ */
+extern void QRcode_clearCache(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif /* __QRENCODE_H__ */
diff --git a/ap/app/qrencode/qrencode_inner.h b/ap/app/qrencode/qrencode_inner.h
new file mode 100644
index 0000000..3c40d06
--- /dev/null
+++ b/ap/app/qrencode/qrencode_inner.h
@@ -0,0 +1,88 @@
+/**
+ * qrencode - QR Code encoder
+ *
+ * Header for test use
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __QRENCODE_INNER_H__
+#define __QRENCODE_INNER_H__
+
+/**
+ * This header file includes definitions for test use.
+ */
+
+/******************************************************************************
+ * Raw code
+ *****************************************************************************/
+
+typedef struct {
+ int dataLength;
+ unsigned char *data;
+ int eccLength;
+ unsigned char *ecc;
+} RSblock;
+
+typedef struct {
+ int version;
+ int dataLength;
+ int eccLength;
+ unsigned char *datacode;
+ unsigned char *ecccode;
+ int b1;
+ int blocks;
+ RSblock *rsblock;
+ int count;
+} QRRawCode;
+
+extern QRRawCode *QRraw_new(QRinput *input);
+extern unsigned char QRraw_getCode(QRRawCode *raw);
+extern void QRraw_free(QRRawCode *raw);
+
+/******************************************************************************
+ * Raw code for Micro QR Code
+ *****************************************************************************/
+
+typedef struct {
+ int version;
+ int dataLength;
+ int eccLength;
+ unsigned char *datacode;
+ unsigned char *ecccode;
+ RSblock *rsblock;
+ int oddbits;
+ int count;
+} MQRRawCode;
+
+extern MQRRawCode *MQRraw_new(QRinput *input);
+extern unsigned char MQRraw_getCode(MQRRawCode *raw);
+extern void MQRraw_free(MQRRawCode *raw);
+
+/******************************************************************************
+ * Frame filling
+ *****************************************************************************/
+extern unsigned char *FrameFiller_test(int version);
+extern unsigned char *FrameFiller_testMQR(int version);
+
+/******************************************************************************
+ * QR-code encoding
+ *****************************************************************************/
+extern QRcode *QRcode_encodeMask(QRinput *input, int mask);
+extern QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask);
+extern QRcode *QRcode_new(int version, int width, unsigned char *data);
+
+#endif /* __QRENCODE_INNER_H__ */
diff --git a/ap/app/qrencode/qrinput.c b/ap/app/qrencode/qrinput.c
new file mode 100644
index 0000000..f5e9e8e
--- /dev/null
+++ b/ap/app/qrencode/qrinput.c
@@ -0,0 +1,1729 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Input data chunk class
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "qrencode.h"
+#include "qrspec.h"
+#include "mqrspec.h"
+#include "bitstream.h"
+#include "qrinput.h"
+
+/******************************************************************************
+ * Utilities
+ *****************************************************************************/
+int QRinput_isSplittableMode(QRencodeMode mode)
+{
+ return (mode >= QR_MODE_NUM && mode <= QR_MODE_KANJI);
+}
+
+/******************************************************************************
+ * Entry of input data
+ *****************************************************************************/
+
+static QRinput_List *QRinput_List_newEntry(QRencodeMode mode, int size, const unsigned char *data)
+{
+ QRinput_List *entry;
+
+ if(QRinput_check(mode, size, data)) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ entry = (QRinput_List *)malloc(sizeof(QRinput_List));
+ if(entry == NULL) return NULL;
+
+ entry->mode = mode;
+ entry->size = size;
+ if(size > 0) {
+ entry->data = (unsigned char *)malloc(size);
+ if(entry->data == NULL) {
+ free(entry);
+ return NULL;
+ }
+ memcpy(entry->data, data, size);
+ }
+ entry->bstream = NULL;
+ entry->next = NULL;
+
+ return entry;
+}
+
+static void QRinput_List_freeEntry(QRinput_List *entry)
+{
+ if(entry != NULL) {
+ free(entry->data);
+ BitStream_free(entry->bstream);
+ free(entry);
+ }
+}
+
+static QRinput_List *QRinput_List_dup(QRinput_List *entry)
+{
+ QRinput_List *n;
+
+ n = (QRinput_List *)malloc(sizeof(QRinput_List));
+ if(n == NULL) return NULL;
+
+ n->mode = entry->mode;
+ n->size = entry->size;
+ n->data = (unsigned char *)malloc(n->size);
+ if(n->data == NULL) {
+ free(n);
+ return NULL;
+ }
+ memcpy(n->data, entry->data, entry->size);
+ n->bstream = NULL;
+ n->next = NULL;
+
+ return n;
+}
+
+/******************************************************************************
+ * Input Data
+ *****************************************************************************/
+
+QRinput *QRinput_new(void)
+{
+ return QRinput_new2(0, QR_ECLEVEL_L);
+}
+
+QRinput *QRinput_new2(int version, QRecLevel level)
+{
+ QRinput *input;
+
+ if(version < 0 || version > QRSPEC_VERSION_MAX || level > QR_ECLEVEL_H) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ input = (QRinput *)malloc(sizeof(QRinput));
+ if(input == NULL) return NULL;
+
+ input->head = NULL;
+ input->tail = NULL;
+ input->version = version;
+ input->level = level;
+ input->mqr = 0;
+ input->fnc1 = 0;
+
+ return input;
+}
+
+QRinput *QRinput_newMQR(int version, QRecLevel level)
+{
+ QRinput *input;
+
+ if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
+ if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
+
+ input = QRinput_new2(version, level);
+ if(input == NULL) return NULL;
+
+ input->mqr = 1;
+
+ return input;
+
+INVALID:
+ errno = EINVAL;
+ return NULL;
+}
+
+int QRinput_getVersion(QRinput *input)
+{
+ return input->version;
+}
+
+int QRinput_setVersion(QRinput *input, int version)
+{
+ if(input->mqr || version < 0 || version > QRSPEC_VERSION_MAX) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ input->version = version;
+
+ return 0;
+}
+
+QRecLevel QRinput_getErrorCorrectionLevel(QRinput *input)
+{
+ return input->level;
+}
+
+int QRinput_setErrorCorrectionLevel(QRinput *input, QRecLevel level)
+{
+ if(input->mqr || level > QR_ECLEVEL_H) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ input->level = level;
+
+ return 0;
+}
+
+int QRinput_setVersionAndErrorCorrectionLevel(QRinput *input, int version, QRecLevel level)
+{
+ if(input->mqr) {
+ if(version <= 0 || version > MQRSPEC_VERSION_MAX) goto INVALID;
+ if((MQRspec_getECCLength(version, level) == 0)) goto INVALID;
+ } else {
+ if(version < 0 || version > QRSPEC_VERSION_MAX) goto INVALID;
+ if(level > QR_ECLEVEL_H) goto INVALID;
+ }
+
+ input->version = version;
+ input->level = level;
+
+ return 0;
+
+INVALID:
+ errno = EINVAL;
+ return -1;
+}
+
+static void QRinput_appendEntry(QRinput *input, QRinput_List *entry)
+{
+ if(input->tail == NULL) {
+ input->head = entry;
+ input->tail = entry;
+ } else {
+ input->tail->next = entry;
+ input->tail = entry;
+ }
+ entry->next = NULL;
+}
+
+int QRinput_append(QRinput *input, QRencodeMode mode, int size, const unsigned char *data)
+{
+ QRinput_List *entry;
+
+ entry = QRinput_List_newEntry(mode, size, data);
+ if(entry == NULL) {
+ return -1;
+ }
+
+ QRinput_appendEntry(input, entry);
+
+ return 0;
+}
+
+/**
+ * Insert a structured-append header to the head of the input data.
+ * @param input input data.
+ * @param size number of structured symbols.
+ * @param index index number of the symbol. (1 <= index <= size)
+ * @param parity parity among input data. (NOTE: each symbol of a set of structured symbols has the same parity data)
+ * @retval 0 success.
+ * @retval -1 error occurred and errno is set to indeicate the error. See Execptions for the details.
+ * @throw EINVAL invalid parameter.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity)
+{
+ QRinput_List *entry;
+ unsigned char buf[3];
+
+ if(size > MAX_STRUCTURED_SYMBOLS) {
+ errno = EINVAL;
+ return -1;
+ }
+ if(index <= 0 || index > MAX_STRUCTURED_SYMBOLS) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ buf[0] = (unsigned char)size;
+ buf[1] = (unsigned char)index;
+ buf[2] = parity;
+ entry = QRinput_List_newEntry(QR_MODE_STRUCTURE, 3, buf);
+ if(entry == NULL) {
+ return -1;
+ }
+
+ entry->next = input->head;
+ input->head = entry;
+
+ return 0;
+}
+
+int QRinput_appendECIheader(QRinput *input, unsigned int ecinum)
+{
+ unsigned char data[4];
+
+ if(ecinum > 999999) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* We manually create byte array of ecinum because
+ (unsigned char *)&ecinum may cause bus error on some architectures, */
+ data[0] = ecinum & 0xff;
+ data[1] = (ecinum >> 8) & 0xff;
+ data[2] = (ecinum >> 16) & 0xff;
+ data[3] = (ecinum >> 24) & 0xff;
+ return QRinput_append(input, QR_MODE_ECI, 4, data);
+}
+
+void QRinput_free(QRinput *input)
+{
+ QRinput_List *list, *next;
+
+ if(input != NULL) {
+ list = input->head;
+ while(list != NULL) {
+ next = list->next;
+ QRinput_List_freeEntry(list);
+ list = next;
+ }
+ free(input);
+ }
+}
+
+static unsigned char QRinput_calcParity(QRinput *input)
+{
+ unsigned char parity = 0;
+ QRinput_List *list;
+ int i;
+
+ list = input->head;
+ while(list != NULL) {
+ if(list->mode != QR_MODE_STRUCTURE) {
+ for(i=list->size-1; i>=0; i--) {
+ parity ^= list->data[i];
+ }
+ }
+ list = list->next;
+ }
+
+ return parity;
+}
+
+QRinput *QRinput_dup(QRinput *input)
+{
+ QRinput *n;
+ QRinput_List *list, *e;
+
+ if(input->mqr) {
+ n = QRinput_newMQR(input->version, input->level);
+ } else {
+ n = QRinput_new2(input->version, input->level);
+ }
+ if(n == NULL) return NULL;
+
+ list = input->head;
+ while(list != NULL) {
+ e = QRinput_List_dup(list);
+ if(e == NULL) {
+ QRinput_free(n);
+ return NULL;
+ }
+ QRinput_appendEntry(n, e);
+ list = list->next;
+ }
+
+ return n;
+}
+
+/******************************************************************************
+ * Numeric data
+ *****************************************************************************/
+
+/**
+ * Check the input data.
+ * @param size
+ * @param data
+ * @return result
+ */
+static int QRinput_checkModeNum(int size, const char *data)
+{
+ int i;
+
+ for(i=0; i<size; i++) {
+ if(data[i] < '0' || data[i] > '9')
+ return -1;
+ }
+
+ return 0;
+}
+
+/**
+ * Estimates the length of the encoded bit stream of numeric data.
+ * @param size
+ * @return number of bits
+ */
+int QRinput_estimateBitsModeNum(int size)
+{
+ int w;
+ int bits;
+
+ w = size / 3;
+ bits = w * 10;
+ switch(size - w * 3) {
+ case 1:
+ bits += 4;
+ break;
+ case 2:
+ bits += 7;
+ break;
+ default:
+ break;
+ }
+
+ return bits;
+}
+
+/**
+ * Convert the number data to a bit stream.
+ * @param entry
+ * @param mqr
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_encodeModeNum(QRinput_List *entry, int version, int mqr)
+{
+ int words, i, ret;
+ unsigned int val;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ if(mqr) {
+ if(version > 1) {
+ ret = BitStream_appendNum(entry->bstream, version - 1, MQRSPEC_MODEID_NUM);
+ if(ret < 0) goto ABORT;
+ }
+ ret = BitStream_appendNum(entry->bstream, MQRspec_lengthIndicator(QR_MODE_NUM, version), entry->size);
+ if(ret < 0) goto ABORT;
+ } else {
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_NUM);
+ if(ret < 0) goto ABORT;
+
+ ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_NUM, version), entry->size);
+ if(ret < 0) goto ABORT;
+ }
+
+ words = entry->size / 3;
+ for(i=0; i<words; i++) {
+ val = (entry->data[i*3 ] - '0') * 100;
+ val += (entry->data[i*3+1] - '0') * 10;
+ val += (entry->data[i*3+2] - '0');
+
+ ret = BitStream_appendNum(entry->bstream, 10, val);
+ if(ret < 0) goto ABORT;
+ }
+
+ if(entry->size - words * 3 == 1) {
+ val = entry->data[words*3] - '0';
+ ret = BitStream_appendNum(entry->bstream, 4, val);
+ if(ret < 0) goto ABORT;
+ } else if(entry->size - words * 3 == 2) {
+ val = (entry->data[words*3 ] - '0') * 10;
+ val += (entry->data[words*3+1] - '0');
+ BitStream_appendNum(entry->bstream, 7, val);
+ if(ret < 0) goto ABORT;
+ }
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * Alphabet-numeric data
+ *****************************************************************************/
+
+const signed char QRinput_anTable[128] = {
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
+ -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+};
+
+/**
+ * Check the input data.
+ * @param size
+ * @param data
+ * @return result
+ */
+static int QRinput_checkModeAn(int size, const char *data)
+{
+ int i;
+
+ for(i=0; i<size; i++) {
+ if(QRinput_lookAnTable(data[i]) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+/**
+ * Estimates the length of the encoded bit stream of alphabet-numeric data.
+ * @param size
+ * @return number of bits
+ */
+int QRinput_estimateBitsModeAn(int size)
+{
+ int w;
+ int bits;
+
+ w = size / 2;
+ bits = w * 11;
+ if(size & 1) {
+ bits += 6;
+ }
+
+ return bits;
+}
+
+/**
+ * Convert the alphabet-numeric data to a bit stream.
+ * @param entry
+ * @param mqr
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL invalid version.
+ */
+static int QRinput_encodeModeAn(QRinput_List *entry, int version, int mqr)
+{
+ int words, i, ret;
+ unsigned int val;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ if(mqr) {
+ if(version < 2) {
+ errno = EINVAL;
+ goto ABORT;
+ }
+ ret = BitStream_appendNum(entry->bstream, version - 1, MQRSPEC_MODEID_AN);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, MQRspec_lengthIndicator(QR_MODE_AN, version), entry->size);
+ if(ret < 0) goto ABORT;
+ } else {
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_AN);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_AN, version), entry->size);
+ if(ret < 0) goto ABORT;
+ }
+
+ words = entry->size / 2;
+ for(i=0; i<words; i++) {
+ val = (unsigned int)QRinput_lookAnTable(entry->data[i*2 ]) * 45;
+ val += (unsigned int)QRinput_lookAnTable(entry->data[i*2+1]);
+
+ ret = BitStream_appendNum(entry->bstream, 11, val);
+ if(ret < 0) goto ABORT;
+ }
+
+ if(entry->size & 1) {
+ val = (unsigned int)QRinput_lookAnTable(entry->data[words * 2]);
+
+ ret = BitStream_appendNum(entry->bstream, 6, val);
+ if(ret < 0) goto ABORT;
+ }
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * 8 bit data
+ *****************************************************************************/
+
+/**
+ * Estimates the length of the encoded bit stream of 8 bit data.
+ * @param size
+ * @return number of bits
+ */
+int QRinput_estimateBitsMode8(int size)
+{
+ return size * 8;
+}
+
+/**
+ * Convert the 8bits data to a bit stream.
+ * @param entry
+ * @param mqr
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_encodeMode8(QRinput_List *entry, int version, int mqr)
+{
+ int ret;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ if(mqr) {
+ if(version < 3) {
+ errno = EINVAL;
+ goto ABORT;
+ }
+ ret = BitStream_appendNum(entry->bstream, version - 1, MQRSPEC_MODEID_8);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, MQRspec_lengthIndicator(QR_MODE_8, version), entry->size);
+ if(ret < 0) goto ABORT;
+ } else {
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_8);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_8, version), entry->size);
+ if(ret < 0) goto ABORT;
+ }
+
+ ret = BitStream_appendBytes(entry->bstream, entry->size, entry->data);
+ if(ret < 0) goto ABORT;
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+
+/******************************************************************************
+ * Kanji data
+ *****************************************************************************/
+
+/**
+ * Estimates the length of the encoded bit stream of kanji data.
+ * @param size
+ * @return number of bits
+ */
+int QRinput_estimateBitsModeKanji(int size)
+{
+ return (size / 2) * 13;
+}
+
+/**
+ * Check the input data.
+ * @param size
+ * @param data
+ * @return result
+ */
+static int QRinput_checkModeKanji(int size, const unsigned char *data)
+{
+ int i;
+ unsigned int val;
+
+ if(size & 1)
+ return -1;
+
+ for(i=0; i<size; i+=2) {
+ val = ((unsigned int)data[i] << 8) | data[i+1];
+ if(val < 0x8140 || (val > 0x9ffc && val < 0xe040) || val > 0xebbf) {
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * Convert the kanji data to a bit stream.
+ * @param entry
+ * @param mqr
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL invalid version.
+ */
+static int QRinput_encodeModeKanji(QRinput_List *entry, int version, int mqr)
+{
+ int ret, i;
+ unsigned int val, h;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ if(mqr) {
+ if(version < 2) {
+ errno = EINVAL;
+ goto ABORT;
+ }
+ ret = BitStream_appendNum(entry->bstream, version - 1, MQRSPEC_MODEID_KANJI);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, MQRspec_lengthIndicator(QR_MODE_KANJI, version), entry->size/2);
+ if(ret < 0) goto ABORT;
+ } else {
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_KANJI);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, QRspec_lengthIndicator(QR_MODE_KANJI, version), entry->size/2);
+ if(ret < 0) goto ABORT;
+ }
+
+ for(i=0; i<entry->size; i+=2) {
+ val = ((unsigned int)entry->data[i] << 8) | entry->data[i+1];
+ if(val <= 0x9ffc) {
+ val -= 0x8140;
+ } else {
+ val -= 0xc140;
+ }
+ h = (val >> 8) * 0xc0;
+ val = (val & 0xff) + h;
+
+ ret = BitStream_appendNum(entry->bstream, 13, val);
+ if(ret < 0) goto ABORT;
+ }
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * Structured Symbol
+ *****************************************************************************/
+
+/**
+ * Convert a structure symbol code to a bit stream.
+ * @param entry
+ * @param mqr
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw EINVAL invalid entry.
+ */
+static int QRinput_encodeModeStructure(QRinput_List *entry, int mqr)
+{
+ int ret;
+
+ if(mqr) {
+ errno = EINVAL;
+ return -1;
+ }
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_STRUCTURE);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, 4, entry->data[1] - 1);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, 4, entry->data[0] - 1);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_appendNum(entry->bstream, 8, entry->data[2]);
+ if(ret < 0) goto ABORT;
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * FNC1
+ *****************************************************************************/
+
+static int QRinput_checkModeFNC1Second(int size, const unsigned char *data)
+{
+ if(size != 1) return -1;
+
+ return 0;
+}
+
+static int QRinput_encodeModeFNC1Second(QRinput_List *entry, int version)
+{
+ int ret;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_FNC1SECOND);
+ if(ret < 0) goto ABORT;
+
+ ret = BitStream_appendBytes(entry->bstream, 1, entry->data);
+ if(ret < 0) goto ABORT;
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * ECI header
+ *****************************************************************************/
+static unsigned int QRinput_decodeECIfromByteArray(unsigned char *data)
+{
+ int i;
+ unsigned int ecinum;
+
+ ecinum = 0;
+ for(i=0; i<4; i++) {
+ ecinum = ecinum << 8;
+ ecinum |= data[3-i];
+ }
+
+ return ecinum;
+}
+
+int QRinput_estimateBitsModeECI(unsigned char *data)
+{
+ unsigned int ecinum;
+
+ ecinum = QRinput_decodeECIfromByteArray(data);;
+
+ /* See Table 4 of JISX 0510:2004 pp.17. */
+ if(ecinum < 128) {
+ return MODE_INDICATOR_SIZE + 8;
+ } else if(ecinum < 16384) {
+ return MODE_INDICATOR_SIZE + 16;
+ } else {
+ return MODE_INDICATOR_SIZE + 24;
+ }
+}
+
+static int QRinput_encodeModeECI(QRinput_List *entry, int version)
+{
+ int ret, words;
+ unsigned int ecinum, code;
+
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) return -1;
+
+ ecinum = QRinput_decodeECIfromByteArray(entry->data);;
+
+ /* See Table 4 of JISX 0510:2004 pp.17. */
+ if(ecinum < 128) {
+ words = 1;
+ code = ecinum;
+ } else if(ecinum < 16384) {
+ words = 2;
+ code = 0x8000 + ecinum;
+ } else {
+ words = 3;
+ code = 0xc0000 + ecinum;
+ }
+
+ ret = BitStream_appendNum(entry->bstream, 4, QRSPEC_MODEID_ECI);
+ if(ret < 0) goto ABORT;
+
+ ret = BitStream_appendNum(entry->bstream, words * 8, code);
+ if(ret < 0) goto ABORT;
+
+ return 0;
+ABORT:
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ return -1;
+}
+
+/******************************************************************************
+ * Validation
+ *****************************************************************************/
+
+int QRinput_check(QRencodeMode mode, int size, const unsigned char *data)
+{
+ if((mode == QR_MODE_FNC1FIRST && size < 0) || size <= 0) return -1;
+
+ switch(mode) {
+ case QR_MODE_NUM:
+ return QRinput_checkModeNum(size, (const char *)data);
+ case QR_MODE_AN:
+ return QRinput_checkModeAn(size, (const char *)data);
+ case QR_MODE_KANJI:
+ return QRinput_checkModeKanji(size, data);
+ case QR_MODE_8:
+ return 0;
+ case QR_MODE_STRUCTURE:
+ return 0;
+ case QR_MODE_ECI:
+ return 0;
+ case QR_MODE_FNC1FIRST:
+ return 0;
+ case QR_MODE_FNC1SECOND:
+ return QRinput_checkModeFNC1Second(size, data);
+ case QR_MODE_NUL:
+ break;
+ }
+
+ return -1;
+}
+
+/******************************************************************************
+ * Estimation of the bit length
+ *****************************************************************************/
+
+/**
+ * Estimates the length of the encoded bit stream on the current version.
+ * @param entry
+ * @param version version of the symbol
+ * @param mqr
+ * @return number of bits
+ */
+static int QRinput_estimateBitStreamSizeOfEntry(QRinput_List *entry, int version, int mqr)
+{
+ int bits = 0;
+ int l, m;
+ int num;
+
+ if(version == 0) version = 1;
+
+ switch(entry->mode) {
+ case QR_MODE_NUM:
+ bits = QRinput_estimateBitsModeNum(entry->size);
+ break;
+ case QR_MODE_AN:
+ bits = QRinput_estimateBitsModeAn(entry->size);
+ break;
+ case QR_MODE_8:
+ bits = QRinput_estimateBitsMode8(entry->size);
+ break;
+ case QR_MODE_KANJI:
+ bits = QRinput_estimateBitsModeKanji(entry->size);
+ break;
+ case QR_MODE_STRUCTURE:
+ return STRUCTURE_HEADER_SIZE;
+ case QR_MODE_ECI:
+ bits = QRinput_estimateBitsModeECI(entry->data);
+ break;
+ case QR_MODE_FNC1FIRST:
+ return MODE_INDICATOR_SIZE;
+ break;
+ case QR_MODE_FNC1SECOND:
+ return MODE_INDICATOR_SIZE + 8;
+ default:
+ return 0;
+ }
+
+ if(mqr) {
+ l = QRspec_lengthIndicator(entry->mode, version);
+ m = version - 1;
+ bits += l + m;
+ } else {
+ l = QRspec_lengthIndicator(entry->mode, version);
+ m = 1 << l;
+ num = (entry->size + m - 1) / m;
+
+ bits += num * (MODE_INDICATOR_SIZE + l);
+ }
+
+ return bits;
+}
+
+/**
+ * Estimates the length of the encoded bit stream of the data.
+ * @param input input data
+ * @param version version of the symbol
+ * @return number of bits
+ */
+static int QRinput_estimateBitStreamSize(QRinput *input, int version)
+{
+ QRinput_List *list;
+ int bits = 0;
+
+ list = input->head;
+ while(list != NULL) {
+ bits += QRinput_estimateBitStreamSizeOfEntry(list, version, input->mqr);
+ list = list->next;
+ }
+
+ return bits;
+}
+
+/**
+ * Estimates the required version number of the symbol.
+ * @param input input data
+ * @return required version number
+ */
+static int QRinput_estimateVersion(QRinput *input)
+{
+ int bits;
+ int version, prev;
+
+ version = 0;
+ do {
+ prev = version;
+ bits = QRinput_estimateBitStreamSize(input, prev);
+ version = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
+ if (version < 0) {
+ return -1;
+ }
+ } while (version > prev);
+
+ return version;
+}
+
+/**
+ * Returns required length in bytes for specified mode, version and bits.
+ * @param mode
+ * @param version
+ * @param bits
+ * @return required length of code words in bytes.
+ */
+static int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits)
+{
+ int payload, size, chunks, remain, maxsize;
+
+ payload = bits - 4 - QRspec_lengthIndicator(mode, version);
+ switch(mode) {
+ case QR_MODE_NUM:
+ chunks = payload / 10;
+ remain = payload - chunks * 10;
+ size = chunks * 3;
+ if(remain >= 7) {
+ size += 2;
+ } else if(remain >= 4) {
+ size += 1;
+ }
+ break;
+ case QR_MODE_AN:
+ chunks = payload / 11;
+ remain = payload - chunks * 11;
+ size = chunks * 2;
+ if(remain >= 6) size++;
+ break;
+ case QR_MODE_8:
+ size = payload / 8;
+ break;
+ case QR_MODE_KANJI:
+ size = (payload / 13) * 2;
+ break;
+ case QR_MODE_STRUCTURE:
+ size = payload / 8;
+ break;
+ default:
+ size = 0;
+ break;
+ }
+ maxsize = QRspec_maximumWords(mode, version);
+ if(size < 0) size = 0;
+ if(maxsize > 0 && size > maxsize) size = maxsize;
+
+ return size;
+}
+
+/******************************************************************************
+ * Data conversion
+ *****************************************************************************/
+
+/**
+ * Convert the input data in the data chunk to a bit stream.
+ * @param entry
+ * @return number of bits (>0) or -1 for failure.
+ */
+static int QRinput_encodeBitStream(QRinput_List *entry, int version, int mqr)
+{
+ int words, ret;
+ QRinput_List *st1 = NULL, *st2 = NULL;
+
+ if(entry->bstream != NULL) {
+ BitStream_free(entry->bstream);
+ entry->bstream = NULL;
+ }
+
+ words = QRspec_maximumWords(entry->mode, version);
+ if(words != 0 && entry->size > words) {
+ st1 = QRinput_List_newEntry(entry->mode, words, entry->data);
+ if(st1 == NULL) goto ABORT;
+ st2 = QRinput_List_newEntry(entry->mode, entry->size - words, &entry->data[words]);
+ if(st2 == NULL) goto ABORT;
+
+ ret = QRinput_encodeBitStream(st1, version, mqr);
+ if(ret < 0) goto ABORT;
+ ret = QRinput_encodeBitStream(st2, version, mqr);
+ if(ret < 0) goto ABORT;
+ entry->bstream = BitStream_new();
+ if(entry->bstream == NULL) goto ABORT;
+ ret = BitStream_append(entry->bstream, st1->bstream);
+ if(ret < 0) goto ABORT;
+ ret = BitStream_append(entry->bstream, st2->bstream);
+ if(ret < 0) goto ABORT;
+ QRinput_List_freeEntry(st1);
+ QRinput_List_freeEntry(st2);
+ } else {
+ ret = 0;
+ switch(entry->mode) {
+ case QR_MODE_NUM:
+ ret = QRinput_encodeModeNum(entry, version, mqr);
+ break;
+ case QR_MODE_AN:
+ ret = QRinput_encodeModeAn(entry, version, mqr);
+ break;
+ case QR_MODE_8:
+ ret = QRinput_encodeMode8(entry, version, mqr);
+ break;
+ case QR_MODE_KANJI:
+ ret = QRinput_encodeModeKanji(entry, version, mqr);
+ break;
+ case QR_MODE_STRUCTURE:
+ ret = QRinput_encodeModeStructure(entry, mqr);
+ break;
+ case QR_MODE_ECI:
+ ret = QRinput_encodeModeECI(entry, version);
+ break;
+ case QR_MODE_FNC1SECOND:
+ ret = QRinput_encodeModeFNC1Second(entry, version);
+ default:
+ break;
+ }
+ if(ret < 0) return -1;
+ }
+
+ return BitStream_size(entry->bstream);
+ABORT:
+ QRinput_List_freeEntry(st1);
+ QRinput_List_freeEntry(st2);
+ return -1;
+}
+
+/**
+ * Convert the input data to a bit stream.
+ * @param input input data.
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_createBitStream(QRinput *input)
+{
+ QRinput_List *list;
+ int bits, total = 0;
+
+ list = input->head;
+ while(list != NULL) {
+ bits = QRinput_encodeBitStream(list, input->version, input->mqr);
+ if(bits < 0) return -1;
+ total += bits;
+ list = list->next;
+ }
+
+ return total;
+}
+
+/**
+ * Convert the input data to a bit stream.
+ * When the version number is given and that is not sufficient, it is increased
+ * automatically.
+ * @param input input data.
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ENOMEM unable to allocate memory.
+ * @throw ERANGE input is too large.
+ */
+static int QRinput_convertData(QRinput *input)
+{
+ int bits;
+ int ver;
+
+ ver = QRinput_estimateVersion(input);
+ if(ver > QRinput_getVersion(input)) {
+ QRinput_setVersion(input, ver);
+ }
+
+ for(;;) {
+ bits = QRinput_createBitStream(input);
+ if(bits < 0) return -1;
+ ver = QRspec_getMinimumVersion((bits + 7) / 8, input->level);
+ if(ver < 0) {
+ errno = ERANGE;
+ return -1;
+ } else if(ver > QRinput_getVersion(input)) {
+ QRinput_setVersion(input, ver);
+ } else {
+ break;
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * Append padding bits for the input data.
+ * @param bstream Bitstream to be appended.
+ * @param input input data.
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ERANGE input data is too large.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_appendPaddingBit(BitStream *bstream, QRinput *input)
+{
+ int bits, maxbits, words, maxwords, i, ret;
+ BitStream *padding = NULL;
+ unsigned char *padbuf;
+ int padlen;
+
+ bits = BitStream_size(bstream);
+ maxwords = QRspec_getDataLength(input->version, input->level);
+ maxbits = maxwords * 8;
+
+ if(maxbits < bits) {
+ errno = ERANGE;
+ return -1;
+ }
+ if(maxbits == bits) {
+ return 0;
+ }
+
+ if(maxbits - bits <= 4) {
+ ret = BitStream_appendNum(bstream, maxbits - bits, 0);
+ goto DONE;
+ }
+
+ words = (bits + 4 + 7) / 8;
+
+ padding = BitStream_new();
+ if(padding == NULL) return -1;
+ ret = BitStream_appendNum(padding, words * 8 - bits, 0);
+ if(ret < 0) goto DONE;
+
+ padlen = maxwords - words;
+ if(padlen > 0) {
+ padbuf = (unsigned char *)malloc(padlen);
+ if(padbuf == NULL) {
+ ret = -1;
+ goto DONE;
+ }
+ for(i=0; i<padlen; i++) {
+ padbuf[i] = (i&1)?0x11:0xec;
+ }
+ ret = BitStream_appendBytes(padding, padlen, padbuf);
+ free(padbuf);
+ if(ret < 0) {
+ goto DONE;
+ }
+ }
+
+ ret = BitStream_append(bstream, padding);
+
+DONE:
+ BitStream_free(padding);
+ return ret;
+}
+
+/**
+ * Append padding bits for the input data - Micro QR Code version.
+ * @param bstream Bitstream to be appended.
+ * @param input input data.
+ * @retval 0 success
+ * @retval -1 an error occurred and errno is set to indeicate the error.
+ * See Execptions for the details.
+ * @throw ERANGE input data is too large.
+ * @throw ENOMEM unable to allocate memory.
+ */
+static int QRinput_appendPaddingBitMQR(BitStream *bstream, QRinput *input)
+{
+ int bits, maxbits, words, maxwords, i, ret, termbits;
+ BitStream *padding = NULL;
+ unsigned char *padbuf;
+ int padlen;
+
+ bits = BitStream_size(bstream);
+ maxbits = MQRspec_getDataLengthBit(input->version, input->level);
+ maxwords = maxbits / 8;
+
+ if(maxbits < bits) {
+ errno = ERANGE;
+ return -1;
+ }
+ if(maxbits == bits) {
+ return 0;
+ }
+
+ termbits = input->version * 2 + 1;
+
+ if(maxbits - bits <= termbits) {
+ ret = BitStream_appendNum(bstream, maxbits - bits, 0);
+ goto DONE;
+ }
+
+ bits += termbits;
+
+ words = (bits + 7) / 8;
+ if(maxbits - words * 8 > 0) {
+ termbits += words * 8 - bits;
+ if(words == maxwords) termbits += maxbits - words * 8;
+ } else {
+ termbits += words * 8 - bits;
+ }
+ padding = BitStream_new();
+ if(padding == NULL) return -1;
+ ret = BitStream_appendNum(padding, termbits, 0);
+ if(ret < 0) goto DONE;
+
+ padlen = maxwords - words;
+ if(padlen > 0) {
+ padbuf = (unsigned char *)malloc(padlen);
+ if(padbuf == NULL) {
+ ret = -1;
+ goto DONE;
+ }
+ for(i=0; i<padlen; i++) {
+ padbuf[i] = (i&1)?0x11:0xec;
+ }
+ ret = BitStream_appendBytes(padding, padlen, padbuf);
+ free(padbuf);
+ if(ret < 0) {
+ goto DONE;
+ }
+ termbits = maxbits - maxwords * 8;
+ if(termbits > 0) {
+ ret = BitStream_appendNum(padding, termbits, 0);
+ if(ret < 0) goto DONE;
+ }
+ }
+
+ ret = BitStream_append(bstream, padding);
+
+DONE:
+ BitStream_free(padding);
+ return ret;
+}
+
+static int QRinput_insertFNC1Header(QRinput *input)
+{
+ QRinput_List *entry = NULL;
+
+ if(input->fnc1 == 1) {
+ entry = QRinput_List_newEntry(QR_MODE_FNC1FIRST, 0, NULL);
+ } else if(input->fnc1 == 2) {
+ entry = QRinput_List_newEntry(QR_MODE_FNC1SECOND, 1, &(input->appid));
+ }
+ if(entry == NULL) {
+ return -1;
+ }
+
+ if(input->head->mode != QR_MODE_STRUCTURE || input->head->mode != QR_MODE_ECI) {
+ entry->next = input->head;
+ input->head = entry;
+ } else {
+ entry->next = input->head->next;
+ input->head->next = entry;
+ }
+
+ return 0;
+}
+
+/**
+ * Merge all bit streams in the input data.
+ * @param input input data.
+ * @return merged bit stream
+ */
+
+static BitStream *QRinput_mergeBitStream(QRinput *input)
+{
+ BitStream *bstream;
+ QRinput_List *list;
+ int ret;
+
+ if(input->mqr) {
+ if(QRinput_createBitStream(input) < 0) {
+ return NULL;
+ }
+ } else {
+ if(input->fnc1) {
+ if(QRinput_insertFNC1Header(input) < 0) {
+ return NULL;
+ }
+ }
+ if(QRinput_convertData(input) < 0) {
+ return NULL;
+ }
+ }
+
+ bstream = BitStream_new();
+ if(bstream == NULL) return NULL;
+
+ list = input->head;
+ while(list != NULL) {
+ ret = BitStream_append(bstream, list->bstream);
+ if(ret < 0) {
+ BitStream_free(bstream);
+ return NULL;
+ }
+ list = list->next;
+ }
+
+ return bstream;
+}
+
+/**
+ * Merge all bit streams in the input data and append padding bits
+ * @param input input data.
+ * @return padded merged bit stream
+ */
+
+static BitStream *QRinput_getBitStream(QRinput *input)
+{
+ BitStream *bstream;
+ int ret;
+
+ bstream = QRinput_mergeBitStream(input);
+ if(bstream == NULL) {
+ return NULL;
+ }
+ if(input->mqr) {
+ ret = QRinput_appendPaddingBitMQR(bstream, input);
+ } else {
+ ret = QRinput_appendPaddingBit(bstream, input);
+ }
+ if(ret < 0) {
+ BitStream_free(bstream);
+ return NULL;
+ }
+
+ return bstream;
+}
+
+/**
+ * Pack all bit streams padding bits into a byte array.
+ * @param input input data.
+ * @return padded merged byte stream
+ */
+
+unsigned char *QRinput_getByteStream(QRinput *input)
+{
+ BitStream *bstream;
+ unsigned char *array;
+
+ bstream = QRinput_getBitStream(input);
+ if(bstream == NULL) {
+ return NULL;
+ }
+ array = BitStream_toByte(bstream);
+ BitStream_free(bstream);
+
+ return array;
+}
+
+/******************************************************************************
+ * Structured input data
+ *****************************************************************************/
+
+static QRinput_InputList *QRinput_InputList_newEntry(QRinput *input)
+{
+ QRinput_InputList *entry;
+
+ entry = (QRinput_InputList *)malloc(sizeof(QRinput_InputList));
+ if(entry == NULL) return NULL;
+
+ entry->input = input;
+ entry->next = NULL;
+
+ return entry;
+}
+
+static void QRinput_InputList_freeEntry(QRinput_InputList *entry)
+{
+ if(entry != NULL) {
+ QRinput_free(entry->input);
+ free(entry);
+ }
+}
+
+QRinput_Struct *QRinput_Struct_new(void)
+{
+ QRinput_Struct *s;
+
+ s = (QRinput_Struct *)malloc(sizeof(QRinput_Struct));
+ if(s == NULL) return NULL;
+
+ s->size = 0;
+ s->parity = -1;
+ s->head = NULL;
+ s->tail = NULL;
+
+ return s;
+}
+
+void QRinput_Struct_setParity(QRinput_Struct *s, unsigned char parity)
+{
+ s->parity = (int)parity;
+}
+
+int QRinput_Struct_appendInput(QRinput_Struct *s, QRinput *input)
+{
+ QRinput_InputList *e;
+
+ if(input->mqr) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ e = QRinput_InputList_newEntry(input);
+ if(e == NULL) return -1;
+
+ s->size++;
+ if(s->tail == NULL) {
+ s->head = e;
+ s->tail = e;
+ } else {
+ s->tail->next = e;
+ s->tail = e;
+ }
+
+ return s->size;
+}
+
+void QRinput_Struct_free(QRinput_Struct *s)
+{
+ QRinput_InputList *list, *next;
+
+ if(s != NULL) {
+ list = s->head;
+ while(list != NULL) {
+ next = list->next;
+ QRinput_InputList_freeEntry(list);
+ list = next;
+ }
+ free(s);
+ }
+}
+
+static unsigned char QRinput_Struct_calcParity(QRinput_Struct *s)
+{
+ QRinput_InputList *list;
+ unsigned char parity = 0;
+
+ list = s->head;
+ while(list != NULL) {
+ parity ^= QRinput_calcParity(list->input);
+ list = list->next;
+ }
+
+ QRinput_Struct_setParity(s, parity);
+
+ return parity;
+}
+
+static int QRinput_List_shrinkEntry(QRinput_List *entry, int bytes)
+{
+ unsigned char *data;
+
+ data = (unsigned char *)malloc(bytes);
+ if(data == NULL) return -1;
+
+ memcpy(data, entry->data, bytes);
+ free(entry->data);
+ entry->data = data;
+ entry->size = bytes;
+
+ return 0;
+}
+
+static int QRinput_splitEntry(QRinput_List *entry, int bytes)
+{
+ QRinput_List *e;
+ int ret;
+
+ e = QRinput_List_newEntry(entry->mode, entry->size - bytes, entry->data + bytes);
+ if(e == NULL) {
+ return -1;
+ }
+
+ ret = QRinput_List_shrinkEntry(entry, bytes);
+ if(ret < 0) {
+ QRinput_List_freeEntry(e);
+ return -1;
+ }
+
+ e->next = entry->next;
+ entry->next = e;
+
+ return 0;
+}
+
+QRinput_Struct *QRinput_splitQRinputToStruct(QRinput *input)
+{
+ QRinput *p;
+ QRinput_Struct *s;
+ int bits, maxbits, nextbits, bytes, ret;
+ QRinput_List *list, *next, *prev;
+
+ if(input->mqr) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ s = QRinput_Struct_new();
+ if(s == NULL) return NULL;
+
+ input = QRinput_dup(input);
+ if(input == NULL) {
+ QRinput_Struct_free(s);
+ return NULL;
+ }
+
+ QRinput_Struct_setParity(s, QRinput_calcParity(input));
+ maxbits = QRspec_getDataLength(input->version, input->level) * 8 - STRUCTURE_HEADER_SIZE;
+
+ if(maxbits <= 0) {
+ QRinput_Struct_free(s);
+ QRinput_free(input);
+ return NULL;
+ }
+
+ bits = 0;
+ list = input->head;
+ prev = NULL;
+ while(list != NULL) {
+ nextbits = QRinput_estimateBitStreamSizeOfEntry(list, input->version, input->mqr);
+ if(bits + nextbits <= maxbits) {
+ ret = QRinput_encodeBitStream(list, input->version, input->mqr);
+ if(ret < 0) goto ABORT;
+ bits += ret;
+ prev = list;
+ list = list->next;
+ } else {
+ bytes = QRinput_lengthOfCode(list->mode, input->version, maxbits - bits);
+ p = QRinput_new2(input->version, input->level);
+ if(p == NULL) goto ABORT;
+ if(bytes > 0) {
+ /* Splits this entry into 2 entries. */
+ ret = QRinput_splitEntry(list, bytes);
+ if(ret < 0) {
+ QRinput_free(p);
+ goto ABORT;
+ }
+ /* First half is the tail of the current input. */
+ next = list->next;
+ list->next = NULL;
+ /* Second half is the head of the next input, p.*/
+ p->head = next;
+ /* Renew QRinput.tail. */
+ p->tail = input->tail;
+ input->tail = list;
+ /* Point to the next entry. */
+ prev = list;
+ list = next;
+ } else {
+ /* Current entry will go to the next input. */
+ prev->next = NULL;
+ p->head = list;
+ p->tail = input->tail;
+ input->tail = prev;
+ }
+ ret = QRinput_Struct_appendInput(s, input);
+ if(ret < 0) {
+ QRinput_free(p);
+ goto ABORT;
+ }
+ input = p;
+ bits = 0;
+ }
+ }
+ ret = QRinput_Struct_appendInput(s, input);
+ if(ret < 0) goto ABORT;
+ if(s->size > MAX_STRUCTURED_SYMBOLS) {
+ QRinput_Struct_free(s);
+ errno = ERANGE;
+ return NULL;
+ }
+ ret = QRinput_Struct_insertStructuredAppendHeaders(s);
+ if(ret < 0) {
+ QRinput_Struct_free(s);
+ return NULL;
+ }
+
+ return s;
+
+ABORT:
+ QRinput_free(input);
+ QRinput_Struct_free(s);
+ return NULL;
+}
+
+int QRinput_Struct_insertStructuredAppendHeaders(QRinput_Struct *s)
+{
+ int num, i;
+ QRinput_InputList *list;
+
+ if(s->parity < 0) {
+ QRinput_Struct_calcParity(s);
+ }
+ num = 0;
+ list = s->head;
+ while(list != NULL) {
+ num++;
+ list = list->next;
+ }
+ i = 1;
+ list = s->head;
+ while(list != NULL) {
+ if(QRinput_insertStructuredAppendHeader(list->input, num, i, s->parity))
+ return -1;
+ i++;
+ list = list->next;
+ }
+
+ return 0;
+}
+
+/******************************************************************************
+ * Extended encoding mode (FNC1 and ECI)
+ *****************************************************************************/
+
+int QRinput_setFNC1First(QRinput *input)
+{
+ if(input->mqr) {
+ errno = EINVAL;
+ return -1;
+ }
+ input->fnc1 = 1;
+
+ return 0;
+}
+
+int QRinput_setFNC1Second(QRinput *input, unsigned char appid)
+{
+ if(input->mqr) {
+ errno = EINVAL;
+ return -1;
+ }
+ input->fnc1 = 2;
+ input->appid = appid;
+
+ return 0;
+}
diff --git a/ap/app/qrencode/qrinput.h b/ap/app/qrencode/qrinput.h
new file mode 100644
index 0000000..9e6bad6
--- /dev/null
+++ b/ap/app/qrencode/qrinput.h
@@ -0,0 +1,123 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Input data chunk class
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __QRINPUT_H__
+#define __QRINPUT_H__
+
+#include "qrencode.h"
+#include "bitstream.h"
+
+int QRinput_isSplittableMode(QRencodeMode mode);
+
+/******************************************************************************
+ * Entry of input data
+ *****************************************************************************/
+typedef struct _QRinput_List QRinput_List;
+
+struct _QRinput_List {
+ QRencodeMode mode;
+ int size; ///< Size of data chunk (byte).
+ unsigned char *data; ///< Data chunk.
+ BitStream *bstream;
+ QRinput_List *next;
+};
+
+/******************************************************************************
+ * Input Data
+ *****************************************************************************/
+struct _QRinput {
+ int version;
+ QRecLevel level;
+ QRinput_List *head;
+ QRinput_List *tail;
+ int mqr;
+ int fnc1;
+ unsigned char appid;
+};
+
+/******************************************************************************
+ * Structured append input data
+ *****************************************************************************/
+typedef struct _QRinput_InputList QRinput_InputList;
+
+struct _QRinput_InputList {
+ QRinput *input;
+ QRinput_InputList *next;
+};
+
+struct _QRinput_Struct {
+ int size; ///< number of structured symbols
+ int parity;
+ QRinput_InputList *head;
+ QRinput_InputList *tail;
+};
+
+/**
+ * Pack all bit streams padding bits into a byte array.
+ * @param input input data.
+ * @return padded merged byte stream
+ */
+extern unsigned char *QRinput_getByteStream(QRinput *input);
+
+
+extern int QRinput_estimateBitsModeNum(int size);
+extern int QRinput_estimateBitsModeAn(int size);
+extern int QRinput_estimateBitsMode8(int size);
+extern int QRinput_estimateBitsModeKanji(int size);
+
+extern QRinput *QRinput_dup(QRinput *input);
+
+extern const signed char QRinput_anTable[128];
+
+/**
+ * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
+ * @param __c__ character
+ * @return value
+ */
+#define QRinput_lookAnTable(__c__) \
+ ((__c__ & 0x80)?-1:QRinput_anTable[(int)__c__])
+
+/**
+ * Length of a standard mode indicator in bits.
+ */
+
+#define MODE_INDICATOR_SIZE 4
+
+/**
+ * Length of a segment of structured-append header.
+ */
+#define STRUCTURE_HEADER_SIZE 20
+
+/**
+ * Maximum number of symbols in a set of structured-appended symbols.
+ */
+#define MAX_STRUCTURED_SYMBOLS 16
+
+#ifdef WITH_TESTS
+extern BitStream *QRinput_mergeBitStream(QRinput *input);
+extern BitStream *QRinput_getBitStream(QRinput *input);
+extern int QRinput_estimateBitStreamSize(QRinput *input, int version);
+extern int QRinput_splitEntry(QRinput_List *entry, int bytes);
+extern int QRinput_lengthOfCode(QRencodeMode mode, int version, int bits);
+extern int QRinput_insertStructuredAppendHeader(QRinput *input, int size, int index, unsigned char parity);
+#endif
+
+#endif /* __QRINPUT_H__ */
diff --git a/ap/app/qrencode/qrspec.c b/ap/app/qrencode/qrspec.c
new file mode 100644
index 0000000..a42706f
--- /dev/null
+++ b/ap/app/qrencode/qrspec.c
@@ -0,0 +1,562 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * QR Code specification in convenient format.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * The following data / specifications are taken from
+ * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
+ * or
+ * "Automatic identification and data capture techniques --
+ * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#ifdef HAVE_LIBPTHREAD
+#include <pthread.h>
+#endif
+
+#include "qrspec.h"
+#include "qrinput.h"
+
+/******************************************************************************
+ * Version and capacity
+ *****************************************************************************/
+
+typedef struct {
+ int width; //< Edge length of the symbol
+ int words; //< Data capacity (bytes)
+ int remainder; //< Remainder bit (bits)
+ int ec[4]; //< Number of ECC code (bytes)
+} QRspec_Capacity;
+
+/**
+ * Table of the capacity of symbols
+ * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
+ */
+static const QRspec_Capacity qrspecCapacity[QRSPEC_VERSION_MAX + 1] = {
+ { 0, 0, 0, { 0, 0, 0, 0}},
+ { 21, 26, 0, { 7, 10, 13, 17}}, // 1
+ { 25, 44, 7, { 10, 16, 22, 28}},
+ { 29, 70, 7, { 15, 26, 36, 44}},
+ { 33, 100, 7, { 20, 36, 52, 64}},
+ { 37, 134, 7, { 26, 48, 72, 88}}, // 5
+ { 41, 172, 7, { 36, 64, 96, 112}},
+ { 45, 196, 0, { 40, 72, 108, 130}},
+ { 49, 242, 0, { 48, 88, 132, 156}},
+ { 53, 292, 0, { 60, 110, 160, 192}},
+ { 57, 346, 0, { 72, 130, 192, 224}}, //10
+ { 61, 404, 0, { 80, 150, 224, 264}},
+ { 65, 466, 0, { 96, 176, 260, 308}},
+ { 69, 532, 0, { 104, 198, 288, 352}},
+ { 73, 581, 3, { 120, 216, 320, 384}},
+ { 77, 655, 3, { 132, 240, 360, 432}}, //15
+ { 81, 733, 3, { 144, 280, 408, 480}},
+ { 85, 815, 3, { 168, 308, 448, 532}},
+ { 89, 901, 3, { 180, 338, 504, 588}},
+ { 93, 991, 3, { 196, 364, 546, 650}},
+ { 97, 1085, 3, { 224, 416, 600, 700}}, //20
+ {101, 1156, 4, { 224, 442, 644, 750}},
+ {105, 1258, 4, { 252, 476, 690, 816}},
+ {109, 1364, 4, { 270, 504, 750, 900}},
+ {113, 1474, 4, { 300, 560, 810, 960}},
+ {117, 1588, 4, { 312, 588, 870, 1050}}, //25
+ {121, 1706, 4, { 336, 644, 952, 1110}},
+ {125, 1828, 4, { 360, 700, 1020, 1200}},
+ {129, 1921, 3, { 390, 728, 1050, 1260}},
+ {133, 2051, 3, { 420, 784, 1140, 1350}},
+ {137, 2185, 3, { 450, 812, 1200, 1440}}, //30
+ {141, 2323, 3, { 480, 868, 1290, 1530}},
+ {145, 2465, 3, { 510, 924, 1350, 1620}},
+ {149, 2611, 3, { 540, 980, 1440, 1710}},
+ {153, 2761, 3, { 570, 1036, 1530, 1800}},
+ {157, 2876, 0, { 570, 1064, 1590, 1890}}, //35
+ {161, 3034, 0, { 600, 1120, 1680, 1980}},
+ {165, 3196, 0, { 630, 1204, 1770, 2100}},
+ {169, 3362, 0, { 660, 1260, 1860, 2220}},
+ {173, 3532, 0, { 720, 1316, 1950, 2310}},
+ {177, 3706, 0, { 750, 1372, 2040, 2430}} //40
+};
+
+int QRspec_getDataLength(int version, QRecLevel level)
+{
+ return qrspecCapacity[version].words - qrspecCapacity[version].ec[level];
+}
+
+int QRspec_getECCLength(int version, QRecLevel level)
+{
+ return qrspecCapacity[version].ec[level];
+}
+
+int QRspec_getMinimumVersion(int size, QRecLevel level)
+{
+ int i;
+ int words;
+
+ for(i=1; i<= QRSPEC_VERSION_MAX; i++) {
+ words = qrspecCapacity[i].words - qrspecCapacity[i].ec[level];
+ if(words >= size) return i;
+ }
+
+ return -1;
+}
+
+int QRspec_getWidth(int version)
+{
+ return qrspecCapacity[version].width;
+}
+
+int QRspec_getRemainder(int version)
+{
+ return qrspecCapacity[version].remainder;
+}
+
+/******************************************************************************
+ * Length indicator
+ *****************************************************************************/
+
+static const int lengthTableBits[4][3] = {
+ {10, 12, 14},
+ { 9, 11, 13},
+ { 8, 16, 16},
+ { 8, 10, 12}
+};
+
+int QRspec_lengthIndicator(QRencodeMode mode, int version)
+{
+ int l;
+
+ if(!QRinput_isSplittableMode(mode)) return 0;
+ if(version <= 9) {
+ l = 0;
+ } else if(version <= 26) {
+ l = 1;
+ } else {
+ l = 2;
+ }
+
+ return lengthTableBits[mode][l];
+}
+
+int QRspec_maximumWords(QRencodeMode mode, int version)
+{
+ int l;
+ int bits;
+ int words;
+
+ if(!QRinput_isSplittableMode(mode)) return 0;
+ if(version <= 9) {
+ l = 0;
+ } else if(version <= 26) {
+ l = 1;
+ } else {
+ l = 2;
+ }
+
+ bits = lengthTableBits[mode][l];
+ words = (1 << bits) - 1;
+ if(mode == QR_MODE_KANJI) {
+ words *= 2; // the number of bytes is required
+ }
+
+ return words;
+}
+
+/******************************************************************************
+ * Error correction code
+ *****************************************************************************/
+
+/**
+ * Table of the error correction code (Reed-Solomon block)
+ * See Table 12-16 (pp.30-36), JIS X0510:2004.
+ */
+static const int eccTable[QRSPEC_VERSION_MAX+1][4][2] = {
+ {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0}},
+ {{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}}, // 1
+ {{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}},
+ {{ 1, 0}, { 1, 0}, { 2, 0}, { 2, 0}},
+ {{ 1, 0}, { 2, 0}, { 2, 0}, { 4, 0}},
+ {{ 1, 0}, { 2, 0}, { 2, 2}, { 2, 2}}, // 5
+ {{ 2, 0}, { 4, 0}, { 4, 0}, { 4, 0}},
+ {{ 2, 0}, { 4, 0}, { 2, 4}, { 4, 1}},
+ {{ 2, 0}, { 2, 2}, { 4, 2}, { 4, 2}},
+ {{ 2, 0}, { 3, 2}, { 4, 4}, { 4, 4}},
+ {{ 2, 2}, { 4, 1}, { 6, 2}, { 6, 2}}, //10
+ {{ 4, 0}, { 1, 4}, { 4, 4}, { 3, 8}},
+ {{ 2, 2}, { 6, 2}, { 4, 6}, { 7, 4}},
+ {{ 4, 0}, { 8, 1}, { 8, 4}, {12, 4}},
+ {{ 3, 1}, { 4, 5}, {11, 5}, {11, 5}},
+ {{ 5, 1}, { 5, 5}, { 5, 7}, {11, 7}}, //15
+ {{ 5, 1}, { 7, 3}, {15, 2}, { 3, 13}},
+ {{ 1, 5}, {10, 1}, { 1, 15}, { 2, 17}},
+ {{ 5, 1}, { 9, 4}, {17, 1}, { 2, 19}},
+ {{ 3, 4}, { 3, 11}, {17, 4}, { 9, 16}},
+ {{ 3, 5}, { 3, 13}, {15, 5}, {15, 10}}, //20
+ {{ 4, 4}, {17, 0}, {17, 6}, {19, 6}},
+ {{ 2, 7}, {17, 0}, { 7, 16}, {34, 0}},
+ {{ 4, 5}, { 4, 14}, {11, 14}, {16, 14}},
+ {{ 6, 4}, { 6, 14}, {11, 16}, {30, 2}},
+ {{ 8, 4}, { 8, 13}, { 7, 22}, {22, 13}}, //25
+ {{10, 2}, {19, 4}, {28, 6}, {33, 4}},
+ {{ 8, 4}, {22, 3}, { 8, 26}, {12, 28}},
+ {{ 3, 10}, { 3, 23}, { 4, 31}, {11, 31}},
+ {{ 7, 7}, {21, 7}, { 1, 37}, {19, 26}},
+ {{ 5, 10}, {19, 10}, {15, 25}, {23, 25}}, //30
+ {{13, 3}, { 2, 29}, {42, 1}, {23, 28}},
+ {{17, 0}, {10, 23}, {10, 35}, {19, 35}},
+ {{17, 1}, {14, 21}, {29, 19}, {11, 46}},
+ {{13, 6}, {14, 23}, {44, 7}, {59, 1}},
+ {{12, 7}, {12, 26}, {39, 14}, {22, 41}}, //35
+ {{ 6, 14}, { 6, 34}, {46, 10}, { 2, 64}},
+ {{17, 4}, {29, 14}, {49, 10}, {24, 46}},
+ {{ 4, 18}, {13, 32}, {48, 14}, {42, 32}},
+ {{20, 4}, {40, 7}, {43, 22}, {10, 67}},
+ {{19, 6}, {18, 31}, {34, 34}, {20, 61}},//40
+};
+
+void QRspec_getEccSpec(int version, QRecLevel level, int spec[5])
+{
+ int b1, b2;
+ int data, ecc;
+
+ b1 = eccTable[version][level][0];
+ b2 = eccTable[version][level][1];
+ data = QRspec_getDataLength(version, level);
+ ecc = QRspec_getECCLength(version, level);
+
+ if(b2 == 0) {
+ spec[0] = b1;
+ spec[1] = data / b1;
+ spec[2] = ecc / b1;
+ spec[3] = spec[4] = 0;
+ } else {
+ spec[0] = b1;
+ spec[1] = data / (b1 + b2);
+ spec[2] = ecc / (b1 + b2);
+ spec[3] = b2;
+ spec[4] = spec[1] + 1;
+ }
+}
+
+/******************************************************************************
+ * Alignment pattern
+ *****************************************************************************/
+
+/**
+ * Positions of alignment patterns.
+ * This array includes only the second and the third position of the alignment
+ * patterns. Rest of them can be calculated from the distance between them.
+ *
+ * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
+ */
+static const int alignmentPattern[QRSPEC_VERSION_MAX+1][2] = {
+ { 0, 0},
+ { 0, 0}, {18, 0}, {22, 0}, {26, 0}, {30, 0}, // 1- 5
+ {34, 0}, {22, 38}, {24, 42}, {26, 46}, {28, 50}, // 6-10
+ {30, 54}, {32, 58}, {34, 62}, {26, 46}, {26, 48}, //11-15
+ {26, 50}, {30, 54}, {30, 56}, {30, 58}, {34, 62}, //16-20
+ {28, 50}, {26, 50}, {30, 54}, {28, 54}, {32, 58}, //21-25
+ {30, 58}, {34, 62}, {26, 50}, {30, 54}, {26, 52}, //26-30
+ {30, 56}, {34, 60}, {30, 58}, {34, 62}, {30, 54}, //31-35
+ {24, 50}, {28, 54}, {32, 58}, {26, 54}, {30, 58}, //35-40
+};
+
+/**
+ * Put an alignment marker.
+ * @param frame
+ * @param width
+ * @param ox,oy center coordinate of the pattern
+ */
+static void QRspec_putAlignmentMarker(unsigned char *frame, int width, int ox, int oy)
+{
+ static const unsigned char finder[] = {
+ 0xa1, 0xa1, 0xa1, 0xa1, 0xa1,
+ 0xa1, 0xa0, 0xa0, 0xa0, 0xa1,
+ 0xa1, 0xa0, 0xa1, 0xa0, 0xa1,
+ 0xa1, 0xa0, 0xa0, 0xa0, 0xa1,
+ 0xa1, 0xa1, 0xa1, 0xa1, 0xa1,
+ };
+ int x, y;
+ const unsigned char *s;
+
+ frame += (oy - 2) * width + ox - 2;
+ s = finder;
+ for(y=0; y<5; y++) {
+ for(x=0; x<5; x++) {
+ frame[x] = s[x];
+ }
+ frame += width;
+ s += 5;
+ }
+}
+
+static void QRspec_putAlignmentPattern(int version, unsigned char *frame, int width)
+{
+ int d, w, x, y, cx, cy;
+
+ if(version < 2) return;
+
+ d = alignmentPattern[version][1] - alignmentPattern[version][0];
+ if(d < 0) {
+ w = 2;
+ } else {
+ w = (width - alignmentPattern[version][0]) / d + 2;
+ }
+
+ if(w * w - 3 == 1) {
+ x = alignmentPattern[version][0];
+ y = alignmentPattern[version][0];
+ QRspec_putAlignmentMarker(frame, width, x, y);
+ return;
+ }
+
+ cx = alignmentPattern[version][0];
+ for(x=1; x<w - 1; x++) {
+ QRspec_putAlignmentMarker(frame, width, 6, cx);
+ QRspec_putAlignmentMarker(frame, width, cx, 6);
+ cx += d;
+ }
+
+ cy = alignmentPattern[version][0];
+ for(y=0; y<w-1; y++) {
+ cx = alignmentPattern[version][0];
+ for(x=0; x<w-1; x++) {
+ QRspec_putAlignmentMarker(frame, width, cx, cy);
+ cx += d;
+ }
+ cy += d;
+ }
+}
+
+/******************************************************************************
+ * Version information pattern
+ *****************************************************************************/
+
+/**
+ * Version information pattern (BCH coded).
+ * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
+ */
+static const unsigned int versionPattern[QRSPEC_VERSION_MAX - 6] = {
+ 0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
+ 0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
+ 0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
+ 0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
+ 0x27541, 0x28c69
+};
+
+unsigned int QRspec_getVersionPattern(int version)
+{
+ if(version < 7 || version > QRSPEC_VERSION_MAX) return 0;
+
+ return versionPattern[version - 7];
+}
+
+/******************************************************************************
+ * Format information
+ *****************************************************************************/
+
+/* See calcFormatInfo in tests/test_qrspec.c */
+static const unsigned int formatInfo[4][8] = {
+ {0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976},
+ {0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0},
+ {0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed},
+ {0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b}
+};
+
+unsigned int QRspec_getFormatInfo(int mask, QRecLevel level)
+{
+ if(mask < 0 || mask > 7) return 0;
+
+ return formatInfo[level][mask];
+}
+
+/******************************************************************************
+ * Frame
+ *****************************************************************************/
+
+/**
+ * Cache of initial frames.
+ */
+/* C99 says that static storage shall be initialized to a null pointer
+ * by compiler. */
+static unsigned char *frames[QRSPEC_VERSION_MAX + 1];
+#ifdef HAVE_LIBPTHREAD
+static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+/**
+ * Put a finder pattern.
+ * @param frame
+ * @param width
+ * @param ox,oy upper-left coordinate of the pattern
+ */
+static void putFinderPattern(unsigned char *frame, int width, int ox, int oy)
+{
+ static const unsigned char finder[] = {
+ 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
+ 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc1, 0xc1, 0xc1, 0xc0, 0xc1,
+ 0xc1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc1,
+ 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
+ };
+ int x, y;
+ const unsigned char *s;
+
+ frame += oy * width + ox;
+ s = finder;
+ for(y=0; y<7; y++) {
+ for(x=0; x<7; x++) {
+ frame[x] = s[x];
+ }
+ frame += width;
+ s += 7;
+ }
+}
+
+
+static unsigned char *QRspec_createFrame(int version)
+{
+ unsigned char *frame, *p, *q;
+ int width;
+ int x, y;
+ unsigned int verinfo, v;
+
+ width = qrspecCapacity[version].width;
+ frame = (unsigned char *)malloc(width * width);
+ if(frame == NULL) return NULL;
+
+ memset(frame, 0, width * width);
+ /* Finder pattern */
+ putFinderPattern(frame, width, 0, 0);
+ putFinderPattern(frame, width, width - 7, 0);
+ putFinderPattern(frame, width, 0, width - 7);
+ /* Separator */
+ p = frame;
+ q = frame + width * (width - 7);
+ for(y=0; y<7; y++) {
+ p[7] = 0xc0;
+ p[width - 8] = 0xc0;
+ q[7] = 0xc0;
+ p += width;
+ q += width;
+ }
+ memset(frame + width * 7, 0xc0, 8);
+ memset(frame + width * 8 - 8, 0xc0, 8);
+ memset(frame + width * (width - 8), 0xc0, 8);
+ /* Mask format information area */
+ memset(frame + width * 8, 0x84, 9);
+ memset(frame + width * 9 - 8, 0x84, 8);
+ p = frame + 8;
+ for(y=0; y<8; y++) {
+ *p = 0x84;
+ p += width;
+ }
+ p = frame + width * (width - 7) + 8;
+ for(y=0; y<7; y++) {
+ *p = 0x84;
+ p += width;
+ }
+ /* Timing pattern */
+ p = frame + width * 6 + 8;
+ q = frame + width * 8 + 6;
+ for(x=1; x<width-15; x++) {
+ *p = 0x90 | (x & 1);
+ *q = 0x90 | (x & 1);
+ p++;
+ q += width;
+ }
+ /* Alignment pattern */
+ QRspec_putAlignmentPattern(version, frame, width);
+
+ /* Version information */
+ if(version >= 7) {
+ verinfo = QRspec_getVersionPattern(version);
+
+ p = frame + width * (width - 11);
+ v = verinfo;
+ for(x=0; x<6; x++) {
+ for(y=0; y<3; y++) {
+ p[width * y + x] = 0x88 | (v & 1);
+ v = v >> 1;
+ }
+ }
+
+ p = frame + width - 11;
+ v = verinfo;
+ for(y=0; y<6; y++) {
+ for(x=0; x<3; x++) {
+ p[x] = 0x88 | (v & 1);
+ v = v >> 1;
+ }
+ p += width;
+ }
+ }
+ /* and a little bit... */
+ frame[width * (width - 8) + 8] = 0x81;
+
+ return frame;
+}
+
+unsigned char *QRspec_newFrame(int version)
+{
+ unsigned char *frame;
+ int width;
+
+ if(version < 1 || version > QRSPEC_VERSION_MAX) return NULL;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&frames_mutex);
+#endif
+ if(frames[version] == NULL) {
+ frames[version] = QRspec_createFrame(version);
+ }
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&frames_mutex);
+#endif
+ if(frames[version] == NULL) return NULL;
+
+ width = qrspecCapacity[version].width;
+ frame = (unsigned char *)malloc(width * width);
+ if(frame == NULL) return NULL;
+ memcpy(frame, frames[version], width * width);
+
+ return frame;
+}
+
+void QRspec_clearCache(void)
+{
+ int i;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&frames_mutex);
+#endif
+ for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
+ free(frames[i]);
+ frames[i] = NULL;
+ }
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&frames_mutex);
+#endif
+}
diff --git a/ap/app/qrencode/qrspec.h b/ap/app/qrencode/qrspec.h
new file mode 100644
index 0000000..54a3d9f
--- /dev/null
+++ b/ap/app/qrencode/qrspec.h
@@ -0,0 +1,181 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * QR Code specification in convenient format.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __QRSPEC_H__
+#define __QRSPEC_H__
+
+#include "qrencode.h"
+
+/******************************************************************************
+ * Version and capacity
+ *****************************************************************************/
+
+/**
+ * Maximum width of a symbol
+ */
+#define QRSPEC_WIDTH_MAX 177
+
+/**
+ * Return maximum data code length (bytes) for the version.
+ * @param version
+ * @param level
+ * @return maximum size (bytes)
+ */
+extern int QRspec_getDataLength(int version, QRecLevel level);
+
+/**
+ * Return maximum error correction code length (bytes) for the version.
+ * @param version
+ * @param level
+ * @return ECC size (bytes)
+ */
+extern int QRspec_getECCLength(int version, QRecLevel level);
+
+/**
+ * Return a version number that satisfies the input code length.
+ * @param size input code length (byte)
+ * @param level
+ * @return version number
+ */
+extern int QRspec_getMinimumVersion(int size, QRecLevel level);
+
+/**
+ * Return the width of the symbol for the version.
+ * @param version
+ * @return width
+ */
+extern int QRspec_getWidth(int version);
+
+/**
+ * Return the numer of remainder bits.
+ * @param version
+ * @return number of remainder bits
+ */
+extern int QRspec_getRemainder(int version);
+
+/******************************************************************************
+ * Length indicator
+ *****************************************************************************/
+
+/**
+ * Return the size of lenght indicator for the mode and version.
+ * @param mode
+ * @param version
+ * @return the size of the appropriate length indicator (bits).
+ */
+extern int QRspec_lengthIndicator(QRencodeMode mode, int version);
+
+/**
+ * Return the maximum length for the mode and version.
+ * @param mode
+ * @param version
+ * @return the maximum length (bytes)
+ */
+extern int QRspec_maximumWords(QRencodeMode mode, int version);
+
+/******************************************************************************
+ * Error correction code
+ *****************************************************************************/
+
+/**
+ * Return an array of ECC specification.
+ * @param version
+ * @param level
+ * @param spec an array of ECC specification contains as following:
+ * {# of type1 blocks, # of data code, # of ecc code,
+ * # of type2 blocks, # of data code}
+ */
+void QRspec_getEccSpec(int version, QRecLevel level, int spec[5]);
+
+#define QRspec_rsBlockNum(__spec__) (__spec__[0] + __spec__[3])
+#define QRspec_rsBlockNum1(__spec__) (__spec__[0])
+#define QRspec_rsDataCodes1(__spec__) (__spec__[1])
+#define QRspec_rsEccCodes1(__spec__) (__spec__[2])
+#define QRspec_rsBlockNum2(__spec__) (__spec__[3])
+#define QRspec_rsDataCodes2(__spec__) (__spec__[4])
+#define QRspec_rsEccCodes2(__spec__) (__spec__[2])
+
+#define QRspec_rsDataLength(__spec__) \
+ ((QRspec_rsBlockNum1(__spec__) * QRspec_rsDataCodes1(__spec__)) + \
+ (QRspec_rsBlockNum2(__spec__) * QRspec_rsDataCodes2(__spec__)))
+#define QRspec_rsEccLength(__spec__) \
+ (QRspec_rsBlockNum(__spec__) * QRspec_rsEccCodes1(__spec__))
+
+/******************************************************************************
+ * Version information pattern
+ *****************************************************************************/
+
+/**
+ * Return BCH encoded version information pattern that is used for the symbol
+ * of version 7 or greater. Use lower 18 bits.
+ * @param version
+ * @return BCH encoded version information pattern
+ */
+extern unsigned int QRspec_getVersionPattern(int version);
+
+/******************************************************************************
+ * Format information
+ *****************************************************************************/
+
+/**
+ * Return BCH encoded format information pattern.
+ * @param mask
+ * @param level
+ * @return BCH encoded format information pattern
+ */
+extern unsigned int QRspec_getFormatInfo(int mask, QRecLevel level);
+
+/******************************************************************************
+ * Frame
+ *****************************************************************************/
+
+/**
+ * Return a copy of initialized frame.
+ * When the same version is requested twice or more, a copy of cached frame
+ * is returned.
+ * @param version
+ * @return Array of unsigned char. You can free it by free().
+ */
+extern unsigned char *QRspec_newFrame(int version);
+
+/**
+ * Clear the frame cache. Typically for debug.
+ */
+extern void QRspec_clearCache(void);
+
+/******************************************************************************
+ * Mode indicator
+ *****************************************************************************/
+
+/**
+ * Mode indicator. See Table 2 of JIS X0510:2004, pp.16.
+ */
+#define QRSPEC_MODEID_ECI 7
+#define QRSPEC_MODEID_NUM 1
+#define QRSPEC_MODEID_AN 2
+#define QRSPEC_MODEID_8 4
+#define QRSPEC_MODEID_KANJI 8
+#define QRSPEC_MODEID_FNC1FIRST 5
+#define QRSPEC_MODEID_FNC1SECOND 9
+#define QRSPEC_MODEID_STRUCTURE 3
+#define QRSPEC_MODEID_TERMINATOR 0
+
+#endif /* __QRSPEC_H__ */
diff --git a/ap/app/qrencode/rscode.c b/ap/app/qrencode/rscode.c
new file mode 100644
index 0000000..edc32e2
--- /dev/null
+++ b/ap/app/qrencode/rscode.c
@@ -0,0 +1,327 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Reed solomon encoder. This code is taken from Phil Karn's libfec then
+ * editted and packed into a pair of .c and .h files.
+ *
+ * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
+ * (libfec is released under the GNU Lesser General Public License.)
+ *
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_LIBPTHREAD
+# include <pthread.h>
+#endif
+
+#include "rscode.h"
+
+/* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
+ *
+ */
+typedef unsigned char data_t;
+
+
+/**
+ * Reed-Solomon codec control block
+ */
+struct _RS {
+ int mm; /* Bits per symbol */
+ int nn; /* Symbols per block (= (1<<mm)-1) */
+ data_t *alpha_to; /* log lookup table */
+ data_t *index_of; /* Antilog lookup table */
+ data_t *genpoly; /* Generator polynomial */
+ int nroots; /* Number of generator roots = number of parity symbols */
+ int fcr; /* First consecutive root, index form */
+ int prim; /* Primitive element, index form */
+ int iprim; /* prim-th root of 1, index form */
+ int pad; /* Padding bytes in shortened block */
+ int gfpoly;
+ struct _RS *next;
+};
+
+static RS *rslist = NULL;
+#ifdef HAVE_LIBPTHREAD
+static pthread_mutex_t rslist_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+static inline int modnn(RS *rs, int x){
+ while (x >= rs->nn) {
+ x -= rs->nn;
+ x = (x >> rs->mm) + (x & rs->nn);
+ }
+ return x;
+}
+
+
+#define MODNN(x) modnn(rs,x)
+
+#define MM (rs->mm)
+#define NN (rs->nn)
+#define ALPHA_TO (rs->alpha_to)
+#define INDEX_OF (rs->index_of)
+#define GENPOLY (rs->genpoly)
+#define NROOTS (rs->nroots)
+#define FCR (rs->fcr)
+#define PRIM (rs->prim)
+#define IPRIM (rs->iprim)
+#define PAD (rs->pad)
+#define A0 (NN)
+
+
+/* Initialize a Reed-Solomon codec
+ * symsize = symbol size, bits
+ * gfpoly = Field generator polynomial coefficients
+ * fcr = first root of RS code generator polynomial, index form
+ * prim = primitive element to generate polynomial roots
+ * nroots = RS code generator polynomial degree (number of roots)
+ * pad = padding bytes at front of shortened block
+ */
+static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
+{
+ RS *rs;
+
+
+/* Common code for intializing a Reed-Solomon control block (char or int symbols)
+ * Copyright 2004 Phil Karn, KA9Q
+ * May be used under the terms of the GNU Lesser General Public License (LGPL)
+ */
+//#undef NULL
+//#define NULL ((void *)0)
+
+ int i, j, sr,root,iprim;
+
+ rs = NULL;
+ /* Check parameter ranges */
+ if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
+ goto done;
+ }
+
+ if(fcr < 0 || fcr >= (1<<symsize))
+ goto done;
+ if(prim <= 0 || prim >= (1<<symsize))
+ goto done;
+ if(nroots < 0 || nroots >= (1<<symsize))
+ goto done; /* Can't have more roots than symbol values! */
+ if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
+ goto done; /* Too much padding */
+
+ rs = (RS *)calloc(1,sizeof(RS));
+ if(rs == NULL)
+ goto done;
+
+ rs->mm = symsize;
+ rs->nn = (1<<symsize)-1;
+ rs->pad = pad;
+
+ rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
+ if(rs->alpha_to == NULL){
+ free(rs);
+ rs = NULL;
+ goto done;
+ }
+ rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
+ if(rs->index_of == NULL){
+ free(rs->alpha_to);
+ free(rs);
+ rs = NULL;
+ goto done;
+ }
+
+ /* Generate Galois field lookup tables */
+ rs->index_of[0] = A0; /* log(zero) = -inf */
+ rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
+ sr = 1;
+ for(i=0;i<rs->nn;i++){
+ rs->index_of[sr] = i;
+ rs->alpha_to[i] = sr;
+ sr <<= 1;
+ if(sr & (1<<symsize))
+ sr ^= gfpoly;
+ sr &= rs->nn;
+ }
+ if(sr != 1){
+ /* field generator polynomial is not primitive! */
+ free(rs->alpha_to);
+ free(rs->index_of);
+ free(rs);
+ rs = NULL;
+ goto done;
+ }
+
+ /* Form RS code generator polynomial from its roots */
+ rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
+ if(rs->genpoly == NULL){
+ free(rs->alpha_to);
+ free(rs->index_of);
+ free(rs);
+ rs = NULL;
+ goto done;
+ }
+ rs->fcr = fcr;
+ rs->prim = prim;
+ rs->nroots = nroots;
+ rs->gfpoly = gfpoly;
+
+ /* Find prim-th root of 1, used in decoding */
+ for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
+ ;
+ rs->iprim = iprim / prim;
+
+ rs->genpoly[0] = 1;
+ for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
+ rs->genpoly[i+1] = 1;
+
+ /* Multiply rs->genpoly[] by @**(root + x) */
+ for (j = i; j > 0; j--){
+ if (rs->genpoly[j] != 0)
+ rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
+ else
+ rs->genpoly[j] = rs->genpoly[j-1];
+ }
+ /* rs->genpoly[0] can never be zero */
+ rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
+ }
+ /* convert rs->genpoly[] to index form for quicker encoding */
+ for (i = 0; i <= nroots; i++)
+ rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
+ done:;
+
+ return rs;
+}
+
+RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
+{
+ RS *rs;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&rslist_mutex);
+#endif
+ for(rs = rslist; rs != NULL; rs = rs->next) {
+ if(rs->pad != pad) continue;
+ if(rs->nroots != nroots) continue;
+ if(rs->mm != symsize) continue;
+ if(rs->gfpoly != gfpoly) continue;
+ if(rs->fcr != fcr) continue;
+ if(rs->prim != prim) continue;
+
+ goto DONE;
+ }
+
+ rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
+ if(rs == NULL) goto DONE;
+ rs->next = rslist;
+ rslist = rs;
+
+DONE:
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&rslist_mutex);
+#endif
+ return rs;
+}
+
+
+void free_rs_char(RS *rs)
+{
+ free(rs->alpha_to);
+ free(rs->index_of);
+ free(rs->genpoly);
+ free(rs);
+}
+
+void free_rs_cache(void)
+{
+ RS *rs, *next;
+
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_lock(&rslist_mutex);
+#endif
+ rs = rslist;
+ while(rs != NULL) {
+ next = rs->next;
+ free_rs_char(rs);
+ rs = next;
+ }
+ rslist = NULL;
+#ifdef HAVE_LIBPTHREAD
+ pthread_mutex_unlock(&rslist_mutex);
+#endif
+}
+
+/* The guts of the Reed-Solomon encoder, meant to be #included
+ * into a function body with the following typedefs, macros and variables supplied
+ * according to the code parameters:
+
+ * data_t - a typedef for the data symbol
+ * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
+ * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
+ * NROOTS - the number of roots in the RS code generator polynomial,
+ * which is the same as the number of parity symbols in a block.
+ Integer variable or literal.
+ *
+ * NN - the total number of symbols in a RS block. Integer variable or literal.
+ * PAD - the number of pad symbols in a block. Integer variable or literal.
+ * ALPHA_TO - The address of an array of NN elements to convert Galois field
+ * elements in index (log) form to polynomial form. Read only.
+ * INDEX_OF - The address of an array of NN elements to convert Galois field
+ * elements in polynomial form to index (log) form. Read only.
+ * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
+ * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
+
+ * The memset() and memmove() functions are used. The appropriate header
+ * file declaring these functions (usually <string.h>) must be included by the calling
+ * program.
+
+ * Copyright 2004, Phil Karn, KA9Q
+ * May be used under the terms of the GNU Lesser General Public License (LGPL)
+ */
+
+#undef A0
+#define A0 (NN) /* Special reserved value encoding zero in index form */
+
+void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
+{
+ int i, j;
+ data_t feedback;
+
+ memset(parity,0,NROOTS*sizeof(data_t));
+
+ for(i=0;i<NN-NROOTS-PAD;i++){
+ feedback = INDEX_OF[data[i] ^ parity[0]];
+ if(feedback != A0){ /* feedback term is non-zero */
+#ifdef UNNORMALIZED
+ /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
+ * always be for the polynomials constructed by init_rs()
+ */
+ feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
+#endif
+ for(j=1;j<NROOTS;j++)
+ parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
+ }
+ /* Shift */
+ memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
+ if(feedback != A0)
+ parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
+ else
+ parity[NROOTS-1] = 0;
+ }
+}
diff --git a/ap/app/qrencode/rscode.h b/ap/app/qrencode/rscode.h
new file mode 100644
index 0000000..5b976b2
--- /dev/null
+++ b/ap/app/qrencode/rscode.h
@@ -0,0 +1,41 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Reed solomon encoder. This code is taken from Phil Karn's libfec then
+ * editted and packed into a pair of .c and .h files.
+ *
+ * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
+ * (libfec is released under the GNU Lesser General Public License.)
+ *
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __RSCODE_H__
+#define __RSCODE_H__
+
+/*
+ * General purpose RS codec, 8-bit symbols.
+ */
+
+typedef struct _RS RS;
+
+extern RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad);
+extern void encode_rs_char(RS *rs, const unsigned char *data, unsigned char *parity);
+extern void free_rs_char(RS *rs);
+extern void free_rs_cache(void);
+
+#endif /* __RSCODE_H__ */
diff --git a/ap/app/qrencode/split.c b/ap/app/qrencode/split.c
new file mode 100644
index 0000000..a2cb0e0
--- /dev/null
+++ b/ap/app/qrencode/split.c
@@ -0,0 +1,326 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Input data splitter.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * The following data / specifications are taken from
+ * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
+ * or
+ * "Automatic identification and data capture techniques --
+ * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "qrencode.h"
+#include "qrinput.h"
+#include "qrspec.h"
+#include "split.h"
+
+#define isdigit(__c__) ((unsigned char)((signed char)(__c__) - '0') < 10)
+#define isalnum(__c__) (QRinput_lookAnTable(__c__) >= 0)
+
+#if !HAVE_STRDUP
+#undef strdup
+char *strdup(const char *s)
+{
+ size_t len = strlen(s) + 1;
+ void *new = malloc(len);
+ if(new == NULL) return NULL;
+ return (char *)memcpy(new, s, len);
+}
+#endif
+
+static QRencodeMode Split_identifyMode(const char *string, QRencodeMode hint)
+{
+ unsigned char c, d;
+ unsigned int word;
+
+ c = string[0];
+
+ if(c == '\0') return QR_MODE_NUL;
+ if(isdigit(c)) {
+ return QR_MODE_NUM;
+ } else if(isalnum(c)) {
+ return QR_MODE_AN;
+ } else if(hint == QR_MODE_KANJI) {
+ d = string[1];
+ if(d != '\0') {
+ word = ((unsigned int)c << 8) | d;
+ if((word >= 0x8140 && word <= 0x9ffc) || (word >= 0xe040 && word <= 0xebbf)) {
+ return QR_MODE_KANJI;
+ }
+ }
+ }
+
+ return QR_MODE_8;
+}
+
+static int Split_eatNum(const char *string, QRinput *input, QRencodeMode hint);
+static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint);
+static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint);
+static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint);
+
+static int Split_eatNum(const char *string, QRinput *input,QRencodeMode hint)
+{
+ const char *p;
+ int ret;
+ int run;
+ int dif;
+ int ln;
+ QRencodeMode mode;
+
+ ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
+
+ p = string;
+ while(isdigit(*p)) {
+ p++;
+ }
+ run = p - string;
+ mode = Split_identifyMode(p, hint);
+ if(mode == QR_MODE_8) {
+ dif = QRinput_estimateBitsModeNum(run) + 4 + ln
+ + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
+ - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
+ if(dif > 0) {
+ return Split_eat8(string, input, hint);
+ }
+ }
+ if(mode == QR_MODE_AN) {
+ dif = QRinput_estimateBitsModeNum(run) + 4 + ln
+ + QRinput_estimateBitsModeAn(1) /* + 4 + la */
+ - QRinput_estimateBitsModeAn(run + 1) /* - 4 - la */;
+ if(dif > 0) {
+ return Split_eatAn(string, input, hint);
+ }
+ }
+
+ ret = QRinput_append(input, QR_MODE_NUM, run, (unsigned char *)string);
+ if(ret < 0) return -1;
+
+ return run;
+}
+
+static int Split_eatAn(const char *string, QRinput *input, QRencodeMode hint)
+{
+ const char *p, *q;
+ int ret;
+ int run;
+ int dif;
+ int la, ln;
+
+ la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
+ ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
+
+ p = string;
+ while(isalnum(*p)) {
+ if(isdigit(*p)) {
+ q = p;
+ while(isdigit(*q)) {
+ q++;
+ }
+ dif = QRinput_estimateBitsModeAn(p - string) /* + 4 + la */
+ + QRinput_estimateBitsModeNum(q - p) + 4 + ln
+ + (isalnum(*q)?(4 + ln):0)
+ - QRinput_estimateBitsModeAn(q - string) /* - 4 - la */;
+ if(dif < 0) {
+ break;
+ } else {
+ p = q;
+ }
+ } else {
+ p++;
+ }
+ }
+
+ run = p - string;
+
+ if(*p && !isalnum(*p)) {
+ dif = QRinput_estimateBitsModeAn(run) + 4 + la
+ + QRinput_estimateBitsMode8(1) /* + 4 + l8 */
+ - QRinput_estimateBitsMode8(run + 1) /* - 4 - l8 */;
+ if(dif > 0) {
+ return Split_eat8(string, input, hint);
+ }
+ }
+
+ ret = QRinput_append(input, QR_MODE_AN, run, (unsigned char *)string);
+ if(ret < 0) return -1;
+
+ return run;
+}
+
+static int Split_eatKanji(const char *string, QRinput *input, QRencodeMode hint)
+{
+ const char *p;
+ int ret;
+ int run;
+
+ p = string;
+ while(Split_identifyMode(p, hint) == QR_MODE_KANJI) {
+ p += 2;
+ }
+ run = p - string;
+ ret = QRinput_append(input, QR_MODE_KANJI, run, (unsigned char *)string);
+ if(ret < 0) return -1;
+
+ return run;
+}
+
+static int Split_eat8(const char *string, QRinput *input, QRencodeMode hint)
+{
+ const char *p, *q;
+ QRencodeMode mode;
+ int ret;
+ int run;
+ int dif;
+ int la, ln, l8;
+ int swcost;
+
+ la = QRspec_lengthIndicator(QR_MODE_AN, input->version);
+ ln = QRspec_lengthIndicator(QR_MODE_NUM, input->version);
+ l8 = QRspec_lengthIndicator(QR_MODE_8, input->version);
+
+ p = string + 1;
+ while(*p != '\0') {
+ mode = Split_identifyMode(p, hint);
+ if(mode == QR_MODE_KANJI) {
+ break;
+ }
+ if(mode == QR_MODE_NUM) {
+ q = p;
+ while(isdigit(*q)) {
+ q++;
+ }
+ if(Split_identifyMode(q, hint) == QR_MODE_8) {
+ swcost = 4 + l8;
+ } else {
+ swcost = 0;
+ }
+ dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
+ + QRinput_estimateBitsModeNum(q - p) + 4 + ln
+ + swcost
+ - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
+ if(dif < 0) {
+ break;
+ } else {
+ p = q;
+ }
+ } else if(mode == QR_MODE_AN) {
+ q = p;
+ while(isalnum(*q)) {
+ q++;
+ }
+ if(Split_identifyMode(q, hint) == QR_MODE_8) {
+ swcost = 4 + l8;
+ } else {
+ swcost = 0;
+ }
+ dif = QRinput_estimateBitsMode8(p - string) /* + 4 + l8 */
+ + QRinput_estimateBitsModeAn(q - p) + 4 + la
+ + swcost
+ - QRinput_estimateBitsMode8(q - string) /* - 4 - l8 */;
+ if(dif < 0) {
+ break;
+ } else {
+ p = q;
+ }
+ } else {
+ p++;
+ }
+ }
+
+ run = p - string;
+ ret = QRinput_append(input, QR_MODE_8, run, (unsigned char *)string);
+ if(ret < 0) return -1;
+
+ return run;
+}
+
+static int Split_splitString(const char *string, QRinput *input,
+ QRencodeMode hint)
+{
+ int length;
+ QRencodeMode mode;
+
+ if(*string == '\0') return 0;
+
+ mode = Split_identifyMode(string, hint);
+ if(mode == QR_MODE_NUM) {
+ length = Split_eatNum(string, input, hint);
+ } else if(mode == QR_MODE_AN) {
+ length = Split_eatAn(string, input, hint);
+ } else if(mode == QR_MODE_KANJI && hint == QR_MODE_KANJI) {
+ length = Split_eatKanji(string, input, hint);
+ } else {
+ length = Split_eat8(string, input, hint);
+ }
+ if(length == 0) return 0;
+ if(length < 0) return -1;
+ return Split_splitString(&string[length], input, hint);
+}
+
+static char *dupAndToUpper(const char *str, QRencodeMode hint)
+{
+ char *newstr, *p;
+ QRencodeMode mode;
+
+ newstr = strdup(str);
+ if(newstr == NULL) return NULL;
+
+ p = newstr;
+ while(*p != '\0') {
+ mode = Split_identifyMode(p, hint);
+ if(mode == QR_MODE_KANJI) {
+ p += 2;
+ } else {
+ if (*p >= 'a' && *p <= 'z') {
+ *p = (char)((int)*p - 32);
+ }
+ p++;
+ }
+ }
+
+ return newstr;
+}
+
+int Split_splitStringToQRinput(const char *string, QRinput *input,
+ QRencodeMode hint, int casesensitive)
+{
+ char *newstr;
+ int ret;
+
+ if(string == NULL || *string == '\0') {
+ errno = EINVAL;
+ return -1;
+ }
+ if(!casesensitive) {
+ newstr = dupAndToUpper(string, hint);
+ if(newstr == NULL) return -1;
+ ret = Split_splitString(newstr, input, hint);
+ free(newstr);
+ } else {
+ ret = Split_splitString(string, input, hint);
+ }
+
+ return ret;
+}
diff --git a/ap/app/qrencode/split.h b/ap/app/qrencode/split.h
new file mode 100644
index 0000000..b2cdbe5
--- /dev/null
+++ b/ap/app/qrencode/split.h
@@ -0,0 +1,47 @@
+/*
+ * qrencode - QR Code encoder
+ *
+ * Input data splitter.
+ * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org>
+ *
+ * The following data / specifications are taken from
+ * "Two dimensional symbol -- QR-code -- Basic Specification" (JIS X0510:2004)
+ * or
+ * "Automatic identification and data capture techniques --
+ * QR Code 2005 bar code symbology specification" (ISO/IEC 18004:2006)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef __SPLIT_H__
+#define __SPLIT_H__
+
+#include "qrencode.h"
+
+/**
+ * Split the input string (null terminated) into QRinput.
+ * @param string input string
+ * @param hint give QR_MODE_KANJI if the input string contains Kanji character encoded in Shift-JIS. If not, give QR_MODE_8.
+ * @param casesensitive 0 for case-insensitive encoding (all alphabet characters are replaced to UPPER-CASE CHARACTERS.
+ * @retval 0 success.
+ * @retval -1 an error occurred. errno is set to indicate the error. See
+ * Exceptions for the details.
+ * @throw EINVAL invalid input object.
+ * @throw ENOMEM unable to allocate memory for input objects.
+ */
+extern int Split_splitStringToQRinput(const char *string, QRinput *input,
+ QRencodeMode hint, int casesensitive);
+
+#endif /* __SPLIT_H__ */