2017-04-21 00:15:07 +00:00
|
|
|
/*
|
|
|
|
* QR Code generator test worker (C)
|
|
|
|
*
|
|
|
|
* This program reads data and encoding parameters from standard input and writes
|
|
|
|
* QR Code bitmaps to standard output. The I/O format is one integer per line.
|
|
|
|
* Run with no command line arguments. The program is intended for automated
|
|
|
|
* batch testing of end-to-end functionality of this QR Code generator library.
|
|
|
|
*
|
2017-04-24 20:41:54 +00:00
|
|
|
* Copyright (c) Project Nayuki. (MIT License)
|
2017-04-21 00:15:07 +00:00
|
|
|
* https://www.nayuki.io/page/qr-code-generator-library
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
* - The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
* - The Software is provided "as is", without warranty of any kind, express or
|
|
|
|
* implied, including but not limited to the warranties of merchantability,
|
|
|
|
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
|
|
* authors or copyright holders be liable for any claim, damages or other
|
|
|
|
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
|
|
* out of or in connection with the Software or the use or other dealings in the
|
|
|
|
* Software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "qrcodegen.h"
|
|
|
|
|
2017-07-04 06:17:08 +00:00
|
|
|
#ifndef __cplusplus
|
|
|
|
#define MALLOC(num, type) malloc((num) * sizeof(type))
|
|
|
|
#else
|
|
|
|
#define MALLOC(num, type) static_cast<type*>(malloc((num) * sizeof(type)))
|
|
|
|
#endif
|
|
|
|
|
2017-04-21 00:15:07 +00:00
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// Read data length or exit
|
|
|
|
int length;
|
2017-04-21 05:07:14 +00:00
|
|
|
if (scanf("%d", &length) != 1)
|
|
|
|
return EXIT_FAILURE;
|
2017-04-21 00:15:07 +00:00
|
|
|
if (length == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Read data bytes
|
|
|
|
bool isAscii = true;
|
2017-07-04 06:17:08 +00:00
|
|
|
uint8_t *data = MALLOC(length, uint8_t);
|
2017-04-21 00:15:07 +00:00
|
|
|
if (data == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
int b;
|
2017-04-21 05:07:14 +00:00
|
|
|
if (scanf("%d", &b) != 1)
|
|
|
|
return EXIT_FAILURE;
|
2017-04-21 00:15:07 +00:00
|
|
|
data[i] = (uint8_t)b;
|
|
|
|
isAscii &= 0 < b && b < 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read encoding parameters
|
|
|
|
int errCorLvl, minVersion, maxVersion, mask, boostEcl;
|
2017-04-21 05:07:14 +00:00
|
|
|
if (scanf("%d %d %d %d %d", &errCorLvl, &minVersion, &maxVersion, &mask, &boostEcl) != 5)
|
|
|
|
return EXIT_FAILURE;
|
2017-04-21 00:15:07 +00:00
|
|
|
|
|
|
|
// Allocate memory for QR Code
|
|
|
|
int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
|
2017-07-04 06:17:08 +00:00
|
|
|
uint8_t *qrcode = MALLOC(bufferLen, uint8_t);
|
|
|
|
uint8_t *tempBuffer = MALLOC(bufferLen, uint8_t);
|
2017-04-21 00:15:07 +00:00
|
|
|
if (qrcode == NULL || tempBuffer == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to make QR Code symbol
|
2017-04-25 06:15:11 +00:00
|
|
|
bool ok;
|
2017-04-21 00:15:07 +00:00
|
|
|
if (isAscii) {
|
2017-07-04 06:17:08 +00:00
|
|
|
char *text = MALLOC(length + 1, char);
|
2017-04-21 00:15:07 +00:00
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
text[i] = (char)data[i];
|
|
|
|
text[length] = '\0';
|
2017-04-25 06:15:11 +00:00
|
|
|
ok = qrcodegen_encodeText(text, tempBuffer, qrcode, (enum qrcodegen_Ecc)errCorLvl,
|
2017-04-21 00:15:07 +00:00
|
|
|
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
|
|
|
|
free(text);
|
|
|
|
} else if (length <= bufferLen) {
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
tempBuffer[i] = data[i];
|
2017-04-25 06:15:11 +00:00
|
|
|
ok = qrcodegen_encodeBinary(tempBuffer, (size_t)length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
|
2017-04-21 00:15:07 +00:00
|
|
|
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
|
|
|
|
} else
|
2017-04-25 06:15:11 +00:00
|
|
|
ok = false;
|
2017-04-21 00:15:07 +00:00
|
|
|
free(data);
|
|
|
|
free(tempBuffer);
|
|
|
|
|
|
|
|
// Print grid of modules
|
2017-04-25 06:15:11 +00:00
|
|
|
if (!ok)
|
2017-04-21 00:15:07 +00:00
|
|
|
printf("-1\n");
|
|
|
|
else {
|
2017-04-25 06:00:39 +00:00
|
|
|
int size = qrcodegen_getSize(qrcode);
|
2017-04-25 06:15:11 +00:00
|
|
|
printf("%d\n", (size - 17) / 4);
|
2017-04-21 00:15:07 +00:00
|
|
|
for (int y = 0; y < size; y++) {
|
|
|
|
for (int x = 0; x < size; x++)
|
2017-04-25 05:46:01 +00:00
|
|
|
printf("%d\n", qrcodegen_getModule(qrcode, x, y) ? 1 : 0);
|
2017-04-21 00:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(qrcode);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|