mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-26 13:03:28 +01:00
Fix a few resource leaks
This commit is contained in:
parent
15846d2b50
commit
abb39ea6b5
@ -105,7 +105,7 @@ int ID_to_QRcode_txt(const char *tox_id, const char *outfile)
|
|||||||
int ID_to_QRcode_png(const char *tox_id, const char *outfile)
|
int ID_to_QRcode_png(const char *tox_id, const char *outfile)
|
||||||
{
|
{
|
||||||
static FILE *fp;
|
static FILE *fp;
|
||||||
unsigned char *row, *p;
|
unsigned char *p;
|
||||||
unsigned char black[4] = {0, 0, 0, 255};
|
unsigned char black[4] = {0, 0, 0, 255};
|
||||||
size_t x, y, xx, yy, real_width;
|
size_t x, y, xx, yy, real_width;
|
||||||
size_t margin = BORDER_LEN;
|
size_t margin = BORDER_LEN;
|
||||||
@ -116,31 +116,40 @@ int ID_to_QRcode_png(const char *tox_id, const char *outfile)
|
|||||||
|
|
||||||
fp = fopen(outfile, "wb");
|
fp = fopen(outfile, "wb");
|
||||||
|
|
||||||
if (fp == NULL)
|
if (fp == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
QRcode *qr_obj = QRcode_encodeString(tox_id, 0, QR_ECLEVEL_L, QR_MODE_8, 0);
|
||||||
|
|
||||||
if (qr_obj == NULL)
|
if (qr_obj == NULL) {
|
||||||
|
fclose(fp);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
real_width = (qr_obj->width + margin * 2) * size;
|
real_width = (qr_obj->width + margin * 2) * size;
|
||||||
row = malloc(real_width * 4);
|
size_t row_size = real_width * 4;
|
||||||
|
unsigned char row[row_size];
|
||||||
if (row == NULL)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||||
|
|
||||||
if (png_ptr == NULL)
|
if (png_ptr == NULL) {
|
||||||
|
fclose(fp);
|
||||||
|
QRcode_free(qr_obj);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
info_ptr = png_create_info_struct(png_ptr);
|
info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
|
||||||
if (info_ptr == NULL)
|
if (info_ptr == NULL) {
|
||||||
|
fclose(fp);
|
||||||
|
QRcode_free(qr_obj);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||||
|
fclose(fp);
|
||||||
|
QRcode_free(qr_obj);
|
||||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -154,40 +163,44 @@ int ID_to_QRcode_png(const char *tox_id, const char *outfile)
|
|||||||
png_write_info(png_ptr, info_ptr);
|
png_write_info(png_ptr, info_ptr);
|
||||||
|
|
||||||
/* top margin */
|
/* top margin */
|
||||||
memset(row, 0xff, real_width * 4);
|
memset(row, 0xff, row_size);
|
||||||
|
|
||||||
for (y = 0; y < margin * size; y++)
|
for (y = 0; y < margin * size; y++) {
|
||||||
png_write_row(png_ptr, row);
|
png_write_row(png_ptr, row);
|
||||||
|
}
|
||||||
|
|
||||||
/* data */
|
/* data */
|
||||||
p = qr_obj->data;
|
p = qr_obj->data;
|
||||||
|
|
||||||
for (y = 0; y < qr_obj->width; y++) {
|
for (y = 0; y < qr_obj->width; y++) {
|
||||||
memset(row, 0xff, real_width * 4);
|
memset(row, 0xff, row_size);
|
||||||
|
|
||||||
for (x = 0; x < qr_obj->width; x++) {
|
for (x = 0; x < qr_obj->width; x++) {
|
||||||
for (xx = 0; xx < size; xx++)
|
for (xx = 0; xx < size; xx++) {
|
||||||
if (*p & 1)
|
if (*p & 1) {
|
||||||
memcpy(&row[((margin + x) * size + xx) * 4], black, 4);
|
memcpy(&row[((margin + x) * size + xx) * 4], black, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (yy = 0; yy < size; yy++)
|
for (yy = 0; yy < size; yy++) {
|
||||||
png_write_row(png_ptr, row);
|
png_write_row(png_ptr, row);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bottom margin */
|
/* bottom margin */
|
||||||
memset(row, 0xff, real_width * 4);
|
memset(row, 0xff, row_size);
|
||||||
|
|
||||||
for (y = 0; y < margin * size; y++)
|
for (y = 0; y < margin * size; y++) {
|
||||||
png_write_row(png_ptr, row);
|
png_write_row(png_ptr, row);
|
||||||
|
}
|
||||||
|
|
||||||
png_write_end(png_ptr, info_ptr);
|
png_write_end(png_ptr, info_ptr);
|
||||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
free(row);
|
|
||||||
QRcode_free(qr_obj);
|
QRcode_free(qr_obj);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user