finished aztec code generator.

fixes #10
This commit is contained in:
boombuler committed 2016-12-26 22:16:28 +01:00
1 parent 3dbe04fe04
commit d039b6bd02
14 files changed
+642 -160

No files matched your search

+7 -47
View File
@@ -5,58 +5,18 @@ import (
)
type errorCorrection struct {
fld *utils.GaloisField
polynomes map[int][]int
rs *utils.ReedSolomonEncoder
}
var ec *errorCorrection = newErrorCorrection()
func newErrorCorrection() *errorCorrection {
result := new(errorCorrection)
result.fld = utils.NewGaloisField(301)
result.polynomes = make(map[int][]int)
return result
}
gf := utils.NewGaloisField(301, 256, 1)
func (ec *errorCorrection) getPolynomial(count int) []int {
poly, ok := ec.polynomes[count]
if !ok {
idx := 1
poly = make([]int, count+1)
poly[0] = 1
for i := 1; i <= count; i++ {
poly[i] = 1
for j := i - 1; j > 0; j-- {
if poly[j] != 0 {
poly[j] = ec.fld.ALogTbl[(int(ec.fld.LogTbl[poly[j]])+idx)%255]
}
poly[j] = ec.fld.AddOrSub(poly[j], poly[j-1])
}
poly[0] = ec.fld.ALogTbl[(int(ec.fld.LogTbl[poly[0]])+idx)%255]
idx++
}
poly = poly[0:count]
ec.polynomes[count] = poly
}
return poly
}
func (ec *errorCorrection) calcECCBlock(data []byte, poly []int) []byte {
ecc := make([]byte, len(poly)+1)
for i := 0; i < len(data); i++ {
k := ec.fld.AddOrSub(int(ecc[0]), int(data[i]))
for j := 0; j < len(ecc)-1; j++ {
ecc[j] = byte(ec.fld.AddOrSub(int(ecc[j+1]), ec.fld.Multiply(k, poly[len(ecc)-j-2])))
}
}
return ecc
return &errorCorrection{utils.NewReedSolomonEncoder(gf)}
}
func (ec *errorCorrection) calcECC(data []byte, size *dmCodeSize) []byte {
poly := ec.getPolynomial(size.ErrorCorrectionCodewordsPerBlock())
dataSize := len(data)
// make some space for error correction codes
data = append(data, make([]byte, size.ECCCount)...)
@@ -64,19 +24,19 @@ func (ec *errorCorrection) calcECC(data []byte, size *dmCodeSize) []byte {
for block := 0; block < size.BlockCount; block++ {
dataCnt := size.DataCodewordsForBlock(block)
buff := make([]byte, dataCnt)
buff := make([]int, dataCnt)
// copy the data for the current block to buff
j := 0
for i := block; i < dataSize; i += size.BlockCount {
buff[j] = data[i]
buff[j] = int(data[i])
j++
}
// calc the error correction codes
ecc := ec.calcECCBlock(buff, poly)
ecc := ec.rs.Encode(buff, size.ErrorCorrectionCodewordsPerBlock())
// and append them to the result
j = 0
for i := block; i < size.ErrorCorrectionCodewordsPerBlock()*size.BlockCount; i += size.BlockCount {
data[dataSize+i] = ecc[j]
data[dataSize+i] = byte(ecc[j])
j++
}
}
-33
View File
@@ -5,39 +5,6 @@ import (
"testing"
)
func Test_GetPolynomial(t *testing.T) {
var gf_polys map[int][]int = map[int][]int{
5: []int{228, 48, 15, 111, 62},
7: []int{23, 68, 144, 134, 240, 92, 254},
10: []int{28, 24, 185, 166, 223, 248, 116, 255, 110, 61},
11: []int{175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120},
12: []int{41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242},
14: []int{156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185},
18: []int{83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, 90, 188},
20: []int{15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27, 174, 186, 172},
24: []int{52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172, 254, 124, 12, 181, 184, 96, 50, 193},
28: []int{211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, 17, 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255},
36: []int{245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80, 182, 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25, 225, 98, 81, 112},
42: []int{77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, 175, 95, 100, 9, 167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, 248, 202, 69, 50, 150, 177, 226, 5, 9, 5},
48: []int{245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237, 87, 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138, 186, 240, 82, 44, 176, 87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19},
56: []int{175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 235, 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 232, 144, 238, 22, 150, 201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 28, 155, 43, 203, 107, 233, 53, 143, 46},
62: []int{242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196, 37, 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114, 71, 161, 44, 147, 6, 27, 218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, 176, 170, 4, 107, 232, 7, 94, 166, 224, 124, 86, 47, 11, 204},
68: []int{220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127, 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227, 236, 66, 139, 153, 185, 202, 167, 179, 25, 220, 232, 96, 210, 231, 136, 223, 239, 181, 241, 59, 52, 172, 25, 49, 232, 211, 189, 64, 54, 108, 153, 132, 63, 96, 103, 82, 186},
}
for i, tst := range gf_polys {
res := ec.getPolynomial(i)
if len(res) != len(tst) {
t.Fail()
}
for i := 0; i < len(res); i++ {
if res[i] != tst[i] {
t.Fail()
}
}
}
}
func Test_CalcECC(t *testing.T) {
data := []byte{142, 164, 186}
var size *dmCodeSize = nil