@hiroosy/baileys-lite 0.0.4 → 0.3.1
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.
- package/LICENSE +21 -0
- package/README.md +658 -10
- package/WAProto/index.js +2 -2558
- package/engine-requirements.js +3 -4
- package/package.json +8 -3
- package/src/binary/wa-binary.js +3 -25
- package/src/constants.js +7 -15
- package/src/defaults.js +2 -5
- package/src/foundation/ai-rich.js +530 -0
- package/src/foundation/audio-duration.js +43 -103
- package/src/foundation/audio-samples.js +19 -37
- package/src/foundation/boom.js +0 -7
- package/src/foundation/cache.js +4 -26
- package/src/foundation/concurrency.js +1 -19
- package/src/foundation/curve25519.js +7 -39
- package/src/foundation/link-preview.js +15 -57
- package/src/foundation/logger.js +0 -11
- package/src/foundation/long.js +4 -14
- package/src/foundation/protobuf-minimal.js +21 -53
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +2 -31
- package/src/foundation/zip.js +25 -41
- package/src/index.js +8 -18
- package/src/signal/libsignal.js +23 -78
- package/src/signal/signal-group.js +0 -35
- package/src/signal/signal-repository.js +10 -29
- package/src/socket/business-communities.js +12 -31
- package/src/socket/chats.js +5 -8
- package/src/socket/groups-newsletter.js +18 -17
- package/src/socket/messages-recv.js +81 -127
- package/src/socket/messages-send.js +372 -139
- package/src/socket/socket-core.js +227 -157
- package/src/socket/usync.js +2 -6
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +84 -38
- package/src/utils/auth-state-core.js +14 -56
- package/src/utils/auth-state-storage.js +7 -42
- package/src/utils/chat-sync.js +4 -26
- package/src/utils/media.js +15 -71
- package/src/utils/message-compose.js +208 -85
- package/src/utils/message-processing.js +10 -65
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +71 -127
- package/src/vendor/jpeg-js/decoder.js +75 -136
- package/src/vendor/jpeg-js/encoder.js +179 -264
- package/src/vendor/pngjs/bitmapper.js +5 -27
- package/src/vendor/pngjs/bitpacker.js +4 -12
- package/src/vendor/pngjs/constants.js +3 -11
- package/src/vendor/pngjs/crc.js +0 -6
- package/src/vendor/pngjs/filter-pack.js +1 -7
- package/src/vendor/pngjs/filter-parse-sync.js +0 -6
- package/src/vendor/pngjs/filter-parse.js +1 -11
- package/src/vendor/pngjs/format-normaliser.js +5 -11
- package/src/vendor/pngjs/interlace.js +7 -24
- package/src/vendor/pngjs/packer-sync.js +0 -11
- package/src/vendor/pngjs/packer.js +7 -15
- package/src/vendor/pngjs/paeth-predictor.js +0 -6
- package/src/vendor/pngjs/parser-sync.js +0 -8
- package/src/vendor/pngjs/parser.js +5 -29
- package/src/vendor/pngjs/png-sync.js +0 -7
- package/src/vendor/pngjs/sync-inflate.js +7 -16
- package/src/vendor/pngjs/sync-reader.js +4 -11
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1705 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +628 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +0 -1
- package/src/wam/wam.js +2 -7
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
const _QRErrorCorrectLevel = { L: 1, M: 0, Q: 3, H: 2 }
|
|
2
|
+
|
|
3
|
+
const _QRMath = (() => {
|
|
4
|
+
const EXP = new Array(256), LOG = new Array(256)
|
|
5
|
+
for (let i = 0; i < 8; i++) EXP[i] = 1 << i
|
|
6
|
+
for (let i = 8; i < 256; i++) EXP[i] = EXP[i-4] ^ EXP[i-5] ^ EXP[i-6] ^ EXP[i-8]
|
|
7
|
+
for (let i = 0; i < 255; i++) LOG[EXP[i]] = i
|
|
8
|
+
return {
|
|
9
|
+
glog: n => { if (n < 1) throw new Error('glog(' + n + ')'); return LOG[n] },
|
|
10
|
+
gexp: n => EXP[((n % 255) + 255) % 255],
|
|
11
|
+
}
|
|
12
|
+
})()
|
|
13
|
+
|
|
14
|
+
class _QRPoly {
|
|
15
|
+
constructor(num, shift) {
|
|
16
|
+
let o = 0; while (o < num.length && num[o] === 0) o++
|
|
17
|
+
this.num = new Array(num.length - o + shift)
|
|
18
|
+
for (let i = 0; i < num.length - o; i++) this.num[i] = num[i + o]
|
|
19
|
+
}
|
|
20
|
+
get(i) { return this.num[i] }
|
|
21
|
+
getLength() { return this.num.length }
|
|
22
|
+
multiply(e) {
|
|
23
|
+
const n = new Array(this.getLength() + e.getLength() - 1).fill(0)
|
|
24
|
+
for (let i = 0; i < this.getLength(); i++)
|
|
25
|
+
for (let j = 0; j < e.getLength(); j++)
|
|
26
|
+
n[i+j] ^= _QRMath.gexp(_QRMath.glog(this.get(i)) + _QRMath.glog(e.get(j)))
|
|
27
|
+
return new _QRPoly(n, 0)
|
|
28
|
+
}
|
|
29
|
+
mod(e) {
|
|
30
|
+
if (this.getLength() - e.getLength() < 0) return this
|
|
31
|
+
const r = _QRMath.glog(this.get(0)) - _QRMath.glog(e.get(0))
|
|
32
|
+
const n = this.num.slice()
|
|
33
|
+
for (let i = 0; i < e.getLength(); i++) n[i] ^= _QRMath.gexp(_QRMath.glog(e.get(i)) + r)
|
|
34
|
+
return new _QRPoly(n, 0).mod(e)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const _RS_TABLE = [
|
|
39
|
+
[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1,44,16],
|
|
40
|
+
[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,24],[4,25,9],
|
|
41
|
+
[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,86,68],[4,43,27],[4,43,19],[4,43,15],
|
|
42
|
+
[2,98,78],[4,49,31],[2,32,14,4,33,15],[4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,41,15],
|
|
43
|
+
[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16],
|
|
44
|
+
[4,101,81],[1,80,50,4,81,51],[4,50,22,4,51,23],[3,36,12,8,37,13],[2,116,92,2,117,93],[6,58,36,2,59,37],[4,46,20,6,47,21],[7,42,14,4,43,15],
|
|
45
|
+
[4,133,107],[8,59,37,1,60,38],[8,44,20,4,45,21],[12,33,11,4,34,12],[3,145,115,1,146,116],[4,64,40,5,65,41],[11,36,16,5,37,17],[11,36,12,5,37,13],
|
|
46
|
+
[5,109,87,1,110,88],[5,65,41,5,66,42],[5,54,24,7,55,25],[11,36,12,7,37,13],[5,122,98,1,123,99],[7,73,45,3,74,46],[15,43,19,2,44,20],[3,45,15,13,46,16],
|
|
47
|
+
[1,135,107,5,136,108],[10,74,46,1,75,47],[1,50,22,15,51,23],[2,42,14,17,43,15],[5,150,120,1,151,121],[9,69,43,4,70,44],[17,50,22,1,51,23],[2,42,14,19,43,15],
|
|
48
|
+
[3,141,113,4,142,114],[3,70,44,11,71,45],[17,47,21,4,48,22],[9,39,13,16,40,14],[3,135,107,5,136,108],[3,67,41,13,68,42],[15,54,24,5,55,25],[15,43,15,10,44,16],
|
|
49
|
+
[4,144,116,4,145,117],[17,68,42],[17,50,22,6,51,23],[19,46,16,6,47,17],[2,139,111,7,140,112],[17,74,46],[7,54,24,16,55,25],[34,37,13],
|
|
50
|
+
[4,151,121,5,152,122],[4,75,47,14,76,48],[11,54,24,14,55,25],[16,45,15,14,46,16],[6,147,117,4,148,118],[6,73,45,14,74,46],[11,54,24,16,55,25],[30,46,16,2,47,17],
|
|
51
|
+
[8,132,106,4,133,107],[8,75,47,13,76,48],[7,54,24,22,55,25],[22,45,15,13,46,16],[10,142,114,2,143,115],[19,74,46,4,75,47],[28,50,22,6,51,23],[33,46,16,4,47,17],
|
|
52
|
+
[8,152,122,4,153,123],[22,73,45,3,74,46],[8,53,23,26,54,24],[12,45,15,28,46,16],[3,147,117,10,148,118],[3,73,45,23,74,46],[4,54,24,31,55,25],[11,45,15,31,46,16],
|
|
53
|
+
[7,146,116,7,147,117],[21,73,45,7,74,46],[1,53,23,37,54,24],[19,45,15,26,46,16],[5,145,115,10,146,116],[19,75,47,10,76,48],[15,54,24,25,55,25],[23,45,15,25,46,16],
|
|
54
|
+
[13,145,115,3,146,116],[2,74,46,29,75,47],[42,54,24,1,55,25],[23,45,15,28,46,16],[17,145,115],[10,74,46,23,75,47],[10,54,24,35,55,25],[19,45,15,35,46,16],
|
|
55
|
+
[17,145,115,1,146,116],[14,74,46,21,75,47],[29,54,24,19,55,25],[11,45,15,46,46,16],[13,145,115,6,146,116],[14,74,46,23,75,47],[44,54,24,7,55,25],[59,46,16,1,47,17],
|
|
56
|
+
[12,151,121,7,152,122],[12,75,47,26,76,48],[39,54,24,14,55,25],[22,45,15,41,46,16],[6,151,121,14,152,122],[6,75,47,34,76,48],[46,54,24,10,55,25],[2,45,15,64,46,16],
|
|
57
|
+
[17,152,122,4,153,123],[29,74,46,14,75,47],[49,54,24,10,55,25],[24,45,15,46,46,16],[4,152,122,18,153,123],[13,74,46,32,75,47],[48,54,24,14,55,25],[42,45,15,32,46,16],
|
|
58
|
+
[20,147,117,4,148,118],[40,75,47,7,76,48],[43,54,24,22,55,25],[10,45,15,67,46,16],[19,148,118,6,149,119],[18,75,47,31,76,48],[34,54,24,34,55,25],[20,45,15,61,46,16],
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
class _QRRSBlock {
|
|
62
|
+
constructor(t, d) { this.totalCount = t; this.dataCount = d }
|
|
63
|
+
static get(typeNumber, ecLevel) {
|
|
64
|
+
const t = _RS_TABLE[(typeNumber - 1) * 4 + [1,0,3,2][ecLevel]]
|
|
65
|
+
const list = []
|
|
66
|
+
for (let i = 0; i < t.length; i += 3)
|
|
67
|
+
for (let j = 0; j < t[i]; j++) list.push(new _QRRSBlock(t[i+1], t[i+2]))
|
|
68
|
+
return list
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class _QRBitBuf {
|
|
73
|
+
constructor() { this.buffer = []; this.length = 0 }
|
|
74
|
+
get(i) { return ((this.buffer[Math.floor(i/8)] >>> (7 - i%8)) & 1) === 1 }
|
|
75
|
+
put(num, len) { for (let i = 0; i < len; i++) this.putBit(((num >>> (len-i-1)) & 1) === 1) }
|
|
76
|
+
getLengthInBits() { return this.length }
|
|
77
|
+
putBit(bit) {
|
|
78
|
+
const bi = Math.floor(this.length/8)
|
|
79
|
+
if (this.buffer.length <= bi) this.buffer.push(0)
|
|
80
|
+
if (bit) this.buffer[bi] |= 0x80 >>> (this.length % 8)
|
|
81
|
+
this.length++
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class _QRCode {
|
|
86
|
+
constructor(typeNumber, ecLevel) {
|
|
87
|
+
this.typeNumber = typeNumber; this.errorCorrectLevel = ecLevel
|
|
88
|
+
this.modules = null; this.moduleCount = 0; this.dataCache = null; this.dataList = []
|
|
89
|
+
}
|
|
90
|
+
addData(data) {
|
|
91
|
+
const obj = { mode: 4, data, parsedData: [] }
|
|
92
|
+
const d = unescape(encodeURIComponent(data))
|
|
93
|
+
for (let i = 0; i < d.length; i++) obj.parsedData.push(d.charCodeAt(i))
|
|
94
|
+
obj.getLength = () => obj.parsedData.length
|
|
95
|
+
obj.write = buf => { for (let i = 0; i < obj.parsedData.length; i++) buf.put(obj.parsedData[i], 8) }
|
|
96
|
+
this.dataList.push(obj); this.dataCache = null
|
|
97
|
+
}
|
|
98
|
+
isDark(r, c) { return this.modules[r][c] }
|
|
99
|
+
getModuleCount() { return this.moduleCount }
|
|
100
|
+
make() { this._make(false, this._bestMask()) }
|
|
101
|
+
_make(test, mask) {
|
|
102
|
+
if (this.typeNumber < 1) {
|
|
103
|
+
let t = 1
|
|
104
|
+
for (; t < 40; t++) {
|
|
105
|
+
const rs = _QRRSBlock.get(t, this.errorCorrectLevel)
|
|
106
|
+
const buf = new _QRBitBuf()
|
|
107
|
+
let total = rs.reduce((s,b) => s + b.dataCount, 0)
|
|
108
|
+
for (const d of this.dataList) { buf.put(d.mode, 4); buf.put(d.getLength(), this._lenBits(d.mode, t)); d.write(buf) }
|
|
109
|
+
if (buf.getLengthInBits() <= total * 8) break
|
|
110
|
+
}
|
|
111
|
+
this.typeNumber = t
|
|
112
|
+
}
|
|
113
|
+
this.moduleCount = this.typeNumber * 4 + 17
|
|
114
|
+
this.modules = Array.from({length: this.moduleCount}, () => new Array(this.moduleCount).fill(null))
|
|
115
|
+
this._probe(0, 0); this._probe(this.moduleCount-7, 0); this._probe(0, this.moduleCount-7)
|
|
116
|
+
this._adjust(); this._timing(); this._typeInfo(test, mask)
|
|
117
|
+
if (this.typeNumber >= 7) this._typeNum(test)
|
|
118
|
+
if (!this.dataCache) this.dataCache = this._buildData()
|
|
119
|
+
this._map(this.dataCache, mask)
|
|
120
|
+
}
|
|
121
|
+
_probe(row, col) {
|
|
122
|
+
for (let r = -1; r <= 7; r++) for (let c = -1; c <= 7; c++) {
|
|
123
|
+
if (row+r < 0 || this.moduleCount <= row+r || col+c < 0 || this.moduleCount <= col+c) continue
|
|
124
|
+
this.modules[row+r][col+c] = (0<=r&&r<=6&&(c===0||c===6))||(0<=c&&c<=6&&(r===0||r===6))||(2<=r&&r<=4&&2<=c&&c<=4)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
_bestMask() {
|
|
128
|
+
let min = 0, pat = 0
|
|
129
|
+
for (let i = 0; i < 8; i++) { this._make(true, i); const lp = this._lostPoint(); if (i===0||min>lp){min=lp;pat=i} }
|
|
130
|
+
return pat
|
|
131
|
+
}
|
|
132
|
+
_timing() {
|
|
133
|
+
for (let r = 8; r < this.moduleCount-8; r++) if (this.modules[r][6]===null) this.modules[r][6] = r%2===0
|
|
134
|
+
for (let c = 8; c < this.moduleCount-8; c++) if (this.modules[6][c]===null) this.modules[6][c] = c%2===0
|
|
135
|
+
}
|
|
136
|
+
_adjust() {
|
|
137
|
+
const pos = this._patPos()
|
|
138
|
+
for (let i = 0; i < pos.length; i++) for (let j = 0; j < pos.length; j++) {
|
|
139
|
+
const row = pos[i], col = pos[j]
|
|
140
|
+
if (this.modules[row][col] !== null) continue
|
|
141
|
+
for (let r = -2; r <= 2; r++) for (let c = -2; c <= 2; c++)
|
|
142
|
+
this.modules[row+r][col+c] = r===-2||r===2||c===-2||c===2||(r===0&&c===0)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
_typeNum(test) {
|
|
146
|
+
const bits = this._bchTypeNum(this.typeNumber)
|
|
147
|
+
for (let i = 0; i < 18; i++) {
|
|
148
|
+
const m = !test && ((bits>>i)&1)===1
|
|
149
|
+
this.modules[Math.floor(i/3)][i%3+this.moduleCount-8-3] = m
|
|
150
|
+
this.modules[i%3+this.moduleCount-8-3][Math.floor(i/3)] = m
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
_typeInfo(test, mask) {
|
|
154
|
+
const bits = this._bchTypeInfo((this.errorCorrectLevel<<3)|mask)
|
|
155
|
+
for (let i = 0; i < 15; i++) {
|
|
156
|
+
const m = !test && ((bits>>i)&1)===1
|
|
157
|
+
if (i<6) this.modules[i][8]=m; else if (i<8) this.modules[i+1][8]=m; else this.modules[this.moduleCount-15+i][8]=m
|
|
158
|
+
if (i<8) this.modules[8][this.moduleCount-i-1]=m; else if (i<9) this.modules[8][15-i]=m; else this.modules[8][15-i-1]=m
|
|
159
|
+
}
|
|
160
|
+
this.modules[this.moduleCount-8][8] = !test
|
|
161
|
+
}
|
|
162
|
+
_map(data, mask) {
|
|
163
|
+
let inc = -1, row = this.moduleCount-1, bi = 7, by = 0
|
|
164
|
+
const mf = [(i,j)=>(i+j)%2===0,(i)=>i%2===0,(_,j)=>j%3===0,(i,j)=>(i+j)%3===0,(i,j)=>(Math.floor(i/2)+Math.floor(j/3))%2===0,(i,j)=>(i*j)%2+(i*j)%3===0,(i,j)=>((i*j)%2+(i*j)%3)%2===0,(i,j)=>((i+j)%2+(i*j)%3)%2===0][mask]
|
|
165
|
+
for (let col = this.moduleCount-1; col > 0; col -= 2) {
|
|
166
|
+
if (col===6) col--
|
|
167
|
+
while (true) {
|
|
168
|
+
for (let c = 0; c < 2; c++) {
|
|
169
|
+
if (this.modules[row][col-c]===null) {
|
|
170
|
+
let dark = by < data.length && ((data[by]>>>bi)&1)===1
|
|
171
|
+
if (mf(row,col-c)) dark=!dark
|
|
172
|
+
this.modules[row][col-c]=dark; bi--
|
|
173
|
+
if (bi===-1){by++;bi=7}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
row+=inc
|
|
177
|
+
if (row<0||this.moduleCount<=row){row-=inc;inc=-inc;break}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
_lostPoint() {
|
|
182
|
+
const m = this.moduleCount; let lp = 0
|
|
183
|
+
for (let r = 0; r < m; r++) for (let c = 0; c < m; c++) {
|
|
184
|
+
let sc = 0; const dark = this.isDark(r, c)
|
|
185
|
+
for (let dr = -1; dr <= 1; dr++) for (let dc = -1; dc <= 1; dc++) {
|
|
186
|
+
if (r+dr<0||m<=r+dr||c+dc<0||m<=c+dc||(!dr&&!dc)) continue
|
|
187
|
+
if (dark===this.isDark(r+dr,c+dc)) sc++
|
|
188
|
+
}
|
|
189
|
+
if (sc>5) lp+=3+sc-5
|
|
190
|
+
}
|
|
191
|
+
for (let r = 0; r < m-1; r++) for (let c = 0; c < m-1; c++) {
|
|
192
|
+
let cnt=0
|
|
193
|
+
if(this.isDark(r,c))cnt++;if(this.isDark(r+1,c))cnt++;if(this.isDark(r,c+1))cnt++;if(this.isDark(r+1,c+1))cnt++
|
|
194
|
+
if(cnt===0||cnt===4)lp+=3
|
|
195
|
+
}
|
|
196
|
+
for (let r = 0; r < m; r++) for (let c = 0; c < m-6; c++)
|
|
197
|
+
if(this.isDark(r,c)&&!this.isDark(r,c+1)&&this.isDark(r,c+2)&&this.isDark(r,c+3)&&this.isDark(r,c+4)&&!this.isDark(r,c+5)&&this.isDark(r,c+6))lp+=40
|
|
198
|
+
for (let c = 0; c < m; c++) for (let r = 0; r < m-6; r++)
|
|
199
|
+
if(this.isDark(r,c)&&!this.isDark(r+1,c)&&this.isDark(r+2,c)&&this.isDark(r+3,c)&&this.isDark(r+4,c)&&!this.isDark(r+5,c)&&this.isDark(r+6,c))lp+=40
|
|
200
|
+
let dc=0; for(let c=0;c<m;c++) for(let r=0;r<m;r++) if(this.isDark(r,c))dc++
|
|
201
|
+
lp+=Math.abs(Math.floor(dc*100/m/m-50))/5*10
|
|
202
|
+
return lp
|
|
203
|
+
}
|
|
204
|
+
_patPos() { return [[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,138],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26,54,82,110,138,166],[6,30,58,86,114,142,170]][this.typeNumber - 1] }
|
|
205
|
+
_bchTypeInfo(d) { let x=d<<10; while(this._bchDigit(x)-this._bchDigit(0x537)>=0)x^=0x537<<(this._bchDigit(x)-this._bchDigit(0x537)); return((d<<10)|x)^0x5412 }
|
|
206
|
+
_bchTypeNum(d) { let x=d<<12; while(this._bchDigit(x)-this._bchDigit(0x1F25)>=0)x^=0x1F25<<(this._bchDigit(x)-this._bchDigit(0x1F25)); return(d<<12)|x }
|
|
207
|
+
_bchDigit(d) { let n=0; while(d!==0){n++;d>>>=1} return n }
|
|
208
|
+
_lenBits(mode, t) {
|
|
209
|
+
if(mode===1)return t<10?10:t<27?12:14
|
|
210
|
+
if(mode===2)return t<10?9:t<27?11:13
|
|
211
|
+
if(mode===4)return t<10?8:16
|
|
212
|
+
if(mode===8)return t<10?8:t<27?10:12
|
|
213
|
+
}
|
|
214
|
+
_buildData() {
|
|
215
|
+
const rs = _QRRSBlock.get(this.typeNumber, this.errorCorrectLevel)
|
|
216
|
+
const buf = new _QRBitBuf()
|
|
217
|
+
for (const d of this.dataList) { buf.put(d.mode,4); buf.put(d.getLength(),this._lenBits(d.mode,this.typeNumber)); d.write(buf) }
|
|
218
|
+
const total = rs.reduce((s,b)=>s+b.dataCount,0)
|
|
219
|
+
if (buf.getLengthInBits()>total*8) throw new Error('code length overflow')
|
|
220
|
+
if (buf.getLengthInBits()+4<=total*8) buf.put(0,4)
|
|
221
|
+
while(buf.getLengthInBits()%8!==0)buf.putBit(false)
|
|
222
|
+
while(true){if(buf.getLengthInBits()>=total*8)break;buf.put(0xEC,8);if(buf.getLengthInBits()>=total*8)break;buf.put(0x11,8)}
|
|
223
|
+
return this._buildBytes(buf, rs)
|
|
224
|
+
}
|
|
225
|
+
_buildBytes(buf, rs) {
|
|
226
|
+
let off=0,maxDc=0,maxEc=0
|
|
227
|
+
const dc=new Array(rs.length),ec=new Array(rs.length)
|
|
228
|
+
for(let r=0;r<rs.length;r++){
|
|
229
|
+
const dCnt=rs[r].dataCount,eCnt=rs[r].totalCount-dCnt
|
|
230
|
+
maxDc=Math.max(maxDc,dCnt);maxEc=Math.max(maxEc,eCnt)
|
|
231
|
+
dc[r]=new Array(dCnt); for(let i=0;i<dCnt;i++)dc[r][i]=0xff&buf.buffer[i+off]; off+=dCnt
|
|
232
|
+
const rsp=this._ecPoly(eCnt),raw=new _QRPoly(dc[r],rsp.getLength()-1),mod=raw.mod(rsp)
|
|
233
|
+
ec[r]=new Array(rsp.getLength()-1)
|
|
234
|
+
for(let i=0;i<ec[r].length;i++){const mi=i+mod.getLength()-ec[r].length;ec[r][i]=mi>=0?mod.get(mi):0}
|
|
235
|
+
}
|
|
236
|
+
const total=rs.reduce((s,b)=>s+b.totalCount,0),data=new Array(total)
|
|
237
|
+
let idx=0
|
|
238
|
+
for(let i=0;i<maxDc;i++)for(let r=0;r<rs.length;r++)if(i<dc[r].length)data[idx++]=dc[r][i]
|
|
239
|
+
for(let i=0;i<maxEc;i++)for(let r=0;r<rs.length;r++)if(i<ec[r].length)data[idx++]=ec[r][i]
|
|
240
|
+
return data
|
|
241
|
+
}
|
|
242
|
+
_ecPoly(len){let a=new _QRPoly([1],0);for(let i=0;i<len;i++)a=a.multiply(new _QRPoly([1,_QRMath.gexp(i)],0));return a}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function generateQR(input, opts, cb) {
|
|
246
|
+
if (typeof opts === 'function') { cb = opts; opts = {} }
|
|
247
|
+
opts = opts || {}
|
|
248
|
+
const qr = new _QRCode(-1, _QRErrorCorrectLevel.L)
|
|
249
|
+
qr.addData(input); qr.make()
|
|
250
|
+
const BLACK = '\x1b[40m \x1b[0m', WHITE = '\x1b[47m \x1b[0m'
|
|
251
|
+
let output = ''
|
|
252
|
+
if (opts.small) {
|
|
253
|
+
const mc = qr.getModuleCount(), md = qr.modules.slice()
|
|
254
|
+
if (mc % 2 === 1) md.push(new Array(mc).fill(false))
|
|
255
|
+
const p = {WW:'\u2588',WB:'\u2580',BW:'\u2584',BB:' '}
|
|
256
|
+
output += p.BW.repeat(mc+3)+'\n'
|
|
257
|
+
for (let r = 0; r < mc; r += 2) {
|
|
258
|
+
output += p.WW
|
|
259
|
+
for (let c = 0; c < mc; c++) {
|
|
260
|
+
const t=md[r][c],b=md[r+1][c]
|
|
261
|
+
output += (!t&&!b)?p.WW:(!t&&b)?p.WB:(t&&!b)?p.BW:p.BB
|
|
262
|
+
}
|
|
263
|
+
output += p.WW+'\n'
|
|
264
|
+
}
|
|
265
|
+
if (mc%2===0) output += p.WB.repeat(mc+3)
|
|
266
|
+
} else {
|
|
267
|
+
const border = WHITE.repeat(qr.getModuleCount()+2)
|
|
268
|
+
output += border+'\n'
|
|
269
|
+
qr.modules.forEach(row => { output += WHITE+row.map(c=>c?BLACK:WHITE).join('')+WHITE+'\n' })
|
|
270
|
+
output += border
|
|
271
|
+
}
|
|
272
|
+
if (cb) cb(output); else console.log(output)
|
|
273
|
+
}
|