summaryrefslogtreecommitdiffstats
path: root/cqrlogo.c
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2014-02-25 12:12:30 +0100
committerGravatar Christian Hesse <mail@eworm.de>2014-02-25 12:12:30 +0100
commitf5f01f3ff143edd87b7e36f1b3540cbc5d42c51f (patch)
tree5c2b6f5dbbb342ccc5ba770fd4bb4f73c151c246 /cqrlogo.c
parent0f32341ead4f1534270a82c4372d84a1aaa9b0cd (diff)
downloadcqrlogo-f5f01f3ff143edd87b7e36f1b3540cbc5d42c51f.tar.gz
cqrlogo-f5f01f3ff143edd87b7e36f1b3540cbc5d42c51f.tar.zst
build a shared library and rework parts of the code
Diffstat (limited to 'cqrlogo.c')
-rw-r--r--cqrlogo.c324
1 files changed, 20 insertions, 304 deletions
diff --git a/cqrlogo.c b/cqrlogo.c
index 1681531..4b87371 100644
--- a/cqrlogo.c
+++ b/cqrlogo.c
@@ -5,285 +5,22 @@
* of the GNU General Public License, incorporated herein by reference.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-#include <regex.h>
-
-#include <iniparser.h>
-#include <png.h>
-#include <zlib.h>
-#include <qrencode.h>
-
-#if HAVE_FCGI
-#include <fcgi_stdio.h>
-#endif
-
#include "config.h"
#include "version.h"
-/* define structs and functions */
#include "cqrlogo.h"
-#define URLPATTERN "^[hH][tT][tT][pP][sS]\\?://%s/"
-#define TEXTSTOLEN "This QR Code has been stolen from http%s://%s/!"
-
-#if defined PNG_TEXT_SUPPORTED && PNG_ENABLE_TEXT
-/*** add_png_text ***/
-png_text * add_png_text(png_text *pngtext, unsigned int *textcount, char *key, char *text) {
- pngtext = realloc(pngtext, ((*textcount) + 1) * sizeof(png_text));
-
- pngtext[*textcount].compression = PNG_TEXT_COMPRESSION_zTXt;
- pngtext[*textcount].key = key;
- pngtext[*textcount].text = text;
-
- (*textcount)++;
- return pngtext;
-}
-#endif
-
-#if HAVE_FCGI
-/*** png_write_stdout ***/
-void png_write_stdout(png_structp png_ptr, png_bytep data, png_size_t length) {
- if (length != fwrite(data, 1, length, png_get_io_ptr(png_ptr)))
- png_error(png_ptr, "Write Error");
-}
-
-/*** png_flush_stdout ***/
-void png_flush_stdout(png_structp png_ptr) {
- fflush(stdout);
-}
-#endif
-
-/*** generate_png ***/
-int generate_png (struct bitmap_t *bitmap, const char *uri) {
- png_structp png_ptr = NULL;
- png_infop info_ptr = NULL;
- png_byte ** row_pointers = NULL;
- unsigned int x, y;
- uint8_t bit, byte;
-
- if ((png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL)
- return 1;
-
- if ((info_ptr = png_create_info_struct (png_ptr)) == NULL ||
- (setjmp (png_jmpbuf (png_ptr)))) {
- png_destroy_write_struct (&png_ptr, &info_ptr);
- return 1;
- }
-
- png_set_IHDR (png_ptr, info_ptr, bitmap->width, bitmap->height, 1 /* depth */,
- PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-
- /* use best compression */
- png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
-
- /* use compression strategy filtered
- * this way pngcrush can not optimize any more */
- png_set_compression_strategy(png_ptr, Z_FILTERED);
-
-#if defined PNG_TEXT_SUPPORTED && PNG_ENABLE_TEXT
- unsigned int textcount = 0;
- png_text *pngtext = NULL;
-
- pngtext = add_png_text(pngtext, &textcount, "comment", "QR-Code created by cqrlogo - https://github.com/eworm-de/cqrlogo");
-# if PNG_ENABLE_TEXT_REFERER
- pngtext = add_png_text(pngtext, &textcount, "referer", (char *)uri);
-# endif
-
-# if PNG_ENABLE_TEXT_VERSIONS
-#if HAVE_FCGI
-# define VERSIONSTR VERSION " (FastCGI) (" __DATE__ ", " __TIME__ ")"
-#else
-# define VERSIONSTR VERSION " (CGI) (" __DATE__ ", " __TIME__ ")"
-#endif
-# define LIBSSTR "libqrencode %s, libpng %s, zlib %s"
- char *libsstr, *qrver = QRcode_APIVersionString();
-
- libsstr = malloc(sizeof(LIBSSTR) + strlen(qrver) + strlen(png_libpng_ver) + strlen(zlib_version));
- sprintf(libsstr, LIBSSTR, qrver, png_libpng_ver, zlib_version);
-
- pngtext = add_png_text(pngtext, &textcount, "version", VERSIONSTR);
- pngtext = add_png_text(pngtext, &textcount, "libs", libsstr);
-# endif
-
- png_set_text(png_ptr, info_ptr, pngtext, textcount);
- png_free (png_ptr, pngtext);
-# if PNG_ENABLE_TEXT_VERSIONS
- free(libsstr);
-# endif
-#endif
-
- row_pointers = png_malloc (png_ptr, bitmap->height * sizeof (png_byte *));
- for (y = 0; y < bitmap->height; ++y) {
- /* we need to round up, need a complete byte for less than eight bits */
- row_pointers[y] = png_malloc (png_ptr, (sizeof(uint8_t) * bitmap->width + 7) / 8);
- for (x = 0; x < bitmap->width; ++x) {
- /* bit are written in reverse order! */
- bit = 7 - (x % 8);
- byte = x / 8;
- if (bitmap->pixel[y * bitmap->width + x])
- row_pointers[y][byte] |= 1 << (bit);
- else
- row_pointers[y][byte] &= ~(1 << (bit));
- }
- }
-
-#if HAVE_FCGI
- /* with fastcgi we can not just open stdout for writing...
- * define a write function instead */
- png_set_write_fn(png_ptr, (png_voidp)stdout, png_write_stdout, png_flush_stdout);
-#else
- png_init_io (png_ptr, stdout);
-#endif
-
- png_set_rows (png_ptr, info_ptr, row_pointers);
- png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
-
- for (y = 0; y < bitmap->height; ++y)
- png_free (png_ptr, row_pointers[y]);
- png_free (png_ptr, row_pointers);
- png_destroy_write_struct(&png_ptr, &info_ptr);
-
- return 0;
-}
-
-/*** bitmap_new ***/
-struct bitmap_t * bitmap_new(int width, int height) {
- struct bitmap_t *bitmap;
-
- if ((bitmap = malloc(sizeof(struct bitmap_t))) == NULL)
- return NULL;
-
- bitmap->width = width;
- bitmap->height = height;
- if ((bitmap->pixel = malloc(width * height * sizeof(uint8_t))) == NULL) {
- free(bitmap);
- return NULL;
- }
-
- /* initialize with white */
- memset(bitmap->pixel, 0xff, width * height);
-
- return bitmap;
-}
-
-/*** bitmap_free ***/
-void bitmap_free(struct bitmap_t * bitmap) {
- free(bitmap->pixel);
- free(bitmap);
-}
-
-/*** encode_qrcode ***/
-struct bitmap_t * encode_qrcode (const char *text, unsigned int scale,
- unsigned int border, unsigned int level) {
- QRcode *qrcode;
- struct bitmap_t *bitmap, *scaled;
- int i, j, k, l;
- unsigned char *data;
-
- qrcode = QRcode_encodeString8bit(text, 0, level);
-
- /* this happens if the string is too long
- * possibly we have an URL (referer) that is too long, so the code
- * automatically falls back to http_server (see main()) */
- if (qrcode == NULL)
- return NULL;
-
- data = qrcode->data;
-
- /* wirte QR code to bitmap */
- if ((bitmap = bitmap_new(qrcode->width + border * 2, qrcode->width + border * 2)) == NULL)
- return NULL;
- for (i = border; i < qrcode->width + border; i++)
- for (j = border; j < qrcode->width + border; j++) {
- bitmap->pixel[i * (qrcode->width + border * 2) + j] = !(*data & 0x1) * 0xff;
- data++;
- }
-
- QRcode_free(qrcode);
-
- if (scale == 1)
- return bitmap;
-
- /* scale bitmap */
- if ((scaled = bitmap_new(bitmap->width * scale, bitmap->height * scale)) == NULL)
- return NULL;
- for (i = 0; i < bitmap->height; i++)
- for (j = 0; j < bitmap->width; j++)
- for (k = 0; k < scale; k++)
- for (l = 0; l < scale; l++)
- scaled->pixel[i * bitmap->width * scale * scale + k * bitmap->width * scale + j * scale + l] =
- bitmap->pixel[i * bitmap->width + j];
-
-
- bitmap_free(bitmap);
-
- return scaled;
-}
-
-/*** get_query_value ***/
-unsigned int get_query_value(const char *query_string, const char *pattern,
- unsigned int value, unsigned int min, unsigned int max) {
- char *match = NULL, *newpattern = NULL;
- unsigned int length;
- int tmp = -1;
-
- newpattern = strdup(pattern);
-
- length = strlen(newpattern);
- /* length is without null termination, allocacte 4 bytes so we
- * have "=", "%u" and null termination */
- newpattern = realloc(newpattern, length + 4);
- sprintf(newpattern + length, "=");
-
- if ((match = strstr(query_string, newpattern)) != NULL) {
- sprintf(newpattern + length + 1, "%%u");
-
- if ((sscanf(match, newpattern, &tmp)) > 0)
- if (tmp >= min && tmp <= max)
- value = tmp;
- }
-
- free(newpattern);
-
- return value;
-}
-
-/*** get_ini_value ***/
-unsigned int get_ini_value(dictionary * ini, uint8_t type, const char * section, const char * parameter,
- unsigned int value, unsigned int min, unsigned int max) {
- char * key;
- unsigned int tmp;
-
- key = malloc(strlen(section) + strlen(parameter) + 2);
- sprintf(key, "%s:%s", section, parameter);
-
- if (type)
- tmp = iniparser_getint(ini, key, value);
- else
- tmp = iniparser_getboolean(ini, key, value);
-
- if (tmp >= min && tmp <= max)
- value = tmp;
-
- free(key);
-
- return value;
-}
-
/*** main ***/
int main(int argc, char **argv) {
- dictionary * ini;
const char * http_referer, * server_name, * query_string, * uri;
char * uri_server_name, * uri_png, * pattern, * stolen;
regex_t preg;
regmatch_t pmatch[1];
- uint8_t https, overwrite;
+ uint8_t https;
+ struct png_t * png;
struct bitmap_t * bitmap;
- unsigned int scale, border, level;
+ struct cqrconf_t cqrconf;
#if HAVE_FCGI
/* loop for requests */
@@ -296,11 +33,12 @@ int main(int argc, char **argv) {
stolen = NULL;
https = 0;
- overwrite = ALLOW_OVERWRITE;
- scale = QRCODE_SCALE;
- border = QRCODE_BORDER;
- level = QRCODE_LEVEL;
+ /* these default values are defined in config.h */
+ cqrconf.scale = QRCODE_SCALE;
+ cqrconf.border = QRCODE_BORDER;
+ cqrconf.level = QRCODE_LEVEL;
+ cqrconf.overwrite = ALLOW_OVERWRITE;
/* check if we have environment variables from CGI */
if ((server_name = getenv("SERVER_NAME")) == NULL) {
@@ -344,42 +82,14 @@ int main(int argc, char **argv) {
uri = uri_server_name;
}
- /* parse config file */
- if ((ini = iniparser_load(CONFIGFILE)) == NULL) {
- fprintf(stderr, "cannot parse file " CONFIGFILE ", continue anyway\n");
- /* continue anyway, there is nothing essential in the config file */
- } else {
- scale = get_ini_value(ini, 1, "general", "scale", scale, 1, QRCODE_MAX_SCALE);
- border = get_ini_value(ini, 1, "general", "border", border, 0, QRCODE_MAX_BORDER);
- level = get_ini_value(ini, 1, "general", "level", level, QR_ECLEVEL_L, QR_ECLEVEL_H);
- overwrite = get_ini_value(ini, 0, "general", "allow overwrite", overwrite, 0, 1);
-
- scale = get_ini_value(ini, 1, server_name, "scale", scale, 1, QRCODE_MAX_SCALE);
- border = get_ini_value(ini, 1, server_name, "border", border, 0, QRCODE_MAX_BORDER);
- level = get_ini_value(ini, 1, server_name, "level", level, QR_ECLEVEL_L, QR_ECLEVEL_H);
- overwrite = get_ini_value(ini, 0, server_name, "allow overwrite", overwrite, 0, 1);
-
- /* done reading config file, free */
- iniparser_freedict(ini);
- }
-
- /* get query string and read settings */
- if (overwrite && (query_string = getenv("QUERY_STRING")) != NULL) {
- /* do we have a special scale? */
- scale = get_query_value(query_string, "scale", scale, 1, QRCODE_MAX_SCALE);
-
- /* width of the border? */
- border = get_query_value(query_string, "border", border, 0, QRCODE_MAX_BORDER);
-
- /* error correction level? */
- level = get_query_value(query_string, "level", level, QR_ECLEVEL_L, QR_ECLEVEL_H);
- }
+ cqrconf_file(server_name, &cqrconf);
+ cqrconf_string(getenv("QUERY_STRING"), &cqrconf);
/* encode the QR-Code */
- if ((bitmap = encode_qrcode(uri, scale, border, level)) == NULL) {
+ if ((bitmap = encode_qrcode(uri, cqrconf)) == NULL) {
/* uri too long? retry with uri from server name */
uri = uri_server_name;
- if ((bitmap = encode_qrcode(uri, scale, border, level)) == NULL) {
+ if ((bitmap = encode_qrcode(uri, cqrconf)) == NULL) {
fprintf(stderr, "Could not generate QR-Code.\n");
return EXIT_FAILURE;
}
@@ -395,12 +105,15 @@ int main(int argc, char **argv) {
uri = uri_png;
}
- /* print PNG data */
- if (generate_png(bitmap, uri)) {
+ /* generate PNG data */
+ if ((png = generate_png(bitmap, CQR_COMMENT|CQR_REFERER|CQR_VERSION|CQR_LIBVERSION, uri)) == NULL) {
fprintf(stderr, "Failed to generate PNG.\n");
return EXIT_FAILURE;
}
+ /* write PNG data to stdout */
+ fwrite(png->buffer, png->size, 1, stdout);
+
/* free memory we no longer need */
if (uri_server_name)
free(uri_server_name);
@@ -409,6 +122,9 @@ int main(int argc, char **argv) {
if (uri_png)
free(uri_png);
bitmap_free(bitmap);
+ if (png->size > 0)
+ free(png->buffer);
+ free(png);
#if HAVE_FCGI
/* end of loop */