diff --git a/qrenc.c b/qrenc.c index c09c4ab6..a937d7d9 100644 --- a/qrenc.c +++ b/qrenc.c @@ -733,19 +733,26 @@ static int writeXPM(const QRcode *qrcode, const char *outfile) return 0; } -static void writeANSI_margin(FILE* fp, int realwidth, - char* buffer, const char* white, int white_s ) +static void writeANSI_margin(FILE* fp, size_t realwidth, + char* buffer, const char* white, size_t white_s) { int y; - strncpy(buffer, white, (size_t)white_s); - memset(buffer + white_s, ' ', (size_t)realwidth * 2); + 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++ ){ + for (y = 0; y < margin; y++) { fputs(buffer, fp); } } +#define COPY_AND_ADVANCE(BUF, STR) \ + do { \ + strcpy(BUF, STR); \ + BUF += strlen(STR); \ + } while (0) + + static int writeANSI(const QRcode *qrcode, const char *outfile) { FILE *fp; @@ -756,29 +763,26 @@ static int writeANSI(const QRcode *qrcode, const char *outfile) const char *white, *black; char *buffer; - int white_s, black_s, buffer_s; + char *pbuf; + size_t white_s, black_s, buffer_s; - if(image_type == ANSI256_TYPE){ + 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; + white_s = strlen(white); + black_s = strlen(black); fp = openFile(outfile); - realwidth = (qrcode->width + margin * 2) * size; - buffer_s = (realwidth * white_s) * 2; - buffer = (char *)malloc((size_t)buffer_s); - if(buffer == NULL) { + realwidth = qrcode->width + margin * 2; + buffer_s = (realwidth * (white_s > black_s ? white_s : black_s)) * 2; + buffer = malloc(buffer_s); + if (buffer == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); exit(EXIT_FAILURE); } @@ -788,36 +792,37 @@ static int writeANSI(const QRcode *qrcode, const char *outfile) /* data */ p = qrcode->data; - for(y = 0; y < qrcode->width; y++) { - row = (p+(y*qrcode->width)); + for (y = 0; y < qrcode->width; y++) { + row = p + (y * qrcode->width); - memset(buffer, 0, (size_t)buffer_s); - strncpy(buffer, white, (size_t)white_s); - for(x = 0; x < margin; x++ ){ - strncat(buffer, " ", 2); + memset(buffer, 0, buffer_s); + pbuf = buffer; + COPY_AND_ADVANCE(pbuf, white); + for (x = 0; x < margin; x++) { + COPY_AND_ADVANCE(pbuf, " "); } last = 0; - for(x = 0; x < qrcode->width; x++) { - if(*(row+x)&0x1) { - if( last != 1 ){ - strncat(buffer, black, (size_t)black_s); + for (x = 0; x < qrcode->width; x++) { + if (row[x] & 0x1) { + if (last != 1) { + COPY_AND_ADVANCE(pbuf, black); last = 1; } - } else if( last != 0 ){ - strncat(buffer, white, (size_t)white_s); + } else if(last != 0) { + COPY_AND_ADVANCE(pbuf, white); last = 0; } - strncat(buffer, " ", 2); + COPY_AND_ADVANCE(pbuf, " "); } - if( last != 0 ){ - strncat(buffer, white, (size_t)white_s); + if (last != 0) { + COPY_AND_ADVANCE(pbuf, white); } - for(x = 0; x < margin; x++ ){ - strncat(buffer, " ", 2); + for (x = 0; x < margin; x++) { + COPY_AND_ADVANCE(pbuf, " "); } - strncat(buffer, "\033[0m\n", 5); + COPY_AND_ADVANCE(pbuf, "\033[0m\n"); fputs(buffer, fp); } @@ -959,8 +964,6 @@ static int writeASCII(const QRcode *qrcode, const char *outfile, int invert) white = '#'; } - size = 1; - fp = openFile(outfile); realwidth = (qrcode->width + margin * 2) * 2;