@zmainer/dsh-wx-bridge 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +201 -0
  3. package/cordis.patch.yml +3 -0
  4. package/lib/client.js +440 -0
  5. package/lib/index.js +650 -0
  6. package/lib/kernel/acp-overlay-chat.yml +26 -0
  7. package/lib/kernel/acp-overlay.yml +22 -0
  8. package/lib/kernel/acp-preset-shim.mjs +162 -0
  9. package/lib/kernel/acp.mjs +232 -0
  10. package/lib/kernel/bridge.mjs +1341 -0
  11. package/lib/kernel/keeper.mjs +302 -0
  12. package/lib/kernel/second-brain-contract.txt +7 -0
  13. package/lib/pairing.js +225 -0
  14. package/lib/vendor/qr-svg.cjs +58 -0
  15. package/lib/vendor/qrcode-core/LICENSE +10 -0
  16. package/lib/vendor/qrcode-core/NOTICE.md +21 -0
  17. package/lib/vendor/qrcode-core/alignment-pattern.js +83 -0
  18. package/lib/vendor/qrcode-core/alphanumeric-data.js +59 -0
  19. package/lib/vendor/qrcode-core/bit-buffer.js +37 -0
  20. package/lib/vendor/qrcode-core/bit-matrix.js +65 -0
  21. package/lib/vendor/qrcode-core/byte-data.js +30 -0
  22. package/lib/vendor/qrcode-core/dijkstrajs.LICENSE +19 -0
  23. package/lib/vendor/qrcode-core/dijkstrajs.js +165 -0
  24. package/lib/vendor/qrcode-core/error-correction-code.js +135 -0
  25. package/lib/vendor/qrcode-core/error-correction-level.js +50 -0
  26. package/lib/vendor/qrcode-core/finder-pattern.js +22 -0
  27. package/lib/vendor/qrcode-core/format-info.js +29 -0
  28. package/lib/vendor/qrcode-core/galois-field.js +69 -0
  29. package/lib/vendor/qrcode-core/kanji-data.js +54 -0
  30. package/lib/vendor/qrcode-core/mask-pattern.js +234 -0
  31. package/lib/vendor/qrcode-core/mode.js +167 -0
  32. package/lib/vendor/qrcode-core/numeric-data.js +43 -0
  33. package/lib/vendor/qrcode-core/package.json +8 -0
  34. package/lib/vendor/qrcode-core/polynomial.js +62 -0
  35. package/lib/vendor/qrcode-core/qrcode.js +495 -0
  36. package/lib/vendor/qrcode-core/reed-solomon-encoder.js +56 -0
  37. package/lib/vendor/qrcode-core/regex.js +31 -0
  38. package/lib/vendor/qrcode-core/segments.js +330 -0
  39. package/lib/vendor/qrcode-core/utils.js +63 -0
  40. package/lib/vendor/qrcode-core/version-check.js +9 -0
  41. package/lib/vendor/qrcode-core/version.js +163 -0
  42. package/package.json +40 -0
  43. package/scripts/build-client.mjs +35 -0
  44. package/src/client.js +427 -0
@@ -0,0 +1,69 @@
1
+ const EXP_TABLE = new Uint8Array(512)
2
+ const LOG_TABLE = new Uint8Array(256)
3
+ /**
4
+ * Precompute the log and anti-log tables for faster computation later
5
+ *
6
+ * For each possible value in the galois field 2^8, we will pre-compute
7
+ * the logarithm and anti-logarithm (exponential) of this value
8
+ *
9
+ * ref {@link https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders#Introduction_to_mathematical_fields}
10
+ */
11
+ ;(function initTables () {
12
+ let x = 1
13
+ for (let i = 0; i < 255; i++) {
14
+ EXP_TABLE[i] = x
15
+ LOG_TABLE[x] = i
16
+
17
+ x <<= 1 // multiply by 2
18
+
19
+ // The QR code specification says to use byte-wise modulo 100011101 arithmetic.
20
+ // This means that when a number is 256 or larger, it should be XORed with 0x11D.
21
+ if (x & 0x100) { // similar to x >= 256, but a lot faster (because 0x100 == 256)
22
+ x ^= 0x11D
23
+ }
24
+ }
25
+
26
+ // Optimization: double the size of the anti-log table so that we don't need to mod 255 to
27
+ // stay inside the bounds (because we will mainly use this table for the multiplication of
28
+ // two GF numbers, no more).
29
+ // @see {@link mul}
30
+ for (let i = 255; i < 512; i++) {
31
+ EXP_TABLE[i] = EXP_TABLE[i - 255]
32
+ }
33
+ }())
34
+
35
+ /**
36
+ * Returns log value of n inside Galois Field
37
+ *
38
+ * @param {Number} n
39
+ * @return {Number}
40
+ */
41
+ exports.log = function log (n) {
42
+ if (n < 1) throw new Error('log(' + n + ')')
43
+ return LOG_TABLE[n]
44
+ }
45
+
46
+ /**
47
+ * Returns anti-log value of n inside Galois Field
48
+ *
49
+ * @param {Number} n
50
+ * @return {Number}
51
+ */
52
+ exports.exp = function exp (n) {
53
+ return EXP_TABLE[n]
54
+ }
55
+
56
+ /**
57
+ * Multiplies two number inside Galois Field
58
+ *
59
+ * @param {Number} x
60
+ * @param {Number} y
61
+ * @return {Number}
62
+ */
63
+ exports.mul = function mul (x, y) {
64
+ if (x === 0 || y === 0) return 0
65
+
66
+ // should be EXP_TABLE[(LOG_TABLE[x] + LOG_TABLE[y]) % 255] if EXP_TABLE wasn't oversized
67
+ // @see {@link initTables}
68
+ return EXP_TABLE[LOG_TABLE[x] + LOG_TABLE[y]]
69
+ }
@@ -0,0 +1,54 @@
1
+ const Mode = require('./mode')
2
+ const Utils = require('./utils')
3
+
4
+ function KanjiData (data) {
5
+ this.mode = Mode.KANJI
6
+ this.data = data
7
+ }
8
+
9
+ KanjiData.getBitsLength = function getBitsLength (length) {
10
+ return length * 13
11
+ }
12
+
13
+ KanjiData.prototype.getLength = function getLength () {
14
+ return this.data.length
15
+ }
16
+
17
+ KanjiData.prototype.getBitsLength = function getBitsLength () {
18
+ return KanjiData.getBitsLength(this.data.length)
19
+ }
20
+
21
+ KanjiData.prototype.write = function (bitBuffer) {
22
+ let i
23
+
24
+ // In the Shift JIS system, Kanji characters are represented by a two byte combination.
25
+ // These byte values are shifted from the JIS X 0208 values.
26
+ // JIS X 0208 gives details of the shift coded representation.
27
+ for (i = 0; i < this.data.length; i++) {
28
+ let value = Utils.toSJIS(this.data[i])
29
+
30
+ // For characters with Shift JIS values from 0x8140 to 0x9FFC:
31
+ if (value >= 0x8140 && value <= 0x9FFC) {
32
+ // Subtract 0x8140 from Shift JIS value
33
+ value -= 0x8140
34
+
35
+ // For characters with Shift JIS values from 0xE040 to 0xEBBF
36
+ } else if (value >= 0xE040 && value <= 0xEBBF) {
37
+ // Subtract 0xC140 from Shift JIS value
38
+ value -= 0xC140
39
+ } else {
40
+ throw new Error(
41
+ 'Invalid SJIS character: ' + this.data[i] + '\n' +
42
+ 'Make sure your charset is UTF-8')
43
+ }
44
+
45
+ // Multiply most significant byte of result by 0xC0
46
+ // and add least significant byte to product
47
+ value = (((value >>> 8) & 0xff) * 0xC0) + (value & 0xff)
48
+
49
+ // Convert result to a 13-bit binary string
50
+ bitBuffer.put(value, 13)
51
+ }
52
+ }
53
+
54
+ module.exports = KanjiData
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Data mask pattern reference
3
+ * @type {Object}
4
+ */
5
+ exports.Patterns = {
6
+ PATTERN000: 0,
7
+ PATTERN001: 1,
8
+ PATTERN010: 2,
9
+ PATTERN011: 3,
10
+ PATTERN100: 4,
11
+ PATTERN101: 5,
12
+ PATTERN110: 6,
13
+ PATTERN111: 7
14
+ }
15
+
16
+ /**
17
+ * Weighted penalty scores for the undesirable features
18
+ * @type {Object}
19
+ */
20
+ const PenaltyScores = {
21
+ N1: 3,
22
+ N2: 3,
23
+ N3: 40,
24
+ N4: 10
25
+ }
26
+
27
+ /**
28
+ * Check if mask pattern value is valid
29
+ *
30
+ * @param {Number} mask Mask pattern
31
+ * @return {Boolean} true if valid, false otherwise
32
+ */
33
+ exports.isValid = function isValid (mask) {
34
+ return mask != null && mask !== '' && !isNaN(mask) && mask >= 0 && mask <= 7
35
+ }
36
+
37
+ /**
38
+ * Returns mask pattern from a value.
39
+ * If value is not valid, returns undefined
40
+ *
41
+ * @param {Number|String} value Mask pattern value
42
+ * @return {Number} Valid mask pattern or undefined
43
+ */
44
+ exports.from = function from (value) {
45
+ return exports.isValid(value) ? parseInt(value, 10) : undefined
46
+ }
47
+
48
+ /**
49
+ * Find adjacent modules in row/column with the same color
50
+ * and assign a penalty value.
51
+ *
52
+ * Points: N1 + i
53
+ * i is the amount by which the number of adjacent modules of the same color exceeds 5
54
+ */
55
+ exports.getPenaltyN1 = function getPenaltyN1 (data) {
56
+ const size = data.size
57
+ let points = 0
58
+ let sameCountCol = 0
59
+ let sameCountRow = 0
60
+ let lastCol = null
61
+ let lastRow = null
62
+
63
+ for (let row = 0; row < size; row++) {
64
+ sameCountCol = sameCountRow = 0
65
+ lastCol = lastRow = null
66
+
67
+ for (let col = 0; col < size; col++) {
68
+ let module = data.get(row, col)
69
+ if (module === lastCol) {
70
+ sameCountCol++
71
+ } else {
72
+ if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5)
73
+ lastCol = module
74
+ sameCountCol = 1
75
+ }
76
+
77
+ module = data.get(col, row)
78
+ if (module === lastRow) {
79
+ sameCountRow++
80
+ } else {
81
+ if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5)
82
+ lastRow = module
83
+ sameCountRow = 1
84
+ }
85
+ }
86
+
87
+ if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5)
88
+ if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5)
89
+ }
90
+
91
+ return points
92
+ }
93
+
94
+ /**
95
+ * Find 2x2 blocks with the same color and assign a penalty value
96
+ *
97
+ * Points: N2 * (m - 1) * (n - 1)
98
+ */
99
+ exports.getPenaltyN2 = function getPenaltyN2 (data) {
100
+ const size = data.size
101
+ let points = 0
102
+
103
+ for (let row = 0; row < size - 1; row++) {
104
+ for (let col = 0; col < size - 1; col++) {
105
+ const last = data.get(row, col) +
106
+ data.get(row, col + 1) +
107
+ data.get(row + 1, col) +
108
+ data.get(row + 1, col + 1)
109
+
110
+ if (last === 4 || last === 0) points++
111
+ }
112
+ }
113
+
114
+ return points * PenaltyScores.N2
115
+ }
116
+
117
+ /**
118
+ * Find 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column,
119
+ * preceded or followed by light area 4 modules wide
120
+ *
121
+ * Points: N3 * number of pattern found
122
+ */
123
+ exports.getPenaltyN3 = function getPenaltyN3 (data) {
124
+ const size = data.size
125
+ let points = 0
126
+ let bitsCol = 0
127
+ let bitsRow = 0
128
+
129
+ for (let row = 0; row < size; row++) {
130
+ bitsCol = bitsRow = 0
131
+ for (let col = 0; col < size; col++) {
132
+ bitsCol = ((bitsCol << 1) & 0x7FF) | data.get(row, col)
133
+ if (col >= 10 && (bitsCol === 0x5D0 || bitsCol === 0x05D)) points++
134
+
135
+ bitsRow = ((bitsRow << 1) & 0x7FF) | data.get(col, row)
136
+ if (col >= 10 && (bitsRow === 0x5D0 || bitsRow === 0x05D)) points++
137
+ }
138
+ }
139
+
140
+ return points * PenaltyScores.N3
141
+ }
142
+
143
+ /**
144
+ * Calculate proportion of dark modules in entire symbol
145
+ *
146
+ * Points: N4 * k
147
+ *
148
+ * k is the rating of the deviation of the proportion of dark modules
149
+ * in the symbol from 50% in steps of 5%
150
+ */
151
+ exports.getPenaltyN4 = function getPenaltyN4 (data) {
152
+ let darkCount = 0
153
+ const modulesCount = data.data.length
154
+
155
+ for (let i = 0; i < modulesCount; i++) darkCount += data.data[i]
156
+
157
+ const k = Math.abs(Math.ceil((darkCount * 100 / modulesCount) / 5) - 10)
158
+
159
+ return k * PenaltyScores.N4
160
+ }
161
+
162
+ /**
163
+ * Return mask value at given position
164
+ *
165
+ * @param {Number} maskPattern Pattern reference value
166
+ * @param {Number} i Row
167
+ * @param {Number} j Column
168
+ * @return {Boolean} Mask value
169
+ */
170
+ function getMaskAt (maskPattern, i, j) {
171
+ switch (maskPattern) {
172
+ case exports.Patterns.PATTERN000: return (i + j) % 2 === 0
173
+ case exports.Patterns.PATTERN001: return i % 2 === 0
174
+ case exports.Patterns.PATTERN010: return j % 3 === 0
175
+ case exports.Patterns.PATTERN011: return (i + j) % 3 === 0
176
+ case exports.Patterns.PATTERN100: return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0
177
+ case exports.Patterns.PATTERN101: return (i * j) % 2 + (i * j) % 3 === 0
178
+ case exports.Patterns.PATTERN110: return ((i * j) % 2 + (i * j) % 3) % 2 === 0
179
+ case exports.Patterns.PATTERN111: return ((i * j) % 3 + (i + j) % 2) % 2 === 0
180
+
181
+ default: throw new Error('bad maskPattern:' + maskPattern)
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Apply a mask pattern to a BitMatrix
187
+ *
188
+ * @param {Number} pattern Pattern reference number
189
+ * @param {BitMatrix} data BitMatrix data
190
+ */
191
+ exports.applyMask = function applyMask (pattern, data) {
192
+ const size = data.size
193
+
194
+ for (let col = 0; col < size; col++) {
195
+ for (let row = 0; row < size; row++) {
196
+ if (data.isReserved(row, col)) continue
197
+ data.xor(row, col, getMaskAt(pattern, row, col))
198
+ }
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Returns the best mask pattern for data
204
+ *
205
+ * @param {BitMatrix} data
206
+ * @return {Number} Mask pattern reference number
207
+ */
208
+ exports.getBestMask = function getBestMask (data, setupFormatFunc) {
209
+ const numPatterns = Object.keys(exports.Patterns).length
210
+ let bestPattern = 0
211
+ let lowerPenalty = Infinity
212
+
213
+ for (let p = 0; p < numPatterns; p++) {
214
+ setupFormatFunc(p)
215
+ exports.applyMask(p, data)
216
+
217
+ // Calculate penalty
218
+ const penalty =
219
+ exports.getPenaltyN1(data) +
220
+ exports.getPenaltyN2(data) +
221
+ exports.getPenaltyN3(data) +
222
+ exports.getPenaltyN4(data)
223
+
224
+ // Undo previously applied mask
225
+ exports.applyMask(p, data)
226
+
227
+ if (penalty < lowerPenalty) {
228
+ lowerPenalty = penalty
229
+ bestPattern = p
230
+ }
231
+ }
232
+
233
+ return bestPattern
234
+ }
@@ -0,0 +1,167 @@
1
+ const VersionCheck = require('./version-check')
2
+ const Regex = require('./regex')
3
+
4
+ /**
5
+ * Numeric mode encodes data from the decimal digit set (0 - 9)
6
+ * (byte values 30HEX to 39HEX).
7
+ * Normally, 3 data characters are represented by 10 bits.
8
+ *
9
+ * @type {Object}
10
+ */
11
+ exports.NUMERIC = {
12
+ id: 'Numeric',
13
+ bit: 1 << 0,
14
+ ccBits: [10, 12, 14]
15
+ }
16
+
17
+ /**
18
+ * Alphanumeric mode encodes data from a set of 45 characters,
19
+ * i.e. 10 numeric digits (0 - 9),
20
+ * 26 alphabetic characters (A - Z),
21
+ * and 9 symbols (SP, $, %, *, +, -, ., /, :).
22
+ * Normally, two input characters are represented by 11 bits.
23
+ *
24
+ * @type {Object}
25
+ */
26
+ exports.ALPHANUMERIC = {
27
+ id: 'Alphanumeric',
28
+ bit: 1 << 1,
29
+ ccBits: [9, 11, 13]
30
+ }
31
+
32
+ /**
33
+ * In byte mode, data is encoded at 8 bits per character.
34
+ *
35
+ * @type {Object}
36
+ */
37
+ exports.BYTE = {
38
+ id: 'Byte',
39
+ bit: 1 << 2,
40
+ ccBits: [8, 16, 16]
41
+ }
42
+
43
+ /**
44
+ * The Kanji mode efficiently encodes Kanji characters in accordance with
45
+ * the Shift JIS system based on JIS X 0208.
46
+ * The Shift JIS values are shifted from the JIS X 0208 values.
47
+ * JIS X 0208 gives details of the shift coded representation.
48
+ * Each two-byte character value is compacted to a 13-bit binary codeword.
49
+ *
50
+ * @type {Object}
51
+ */
52
+ exports.KANJI = {
53
+ id: 'Kanji',
54
+ bit: 1 << 3,
55
+ ccBits: [8, 10, 12]
56
+ }
57
+
58
+ /**
59
+ * Mixed mode will contain a sequences of data in a combination of any of
60
+ * the modes described above
61
+ *
62
+ * @type {Object}
63
+ */
64
+ exports.MIXED = {
65
+ bit: -1
66
+ }
67
+
68
+ /**
69
+ * Returns the number of bits needed to store the data length
70
+ * according to QR Code specifications.
71
+ *
72
+ * @param {Mode} mode Data mode
73
+ * @param {Number} version QR Code version
74
+ * @return {Number} Number of bits
75
+ */
76
+ exports.getCharCountIndicator = function getCharCountIndicator (mode, version) {
77
+ if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
78
+
79
+ if (!VersionCheck.isValid(version)) {
80
+ throw new Error('Invalid version: ' + version)
81
+ }
82
+
83
+ if (version >= 1 && version < 10) return mode.ccBits[0]
84
+ else if (version < 27) return mode.ccBits[1]
85
+ return mode.ccBits[2]
86
+ }
87
+
88
+ /**
89
+ * Returns the most efficient mode to store the specified data
90
+ *
91
+ * @param {String} dataStr Input data string
92
+ * @return {Mode} Best mode
93
+ */
94
+ exports.getBestModeForData = function getBestModeForData (dataStr) {
95
+ if (Regex.testNumeric(dataStr)) return exports.NUMERIC
96
+ else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC
97
+ else if (Regex.testKanji(dataStr)) return exports.KANJI
98
+ else return exports.BYTE
99
+ }
100
+
101
+ /**
102
+ * Return mode name as string
103
+ *
104
+ * @param {Mode} mode Mode object
105
+ * @returns {String} Mode name
106
+ */
107
+ exports.toString = function toString (mode) {
108
+ if (mode && mode.id) return mode.id
109
+ throw new Error('Invalid mode')
110
+ }
111
+
112
+ /**
113
+ * Check if input param is a valid mode object
114
+ *
115
+ * @param {Mode} mode Mode object
116
+ * @returns {Boolean} True if valid mode, false otherwise
117
+ */
118
+ exports.isValid = function isValid (mode) {
119
+ return mode && mode.bit && mode.ccBits
120
+ }
121
+
122
+ /**
123
+ * Get mode object from its name
124
+ *
125
+ * @param {String} string Mode name
126
+ * @returns {Mode} Mode object
127
+ */
128
+ function fromString (string) {
129
+ if (typeof string !== 'string') {
130
+ throw new Error('Param is not a string')
131
+ }
132
+
133
+ const lcStr = string.toLowerCase()
134
+
135
+ switch (lcStr) {
136
+ case 'numeric':
137
+ return exports.NUMERIC
138
+ case 'alphanumeric':
139
+ return exports.ALPHANUMERIC
140
+ case 'kanji':
141
+ return exports.KANJI
142
+ case 'byte':
143
+ return exports.BYTE
144
+ default:
145
+ throw new Error('Unknown mode: ' + string)
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Returns mode from a value.
151
+ * If value is not a valid mode, returns defaultValue
152
+ *
153
+ * @param {Mode|String} value Encoding mode
154
+ * @param {Mode} defaultValue Fallback value
155
+ * @return {Mode} Encoding mode
156
+ */
157
+ exports.from = function from (value, defaultValue) {
158
+ if (exports.isValid(value)) {
159
+ return value
160
+ }
161
+
162
+ try {
163
+ return fromString(value)
164
+ } catch (e) {
165
+ return defaultValue
166
+ }
167
+ }
@@ -0,0 +1,43 @@
1
+ const Mode = require('./mode')
2
+
3
+ function NumericData (data) {
4
+ this.mode = Mode.NUMERIC
5
+ this.data = data.toString()
6
+ }
7
+
8
+ NumericData.getBitsLength = function getBitsLength (length) {
9
+ return 10 * Math.floor(length / 3) + ((length % 3) ? ((length % 3) * 3 + 1) : 0)
10
+ }
11
+
12
+ NumericData.prototype.getLength = function getLength () {
13
+ return this.data.length
14
+ }
15
+
16
+ NumericData.prototype.getBitsLength = function getBitsLength () {
17
+ return NumericData.getBitsLength(this.data.length)
18
+ }
19
+
20
+ NumericData.prototype.write = function write (bitBuffer) {
21
+ let i, group, value
22
+
23
+ // The input data string is divided into groups of three digits,
24
+ // and each group is converted to its 10-bit binary equivalent.
25
+ for (i = 0; i + 3 <= this.data.length; i += 3) {
26
+ group = this.data.substr(i, 3)
27
+ value = parseInt(group, 10)
28
+
29
+ bitBuffer.put(value, 10)
30
+ }
31
+
32
+ // If the number of input digits is not an exact multiple of three,
33
+ // the final one or two digits are converted to 4 or 7 bits respectively.
34
+ const remainingNum = this.data.length - i
35
+ if (remainingNum > 0) {
36
+ group = this.data.substr(i)
37
+ value = parseInt(group, 10)
38
+
39
+ bitBuffer.put(value, remainingNum * 3 + 1)
40
+ }
41
+ }
42
+
43
+ module.exports = NumericData
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "qrcode-core-vendored",
3
+ "private": true,
4
+ "version": "1.5.4",
5
+ "description": "Vendored copy of qrcode@1.5.4 lib/core (MIT, Copyright (c) 2012 Ryan Day). CommonJS on purpose: the plugin package itself is type=module, so this nested manifest keeps these files CJS.",
6
+ "type": "commonjs",
7
+ "license": "MIT"
8
+ }
@@ -0,0 +1,62 @@
1
+ const GF = require('./galois-field')
2
+
3
+ /**
4
+ * Multiplies two polynomials inside Galois Field
5
+ *
6
+ * @param {Uint8Array} p1 Polynomial
7
+ * @param {Uint8Array} p2 Polynomial
8
+ * @return {Uint8Array} Product of p1 and p2
9
+ */
10
+ exports.mul = function mul (p1, p2) {
11
+ const coeff = new Uint8Array(p1.length + p2.length - 1)
12
+
13
+ for (let i = 0; i < p1.length; i++) {
14
+ for (let j = 0; j < p2.length; j++) {
15
+ coeff[i + j] ^= GF.mul(p1[i], p2[j])
16
+ }
17
+ }
18
+
19
+ return coeff
20
+ }
21
+
22
+ /**
23
+ * Calculate the remainder of polynomials division
24
+ *
25
+ * @param {Uint8Array} divident Polynomial
26
+ * @param {Uint8Array} divisor Polynomial
27
+ * @return {Uint8Array} Remainder
28
+ */
29
+ exports.mod = function mod (divident, divisor) {
30
+ let result = new Uint8Array(divident)
31
+
32
+ while ((result.length - divisor.length) >= 0) {
33
+ const coeff = result[0]
34
+
35
+ for (let i = 0; i < divisor.length; i++) {
36
+ result[i] ^= GF.mul(divisor[i], coeff)
37
+ }
38
+
39
+ // remove all zeros from buffer head
40
+ let offset = 0
41
+ while (offset < result.length && result[offset] === 0) offset++
42
+ result = result.slice(offset)
43
+ }
44
+
45
+ return result
46
+ }
47
+
48
+ /**
49
+ * Generate an irreducible generator polynomial of specified degree
50
+ * (used by Reed-Solomon encoder)
51
+ *
52
+ * @param {Number} degree Degree of the generator polynomial
53
+ * @return {Uint8Array} Buffer containing polynomial coefficients
54
+ */
55
+ exports.generateECPolynomial = function generateECPolynomial (degree) {
56
+ let poly = new Uint8Array([1])
57
+ for (let i = 0; i < degree; i++) {
58
+ poly = exports.mul(poly, new Uint8Array([1, GF.exp(i)]))
59
+ }
60
+
61
+ return poly
62
+ }