Skip to content

Commit

Permalink
FrameFiller allocation has been moved from heap to stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed Sep 9, 2014
1 parent b650d72 commit b9a50ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 45 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
tests/test_mmask.c, tests/test_monkey.c, tests/test_mqrspec.c,
tests/test_qrencode.c, tests/test_qrspec.c:
- Removed cache clearing calls.
* qrencode.c:
- FrameFiller now allocated in stack, not heap.

2014.09.08 Kentaro FUKUCHI <kentaro@fukuchi.org>
* qrenc.c:
Expand Down
60 changes: 15 additions & 45 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,15 @@ typedef struct {
int mqr;
} FrameFiller;

static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr)
static void FrameFiller_set(FrameFiller *filler, int width, unsigned char *frame, int mqr)
{
FrameFiller *filler;

filler = (FrameFiller *)malloc(sizeof(FrameFiller));
if(filler == NULL) return NULL;
filler->width = width;
filler->frame = frame;
filler->x = width - 1;
filler->y = width - 1;
filler->dir = -1;
filler->bit = -1;
filler->mqr = mqr;

return filler;
}

static unsigned char *FrameFiller_next(FrameFiller *filler)
Expand Down Expand Up @@ -363,60 +357,48 @@ extern unsigned char *FrameFiller_test(int version)
{
int width;
unsigned char *frame, *p;
FrameFiller *filler;
int i, length;
FrameFiller filler;

width = QRspec_getWidth(version);
frame = QRspec_newFrame(version);
if(frame == NULL) return NULL;
filler = FrameFiller_new(width, frame, 0);
if(filler == NULL) {
free(frame);
return NULL;
}
FrameFiller_set(&filler, width, frame, 0);
length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8
+ QRspec_getECCLength(version, QR_ECLEVEL_L) * 8
+ QRspec_getRemainder(version);
for(i=0; i<length; i++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) {
free(filler);
free(frame);
return NULL;
}
*p = (unsigned char)(i & 0x7f) | 0x80;
}
free(filler);
return frame;
}

extern unsigned char *FrameFiller_testMQR(int version)
{
int width;
unsigned char *frame, *p;
FrameFiller *filler;
int i, length;
FrameFiller filler;

width = MQRspec_getWidth(version);
frame = MQRspec_newFrame(version);
if(frame == NULL) return NULL;
filler = FrameFiller_new(width, frame, 1);
if(filler == NULL) {
free(frame);
return NULL;
}
FrameFiller_set(&filler, width, frame, 1);
length = MQRspec_getDataLengthBit(version, QR_ECLEVEL_L)
+ MQRspec_getECCLength(version, QR_ECLEVEL_L) * 8;
for(i=0; i<length; i++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) {
fprintf(stderr, "Frame filler run over the frame!\n");
free(filler);
return frame;
}
*p = (unsigned char)(i & 0x7f) | 0x80;
}
free(filler);
return frame;
}
#endif
Expand Down Expand Up @@ -453,9 +435,9 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
int width, version;
QRRawCode *raw;
unsigned char *frame, *masked, *p, code, bit;
FrameFiller *filler;
int i, j;
QRcode *qrcode = NULL;
FrameFiller filler;

if(input->mqr) {
errno = EINVAL;
Expand All @@ -480,19 +462,14 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
QRraw_free(raw);
return NULL;
}
filler = FrameFiller_new(width, frame, 0);
if(filler == NULL) {
QRraw_free(raw);
free(frame);
return NULL;
}
FrameFiller_set(&filler, width, frame, 0);

/* inteleaved data and ecc codes */
for(i=0; i<raw->dataLength + raw->eccLength; i++) {
code = QRraw_getCode(raw);
bit = 0x80;
for(j=0; j<8; j++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) goto EXIT;
*p = 0x02 | ((bit & code) != 0);
bit = bit >> 1;
Expand All @@ -503,7 +480,7 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)
/* remainder bits */
j = QRspec_getRemainder(version);
for(i=0; i<j; i++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) goto EXIT;
*p = 0x02;
}
Expand All @@ -527,7 +504,6 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask)

EXIT:
QRraw_free(raw);
free(filler);
free(frame);
return qrcode;
}
Expand All @@ -537,9 +513,9 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
int width, version;
MQRRawCode *raw;
unsigned char *frame, *masked, *p, code, bit;
FrameFiller *filler;
int i, j;
QRcode *qrcode = NULL;
FrameFiller filler;

if(!input->mqr) {
errno = EINVAL;
Expand All @@ -564,28 +540,23 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)
MQRraw_free(raw);
return NULL;
}
filler = FrameFiller_new(width, frame, 1);
if(filler == NULL) {
MQRraw_free(raw);
free(frame);
return NULL;
}
FrameFiller_set(&filler, width, frame, 1);

/* inteleaved data and ecc codes */
for(i=0; i<raw->dataLength + raw->eccLength; i++) {
code = MQRraw_getCode(raw);
if(raw->oddbits && i == raw->dataLength - 1) {
bit = 1 << (raw->oddbits - 1);
for(j=0; j<raw->oddbits; j++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) goto EXIT;
*p = 0x02 | ((bit & code) != 0);
bit = bit >> 1;
}
} else {
bit = 0x80;
for(j=0; j<8; j++) {
p = FrameFiller_next(filler);
p = FrameFiller_next(&filler);
if(p == NULL) goto EXIT;
*p = 0x02 | ((bit & code) != 0);
bit = bit >> 1;
Expand All @@ -609,7 +580,6 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask)

EXIT:
MQRraw_free(raw);
free(filler);
free(frame);
return qrcode;
}
Expand Down

0 comments on commit b9a50ba

Please sign in to comment.