mirror of
https://github.com/woo-j/zint.git
synced 2024-12-26 07:21:31 +03:00
Reduce memory leak and tidy code
This commit is contained in:
parent
858f69ea55
commit
608009b2a5
@ -28,6 +28,7 @@
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include "reedsol.h"
|
||||
#include "common.h"
|
||||
#include "dm200.h"
|
||||
|
||||
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).
|
||||
*/
|
||||
|
||||
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
|
||||
int W = 0, H = 0;
|
||||
char *encoding = 0;
|
||||
unsigned char *grid = 0;
|
||||
int lend, *lenp;
|
||||
struct ecc200matrix_s *matrix;
|
||||
memset(binary, 0, sizeof(binary));
|
||||
if (encodingptr)
|
||||
encoding = *encodingptr;
|
||||
if (Wptr)
|
||||
W = *Wptr;
|
||||
if (Hptr)
|
||||
H = *Hptr;
|
||||
|
||||
lend = 0;
|
||||
lenp = &lend;
|
||||
|
||||
// encoding
|
||||
if (W) { // known size
|
||||
for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++) ;
|
||||
if (!matrix->W) {
|
||||
fprintf(stderr, "Invalid size %dx%d\n", W, H);
|
||||
return 0;
|
||||
strcpy(symbol->errtxt, "Invalid size");
|
||||
return ERROR_INVALID_OPTION;
|
||||
}
|
||||
if (!encoding) {
|
||||
int len;
|
||||
@ -769,12 +768,9 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
|
||||
free(e);
|
||||
e = encmake(barcodelen, barcode, &len, 0);
|
||||
if (len > matrix->bytes) {
|
||||
fprintf(stderr,
|
||||
"Cannot make barcode fit %dx%d\n",
|
||||
W, H);
|
||||
if (e)
|
||||
free(e);
|
||||
return 0;
|
||||
strcpy(symbol->errtxt, "Cannot make barcode fit");
|
||||
if (e) free (e);
|
||||
return ERROR_INVALID_OPTION;
|
||||
}
|
||||
}
|
||||
encoding = e;
|
||||
@ -786,7 +782,7 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
|
||||
|
||||
if (encoding) { // find one that fits chosen encoding
|
||||
for (matrix = ecc200matrix; matrix->W; matrix++)
|
||||
if (ecc200encode(binary, matrix->bytes, barcode, barcodelen, encoding, 0))
|
||||
if (ecc200encode(binary, matrix->bytes, barcode, barcodelen, encoding, 0))
|
||||
break;
|
||||
} else {
|
||||
int len;
|
||||
@ -802,16 +798,16 @@ unsigned char *iec16022ecc200(int *Wptr, int *Hptr, char **encodingptr, int barc
|
||||
encoding = e;
|
||||
}
|
||||
if (!matrix->W) {
|
||||
fprintf(stderr, "Cannot find suitable size, barcode too long\n");
|
||||
return 0;
|
||||
strcpy(symbol->errtxt, "Cannot find suitable size, barcode too long");
|
||||
return ERROR_INVALID_OPTION;
|
||||
}
|
||||
W = matrix->W;
|
||||
H = matrix->H;
|
||||
}
|
||||
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);
|
||||
return 0;
|
||||
return ERROR_INVALID_OPTION;
|
||||
}
|
||||
// ecc code
|
||||
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");
|
||||
}
|
||||
|
||||
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(places);
|
||||
}
|
||||
if (Wptr)
|
||||
*Wptr = W;
|
||||
if (Hptr)
|
||||
*Hptr = H;
|
||||
if (encodingptr)
|
||||
*encodingptr = encoding;
|
||||
if (maxp)
|
||||
*maxp = matrix->bytes;
|
||||
if (eccp)
|
||||
*eccp =
|
||||
(matrix->bytes + 2) / matrix->datablock * matrix->rsblock;
|
||||
|
||||
symbol->rows = H;
|
||||
symbol->width = W;
|
||||
|
||||
free(encoding);
|
||||
return grid;
|
||||
return 0;
|
||||
}
|
||||
|
@ -36,12 +36,12 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifndef __IEC16022ECC200_H
|
||||
#define __IEC16022ECC200_H
|
||||
|
||||
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);
|
||||
#define MAXBARCODE 3116
|
||||
|
||||
#endif /* __IEC16022ECC200_H */
|
||||
|
@ -4,14 +4,6 @@
|
||||
libzint - the open source barcode library
|
||||
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
|
||||
it under the terms of the GNU General Public License as published by
|
||||
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.
|
||||
*/
|
||||
|
||||
/* 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 "dm200.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 W = 0, H = 0;
|
||||
int ecc = 0;
|
||||
int barcodelen = 0;
|
||||
char *encoding = 0;
|
||||
int len = 0, maxlen = 0, ecclen = 0;
|
||||
unsigned char *grid = 0;
|
||||
char size[3], eccstr[3];
|
||||
/* In later versions of this code this procedure will redirect control
|
||||
dependent on the version of Data Matrix symbol required - for now it is
|
||||
just a place holder */
|
||||
int barcodelen;
|
||||
int error_number;
|
||||
|
||||
strcpy(size, "");
|
||||
strcpy(eccstr, "200");
|
||||
|
||||
/* if (strlen(barcode) == 0) { // read from file
|
||||
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;
|
||||
barcodelen = ustrlen(source);
|
||||
if(barcodelen > 780) {
|
||||
strcpy(symbol->errtxt, "Input too long [711]");
|
||||
return ERROR_TOO_LONG;
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
error_number = iec16022ecc200(source, barcodelen, symbol);
|
||||
|
||||
else { // auto size
|
||||
if (!eccstr)
|
||||
// default is even sizes only unless explicit ecc set to force odd
|
||||
// sizes
|
||||
ecc = 200;
|
||||
} */
|
||||
|
||||
// processing stamps
|
||||
/*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;
|
||||
return error_number;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user