__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; if(!input->mqr) { errno = EINVAL; return NULL; } if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) { errno = EINVAL; return NULL; } if(input->level > QR_ECLEVEL_Q) { errno = EINVAL; return NULL; } raw = MQRraw_new(input); if(raw == NULL) return NULL; version = raw->version; width = MQRspec_getWidth(version); frame = MQRspec_newFrame(version); if(frame == NULL) { MQRraw_free(raw); return NULL; } filler = FrameFiller_new(width, frame, 1); if(filler == NULL) { MQRraw_free(raw); free(frame); return NULL; } /* 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); if(p == NULL) goto EXIT; *p = 0x02 | ((bit & code) != 0); bit = bit >> 1; } } else {
void test_mqrraw_new(void) { QRinput *stream; char *num = "01234"; unsigned char datacode[] = {0xa0, 0x62, 0x02}; MQRRawCode *raw; testStart("Test MQRRaw_new()"); stream = QRinput_newMQR(1, QR_ECLEVEL_L); QRinput_append(stream, QR_MODE_NUM, 5, (unsigned char *)num); raw = MQRraw_new(stream); assert_nonnull(raw, "Failed MQRraw_new().\n"); assert_zero(raw->count, "MQRraw.count = %d != 0\n", raw->count); assert_equal(raw->version, 1, "MQRraw.version was not as expected. (%d)\n", raw->version); assert_equal(raw->dataLength, 3, "MQRraw.dataLength was not as expected.\n"); assert_equal(raw->eccLength, 2, "MQRraw.eccLength was not as expected.\n"); assert_zero(memcmp(raw->datacode, datacode, 3), "Datacode doesn't match.\n"); QRinput_free(stream); MQRraw_free(raw); testFinish(); }
STATIC_IN_RELEASE QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) { int width, version; MQRRawCode *raw; unsigned char *frame, *masked, *p, code, bit; int i, j, length; QRcode *qrcode = NULL; FrameFiller filler; if(!input->mqr) { errno = EINVAL; return NULL; } if(input->version <= 0 || input->version > MQRSPEC_VERSION_MAX) { errno = EINVAL; return NULL; } if(input->level > QR_ECLEVEL_Q) { errno = EINVAL; return NULL; } raw = MQRraw_new(input); if(raw == NULL) return NULL; version = raw->version; width = MQRspec_getWidth(version); frame = MQRspec_newFrame(version); if(frame == NULL) { MQRraw_free(raw); return NULL; } FrameFiller_set(&filler, width, frame, 1); /* interleaved data and ecc codes */ for(i = 0; i < raw->dataLength; i++) { code = MQRraw_getCode(raw); bit = 0x80; if(raw->oddbits && i == raw->dataLength - 1) { length = raw->oddbits; } else { length = 8; } for(j = 0; j < length; j++) { p = FrameFiller_next(&filler); if(p == NULL) goto EXIT; *p = ((bit & code) != 0); bit = bit >> 1; } } for(i = 0; i < raw->eccLength; i++) { code = MQRraw_getCode(raw); bit = 0x80; length = 8; for(j = 0; j < length; j++) { p = FrameFiller_next(&filler); if(p == NULL) goto EXIT; *p = 0x02 | ((bit & code) != 0); bit = bit >> 1; } } MQRraw_free(raw); raw = NULL; /* masking */ if(mask == -2) { // just for debug purpose masked = (unsigned char *)malloc(width * width); memcpy(masked, frame, width * width); } else if(mask < 0) { masked = MMask_mask(version, frame, input->level); } else { masked = MMask_makeMask(version, frame, mask, input->level); } if(masked == NULL) { goto EXIT; } qrcode = QRcode_new(version, width, masked); if(qrcode == NULL) { free(masked); } EXIT: MQRraw_free(raw); free(frame); return qrcode; }