@ohos-ports/bare-buffer 3.7.1-beta.0

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 (155) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +26 -0
  3. package/binding.c +1077 -0
  4. package/binding.gyp +32 -0
  5. package/binding.js +1 -0
  6. package/build/Release/bare_buffer.node +0 -0
  7. package/deps/libbase64/.clang-format +10 -0
  8. package/deps/libbase64/.gitattributes +1 -0
  9. package/deps/libbase64/.github/workflows/integrate.yml +45 -0
  10. package/deps/libbase64/.github/workflows/nightly.yml +28 -0
  11. package/deps/libbase64/.prettierrc +1 -0
  12. package/deps/libbase64/CMakeLists.txt +71 -0
  13. package/deps/libbase64/LICENSE +201 -0
  14. package/deps/libbase64/NOTICE +13 -0
  15. package/deps/libbase64/README.md +11 -0
  16. package/deps/libbase64/include/base64.h +323 -0
  17. package/deps/libbase64/package.json +25 -0
  18. package/deps/libbase64/src/base64.c +22 -0
  19. package/deps/libbase64/test/CMakeLists.txt +31 -0
  20. package/deps/libbase64/test/decode-invalid.c +35 -0
  21. package/deps/libbase64/test/decode-length.c +23 -0
  22. package/deps/libbase64/test/decode.c +36 -0
  23. package/deps/libbase64/test/encode-length.c +22 -0
  24. package/deps/libbase64/test/encode.c +42 -0
  25. package/deps/libbase64/test/fuzz/CMakeLists.txt +18 -0
  26. package/deps/libbase64/test/fuzz/decode/utf16le.c +53 -0
  27. package/deps/libbase64/test/fuzz/decode/utf8.c +47 -0
  28. package/deps/libbase64/test/fuzz/encode/utf16le.c +56 -0
  29. package/deps/libbase64/test/fuzz/encode/utf8.c +56 -0
  30. package/deps/libhex/.clang-format +10 -0
  31. package/deps/libhex/.gitattributes +1 -0
  32. package/deps/libhex/.github/workflows/integrate.yml +45 -0
  33. package/deps/libhex/.github/workflows/nightly.yml +28 -0
  34. package/deps/libhex/.prettierrc +1 -0
  35. package/deps/libhex/CMakeLists.txt +72 -0
  36. package/deps/libhex/LICENSE +201 -0
  37. package/deps/libhex/NOTICE +13 -0
  38. package/deps/libhex/README.md +11 -0
  39. package/deps/libhex/include/hex.h +163 -0
  40. package/deps/libhex/package.json +25 -0
  41. package/deps/libhex/src/hex.c +16 -0
  42. package/deps/libhex/test/CMakeLists.txt +31 -0
  43. package/deps/libhex/test/decode-invalid.c +37 -0
  44. package/deps/libhex/test/decode-length.c +23 -0
  45. package/deps/libhex/test/decode.c +25 -0
  46. package/deps/libhex/test/encode-length.c +22 -0
  47. package/deps/libhex/test/encode.c +25 -0
  48. package/deps/libhex/test/fuzz/CMakeLists.txt +18 -0
  49. package/deps/libhex/test/fuzz/decode/utf16le.c +53 -0
  50. package/deps/libhex/test/fuzz/decode/utf8.c +47 -0
  51. package/deps/libhex/test/fuzz/encode/utf16le.c +36 -0
  52. package/deps/libhex/test/fuzz/encode/utf8.c +36 -0
  53. package/deps/libutf/.clang-format +10 -0
  54. package/deps/libutf/.gitattributes +1 -0
  55. package/deps/libutf/.github/workflows/integrate.yml +45 -0
  56. package/deps/libutf/.github/workflows/nightly.yml +28 -0
  57. package/deps/libutf/.prettierrc +1 -0
  58. package/deps/libutf/CMakeLists.txt +105 -0
  59. package/deps/libutf/LICENSE +201 -0
  60. package/deps/libutf/NOTICE +13 -0
  61. package/deps/libutf/README.md +11 -0
  62. package/deps/libutf/include/utf/string.h +1655 -0
  63. package/deps/libutf/include/utf.h +132 -0
  64. package/deps/libutf/package.json +24 -0
  65. package/deps/libutf/src/ascii/validate.c +47 -0
  66. package/deps/libutf/src/endianness.c +19 -0
  67. package/deps/libutf/src/endianness.h +54 -0
  68. package/deps/libutf/src/latin1/convert-to-utf16.c +37 -0
  69. package/deps/libutf/src/latin1/convert-to-utf32.c +34 -0
  70. package/deps/libutf/src/latin1/convert-to-utf8.c +58 -0
  71. package/deps/libutf/src/latin1/length-from-utf16.c +26 -0
  72. package/deps/libutf/src/latin1/length-from-utf32.c +26 -0
  73. package/deps/libutf/src/latin1/length-from-utf8.c +34 -0
  74. package/deps/libutf/src/utf16/convert-to-latin1.c +41 -0
  75. package/deps/libutf/src/utf16/convert-to-utf32.c +56 -0
  76. package/deps/libutf/src/utf16/convert-to-utf8.c +80 -0
  77. package/deps/libutf/src/utf16/length-from-latin1.c +26 -0
  78. package/deps/libutf/src/utf16/length-from-utf32.c +33 -0
  79. package/deps/libutf/src/utf16/length-from-utf8.c +38 -0
  80. package/deps/libutf/src/utf16/validate.c +53 -0
  81. package/deps/libutf/src/utf32/convert-to-latin1.c +40 -0
  82. package/deps/libutf/src/utf32/convert-to-utf16.c +56 -0
  83. package/deps/libutf/src/utf32/convert-to-utf8.c +71 -0
  84. package/deps/libutf/src/utf32/length-from-latin1.c +26 -0
  85. package/deps/libutf/src/utf32/length-from-utf16.c +35 -0
  86. package/deps/libutf/src/utf32/length-from-utf8.c +35 -0
  87. package/deps/libutf/src/utf32/string.c +149 -0
  88. package/deps/libutf/src/utf32/validate.c +39 -0
  89. package/deps/libutf/src/utf8/convert-to-latin1.c +70 -0
  90. package/deps/libutf/src/utf8/convert-to-utf16.c +95 -0
  91. package/deps/libutf/src/utf8/convert-to-utf32.c +110 -0
  92. package/deps/libutf/src/utf8/length-from-latin1.c +32 -0
  93. package/deps/libutf/src/utf8/length-from-utf16.c +48 -0
  94. package/deps/libutf/src/utf8/length-from-utf32.c +35 -0
  95. package/deps/libutf/src/utf8/string.c +149 -0
  96. package/deps/libutf/src/utf8/validate.c +107 -0
  97. package/deps/libutf/test/CMakeLists.txt +56 -0
  98. package/deps/libutf/test/fuzz/CMakeLists.txt +26 -0
  99. package/deps/libutf/test/fuzz/corpus/.gitignore +2 -0
  100. package/deps/libutf/test/fuzz/crashes/.gitignore +2 -0
  101. package/deps/libutf/test/fuzz/latin1/convert-to-utf16.c +32 -0
  102. package/deps/libutf/test/fuzz/latin1/convert-to-utf32.c +32 -0
  103. package/deps/libutf/test/fuzz/latin1/convert-to-utf8.c +33 -0
  104. package/deps/libutf/test/fuzz/utf16/convert-to-latin1.c +36 -0
  105. package/deps/libutf/test/fuzz/utf16/convert-to-utf32.c +38 -0
  106. package/deps/libutf/test/fuzz/utf16/convert-to-utf8.c +38 -0
  107. package/deps/libutf/test/fuzz/utf32/convert-to-latin1.c +36 -0
  108. package/deps/libutf/test/fuzz/utf32/convert-to-utf16.c +38 -0
  109. package/deps/libutf/test/fuzz/utf32/convert-to-utf8.c +38 -0
  110. package/deps/libutf/test/fuzz/utf8/convert-to-latin1.c +33 -0
  111. package/deps/libutf/test/fuzz/utf8/convert-to-utf16.c +35 -0
  112. package/deps/libutf/test/fuzz/utf8/convert-to-utf32.c +35 -0
  113. package/deps/libutf/test/utf16/utf8-convert.c +45 -0
  114. package/deps/libutf/test/utf16/utf8-length.c +41 -0
  115. package/deps/libutf/test/utf16/validate-invalid.c +50 -0
  116. package/deps/libutf/test/utf16/validate.c +32 -0
  117. package/deps/libutf/test/utf32/string-append.c +74 -0
  118. package/deps/libutf/test/utf32/string-compare.c +108 -0
  119. package/deps/libutf/test/utf32/string-erase.c +79 -0
  120. package/deps/libutf/test/utf32/string-index.c +102 -0
  121. package/deps/libutf/test/utf32/string-insert.c +92 -0
  122. package/deps/libutf/test/utf32/string-last-index.c +71 -0
  123. package/deps/libutf/test/utf32/string-prepend.c +51 -0
  124. package/deps/libutf/test/utf32/string-replace.c +88 -0
  125. package/deps/libutf/test/utf32/string-reserve.c +99 -0
  126. package/deps/libutf/test/utf32/string-substring.c +93 -0
  127. package/deps/libutf/test/utf8/latin1-convert.c +23 -0
  128. package/deps/libutf/test/utf8/string-append.c +23 -0
  129. package/deps/libutf/test/utf8/string-compare.c +67 -0
  130. package/deps/libutf/test/utf8/string-erase.c +75 -0
  131. package/deps/libutf/test/utf8/string-index.c +99 -0
  132. package/deps/libutf/test/utf8/string-last-index.c +68 -0
  133. package/deps/libutf/test/utf8/string-prepend.c +23 -0
  134. package/deps/libutf/test/utf8/string-replace.c +74 -0
  135. package/deps/libutf/test/utf8/string-reserve.c +51 -0
  136. package/deps/libutf/test/utf8/string-substring.c +68 -0
  137. package/deps/libutf/test/utf8/utf16-convert.c +32 -0
  138. package/deps/libutf/test/utf8/utf16-length.c +29 -0
  139. package/deps/libutf/test/utf8/validate-invalid.c +26 -0
  140. package/deps/libutf/test/utf8/validate.c +21 -0
  141. package/global.d.ts +21 -0
  142. package/global.js +6 -0
  143. package/index.d.ts +769 -0
  144. package/index.js +1339 -0
  145. package/lib/ascii.js +36 -0
  146. package/lib/base64.js +25 -0
  147. package/lib/base64url.js +25 -0
  148. package/lib/checked.js +23 -0
  149. package/lib/constants.d.ts +7 -0
  150. package/lib/constants.js +3 -0
  151. package/lib/hex.js +20 -0
  152. package/lib/latin1.js +14 -0
  153. package/lib/utf16le.js +20 -0
  154. package/lib/utf8.js +18 -0
  155. package/package.json +64 -0
package/index.js ADDED
@@ -0,0 +1,1339 @@
1
+ const constants = require('./lib/constants')
2
+ const ascii = require('./lib/ascii')
3
+ const base64 = require('./lib/base64')
4
+ const base64url = require('./lib/base64url')
5
+ const hex = require('./lib/hex')
6
+ const utf8 = require('./lib/utf8')
7
+ const utf16le = require('./lib/utf16le')
8
+ const latin1 = require('./lib/latin1')
9
+ const binding = require('./binding')
10
+ const checked = require('./lib/checked')
11
+
12
+ const kind = Symbol.for('bare.buffer.kind')
13
+
14
+ const SWAP_MIN_LENGTH = 128
15
+
16
+ let poolSize = 65536
17
+ let pool = null
18
+ let poolLength = 0
19
+ let poolOffset = 0
20
+
21
+ class Buffer extends Uint8Array {
22
+ static get [kind]() {
23
+ return 0 // Compatibility version
24
+ }
25
+
26
+ static get poolSize() {
27
+ return poolSize
28
+ }
29
+
30
+ static set poolSize(value) {
31
+ assertSize(value, 'Pool size')
32
+
33
+ poolSize = value
34
+ }
35
+
36
+ constructor(arrayBuffer, offset, length, opts = {}) {
37
+ if (typeof arrayBuffer === 'number') {
38
+ opts = offset || {}
39
+
40
+ const { uninitialized = false } = opts
41
+
42
+ offset = 0
43
+ length = arrayBuffer
44
+
45
+ if (Number.isInteger(length) === false || length < 0 || length > constants.MAX_LENGTH) {
46
+ throw new RangeError(
47
+ `Buffer length must be an integer between 0 and ${constants.MAX_LENGTH}`
48
+ )
49
+ }
50
+
51
+ arrayBuffer = uninitialized ? binding.allocUnsafe(length) : binding.alloc(length)
52
+ } else {
53
+ if (typeof offset === 'number') assertInteger(offset, 'Offset')
54
+ if (typeof length === 'number') assertInteger(length, 'Length')
55
+
56
+ if (length > constants.MAX_LENGTH) {
57
+ throw new RangeError(`Buffer length must be at most ${constants.MAX_LENGTH}`)
58
+ }
59
+
60
+ if (typeof offset === 'object' && offset !== null) {
61
+ opts = offset
62
+ offset = 0
63
+ length = arrayBuffer.byteLength
64
+ } else if (typeof length === 'object' && length !== null) {
65
+ opts = length
66
+ length = arrayBuffer.byteLength - offset
67
+ }
68
+ }
69
+
70
+ super(arrayBuffer, offset, length)
71
+ }
72
+
73
+ get [kind]() {
74
+ return Buffer[kind]
75
+ }
76
+
77
+ copy(target, targetStart = 0, sourceStart = 0, sourceEnd = this.byteLength) {
78
+ const source = this
79
+
80
+ assertView(target, 'Target')
81
+
82
+ assertSize(targetStart, 'Target start')
83
+ assertSize(sourceStart, 'Source start')
84
+ assertSize(sourceEnd, 'Source end')
85
+
86
+ if (target.BYTES_PER_ELEMENT !== 1) {
87
+ target = new Uint8Array(target.buffer, target.byteOffset, target.byteLength)
88
+ }
89
+
90
+ const sourceLength = source.byteLength
91
+ const targetLength = target.byteLength
92
+
93
+ if (sourceStart > sourceLength) {
94
+ throw new RangeError(`Source start must be at most ${sourceLength}`)
95
+ }
96
+
97
+ if (targetStart >= targetLength) return 0
98
+
99
+ if (sourceEnd > sourceLength) sourceEnd = sourceLength
100
+ if (sourceEnd <= sourceStart) return 0
101
+
102
+ const room = targetLength - targetStart
103
+
104
+ if (sourceEnd - sourceStart > room) sourceEnd = sourceStart + room
105
+
106
+ const length = sourceEnd - sourceStart
107
+
108
+ if (source === target) {
109
+ target.copyWithin(targetStart, sourceStart, sourceEnd)
110
+ } else if (sourceStart === 0 && sourceEnd === sourceLength) {
111
+ target.set(source, targetStart)
112
+ } else {
113
+ target.set(
114
+ new Uint8Array(source.buffer, source.byteOffset + sourceStart, length),
115
+ targetStart
116
+ )
117
+ }
118
+
119
+ return length
120
+ }
121
+
122
+ equals(target) {
123
+ const source = this
124
+
125
+ if (source === target) return true
126
+
127
+ assertView(target, 'Target')
128
+
129
+ const sourceLength = source.byteLength
130
+ const targetLength = target.byteLength
131
+
132
+ if (sourceLength !== targetLength) return false
133
+
134
+ return (
135
+ checked(
136
+ binding.compare(
137
+ source.buffer,
138
+ source.byteOffset,
139
+ sourceLength,
140
+ target.buffer,
141
+ target.byteOffset,
142
+ targetLength
143
+ )
144
+ ) === 0
145
+ )
146
+ }
147
+
148
+ compare(
149
+ target,
150
+ targetStart = 0,
151
+ targetEnd = target.byteLength,
152
+ sourceStart = 0,
153
+ sourceEnd = this.byteLength
154
+ ) {
155
+ const source = this
156
+
157
+ if (source === target) return 0
158
+
159
+ assertView(target, 'Target')
160
+
161
+ const sourceLength = source.byteLength
162
+ const targetLength = target.byteLength
163
+
164
+ if (arguments.length === 1) {
165
+ targetStart = 0
166
+ targetEnd = targetLength
167
+ sourceStart = 0
168
+ sourceEnd = sourceLength
169
+ } else {
170
+ assertInteger(targetStart, 'Target start')
171
+ assertInteger(targetEnd, 'Target end')
172
+ assertInteger(sourceStart, 'Source start')
173
+ assertInteger(sourceEnd, 'Source end')
174
+
175
+ if (targetStart < 0) targetStart = 0
176
+ if (targetStart > targetLength) targetStart = targetLength
177
+
178
+ if (targetEnd < targetStart) targetEnd = targetStart
179
+ if (targetEnd > targetLength) targetEnd = targetLength
180
+
181
+ if (sourceStart < 0) sourceStart = 0
182
+ if (sourceStart > sourceLength) sourceStart = sourceLength
183
+
184
+ if (sourceEnd < sourceStart) sourceEnd = sourceStart
185
+ if (sourceEnd > sourceLength) sourceEnd = sourceLength
186
+ }
187
+
188
+ return checked(
189
+ binding.compare(
190
+ source.buffer,
191
+ source.byteOffset + sourceStart,
192
+ sourceEnd - sourceStart,
193
+ target.buffer,
194
+ target.byteOffset + targetStart,
195
+ targetEnd - targetStart
196
+ )
197
+ )
198
+ }
199
+
200
+ fill(value, offset = 0, end = this.byteLength, encoding = 'utf8') {
201
+ if (typeof value === 'string') {
202
+ if (typeof offset === 'string') {
203
+ // fill(string, encoding)
204
+ encoding = offset
205
+ offset = 0
206
+ end = this.byteLength
207
+ } else if (typeof end === 'string') {
208
+ // fill(string, offset, encoding)
209
+ encoding = end
210
+ end = this.byteLength
211
+ }
212
+ } else if (typeof value === 'number') {
213
+ value = value & 0xff
214
+ } else if (typeof value === 'boolean') {
215
+ value = +value
216
+ } else if (ArrayBuffer.isView(value)) {
217
+ if (value.BYTES_PER_ELEMENT !== 1) {
218
+ value = new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
219
+ }
220
+ } else {
221
+ throw new TypeError(
222
+ `Fill value must be a number, a string or a view, received type ${typeof value}`
223
+ )
224
+ }
225
+
226
+ assertInteger(offset, 'Offset')
227
+ assertInteger(end, 'End')
228
+
229
+ const byteLength = this.byteLength
230
+
231
+ if (offset < 0) offset = 0
232
+ if (offset >= byteLength) return this
233
+
234
+ if (end <= offset) return this
235
+ if (end > byteLength) end = byteLength
236
+
237
+ if (typeof value === 'number') return super.fill(value, offset, end)
238
+
239
+ if (typeof value === 'string') value = exports.from(value, encoding)
240
+
241
+ const length = value.byteLength
242
+
243
+ if (length === 0) return super.fill(0, offset, end)
244
+ if (length === 1) return super.fill(value[0], offset, end)
245
+
246
+ return fillPattern(this, value, length, offset, end)
247
+ }
248
+
249
+ includes(value, offset, encoding) {
250
+ return this.indexOf(value, offset, encoding) !== -1
251
+ }
252
+
253
+ indexOf(value, offset = 0, encoding) {
254
+ if (typeof offset === 'number') assertInteger(offset, 'Offset')
255
+
256
+ if (typeof value === 'boolean') value = +value
257
+
258
+ if (typeof value === 'number') {
259
+ return super.indexOf(value & 0xff, offset)
260
+ }
261
+
262
+ return bidirectionalIndexOf(this, value, offset, encoding, true /* first */)
263
+ }
264
+
265
+ lastIndexOf(value, offset = this.byteLength - 1, encoding) {
266
+ if (typeof offset === 'number') assertInteger(offset, 'Offset')
267
+
268
+ if (typeof value === 'boolean') value = +value
269
+
270
+ if (typeof value === 'number') {
271
+ return super.lastIndexOf(value & 0xff, offset)
272
+ }
273
+
274
+ return bidirectionalIndexOf(this, value, offset, encoding, false /* last */)
275
+ }
276
+
277
+ swap16() {
278
+ const length = this.byteLength
279
+
280
+ if (length % 2 !== 0) {
281
+ throw new RangeError('Buffer size must be a multiple of 16-bits')
282
+ }
283
+
284
+ if (length < SWAP_MIN_LENGTH) {
285
+ for (let i = 0; i < length; i += 2) swap(this, i, i + 1)
286
+ } else {
287
+ checked(binding.swap16(this.buffer, this.byteOffset, length))
288
+ }
289
+
290
+ return this
291
+ }
292
+
293
+ swap32() {
294
+ const length = this.byteLength
295
+
296
+ if (length % 4 !== 0) {
297
+ throw new RangeError('Buffer size must be a multiple of 32-bits')
298
+ }
299
+
300
+ if (length < SWAP_MIN_LENGTH) {
301
+ for (let i = 0; i < length; i += 4) {
302
+ swap(this, i, i + 3)
303
+ swap(this, i + 1, i + 2)
304
+ }
305
+ } else {
306
+ checked(binding.swap32(this.buffer, this.byteOffset, length))
307
+ }
308
+
309
+ return this
310
+ }
311
+
312
+ swap64() {
313
+ const length = this.byteLength
314
+
315
+ if (length % 8 !== 0) {
316
+ throw new RangeError('Buffer size must be a multiple of 64-bits')
317
+ }
318
+
319
+ if (length < SWAP_MIN_LENGTH) {
320
+ for (let i = 0; i < length; i += 8) {
321
+ swap(this, i, i + 7)
322
+ swap(this, i + 1, i + 6)
323
+ swap(this, i + 2, i + 5)
324
+ swap(this, i + 3, i + 4)
325
+ }
326
+ } else {
327
+ checked(binding.swap64(this.buffer, this.byteOffset, length))
328
+ }
329
+
330
+ return this
331
+ }
332
+
333
+ toString(encoding = 'utf8', start = 0, end = this.byteLength) {
334
+ // toString()
335
+ if (arguments.length === 0) return utf8.toString(this)
336
+
337
+ // toString(encoding)
338
+ if (arguments.length === 1) return codecFor(encoding).toString(this)
339
+
340
+ assertInteger(start, 'Start')
341
+ assertInteger(end, 'End')
342
+
343
+ const length = this.byteLength
344
+
345
+ if (start < 0) start = 0
346
+ if (start >= length) return ''
347
+
348
+ if (end <= start) return ''
349
+ if (end > length) end = length
350
+
351
+ return codecFor(encoding).toString(this, start, end)
352
+ }
353
+
354
+ toJSON() {
355
+ const length = this.byteLength
356
+ const data = new Array(length)
357
+
358
+ for (let i = 0; i < length; i++) data[i] = this[i]
359
+
360
+ return data
361
+ }
362
+
363
+ write(string, offset = 0, length = this.byteLength - offset, encoding = 'utf8') {
364
+ assertString(string, 'String')
365
+
366
+ // write(string)
367
+ if (arguments.length === 1) return utf8.write(this, string)
368
+
369
+ if (typeof offset === 'string') {
370
+ // write(string, encoding)
371
+ encoding = offset
372
+ offset = 0
373
+ length = this.byteLength
374
+ } else if (typeof length === 'string') {
375
+ // write(string, offset, encoding)
376
+ encoding = length
377
+ length = this.byteLength - offset
378
+ }
379
+
380
+ assertInteger(offset, 'Offset')
381
+ assertInteger(length, 'Length')
382
+
383
+ length = Math.min(length, exports.byteLength(string, encoding))
384
+
385
+ const byteLength = this.byteLength
386
+
387
+ let start = offset
388
+ if (start < 0) start = 0
389
+ if (start >= byteLength) return 0
390
+
391
+ let end = offset + length
392
+ if (end <= start) return 0
393
+ if (end > byteLength) end = byteLength
394
+
395
+ return codecFor(encoding).write(this, string, start, end)
396
+ }
397
+
398
+ readBigInt64BE(offset = 0) {
399
+ if (!Number.isInteger(offset)) offsetError(offset)
400
+ return viewOf(this).getBigInt64(offset, false)
401
+ }
402
+ readBigInt64LE(offset = 0) {
403
+ if (!Number.isInteger(offset)) offsetError(offset)
404
+ return viewOf(this).getBigInt64(offset, true)
405
+ }
406
+
407
+ readBigUint64BE(offset = 0) {
408
+ if (!Number.isInteger(offset)) offsetError(offset)
409
+ return viewOf(this).getBigUint64(offset, false)
410
+ }
411
+ readBigUint64LE(offset = 0) {
412
+ if (!Number.isInteger(offset)) offsetError(offset)
413
+ return viewOf(this).getBigUint64(offset, true)
414
+ }
415
+
416
+ readDoubleBE(offset = 0) {
417
+ if (!Number.isInteger(offset)) offsetError(offset)
418
+ return viewOf(this).getFloat64(offset, false)
419
+ }
420
+ readDoubleLE(offset = 0) {
421
+ if (!Number.isInteger(offset)) offsetError(offset)
422
+ return viewOf(this).getFloat64(offset, true)
423
+ }
424
+
425
+ readFloatBE(offset = 0) {
426
+ if (!Number.isInteger(offset)) offsetError(offset)
427
+ return viewOf(this).getFloat32(offset, false)
428
+ }
429
+ readFloatLE(offset = 0) {
430
+ if (!Number.isInteger(offset)) offsetError(offset)
431
+ return viewOf(this).getFloat32(offset, true)
432
+ }
433
+
434
+ readInt8(offset = 0) {
435
+ if (!Number.isInteger(offset)) offsetError(offset)
436
+ const value = this[offset]
437
+ if (value === undefined) boundsError()
438
+ return (value << 24) >> 24
439
+ }
440
+
441
+ readInt16BE(offset = 0) {
442
+ if (!Number.isInteger(offset)) offsetError(offset)
443
+ const first = this[offset]
444
+ const last = this[offset + 1]
445
+ if (first === undefined || last === undefined) boundsError()
446
+ return (((first << 8) | last) << 16) >> 16
447
+ }
448
+ readInt16LE(offset = 0) {
449
+ if (!Number.isInteger(offset)) offsetError(offset)
450
+ const first = this[offset]
451
+ const last = this[offset + 1]
452
+ if (first === undefined || last === undefined) boundsError()
453
+ return (((last << 8) | first) << 16) >> 16
454
+ }
455
+
456
+ readInt32BE(offset = 0) {
457
+ if (!Number.isInteger(offset)) offsetError(offset)
458
+ const first = this[offset]
459
+ const last = this[offset + 3]
460
+ if (first === undefined || last === undefined) boundsError()
461
+ return (first << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | last
462
+ }
463
+ readInt32LE(offset = 0) {
464
+ if (!Number.isInteger(offset)) offsetError(offset)
465
+ const first = this[offset]
466
+ const last = this[offset + 3]
467
+ if (first === undefined || last === undefined) boundsError()
468
+ return first | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (last << 24)
469
+ }
470
+
471
+ readIntBE(offset, byteLength) {
472
+ if (!Number.isInteger(offset)) offsetError(offset)
473
+ if (byteLength === 6) return readInt48BE(this, offset)
474
+ if (byteLength === 5) return readInt40BE(this, offset)
475
+ if (byteLength === 3) return readInt24BE(this, offset)
476
+ if (byteLength === 4) return this.readInt32BE(offset)
477
+ if (byteLength === 2) return this.readInt16BE(offset)
478
+ if (byteLength === 1) return this.readInt8(offset)
479
+ throw new RangeError(`Byte length must be between 1 and 6`)
480
+ }
481
+
482
+ readIntLE(offset, byteLength) {
483
+ if (!Number.isInteger(offset)) offsetError(offset)
484
+ if (byteLength === 6) return readInt48LE(this, offset)
485
+ if (byteLength === 5) return readInt40LE(this, offset)
486
+ if (byteLength === 3) return readInt24LE(this, offset)
487
+ if (byteLength === 4) return this.readInt32LE(offset)
488
+ if (byteLength === 2) return this.readInt16LE(offset)
489
+ if (byteLength === 1) return this.readInt8(offset)
490
+ throw new RangeError(`Byte length must be between 1 and 6`)
491
+ }
492
+
493
+ readUint8(offset = 0) {
494
+ if (!Number.isInteger(offset)) offsetError(offset)
495
+ const value = this[offset]
496
+ if (value === undefined) boundsError()
497
+ return value
498
+ }
499
+
500
+ readUint16BE(offset = 0) {
501
+ if (!Number.isInteger(offset)) offsetError(offset)
502
+ const first = this[offset]
503
+ const last = this[offset + 1]
504
+ if (first === undefined || last === undefined) boundsError()
505
+ return (first << 8) | last
506
+ }
507
+ readUint16LE(offset = 0) {
508
+ if (!Number.isInteger(offset)) offsetError(offset)
509
+ const first = this[offset]
510
+ const last = this[offset + 1]
511
+ if (first === undefined || last === undefined) boundsError()
512
+ return (last << 8) | first
513
+ }
514
+
515
+ readUint32BE(offset = 0) {
516
+ if (!Number.isInteger(offset)) offsetError(offset)
517
+ const first = this[offset]
518
+ const last = this[offset + 3]
519
+ if (first === undefined || last === undefined) boundsError()
520
+ return first * 0x1000000 + ((this[offset + 1] << 16) | (this[offset + 2] << 8) | last)
521
+ }
522
+ readUint32LE(offset = 0) {
523
+ if (!Number.isInteger(offset)) offsetError(offset)
524
+ const first = this[offset]
525
+ const last = this[offset + 3]
526
+ if (first === undefined || last === undefined) boundsError()
527
+ return last * 0x1000000 + ((this[offset + 2] << 16) | (this[offset + 1] << 8) | first)
528
+ }
529
+
530
+ readUintBE(offset, byteLength) {
531
+ if (!Number.isInteger(offset)) offsetError(offset)
532
+ if (byteLength === 6) return readUint48BE(this, offset)
533
+ if (byteLength === 5) return readUint40BE(this, offset)
534
+ if (byteLength === 3) return readUint24BE(this, offset)
535
+ if (byteLength === 4) return this.readUint32BE(offset)
536
+ if (byteLength === 2) return this.readUint16BE(offset)
537
+ if (byteLength === 1) return this.readUint8(offset)
538
+ throw new RangeError(`Byte length must be between 1 and 6`)
539
+ }
540
+
541
+ readUintLE(offset, byteLength) {
542
+ if (!Number.isInteger(offset)) offsetError(offset)
543
+ if (byteLength === 6) return readUint48LE(this, offset)
544
+ if (byteLength === 5) return readUint40LE(this, offset)
545
+ if (byteLength === 3) return readUint24LE(this, offset)
546
+ if (byteLength === 4) return this.readUint32LE(offset)
547
+ if (byteLength === 2) return this.readUint16LE(offset)
548
+ if (byteLength === 1) return this.readUint8(offset)
549
+ throw new RangeError(`Byte length must be between 1 and 6`)
550
+ }
551
+
552
+ writeBigInt64BE(value, offset = 0) {
553
+ if (!Number.isInteger(offset)) offsetError(offset)
554
+ viewOf(this).setBigInt64(offset, value, false)
555
+ return offset + 8
556
+ }
557
+ writeBigInt64LE(value, offset = 0) {
558
+ if (!Number.isInteger(offset)) offsetError(offset)
559
+ viewOf(this).setBigInt64(offset, value, true)
560
+ return offset + 8
561
+ }
562
+
563
+ writeBigUint64BE(value, offset = 0) {
564
+ if (!Number.isInteger(offset)) offsetError(offset)
565
+ viewOf(this).setBigUint64(offset, value, false)
566
+ return offset + 8
567
+ }
568
+ writeBigUint64LE(value, offset = 0) {
569
+ if (!Number.isInteger(offset)) offsetError(offset)
570
+ viewOf(this).setBigUint64(offset, value, true)
571
+ return offset + 8
572
+ }
573
+
574
+ writeDoubleBE(value, offset = 0) {
575
+ if (!Number.isInteger(offset)) offsetError(offset)
576
+ viewOf(this).setFloat64(offset, value, false)
577
+ return offset + 8
578
+ }
579
+ writeDoubleLE(value, offset = 0) {
580
+ if (!Number.isInteger(offset)) offsetError(offset)
581
+ viewOf(this).setFloat64(offset, value, true)
582
+ return offset + 8
583
+ }
584
+
585
+ writeFloatBE(value, offset = 0) {
586
+ if (!Number.isInteger(offset)) offsetError(offset)
587
+ viewOf(this).setFloat32(offset, value, false)
588
+ return offset + 4
589
+ }
590
+ writeFloatLE(value, offset = 0) {
591
+ if (!Number.isInteger(offset)) offsetError(offset)
592
+ viewOf(this).setFloat32(offset, value, true)
593
+ return offset + 4
594
+ }
595
+
596
+ writeInt8(value, offset = 0) {
597
+ return this.writeUint8(value, offset)
598
+ }
599
+
600
+ writeInt16BE(value, offset = 0) {
601
+ return this.writeUint16BE(value, offset)
602
+ }
603
+ writeInt16LE(value, offset = 0) {
604
+ return this.writeUint16LE(value, offset)
605
+ }
606
+
607
+ writeInt32BE(value, offset = 0) {
608
+ return this.writeUint32BE(value, offset)
609
+ }
610
+ writeInt32LE(value, offset = 0) {
611
+ return this.writeUint32LE(value, offset)
612
+ }
613
+
614
+ writeIntBE(value, offset, byteLength) {
615
+ if (!Number.isInteger(offset)) offsetError(offset)
616
+ if (byteLength === 6) return writeInt48BE(this, value, offset)
617
+ if (byteLength === 5) return writeInt40BE(this, value, offset)
618
+ if (byteLength === 3) return writeInt24BE(this, value, offset)
619
+ if (byteLength === 4) return this.writeInt32BE(value, offset)
620
+ if (byteLength === 2) return this.writeInt16BE(value, offset)
621
+ if (byteLength === 1) return this.writeInt8(value, offset)
622
+ throw new RangeError(`Byte length must be between 1 and 6`)
623
+ }
624
+
625
+ writeIntLE(value, offset, byteLength) {
626
+ if (!Number.isInteger(offset)) offsetError(offset)
627
+ if (byteLength === 6) return writeInt48LE(this, value, offset)
628
+ if (byteLength === 5) return writeInt40LE(this, value, offset)
629
+ if (byteLength === 3) return writeInt24LE(this, value, offset)
630
+ if (byteLength === 4) return this.writeInt32LE(value, offset)
631
+ if (byteLength === 2) return this.writeInt16LE(value, offset)
632
+ if (byteLength === 1) return this.writeInt8(value, offset)
633
+ throw new RangeError(`Byte length must be between 1 and 6`)
634
+ }
635
+
636
+ writeUint8(value, offset = 0) {
637
+ if (!Number.isInteger(offset)) offsetError(offset)
638
+ if (this[offset] === undefined) boundsError()
639
+ this[offset] = value
640
+ return offset + 1
641
+ }
642
+
643
+ writeUint16BE(value, offset = 0) {
644
+ if (!Number.isInteger(offset)) offsetError(offset)
645
+ if (this[offset] === undefined || this[offset + 1] === undefined) boundsError()
646
+ this[offset] = value >>> 8
647
+ this[offset + 1] = value
648
+ return offset + 2
649
+ }
650
+ writeUint16LE(value, offset = 0) {
651
+ if (!Number.isInteger(offset)) offsetError(offset)
652
+ if (this[offset] === undefined || this[offset + 1] === undefined) boundsError()
653
+ this[offset] = value
654
+ this[offset + 1] = value >>> 8
655
+ return offset + 2
656
+ }
657
+
658
+ writeUint32LE(value, offset = 0) {
659
+ if (!Number.isInteger(offset)) offsetError(offset)
660
+ if (this[offset] === undefined || this[offset + 3] === undefined) boundsError()
661
+ this[offset] = value
662
+ this[offset + 1] = value >>> 8
663
+ this[offset + 2] = value >>> 16
664
+ this[offset + 3] = value >>> 24
665
+ return offset + 4
666
+ }
667
+ writeUint32BE(value, offset = 0) {
668
+ if (!Number.isInteger(offset)) offsetError(offset)
669
+ if (this[offset] === undefined || this[offset + 3] === undefined) boundsError()
670
+ this[offset] = value >>> 24
671
+ this[offset + 1] = value >>> 16
672
+ this[offset + 2] = value >>> 8
673
+ this[offset + 3] = value
674
+ return offset + 4
675
+ }
676
+
677
+ writeUintBE(value, offset, byteLength) {
678
+ if (!Number.isInteger(offset)) offsetError(offset)
679
+ if (byteLength === 6) return writeUint48BE(this, value, offset)
680
+ if (byteLength === 5) return writeUint40BE(this, value, offset)
681
+ if (byteLength === 3) return writeUint24BE(this, value, offset)
682
+ if (byteLength === 4) return this.writeUint32BE(value, offset)
683
+ if (byteLength === 2) return this.writeUint16BE(value, offset)
684
+ if (byteLength === 1) return this.writeUint8(value, offset)
685
+ throw new RangeError(`Byte length must be between 1 and 6`)
686
+ }
687
+
688
+ writeUintLE(value, offset, byteLength) {
689
+ if (!Number.isInteger(offset)) offsetError(offset)
690
+ if (byteLength === 6) return writeUint48LE(this, value, offset)
691
+ if (byteLength === 5) return writeUint40LE(this, value, offset)
692
+ if (byteLength === 3) return writeUint24LE(this, value, offset)
693
+ if (byteLength === 4) return this.writeUint32LE(value, offset)
694
+ if (byteLength === 2) return this.writeUint16LE(value, offset)
695
+ if (byteLength === 1) return this.writeUint8(value, offset)
696
+ throw new RangeError(`Byte length must be between 1 and 6`)
697
+ }
698
+ }
699
+
700
+ for (const [alias, name] of [
701
+ ['readBigUInt64BE', 'readBigUint64BE'],
702
+ ['readBigUInt64LE', 'readBigUint64LE'],
703
+ ['readUInt8', 'readUint8'],
704
+ ['readUInt16BE', 'readUint16BE'],
705
+ ['readUInt16LE', 'readUint16LE'],
706
+ ['readUInt32BE', 'readUint32BE'],
707
+ ['readUInt32LE', 'readUint32LE'],
708
+ ['readUIntBE', 'readUintBE'],
709
+ ['readUIntLE', 'readUintLE'],
710
+ ['writeBigUInt64BE', 'writeBigUint64BE'],
711
+ ['writeBigUInt64LE', 'writeBigUint64LE'],
712
+ ['writeUInt8', 'writeUint8'],
713
+ ['writeUInt16BE', 'writeUint16BE'],
714
+ ['writeUInt16LE', 'writeUint16LE'],
715
+ ['writeUInt32BE', 'writeUint32BE'],
716
+ ['writeUInt32LE', 'writeUint32LE'],
717
+ ['writeUIntBE', 'writeUintBE'],
718
+ ['writeUIntLE', 'writeUintLE']
719
+ ]) {
720
+ Object.defineProperty(Buffer.prototype, alias, {
721
+ value: Buffer.prototype[name],
722
+ writable: true,
723
+ configurable: true
724
+ })
725
+ }
726
+
727
+ module.exports = exports = Buffer
728
+
729
+ exports.Buffer = Buffer // For Node.js compatibility
730
+
731
+ exports.constants = constants
732
+
733
+ const codecs = Object.create(null)
734
+
735
+ codecs.ascii = ascii
736
+ codecs.base64 = base64
737
+ codecs.base64url = base64url
738
+ codecs.hex = hex
739
+ codecs.utf8 = codecs['utf-8'] = utf8
740
+ codecs.utf16le = codecs.ucs2 = codecs['utf-16le'] = codecs['ucs-2'] = utf16le
741
+ codecs.latin1 = codecs.binary = latin1
742
+
743
+ function codecFor(encoding) {
744
+ if (encoding === undefined) return utf8
745
+
746
+ let codec = codecs[encoding]
747
+ if (codec !== undefined) return codec
748
+
749
+ if (typeof encoding !== 'string') {
750
+ throw new TypeError(`Encoding must be a string, received type ${typeof encoding}`)
751
+ }
752
+
753
+ codec = codecs[encoding.toLowerCase()]
754
+ if (codec !== undefined) return codec
755
+
756
+ throw new Error(`Unknown encoding '${encoding}'`)
757
+ }
758
+
759
+ const views = new WeakMap()
760
+
761
+ function viewOf(buffer) {
762
+ let view = views.get(buffer)
763
+ if (view === undefined) {
764
+ const store = buffer.buffer
765
+
766
+ view = new DataView(store, buffer.byteOffset, buffer.byteLength)
767
+
768
+ // A view of a store that can still change size covers the wrong range as
769
+ // soon as it does, or falls out of bounds entirely, so it is only worth
770
+ // remembering one of a store that cannot.
771
+ if (store.resizable !== true && store.growable !== true) {
772
+ views.set(buffer, view)
773
+ }
774
+ }
775
+ return view
776
+ }
777
+
778
+ exports.isBuffer = function isBuffer(value) {
779
+ if (value instanceof Buffer) return true
780
+
781
+ return typeof value === 'object' && value !== null && value[kind] === Buffer[kind]
782
+ }
783
+
784
+ exports.isEncoding = function isEncoding(encoding) {
785
+ try {
786
+ codecFor(encoding)
787
+ return true
788
+ } catch {
789
+ return false
790
+ }
791
+ }
792
+
793
+ exports.isASCII = function isASCII(buffer) {
794
+ return ascii.validate(buffer)
795
+ }
796
+
797
+ // for Node.js compatibily
798
+ exports.isAscii = exports.isASCII
799
+
800
+ exports.isUTF8 = function isUTF8(buffer) {
801
+ return utf8.validate(buffer)
802
+ }
803
+
804
+ // for Node.js compatibility
805
+ exports.isUtf8 = exports.isUTF8
806
+
807
+ exports.alloc = function alloc(size, fill, encoding) {
808
+ assertSize(size, 'Size')
809
+
810
+ if (fill !== undefined && fill !== 0) {
811
+ const buffer = new Buffer(size, { uninitialized: true })
812
+
813
+ return buffer.fill(fill, 0, buffer.byteLength, encoding)
814
+ }
815
+
816
+ return new Buffer(size)
817
+ }
818
+
819
+ exports.allocUnsafe = function allocUnsafe(size) {
820
+ assertSize(size, 'Size')
821
+
822
+ return allocate(size)
823
+ }
824
+
825
+ exports.allocUnsafeSlow = function allocUnsafeSlow(size) {
826
+ assertSize(size, 'Size')
827
+
828
+ return new Buffer(size, { uninitialized: true })
829
+ }
830
+
831
+ function assertInteger(value, name) {
832
+ if (typeof value !== 'number') {
833
+ throw new TypeError(`${name} must be a number, received type ${typeof value}`)
834
+ }
835
+
836
+ if (Number.isInteger(value) === false) {
837
+ throw new RangeError(`${name} must be an integer`)
838
+ }
839
+ }
840
+
841
+ function assertString(value, name) {
842
+ if (typeof value !== 'string') {
843
+ throw new TypeError(`${name} must be a string, received type ${typeof value}`)
844
+ }
845
+ }
846
+
847
+ function assertView(value, name) {
848
+ if (ArrayBuffer.isView(value) === false) {
849
+ throw new TypeError(`${name} must be a view, received type ${typeof value}`)
850
+ }
851
+ }
852
+
853
+ function assertSize(size, name) {
854
+ if (typeof size !== 'number') {
855
+ throw new TypeError(`${name} must be a number, received type ${typeof size}`)
856
+ }
857
+
858
+ if (Number.isInteger(size) === false || size < 0 || size > constants.MAX_LENGTH) {
859
+ throw new RangeError(`${name} must be an integer between 0 and ${constants.MAX_LENGTH}`)
860
+ }
861
+ }
862
+
863
+ exports.byteLength = function byteLength(string, encoding) {
864
+ if (typeof string === 'string') {
865
+ return codecFor(encoding).byteLength(string)
866
+ }
867
+
868
+ return string.byteLength
869
+ }
870
+
871
+ exports.compare = function compare(a, b) {
872
+ assertView(a, 'First buffer')
873
+ assertView(b, 'Second buffer')
874
+
875
+ return checked(
876
+ binding.compare(a.buffer, a.byteOffset, a.byteLength, b.buffer, b.byteOffset, b.byteLength)
877
+ )
878
+ }
879
+
880
+ exports.concat = function concat(buffers, length) {
881
+ const n = buffers.length
882
+
883
+ for (let i = 0; i < n; i++) {
884
+ const buffer = buffers[i]
885
+
886
+ if (ArrayBuffer.isView(buffer) === false || buffer.BYTES_PER_ELEMENT !== 1) {
887
+ throw new TypeError(`buffers[${i}] must be a view of single bytes`)
888
+ }
889
+ }
890
+
891
+ if (length === undefined) {
892
+ length = 0
893
+ for (let i = 0; i < n; i++) length += buffers[i].byteLength
894
+ } else {
895
+ assertSize(length, 'Length')
896
+ }
897
+
898
+ const result = allocate(length)
899
+
900
+ let offset = 0
901
+
902
+ for (let i = 0; i < n; i++) {
903
+ const buffer = buffers[i]
904
+ const buffered = buffer.byteLength
905
+
906
+ if (offset + buffered > length) {
907
+ const remaining = length - offset
908
+
909
+ result.set(new Uint8Array(buffer.buffer, buffer.byteOffset, remaining), offset)
910
+
911
+ offset = length
912
+ break
913
+ }
914
+
915
+ result.set(buffer, offset)
916
+ offset += buffered
917
+ }
918
+
919
+ // Only the bytes the inputs did not reach need initializing.
920
+ if (offset < length) result.fill(0, offset, length)
921
+
922
+ return result
923
+ }
924
+
925
+ exports.coerce = function coerce(buffer) {
926
+ if (exports.isBuffer(buffer)) return buffer
927
+ return new Buffer(buffer.buffer, buffer.byteOffset, buffer.byteLength)
928
+ }
929
+
930
+ exports.copyBytesFrom = function copyBytesFrom(view, offset = 0, length = view.length - offset) {
931
+ assertSize(offset, 'Offset')
932
+ assertSize(length, 'Length')
933
+
934
+ if (offset + length > view.length) {
935
+ throw new RangeError('View length is out of range')
936
+ }
937
+
938
+ if (offset !== 0 || length !== view.length) {
939
+ view = view.subarray(offset, offset + length)
940
+ }
941
+
942
+ return new Buffer(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength))
943
+ }
944
+
945
+ exports.from = function from(value, encodingOrOffset, length) {
946
+ // from(string, encoding)
947
+ if (typeof value === 'string') return fromString(value, encodingOrOffset)
948
+
949
+ // from(array)
950
+ if (Array.isArray(value)) return fromArray(value)
951
+
952
+ // from(buffer)
953
+ if (ArrayBuffer.isView(value)) return fromBuffer(value)
954
+
955
+ // from(arrayBuffer[, offset[, length]])
956
+ return fromArrayBuffer(value, encodingOrOffset, length)
957
+ }
958
+
959
+ function fromString(string, encoding) {
960
+ const codec = codecFor(encoding)
961
+ const length = codec.byteLength(string)
962
+ const buffer = allocate(length)
963
+ const written = codec.write(buffer, string, 0, length)
964
+
965
+ // Some codecs only know an upper bound before encoding, and the remainder is
966
+ // uninitialized rather than zeroed.
967
+ if (written < length) return buffer.subarray(0, written)
968
+
969
+ return buffer
970
+ }
971
+
972
+ function fromArray(array) {
973
+ const buffer = allocate(array.length)
974
+ buffer.set(array)
975
+ return buffer
976
+ }
977
+
978
+ function fromBuffer(buffer) {
979
+ // A view is copied element wise, each element truncated to a byte, so the
980
+ // copy is sized by element count and not by byte length. A view that is not
981
+ // array like, such as a DataView, reports no length and has nothing to copy.
982
+ const length = typeof buffer.length === 'number' ? buffer.length : 0
983
+
984
+ const copy = allocate(length)
985
+ copy.set(buffer)
986
+ return copy
987
+ }
988
+
989
+ function fromArrayBuffer(arrayBuffer, offset, length) {
990
+ if (offset !== undefined) assertInteger(offset, 'Offset')
991
+ if (length !== undefined) assertInteger(length, 'Length')
992
+
993
+ return new Buffer(arrayBuffer, offset, length)
994
+ }
995
+
996
+ function bidirectionalIndexOf(buffer, value, offset, encoding, first) {
997
+ if (typeof value !== 'string') assertView(value, 'Value')
998
+
999
+ const length = buffer.byteLength
1000
+
1001
+ if (length === 0) return -1
1002
+
1003
+ if (typeof offset === 'string') {
1004
+ encoding = offset
1005
+ offset = 0
1006
+ } else if (offset === undefined) {
1007
+ offset = first ? 0 : length - 1
1008
+ } else if (offset < 0) {
1009
+ offset += length
1010
+ }
1011
+
1012
+ if (offset >= length) {
1013
+ if (first) return -1
1014
+ else offset = length - 1
1015
+ } else if (offset < 0) {
1016
+ if (first) offset = 0
1017
+ else return -1
1018
+ }
1019
+
1020
+ if (typeof value === 'string') value = exports.from(value, encoding)
1021
+
1022
+ const needleLength = value.byteLength
1023
+
1024
+ if (needleLength === 0) return -1
1025
+
1026
+ const last = length - needleLength
1027
+
1028
+ if (first) {
1029
+ if (offset > last) return -1
1030
+ } else {
1031
+ if (offset > last) offset = last
1032
+ if (offset < 0) return -1
1033
+ }
1034
+
1035
+ // Confirming a match at the position the search starts from costs a handful
1036
+ // of comparisons, and keeps prefix checks off the native path entirely.
1037
+ let j = 0
1038
+ while (j < needleLength && buffer[offset + j] === value[j]) j++
1039
+ if (j === needleLength) return offset
1040
+
1041
+ if (first) {
1042
+ if (offset === last) return -1
1043
+
1044
+ return checked(
1045
+ binding.indexOf(
1046
+ buffer.buffer,
1047
+ buffer.byteOffset,
1048
+ length,
1049
+ value.buffer,
1050
+ value.byteOffset,
1051
+ needleLength,
1052
+ offset + 1
1053
+ )
1054
+ )
1055
+ }
1056
+
1057
+ if (offset === 0) return -1
1058
+
1059
+ return checked(
1060
+ binding.lastIndexOf(
1061
+ buffer.buffer,
1062
+ buffer.byteOffset,
1063
+ length,
1064
+ value.buffer,
1065
+ value.byteOffset,
1066
+ needleLength,
1067
+ offset - 1
1068
+ )
1069
+ )
1070
+ }
1071
+
1072
+ // Writes the pattern once and then repeatedly doubles it in place, so the byte
1073
+ // wise work is proportional to the pattern rather than to the range. Each block
1074
+ // copied is a whole number of periods, which keeps the pattern aligned.
1075
+ function fillPattern(buffer, value, length, offset, end) {
1076
+ const n = end - offset
1077
+
1078
+ // Below this the copies cost more than they save.
1079
+ if (n < 64) {
1080
+ for (let i = 0; i < n; i++) buffer[offset + i] = value[i % length]
1081
+ return buffer
1082
+ }
1083
+
1084
+ let filled = length < n ? length : n
1085
+
1086
+ for (let i = 0; i < filled; i++) buffer[offset + i] = value[i]
1087
+
1088
+ while (filled < n) {
1089
+ const copy = filled < n - filled ? filled : n - filled
1090
+
1091
+ buffer.copyWithin(offset + filled, offset, offset + copy)
1092
+
1093
+ filled += copy
1094
+ }
1095
+
1096
+ return buffer
1097
+ }
1098
+
1099
+ function allocate(size) {
1100
+ if (Number.isNaN(size) || size <= 0 || size >= poolSize / 2) {
1101
+ return new Buffer(size, { uninitialized: true })
1102
+ }
1103
+
1104
+ if (pool === null || poolLength !== poolSize || poolLength - poolOffset < size) {
1105
+ pool = binding.allocUnsafe(poolSize)
1106
+ poolLength = poolSize
1107
+ poolOffset = 0
1108
+ }
1109
+
1110
+ const buffer = new Buffer(pool, poolOffset, size)
1111
+
1112
+ // Keep the next buffer 8 byte aligned so the wider accessors are not needlessly
1113
+ // reading across word boundaries.
1114
+ poolOffset = (poolOffset + size + 7) & ~7
1115
+
1116
+ return buffer
1117
+ }
1118
+
1119
+ function swap(buffer, n, m) {
1120
+ const i = buffer[n]
1121
+ buffer[n] = buffer[m]
1122
+ buffer[m] = i
1123
+ }
1124
+
1125
+ exports.atob = function atob(data) {
1126
+ return Buffer.from(data, 'base64').toString('latin1')
1127
+ }
1128
+
1129
+ exports.btoa = function btoa(data) {
1130
+ if (typeof data !== 'string') data = String(data)
1131
+
1132
+ return Buffer.from(data, 'latin1').toString('base64')
1133
+ }
1134
+
1135
+ exports.transcode = function transcode(buffer, from, to) {
1136
+ return Buffer.from(buffer.toString(from), to)
1137
+ }
1138
+
1139
+ function boundsError() {
1140
+ throw new RangeError('Offset is outside the bounds of the buffer')
1141
+ }
1142
+
1143
+ function offsetError(offset) {
1144
+ if (typeof offset !== 'number') {
1145
+ throw new TypeError(`Offset must be a number, received type ${typeof offset}`)
1146
+ }
1147
+
1148
+ throw new RangeError('Offset must be an integer')
1149
+ }
1150
+
1151
+ function readInt48BE(buffer, offset) {
1152
+ const value = readUint48BE(buffer, offset)
1153
+ return value >= 0x800000000000 ? value - 0x1000000000000 : value
1154
+ }
1155
+
1156
+ function readInt48LE(buffer, offset) {
1157
+ const value = readUint48LE(buffer, offset)
1158
+ return value >= 0x800000000000 ? value - 0x1000000000000 : value
1159
+ }
1160
+
1161
+ function readInt40BE(buffer, offset) {
1162
+ const value = readUint40BE(buffer, offset)
1163
+ return value >= 0x8000000000 ? value - 0x10000000000 : value
1164
+ }
1165
+
1166
+ function readInt40LE(buffer, offset) {
1167
+ const value = readUint40LE(buffer, offset)
1168
+ return value >= 0x8000000000 ? value - 0x10000000000 : value
1169
+ }
1170
+
1171
+ function readInt24BE(buffer, offset) {
1172
+ const value = readUint24BE(buffer, offset)
1173
+ return value & 0x800000 ? value - 0x1000000 : value
1174
+ }
1175
+
1176
+ function readInt24LE(buffer, offset) {
1177
+ const value = readUint24LE(buffer, offset)
1178
+ return value & 0x800000 ? value - 0x1000000 : value
1179
+ }
1180
+
1181
+ function readUint48BE(buffer, offset) {
1182
+ const first = buffer[offset]
1183
+ const last = buffer[offset + 5]
1184
+ if (first === undefined || last === undefined) boundsError()
1185
+ return (
1186
+ (first * 0x100 + buffer[offset + 1]) * 0x100000000 +
1187
+ ((buffer[offset + 2] << 24) >>> 0) +
1188
+ (buffer[offset + 3] << 16) +
1189
+ (buffer[offset + 4] << 8) +
1190
+ last
1191
+ )
1192
+ }
1193
+
1194
+ function readUint48LE(buffer, offset) {
1195
+ const first = buffer[offset]
1196
+ const last = buffer[offset + 5]
1197
+ if (first === undefined || last === undefined) boundsError()
1198
+ return (
1199
+ (buffer[offset + 4] + last * 0x100) * 0x100000000 +
1200
+ ((buffer[offset + 3] << 24) >>> 0) +
1201
+ (buffer[offset + 2] << 16) +
1202
+ (buffer[offset + 1] << 8) +
1203
+ first
1204
+ )
1205
+ }
1206
+
1207
+ function readUint40BE(buffer, offset) {
1208
+ const first = buffer[offset]
1209
+ const last = buffer[offset + 4]
1210
+ if (first === undefined || last === undefined) boundsError()
1211
+ return (
1212
+ first * 0x100000000 +
1213
+ ((buffer[offset + 1] << 24) >>> 0) +
1214
+ (buffer[offset + 2] << 16) +
1215
+ (buffer[offset + 3] << 8) +
1216
+ last
1217
+ )
1218
+ }
1219
+
1220
+ function readUint40LE(buffer, offset) {
1221
+ const first = buffer[offset]
1222
+ const last = buffer[offset + 4]
1223
+ if (first === undefined || last === undefined) boundsError()
1224
+ return (
1225
+ last * 0x100000000 +
1226
+ ((buffer[offset + 3] << 24) >>> 0) +
1227
+ (buffer[offset + 2] << 16) +
1228
+ (buffer[offset + 1] << 8) +
1229
+ first
1230
+ )
1231
+ }
1232
+
1233
+ function readUint24BE(buffer, offset) {
1234
+ const first = buffer[offset]
1235
+ const last = buffer[offset + 2]
1236
+ if (first === undefined || last === undefined) boundsError()
1237
+ return (first << 16) | (buffer[offset + 1] << 8) | last
1238
+ }
1239
+
1240
+ function readUint24LE(buffer, offset) {
1241
+ const first = buffer[offset]
1242
+ const last = buffer[offset + 2]
1243
+ if (first === undefined || last === undefined) boundsError()
1244
+ return (last << 16) | (buffer[offset + 1] << 8) | first
1245
+ }
1246
+
1247
+ function writeInt48BE(buffer, value, offset) {
1248
+ if (value < 0) value += 0x1000000000000
1249
+ return writeUint48BE(buffer, value, offset)
1250
+ }
1251
+
1252
+ function writeInt48LE(buffer, value, offset) {
1253
+ if (value < 0) value += 0x1000000000000
1254
+ return writeUint48LE(buffer, value, offset)
1255
+ }
1256
+
1257
+ function writeInt40BE(buffer, value, offset) {
1258
+ if (value < 0) value += 0x10000000000
1259
+ return writeUint40BE(buffer, value, offset)
1260
+ }
1261
+
1262
+ function writeInt40LE(buffer, value, offset) {
1263
+ if (value < 0) value += 0x10000000000
1264
+ return writeUint40LE(buffer, value, offset)
1265
+ }
1266
+
1267
+ function writeInt24BE(buffer, value, offset) {
1268
+ if (value < 0) value += 0x1000000
1269
+ return writeUint24BE(buffer, value, offset)
1270
+ }
1271
+
1272
+ function writeInt24LE(buffer, value, offset) {
1273
+ if (value < 0) value += 0x1000000
1274
+ return writeUint24LE(buffer, value, offset)
1275
+ }
1276
+
1277
+ function writeUint48BE(buffer, value, offset) {
1278
+ if (buffer[offset] === undefined || buffer[offset + 5] === undefined) boundsError()
1279
+ const hi = Math.floor(value / 0x100000000)
1280
+ const lo = value >>> 0
1281
+ buffer[offset] = hi >>> 8
1282
+ buffer[offset + 1] = hi
1283
+ buffer[offset + 2] = lo >>> 24
1284
+ buffer[offset + 3] = lo >>> 16
1285
+ buffer[offset + 4] = lo >>> 8
1286
+ buffer[offset + 5] = lo
1287
+ return offset + 6
1288
+ }
1289
+
1290
+ function writeUint48LE(buffer, value, offset) {
1291
+ if (buffer[offset] === undefined || buffer[offset + 5] === undefined) boundsError()
1292
+ const hi = Math.floor(value / 0x100000000)
1293
+ const lo = value >>> 0
1294
+ buffer[offset] = lo
1295
+ buffer[offset + 1] = lo >>> 8
1296
+ buffer[offset + 2] = lo >>> 16
1297
+ buffer[offset + 3] = lo >>> 24
1298
+ buffer[offset + 4] = hi
1299
+ buffer[offset + 5] = hi >>> 8
1300
+ return offset + 6
1301
+ }
1302
+
1303
+ function writeUint40BE(buffer, value, offset) {
1304
+ if (buffer[offset] === undefined || buffer[offset + 4] === undefined) boundsError()
1305
+ const lo = value >>> 0
1306
+ buffer[offset] = Math.floor(value / 0x100000000)
1307
+ buffer[offset + 1] = lo >>> 24
1308
+ buffer[offset + 2] = lo >>> 16
1309
+ buffer[offset + 3] = lo >>> 8
1310
+ buffer[offset + 4] = lo
1311
+ return offset + 5
1312
+ }
1313
+
1314
+ function writeUint40LE(buffer, value, offset) {
1315
+ if (buffer[offset] === undefined || buffer[offset + 4] === undefined) boundsError()
1316
+ const lo = value >>> 0
1317
+ buffer[offset] = lo
1318
+ buffer[offset + 1] = lo >>> 8
1319
+ buffer[offset + 2] = lo >>> 16
1320
+ buffer[offset + 3] = lo >>> 24
1321
+ buffer[offset + 4] = Math.floor(value / 0x100000000)
1322
+ return offset + 5
1323
+ }
1324
+
1325
+ function writeUint24BE(buffer, value, offset) {
1326
+ if (buffer[offset] === undefined || buffer[offset + 2] === undefined) boundsError()
1327
+ buffer[offset] = value >>> 16
1328
+ buffer[offset + 1] = value >>> 8
1329
+ buffer[offset + 2] = value
1330
+ return offset + 3
1331
+ }
1332
+
1333
+ function writeUint24LE(buffer, value, offset) {
1334
+ if (buffer[offset] === undefined || buffer[offset + 2] === undefined) boundsError()
1335
+ buffer[offset] = value
1336
+ buffer[offset + 1] = value >>> 8
1337
+ buffer[offset + 2] = value >>> 16
1338
+ return offset + 3
1339
+ }