From 761de18ba52e1dc0ce0c85c30eec7b79db9a09a0 Mon Sep 17 00:00:00 2001
From: wangyu- <wangyucn@gmail.com>
Date: Thu, 14 Sep 2017 11:57:27 -0500
Subject: [PATCH] implemented new interface for fec.h fec.c in rs.h and rs.c

---
 lib/fec.c | 11 ++++++++++-
 lib/fec.h |  5 ++++-
 lib/rs.c  | 36 ++++++++++++++++++++++++++++++++++++
 lib/rs.h  | 31 ++++++++++++++++++++++++++++---
 4 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/lib/fec.c b/lib/fec.c
index 46eba27..4097e98 100644
--- a/lib/fec.c
+++ b/lib/fec.c
@@ -879,7 +879,16 @@ fec_decode(void *code0, void *pkt0[], int index[], int sz)
 
     return 0;
 }
-
+int get_n(void *code0)
+{
+	struct fec_parms * code= (struct fec_parms *)code0;
+	return code->n;
+}
+int get_k(void *code0)
+{
+	struct fec_parms * code= (struct fec_parms *)code0;
+	return code->k;
+}
 /*********** end of FEC code -- beginning of test code ************/
 
 #if (TEST || DEBUG)
diff --git a/lib/fec.h b/lib/fec.h
index 8d298d2..871af50 100644
--- a/lib/fec.h
+++ b/lib/fec.h
@@ -44,10 +44,13 @@
 
 #define	GF_SIZE ((1 << GF_BITS) - 1)	/* powers of \alpha */
 void fec_free(void *p) ;
-void * fec_new(int k, int n) ;
+void * fec_new(int k, int n) ;//n>=k
 
 void init_fec() ;
 void fec_encode(void *code, void *src[], void *dst, int index, int sz) ;
 int fec_decode(void *code, void *pkt[], int index[], int sz) ;
 
+int get_k(void *code);
+int get_n(void *codev);
+
 /* end of file */
diff --git a/lib/rs.c b/lib/rs.c
index 8b1cd46..aaf8a02 100644
--- a/lib/rs.c
+++ b/lib/rs.c
@@ -4,5 +4,41 @@
  *  Created on: Sep 14, 2017
  *      Author: root
  */
+#include "rs.h"
 
+void rs_encode(void *code,void *data[],int size)
+{
+	int k=get_k(code);
+	int n=get_n(code);
+	for(int i=k;i<n;i++)
+	{
+		fec_encode(code, data, data[i],i, size);
+	}
 
+	return ;
+}
+
+int rs_decode(void *code,void *data[],int size)
+{
+	int k=get_k(code);
+	int n=get_n(code);
+	int index[n];
+	int count=0;
+	for(int i=0;i<n;i++)
+	{
+		if(data[i]!=0)
+		{
+			index[count++]=i;
+		}
+	}
+	if(count<k)
+		return -1;
+	for(int i=0;i<n;i++)
+	{
+		if(k<count)
+			data[i]=data[index[i]];
+		else
+			data[i]=0;
+	}
+	return fec_decode(code,data,index,size);
+}
diff --git a/lib/rs.h b/lib/rs.h
index 010a115..1e7fa32 100644
--- a/lib/rs.h
+++ b/lib/rs.h
@@ -10,12 +10,37 @@
 
 #include "fec.h"
 
+// input:
+// code, generated by fec_new() function from fec.h
+// data[0....k-1 ], points to original data
+// size, data length
+//
+// output:
+// data[k....n-1], points to generated redundant data
+//
+// info:
+// the function will always succeed,except malloc fail.if malloc fail,it will call exit()
+void rs_encode(void *code,char *data[],int size); 
+
+
+// input:
+// data[0.....n-1] points to original data and redundate data,in right order
+// if data[i] is missing ,set poniter data[i] to 0 (point it to null)
+//
+// outout:
+// data[0.....k-1] will point to the recovered original data.
+//
+// info:
+// return zero on success
+// if the number of no-zero pointers is less than k,the function will fail and return non-zero
+//
+// advanced info:
+// 1. rs_decode wont malloc memory for those zero pointers in data[0.....k-1]. instead it will re-use the memory of other non-zero pointers (and let data[0.....k-1] point to those memory).
+// 2. if the input data[0.....n-1] contains x non-zero pointers,after called rs_decode,there will still be exactly x non-zero poninters in data[0.....n-1],just the order may change.
+int rs_decode(void *code,char *data[],int size);
 
-int rs_encode(void *code,void *data[],int size);
 
-int rs_recover_data(void *code,void *data[],int size);
 
-int rs_recover_all(void *code,void *data[],int size);