UNPKG

smart-upiqr

Version:

Easily generate branded UPI QR codes and payment links with customizable options for JavaScript and Node.js.

1,821 lines (1,529 loc) 182 kB
import require$$2 from 'buffer'; import require$$1$2 from 'fs'; import require$$0 from 'util'; import require$$1 from 'stream'; import require$$1$1 from 'zlib'; import require$$0$1 from 'assert'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; var server = {}; var canPromise; var hasRequiredCanPromise; function requireCanPromise () { if (hasRequiredCanPromise) return canPromise; hasRequiredCanPromise = 1; // can-promise has a crash in some versions of react native that dont have // standard global objects // https://github.com/soldair/node-qrcode/issues/157 canPromise = function () { return typeof Promise === 'function' && Promise.prototype && Promise.prototype.then }; return canPromise; } var qrcode = {}; var buffer = {}; /* Node.js 6.4.0 and up has full support */ var bufferFill; var hasRequiredBufferFill; function requireBufferFill () { if (hasRequiredBufferFill) return bufferFill; hasRequiredBufferFill = 1; var hasFullSupport = (function () { try { if (!Buffer.isEncoding('latin1')) { return false } var buf = Buffer.alloc ? Buffer.alloc(4) : new Buffer(4); buf.fill('ab', 'ucs2'); return (buf.toString('hex') === '61006200') } catch (_) { return false } }()); function isSingleByte (val) { return (val.length === 1 && val.charCodeAt(0) < 256) } function fillWithNumber (buffer, val, start, end) { if (start < 0 || end > buffer.length) { throw new RangeError('Out of range index') } start = start >>> 0; end = end === undefined ? buffer.length : end >>> 0; if (end > start) { buffer.fill(val, start, end); } return buffer } function fillWithBuffer (buffer, val, start, end) { if (start < 0 || end > buffer.length) { throw new RangeError('Out of range index') } if (end <= start) { return buffer } start = start >>> 0; end = end === undefined ? buffer.length : end >>> 0; var pos = start; var len = val.length; while (pos <= (end - len)) { val.copy(buffer, pos); pos += len; } if (pos !== end) { val.copy(buffer, pos, 0, end - pos); } return buffer } function fill (buffer, val, start, end, encoding) { if (hasFullSupport) { return buffer.fill(val, start, end, encoding) } if (typeof val === 'number') { return fillWithNumber(buffer, val, start, end) } if (typeof val === 'string') { if (typeof start === 'string') { encoding = start; start = 0; end = buffer.length; } else if (typeof end === 'string') { encoding = end; end = buffer.length; } if (encoding !== undefined && typeof encoding !== 'string') { throw new TypeError('encoding must be a string') } if (encoding === 'latin1') { encoding = 'binary'; } if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { throw new TypeError('Unknown encoding: ' + encoding) } if (val === '') { return fillWithNumber(buffer, 0, start, end) } if (isSingleByte(val)) { return fillWithNumber(buffer, val.charCodeAt(0), start, end) } val = new Buffer(val, encoding); } if (Buffer.isBuffer(val)) { return fillWithBuffer(buffer, val, start, end) } // Other values (e.g. undefined, boolean, object) results in zero-fill return fillWithNumber(buffer, 0, start, end) } bufferFill = fill; return bufferFill; } var bufferAllocUnsafe; var hasRequiredBufferAllocUnsafe; function requireBufferAllocUnsafe () { if (hasRequiredBufferAllocUnsafe) return bufferAllocUnsafe; hasRequiredBufferAllocUnsafe = 1; function allocUnsafe (size) { if (typeof size !== 'number') { throw new TypeError('"size" argument must be a number') } if (size < 0) { throw new RangeError('"size" argument must not be negative') } if (Buffer.allocUnsafe) { return Buffer.allocUnsafe(size) } else { return new Buffer(size) } } bufferAllocUnsafe = allocUnsafe; return bufferAllocUnsafe; } var bufferAlloc; var hasRequiredBufferAlloc; function requireBufferAlloc () { if (hasRequiredBufferAlloc) return bufferAlloc; hasRequiredBufferAlloc = 1; var bufferFill = requireBufferFill(); var allocUnsafe = requireBufferAllocUnsafe(); bufferAlloc = function alloc (size, fill, encoding) { if (typeof size !== 'number') { throw new TypeError('"size" argument must be a number') } if (size < 0) { throw new RangeError('"size" argument must not be negative') } if (Buffer.alloc) { return Buffer.alloc(size, fill, encoding) } var buffer = allocUnsafe(size); if (size === 0) { return buffer } if (fill === undefined) { return bufferFill(buffer, 0) } if (typeof encoding !== 'string') { encoding = undefined; } return bufferFill(buffer, fill, encoding) }; return bufferAlloc; } /* eslint-disable node/no-deprecated-api */ var bufferFrom_1; var hasRequiredBufferFrom; function requireBufferFrom () { if (hasRequiredBufferFrom) return bufferFrom_1; hasRequiredBufferFrom = 1; var toString = Object.prototype.toString; var isModern = ( typeof Buffer !== 'undefined' && typeof Buffer.alloc === 'function' && typeof Buffer.allocUnsafe === 'function' && typeof Buffer.from === 'function' ); function isArrayBuffer (input) { return toString.call(input).slice(8, -1) === 'ArrayBuffer' } function fromArrayBuffer (obj, byteOffset, length) { byteOffset >>>= 0; var maxLength = obj.byteLength - byteOffset; if (maxLength < 0) { throw new RangeError("'offset' is out of bounds") } if (length === undefined) { length = maxLength; } else { length >>>= 0; if (length > maxLength) { throw new RangeError("'length' is out of bounds") } } return isModern ? Buffer.from(obj.slice(byteOffset, byteOffset + length)) : new Buffer(new Uint8Array(obj.slice(byteOffset, byteOffset + length))) } function fromString (string, encoding) { if (typeof encoding !== 'string' || encoding === '') { encoding = 'utf8'; } if (!Buffer.isEncoding(encoding)) { throw new TypeError('"encoding" must be a valid string encoding') } return isModern ? Buffer.from(string, encoding) : new Buffer(string, encoding) } function bufferFrom (value, encodingOrOffset, length) { if (typeof value === 'number') { throw new TypeError('"value" argument must not be a number') } if (isArrayBuffer(value)) { return fromArrayBuffer(value, encodingOrOffset, length) } if (typeof value === 'string') { return fromString(value, encodingOrOffset) } return isModern ? Buffer.from(value) : new Buffer(value) } bufferFrom_1 = bufferFrom; return bufferFrom_1; } var hasRequiredBuffer; function requireBuffer () { if (hasRequiredBuffer) return buffer; hasRequiredBuffer = 1; buffer.alloc = requireBufferAlloc(); buffer.from = requireBufferFrom(); return buffer; } var utils$1 = {}; var hasRequiredUtils$1; function requireUtils$1 () { if (hasRequiredUtils$1) return utils$1; hasRequiredUtils$1 = 1; var toSJISFunction; var CODEWORDS_COUNT = [ 0, // Not used 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706 ]; /** * Returns the QR Code size for the specified version * * @param {Number} version QR Code version * @return {Number} size of QR code */ utils$1.getSymbolSize = function getSymbolSize (version) { if (!version) throw new Error('"version" cannot be null or undefined') if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40') return version * 4 + 17 }; /** * Returns the total number of codewords used to store data and EC information. * * @param {Number} version QR Code version * @return {Number} Data length in bits */ utils$1.getSymbolTotalCodewords = function getSymbolTotalCodewords (version) { return CODEWORDS_COUNT[version] }; /** * Encode data with Bose-Chaudhuri-Hocquenghem * * @param {Number} data Value to encode * @return {Number} Encoded value */ utils$1.getBCHDigit = function (data) { var digit = 0; while (data !== 0) { digit++; data >>>= 1; } return digit }; utils$1.setToSJISFunction = function setToSJISFunction (f) { if (typeof f !== 'function') { throw new Error('"toSJISFunc" is not a valid function.') } toSJISFunction = f; }; utils$1.isKanjiModeEnabled = function () { return typeof toSJISFunction !== 'undefined' }; utils$1.toSJIS = function toSJIS (kanji) { return toSJISFunction(kanji) }; return utils$1; } var errorCorrectionLevel = {}; var hasRequiredErrorCorrectionLevel; function requireErrorCorrectionLevel () { if (hasRequiredErrorCorrectionLevel) return errorCorrectionLevel; hasRequiredErrorCorrectionLevel = 1; (function (exports) { exports.L = { bit: 1 }; exports.M = { bit: 0 }; exports.Q = { bit: 3 }; exports.H = { bit: 2 }; function fromString (string) { if (typeof string !== 'string') { throw new Error('Param is not a string') } var lcStr = string.toLowerCase(); switch (lcStr) { case 'l': case 'low': return exports.L case 'm': case 'medium': return exports.M case 'q': case 'quartile': return exports.Q case 'h': case 'high': return exports.H default: throw new Error('Unknown EC Level: ' + string) } } exports.isValid = function isValid (level) { return level && typeof level.bit !== 'undefined' && level.bit >= 0 && level.bit < 4 }; exports.from = function from (value, defaultValue) { if (exports.isValid(value)) { return value } try { return fromString(value) } catch (e) { return defaultValue } }; } (errorCorrectionLevel)); return errorCorrectionLevel; } var bitBuffer; var hasRequiredBitBuffer; function requireBitBuffer () { if (hasRequiredBitBuffer) return bitBuffer; hasRequiredBitBuffer = 1; function BitBuffer () { this.buffer = []; this.length = 0; } BitBuffer.prototype = { get: function (index) { var bufIndex = Math.floor(index / 8); return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1 }, put: function (num, length) { for (var i = 0; i < length; i++) { this.putBit(((num >>> (length - i - 1)) & 1) === 1); } }, getLengthInBits: function () { return this.length }, putBit: function (bit) { var bufIndex = Math.floor(this.length / 8); if (this.buffer.length <= bufIndex) { this.buffer.push(0); } if (bit) { this.buffer[bufIndex] |= (0x80 >>> (this.length % 8)); } this.length++; } }; bitBuffer = BitBuffer; return bitBuffer; } var bitMatrix; var hasRequiredBitMatrix; function requireBitMatrix () { if (hasRequiredBitMatrix) return bitMatrix; hasRequiredBitMatrix = 1; var BufferUtil = requireBuffer(); /** * Helper class to handle QR Code symbol modules * * @param {Number} size Symbol size */ function BitMatrix (size) { if (!size || size < 1) { throw new Error('BitMatrix size must be defined and greater than 0') } this.size = size; this.data = BufferUtil.alloc(size * size); this.reservedBit = BufferUtil.alloc(size * size); } /** * Set bit value at specified location * If reserved flag is set, this bit will be ignored during masking process * * @param {Number} row * @param {Number} col * @param {Boolean} value * @param {Boolean} reserved */ BitMatrix.prototype.set = function (row, col, value, reserved) { var index = row * this.size + col; this.data[index] = value; if (reserved) this.reservedBit[index] = true; }; /** * Returns bit value at specified location * * @param {Number} row * @param {Number} col * @return {Boolean} */ BitMatrix.prototype.get = function (row, col) { return this.data[row * this.size + col] }; /** * Applies xor operator at specified location * (used during masking process) * * @param {Number} row * @param {Number} col * @param {Boolean} value */ BitMatrix.prototype.xor = function (row, col, value) { this.data[row * this.size + col] ^= value; }; /** * Check if bit at specified location is reserved * * @param {Number} row * @param {Number} col * @return {Boolean} */ BitMatrix.prototype.isReserved = function (row, col) { return this.reservedBit[row * this.size + col] }; bitMatrix = BitMatrix; return bitMatrix; } var alignmentPattern = {}; /** * Alignment pattern are fixed reference pattern in defined positions * in a matrix symbology, which enables the decode software to re-synchronise * the coordinate mapping of the image modules in the event of moderate amounts * of distortion of the image. * * Alignment patterns are present only in QR Code symbols of version 2 or larger * and their number depends on the symbol version. */ var hasRequiredAlignmentPattern; function requireAlignmentPattern () { if (hasRequiredAlignmentPattern) return alignmentPattern; hasRequiredAlignmentPattern = 1; (function (exports) { var getSymbolSize = requireUtils$1().getSymbolSize; /** * Calculate the row/column coordinates of the center module of each alignment pattern * for the specified QR Code version. * * The alignment patterns are positioned symmetrically on either side of the diagonal * running from the top left corner of the symbol to the bottom right corner. * * Since positions are simmetrical only half of the coordinates are returned. * Each item of the array will represent in turn the x and y coordinate. * @see {@link getPositions} * * @param {Number} version QR Code version * @return {Array} Array of coordinate */ exports.getRowColCoords = function getRowColCoords (version) { if (version === 1) return [] var posCount = Math.floor(version / 7) + 2; var size = getSymbolSize(version); var intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2; var positions = [size - 7]; // Last coord is always (size - 7) for (var i = 1; i < posCount - 1; i++) { positions[i] = positions[i - 1] - intervals; } positions.push(6); // First coord is always 6 return positions.reverse() }; /** * Returns an array containing the positions of each alignment pattern. * Each array's element represent the center point of the pattern as (x, y) coordinates * * Coordinates are calculated expanding the row/column coordinates returned by {@link getRowColCoords} * and filtering out the items that overlaps with finder pattern * * @example * For a Version 7 symbol {@link getRowColCoords} returns values 6, 22 and 38. * The alignment patterns, therefore, are to be centered on (row, column) * positions (6,22), (22,6), (22,22), (22,38), (38,22), (38,38). * Note that the coordinates (6,6), (6,38), (38,6) are occupied by finder patterns * and are not therefore used for alignment patterns. * * var pos = getPositions(7) * // [[6,22], [22,6], [22,22], [22,38], [38,22], [38,38]] * * @param {Number} version QR Code version * @return {Array} Array of coordinates */ exports.getPositions = function getPositions (version) { var coords = []; var pos = exports.getRowColCoords(version); var posLength = pos.length; for (var i = 0; i < posLength; i++) { for (var j = 0; j < posLength; j++) { // Skip if position is occupied by finder patterns if ((i === 0 && j === 0) || // top-left (i === 0 && j === posLength - 1) || // bottom-left (i === posLength - 1 && j === 0)) { // top-right continue } coords.push([pos[i], pos[j]]); } } return coords }; } (alignmentPattern)); return alignmentPattern; } var finderPattern = {}; var hasRequiredFinderPattern; function requireFinderPattern () { if (hasRequiredFinderPattern) return finderPattern; hasRequiredFinderPattern = 1; var getSymbolSize = requireUtils$1().getSymbolSize; var FINDER_PATTERN_SIZE = 7; /** * Returns an array containing the positions of each finder pattern. * Each array's element represent the top-left point of the pattern as (x, y) coordinates * * @param {Number} version QR Code version * @return {Array} Array of coordinates */ finderPattern.getPositions = function getPositions (version) { var size = getSymbolSize(version); return [ // top-left [0, 0], // top-right [size - FINDER_PATTERN_SIZE, 0], // bottom-left [0, size - FINDER_PATTERN_SIZE] ] }; return finderPattern; } var maskPattern = {}; /** * Data mask pattern reference * @type {Object} */ var hasRequiredMaskPattern; function requireMaskPattern () { if (hasRequiredMaskPattern) return maskPattern; hasRequiredMaskPattern = 1; (function (exports) { exports.Patterns = { PATTERN000: 0, PATTERN001: 1, PATTERN010: 2, PATTERN011: 3, PATTERN100: 4, PATTERN101: 5, PATTERN110: 6, PATTERN111: 7 }; /** * Weighted penalty scores for the undesirable features * @type {Object} */ var PenaltyScores = { N1: 3, N2: 3, N3: 40, N4: 10 }; /** * Check if mask pattern value is valid * * @param {Number} mask Mask pattern * @return {Boolean} true if valid, false otherwise */ exports.isValid = function isValid (mask) { return mask != null && mask !== '' && !isNaN(mask) && mask >= 0 && mask <= 7 }; /** * Returns mask pattern from a value. * If value is not valid, returns undefined * * @param {Number|String} value Mask pattern value * @return {Number} Valid mask pattern or undefined */ exports.from = function from (value) { return exports.isValid(value) ? parseInt(value, 10) : undefined }; /** * Find adjacent modules in row/column with the same color * and assign a penalty value. * * Points: N1 + i * i is the amount by which the number of adjacent modules of the same color exceeds 5 */ exports.getPenaltyN1 = function getPenaltyN1 (data) { var size = data.size; var points = 0; var sameCountCol = 0; var sameCountRow = 0; var lastCol = null; var lastRow = null; for (var row = 0; row < size; row++) { sameCountCol = sameCountRow = 0; lastCol = lastRow = null; for (var col = 0; col < size; col++) { var module = data.get(row, col); if (module === lastCol) { sameCountCol++; } else { if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5); lastCol = module; sameCountCol = 1; } module = data.get(col, row); if (module === lastRow) { sameCountRow++; } else { if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5); lastRow = module; sameCountRow = 1; } } if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5); if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5); } return points }; /** * Find 2x2 blocks with the same color and assign a penalty value * * Points: N2 * (m - 1) * (n - 1) */ exports.getPenaltyN2 = function getPenaltyN2 (data) { var size = data.size; var points = 0; for (var row = 0; row < size - 1; row++) { for (var col = 0; col < size - 1; col++) { var last = data.get(row, col) + data.get(row, col + 1) + data.get(row + 1, col) + data.get(row + 1, col + 1); if (last === 4 || last === 0) points++; } } return points * PenaltyScores.N2 }; /** * Find 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column, * preceded or followed by light area 4 modules wide * * Points: N3 * number of pattern found */ exports.getPenaltyN3 = function getPenaltyN3 (data) { var size = data.size; var points = 0; var bitsCol = 0; var bitsRow = 0; for (var row = 0; row < size; row++) { bitsCol = bitsRow = 0; for (var col = 0; col < size; col++) { bitsCol = ((bitsCol << 1) & 0x7FF) | data.get(row, col); if (col >= 10 && (bitsCol === 0x5D0 || bitsCol === 0x05D)) points++; bitsRow = ((bitsRow << 1) & 0x7FF) | data.get(col, row); if (col >= 10 && (bitsRow === 0x5D0 || bitsRow === 0x05D)) points++; } } return points * PenaltyScores.N3 }; /** * Calculate proportion of dark modules in entire symbol * * Points: N4 * k * * k is the rating of the deviation of the proportion of dark modules * in the symbol from 50% in steps of 5% */ exports.getPenaltyN4 = function getPenaltyN4 (data) { var darkCount = 0; var modulesCount = data.data.length; for (var i = 0; i < modulesCount; i++) darkCount += data.data[i]; var k = Math.abs(Math.ceil((darkCount * 100 / modulesCount) / 5) - 10); return k * PenaltyScores.N4 }; /** * Return mask value at given position * * @param {Number} maskPattern Pattern reference value * @param {Number} i Row * @param {Number} j Column * @return {Boolean} Mask value */ function getMaskAt (maskPattern, i, j) { switch (maskPattern) { case exports.Patterns.PATTERN000: return (i + j) % 2 === 0 case exports.Patterns.PATTERN001: return i % 2 === 0 case exports.Patterns.PATTERN010: return j % 3 === 0 case exports.Patterns.PATTERN011: return (i + j) % 3 === 0 case exports.Patterns.PATTERN100: return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0 case exports.Patterns.PATTERN101: return (i * j) % 2 + (i * j) % 3 === 0 case exports.Patterns.PATTERN110: return ((i * j) % 2 + (i * j) % 3) % 2 === 0 case exports.Patterns.PATTERN111: return ((i * j) % 3 + (i + j) % 2) % 2 === 0 default: throw new Error('bad maskPattern:' + maskPattern) } } /** * Apply a mask pattern to a BitMatrix * * @param {Number} pattern Pattern reference number * @param {BitMatrix} data BitMatrix data */ exports.applyMask = function applyMask (pattern, data) { var size = data.size; for (var col = 0; col < size; col++) { for (var row = 0; row < size; row++) { if (data.isReserved(row, col)) continue data.xor(row, col, getMaskAt(pattern, row, col)); } } }; /** * Returns the best mask pattern for data * * @param {BitMatrix} data * @return {Number} Mask pattern reference number */ exports.getBestMask = function getBestMask (data, setupFormatFunc) { var numPatterns = Object.keys(exports.Patterns).length; var bestPattern = 0; var lowerPenalty = Infinity; for (var p = 0; p < numPatterns; p++) { setupFormatFunc(p); exports.applyMask(p, data); // Calculate penalty var penalty = exports.getPenaltyN1(data) + exports.getPenaltyN2(data) + exports.getPenaltyN3(data) + exports.getPenaltyN4(data); // Undo previously applied mask exports.applyMask(p, data); if (penalty < lowerPenalty) { lowerPenalty = penalty; bestPattern = p; } } return bestPattern }; } (maskPattern)); return maskPattern; } var errorCorrectionCode = {}; var hasRequiredErrorCorrectionCode; function requireErrorCorrectionCode () { if (hasRequiredErrorCorrectionCode) return errorCorrectionCode; hasRequiredErrorCorrectionCode = 1; var ECLevel = requireErrorCorrectionLevel(); var EC_BLOCKS_TABLE = [ // L M Q H 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 2, 4, 1, 2, 4, 4, 2, 4, 4, 4, 2, 4, 6, 5, 2, 4, 6, 6, 2, 5, 8, 8, 4, 5, 8, 8, 4, 5, 8, 11, 4, 8, 10, 11, 4, 9, 12, 16, 4, 9, 16, 16, 6, 10, 12, 18, 6, 10, 17, 16, 6, 11, 16, 19, 6, 13, 18, 21, 7, 14, 21, 25, 8, 16, 20, 25, 8, 17, 23, 25, 9, 17, 23, 34, 9, 18, 25, 30, 10, 20, 27, 32, 12, 21, 29, 35, 12, 23, 34, 37, 12, 25, 34, 40, 13, 26, 35, 42, 14, 28, 38, 45, 15, 29, 40, 48, 16, 31, 43, 51, 17, 33, 45, 54, 18, 35, 48, 57, 19, 37, 51, 60, 19, 38, 53, 63, 20, 40, 56, 66, 21, 43, 59, 70, 22, 45, 62, 74, 24, 47, 65, 77, 25, 49, 68, 81 ]; var EC_CODEWORDS_TABLE = [ // L M Q H 7, 10, 13, 17, 10, 16, 22, 28, 15, 26, 36, 44, 20, 36, 52, 64, 26, 48, 72, 88, 36, 64, 96, 112, 40, 72, 108, 130, 48, 88, 132, 156, 60, 110, 160, 192, 72, 130, 192, 224, 80, 150, 224, 264, 96, 176, 260, 308, 104, 198, 288, 352, 120, 216, 320, 384, 132, 240, 360, 432, 144, 280, 408, 480, 168, 308, 448, 532, 180, 338, 504, 588, 196, 364, 546, 650, 224, 416, 600, 700, 224, 442, 644, 750, 252, 476, 690, 816, 270, 504, 750, 900, 300, 560, 810, 960, 312, 588, 870, 1050, 336, 644, 952, 1110, 360, 700, 1020, 1200, 390, 728, 1050, 1260, 420, 784, 1140, 1350, 450, 812, 1200, 1440, 480, 868, 1290, 1530, 510, 924, 1350, 1620, 540, 980, 1440, 1710, 570, 1036, 1530, 1800, 570, 1064, 1590, 1890, 600, 1120, 1680, 1980, 630, 1204, 1770, 2100, 660, 1260, 1860, 2220, 720, 1316, 1950, 2310, 750, 1372, 2040, 2430 ]; /** * Returns the number of error correction block that the QR Code should contain * for the specified version and error correction level. * * @param {Number} version QR Code version * @param {Number} errorCorrectionLevel Error correction level * @return {Number} Number of error correction blocks */ errorCorrectionCode.getBlocksCount = function getBlocksCount (version, errorCorrectionLevel) { switch (errorCorrectionLevel) { case ECLevel.L: return EC_BLOCKS_TABLE[(version - 1) * 4 + 0] case ECLevel.M: return EC_BLOCKS_TABLE[(version - 1) * 4 + 1] case ECLevel.Q: return EC_BLOCKS_TABLE[(version - 1) * 4 + 2] case ECLevel.H: return EC_BLOCKS_TABLE[(version - 1) * 4 + 3] default: return undefined } }; /** * Returns the number of error correction codewords to use for the specified * version and error correction level. * * @param {Number} version QR Code version * @param {Number} errorCorrectionLevel Error correction level * @return {Number} Number of error correction codewords */ errorCorrectionCode.getTotalCodewordsCount = function getTotalCodewordsCount (version, errorCorrectionLevel) { switch (errorCorrectionLevel) { case ECLevel.L: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0] case ECLevel.M: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1] case ECLevel.Q: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2] case ECLevel.H: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3] default: return undefined } }; return errorCorrectionCode; } var polynomial = {}; var galoisField = {}; var hasRequiredGaloisField; function requireGaloisField () { if (hasRequiredGaloisField) return galoisField; hasRequiredGaloisField = 1; var BufferUtil = requireBuffer(); var EXP_TABLE = BufferUtil.alloc(512); var LOG_TABLE = BufferUtil.alloc(256) /** * Precompute the log and anti-log tables for faster computation later * * For each possible value in the galois field 2^8, we will pre-compute * the logarithm and anti-logarithm (exponential) of this value * * ref {@link https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders#Introduction_to_mathematical_fields} */ ;(function initTables () { var x = 1; for (var i = 0; i < 255; i++) { EXP_TABLE[i] = x; LOG_TABLE[x] = i; x <<= 1; // multiply by 2 // The QR code specification says to use byte-wise modulo 100011101 arithmetic. // This means that when a number is 256 or larger, it should be XORed with 0x11D. if (x & 0x100) { // similar to x >= 256, but a lot faster (because 0x100 == 256) x ^= 0x11D; } } // Optimization: double the size of the anti-log table so that we don't need to mod 255 to // stay inside the bounds (because we will mainly use this table for the multiplication of // two GF numbers, no more). // @see {@link mul} for (i = 255; i < 512; i++) { EXP_TABLE[i] = EXP_TABLE[i - 255]; } }()); /** * Returns log value of n inside Galois Field * * @param {Number} n * @return {Number} */ galoisField.log = function log (n) { if (n < 1) throw new Error('log(' + n + ')') return LOG_TABLE[n] }; /** * Returns anti-log value of n inside Galois Field * * @param {Number} n * @return {Number} */ galoisField.exp = function exp (n) { return EXP_TABLE[n] }; /** * Multiplies two number inside Galois Field * * @param {Number} x * @param {Number} y * @return {Number} */ galoisField.mul = function mul (x, y) { if (x === 0 || y === 0) return 0 // should be EXP_TABLE[(LOG_TABLE[x] + LOG_TABLE[y]) % 255] if EXP_TABLE wasn't oversized // @see {@link initTables} return EXP_TABLE[LOG_TABLE[x] + LOG_TABLE[y]] }; return galoisField; } var hasRequiredPolynomial; function requirePolynomial () { if (hasRequiredPolynomial) return polynomial; hasRequiredPolynomial = 1; (function (exports) { var BufferUtil = requireBuffer(); var GF = requireGaloisField(); /** * Multiplies two polynomials inside Galois Field * * @param {Buffer} p1 Polynomial * @param {Buffer} p2 Polynomial * @return {Buffer} Product of p1 and p2 */ exports.mul = function mul (p1, p2) { var coeff = BufferUtil.alloc(p1.length + p2.length - 1); for (var i = 0; i < p1.length; i++) { for (var j = 0; j < p2.length; j++) { coeff[i + j] ^= GF.mul(p1[i], p2[j]); } } return coeff }; /** * Calculate the remainder of polynomials division * * @param {Buffer} divident Polynomial * @param {Buffer} divisor Polynomial * @return {Buffer} Remainder */ exports.mod = function mod (divident, divisor) { var result = BufferUtil.from(divident); while ((result.length - divisor.length) >= 0) { var coeff = result[0]; for (var i = 0; i < divisor.length; i++) { result[i] ^= GF.mul(divisor[i], coeff); } // remove all zeros from buffer head var offset = 0; while (offset < result.length && result[offset] === 0) offset++; result = result.slice(offset); } return result }; /** * Generate an irreducible generator polynomial of specified degree * (used by Reed-Solomon encoder) * * @param {Number} degree Degree of the generator polynomial * @return {Buffer} Buffer containing polynomial coefficients */ exports.generateECPolynomial = function generateECPolynomial (degree) { var poly = BufferUtil.from([1]); for (var i = 0; i < degree; i++) { poly = exports.mul(poly, [1, GF.exp(i)]); } return poly }; } (polynomial)); return polynomial; } var reedSolomonEncoder; var hasRequiredReedSolomonEncoder; function requireReedSolomonEncoder () { if (hasRequiredReedSolomonEncoder) return reedSolomonEncoder; hasRequiredReedSolomonEncoder = 1; var BufferUtil = requireBuffer(); var Polynomial = requirePolynomial(); var Buffer = require$$2.Buffer; 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 var pad = BufferUtil.alloc(this.degree); var 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 var 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 var start = this.degree - remainder.length; if (start > 0) { var buff = BufferUtil.alloc(this.degree); remainder.copy(buff, start); return buff } return remainder }; reedSolomonEncoder = ReedSolomonEncoder; return reedSolomonEncoder; } var version = {}; var mode = {}; var versionCheck = {}; /** * Check if QR Code version is valid * * @param {Number} version QR Code version * @return {Boolean} true if valid version, false otherwise */ var hasRequiredVersionCheck; function requireVersionCheck () { if (hasRequiredVersionCheck) return versionCheck; hasRequiredVersionCheck = 1; versionCheck.isValid = function isValid (version) { return !isNaN(version) && version >= 1 && version <= 40 }; return versionCheck; } var regex = {}; var hasRequiredRegex; function requireRegex () { if (hasRequiredRegex) return regex; hasRequiredRegex = 1; var numeric = '[0-9]+'; var alphanumeric = '[A-Z $%*+\\-./:]+'; var kanji = '(?:[u3000-u303F]|[u3040-u309F]|[u30A0-u30FF]|' + '[uFF00-uFFEF]|[u4E00-u9FAF]|[u2605-u2606]|[u2190-u2195]|u203B|' + '[u2010u2015u2018u2019u2025u2026u201Cu201Du2225u2260]|' + '[u0391-u0451]|[u00A7u00A8u00B1u00B4u00D7u00F7])+'; kanji = kanji.replace(/u/g, '\\u'); var byte = '(?:(?![A-Z0-9 $%*+\\-./:]|' + kanji + ')(?:.|[\r\n]))+'; regex.KANJI = new RegExp(kanji, 'g'); regex.BYTE_KANJI = new RegExp('[^A-Z0-9 $%*+\\-./:]+', 'g'); regex.BYTE = new RegExp(byte, 'g'); regex.NUMERIC = new RegExp(numeric, 'g'); regex.ALPHANUMERIC = new RegExp(alphanumeric, 'g'); var TEST_KANJI = new RegExp('^' + kanji + '$'); var TEST_NUMERIC = new RegExp('^' + numeric + '$'); var TEST_ALPHANUMERIC = new RegExp('^[A-Z0-9 $%*+\\-./:]+$'); regex.testKanji = function testKanji (str) { return TEST_KANJI.test(str) }; regex.testNumeric = function testNumeric (str) { return TEST_NUMERIC.test(str) }; regex.testAlphanumeric = function testAlphanumeric (str) { return TEST_ALPHANUMERIC.test(str) }; return regex; } var hasRequiredMode; function requireMode () { if (hasRequiredMode) return mode; hasRequiredMode = 1; (function (exports) { var VersionCheck = requireVersionCheck(); var Regex = requireRegex(); /** * Numeric mode encodes data from the decimal digit set (0 - 9) * (byte values 30HEX to 39HEX). * Normally, 3 data characters are represented by 10 bits. * * @type {Object} */ exports.NUMERIC = { id: 'Numeric', bit: 1 << 0, ccBits: [10, 12, 14] }; /** * Alphanumeric mode encodes data from a set of 45 characters, * i.e. 10 numeric digits (0 - 9), * 26 alphabetic characters (A - Z), * and 9 symbols (SP, $, %, *, +, -, ., /, :). * Normally, two input characters are represented by 11 bits. * * @type {Object} */ exports.ALPHANUMERIC = { id: 'Alphanumeric', bit: 1 << 1, ccBits: [9, 11, 13] }; /** * In byte mode, data is encoded at 8 bits per character. * * @type {Object} */ exports.BYTE = { id: 'Byte', bit: 1 << 2, ccBits: [8, 16, 16] }; /** * The Kanji mode efficiently encodes Kanji characters in accordance with * the Shift JIS system based on JIS X 0208. * The Shift JIS values are shifted from the JIS X 0208 values. * JIS X 0208 gives details of the shift coded representation. * Each two-byte character value is compacted to a 13-bit binary codeword. * * @type {Object} */ exports.KANJI = { id: 'Kanji', bit: 1 << 3, ccBits: [8, 10, 12] }; /** * Mixed mode will contain a sequences of data in a combination of any of * the modes described above * * @type {Object} */ exports.MIXED = { bit: -1 }; /** * Returns the number of bits needed to store the data length * according to QR Code specifications. * * @param {Mode} mode Data mode * @param {Number} version QR Code version * @return {Number} Number of bits */ exports.getCharCountIndicator = function getCharCountIndicator (mode, version) { if (!mode.ccBits) throw new Error('Invalid mode: ' + mode) if (!VersionCheck.isValid(version)) { throw new Error('Invalid version: ' + version) } if (version >= 1 && version < 10) return mode.ccBits[0] else if (version < 27) return mode.ccBits[1] return mode.ccBits[2] }; /** * Returns the most efficient mode to store the specified data * * @param {String} dataStr Input data string * @return {Mode} Best mode */ exports.getBestModeForData = function getBestModeForData (dataStr) { if (Regex.testNumeric(dataStr)) return exports.NUMERIC else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC else if (Regex.testKanji(dataStr)) return exports.KANJI else return exports.BYTE }; /** * Return mode name as string * * @param {Mode} mode Mode object * @returns {String} Mode name */ exports.toString = function toString (mode) { if (mode && mode.id) return mode.id throw new Error('Invalid mode') }; /** * Check if input param is a valid mode object * * @param {Mode} mode Mode object * @returns {Boolean} True if valid mode, false otherwise */ exports.isValid = function isValid (mode) { return mode && mode.bit && mode.ccBits }; /** * Get mode object from its name * * @param {String} string Mode name * @returns {Mode} Mode object */ function fromString (string) { if (typeof string !== 'string') { throw new Error('Param is not a string') } var lcStr = string.toLowerCase(); switch (lcStr) { case 'numeric': return exports.NUMERIC case 'alphanumeric': return exports.ALPHANUMERIC case 'kanji': return exports.KANJI case 'byte': return exports.BYTE default: throw new Error('Unknown mode: ' + string) } } /** * Returns mode from a value. * If value is not a valid mode, returns defaultValue * * @param {Mode|String} value Encoding mode * @param {Mode} defaultValue Fallback value * @return {Mode} Encoding mode */ exports.from = function from (value, defaultValue) { if (exports.isValid(value)) { return value } try { return fromString(value) } catch (e) { return defaultValue } }; } (mode)); return mode; } var isarray; var hasRequiredIsarray; function requireIsarray () { if (hasRequiredIsarray) return isarray; hasRequiredIsarray = 1; var toString = {}.toString; isarray = Array.isArray || function (arr) { return toString.call(arr) == '[object Array]'; }; return isarray; } var hasRequiredVersion; function requireVersion () { if (hasRequiredVersion) return version; hasRequiredVersion = 1; (function (exports) { var Utils = requireUtils$1(); var ECCode = requireErrorCorrectionCode(); var ECLevel = requireErrorCorrectionLevel(); var Mode = requireMode(); var VersionCheck = requireVersionCheck(); var isArray = requireIsarray(); // Generator polynomial used to encode version information var G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0); var G18_BCH = Utils.getBCHDigit(G18); function getBestVersionForDataLength (mode, length, errorCorrectionLevel) { for (var currentVersion = 1; currentVersion <= 40; currentVersion++) { if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, mode)) { return currentVersion } } return undefined } function getReservedBitsCount (mode, version) { // Character count indicator + mode indicator bits return Mode.getCharCountIndicator(mode, version) + 4 } function getTotalBitsFromDataArray (segments, version) { var totalBits = 0; segments.forEach(function (data) { var reservedBits = getReservedBitsCount(data.mode, version); totalBits += reservedBits + data.getBitsLength(); }); return totalBits } function getBestVersionForMixedData (segments, errorCorrectionLevel) { for (var currentVersion = 1; currentVersion <= 40; currentVersion++) { var length = getTotalBitsFromDataArray(segments, currentVersion); if (length <= exports.getCapacity(currentVersion, errorCorrectionLevel, Mode.MIXED)) { return currentVersion } } return undefined } /** * Returns version number from a value. * If value is not a valid version, returns defaultValue * * @param {Number|String} value QR Code version * @param {Number} defaultValue Fallback value * @return {Number} QR Code version number */ exports.from = function from (value, defaultValue) { if (VersionCheck.isValid(value)) { return parseInt(value, 10) } return defaultValue }; /** * Returns how much data can be stored with the specified QR code version * and error correction level * * @param {Number} version QR Code version (1-40) * @param {Number} errorCorrectionLevel Error correction level * @param {Mode} mode Data mode * @return {Number} Quantity of storable data */ exports.getCapacity = function getCapacity (version, errorCorrectionLevel, mode) { if (!VersionCheck.isValid(version)) { throw new Error('Invalid QR Code version') } // Use Byte mode as default if (typeof mode === 'undefined') mode = Mode.BYTE; // Total codewords for this QR code version (Data + Error correction) var totalCodewords = Utils.getSymbolTotalCodewords(version); // Total number of error correction codewords var ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel); // Total number of data codewords var dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8; if (mode === Mode.MIXED) return dataTotalCodewordsBits var usableBits = dataTotalCodewordsBits - getReservedBitsCount(mode, version); // Return max number of storable codewords switch (mode) { case Mode.NUMERIC: return Math.floor((usableBits / 10) * 3) case Mode.ALPHANUMERIC: return Math.floor((usableBits / 11) * 2) case Mode.KANJI: return Math.floor(usableBits / 13) case Mode.BYTE: default: return Math.floor(usableBits / 8) } }; /** * Returns the minimum version needed to contain the amount of data * * @param {Segment} data Segment of data * @param {Number} [errorCorrectionLevel=H] Error correction level * @param {Mode} mode Data mode * @return {Number} QR Code version */ exports.getBestVersionForData = function getBestVersionForData (data, errorCorrectionLevel) { var seg; var ecl = ECLevel.from(errorCorrectionLevel, ECLevel.M); if (isArray(data)) { if (data.length > 1) { return getBestVersionForMixedData(data, ecl) } if (data.length === 0) { return 1 } seg = data[0]; } else { seg = data; } return getBestVersionForDataLength(seg.mode, seg.getLength(), ecl) }; /** * Returns version information with relative error correction bits * * The version information is included in QR Code symbols of version 7 or larger. * It consists of an 18-bit sequence containing 6 data bits, * with 12 error correction bits calculated using the (18, 6) Golay code. * * @param {Number} version QR Code version * @return {Number} Encoded version info bits */ exports.getEncodedBits = function getEncodedBits (version) { if (!VersionCheck.isValid(version) || version < 7) { throw new Error('Invalid QR Code version') } var d = version << 12; while (Utils.getBCHDigit(d) - G18_BCH >= 0) { d ^= (G18 << (Utils.getBCHDigit(d) - G18_BCH)); } return (version << 12) | d }; } (version)); return version; } var formatInfo = {}; var hasRequiredFormatInfo; function requireFormatInfo () { if (hasRequiredFormatInfo) return formatInfo; hasRequiredFormatInfo = 1; var Utils = requireUtils$1(); var G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0); var G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1); var G15_BCH = Utils.getBCHDigit(G15); /** * Returns format information with relative error correction bits * * The format information is a 15-bit sequence containing 5 data bits, * with 10 error correction bits calculated using the (15, 5) BCH code. * * @param {Number} errorCorrectionLevel Error correction level * @param {Number} mask Mask pattern * @return {Number} Encoded format information bits */ formatInfo.getE