bare-buffer 1.2.2
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/CMakeLists.txt +35 -0
- package/LICENSE +201 -0
- package/README.md +22 -0
- package/binding.c +402 -0
- package/binding.js +1 -0
- package/index.js +462 -0
- package/lib/ascii.js +25 -0
- package/lib/base64.js +18 -0
- package/lib/hex.js +13 -0
- package/lib/utf16le.js +34 -0
- package/lib/utf8.js +13 -0
- package/package.json +37 -0
- package/vendor/libbase64/CMakeLists.txt +52 -0
- package/vendor/libbase64/LICENSE +201 -0
- package/vendor/libbase64/README.md +11 -0
- package/vendor/libbase64/include/base64.h +21 -0
- package/vendor/libbase64/src/base64.c +96 -0
- package/vendor/libhex/CMakeLists.txt +52 -0
- package/vendor/libhex/LICENSE +201 -0
- package/vendor/libhex/README.md +11 -0
- package/vendor/libhex/include/hex.h +21 -0
- package/vendor/libhex/src/hex.c +66 -0
package/index.js
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
const ascii = require('./lib/ascii')
|
|
2
|
+
const base64 = require('./lib/base64')
|
|
3
|
+
const hex = require('./lib/hex')
|
|
4
|
+
const utf8 = require('./lib/utf8')
|
|
5
|
+
const utf16le = require('./lib/utf16le')
|
|
6
|
+
const binding = require('./binding')
|
|
7
|
+
|
|
8
|
+
const Buffer = module.exports = exports = class Buffer extends Uint8Array {
|
|
9
|
+
[Symbol.species] () {
|
|
10
|
+
return Buffer
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
copy (target, targetStart = 0, sourceStart = 0, sourceEnd = this.byteLength) {
|
|
14
|
+
let source = this
|
|
15
|
+
|
|
16
|
+
if (targetStart < 0) targetStart = 0
|
|
17
|
+
if (targetStart >= target.byteLength) return 0
|
|
18
|
+
|
|
19
|
+
const targetLength = target.byteLength - targetStart
|
|
20
|
+
|
|
21
|
+
if (sourceStart < 0) sourceStart = 0
|
|
22
|
+
if (sourceStart >= source.byteLength) return 0
|
|
23
|
+
|
|
24
|
+
if (sourceEnd <= sourceStart) return 0
|
|
25
|
+
if (sourceEnd > source.byteLength) sourceEnd = source.byteLength
|
|
26
|
+
|
|
27
|
+
if (sourceEnd - sourceStart > targetLength) sourceEnd = sourceStart + targetLength
|
|
28
|
+
|
|
29
|
+
const sourceLength = sourceEnd - sourceStart
|
|
30
|
+
|
|
31
|
+
if (source === target) {
|
|
32
|
+
target.copyWithin(targetStart, sourceStart, sourceEnd)
|
|
33
|
+
} else {
|
|
34
|
+
if (sourceStart !== 0 || sourceEnd !== source.byteLength) source = source.subarray(sourceStart, sourceEnd)
|
|
35
|
+
|
|
36
|
+
target.set(source, targetStart)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return sourceLength
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
equals (target) {
|
|
43
|
+
const source = this
|
|
44
|
+
|
|
45
|
+
if (source === target) return true
|
|
46
|
+
|
|
47
|
+
if (source.byteLength !== target.byteLength) return false
|
|
48
|
+
|
|
49
|
+
return binding.compare(source, target) === 0
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
compare (target, targetStart = 0, targetEnd = target.byteLength, sourceStart = 0, sourceEnd = this.byteLength) {
|
|
53
|
+
let source = this
|
|
54
|
+
|
|
55
|
+
if (source === target) return 0
|
|
56
|
+
|
|
57
|
+
if (arguments.length === 1) return binding.compare(source, target)
|
|
58
|
+
|
|
59
|
+
if (targetStart < 0) targetStart = 0
|
|
60
|
+
if (targetStart > target.byteLength) targetStart = target.byteLength
|
|
61
|
+
|
|
62
|
+
if (targetEnd < targetStart) targetEnd = targetStart
|
|
63
|
+
if (targetEnd > target.byteLength) targetEnd = target.byteLength
|
|
64
|
+
|
|
65
|
+
if (sourceStart < 0) sourceStart = 0
|
|
66
|
+
if (sourceStart > source.byteLength) sourceStart = source.byteLength
|
|
67
|
+
|
|
68
|
+
if (sourceEnd < sourceStart) sourceEnd = sourceStart
|
|
69
|
+
if (sourceEnd > source.byteLength) sourceEnd = source.byteLength
|
|
70
|
+
|
|
71
|
+
if (sourceStart !== 0 || sourceEnd !== source.byteLength) source = source.subarray(sourceStart, sourceEnd)
|
|
72
|
+
|
|
73
|
+
if (targetStart !== 0 || targetEnd !== target.byteLength) target = target.subarray(targetStart, targetEnd)
|
|
74
|
+
|
|
75
|
+
return binding.compare(source, target)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fill (value, offset = 0, end = this.byteLength, encoding = 'utf8') {
|
|
79
|
+
if (typeof value === 'string') {
|
|
80
|
+
// fill(string, encoding)
|
|
81
|
+
if (typeof offset === 'string') {
|
|
82
|
+
encoding = offset
|
|
83
|
+
offset = 0
|
|
84
|
+
end = this.byteLength
|
|
85
|
+
|
|
86
|
+
// fill(string, offset, encoding)
|
|
87
|
+
} else if (typeof end === 'string') {
|
|
88
|
+
encoding = end
|
|
89
|
+
end = this.byteLength
|
|
90
|
+
}
|
|
91
|
+
} else if (typeof value === 'number') {
|
|
92
|
+
value = value & 0xff
|
|
93
|
+
} else if (typeof value === 'boolean') {
|
|
94
|
+
value = +value
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (offset < 0) offset = 0
|
|
98
|
+
if (offset >= this.byteLength) return this
|
|
99
|
+
|
|
100
|
+
if (end <= offset) return this
|
|
101
|
+
if (end > this.byteLength) end = this.byteLength
|
|
102
|
+
|
|
103
|
+
if (typeof value === 'number') return super.fill(value, offset, end)
|
|
104
|
+
|
|
105
|
+
if (typeof value === 'string') value = exports.from(value, encoding)
|
|
106
|
+
|
|
107
|
+
const length = value.byteLength
|
|
108
|
+
|
|
109
|
+
for (let i = 0, n = end - offset; i < n; ++i) {
|
|
110
|
+
this[i + offset] = value[i % length]
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return this
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
includes (value, offset, encoding) {
|
|
117
|
+
return this.indexOf(value, offset, encoding) !== -1
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
indexOf (value, offset = 0, encoding) {
|
|
121
|
+
if (typeof value === 'number') {
|
|
122
|
+
return super.indexOf(value & 0xff, offset)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return bidirectionalIndexOf(this, value, offset, encoding, true /* first */)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
lastIndexOf (value, offset = this.byteLength - 1, encoding) {
|
|
129
|
+
if (typeof value === 'number') {
|
|
130
|
+
return super.lastIndexOf(value & 0xff, offset)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return bidirectionalIndexOf(this, value, offset, encoding, false /* last */)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
swap16 () {
|
|
137
|
+
const length = this.byteLength
|
|
138
|
+
|
|
139
|
+
if (length % 2 !== 0) throw new RangeError('Buffer size must be a multiple of 16-bits')
|
|
140
|
+
|
|
141
|
+
for (let i = 0; i < length; i += 2) swap(this, i, i + 1)
|
|
142
|
+
|
|
143
|
+
return this
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
swap32 () {
|
|
147
|
+
const length = this.byteLength
|
|
148
|
+
|
|
149
|
+
if (length % 4 !== 0) throw new RangeError('Buffer size must be a multiple of 32-bits')
|
|
150
|
+
|
|
151
|
+
for (let i = 0; i < length; i += 4) {
|
|
152
|
+
swap(this, i, i + 3)
|
|
153
|
+
swap(this, i + 1, i + 2)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return this
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
swap64 () {
|
|
160
|
+
const length = this.byteLength
|
|
161
|
+
|
|
162
|
+
if (length % 8 !== 0) throw new RangeError('Buffer size must be a multiple of 64-bits')
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < length; i += 8) {
|
|
165
|
+
swap(this, i, i + 7)
|
|
166
|
+
swap(this, i + 1, i + 6)
|
|
167
|
+
swap(this, i + 2, i + 5)
|
|
168
|
+
swap(this, i + 3, i + 4)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return this
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
toString (encoding, start = 0, end = this.byteLength) {
|
|
175
|
+
if (arguments.length === 0) return utf8.toString(this)
|
|
176
|
+
|
|
177
|
+
if (arguments.length === 1) return codecFor(encoding).toString(this)
|
|
178
|
+
|
|
179
|
+
if (start < 0) start = 0
|
|
180
|
+
if (start >= this.byteLength) return ''
|
|
181
|
+
|
|
182
|
+
if (end <= start) return ''
|
|
183
|
+
if (end > this.byteLength) end = this.byteLength
|
|
184
|
+
|
|
185
|
+
let buffer = this
|
|
186
|
+
|
|
187
|
+
if (start !== 0 || end !== this.byteLength) buffer = buffer.subarray(start, end)
|
|
188
|
+
|
|
189
|
+
return codecFor(encoding).toString(buffer)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
write (string, offset = 0, length = this.byteLength - offset, encoding = 'utf8') {
|
|
193
|
+
if (arguments.length === 1) return utf8.write(this, string)
|
|
194
|
+
|
|
195
|
+
// write(string, encoding)
|
|
196
|
+
if (typeof offset === 'string') {
|
|
197
|
+
encoding = offset
|
|
198
|
+
offset = 0
|
|
199
|
+
length = this.byteLength
|
|
200
|
+
|
|
201
|
+
// write(string, offset, encoding)
|
|
202
|
+
} else if (typeof length === 'string') {
|
|
203
|
+
encoding = length
|
|
204
|
+
length = this.byteLength - offset
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
length = Math.min(length, exports.byteLength(string, encoding))
|
|
208
|
+
|
|
209
|
+
let start = offset
|
|
210
|
+
if (start < 0) start = 0
|
|
211
|
+
if (start >= this.byteLength) return 0
|
|
212
|
+
|
|
213
|
+
let end = offset + length
|
|
214
|
+
if (end <= start) return 0
|
|
215
|
+
if (end > this.byteLength) end = this.byteLength
|
|
216
|
+
|
|
217
|
+
let buffer = this
|
|
218
|
+
|
|
219
|
+
if (start !== 0 || end !== this.byteLength) buffer = buffer.subarray(start, end)
|
|
220
|
+
|
|
221
|
+
return codecFor(encoding).write(buffer, string)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
writeDoubleLE (value, offset = 0) {
|
|
225
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
226
|
+
view.setFloat64(offset, value, true)
|
|
227
|
+
|
|
228
|
+
return offset + 8
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
writeFloatLE (value, offset = 0) {
|
|
232
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
233
|
+
view.setFloat32(offset, value, true)
|
|
234
|
+
|
|
235
|
+
return offset + 4
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
writeUInt32LE (value, offset = 0) {
|
|
239
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
240
|
+
view.setUint32(offset, value, true)
|
|
241
|
+
|
|
242
|
+
return offset + 4
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
writeInt32LE (value, offset = 0) {
|
|
246
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
247
|
+
view.setInt32(offset, value, true)
|
|
248
|
+
|
|
249
|
+
return offset + 4
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
readDoubleLE (offset = 0) {
|
|
253
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
254
|
+
|
|
255
|
+
return view.getFloat64(offset, true)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
readFloatLE (offset = 0) {
|
|
259
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
260
|
+
|
|
261
|
+
return view.getFloat32(offset, true)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
readUInt32LE (offset = 0) {
|
|
265
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
266
|
+
|
|
267
|
+
return view.getUint32(offset, true)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
readInt32LE (offset = 0) {
|
|
271
|
+
const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
|
|
272
|
+
|
|
273
|
+
return view.getInt32(offset, true)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const codecs = Object.create(null)
|
|
278
|
+
|
|
279
|
+
codecs.ascii = ascii
|
|
280
|
+
codecs.base64 = base64
|
|
281
|
+
codecs.hex = hex
|
|
282
|
+
codecs.utf8 = codecs['utf-8'] = utf8
|
|
283
|
+
codecs.utf16le = codecs.ucs2 = codecs['utf-16le'] = codecs['ucs-2'] = utf16le
|
|
284
|
+
|
|
285
|
+
function codecFor (encoding = 'utf8') {
|
|
286
|
+
if (encoding in codecs) return codecs[encoding]
|
|
287
|
+
|
|
288
|
+
encoding = encoding.toLowerCase()
|
|
289
|
+
|
|
290
|
+
if (encoding in codecs) return codecs[encoding]
|
|
291
|
+
|
|
292
|
+
throw new Error(`Unknown encoding: ${encoding}`)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
exports.isBuffer = function isBuffer (value) {
|
|
296
|
+
return value instanceof Buffer
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
exports.isEncoding = function isEncoding (encoding) {
|
|
300
|
+
try {
|
|
301
|
+
codecFor(encoding)
|
|
302
|
+
return true
|
|
303
|
+
} catch {
|
|
304
|
+
return false
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
exports.alloc = function alloc (size, fill, encoding) {
|
|
309
|
+
const buffer = new Buffer(size)
|
|
310
|
+
if (fill !== undefined) buffer.fill(fill, 0, buffer.byteLength, encoding)
|
|
311
|
+
return buffer
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
exports.allocUnsafe = function allocUnsafe (size) {
|
|
315
|
+
binding.setZeroFillEnabled(0)
|
|
316
|
+
try {
|
|
317
|
+
return new Buffer(size)
|
|
318
|
+
} finally {
|
|
319
|
+
binding.setZeroFillEnabled(1)
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
exports.allocUnsafeSlow = function allocUnsafeSlow (size) {
|
|
324
|
+
return exports.allocUnsafe(size)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
exports.byteLength = function byteLength (string, encoding) {
|
|
328
|
+
return codecFor(encoding).byteLength(string)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
exports.compare = function compare (a, b) {
|
|
332
|
+
return binding.compare(a, b)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
exports.concat = function concat (buffers, length) {
|
|
336
|
+
if (length === undefined) {
|
|
337
|
+
length = buffers.reduce((length, buffer) => length + buffer.byteLength, 0)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const result = new Buffer(length)
|
|
341
|
+
|
|
342
|
+
for (let i = 0, n = buffers.length, offset = 0; i < n; i++) {
|
|
343
|
+
const buffer = buffers[i]
|
|
344
|
+
|
|
345
|
+
if (offset + buffer.byteLength > result.byteLength) {
|
|
346
|
+
result.set(buffer.subarray(0, result.byteLength - offset), offset)
|
|
347
|
+
return result
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
result.set(buffer, offset)
|
|
351
|
+
|
|
352
|
+
offset += buffer.byteLength
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return result
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
exports.coerce = function coerce (buffer) {
|
|
359
|
+
if (exports.isBuffer(buffer)) return buffer
|
|
360
|
+
return new Buffer(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
exports.from = function from (value, encodingOrOffset, length) {
|
|
364
|
+
// from(string, encoding)
|
|
365
|
+
if (typeof value === 'string') return fromString(value, encodingOrOffset)
|
|
366
|
+
|
|
367
|
+
// from(array)
|
|
368
|
+
if (Array.isArray(value)) return fromArray(value)
|
|
369
|
+
|
|
370
|
+
// from(buffer)
|
|
371
|
+
if (ArrayBuffer.isView(value)) return fromBuffer(value)
|
|
372
|
+
|
|
373
|
+
// from(arrayBuffer[, offset[, length]])
|
|
374
|
+
return fromArrayBuffer(value, encodingOrOffset, length)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function fromString (string, encoding) {
|
|
378
|
+
const codec = codecFor(encoding)
|
|
379
|
+
const buffer = new Buffer(codec.byteLength(string))
|
|
380
|
+
codec.write(buffer, string)
|
|
381
|
+
return buffer
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function fromArray (array) {
|
|
385
|
+
const buffer = new Buffer(array.length)
|
|
386
|
+
buffer.set(array)
|
|
387
|
+
return buffer
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function fromBuffer (buffer) {
|
|
391
|
+
const copy = new Buffer(buffer.byteLength)
|
|
392
|
+
copy.set(buffer)
|
|
393
|
+
return copy
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function fromArrayBuffer (arrayBuffer, offset, length) {
|
|
397
|
+
return new Buffer(arrayBuffer, offset, length)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function bidirectionalIndexOf (buffer, value, offset, encoding, first) {
|
|
401
|
+
if (buffer.byteLength === 0) return -1
|
|
402
|
+
|
|
403
|
+
if (typeof offset === 'string') {
|
|
404
|
+
encoding = offset
|
|
405
|
+
offset = 0
|
|
406
|
+
} else if (offset === undefined) {
|
|
407
|
+
offset = first ? 0 : (buffer.byteLength - 1)
|
|
408
|
+
} else if (offset < 0) {
|
|
409
|
+
offset += buffer.byteLength
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (offset >= buffer.byteLength) {
|
|
413
|
+
if (first) return -1
|
|
414
|
+
else offset = buffer.byteLength - 1
|
|
415
|
+
} else if (offset < 0) {
|
|
416
|
+
if (first) offset = 0
|
|
417
|
+
else return -1
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (typeof value === 'string') value = exports.from(value, encoding)
|
|
421
|
+
|
|
422
|
+
if (value.byteLength === 0) return -1
|
|
423
|
+
|
|
424
|
+
if (first) {
|
|
425
|
+
let foundIndex = -1
|
|
426
|
+
|
|
427
|
+
for (let i = offset; i < buffer.byteLength; i++) {
|
|
428
|
+
if (buffer[i] === value[foundIndex === -1 ? 0 : i - foundIndex]) {
|
|
429
|
+
if (foundIndex === -1) foundIndex = i
|
|
430
|
+
if (i - foundIndex + 1 === value.byteLength) return foundIndex
|
|
431
|
+
} else {
|
|
432
|
+
if (foundIndex !== -1) i -= i - foundIndex
|
|
433
|
+
foundIndex = -1
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
} else {
|
|
437
|
+
if (offset + value.byteLength > buffer.byteLength) {
|
|
438
|
+
offset = buffer.byteLength - value.byteLength
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
for (let i = offset; i >= 0; i--) {
|
|
442
|
+
let found = true
|
|
443
|
+
|
|
444
|
+
for (let j = 0; j < value.byteLength; j++) {
|
|
445
|
+
if (buffer[i + j] !== value[j]) {
|
|
446
|
+
found = false
|
|
447
|
+
break
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (found) return i
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return -1
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function swap (buffer, n, m) {
|
|
459
|
+
const i = buffer[n]
|
|
460
|
+
buffer[n] = buffer[m]
|
|
461
|
+
buffer[m] = i
|
|
462
|
+
}
|
package/lib/ascii.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
exports.byteLength = function byteLength (string) {
|
|
2
|
+
return string.length
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
exports.toString = function toString (buffer) {
|
|
6
|
+
const len = buffer.byteLength
|
|
7
|
+
|
|
8
|
+
let result = ''
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < len; i++) {
|
|
11
|
+
result += String.fromCharCode(buffer[i])
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return result
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.write = function write (buffer, string) {
|
|
18
|
+
const len = buffer.byteLength
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < len; i++) {
|
|
21
|
+
buffer[i] = string.charCodeAt(i)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return len
|
|
25
|
+
}
|
package/lib/base64.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const binding = require('../binding')
|
|
2
|
+
|
|
3
|
+
exports.byteLength = function byteLength (string) {
|
|
4
|
+
let len = string.length
|
|
5
|
+
|
|
6
|
+
if (string.charCodeAt(len - 1) === 0x3d) len--
|
|
7
|
+
if (len > 1 && string.charCodeAt(len - 1) === 0x3d) len--
|
|
8
|
+
|
|
9
|
+
return (len * 3) >>> 2
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
exports.toString = function toString (buffer) {
|
|
13
|
+
return binding.toStringBase64(buffer)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.write = function write (buffer, string) {
|
|
17
|
+
return binding.writeBase64(buffer, string)
|
|
18
|
+
}
|
package/lib/hex.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const binding = require('../binding')
|
|
2
|
+
|
|
3
|
+
exports.byteLength = function byteLength (string) {
|
|
4
|
+
return string.length >>> 1
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
exports.toString = function toString (buffer) {
|
|
8
|
+
return binding.toStringHex(buffer)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
exports.write = function write (buffer, string) {
|
|
12
|
+
return binding.writeHex(buffer, string)
|
|
13
|
+
}
|
package/lib/utf16le.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
exports.byteLength = function byteLength (string) {
|
|
2
|
+
return string.length * 2
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
exports.toString = function toString (buffer) {
|
|
6
|
+
const len = buffer.byteLength
|
|
7
|
+
|
|
8
|
+
let result = ''
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < len - 1; i += 2) {
|
|
11
|
+
result += String.fromCharCode(buffer[i] + (buffer[i + 1] * 256))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return result
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.write = function write (buffer, string) {
|
|
18
|
+
const len = buffer.byteLength
|
|
19
|
+
|
|
20
|
+
let units = len
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < string.length; ++i) {
|
|
23
|
+
if ((units -= 2) < 0) break
|
|
24
|
+
|
|
25
|
+
const c = string.charCodeAt(i)
|
|
26
|
+
const hi = c >> 8
|
|
27
|
+
const lo = c % 256
|
|
28
|
+
|
|
29
|
+
buffer[i * 2] = lo
|
|
30
|
+
buffer[i * 2 + 1] = hi
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return len
|
|
34
|
+
}
|
package/lib/utf8.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const binding = require('../binding')
|
|
2
|
+
|
|
3
|
+
exports.byteLength = function byteLength (string) {
|
|
4
|
+
return binding.byteLengthUTF8(string)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
exports.toString = function toString (buffer) {
|
|
8
|
+
return binding.toStringUTF8(buffer)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
exports.write = function write (buffer, string) {
|
|
12
|
+
return binding.writeUTF8(buffer, string)
|
|
13
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bare-buffer",
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"description": "Native buffers for JavaScript",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"binding.c",
|
|
9
|
+
"binding.js",
|
|
10
|
+
"CMakeLists.txt",
|
|
11
|
+
"lib",
|
|
12
|
+
"prebuilds",
|
|
13
|
+
"vendor/libbase64/include",
|
|
14
|
+
"vendor/libbase64/src",
|
|
15
|
+
"vendor/libhex/include",
|
|
16
|
+
"vendor/libhex/src"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "standard && bare test/all.js",
|
|
20
|
+
"install": "bare-dev vendor sync && bare-dev rebuild",
|
|
21
|
+
"prebuild": "bare-dev prebuild"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/holepunchto/bare-buffer.git"
|
|
26
|
+
},
|
|
27
|
+
"author": "Holepunch",
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/holepunchto/bare-buffer/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/holepunchto/bare-buffer#readme",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"brittle": "^3.1.1",
|
|
35
|
+
"standard": "^17.0.0"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.22)
|
|
2
|
+
|
|
3
|
+
project(base64 C)
|
|
4
|
+
|
|
5
|
+
add_library(base64 OBJECT)
|
|
6
|
+
|
|
7
|
+
set_target_properties(
|
|
8
|
+
base64
|
|
9
|
+
PROPERTIES
|
|
10
|
+
C_STANDARD 99
|
|
11
|
+
POSITION_INDEPENDENT_CODE ON
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
target_sources(
|
|
15
|
+
base64
|
|
16
|
+
INTERFACE
|
|
17
|
+
include/base64.h
|
|
18
|
+
PRIVATE
|
|
19
|
+
src/base64.c
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
target_include_directories(
|
|
23
|
+
base64
|
|
24
|
+
PUBLIC
|
|
25
|
+
include
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
add_library(base64_shared SHARED $<TARGET_OBJECTS:base64>)
|
|
29
|
+
|
|
30
|
+
set_target_properties(
|
|
31
|
+
base64_shared
|
|
32
|
+
PROPERTIES
|
|
33
|
+
OUTPUT_NAME base64
|
|
34
|
+
WINDOWS_EXPORT_ALL_SYMBOLS ON
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
add_library(base64_static STATIC $<TARGET_OBJECTS:base64>)
|
|
38
|
+
|
|
39
|
+
set_target_properties(
|
|
40
|
+
base64_static
|
|
41
|
+
PROPERTIES
|
|
42
|
+
OUTPUT_NAME base64
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
install(TARGETS base64_shared base64_static)
|
|
46
|
+
|
|
47
|
+
install(FILES include/base64.h DESTINATION include)
|
|
48
|
+
|
|
49
|
+
if(PROJECT_IS_TOP_LEVEL)
|
|
50
|
+
enable_testing()
|
|
51
|
+
add_subdirectory(test)
|
|
52
|
+
endif()
|