void test_toByte_4bitpadding(void) { BitStream *bstream; unsigned char *result; testStart("Convert to a byte array"); bstream = BitStream_new(); BitStream_appendNum(bstream, 4, 0xb); result = BitStream_toByte(bstream); assert_equal(result[0], 0xb0, "incorrect paddings\n"); BitStream_free(bstream); free(result); bstream = BitStream_new(); BitStream_appendNum(bstream, 12, 0x335); result = BitStream_toByte(bstream); assert_equal(result[0], 0x33, "incorrect paddings\n"); assert_equal(result[1], 0x50, "incorrect paddings\n"); BitStream_free(bstream); free(result); testFinish(); }
void test_padding2(void) { QRinput *input; BitStream *bstream; int i, size, ret; char data[] = "0123456789ABCDeF"; char correct[153]; unsigned char c; testStart("Padding bit check. (1 or 2 padding bytes)"); /* 16 byte data (4 bit terminator and 1 byte padding) */ memset(correct, 0, 153); memcpy(correct, "010000010000", 12); for(size=0; size<16; size++) { c = 0x80; for(i=0; i<8; i++) { correct[size * 8 + i + 12] = (data[size]&c)?'1':'0'; c = c >> 1; } } memcpy(correct + 140, "000011101100", 12); input = QRinput_new2(1, QR_ECLEVEL_L); QRinput_append(input, QR_MODE_8, 16, (unsigned char *)data); bstream = BitStream_new(); QRinput_getBitStream(input, bstream); size = BitStream_size(bstream); assert_equal(size, 152, "16byte: # of bit is incorrect (%d != 152).\n", size); ret = ncmpBin(correct, bstream, 152); assert_zero(ret, "Padding bits incorrect.\n"); printBstream(bstream); QRinput_free(input); BitStream_free(bstream); /* 15 byte data (4 bit terminator and 2 byte paddings) */ memcpy(correct, "010000001111", 12); memcpy(correct + 132, "00001110110000010001", 20); input = QRinput_new2(1, QR_ECLEVEL_L); QRinput_append(input, QR_MODE_8, 15, (unsigned char *)data); bstream = BitStream_new(); QRinput_getBitStream(input, bstream); size = BitStream_size(bstream); assert_equal(size, 152, "15byte: # of bit is incorrect (%d != 152).\n", size); ret = ncmpBin(correct, bstream, 152); assert_zero(ret, "Padding bits incorrect.\n"); printBstream(bstream); testFinish(); QRinput_free(input); BitStream_free(bstream); }
void test_encodeECI(void) { QRinput *input; BitStream *bstream; unsigned char str[] = {0xa1, 0xa2, 0xa3, 0xa4, 0xa5}; char *correct = "0111 00001001 0100 00000101 10100001 10100010 10100011 10100100 10100101"; int ret; testStart("Encoding characters with ECI header."); input = QRinput_new(); ret = QRinput_appendECIheader(input, 9); assert_zero(ret, "Valid ECI header rejected.\n"); ret = QRinput_append(input, QR_MODE_8, 5, str); assert_zero(ret, "Failed to append characters.\n"); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); assert_nonnull(bstream, "Failed to merge.\n"); if(bstream != NULL) { ret = ncmpBin(correct, bstream, 64); assert_zero(ret, "Encodation of ECI header was invalid.\n"); BitStream_free(bstream); } QRinput_free(input); testFinish(); }
int encodeURLandCompare(char *url, int expected_length) { QRinput *input; BitStream *bstream; int ret = 0; input = QRinput_new2(0, QR_ECLEVEL_L); Split_splitStringToQRinput(url, input, QR_MODE_8, 1); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); int length = BitStream_size(bstream); if(length > expected_length) { printf("The length of the encode stream is longer than expected: %d over %d\n", length, expected_length); printQRinput(input); ret = 1; } else if(length < expected_length) { printf("The length of the encode stream is shorter than expected: %d under %d\n", length, expected_length); printQRinput(input); ret = 1; } QRinput_free(input); BitStream_free(bstream); return ret; }
void test_padding(void) { QRinput *input; BitStream *bstream; int i, size; char data[] = "0123456789ABCDeFG"; unsigned char c; testStart("Padding bit check. (less than 5 bits)"); input = QRinput_new2(1, QR_ECLEVEL_L); QRinput_append(input, QR_MODE_8, 17, (unsigned char *)data); bstream = BitStream_new(); QRinput_getBitStream(input, bstream); size = BitStream_size(bstream); assert_equal(size, 152, "# of bit is incorrect (%d != 152).\n", size); c = 0; for(i=0; i<4; i++) { c += bstream->data[size - i - 1]; } assert_zero(c, "Padding bits are not zero."); testFinish(); QRinput_free(input); BitStream_free(bstream); }
static BitStream *BitStream_newFromBytes(int size, unsigned char *data) { unsigned char mask; int i, j; unsigned char *p; BitStream *bstream; bstream = BitStream_new(); if(bstream == NULL) return NULL; if(BitStream_allocate(bstream, size * 8)) { BitStream_free(bstream); return NULL; } p = bstream->data; for(i=0; i<size; i++) { mask = 0x80; for(j=0; j<8; j++) { if(data[i] & mask) { *p = 1; } else { *p = 0; } p++; mask = mask >> 1; } } return bstream; }
static BitStream *BitStream_newFromNum(int bits, unsigned int num) { unsigned int mask; int i; unsigned char *p; BitStream *bstream; bstream = BitStream_new(); if(bstream == NULL) return NULL; if(BitStream_allocate(bstream, bits)) { BitStream_free(bstream); return NULL; } p = bstream->data; mask = 1 << (bits - 1); for(i=0; i<bits; i++) { if(num & mask) { *p = 1; } else { *p = 0; } p++; mask = mask >> 1; } return bstream; }
void test_appendBytes(void) { BitStream *bstream; unsigned char data[8]; char correct[] = "10001010111111111111111100010010001101000101011001111000"; testStart("Append Bytes"); bstream = BitStream_new(); data[0] = 0x8a; BitStream_appendBytes(bstream, 1, data); assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect."); data[0] = 0xff; data[1] = 0xff; BitStream_appendBytes(bstream, 2, data); assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.\n"); data[0] = 0x12; data[1] = 0x34; data[2] = 0x56; data[3] = 0x78; BitStream_appendBytes(bstream, 4, data); assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.\n"); testFinish(); BitStream_free(bstream); }
int encodeAndCheckBStream(int mqr, int version, QRecLevel level, QRencodeMode mode, char *data, char *correct) { QRinput *input; BitStream *bstream; int ret; if(mqr) { input = QRinput_newMQR(version, level); } else { input = QRinput_new2(version, level); } QRinput_append(input, mode, strlen(data), (unsigned char *)data); bstream = BitStream_new(); QRinput_getBitStream(input, bstream); ret = cmpBin(correct, bstream); if(ret) { printf("result : "); printBstream(bstream); printf("correct: %s\n", correct); } QRinput_free(input); BitStream_free(bstream); return ret; }
void test_struct_split_example(void) { QRinput *input; QRinput_Struct *s; QRinput_InputList *e; QRinput_List *l; const char *str[4] = { "an example ", "of four Str", "uctured Appe", "nd symbols,"}; int i; BitStream *bstream; testStart("Testing the example of structured-append symbols"); s = QRinput_Struct_new(); for(i=0; i<4; i++) { input = QRinput_new2(1, QR_ECLEVEL_M); QRinput_append(input, QR_MODE_8, strlen(str[i]), (unsigned char *)str[i]); QRinput_Struct_appendInput(s, input); } QRinput_Struct_insertStructuredAppendHeaders(s); e = s->head; i = 0; while(e != NULL) { bstream = BitStream_new(); QRinput_mergeBitStream(e->input, bstream); BitStream_free(bstream); l = e->input->head->next; assert_equal(l->mode, QR_MODE_8, "#%d: wrong mode (%d).\n", i, l->mode); assert_equal(e->input->level, QR_ECLEVEL_M, "#%d: wrong level (%d).\n", i, e->input->level); e = e->next; i++; } testFinish(); QRinput_Struct_free(s); }
void test_append(void) { BitStream *bs1, *bs2; char c1[] = "00"; char c2[] = "0011"; char c3[] = "01111111111111111"; char c4[] = "001101111111111111111"; char c5[] = "0011011111111111111111111111111111"; int ret; testStart("Append two BitStreams"); bs1 = BitStream_new(); bs2 = BitStream_new(); ret = BitStream_appendNum(bs1, 1, 0); ret = BitStream_appendNum(bs2, 1, 0); ret = BitStream_append(bs1, bs2); assert_zero(ret, "Failed to append."); assert_zero(cmpBin(c1, bs1), "Internal data is incorrect."); ret = BitStream_appendNum(bs1, 2, 3); assert_zero(ret, "Failed to append."); assert_zero(cmpBin(c2, bs1), "Internal data is incorrect."); ret = BitStream_appendNum(bs2, 16, 65535); assert_zero(ret, "Failed to append."); assert_zero(cmpBin(c3, bs2), "Internal data is incorrect."); ret = BitStream_append(bs1, bs2); assert_zero(ret, "Failed to append."); assert_zero(cmpBin(c4, bs1), "Internal data is incorrect."); ret = BitStream_appendNum(bs1, 13, 16383); assert_zero(ret, "Failed to append."); assert_zero(cmpBin(c5, bs1), "Internal data is incorrect."); testFinish(); BitStream_free(bs1); BitStream_free(bs2); }
void test_bytes(void) { BitStream *bstream; unsigned char data[1] = {0x3a}; char correct[] = "00111010"; testStart("New from bytes"); bstream = BitStream_new(); BitStream_appendBytes(bstream, 1, data); testEnd(cmpBin(correct, bstream)); BitStream_free(bstream); }
int inputSize(QRinput *input) { BitStream *bstream; int size; bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); size = BitStream_size(bstream); BitStream_free(bstream); return size; }
void test_num(void) { BitStream *bstream; unsigned int data = 0x13579bdf; char correct[] = "0010011010101111001101111011111"; testStart("New from num"); bstream = BitStream_new(); BitStream_appendNum(bstream, 31, data); testEnd(cmpBin(correct, bstream)); BitStream_free(bstream); }
void test_null(void) { BitStream *bstream; testStart("Empty stream"); bstream = BitStream_new(); assert_zero(BitStream_size(bstream), "Size of empty BitStream is not 0.\n"); assert_null(BitStream_toByte(bstream), "BitStream_toByte returned non-NULL.\n"); assert_nothing(BitStream_free(NULL), "Checking BitStream_free(NULL).\n"); testFinish(); BitStream_free(bstream); }
void encodeURLandPrint(char *url) { QRinput *input; BitStream *bstream; input = QRinput_new2(0, QR_ECLEVEL_L); Split_splitStringToQRinput(url, input, QR_MODE_8, 1); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); printf("{%d,\"%s\"},\n", BitStream_size(bstream), url); QRinput_free(input); BitStream_free(bstream); }
void test_size(void) { BitStream *bstream; testStart("size check"); bstream = BitStream_new(); assert_equal(BitStream_size(bstream), 0, "Initialized BitStream is not 0 length"); BitStream_appendNum(bstream, 1, 0); assert_equal(BitStream_size(bstream), 1, "Size incorrect. (first append)"); BitStream_appendNum(bstream, 2, 0); assert_equal(BitStream_size(bstream), 3, "Size incorrect. (second append)"); testFinish(); BitStream_free(bstream); }
void test_encodeAnNum(void) { QRinput *input; BitStream *bstream; testStart("Bit length check of alpha-numeric stream. (11 + 12)"); input = QRinput_new(); QRinput_append(input, QR_MODE_AN, 11, (unsigned char *)"ABCDEFGHIJK"); QRinput_append(input, QR_MODE_NUM, 12, (unsigned char *)"123456789012"); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); testEndExp(BitStream_size(bstream) == 128); QRinput_free(input); BitStream_free(bstream); testStart("Bit length check of alphabet stream. (23)"); input = QRinput_new(); QRinput_append(input, QR_MODE_AN, 23, (unsigned char *)"ABCDEFGHIJK123456789012"); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); testEndExp(BitStream_size(bstream) == 140); QRinput_free(input); BitStream_free(bstream); }
static int check_lengthOfCode(QRencodeMode mode, char *data, int size, int version) { QRinput *input; BitStream *b; int bits; int bytes; input = QRinput_new(); QRinput_setVersion(input, version); QRinput_append(input, mode, size, (unsigned char *)data); b = BitStream_new(); QRinput_mergeBitStream(input, b); bits = BitStream_size(b); bytes = QRinput_lengthOfCode(mode, version, bits); QRinput_free(input); BitStream_free(b); return bytes; }
void test_toByte(void) { BitStream *bstream; unsigned char correct[] = { 0x8a, 0xff, 0xff, 0x12, 0x34, 0x56, 0x78 }; unsigned char *result; testStart("Convert to a byte array"); bstream = BitStream_new(); BitStream_appendBytes(bstream, 1, &correct[0]); BitStream_appendBytes(bstream, 2, &correct[1]); BitStream_appendBytes(bstream, 4, &correct[3]); result = BitStream_toByte(bstream); testEnd(memcmp(correct, result, 7)); BitStream_free(bstream); free(result); }
void test_encodeNumeric_versionup(void) { QRinput *stream; BitStream *bstream; char *str; int version; testStart("Encoding numeric stream. (auto-version up test)"); str = (char *)malloc(1050); memset(str, '1', 1050); stream = QRinput_new2(0, QR_ECLEVEL_L); bstream = BitStream_new(); QRinput_append(stream, QR_MODE_NUM, 1050, (unsigned char *)str); QRinput_mergeBitStream(stream, bstream); version = QRinput_getVersion(stream); assert_equal(version, 14, "Version is %d (14 expected).", version); testFinish(); QRinput_free(stream); BitStream_free(bstream); free(str); }
void test_appendNum(void) { BitStream *bstream; char correct[] = "10001010 11111111 11111111 00010010001101000101011001111000"; testStart("Append Num"); bstream = BitStream_new(); BitStream_appendNum(bstream, 8, 0x0000008a); assert_zero(ncmpBin(correct, bstream, 8), "Internal data is incorrect.\n"); BitStream_appendNum(bstream, 16, 0x0000ffff); assert_zero(ncmpBin(correct, bstream, 24), "Internal data is incorrect.\n"); BitStream_appendNum(bstream, 32, 0x12345678); assert_zero(cmpBin(correct, bstream), "Internal data is incorrect.\n"); testFinish(); BitStream_free(bstream); }
void test_insertStructuredAppendHeader(void) { QRinput *stream; char correct[] = "0011000011111010010101000000000101000001"; BitStream *bstream; int ret; testStart("Insert a structured-append header"); stream = QRinput_new(); bstream = BitStream_new(); QRinput_append(stream, QR_MODE_8, 1, (unsigned char *)"A"); ret = QRinput_insertStructuredAppendHeader(stream, 16, 1, 0xa5); assert_zero(ret, "QRinput_insertStructuredAppendHeader() returns nonzero.\n"); QRinput_mergeBitStream(stream, bstream); assert_nonnull(bstream->data, "Bstream->data is null."); assert_zero(cmpBin(correct, bstream), "bitstream is wrong."); testFinish(); QRinput_free(stream); BitStream_free(bstream); }
int mergeAndCheckBStream(int mqr, QRencodeMode mode, char *data, char *correct) { QRinput *input; BitStream *bstream; int ret; if(mqr) { input = QRinput_newMQR(1, QR_ECLEVEL_L); } else { input = QRinput_new(); } QRinput_append(input, mode, strlen(data), (unsigned char *)data); bstream = BitStream_new(); QRinput_mergeBitStream(input, bstream); ret = cmpBin(correct, bstream); QRinput_free(input); BitStream_free(bstream); return ret; }
void test_encode8_versionup(void) { QRinput *stream; BitStream *bstream; char *str; int version; testStart("Encoding 8bit stream. (auto-version up test)"); str = (char *)malloc(2900); memset(str, 0xff, 2900); stream = QRinput_new(); bstream = BitStream_new(); QRinput_append(stream, QR_MODE_8, 2900, (unsigned char *)str); QRinput_mergeBitStream(stream, bstream); version = QRinput_getVersion(stream); assert_equal(version, 40, "Version is %d (40 expected).\n", version); testFinish(); QRinput_free(stream); BitStream_free(bstream); free(str); }
BitStream *BitStream_newWithBits(int size, unsigned char *bits) { BitStream *bstream; if(size < 0) return NULL; if(size == 0) return BitStream_new(); bstream = (BitStream *)malloc(sizeof(BitStream)); if(bstream == NULL) return NULL; bstream->data = (unsigned char *)malloc(size); if(bstream->data == NULL) { free(bstream); return NULL; } bstream->length = size; bstream->datasize = size; memcpy(bstream->data, bits, size); return bstream; }
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 = BitStream_new(); QRinput_mergeBitStream(input, bstream); 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); }