Esempio n. 1
0
void print_eccTable(void)
{
	int i, j;
	int ecc;
	int data;
	int spec[5];

	for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
		printf("Version %2d\n", i);
		for(j=0; j<4; j++) {
			QRspec_getEccSpec(i, (QRecLevel)j, spec);
			data = QRspec_rsBlockNum1(spec) * QRspec_rsDataCodes1(spec)
			     + QRspec_rsBlockNum2(spec) * QRspec_rsDataCodes2(spec);
			ecc  = QRspec_rsBlockNum1(spec) * QRspec_rsEccCodes1(spec)
			     + QRspec_rsBlockNum2(spec) * QRspec_rsEccCodes2(spec);
			printf("%3d\t", data);
			printf("%3d\t", ecc);
			printf("%2d\t", QRspec_rsBlockNum1(spec));
			printf("(%3d, %3d, %3d)\n",
				   QRspec_rsDataCodes1(spec) + QRspec_rsEccCodes1(spec),
				   QRspec_rsDataCodes1(spec),
				   QRspec_rsEccCodes1(spec));
			if(QRspec_rsBlockNum2(spec) > 0) {
				printf("\t%2d\t", QRspec_rsBlockNum2(spec));
				printf("(%3d, %3d, %3d)\n",
					   QRspec_rsDataCodes2(spec) + QRspec_rsEccCodes2(spec),
					   QRspec_rsDataCodes2(spec),
					   QRspec_rsEccCodes2(spec));
			}
		}
	}
}
Esempio n. 2
0
void test_eccTable(void)
{
	int i, j;
	int ecc;
	int data;
	int err = 0;
	int spec[5];

	testStart("Checking ECC table.");
	for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
		for(j=0; j<4; j++) {
			QRspec_getEccSpec(i, (QRecLevel)j, spec);
			data = QRspec_rsBlockNum1(spec) * QRspec_rsDataCodes1(spec)
			     + QRspec_rsBlockNum2(spec) * QRspec_rsDataCodes2(spec);
			ecc  = QRspec_rsBlockNum1(spec) * QRspec_rsEccCodes1(spec)
			     + QRspec_rsBlockNum2(spec) * QRspec_rsEccCodes2(spec);
			if(data + ecc != QRspec_getDataLength(i, (QRecLevel)j) + QRspec_getECCLength(i, (QRecLevel)j)) {
				printf("Error in version %d, level %d: invalid size\n", i, j);
				printf("%d %d %d %d %d %d\n", spec[0], spec[1], spec[2], spec[3], spec[4], spec[2]);
				err++;
			}
			if(ecc != QRspec_getECCLength(i, (QRecLevel)j)) {
				printf("Error in version %d, level %d: invalid data\n", i, j);
				printf("%d %d %d %d %d %d\n", spec[0], spec[1], spec[2], spec[3], spec[4], spec[2]);
				err++;
			}
		}
	}
	testEnd(err);
}
Esempio n. 3
0
void test_eccTable2(void)
{
	int i;
	int spec[5];

	const int correct[7][6] = {
		{ 8,  1, 0,  2, 60, 38},
		{ 8,  1, 1,  2, 61, 39},
		{24,  2, 0, 11, 54, 24},
		{24,  2, 1, 16, 55, 25},
		{32,  0, 0, 17, 145, 115},
		{40,  3, 0, 20, 45, 15},
		{40,  3, 1, 61, 46, 16},
	};

	testStart("Checking ECC table(2)");
	for(i=0; i<7; i++) {
		QRspec_getEccSpec(correct[i][0], (QRecLevel)correct[i][1], spec);
		if(correct[i][2] == 0) {
			assert_equal(QRspec_rsBlockNum1(spec), correct[i][3],
				"Error in version %d, level %d. rsBlockNum1 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsBlockNum1(spec), correct[i][3]);
			assert_equal(QRspec_rsDataCodes1(spec) + QRspec_rsEccCodes1(spec), correct[i][4],
				"Error in version %d, level %d. rsDataCodes1 + rsEccCodes1 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsDataCodes1(spec) + QRspec_rsEccCodes1(spec), correct[i][4]);
			assert_equal(QRspec_rsDataCodes1(spec), correct[i][5],
				"Error in version %d, level %d. rsDataCodes1 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsDataCodes1(spec), correct[i][5]);
		} else {
			assert_equal(QRspec_rsBlockNum2(spec), correct[i][3],
				"Error in version %d, level %d. rsBlockNum2 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsBlockNum2(spec), correct[i][3]);
			assert_equal(QRspec_rsDataCodes2(spec) + QRspec_rsEccCodes2(spec), correct[i][4],
				"Error in version %d, level %d. rsDataCodes2 + rsEccCodes2 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsDataCodes2(spec) + QRspec_rsEccCodes2(spec), correct[i][4]);
			assert_equal(QRspec_rsDataCodes2(spec), correct[i][5],
				"Error in version %d, level %d. rsDataCodes2 was %d, expected %d.\n",
				correct[i][0], correct[i][1],
				QRspec_rsDataCodes2(spec), correct[i][5]);
		}
	}
	testFinish();
}
Esempio n. 4
0
static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
{
	int i;
	RSblock *block;
	unsigned char *dp, *ep;
	int el, dl;

	dl = QRspec_rsDataCodes1(spec);
	el = QRspec_rsEccCodes1(spec);

	block = blocks;
	dp = data;
	ep = ecc;
	for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
		RSblock_initBlock(block, dl, dp, el, ep);
		dp += dl;
		ep += el;
		block++;
	}

	if(QRspec_rsBlockNum2(spec) == 0) return 0;

	dl = QRspec_rsDataCodes2(spec);
	el = QRspec_rsEccCodes2(spec);
	for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
		RSblock_initBlock(block, dl, dp, el, ep);
		dp += dl;
		ep += el;
		block++;
	}

	return 0;
}
Esempio n. 5
0
static int RSblock_init(RSblock *blocks, int spec[5], unsigned char *data, unsigned char *ecc)
{
	int i;
	RSblock *block;
	unsigned char *dp, *ep;
	RS *rs;
	int el, dl;
	dl = QRspec_rsDataCodes1(spec);
	el = QRspec_rsEccCodes1(spec);
	rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
	if(rs == NULL) return -1;
	block = blocks;
	dp = data;
	ep = ecc;
	for(i=0; i<QRspec_rsBlockNum1(spec); i++) {
		RSblock_initBlock(block, dl, dp, el, ep, rs);
		dp += dl;
		ep += el;
		block++;
	}
	if(QRspec_rsBlockNum2(spec) == 0) return 0;
	dl = QRspec_rsDataCodes2(spec);
	el = QRspec_rsEccCodes2(spec);
	rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el);
	if(rs == NULL) return -1;
	for(i=0; i<QRspec_rsBlockNum2(spec); i++) {
		RSblock_initBlock(block, dl, dp, el, ep, rs);
		dp += dl;
		ep += el;
		block++;
	}
	return 0;
}
Esempio n. 6
0
__STATIC QRRawCode *QRraw_new(QRinput *input)
{
	QRRawCode *raw;
	int spec[5], ret;

	raw = (QRRawCode *)malloc(sizeof(QRRawCode));
	if(raw == NULL) return NULL;

	raw->datacode = QRinput_getByteStream(input);
	if(raw->datacode == NULL) {
		free(raw);
		return NULL;
	}

	QRspec_getEccSpec(input->version, input->level, spec);

	raw->version = input->version;
	raw->b1 = QRspec_rsBlockNum1(spec);
	raw->dataLength = QRspec_rsDataLength(spec);
	raw->eccLength = QRspec_rsEccLength(spec);
	raw->ecccode = (unsigned char *)malloc(raw->eccLength);
	if(raw->ecccode == NULL) {
		free(raw->datacode);
		free(raw);
		return NULL;
	}

	raw->blocks = QRspec_rsBlockNum(spec);
	raw->rsblock = (RSblock *)calloc(raw->blocks, sizeof(RSblock));
	if(raw->rsblock == NULL) {
		QRraw_free(raw);
		return NULL;
	}
	ret = RSblock_init(raw->rsblock, spec, raw->datacode, raw->ecccode);
	if(ret < 0) {
		QRraw_free(raw);
		return NULL;
	}

	raw->count = 0;

	return raw;
}
Esempio n. 7
0
void test_encode_an(int num)
{
	int ret;
	int len;
	len = fillANData();
	QRcode *qrcode;
	QRdata *qrdata;
	FILE *fp;
	char buf[256];

	qrcode = QRcode_encodeString((char *)data, 0, num % 4, QR_MODE_8, num % 2);
	if(qrcode == NULL) {
		if(errno == ERANGE) return;
		perror("test_encode_an aborted at QRcode_encodeString():");
		printf("Length: %d\n", len);
		printf("Level: %d\n", num % 4);
		return;
	}
	qrdata = QRcode_decode(qrcode);
	if(qrdata == NULL) {
		printf("#%d: Failed to decode this code.\n", num);
		QRcode_free(qrcode);
		return;
	}
	if(qrdata->size != len) {
		printf("#%d: length mismatched (orig: %d, decoded: %d)\n", num, len, qrdata->size);
	}
	ret = memcmp(qrdata->data, data, len);
	if(ret != 0) {
		unsigned char *frame, *p;
		int x,y, c;
		QRinput *input;
		QRcode *origcode;
		BitStream *bstream;
		int spec[5];

		printf("#%d: data mismatched.\n", num);
		printf("Version: %d\n", qrcode->version);
		QRspec_getEccSpec(qrcode->version, num%4, spec);
		printf("DataLength: %d\n", QRspec_rsDataLength(spec));
		printf("BlockNum1: %d\n", QRspec_rsBlockNum1(spec));
		printf("BlockNum: %d\n", QRspec_rsBlockNum(spec));
		printf("DataCodes1: %d\n", QRspec_rsDataCodes1(spec));

		snprintf(buf, 256, "monkey-orig-%d.dat", num);
		fp = fopen(buf, "w");
		fputs((char *)data, fp);
		fclose(fp);

		snprintf(buf, 256, "monkey-result-%d.dat", num);
		fp = fopen(buf, "w");
		fputs((char *)qrdata->data, fp);
		fclose(fp);

		snprintf(buf, 256, "monkey-result-unmasked-%d.dat", num);
		fp = fopen(buf, "w");
		frame = QRcode_unmask(qrcode);
		p = frame;
		for(y=0; y<qrcode->width; y++) {
			for(x=0; x<qrcode->width; x++) {
				fputc((*p&1)?'1':'0', fp);
				p++;
			}
			fputc('\n', fp);
		}
		fclose(fp);
		free(frame);

		snprintf(buf, 256, "monkey-orig-unmasked-%d.dat", num);
		fp = fopen(buf, "w");
		input = QRinput_new2(0, num % 4);
		Split_splitStringToQRinput((char *)data, input, QR_MODE_8, num % 2);
		origcode = QRcode_encodeMask(input, -2);
		p = origcode->data;
		for(y=0; y<origcode->width; y++) {
			for(x=0; x<origcode->width; x++) {
				fputc((*p&1)?'1':'0', fp);
				p++;
			}
			fputc('\n', fp);
		}
		fclose(fp);
		QRcode_free(origcode);

		snprintf(buf, 256, "monkey-orig-bits-%d.dat", num);
		fp = fopen(buf, "w");
		bstream = QRinput_mergeBitStream(input);
		c = 0;
		for(x=0; x<bstream->length; x++) {
			fputc((bstream->data[x]&1)?'1':'0', fp);
			if((x & 7) == 7) {
				fputc(' ', fp);
				c++;
			}
			if((x & 63) == 63) {
				fprintf(fp, "%d\n", c);
			}
		}
		fclose(fp);
		QRinput_free(input);
		BitStream_free(bstream);

		snprintf(buf, 256, "monkey-result-bits-%d.dat", num);
		fp = fopen(buf, "w");
		p = QRcode_extractBits(qrcode, &y);
		c = 0;
		for(x=0; x<y; x++) {
			fputc((p[x]&1)?'1':'0', fp);
			if((x & 7) == 7) {
				fputc(' ', fp);
				c++;
			}
			if((x & 63) == 63) {
				fprintf(fp, "%d\n", c);
			}
		}
		fclose(fp);
		free(p);
	}
	QRdata_free(qrdata);
	QRcode_free(qrcode);
}