mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-05 16:00:05 +01:00
Restructure Reed-Solomon code for verity FEC and remove unneeded parts.
This commit is contained in:
@@ -9,13 +9,8 @@ libverity_la_SOURCES = \
|
||||
verity_fec.c \
|
||||
verity.c \
|
||||
verity.h \
|
||||
libfec/encode_rs_char.c \
|
||||
libfec/encode_rs.h \
|
||||
libfec/init_rs_char.c \
|
||||
libfec/init_rs.h \
|
||||
libfec/rs-common.h \
|
||||
libfec/char.h \
|
||||
libfec/fec.h
|
||||
rs_encode_char.c \
|
||||
rs.h
|
||||
|
||||
AM_CPPFLAGS = -include config.h \
|
||||
-I$(top_srcdir)/lib \
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
|
||||
*
|
||||
* Copyright 2003, Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
typedef unsigned char data_t;
|
||||
|
||||
#define MODNN(x) modnn(rs,x)
|
||||
|
||||
#define MM (rs->mm)
|
||||
#define NN (rs->nn)
|
||||
#define ALPHA_TO (rs->alpha_to)
|
||||
#define INDEX_OF (rs->index_of)
|
||||
#define GENPOLY (rs->genpoly)
|
||||
#define NROOTS (rs->nroots)
|
||||
#define FCR (rs->fcr)
|
||||
#define PRIM (rs->prim)
|
||||
#define IPRIM (rs->iprim)
|
||||
#define PAD (rs->pad)
|
||||
#define A0 (NN)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/* The guts of the Reed-Solomon encoder, meant to be #included
|
||||
* into a function body with the following typedefs, macros and variables supplied
|
||||
* according to the code parameters:
|
||||
|
||||
* data_t - a typedef for the data symbol
|
||||
* data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
|
||||
* data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
|
||||
* NROOTS - the number of roots in the RS code generator polynomial,
|
||||
* which is the same as the number of parity symbols in a block.
|
||||
Integer variable or literal.
|
||||
*
|
||||
* NN - the total number of symbols in a RS block. Integer variable or literal.
|
||||
* PAD - the number of pad symbols in a block. Integer variable or literal.
|
||||
* ALPHA_TO - The address of an array of NN elements to convert Galois field
|
||||
* elements in index (log) form to polynomial form. Read only.
|
||||
* INDEX_OF - The address of an array of NN elements to convert Galois field
|
||||
* elements in polynomial form to index (log) form. Read only.
|
||||
* MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
|
||||
* GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
|
||||
|
||||
* The memset() and memmove() functions are used. The appropriate header
|
||||
* file declaring these functions (usually <string.h>) must be included by the calling
|
||||
* program.
|
||||
|
||||
* Copyright 2004, Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
|
||||
|
||||
#undef A0
|
||||
#define A0 (NN) /* Special reserved value encoding zero in index form */
|
||||
|
||||
{
|
||||
int i, j;
|
||||
data_t feedback;
|
||||
|
||||
memset(parity,0,NROOTS*sizeof(data_t));
|
||||
|
||||
for(i=0;i<NN-NROOTS-PAD;i++){
|
||||
feedback = INDEX_OF[data[i] ^ parity[0]];
|
||||
if(feedback != A0){ /* feedback term is non-zero */
|
||||
#ifdef UNNORMALIZED
|
||||
/* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
|
||||
* always be for the polynomials constructed by init_rs()
|
||||
*/
|
||||
feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
|
||||
#endif
|
||||
for(j=1;j<NROOTS;j++)
|
||||
parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
|
||||
}
|
||||
/* Shift */
|
||||
memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
|
||||
if(feedback != A0)
|
||||
parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
|
||||
else
|
||||
parity[NROOTS-1] = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
/* Reed-Solomon encoder
|
||||
* Copyright 2002, Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
#include <string.h>
|
||||
|
||||
#include "char.h"
|
||||
#include "rs-common.h"
|
||||
|
||||
void encode_rs_char(void *p,data_t *data, data_t *parity){
|
||||
struct rs *rs = (struct rs *)p;
|
||||
|
||||
#include "encode_rs.h"
|
||||
|
||||
}
|
||||
@@ -1,347 +0,0 @@
|
||||
/* User include file for libfec
|
||||
* Copyright 2004, Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
|
||||
#ifndef _FEC_H_
|
||||
#define _FEC_H_
|
||||
|
||||
/* r=1/2 k=7 convolutional encoder polynomials
|
||||
* The NASA-DSN convention is to use V27POLYA inverted, then V27POLYB
|
||||
* The CCSDS/NASA-GSFC convention is to use V27POLYB, then V27POLYA inverted
|
||||
*/
|
||||
#define V27POLYA 0x6d
|
||||
#define V27POLYB 0x4f
|
||||
|
||||
void *create_viterbi27(int len);
|
||||
void set_viterbi27_polynomial(int polys[2]);
|
||||
int init_viterbi27(void *vp,int starting_state);
|
||||
int update_viterbi27_blk(void *vp,unsigned char sym[],int npairs);
|
||||
int chainback_viterbi27(void *vp, unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27(void *vp);
|
||||
|
||||
#ifdef __VEC__
|
||||
void *create_viterbi27_av(int len);
|
||||
void set_viterbi27_polynomial_av(int polys[2]);
|
||||
int init_viterbi27_av(void *p,int starting_state);
|
||||
int chainback_viterbi27_av(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27_av(void *p);
|
||||
int update_viterbi27_blk_av(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
#ifdef __i386__
|
||||
void *create_viterbi27_mmx(int len);
|
||||
void set_viterbi27_polynomial_mmx(int polys[2]);
|
||||
int init_viterbi27_mmx(void *p,int starting_state);
|
||||
int chainback_viterbi27_mmx(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27_mmx(void *p);
|
||||
int update_viterbi27_blk_mmx(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi27_sse(int len);
|
||||
void set_viterbi27_polynomial_sse(int polys[2]);
|
||||
int init_viterbi27_sse(void *p,int starting_state);
|
||||
int chainback_viterbi27_sse(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27_sse(void *p);
|
||||
int update_viterbi27_blk_sse(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi27_sse2(int len);
|
||||
void set_viterbi27_polynomial_sse2(int polys[2]);
|
||||
int init_viterbi27_sse2(void *p,int starting_state);
|
||||
int chainback_viterbi27_sse2(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27_sse2(void *p);
|
||||
int update_viterbi27_blk_sse2(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
void *create_viterbi27_port(int len);
|
||||
void set_viterbi27_polynomial_port(int polys[2]);
|
||||
int init_viterbi27_port(void *p,int starting_state);
|
||||
int chainback_viterbi27_port(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi27_port(void *p);
|
||||
int update_viterbi27_blk_port(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
/* r=1/2 k=9 convolutional encoder polynomials */
|
||||
#define V29POLYA 0x1af
|
||||
#define V29POLYB 0x11d
|
||||
|
||||
void *create_viterbi29(int len);
|
||||
void set_viterbi29_polynomial(int polys[2]);
|
||||
int init_viterbi29(void *vp,int starting_state);
|
||||
int update_viterbi29_blk(void *vp,unsigned char syms[],int nbits);
|
||||
int chainback_viterbi29(void *vp, unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29(void *vp);
|
||||
|
||||
#ifdef __VEC__
|
||||
void *create_viterbi29_av(int len);
|
||||
void set_viterbi29_polynomial_av(int polys[2]);
|
||||
int init_viterbi29_av(void *p,int starting_state);
|
||||
int chainback_viterbi29_av(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29_av(void *p);
|
||||
int update_viterbi29_blk_av(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
#ifdef __i386__
|
||||
void *create_viterbi29_mmx(int len);
|
||||
void set_viterbi29_polynomial_mmx(int polys[2]);
|
||||
int init_viterbi29_mmx(void *p,int starting_state);
|
||||
int chainback_viterbi29_mmx(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29_mmx(void *p);
|
||||
int update_viterbi29_blk_mmx(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi29_sse(int len);
|
||||
void set_viterbi29_polynomial_sse(int polys[2]);
|
||||
int init_viterbi29_sse(void *p,int starting_state);
|
||||
int chainback_viterbi29_sse(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29_sse(void *p);
|
||||
int update_viterbi29_blk_sse(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi29_sse2(int len);
|
||||
void set_viterbi29_polynomial_sse2(int polys[2]);
|
||||
int init_viterbi29_sse2(void *p,int starting_state);
|
||||
int chainback_viterbi29_sse2(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29_sse2(void *p);
|
||||
int update_viterbi29_blk_sse2(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
void *create_viterbi29_port(int len);
|
||||
void set_viterbi29_polynomial_port(int polys[2]);
|
||||
int init_viterbi29_port(void *p,int starting_state);
|
||||
int chainback_viterbi29_port(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi29_port(void *p);
|
||||
int update_viterbi29_blk_port(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
/* r=1/3 k=9 convolutional encoder polynomials */
|
||||
#define V39POLYA 0x1ed
|
||||
#define V39POLYB 0x19b
|
||||
#define V39POLYC 0x127
|
||||
|
||||
void *create_viterbi39(int len);
|
||||
void set_viterbi39_polynomial(int polys[3]);
|
||||
int init_viterbi39(void *vp,int starting_state);
|
||||
int update_viterbi39_blk(void *vp,unsigned char syms[],int nbits);
|
||||
int chainback_viterbi39(void *vp, unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39(void *vp);
|
||||
|
||||
#ifdef __VEC__
|
||||
void *create_viterbi39_av(int len);
|
||||
void set_viterbi39_polynomial_av(int polys[3]);
|
||||
int init_viterbi39_av(void *p,int starting_state);
|
||||
int chainback_viterbi39_av(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39_av(void *p);
|
||||
int update_viterbi39_blk_av(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
#ifdef __i386__
|
||||
void *create_viterbi39_mmx(int len);
|
||||
void set_viterbi39_polynomial_mmx(int polys[3]);
|
||||
int init_viterbi39_mmx(void *p,int starting_state);
|
||||
int chainback_viterbi39_mmx(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39_mmx(void *p);
|
||||
int update_viterbi39_blk_mmx(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi39_sse(int len);
|
||||
void set_viterbi39_polynomial_sse(int polys[3]);
|
||||
int init_viterbi39_sse(void *p,int starting_state);
|
||||
int chainback_viterbi39_sse(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39_sse(void *p);
|
||||
int update_viterbi39_blk_sse(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi39_sse2(int len);
|
||||
void set_viterbi39_polynomial_sse2(int polys[3]);
|
||||
int init_viterbi39_sse2(void *p,int starting_state);
|
||||
int chainback_viterbi39_sse2(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39_sse2(void *p);
|
||||
int update_viterbi39_blk_sse2(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
void *create_viterbi39_port(int len);
|
||||
void set_viterbi39_polynomial_port(int polys[3]);
|
||||
int init_viterbi39_port(void *p,int starting_state);
|
||||
int chainback_viterbi39_port(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi39_port(void *p);
|
||||
int update_viterbi39_blk_port(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
|
||||
/* r=1/6 k=15 Cassini convolutional encoder polynomials without symbol inversion
|
||||
* dfree = 56
|
||||
* These bits may be left-right flipped from some textbook representations;
|
||||
* here I have the bits entering the shift register from the right (low) end
|
||||
*
|
||||
* Some other spacecraft use the same code, but with the polynomials in a different order.
|
||||
* E.g., Mars Pathfinder and STEREO swap POLYC and POLYD. All use alternate symbol inversion,
|
||||
* so use set_viterbi615_polynomial() as appropriate.
|
||||
*/
|
||||
#define V615POLYA 042631
|
||||
#define V615POLYB 047245
|
||||
#define V615POLYC 056507
|
||||
#define V615POLYD 073363
|
||||
#define V615POLYE 077267
|
||||
#define V615POLYF 064537
|
||||
|
||||
void *create_viterbi615(int len);
|
||||
void set_viterbi615_polynomial(int polys[6]);
|
||||
int init_viterbi615(void *vp,int starting_state);
|
||||
int update_viterbi615_blk(void *vp,unsigned char *syms,int nbits);
|
||||
int chainback_viterbi615(void *vp, unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615(void *vp);
|
||||
|
||||
#ifdef __VEC__
|
||||
void *create_viterbi615_av(int len);
|
||||
void set_viterbi615_polynomial_av(int polys[6]);
|
||||
int init_viterbi615_av(void *p,int starting_state);
|
||||
int chainback_viterbi615_av(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615_av(void *p);
|
||||
int update_viterbi615_blk_av(void *p,unsigned char *syms,int nbits);
|
||||
#endif
|
||||
|
||||
#ifdef __i386__
|
||||
void *create_viterbi615_mmx(int len);
|
||||
void set_viterbi615_polynomial_mmx(int polys[6]);
|
||||
int init_viterbi615_mmx(void *p,int starting_state);
|
||||
int chainback_viterbi615_mmx(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615_mmx(void *p);
|
||||
int update_viterbi615_blk_mmx(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi615_sse(int len);
|
||||
void set_viterbi615_polynomial_sse(int polys[6]);
|
||||
int init_viterbi615_sse(void *p,int starting_state);
|
||||
int chainback_viterbi615_sse(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615_sse(void *p);
|
||||
int update_viterbi615_blk_sse(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
void *create_viterbi615_sse2(int len);
|
||||
void set_viterbi615_polynomial_sse2(int polys[6]);
|
||||
int init_viterbi615_sse2(void *p,int starting_state);
|
||||
int chainback_viterbi615_sse2(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615_sse2(void *p);
|
||||
int update_viterbi615_blk_sse2(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
#endif
|
||||
|
||||
void *create_viterbi615_port(int len);
|
||||
void set_viterbi615_polynomial_port(int polys[6]);
|
||||
int init_viterbi615_port(void *p,int starting_state);
|
||||
int chainback_viterbi615_port(void *p,unsigned char *data,unsigned int nbits,unsigned int endstate);
|
||||
void delete_viterbi615_port(void *p);
|
||||
int update_viterbi615_blk_port(void *p,unsigned char *syms,int nbits);
|
||||
|
||||
|
||||
/* General purpose RS codec, 8-bit symbols */
|
||||
void encode_rs_char(void *rs,unsigned char *data,unsigned char *parity);
|
||||
int decode_rs_char(void *rs,unsigned char *data,int *eras_pos,
|
||||
int no_eras);
|
||||
void *init_rs_char(int symsize,int gfpoly,
|
||||
int fcr,int prim,int nroots,
|
||||
int pad);
|
||||
void free_rs_char(void *rs);
|
||||
|
||||
/* General purpose RS codec, integer symbols */
|
||||
void encode_rs_int(void *rs,int *data,int *parity);
|
||||
int decode_rs_int(void *rs,int *data,int *eras_pos,int no_eras);
|
||||
void *init_rs_int(int symsize,int gfpoly,int fcr,
|
||||
int prim,int nroots,int pad);
|
||||
void free_rs_int(void *rs);
|
||||
|
||||
/* CCSDS standard (255,223) RS codec with conventional (*not* dual-basis)
|
||||
* symbol representation
|
||||
*/
|
||||
void encode_rs_8(unsigned char *data,unsigned char *parity,int pad);
|
||||
int decode_rs_8(unsigned char *data,int *eras_pos,int no_eras,int pad);
|
||||
|
||||
/* CCSDS standard (255,223) RS codec with dual-basis symbol representation */
|
||||
void encode_rs_ccsds(unsigned char *data,unsigned char *parity,int pad);
|
||||
int decode_rs_ccsds(unsigned char *data,int *eras_pos,int no_eras,int pad);
|
||||
|
||||
/* Tables to map from conventional->dual (Taltab) and
|
||||
* dual->conventional (Tal1tab) bases
|
||||
*/
|
||||
extern unsigned char Taltab[],Tal1tab[];
|
||||
|
||||
|
||||
/* CPU SIMD instruction set available */
|
||||
extern enum cpu_mode {UNKNOWN=0,PORT,MMX,SSE,SSE2,ALTIVEC} Cpu_mode;
|
||||
void find_cpu_mode(void); /* Call this once at startup to set Cpu_mode */
|
||||
|
||||
/* Determine parity of argument: 1 = odd, 0 = even */
|
||||
#ifdef __i386__
|
||||
static inline int parityb(unsigned char x){
|
||||
__asm__ __volatile__ ("test %1,%1;setpo %0" : "=g"(x) : "r" (x));
|
||||
return x;
|
||||
}
|
||||
#else
|
||||
void partab_init();
|
||||
|
||||
static inline int parityb(unsigned char x){
|
||||
extern unsigned char Partab[256];
|
||||
extern int P_init;
|
||||
if(!P_init){
|
||||
partab_init();
|
||||
}
|
||||
return Partab[x];
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
static inline int parity(int x){
|
||||
/* Fold down to one byte */
|
||||
x ^= (x >> 16);
|
||||
x ^= (x >> 8);
|
||||
return parityb(x);
|
||||
}
|
||||
|
||||
/* Useful utilities for simulation */
|
||||
double normal_rand(double mean, double std_dev);
|
||||
unsigned char addnoise(int sym,double amp,double gain,double offset,int clip);
|
||||
|
||||
extern int Bitcnt[];
|
||||
|
||||
/* Dot product functions */
|
||||
void *initdp(signed short coeffs[],int len);
|
||||
void freedp(void *dp);
|
||||
long dotprod(void *dp,signed short a[]);
|
||||
|
||||
void *initdp_port(signed short coeffs[],int len);
|
||||
void freedp_port(void *dp);
|
||||
long dotprod_port(void *dp,signed short a[]);
|
||||
|
||||
#ifdef __i386__
|
||||
void *initdp_mmx(signed short coeffs[],int len);
|
||||
void freedp_mmx(void *dp);
|
||||
long dotprod_mmx(void *dp,signed short a[]);
|
||||
|
||||
void *initdp_sse(signed short coeffs[],int len);
|
||||
void freedp_sse(void *dp);
|
||||
long dotprod_sse(void *dp,signed short a[]);
|
||||
|
||||
void *initdp_sse2(signed short coeffs[],int len);
|
||||
void freedp_sse2(void *dp);
|
||||
long dotprod_sse2(void *dp,signed short a[]);
|
||||
#endif
|
||||
|
||||
#ifdef __VEC__
|
||||
void *initdp_av(signed short coeffs[],int len);
|
||||
void freedp_av(void *dp);
|
||||
long dotprod_av(void *dp,signed short a[]);
|
||||
#endif
|
||||
|
||||
/* Sum of squares - accepts signed shorts, produces unsigned long long */
|
||||
unsigned long long sumsq(signed short *in,int cnt);
|
||||
unsigned long long sumsq_port(signed short *in,int cnt);
|
||||
|
||||
#ifdef __i386__
|
||||
unsigned long long sumsq_mmx(signed short *in,int cnt);
|
||||
unsigned long long sumsq_sse(signed short *in,int cnt);
|
||||
unsigned long long sumsq_sse2(signed short *in,int cnt);
|
||||
#endif
|
||||
#ifdef __VEC__
|
||||
unsigned long long sumsq_av(signed short *in,int cnt);
|
||||
#endif
|
||||
|
||||
|
||||
/* Low-level data structures and routines */
|
||||
|
||||
int cpu_features(void);
|
||||
|
||||
#endif /* _FEC_H_ */
|
||||
|
||||
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
/* Common code for intializing a Reed-Solomon control block (char or int symbols)
|
||||
* Copyright 2004 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
#undef NULL
|
||||
#define NULL ((void *)0)
|
||||
|
||||
{
|
||||
int i, j, sr,root,iprim;
|
||||
|
||||
rs = NULL;
|
||||
/* Check parameter ranges */
|
||||
if(symsize < 0 || symsize > 8*(int)sizeof(data_t)){
|
||||
goto done;
|
||||
}
|
||||
|
||||
if(fcr < 0 || fcr >= (1<<symsize))
|
||||
goto done;
|
||||
if(prim <= 0 || prim >= (1<<symsize))
|
||||
goto done;
|
||||
if(nroots < 0 || nroots >= (1<<symsize))
|
||||
goto done; /* Can't have more roots than symbol values! */
|
||||
if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
|
||||
goto done; /* Too much padding */
|
||||
|
||||
rs = (struct rs *)calloc(1,sizeof(struct rs));
|
||||
if(rs == NULL)
|
||||
goto done;
|
||||
|
||||
rs->mm = symsize;
|
||||
rs->nn = (1<<symsize)-1;
|
||||
rs->pad = pad;
|
||||
|
||||
rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
|
||||
if(rs->alpha_to == NULL){
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
|
||||
if(rs->index_of == NULL){
|
||||
free(rs->alpha_to);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Generate Galois field lookup tables */
|
||||
rs->index_of[0] = A0; /* log(zero) = -inf */
|
||||
rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
|
||||
sr = 1;
|
||||
for(i=0;i<rs->nn;i++){
|
||||
rs->index_of[sr] = i;
|
||||
rs->alpha_to[i] = sr;
|
||||
sr <<= 1;
|
||||
if(sr & (1<<symsize))
|
||||
sr ^= gfpoly;
|
||||
sr &= rs->nn;
|
||||
}
|
||||
if(sr != 1){
|
||||
/* field generator polynomial is not primitive! */
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Form RS code generator polynomial from its roots */
|
||||
rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
|
||||
if(rs->genpoly == NULL){
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
rs = NULL;
|
||||
goto done;
|
||||
}
|
||||
rs->fcr = fcr;
|
||||
rs->prim = prim;
|
||||
rs->nroots = nroots;
|
||||
|
||||
/* Find prim-th root of 1, used in decoding */
|
||||
for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
|
||||
;
|
||||
rs->iprim = iprim / prim;
|
||||
|
||||
rs->genpoly[0] = 1;
|
||||
for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
|
||||
rs->genpoly[i+1] = 1;
|
||||
|
||||
/* Multiply rs->genpoly[] by @**(root + x) */
|
||||
for (j = i; j > 0; j--){
|
||||
if (rs->genpoly[j] != 0)
|
||||
rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
|
||||
else
|
||||
rs->genpoly[j] = rs->genpoly[j-1];
|
||||
}
|
||||
/* rs->genpoly[0] can never be zero */
|
||||
rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
|
||||
}
|
||||
/* convert rs->genpoly[] to index form for quicker encoding */
|
||||
for (i = 0; i <= nroots; i++)
|
||||
rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
|
||||
done:;
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/* Initialize a RS codec
|
||||
*
|
||||
* Copyright 2002 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "char.h"
|
||||
#include "rs-common.h"
|
||||
|
||||
void free_rs_char(void *p){
|
||||
struct rs *rs = (struct rs *)p;
|
||||
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs->genpoly);
|
||||
free(rs);
|
||||
}
|
||||
|
||||
/* Initialize a Reed-Solomon codec
|
||||
* symsize = symbol size, bits
|
||||
* gfpoly = Field generator polynomial coefficients
|
||||
* fcr = first root of RS code generator polynomial, index form
|
||||
* prim = primitive element to generate polynomial roots
|
||||
* nroots = RS code generator polynomial degree (number of roots)
|
||||
* pad = padding bytes at front of shortened block
|
||||
*/
|
||||
void *init_rs_char(int symsize,int gfpoly,int fcr,int prim,
|
||||
int nroots,int pad){
|
||||
struct rs *rs;
|
||||
|
||||
#include "init_rs.h"
|
||||
|
||||
return rs;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
/* Stuff common to all the general-purpose Reed-Solomon codecs
|
||||
* Copyright 2004 Phil Karn, KA9Q
|
||||
* May be used under the terms of the GNU Lesser General Public License (LGPL)
|
||||
*/
|
||||
|
||||
/* Reed-Solomon codec control block */
|
||||
struct rs {
|
||||
int mm; /* Bits per symbol */
|
||||
int nn; /* Symbols per block (= (1<<mm)-1) */
|
||||
data_t *alpha_to; /* log lookup table */
|
||||
data_t *index_of; /* Antilog lookup table */
|
||||
data_t *genpoly; /* Generator polynomial */
|
||||
int nroots; /* Number of generator roots = number of parity symbols */
|
||||
int fcr; /* First consecutive root, index form */
|
||||
int prim; /* Primitive element, index form */
|
||||
int iprim; /* prim-th root of 1, index form */
|
||||
int pad; /* Padding bytes in shortened block */
|
||||
};
|
||||
|
||||
static inline int modnn(struct rs *rs,int x){
|
||||
while (x >= rs->nn) {
|
||||
x -= rs->nn;
|
||||
x = (x >> rs->mm) + (x & rs->nn);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
35
lib/verity/rs.h
Normal file
35
lib/verity/rs.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Reed-Solomon codecs, based on libfec
|
||||
*
|
||||
* Copyright (C) 2004 Phil Karn, KA9Q
|
||||
* libcryptsetup modifications
|
||||
* Copyright (C) 2017, Red Hat, Inc. All rights reserved.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this file; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#ifndef _LIBFEC_RS_H
|
||||
#define _LIBFEC_RS_H
|
||||
|
||||
typedef unsigned char data_t;
|
||||
struct rs;
|
||||
|
||||
struct rs *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad);
|
||||
void free_rs_char(struct rs *rs);
|
||||
|
||||
/* General purpose RS codec, 8-bit symbols */
|
||||
void encode_rs_char(struct rs *rs, data_t *data, data_t *parity);
|
||||
|
||||
#endif
|
||||
199
lib/verity/rs_encode_char.c
Normal file
199
lib/verity/rs_encode_char.c
Normal file
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Reed-Solomon encoder, based on libfec
|
||||
*
|
||||
* Copyright (C) 2002, Phil Karn, KA9Q
|
||||
* libcryptsetup modifications
|
||||
* Copyright (C) 2017, Red Hat, Inc. All rights reserved.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this file; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "rs.h"
|
||||
|
||||
/* Special reserved value encoding zero in index form. */
|
||||
#define A0 (rs->nn)
|
||||
|
||||
/* Reed-Solomon codec control block */
|
||||
struct rs {
|
||||
int mm; /* Bits per symbol */
|
||||
int nn; /* Symbols per block (= (1<<mm)-1) */
|
||||
data_t *alpha_to;/* log lookup table */
|
||||
data_t *index_of;/* Antilog lookup table */
|
||||
data_t *genpoly; /* Generator polynomial */
|
||||
int nroots; /* Number of generator roots = number of parity symbols */
|
||||
int fcr; /* First consecutive root, index form */
|
||||
int prim; /* Primitive element, index form */
|
||||
int iprim; /* prim-th root of 1, index form */
|
||||
int pad; /* Padding bytes in shortened block */
|
||||
};
|
||||
|
||||
static inline int modnn(struct rs *rs, int x)
|
||||
{
|
||||
while (x >= rs->nn) {
|
||||
x -= rs->nn;
|
||||
x = (x >> rs->mm) + (x & rs->nn);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Initialize a Reed-Solomon codec
|
||||
* symsize = symbol size, bits
|
||||
* gfpoly = Field generator polynomial coefficients
|
||||
* fcr = first root of RS code generator polynomial, index form
|
||||
* prim = primitive element to generate polynomial roots
|
||||
* nroots = RS code generator polynomial degree (number of roots)
|
||||
* pad = padding bytes at front of shortened block
|
||||
*/
|
||||
struct rs *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
|
||||
{
|
||||
struct rs *rs;
|
||||
int i, j, sr, root, iprim;
|
||||
|
||||
/* Check parameter ranges */
|
||||
if (symsize < 0 || symsize > 8 * (int)sizeof(data_t))
|
||||
return NULL;
|
||||
if (fcr < 0 || fcr >= (1<<symsize))
|
||||
return NULL;
|
||||
if (prim <= 0 || prim >= (1<<symsize))
|
||||
return NULL;
|
||||
if (nroots < 0 || nroots >= (1<<symsize))
|
||||
return NULL; /* Can't have more roots than symbol values! */
|
||||
|
||||
if (pad < 0 || pad >= ((1<<symsize) - 1 - nroots))
|
||||
return NULL; /* Too much padding */
|
||||
|
||||
rs = calloc(1, sizeof(struct rs));
|
||||
if (rs == NULL)
|
||||
return NULL;
|
||||
|
||||
rs->mm = symsize;
|
||||
rs->nn = (1<<symsize) - 1;
|
||||
rs->pad = pad;
|
||||
|
||||
rs->alpha_to = malloc(sizeof(data_t) * (rs->nn + 1));
|
||||
if (rs->alpha_to == NULL) {
|
||||
free(rs);
|
||||
return NULL;
|
||||
}
|
||||
rs->index_of = malloc(sizeof(data_t) * (rs->nn + 1));
|
||||
if (rs->index_of == NULL) {
|
||||
free(rs->alpha_to);
|
||||
free(rs);
|
||||
return NULL;
|
||||
}
|
||||
memset(rs->index_of, 0, sizeof(data_t) * (rs->nn + 1));
|
||||
|
||||
/* Generate Galois field lookup tables */
|
||||
rs->index_of[0] = A0; /* log(zero) = -inf */
|
||||
rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
|
||||
sr = 1;
|
||||
for (i = 0; i < rs->nn; i++) {
|
||||
rs->index_of[sr] = i;
|
||||
rs->alpha_to[i] = sr;
|
||||
sr <<= 1;
|
||||
if(sr & (1<<symsize))
|
||||
sr ^= gfpoly;
|
||||
sr &= rs->nn;
|
||||
}
|
||||
if (sr != 1) {
|
||||
/* field generator polynomial is not primitive! */
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Form RS code generator polynomial from its roots */
|
||||
rs->genpoly = malloc(sizeof(data_t) * (nroots + 1));
|
||||
if (rs->genpoly == NULL) {
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rs->fcr = fcr;
|
||||
rs->prim = prim;
|
||||
rs->nroots = nroots;
|
||||
|
||||
/* Find prim-th root of 1, used in decoding */
|
||||
for (iprim = 1; (iprim % prim) != 0; iprim += rs->nn)
|
||||
;
|
||||
rs->iprim = iprim / prim;
|
||||
|
||||
rs->genpoly[0] = 1;
|
||||
for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
|
||||
rs->genpoly[i + 1] = 1;
|
||||
|
||||
/* Multiply rs->genpoly[] by @**(root + x) */
|
||||
for (j = i; j > 0; j--){
|
||||
if (rs->genpoly[j] != 0)
|
||||
rs->genpoly[j] = rs->genpoly[j - 1] ^ rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[j]] + root)];
|
||||
else
|
||||
rs->genpoly[j] = rs->genpoly[j - 1];
|
||||
}
|
||||
/* rs->genpoly[0] can never be zero */
|
||||
rs->genpoly[0] = rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[0]] + root)];
|
||||
}
|
||||
/* convert rs->genpoly[] to index form for quicker encoding */
|
||||
for (i = 0; i <= nroots; i++)
|
||||
rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
void free_rs_char(struct rs *rs)
|
||||
{
|
||||
if (!rs)
|
||||
return;
|
||||
|
||||
free(rs->alpha_to);
|
||||
free(rs->index_of);
|
||||
free(rs->genpoly);
|
||||
free(rs);
|
||||
}
|
||||
|
||||
void encode_rs_char(struct rs *rs, data_t *data, data_t *parity)
|
||||
{
|
||||
int i, j;
|
||||
data_t feedback;
|
||||
|
||||
memset(parity, 0, rs->nroots * sizeof(data_t));
|
||||
|
||||
for (i = 0; i < rs->nroots - rs->pad; i++) {
|
||||
feedback = rs->index_of[data[i] ^ parity[0]];
|
||||
if (feedback != A0) {
|
||||
/* feedback term is non-zero */
|
||||
#ifdef UNNORMALIZED
|
||||
/* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
|
||||
* always be for the polynomials constructed by init_rs() */
|
||||
feedback = modnn(rs, rs->nn - rs->genpoly[rs->nroots] + feedback);
|
||||
#endif
|
||||
for(j = 1; j < rs->nroots; j++)
|
||||
parity[j] ^= rs->alpha_to[modnn(rs, feedback + rs->genpoly[rs->nroots - j])];
|
||||
}
|
||||
|
||||
/* Shift */
|
||||
memmove(&parity[0], &parity[1], sizeof(data_t) * (rs->nroots - 1));
|
||||
|
||||
if (feedback != A0)
|
||||
parity[rs->nroots - 1] = rs->alpha_to[modnn(rs, feedback + rs->genpoly[0])];
|
||||
else
|
||||
parity[rs->nroots - 1] = 0;
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "verity.h"
|
||||
#include "internal.h"
|
||||
#include "libfec/fec.h"
|
||||
#include "rs.h"
|
||||
|
||||
/* ecc parameters */
|
||||
#define FEC_RSM 255
|
||||
|
||||
Reference in New Issue
Block a user