Reduce memory leak and tidy code

This commit is contained in:
hooper114 2008-10-08 17:06:16 +00:00
parent 858f69ea55
commit 608009b2a5
3 changed files with 47 additions and 185 deletions

View File

@ -28,6 +28,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include "reedsol.h" #include "reedsol.h"
#include "common.h"
#include "dm200.h" #include "dm200.h"
static struct ecc200matrix_s { static struct ecc200matrix_s {
@ -740,27 +741,25 @@ static char *encmake(int l, unsigned char *s, int *lenp, char exact)
* Returns 0 on error (writes to stderr with details). * Returns 0 on error (writes to stderr with details).
*/ */
unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barcodelen, unsigned char *barcode, int *lenp, int *maxp, int *eccp) int iec16022ecc200(unsigned char *barcode, int barcodelen, struct zint_symbol *symbol)
{ {
unsigned char binary[3000]; // encoded raw data and ecc to place in barcode unsigned char binary[3000]; // encoded raw data and ecc to place in barcode
int W = 0, H = 0; int W = 0, H = 0;
char *encoding = 0; char *encoding = 0;
unsigned char *grid = 0; unsigned char *grid = 0;
int lend, *lenp;
struct ecc200matrix_s *matrix; struct ecc200matrix_s *matrix;
memset(binary, 0, sizeof(binary)); memset(binary, 0, sizeof(binary));
if (encodingptr)
encoding = *encodingptr; lend = 0;
if (Wptr) lenp = &lend;
W = *Wptr;
if (Hptr)
H = *Hptr;
// encoding // encoding
if (W) { // known size if (W) { // known size
for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++) ; for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++) ;
if (!matrix->W) { if (!matrix->W) {
fprintf(stderr, "Invalid size %dx%d\n", W, H); strcpy(symbol->errtxt, "Invalid size");
return 0; return ERROR_INVALID_OPTION;
} }
if (!encoding) { if (!encoding) {
int len; int len;
@ -769,12 +768,9 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
free(e); free(e);
e = encmake(barcodelen, barcode, &len, 0); e = encmake(barcodelen, barcode, &len, 0);
if (len > matrix->bytes) { if (len > matrix->bytes) {
fprintf(stderr, strcpy(symbol->errtxt, "Cannot make barcode fit");
"Cannot make barcode fit %dx%d\n", if (e) free (e);
W, H); return ERROR_INVALID_OPTION;
if (e)
free(e);
return 0;
} }
} }
encoding = e; encoding = e;
@ -802,16 +798,16 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
encoding = e; encoding = e;
} }
if (!matrix->W) { if (!matrix->W) {
fprintf(stderr, "Cannot find suitable size, barcode too long\n"); strcpy(symbol->errtxt, "Cannot find suitable size, barcode too long");
return 0; return ERROR_INVALID_OPTION;
} }
W = matrix->W; W = matrix->W;
H = matrix->H; H = matrix->H;
} }
if (!ecc200encode(binary, matrix->bytes, barcode, barcodelen, encoding, lenp)) { if (!ecc200encode(binary, matrix->bytes, barcode, barcodelen, encoding, lenp)) {
fprintf(stderr, "Barcode too long for %dx%d\n", W, H); strcpy(symbol->errtxt, "Barcode too long");
free(encoding); free(encoding);
return 0; return ERROR_INVALID_OPTION;
} }
// ecc code // ecc code
ecc200(binary, matrix->bytes, matrix->datablock, matrix->rsblock); ecc200(binary, matrix->bytes, matrix->datablock, matrix->rsblock);
@ -844,20 +840,25 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
} }
//fprintf (stderr, "\n"); //fprintf (stderr, "\n");
} }
for(y = H - 1; y >= 0; y--) {
int x;
for(x = 0; x < W; x++) {
if(grid[W * y + x]) {
symbol->encoded_data[(H - y) - 1][x] = '1'; }
else {
symbol->encoded_data[(H - y) - 1][x] = '0'; }
}
symbol->row_height[(H - y) - 1] = 1;
}
free(grid); free(grid);
free(places); free(places);
} }
if (Wptr)
*Wptr = W; symbol->rows = H;
if (Hptr) symbol->width = W;
*Hptr = H;
if (encodingptr)
*encodingptr = encoding;
if (maxp)
*maxp = matrix->bytes;
if (eccp)
*eccp =
(matrix->bytes + 2) / matrix->datablock * matrix->rsblock;
free(encoding); free(encoding);
return grid; return 0;
} }

View File

@ -36,12 +36,12 @@
* *
*/ */
#include "common.h"
#ifndef __IEC16022ECC200_H #ifndef __IEC16022ECC200_H
#define __IEC16022ECC200_H #define __IEC16022ECC200_H
unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int iec16022ecc200(unsigned char *barcode, int barcodelen, struct zint_symbol *symbol);
int barcodelen, unsigned char *barcode,
int *lenp, int *maxp, int *eccp);
#define MAXBARCODE 3116 #define MAXBARCODE 3116
#endif /* __IEC16022ECC200_H */ #endif /* __IEC16022ECC200_H */

View File

@ -4,14 +4,6 @@
libzint - the open source barcode library libzint - the open source barcode library
Copyright (C) 2008 Robin Stuart <zint@hotmail.co.uk> Copyright (C) 2008 Robin Stuart <zint@hotmail.co.uk>
This file is a hacked-up copy of:
* IEC16022 bar code generation by
* Adrian Kennard, Andrews & Arnold Ltd
* with help from Cliff Hones on the RS coding
*
* (c) 2004 Adrian Kennard, Andrews & Arnold Ltd
* (c) 2006 Stefan Schmidt <stefan@datenfreihafen.org>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or the Free Software Foundation; either version 3 of the License, or
@ -27,156 +19,25 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
/* The original version of this code is available at:
http://www.datenfreihafen.org/projects/iec16022.html */
#define IEC16022_VERSION "0.2"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h> #include <string.h>
#include "dm200.h" #include "dm200.h"
#include "common.h" #include "common.h"
// simple checked response malloc
void *safemalloc(int n)
{
void *p = malloc(n);
if (!p) {
fprintf(stderr, "Malloc(%d) failed\n", n);
exit(1);
}
return p;
}
int dmatrix(struct zint_symbol *symbol, unsigned char source[]) int dmatrix(struct zint_symbol *symbol, unsigned char source[])
{ {
int W = 0, H = 0; /* In later versions of this code this procedure will redirect control
int ecc = 0; dependent on the version of Data Matrix symbol required - for now it is
int barcodelen = 0; just a place holder */
char *encoding = 0; int barcodelen;
int len = 0, maxlen = 0, ecclen = 0; int error_number;
unsigned char *grid = 0;
char size[3], eccstr[3];
strcpy(size, ""); barcodelen = ustrlen(source);
strcpy(eccstr, "200"); if(barcodelen > 780) {
strcpy(symbol->errtxt, "Input too long [711]");
/* if (strlen(barcode) == 0) { // read from file return ERROR_TOO_LONG;
FILE *f = fopen(infile, "rb");
barcode = safemalloc(4001);
if (!f) {
strcpy(symbol->errtxt, "error: could not open file");
return 8;
}
barcodelen = fread(barcode, 1, 4000, f);
if (barcodelen < 0) {
strcpy(symbol->errtxt, "error: could not read file");
return 8;
}
barcode[barcodelen] = 0; // null terminate anyway
fclose(f);
} else */
barcodelen = ustrlen(source);
if(barcodelen > 780) {
strcpy(symbol->errtxt, "Input too long [711]");
return ERROR_TOO_LONG;
}
// check parameters
if (strlen(size) != 0) {
char *x = strchr(size, 'x');
W = atoi(size);
if (x)
H = atoi(x + 1);
if (!H)
W = H;
}
/* if (eccstr) */
ecc = atoi(eccstr);
/* Yes I _have_ commented out large blocks of code! - odd size Data Matrix support
may be included in a later release but the code for it isn't needed here */
/* if (W & 1) { // odd size
if (W != H || W < 9 || W > 49) {
strcpy(symbol->errtxt, "error: invalid Data Matrix size");
return ERROR_INVALID_OPTION;
}
if (!eccstr) {
if (W >= 17)
ecc = 140;
else if (W >= 13)
ecc = 100;
else if (W >= 11)
ecc = 80;
else
ecc = 0;
}
if (ecc && ecc != 50 && ecc != 80 && ecc != 100 && ecc != 140 ||
ecc == 50 && W < 11 || ecc == 80 && W < 13 || ecc == 100
&& W < 13 || ecc == 140 && W < 17) {
strcpy(symbol->errtxt, "error: invalid ecc value");
return ERROR_INVALID_OPTION;
}
} else if (W) { // even size
if (W < H) {
int t = W;
W = H;
H = t;
}
if (!eccstr)
ecc = 200;
if (ecc != 200) {
strcpy(symbol->errtxt, "error: invalid size for ecc 200");
return ERROR_INVALID_OPTION;
}
} }
else { // auto size error_number = iec16022ecc200(source, barcodelen, symbol);
if (!eccstr)
// default is even sizes only unless explicit ecc set to force odd
// sizes
ecc = 200;
} */
// processing stamps return error_number;
/*if ((W & 1) || ecc < 200) { // odd sizes
strcpy(symbol->errtxt, "error: odd sizes not supported");
return ERROR_INVALID_OPTION;
} else { */ // even sizes
grid = iec16022ecc200(&W, &H, &encoding, barcodelen, source, &len, &maxlen, &ecclen);
/*} */
// output
if (!grid || !W) {
strcpy(symbol->errtxt, "Data Matrix encoding error [722]");
return ERROR_ENCODING_PROBLEM;
}
int y;
/*for (y = H - 1; y >= 0; y--) {
int x;
for (x = 0; x < W; x++)
printf("%c",
grid[W * y + x] ? '*' : ' ');
printf("\n");
}*/
symbol->rows = H;
symbol->width = W;
for(y = H - 1; y >= 0; y--) {
int x;
for(x = 0; x < W; x++) {
if(grid[W * y + x]) {
symbol->encoded_data[(H - y) - 1][x] = '1'; }
else {
symbol->encoded_data[(H - y) - 1][x] = '0'; }
}
symbol->row_height[(H - y) - 1] = 1;
}
return 0;
} }