📄 dmtxreedsol.c
字号:
/*libdmtx - Data Matrix Encoding/Decoding LibraryCopyright (c) 2008 Mike LaughtonThis library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General PublicLicense as published by the Free Software Foundation; eitherversion 2.1 of the License, or (at your option) any later version.This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser General Public License for more details.You should have received a copy of the GNU Lesser General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USAContact: mike@dragonflylogic.com*//* $Id: dmtxreedsol.c 492 2008-11-06 19:24:26Z mblaughton $ *//** * @file dmtxreedsol.c * @brief Reed Solomon error correction */#ifndef GfSum#define GfSum(a,b) (a ^ b)#endif/** * @brief XXX * @param message * @param sizeIdx * @return void */static voidGenReedSolEcc(DmtxMessage *message, int sizeIdx){ int i, j, val; int step, block; int blockErrorWords; int symbolDataWords; int blockDataWords; int symbolErrorWords; int symbolTotalWords; unsigned char g[69], b[68], *bPtr; unsigned char *codewords = message->code; symbolDataWords = dmtxGetSymbolAttribute(DmtxSymAttribSymbolDataWords, sizeIdx); symbolErrorWords = dmtxGetSymbolAttribute(DmtxSymAttribSymbolErrorWords, sizeIdx); symbolTotalWords = symbolDataWords + symbolErrorWords; blockErrorWords = dmtxGetSymbolAttribute(DmtxSymAttribBlockErrorWords, sizeIdx); step = dmtxGetSymbolAttribute(DmtxSymAttribInterleavedBlocks, sizeIdx); assert(blockErrorWords == symbolErrorWords / step); memset(g, 0x01, sizeof(g)); /* Generate ECC polynomial */ for(i = 1; i <= blockErrorWords; i++) { for(j = i - 1; j >= 0; j--) { g[j] = GfDoublify(g[j], i); /* g[j] *= 2**i */ if(j > 0) g[j] = GfSum(g[j], g[j-1]); /* g[j] += g[j-1] */ } } /* Populate error codeword array */ for(block = 0; block < step; block++) { memset(b, 0x00, sizeof(b)); for(i = block; i < symbolDataWords; i += step) { val = GfSum(b[blockErrorWords-1], codewords[i]); for(j = blockErrorWords - 1; j > 0; j--) { b[j] = GfSum(b[j-1], GfProduct(g[j], val)); } b[0] = GfProduct(g[0], val); } blockDataWords = dmtxGetBlockDataSize(sizeIdx, block); bPtr = b + blockErrorWords; for(i = block + (step * blockDataWords); i < symbolTotalWords; i += step) codewords[i] = *(--bPtr); assert(b == bPtr); }}/** * @brief XXX * @param code * @param sizeIdx * @param fix * @return DMTX_SUCCESS | DMTX_FAILURE */static intDecodeCheckErrors(unsigned char *code, int sizeIdx, int fix){ int i, j; int interleavedBlocks; int blockErrorWords; int blockTotalWords; int blockMaxCorrectable; struct rs *rs; int fixedErr, fixedErrSum; unsigned char data[255]; interleavedBlocks = dmtxGetSymbolAttribute(DmtxSymAttribInterleavedBlocks, sizeIdx); blockErrorWords = dmtxGetSymbolAttribute(DmtxSymAttribBlockErrorWords, sizeIdx); blockMaxCorrectable = dmtxGetSymbolAttribute(DmtxSymAttribBlockMaxCorrectable, sizeIdx); fixedErrSum = 0; for(i = 0; i < interleavedBlocks; i++) { blockTotalWords = blockErrorWords + dmtxGetBlockDataSize(sizeIdx, i); rs = init_rs_char(blockErrorWords, 255 - blockTotalWords); if(rs == NULL) return DMTX_FAILURE; for(j = 0; j < blockTotalWords; j++) data[j] = code[j*interleavedBlocks+i]; fixedErr = decode_rs_char(rs, data, NULL, 0, fix); if(fixedErr < 0 || fixedErr > blockMaxCorrectable) { free_rs_char(&rs); return DMTX_FAILURE; } fixedErrSum += fixedErr; for(j = 0; j < blockTotalWords; j++) code[j*interleavedBlocks+i] = data[j]; free_rs_char(&rs); } if(fix >= 0 && fixedErrSum > fix) return DMTX_FAILURE; return DMTX_SUCCESS;}/** * @brief Galois Field Arithmetic: a times b * @param a * @param b * @return Galois Field Product */static intGfProduct(int a, int b){ if(a == 0 || b == 0) return 0; else return aLogVal[(logVal[a] + logVal[b]) % 255];}/** * @brief XXX * @param a * @param b * @return Result */static intGfDoublify(int a, int b){ if(a == 0) /* XXX this is right, right? */ return 0; else if(b == 0) return a; /* XXX this is right, right? */ else return aLogVal[(logVal[a] + b) % 255];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -