Clarified C qrcodegen-worker so that two internal variables use size_t instead of int.

This commit is contained in:
Project Nayuki 2019-07-22 17:30:40 +00:00
parent ae0ff71ff1
commit a492346364
1 changed files with 18 additions and 14 deletions

View File

@ -39,20 +39,24 @@ int main(void) {
while (true) {
// Read data length or exit
int length;
if (scanf("%d", &length) != 1)
return EXIT_FAILURE;
if (length == -1)
break;
size_t length;
{
int temp;
if (scanf("%d", &temp) != 1)
return EXIT_FAILURE;
if (temp == -1)
break;
length = (size_t)temp;
}
// Read data bytes
bool isAscii = true;
uint8_t *data = malloc((size_t)length * sizeof(uint8_t));
uint8_t *data = malloc(length * sizeof(uint8_t));
if (data == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
for (int i = 0; i < length; i++) {
for (size_t i = 0; i < length; i++) {
int b;
if (scanf("%d", &b) != 1)
return EXIT_FAILURE;
@ -66,9 +70,9 @@ int main(void) {
return EXIT_FAILURE;
// Allocate memory for QR Code
int bufferLen = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
uint8_t *qrcode = malloc((size_t)bufferLen * sizeof(uint8_t));
uint8_t *tempBuffer = malloc((size_t)bufferLen * sizeof(uint8_t));
size_t bufferLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
uint8_t *qrcode = malloc(bufferLen * sizeof(uint8_t));
uint8_t *tempBuffer = malloc(bufferLen * sizeof(uint8_t));
if (qrcode == NULL || tempBuffer == NULL) {
perror("malloc");
return EXIT_FAILURE;
@ -77,20 +81,20 @@ int main(void) {
// Try to make QR Code symbol
bool ok;
if (isAscii) {
char *text = malloc((size_t)(length + 1) * sizeof(char));
char *text = malloc((length + 1) * sizeof(char));
if (text == NULL) {
perror("malloc");
return EXIT_FAILURE;
}
for (int i = 0; i < length; i++)
for (size_t i = 0; i < length; i++)
text[i] = (char)data[i];
text[length] = '\0';
ok = qrcodegen_encodeText(text, tempBuffer, qrcode, (enum qrcodegen_Ecc)errCorLvl,
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
free(text);
} else if (length <= bufferLen) {
memcpy(tempBuffer, data, (size_t)length * sizeof(data[0]));
ok = qrcodegen_encodeBinary(tempBuffer, (size_t)length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
memcpy(tempBuffer, data, length * sizeof(data[0]));
ok = qrcodegen_encodeBinary(tempBuffer, length, qrcode, (enum qrcodegen_Ecc)errCorLvl,
minVersion, maxVersion, (enum qrcodegen_Mask)mask, boostEcl == 1);
} else
ok = false;