Avoid dependency on Buffer
This commit is contained in:
parent
eb2d499ba7
commit
94009ab826
13 changed files with 50 additions and 596 deletions
|
@ -1,5 +1,3 @@
|
|||
var BufferUtil = require('../utils/buffer')
|
||||
|
||||
/**
|
||||
* Helper class to handle QR Code symbol modules
|
||||
*
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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++) {
|
||||
|
@ -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]
|
||||
|
@ -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
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
var BufferUtil = require('../utils/buffer')
|
||||
var Utils = require('./utils')
|
||||
var ECLevel = require('./error-correction-level')
|
||||
var BitBuffer = require('./bit-buffer')
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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++) {
|
||||
|
@ -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
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
var BufferUtil = require('../utils/buffer')
|
||||
var Polynomial = require('./polynomial')
|
||||
var Buffer = require('buffer/').Buffer
|
||||
|
||||
function ReedSolomonEncoder (degree) {
|
||||
this.genPoly = undefined
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
module.exports.alloc = require('buffer-alloc')
|
||||
module.exports.from = require('buffer-from')
|
|
@ -1,520 +0,0 @@
|
|||
/**
|
||||
* Implementation of a subset of node.js Buffer methods for the browser.
|
||||
* Based on https://github.com/feross/buffer
|
||||
*/
|
||||
|
||||
/* eslint-disable no-proto */
|
||||
|
||||
'use strict'
|
||||
|
||||
var isArray = require('isarray')
|
||||
|
||||
function typedArraySupport () {
|
||||
// Can typed array instances be augmented?
|
||||
try {
|
||||
var arr = new Uint8Array(1)
|
||||
arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
|
||||
return arr.foo() === 42
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
|
||||
|
||||
var K_MAX_LENGTH = Buffer.TYPED_ARRAY_SUPPORT
|
||||
? 0x7fffffff
|
||||
: 0x3fffffff
|
||||
|
||||
function Buffer (arg, offset, length) {
|
||||
if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
|
||||
return new Buffer(arg, offset, length)
|
||||
}
|
||||
|
||||
if (typeof arg === 'number') {
|
||||
return allocUnsafe(this, arg)
|
||||
}
|
||||
|
||||
return from(this, arg, offset, length)
|
||||
}
|
||||
|
||||
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
Buffer.prototype.__proto__ = Uint8Array.prototype
|
||||
Buffer.__proto__ = Uint8Array
|
||||
|
||||
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
|
||||
if (typeof Symbol !== 'undefined' && Symbol.species &&
|
||||
Buffer[Symbol.species] === Buffer) {
|
||||
Object.defineProperty(Buffer, Symbol.species, {
|
||||
value: null,
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
writable: false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function checked (length) {
|
||||
// Note: cannot use `length < K_MAX_LENGTH` here because that fails when
|
||||
// length is NaN (which is otherwise coerced to zero.)
|
||||
if (length >= K_MAX_LENGTH) {
|
||||
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
|
||||
'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
|
||||
}
|
||||
return length | 0
|
||||
}
|
||||
|
||||
function isnan (val) {
|
||||
return val !== val // eslint-disable-line no-self-compare
|
||||
}
|
||||
|
||||
function createBuffer (that, length) {
|
||||
var buf
|
||||
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
buf = new Uint8Array(length)
|
||||
buf.__proto__ = Buffer.prototype
|
||||
} else {
|
||||
// Fallback: Return an object instance of the Buffer class
|
||||
buf = that
|
||||
if (buf === null) {
|
||||
buf = new Buffer(length)
|
||||
}
|
||||
buf.length = length
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
function allocUnsafe (that, size) {
|
||||
var buf = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
|
||||
|
||||
if (!Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
for (var i = 0; i < size; ++i) {
|
||||
buf[i] = 0
|
||||
}
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
function fromString (that, string) {
|
||||
var length = byteLength(string) | 0
|
||||
var buf = createBuffer(that, length)
|
||||
|
||||
var actual = buf.write(string)
|
||||
|
||||
if (actual !== length) {
|
||||
// Writing a hex string, for example, that contains invalid characters will
|
||||
// cause everything after the first invalid character to be ignored. (e.g.
|
||||
// 'abxxcd' will be treated as 'ab')
|
||||
buf = buf.slice(0, actual)
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
function fromArrayLike (that, array) {
|
||||
var length = array.length < 0 ? 0 : checked(array.length) | 0
|
||||
var buf = createBuffer(that, length)
|
||||
for (var i = 0; i < length; i += 1) {
|
||||
buf[i] = array[i] & 255
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
function fromArrayBuffer (that, array, byteOffset, length) {
|
||||
if (byteOffset < 0 || array.byteLength < byteOffset) {
|
||||
throw new RangeError('\'offset\' is out of bounds')
|
||||
}
|
||||
|
||||
if (array.byteLength < byteOffset + (length || 0)) {
|
||||
throw new RangeError('\'length\' is out of bounds')
|
||||
}
|
||||
|
||||
var buf
|
||||
if (byteOffset === undefined && length === undefined) {
|
||||
buf = new Uint8Array(array)
|
||||
} else if (length === undefined) {
|
||||
buf = new Uint8Array(array, byteOffset)
|
||||
} else {
|
||||
buf = new Uint8Array(array, byteOffset, length)
|
||||
}
|
||||
|
||||
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
// Return an augmented `Uint8Array` instance, for best performance
|
||||
buf.__proto__ = Buffer.prototype
|
||||
} else {
|
||||
// Fallback: Return an object instance of the Buffer class
|
||||
buf = fromArrayLike(that, buf)
|
||||
}
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
function fromObject (that, obj) {
|
||||
if (Buffer.isBuffer(obj)) {
|
||||
var len = checked(obj.length) | 0
|
||||
var buf = createBuffer(that, len)
|
||||
|
||||
if (buf.length === 0) {
|
||||
return buf
|
||||
}
|
||||
|
||||
obj.copy(buf, 0, 0, len)
|
||||
return buf
|
||||
}
|
||||
|
||||
if (obj) {
|
||||
if ((typeof ArrayBuffer !== 'undefined' &&
|
||||
obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
|
||||
if (typeof obj.length !== 'number' || isnan(obj.length)) {
|
||||
return createBuffer(that, 0)
|
||||
}
|
||||
return fromArrayLike(that, obj)
|
||||
}
|
||||
|
||||
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
|
||||
return fromArrayLike(that, obj.data)
|
||||
}
|
||||
}
|
||||
|
||||
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
||||
}
|
||||
|
||||
function utf8ToBytes (string, units) {
|
||||
units = units || Infinity
|
||||
var codePoint
|
||||
var length = string.length
|
||||
var leadSurrogate = null
|
||||
var bytes = []
|
||||
|
||||
for (var i = 0; i < length; ++i) {
|
||||
codePoint = string.charCodeAt(i)
|
||||
|
||||
// is surrogate component
|
||||
if (codePoint > 0xD7FF && codePoint < 0xE000) {
|
||||
// last char was a lead
|
||||
if (!leadSurrogate) {
|
||||
// no lead yet
|
||||
if (codePoint > 0xDBFF) {
|
||||
// unexpected trail
|
||||
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||
continue
|
||||
} else if (i + 1 === length) {
|
||||
// unpaired lead
|
||||
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||
continue
|
||||
}
|
||||
|
||||
// valid lead
|
||||
leadSurrogate = codePoint
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
// 2 leads in a row
|
||||
if (codePoint < 0xDC00) {
|
||||
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||
leadSurrogate = codePoint
|
||||
continue
|
||||
}
|
||||
|
||||
// valid surrogate pair
|
||||
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
|
||||
} else if (leadSurrogate) {
|
||||
// valid bmp char, but last char was a lead
|
||||
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
|
||||
}
|
||||
|
||||
leadSurrogate = null
|
||||
|
||||
// encode utf8
|
||||
if (codePoint < 0x80) {
|
||||
if ((units -= 1) < 0) break
|
||||
bytes.push(codePoint)
|
||||
} else if (codePoint < 0x800) {
|
||||
if ((units -= 2) < 0) break
|
||||
bytes.push(
|
||||
codePoint >> 0x6 | 0xC0,
|
||||
codePoint & 0x3F | 0x80
|
||||
)
|
||||
} else if (codePoint < 0x10000) {
|
||||
if ((units -= 3) < 0) break
|
||||
bytes.push(
|
||||
codePoint >> 0xC | 0xE0,
|
||||
codePoint >> 0x6 & 0x3F | 0x80,
|
||||
codePoint & 0x3F | 0x80
|
||||
)
|
||||
} else if (codePoint < 0x110000) {
|
||||
if ((units -= 4) < 0) break
|
||||
bytes.push(
|
||||
codePoint >> 0x12 | 0xF0,
|
||||
codePoint >> 0xC & 0x3F | 0x80,
|
||||
codePoint >> 0x6 & 0x3F | 0x80,
|
||||
codePoint & 0x3F | 0x80
|
||||
)
|
||||
} else {
|
||||
throw new Error('Invalid code point')
|
||||
}
|
||||
}
|
||||
|
||||
return bytes
|
||||
}
|
||||
|
||||
function byteLength (string) {
|
||||
if (Buffer.isBuffer(string)) {
|
||||
return string.length
|
||||
}
|
||||
if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
|
||||
(ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
|
||||
return string.byteLength
|
||||
}
|
||||
if (typeof string !== 'string') {
|
||||
string = '' + string
|
||||
}
|
||||
|
||||
var len = string.length
|
||||
if (len === 0) return 0
|
||||
|
||||
return utf8ToBytes(string).length
|
||||
}
|
||||
|
||||
function blitBuffer (src, dst, offset, length) {
|
||||
for (var i = 0; i < length; ++i) {
|
||||
if ((i + offset >= dst.length) || (i >= src.length)) break
|
||||
dst[i + offset] = src[i]
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
function utf8Write (buf, string, offset, length) {
|
||||
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
|
||||
}
|
||||
|
||||
function from (that, value, offset, length) {
|
||||
if (typeof value === 'number') {
|
||||
throw new TypeError('"value" argument must not be a number')
|
||||
}
|
||||
|
||||
if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
|
||||
return fromArrayBuffer(that, value, offset, length)
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
return fromString(that, value, offset)
|
||||
}
|
||||
|
||||
return fromObject(that, value)
|
||||
}
|
||||
|
||||
Buffer.prototype.write = function write (string, offset, length) {
|
||||
// Buffer#write(string)
|
||||
if (offset === undefined) {
|
||||
length = this.length
|
||||
offset = 0
|
||||
// Buffer#write(string, encoding)
|
||||
} else if (length === undefined && typeof offset === 'string') {
|
||||
length = this.length
|
||||
offset = 0
|
||||
// Buffer#write(string, offset[, length])
|
||||
} else if (isFinite(offset)) {
|
||||
offset = offset | 0
|
||||
if (isFinite(length)) {
|
||||
length = length | 0
|
||||
} else {
|
||||
length = undefined
|
||||
}
|
||||
}
|
||||
|
||||
var remaining = this.length - offset
|
||||
if (length === undefined || length > remaining) length = remaining
|
||||
|
||||
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
|
||||
throw new RangeError('Attempt to write outside buffer bounds')
|
||||
}
|
||||
|
||||
return utf8Write(this, string, offset, length)
|
||||
}
|
||||
|
||||
Buffer.prototype.slice = function slice (start, end) {
|
||||
var len = this.length
|
||||
start = ~~start
|
||||
end = end === undefined ? len : ~~end
|
||||
|
||||
if (start < 0) {
|
||||
start += len
|
||||
if (start < 0) start = 0
|
||||
} else if (start > len) {
|
||||
start = len
|
||||
}
|
||||
|
||||
if (end < 0) {
|
||||
end += len
|
||||
if (end < 0) end = 0
|
||||
} else if (end > len) {
|
||||
end = len
|
||||
}
|
||||
|
||||
if (end < start) end = start
|
||||
|
||||
var newBuf
|
||||
if (Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
newBuf = this.subarray(start, end)
|
||||
// Return an augmented `Uint8Array` instance
|
||||
newBuf.__proto__ = Buffer.prototype
|
||||
} else {
|
||||
var sliceLen = end - start
|
||||
newBuf = new Buffer(sliceLen, undefined)
|
||||
for (var i = 0; i < sliceLen; ++i) {
|
||||
newBuf[i] = this[i + start]
|
||||
}
|
||||
}
|
||||
|
||||
return newBuf
|
||||
}
|
||||
|
||||
Buffer.prototype.copy = function copy (target, targetStart, start, end) {
|
||||
if (!start) start = 0
|
||||
if (!end && end !== 0) end = this.length
|
||||
if (targetStart >= target.length) targetStart = target.length
|
||||
if (!targetStart) targetStart = 0
|
||||
if (end > 0 && end < start) end = start
|
||||
|
||||
// Copy 0 bytes; we're done
|
||||
if (end === start) return 0
|
||||
if (target.length === 0 || this.length === 0) return 0
|
||||
|
||||
// Fatal error conditions
|
||||
if (targetStart < 0) {
|
||||
throw new RangeError('targetStart out of bounds')
|
||||
}
|
||||
if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
|
||||
if (end < 0) throw new RangeError('sourceEnd out of bounds')
|
||||
|
||||
// Are we oob?
|
||||
if (end > this.length) end = this.length
|
||||
if (target.length - targetStart < end - start) {
|
||||
end = target.length - targetStart + start
|
||||
}
|
||||
|
||||
var len = end - start
|
||||
var i
|
||||
|
||||
if (this === target && start < targetStart && targetStart < end) {
|
||||
// descending copy from end
|
||||
for (i = len - 1; i >= 0; --i) {
|
||||
target[i + targetStart] = this[i + start]
|
||||
}
|
||||
} else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
|
||||
// ascending copy from start
|
||||
for (i = 0; i < len; ++i) {
|
||||
target[i + targetStart] = this[i + start]
|
||||
}
|
||||
} else {
|
||||
Uint8Array.prototype.set.call(
|
||||
target,
|
||||
this.subarray(start, start + len),
|
||||
targetStart
|
||||
)
|
||||
}
|
||||
|
||||
return len
|
||||
}
|
||||
|
||||
Buffer.prototype.fill = function fill (val, start, end) {
|
||||
// Handle string cases:
|
||||
if (typeof val === 'string') {
|
||||
if (typeof start === 'string') {
|
||||
start = 0
|
||||
end = this.length
|
||||
} else if (typeof end === 'string') {
|
||||
end = this.length
|
||||
}
|
||||
if (val.length === 1) {
|
||||
var code = val.charCodeAt(0)
|
||||
if (code < 256) {
|
||||
val = code
|
||||
}
|
||||
}
|
||||
} else if (typeof val === 'number') {
|
||||
val = val & 255
|
||||
}
|
||||
|
||||
// Invalid ranges are not set to a default, so can range check early.
|
||||
if (start < 0 || this.length < start || this.length < end) {
|
||||
throw new RangeError('Out of range index')
|
||||
}
|
||||
|
||||
if (end <= start) {
|
||||
return this
|
||||
}
|
||||
|
||||
start = start >>> 0
|
||||
end = end === undefined ? this.length : end >>> 0
|
||||
|
||||
if (!val) val = 0
|
||||
|
||||
var i
|
||||
if (typeof val === 'number') {
|
||||
for (i = start; i < end; ++i) {
|
||||
this[i] = val
|
||||
}
|
||||
} else {
|
||||
var bytes = Buffer.isBuffer(val)
|
||||
? val
|
||||
: new Buffer(val)
|
||||
var len = bytes.length
|
||||
for (i = 0; i < end - start; ++i) {
|
||||
this[i + start] = bytes[i % len]
|
||||
}
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
Buffer.concat = function concat (list, length) {
|
||||
if (!isArray(list)) {
|
||||
throw new TypeError('"list" argument must be an Array of Buffers')
|
||||
}
|
||||
|
||||
if (list.length === 0) {
|
||||
return createBuffer(null, 0)
|
||||
}
|
||||
|
||||
var i
|
||||
if (length === undefined) {
|
||||
length = 0
|
||||
for (i = 0; i < list.length; ++i) {
|
||||
length += list[i].length
|
||||
}
|
||||
}
|
||||
|
||||
var buffer = allocUnsafe(null, length)
|
||||
var pos = 0
|
||||
for (i = 0; i < list.length; ++i) {
|
||||
var buf = list[i]
|
||||
if (!Buffer.isBuffer(buf)) {
|
||||
throw new TypeError('"list" argument must be an Array of Buffers')
|
||||
}
|
||||
buf.copy(buffer, pos)
|
||||
pos += buf.length
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
||||
Buffer.byteLength = byteLength
|
||||
|
||||
Buffer.prototype._isBuffer = true
|
||||
Buffer.isBuffer = function isBuffer (b) {
|
||||
return !!(b != null && b._isBuffer)
|
||||
}
|
||||
|
||||
module.exports.alloc = function (size) {
|
||||
var buffer = new Buffer(size)
|
||||
buffer.fill(0)
|
||||
return buffer
|
||||
}
|
||||
|
||||
module.exports.from = function (data) {
|
||||
return new Buffer(data)
|
||||
}
|
34
package-lock.json
generated
34
package-lock.json
generated
|
@ -281,7 +281,8 @@
|
|||
"base64-js": {
|
||||
"version": "1.3.1",
|
||||
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz",
|
||||
"integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g=="
|
||||
"integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==",
|
||||
"dev": true
|
||||
},
|
||||
"bcrypt-pbkdf": {
|
||||
"version": "1.0.2",
|
||||
|
@ -541,34 +542,17 @@
|
|||
"version": "5.4.3",
|
||||
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.3.tgz",
|
||||
"integrity": "sha512-zvj65TkFeIt3i6aj5bIvJDzjjQQGs4o/sNoezg1F1kYap9Nu2jcUdpwzRSJTHMMzG0H7bZkn4rNQpImhuxWX2A==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"base64-js": "^1.0.2",
|
||||
"ieee754": "^1.1.4"
|
||||
}
|
||||
},
|
||||
"buffer-alloc": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz",
|
||||
"integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==",
|
||||
"requires": {
|
||||
"buffer-alloc-unsafe": "^1.1.0",
|
||||
"buffer-fill": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"buffer-alloc-unsafe": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz",
|
||||
"integrity": "sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg=="
|
||||
},
|
||||
"buffer-fill": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz",
|
||||
"integrity": "sha1-+PeLdniYiO858gXNY39o5wISKyw="
|
||||
},
|
||||
"buffer-from": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
|
||||
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
|
||||
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
|
||||
"dev": true
|
||||
},
|
||||
"buffer-xor": {
|
||||
"version": "1.0.3",
|
||||
|
@ -1253,6 +1237,11 @@
|
|||
"resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
|
||||
"integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA=="
|
||||
},
|
||||
"encode-utf8": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/encode-utf8/-/encode-utf8-1.0.3.tgz",
|
||||
"integrity": "sha512-ucAnuBEhUK4boH2HjVYG5Q2mQyPorvv0u/ocS+zhdw0S8AlHYY+GOFhP1Gio5z4icpP2ivFSvhtFjQi8+T9ppw=="
|
||||
},
|
||||
"encodeurl": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
|
||||
|
@ -2103,7 +2092,8 @@
|
|||
"ieee754": {
|
||||
"version": "1.1.13",
|
||||
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz",
|
||||
"integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg=="
|
||||
"integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==",
|
||||
"dev": true
|
||||
},
|
||||
"ignore": {
|
||||
"version": "3.3.10",
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
"main": "./lib/index.js",
|
||||
"browser": {
|
||||
"./lib/index.js": "./lib/browser.js",
|
||||
"./lib/utils/buffer.js": "./lib/utils/typedarray-buffer.js",
|
||||
"fs": false
|
||||
},
|
||||
"files": [
|
||||
|
@ -38,10 +37,8 @@
|
|||
"qrcode": "./bin/qrcode"
|
||||
},
|
||||
"dependencies": {
|
||||
"buffer": "^5.4.3",
|
||||
"buffer-alloc": "^1.2.0",
|
||||
"buffer-from": "^1.1.1",
|
||||
"dijkstrajs": "^1.0.1",
|
||||
"encode-utf8": "^1.0.3",
|
||||
"isarray": "^2.0.1",
|
||||
"pngjs": "^3.3.0",
|
||||
"yargs": "^13.2.4"
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
var test = require('tap').test
|
||||
var BitMatrix = require('core/bit-matrix')
|
||||
var MaskPattern = require('core/mask-pattern')
|
||||
var BufferUtil = require('utils/buffer')
|
||||
|
||||
test('Mask pattern - Pattern references', function (t) {
|
||||
var patternsCount = Object.keys(MaskPattern.Patterns).length
|
||||
|
@ -110,7 +109,7 @@ test('Mask pattern - Apply mask', function (t) {
|
|||
for (var p = 0; p < patterns; p++) {
|
||||
var matrix = new BitMatrix(6)
|
||||
MaskPattern.applyMask(p, matrix)
|
||||
t.deepEqual(matrix.data, BufferUtil.from(expectedPatterns[p]), 'Should return correct pattern')
|
||||
t.deepEqual(matrix.data, new Uint8Array(expectedPatterns[p]), 'Should return correct pattern')
|
||||
}
|
||||
|
||||
matrix = new BitMatrix(2)
|
||||
|
@ -120,7 +119,7 @@ test('Mask pattern - Apply mask', function (t) {
|
|||
matrix.set(1, 1, false, true)
|
||||
MaskPattern.applyMask(0, matrix)
|
||||
|
||||
t.deepEqual(matrix.data, BufferUtil.from([false, false, false, false]), 'Should leave reserved bit unchanged')
|
||||
t.deepEqual(matrix.data, new Uint8Array([false, false, false, false]), 'Should leave reserved bit unchanged')
|
||||
|
||||
t.throws(function () { MaskPattern.applyMask(-1, new BitMatrix(1)) }, 'Should throw if pattern is invalid')
|
||||
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
var test = require('tap').test
|
||||
var Poly = require('core/polynomial')
|
||||
var BufferUtil = require('utils/buffer')
|
||||
|
||||
test('Generator polynomial', function (t) {
|
||||
var result = Poly.generateECPolynomial(0)
|
||||
t.ok(Buffer.isBuffer(result), 'Should return a buffer')
|
||||
t.deepEqual(result, BufferUtil.from([1]), 'Should return coeff [1] for polynomial of degree 0')
|
||||
t.ok(result instanceof Uint8Array, 'Should return an Uint8Array')
|
||||
t.deepEqual(result, new Uint8Array([1]), 'Should return coeff [1] for polynomial of degree 0')
|
||||
|
||||
for (var e = 2; e <= 68; e++) {
|
||||
t.equal(Poly.generateECPolynomial(e).length, e + 1, 'Should return a number of coefficients equal to (degree + 1)')
|
||||
|
@ -19,11 +18,11 @@ test('Polynomial', function (t) {
|
|||
var p2 = [5, 6]
|
||||
|
||||
var result = Poly.mul(p1, p2)
|
||||
t.ok(Buffer.isBuffer(result), 'Should return a buffer')
|
||||
t.ok(result instanceof Uint8Array, 'Should return an Uint8Array')
|
||||
t.equal(result.length, 6, 'Should return correct number of coefficients')
|
||||
|
||||
result = Poly.mod(p1, Poly.generateECPolynomial(2))
|
||||
t.ok(Buffer.isBuffer(result), 'Should return a buffer')
|
||||
t.ok(result instanceof Uint8Array, 'Should return an Uint8Array')
|
||||
t.equal(result.length, 2, 'Should return correct number of coefficients')
|
||||
|
||||
t.end()
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
var test = require('tap').test
|
||||
var RS = require('core/reed-solomon-encoder')
|
||||
var BufferUtil = require('utils/buffer')
|
||||
|
||||
test('Reed-Solomon encoder', function (t) {
|
||||
var enc = new RS()
|
||||
|
@ -12,7 +11,7 @@ test('Reed-Solomon encoder', function (t) {
|
|||
t.equal(enc.degree, 2, 'Should set correct degree value')
|
||||
t.ok(enc.genPoly, 'Generator polynomial should be defined')
|
||||
|
||||
var result = enc.encode(BufferUtil.from('01234'))
|
||||
var result = enc.encode(new Uint8Array([48, 49, 50, 51, 52]))
|
||||
t.equal(result.length, 2, 'Should return a number of codewords equal to gen poly degree')
|
||||
|
||||
enc = new RS(2)
|
||||
|
@ -27,7 +26,7 @@ test('Reed-Solomon encoder', function (t) {
|
|||
t.notOk(enc.genPoly, 'Should not create a generator polynomial if degree is 0')
|
||||
|
||||
enc = new RS(1)
|
||||
t.deepEqual(enc.encode(BufferUtil.from([0])), BufferUtil.from([0]),
|
||||
t.deepEqual(enc.encode(new Uint8Array([0])), new Uint8Array([0]),
|
||||
'Should return correct buffer')
|
||||
|
||||
t.end()
|
||||
|
|
Loading…
Reference in a new issue