static void compareRS(unsigned char data[]) { int i, j; RS *rs; int spec[5]; int dl, el; unsigned char ecc_expected[256], ecc_rscodec[256]; for(i = 1; i <= QRSPEC_VERSION_MAX; i++) { for(j = QR_ECLEVEL_L; j <= QR_ECLEVEL_H; j++) { QRspec_getEccSpec(i, (QRecLevel)j, spec); dl = QRspec_rsDataCodes1(spec); el = QRspec_rsEccCodes1(spec); rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el); RSECC_encode(dl, el, data, ecc_rscodec); encode_rs_char(rs, data, ecc_expected); assert_zero(memcmp(ecc_expected, ecc_rscodec, el), "Invalid ECC found: length %d.\n", el); free_rs_char(rs); dl = QRspec_rsDataCodes2(spec); el = QRspec_rsEccCodes2(spec); if(dl != 0) { rs = init_rs(8, 0x11d, 0, 1, el, 255 - dl - el); RSECC_encode(dl, el, data, ecc_rscodec); encode_rs_char(rs, data, ecc_expected); assert_zero(memcmp(ecc_expected, ecc_rscodec, el), "Invalid ECC found: length %d.\n", el); free_rs_char(rs); } } } }
static void RSblock_initBlock(RSblock *block, int dl, unsigned char *data, int el, unsigned char *ecc, RS *rs) { block->dataLength = dl; block->data = data; block->eccLength = el; block->ecc = ecc; encode_rs_char(rs, data, ecc); }
static void encode_rs(struct image_proc_ctx *ctx) { struct image *fcx = ctx->ctx; int j; uint8_t data[fcx->rs_n]; uint64_t i; for (i = ctx->start; i < ctx->end; i += fcx->rs_n) { for (j = 0; j < fcx->rs_n; ++j) { data[j] = image_get_interleaved_byte(i + j, fcx); } encode_rs_char(ctx->rs, data, &fcx->fec[ctx->fec_pos]); ctx->fec_pos += fcx->roots; } }
void fec_rs_encode(fec _q, unsigned int _dec_msg_len, unsigned char *_msg_dec, unsigned char *_msg_enc) { // validate input if (_dec_msg_len == 0) { fprintf(stderr,"error: fec_rs_encode(), input lenght must be > 0\n"); exit(1); } // re-allocate resources if necessary fec_rs_setlength(_q, _dec_msg_len); unsigned int i; unsigned int n0=0; // input index unsigned int n1=0; // output index unsigned int block_size = _q->dec_block_len; for (i=0; i<_q->num_blocks; i++) { // the last block is smaller by the residual block length if (i == _q->num_blocks-1) block_size -= _q->res_block_len; // copy sequence memmove(_q->tblock, &_msg_dec[n0], block_size*sizeof(unsigned char)); // last block: we could pad end with zeros, but it's not really // necessary as these bits are going to be thrown away anyway // encode data, appending parity bits to end of sequence encode_rs_char(_q->rs, _q->tblock, &_q->tblock[_q->dec_block_len]); // copy result to output memmove(&_msg_enc[n1], _q->tblock, _q->enc_block_len*sizeof(unsigned char)); // increment counters n0 += block_size; n1 += _q->enc_block_len; } // sanity check assert( n0 == _q->num_dec_bytes ); assert( n1 == _q->num_enc_bytes ); }
int main(){ unsigned char block[255]; int i; void *rs; struct rusage start,finish; double extime; int trials = 10000; for(i=0;i<223;i++) block[i] = 0x01; rs = init_rs_char(8,0x187,112,11,32,0); encode_rs_char(rs,block,&block[223]); getrusage(RUSAGE_SELF,&start); for(i=0;i<trials;i++){ #if 0 block[0] ^= 0xff; /* Introduce an error */ block[2] ^= 0xff; /* Introduce an error */ #endif decode_rs_char(rs,block,NULL,0); } getrusage(RUSAGE_SELF,&finish); extime = finish.ru_utime.tv_sec - start.ru_utime.tv_sec + 1e-6*(finish.ru_utime.tv_usec - start.ru_utime.tv_usec); printf("Execution time for %d Reed-Solomon blocks using general decoder: %.2f sec\n",trials,extime); printf("decoder speed: %g bits/s\n",trials*223*8/extime); encode_rs_8(block,&block[223],0); getrusage(RUSAGE_SELF,&start); for(i=0;i<trials;i++){ #if 0 block[0] ^= 0xff; /* Introduce an error */ block[2] ^= 0xff; /* Introduce an error */ #endif decode_rs_8(block,NULL,0,0); } getrusage(RUSAGE_SELF,&finish); extime = finish.ru_utime.tv_sec - start.ru_utime.tv_sec + 1e-6*(finish.ru_utime.tv_usec - start.ru_utime.tv_usec); printf("Execution time for %d Reed-Solomon blocks using CCSDS decoder: %.2f sec\n",trials,extime); printf("decoder speed: %g bits/s\n",trials*223*8/extime); exit(0); }
void test_char() { int nn = 255; int roots = 8; int kk = nn - roots; int i; int r; int nerr = 0; unsigned char block[nn]; unsigned char blocktmp[nn]; int pad = 215; void *rs; printf("Using RS(%d, %d), it means words of %d 8-bit symbols and %d redundant symbols\n", nn, kk, kk - pad, roots); rs = init_rs_char(8, 0x187, 112, 11, roots, pad); memset(block, 0, nn); for (i = 0; i < kk - pad; ++i) block[i] = 'a' + i % 26; dump_buf("dump-before", block, nn); encode_rs_char(rs, block, &block[kk - pad]); dump_buf("dump-after", block, nn); do { nerr ++; memcpy(blocktmp, block, nn); for (i = 0; i < nerr; ++i) blocktmp[i] = ~blocktmp[i]; if (nerr == 16) dump_buf("dump-decode-16-before", blocktmp, nn); r = decode_rs_char(rs, blocktmp, NULL, 0); if (nerr == 16) dump_buf("dump-decode-16-after", blocktmp, nn); printf("decode with %d bad symbols returns %d\n", nerr, r); } while (r >= 0 && nerr < 18); }