UNPKG

cnf-qrcode

Version:

generate qrcode,support svg base64 utf8

60 lines (49 loc) 1.73 kB
import Buffer from '../utils/buffer'; import * as Polynomial from './polynomial'; function ReedSolomonEncoder(degree) { this.genPoly = undefined; this.degree = degree; if (this.degree) this.initialize(this.degree); } /** * Initialize the encoder. * The input param should correspond to the number of error correction codewords. * * @param {Number} degree */ ReedSolomonEncoder.prototype.initialize = function initialize(degree) { // create an irreducible generator polynomial this.degree = degree; this.genPoly = Polynomial.generateECPolynomial(this.degree); }; /** * Encodes a chunk of data * * @param {Buffer} data Buffer containing input data * @return {Buffer} Buffer containing encoded data */ ReedSolomonEncoder.prototype.encode = function encode(data) { if (!this.genPoly) { throw new Error('Encoder not initialized'); } // Calculate EC for this data block // extends data size to data+genPoly size const pad = new Buffer(this.degree); pad.fill(0); const paddedData = Buffer.concat([data, pad], data.length + this.degree); // The error correction codewords are the remainder after dividing the data codewords // by a generator polynomial const remainder = Polynomial.mod(paddedData, this.genPoly); // return EC data blocks (last n byte, where n is the degree of genPoly) // If coefficients number in remainder are less than genPoly degree, // pad with 0s to the left to reach the needed number of coefficients const start = this.degree - remainder.length; if (start > 0) { const buff = new Buffer(this.degree); buff.fill(0); remainder.copy(buff, start); return buff; } return remainder; }; export default ReedSolomonEncoder;