Skip to content

Commit

Permalink
Avoid dependency on Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Mar 30, 2020
1 parent eb2d499 commit 94009ab
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 596 deletions.
6 changes: 2 additions & 4 deletions lib/core/bit-matrix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var BufferUtil = require('../utils/buffer')

/**
* Helper class to handle QR Code symbol modules
*
Expand All @@ -11,8 +9,8 @@ function BitMatrix (size) {
}

this.size = size
this.data = BufferUtil.alloc(size * size)
this.reservedBit = BufferUtil.alloc(size * size)
this.data = new Uint8Array(size * size)
this.reservedBit = new Uint8Array(size * size)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/core/byte-data.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var BufferUtil = require('../utils/buffer')
var encodeUtf8 = require('encode-utf8')
var Mode = require('./mode')

function ByteData (data) {
this.mode = Mode.BYTE
this.data = BufferUtil.from(data)
this.data = new Uint8Array(encodeUtf8(data))
}

ByteData.getBitsLength = function getBitsLength (length) {
Expand Down
6 changes: 2 additions & 4 deletions lib/core/galois-field.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var BufferUtil = require('../utils/buffer')

var EXP_TABLE = BufferUtil.alloc(512)
var LOG_TABLE = BufferUtil.alloc(256)
var EXP_TABLE = new Uint8Array(512)
var LOG_TABLE = new Uint8Array(256)
/**
* Precompute the log and anti-log tables for faster computation later
*
Expand Down
23 changes: 11 additions & 12 deletions lib/core/polynomial.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var BufferUtil = require('../utils/buffer')
var GF = require('./galois-field')

/**
* Multiplies two polynomials inside Galois Field
*
* @param {Buffer} p1 Polynomial
* @param {Buffer} p2 Polynomial
* @return {Buffer} Product of p1 and p2
* @param {Uint8Array} p1 Polynomial
* @param {Uint8Array} p2 Polynomial
* @return {Uint8Array} Product of p1 and p2
*/
exports.mul = function mul (p1, p2) {
var coeff = BufferUtil.alloc(p1.length + p2.length - 1)
var coeff = new Uint8Array(p1.length + p2.length - 1)

for (var i = 0; i < p1.length; i++) {
for (var j = 0; j < p2.length; j++) {
Expand All @@ -23,12 +22,12 @@ exports.mul = function mul (p1, p2) {
/**
* Calculate the remainder of polynomials division
*
* @param {Buffer} divident Polynomial
* @param {Buffer} divisor Polynomial
* @return {Buffer} Remainder
* @param {Uint8Array} divident Polynomial
* @param {Uint8Array} divisor Polynomial
* @return {Uint8Array} Remainder
*/
exports.mod = function mod (divident, divisor) {
var result = BufferUtil.from(divident)
var result = new Uint8Array(divident)

while ((result.length - divisor.length) >= 0) {
var coeff = result[0]
Expand All @@ -51,12 +50,12 @@ exports.mod = function mod (divident, divisor) {
* (used by Reed-Solomon encoder)
*
* @param {Number} degree Degree of the generator polynomial
* @return {Buffer} Buffer containing polynomial coefficients
* @return {Uint8Array} Buffer containing polynomial coefficients
*/
exports.generateECPolynomial = function generateECPolynomial (degree) {
var poly = BufferUtil.from([1])
var poly = new Uint8Array([1])
for (var i = 0; i < degree; i++) {
poly = exports.mul(poly, [1, GF.exp(i)])
poly = exports.mul(poly, new Uint8Array([1, GF.exp(i)]))
}

return poly
Expand Down
13 changes: 6 additions & 7 deletions lib/core/qrcode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var BufferUtil = require('../utils/buffer')
var Utils = require('./utils')
var ECLevel = require('./error-correction-level')
var BitBuffer = require('./bit-buffer')
Expand Down Expand Up @@ -179,8 +178,8 @@ function setupFormatInfo (matrix, errorCorrectionLevel, maskPattern) {
/**
* Add encoded data bits to matrix
*
* @param {BitMatrix} matrix Modules matrix
* @param {Buffer} data Data codewords
* @param {BitMatrix} matrix Modules matrix
* @param {Uint8Array} data Data codewords
*/
function setupData (matrix, data) {
var size = matrix.size
Expand Down Expand Up @@ -228,7 +227,7 @@ function setupData (matrix, data) {
* @param {Number} version QR Code version
* @param {ErrorCorrectionLevel} errorCorrectionLevel Error correction level
* @param {ByteData} data Data input
* @return {Buffer} Buffer containing encoded codewords
* @return {Uint8Array} Buffer containing encoded codewords
*/
function createData (version, errorCorrectionLevel, segments) {
// Prepare data buffer
Expand Down Expand Up @@ -293,7 +292,7 @@ function createData (version, errorCorrectionLevel, segments) {
* @param {BitBuffer} bitBuffer Data to encode
* @param {Number} version QR Code version
* @param {ErrorCorrectionLevel} errorCorrectionLevel Error correction level
* @return {Buffer} Buffer containing encoded codewords
* @return {Uint8Array} Buffer containing encoded codewords
*/
function createCodewords (bitBuffer, version, errorCorrectionLevel) {
// Total codewords for this QR code version (Data + Error correction)
Expand Down Expand Up @@ -327,7 +326,7 @@ function createCodewords (bitBuffer, version, errorCorrectionLevel) {
var dcData = new Array(ecTotalBlocks)
var ecData = new Array(ecTotalBlocks)
var maxDataSize = 0
var buffer = BufferUtil.from(bitBuffer.buffer)
var buffer = new Uint8Array(bitBuffer.buffer)

// Divide the buffer into the required number of blocks
for (var b = 0; b < ecTotalBlocks; b++) {
Expand All @@ -345,7 +344,7 @@ function createCodewords (bitBuffer, version, errorCorrectionLevel) {

// Create final data
// Interleave the data and error correction codewords from each block
var data = BufferUtil.alloc(totalCodewords)
var data = new Uint8Array(totalCodewords)
var index = 0
var i, r

Expand Down
14 changes: 6 additions & 8 deletions lib/core/reed-solomon-encoder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var BufferUtil = require('../utils/buffer')
var Polynomial = require('./polynomial')
var Buffer = require('buffer/').Buffer

function ReedSolomonEncoder (degree) {
this.genPoly = undefined
Expand All @@ -24,8 +22,8 @@ ReedSolomonEncoder.prototype.initialize = function initialize (degree) {
/**
* Encodes a chunk of data
*
* @param {Buffer} data Buffer containing input data
* @return {Buffer} Buffer containing encoded data
* @param {Uint8Array} data Buffer containing input data
* @return {Uint8Array} Buffer containing encoded data
*/
ReedSolomonEncoder.prototype.encode = function encode (data) {
if (!this.genPoly) {
Expand All @@ -34,8 +32,8 @@ ReedSolomonEncoder.prototype.encode = function encode (data) {

// Calculate EC for this data block
// extends data size to data+genPoly size
var pad = BufferUtil.alloc(this.degree)
var paddedData = Buffer.concat([data, pad], data.length + this.degree)
var paddedData = new Uint8Array(data.length + this.degree)
paddedData.set(data)

// The error correction codewords are the remainder after dividing the data codewords
// by a generator polynomial
Expand All @@ -46,8 +44,8 @@ ReedSolomonEncoder.prototype.encode = function encode (data) {
// pad with 0s to the left to reach the needed number of coefficients
var start = this.degree - remainder.length
if (start > 0) {
var buff = BufferUtil.alloc(this.degree)
remainder.copy(buff, start)
var buff = new Uint8Array(this.degree)
buff.set(remainder, start)

return buff
}
Expand Down
2 changes: 0 additions & 2 deletions lib/utils/buffer.js

This file was deleted.

Loading

0 comments on commit 94009ab

Please sign in to comment.