Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / QRCode / repo / c / qrcodegen-worker.c
1 /* 
2  * QR Code generator test worker (C)
3  * 
4  * This program reads data and encoding parameters from standard input and writes
5  * QR Code bitmaps to standard output. The I/O format is one integer per line.
6  * Run with no command line arguments. The program is intended for automated
7  * batch testing of end-to-end functionality of this QR Code generator library.
8  * 
9  * Copyright (c) Project Nayuki. (MIT License)
10  * https://www.nayuki.io/page/qr-code-generator-library
11  * 
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
13  * this software and associated documentation files (the "Software"), to deal in
14  * the Software without restriction, including without limitation the rights to
15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16  * the Software, and to permit persons to whom the Software is furnished to do so,
17  * subject to the following conditions:
18  * - The above copyright notice and this permission notice shall be included in
19  *   all copies or substantial portions of the Software.
20  * - The Software is provided "as is", without warranty of any kind, express or
21  *   implied, including but not limited to the warranties of merchantability,
22  *   fitness for a particular purpose and noninfringement. In no event shall the
23  *   authors or copyright holders be liable for any claim, damages or other
24  *   liability, whether in an action of contract, tort or otherwise, arising from,
25  *   out of or in connection with the Software or the use or other dealings in the
26  *   Software.
27  */
28
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "qrcodegen.h"
36
37
38 int main(void) {
39         while (true) {
40                 
41                 // Read data length or exit
42                 size_t length;
43                 {
44                         int temp;
45                         if (scanf("%d", &temp) != 1)
46                                 return EXIT_FAILURE;
47                         if (temp == -1)
48                                 break;
49                         length = (size_t)temp;
50                 }
51                 
52                 // Read data bytes
53                 bool isAscii = true;
54                 uint8_t *data = malloc(length * sizeof(uint8_t));
55                 if (data == NULL) {
56                         perror("malloc");
57                         return EXIT_FAILURE;
58                 }
59                 for (size_t i = 0; i < length; i++) {
60                         int b;
61                         if (scanf("%d", &b) != 1)
62                                 return EXIT_FAILURE;
63                         data[i] = (uint8_t)b;
64                         isAscii &= 0 < b && b < 128;
65                 }
66                 
67                 // Read encoding parameters
68                 int errCorLvl, minVersion, maxVersion, mask, boostEcl;
69                 if (scanf("%d %d %d %d %d", &errCorLvl, &minVersion, &maxVersion, &mask, &boostEcl) != 5)
70                         return EXIT_FAILURE;
71                 
72                 // Allocate memory for QR Code
73                 size_t bufferLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
74                 uint8_t *qrcode     = malloc(bufferLen * sizeof(uint8_t));
75                 uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t));
76                 if (qrcode == NULL || tempBuffer == NULL) {
77                         perror("malloc");
78                         return EXIT_FAILURE;
79                 }
80                 
81                 // Try to make QR Code symbol
82                 bool ok;
83                 if (isAscii) {
84                         char *text = malloc((length + 1) * sizeof(char));
85                         if (text == NULL) {
86                                 perror("malloc");
87                                 return EXIT_FAILURE;
88                         }
89                         for (size_t i = 0; i < length; i++)
90                                 text[i] = (char)data[i];
91                         text[length] = '\0';
92                         ok = qrcodegen_encodeText(text, tempBuffer, qrcode, (enum qrcodegen_Ecc)errCorLvl,
93                                 minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
94                         free(text);
95                 } else if (length <= bufferLen) {
96                         memcpy(tempBuffer, data, length * sizeof(data[0]));
97                         ok = qrcodegen_encodeBinary(tempBuffer, length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
98                                 minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
99                 } else
100                         ok = false;
101                 free(data);
102                 free(tempBuffer);
103                 
104                 if (ok) {
105                         // Print grid of modules
106                         int size = qrcodegen_getSize(qrcode);
107                         printf("%d\n", (size - 17) / 4);
108                         for (int y = 0; y < size; y++) {
109                                 for (int x = 0; x < size; x++)
110                                         printf("%d\n", qrcodegen_getModule(qrcode, x, y) ? 1 : 0);
111                         }
112                 } else
113                         printf("-1\n");
114                 free(qrcode);
115                 fflush(stdout);
116         }
117         return EXIT_SUCCESS;
118 }