From 3ca7797360e563aa6ebdcca10f388431f47f9377 Mon Sep 17 00:00:00 2001 From: sylnsfar Date: Sat, 27 Aug 2016 11:27:09 +0800 Subject: [PATCH] complete ecc --- draw.py | 2 ++ ecc.py | 52 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/draw.py b/draw.py index e458bc6..ea9736d 100644 --- a/draw.py +++ b/draw.py @@ -2,6 +2,7 @@ from PIL import Image, ImageDraw +# test: m1 = [[1]*7+[0]*7+[1]*7, [1,0,0,0,0,0,1]+[0]*7+[1,0,0,0,0,0,1], [1,0,1,1,1,0,1]+[0]*7+[1,0,1,1,1,0,1], @@ -41,4 +42,5 @@ def draw_qrcode(qrmatrix): pic.save('qrcode.jpg') pic.show() +# test: draw_qrcode(m1) \ No newline at end of file diff --git a/ecc.py b/ecc.py index 7d38973..e74cc1d 100644 --- a/ecc.py +++ b/ecc.py @@ -2,31 +2,54 @@ #GP: Generator Polynomial, MP: Message Polynomial # test: -GP = [0, 251, 67, 46, 61, 118, 70, 64, 94, 32, 45] +GP_list = { + 7: [0, 87, 229, 146, 149, 238, 102, 21], + 10: [0, 251, 67, 46, 61, 118, 70, 64, 94, 32, 45], + 13: [0, 74, 152, 176, 100, 86, 100, 106, 104, 130, 218, 206, 140, 78], + 15: [0, 8, 183, 61, 91, 202, 37, 51, 58, 58, 237, 140, 124, 5, 99, 105], + 16: [0, 120, 104, 107, 109, 102, 161, 76, 3, 91, 191, 147, 169, 182, 194, 225, 120], + 17: [0, 43, 139, 206, 78, 43, 239, 123, 206, 214, 147, 24, 99, 150, 39, 243, 163, 136], + 18: [0, 215, 234, 158, 94, 184, 97, 118, 170, 79, 187, 152, 148, 252, 179, 5, 98, 96, 153], + 20: [0, 17, 60, 79, 50, 61, 163, 26, 187, 202, 180, 221, 225, 83, 239, 156, 164, 212, 212, 188, 190], + 22: [0, 210, 171, 247, 242, 93, 230, 14, 109, 221, 53, 200, 74, 8, 172, 98, 80, 219, 134, 160, 105, 165, 231], + 23: [0, 229, 121, 135, 48, 211, 117, 251, 126, 159, 180, 169, 152, 192, 226, 228, 218, 111, 117, 232, 87, 96, 227, 21], + 26: [0, 173, 125, 158, 2, 103, 182, 118, 17, 145, 201, 111, 28, 165, 53, 161, 21, 245, 142, 13, 102, 48, 227, 153, 145, 218, 70], + 28: [0, 168, 223, 200, 104, 224, 234, 108, 180, 110, 190, 195, 147, 205, 27, 232, 201, 21, 43, 245, 87, 42, 195, 212, 119, 242, 37, 9, 123], + 30: [0, 41, 173, 145, 152, 216, 31, 179, 182, 50, 48, 110, 86, 239, 96, 222, 125, 42, 173, 226, 193, 224, 130, 156, 37, 251, 216, 238, 40, 192, 180] + } #DC: Data Codewords, ECC: Error Correction Codewords def get_ECC(DC, ECC_num): - po2 = get_power_of_2_list() - log = get_log_list(po2) - - # ..... get the correct GP according to ECC_num - + GP = GP_list[ECC_num] remainder = DC for i in range(len(DC)): - remainder = divide(remainder, GP) + remainder = divide(remainder, *GP) return remainder -def divide(MP, GP): +def divide(MP, *GP): + po2 = get_power_of_2_list() + log = get_log_list(po2) + GP = list(GP) + for i in range(len(GP)): + GP[i] += log[MP[0]] + if GP[i] > 255: + GP[i] %= 255 + GP[i] = po2[GP[i]] + + return XOR(MP, GP) + + +def XOR(MP, GP): a = len(MP) - len(GP) if a < 0: MP += [0] * (-a) elif a > 0: GP += [0] * a - log(MP[0]) - -def XOR(): - pass + remainder = [] + for i in range(1, len(MP)): + remainder.append(MP[i]^GP[i]) + return remainder def get_power_of_2_list(): po2 = [1] @@ -41,7 +64,4 @@ def get_log_list(po2): log = [None]*256 for i in range(255): log[po2[i]] = i - return log - -# test: -dc = '32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17' + return log \ No newline at end of file