Bye buffer

This commit is contained in:
LittleChest 2026-04-07 23:01:07 +08:00
parent 5c37d07fcf
commit d95b456ddf
4 changed files with 539 additions and 313 deletions

193
buffer.js Normal file
View File

@ -0,0 +1,193 @@
'use strict'
// Allocate a new Uint8Array buffer
function allocBuffer (size) {
return new Uint8Array(size)
}
// Allocate an uninitialized Uint8Array (same as allocBuffer for Uint8Array)
function allocUnsafeBuffer (size) {
return new Uint8Array(size)
}
// Check if an object is a Uint8Array
function isBuffer (obj) {
return obj instanceof Uint8Array
}
// Create Uint8Array from data with optional encoding
function fromBuffer (data, encoding) {
if (data instanceof Uint8Array) {
return new Uint8Array(data)
}
if (Array.isArray(data)) {
return new Uint8Array(data)
}
if (typeof data === 'string') {
if (encoding === 'base64') {
return base64ToBuffer(data)
} else if (encoding === 'hex') {
return hexToBuffer(data)
} else {
return stringToBuffer(data)
}
}
return new Uint8Array(data)
}
// Convert string to Uint8Array (UTF-8 encoding)
function stringToBuffer (str) {
const encoder = new TextEncoder()
return encoder.encode(str)
}
// Convert Base64 string to Uint8Array
function base64ToBuffer (base64Str) {
if (typeof window !== 'undefined' && window.atob) {
// Browser environment
const binaryString = window.atob(base64Str)
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
return bytes
} else {
// Node.js environment
return new Uint8Array(Buffer.from(base64Str, 'base64'))
}
}
// Convert hex string to Uint8Array
function hexToBuffer (hexStr) {
const bytes = new Uint8Array(hexStr.length / 2)
for (let i = 0; i < hexStr.length; i += 2) {
bytes[i / 2] = parseInt(hexStr.substr(i, 2), 16)
}
return bytes
}
// Convert Uint8Array to string
function bufferToString (buf, encoding, start, end) {
encoding = encoding || 'utf-8'
start = start || 0
end = end || buf.length
if (encoding !== 'utf-8' && encoding !== 'utf8') {
throw new Error('Unsupported encoding: ' + encoding)
}
const slice = buf.slice(start, end)
const decoder = new TextDecoder('utf-8')
return decoder.decode(slice)
}
// Write string to buffer, return number of bytes written
function writeString (buf, str, offset) {
offset = offset || 0
const encoded = stringToBuffer(str)
for (let i = 0; i < encoded.length && offset + i < buf.length; i++) {
buf[offset + i] = encoded[i]
}
return encoded.length
}
// Get byte length of a string
function byteLength (str) {
const encoder = new TextEncoder()
return encoder.encode(str).length
}
// Read big-endian 16-bit unsigned integer
function readUInt16BE (buf, offset) {
offset = offset || 0
return (buf[offset] << 8) | buf[offset + 1]
}
// Write big-endian 16-bit unsigned integer
function writeUInt16BE (buf, value, offset) {
offset = offset || 0
buf[offset] = (value >> 8) & 0xff
buf[offset + 1] = value & 0xff
}
// Read big-endian 32-bit unsigned integer
function readUInt32BE (buf, offset) {
offset = offset || 0
return ((buf[offset] << 24) | (buf[offset + 1] << 16) | (buf[offset + 2] << 8) | buf[offset + 3]) >>> 0
}
// Write big-endian 32-bit unsigned integer
function writeUInt32BE (buf, value, offset) {
offset = offset || 0
buf[offset] = (value >> 24) & 0xff
buf[offset + 1] = (value >> 16) & 0xff
buf[offset + 2] = (value >> 8) & 0xff
buf[offset + 3] = value & 0xff
}
// Read 8-bit unsigned integer
function readUInt8 (buf, offset) {
offset = offset || 0
return buf[offset]
}
// Write 8-bit unsigned integer
function writeUInt8 (buf, value, offset) {
offset = offset || 0
buf[offset] = value & 0xff
}
// Copy buffer contents
function bufferCopy (source, target, targetStart, sourceStart, sourceEnd) {
targetStart = targetStart || 0
sourceStart = sourceStart || 0
sourceEnd = sourceEnd !== undefined ? sourceEnd : source.length
for (let i = sourceStart; i < sourceEnd; i++) {
target[targetStart + (i - sourceStart)] = source[i]
}
}
// Concatenate multiple buffers
function bufferConcat (buffers, totalLength) {
if (!Array.isArray(buffers)) {
throw new Error('buffers must be an Array')
}
if (buffers.length === 0) {
return new Uint8Array(0)
}
totalLength = totalLength || buffers.reduce((sum, buf) => sum + buf.length, 0)
const result = new Uint8Array(totalLength)
let offset = 0
for (let i = 0; i < buffers.length; i++) {
const buf = buffers[i]
result.set(buf, offset)
offset += buf.length
}
return result
}
module.exports = {
allocBuffer,
allocUnsafeBuffer,
isBuffer,
fromBuffer,
stringToBuffer,
base64ToBuffer,
hexToBuffer,
bufferToString,
writeString,
byteLength,
readUInt8,
writeUInt8,
readUInt16BE,
writeUInt16BE,
readUInt32BE,
writeUInt32BE,
bufferCopy,
bufferConcat
}

View File

@ -1,7 +1,16 @@
'use strict'
const wout = (s) => {
if (typeof process !== 'undefined' && process.stdout) {
process.stdout.write(s)
} else if (typeof console !== 'undefined') {
if (s === '\n') {
console.log('')
} else {
process.stdout = process.stdout || ''
process.stdout += s
}
}
}
const padStr = (s, len, ch) => {
@ -55,7 +64,7 @@ const hexdump = (buf, len) => {
}
/*
let buf = Buffer.alloc(500)
let buf = new Uint8Array(500)
for (let i = 0; i < buf.length; i++) {
buf[i] = i & 0xff
}

561
index.js

File diff suppressed because it is too large Load Diff

85
test.js
View File

@ -11,7 +11,7 @@ const ip = require('@leichtgewicht/ip-codec')
const hexdump = require('./hexdump')
tape('unknown', function (t) {
testEncoder(t, packet.unknown, Buffer.from('hello world'))
testEncoder(t, packet.unknown, new Uint8Array('hello world'))
t.end()
})
@ -19,9 +19,9 @@ tape('txt', function (t) {
testEncoder(t, packet.txt, [])
testEncoder(t, packet.txt, ['hello world'])
testEncoder(t, packet.txt, ['hello', 'world'])
testEncoder(t, packet.txt, [Buffer.from([0, 1, 2, 3, 4, 5])])
testEncoder(t, packet.txt, ['a', 'b', Buffer.from([0, 1, 2, 3, 4, 5])])
testEncoder(t, packet.txt, ['', Buffer.allocUnsafe(0)])
testEncoder(t, packet.txt, [new Uint8Array([0, 1, 2, 3, 4, 5])])
testEncoder(t, packet.txt, ['a', 'b', new Uint8Array([0, 1, 2, 3, 4, 5])])
testEncoder(t, packet.txt, ['', new Uint8Array(0)])
t.end()
})
@ -29,16 +29,17 @@ tape('txt-scalar-string', function (t) {
const buf = packet.txt.encode('hi')
const val = packet.txt.decode(buf)
t.ok(val.length === 1, 'array length')
t.ok(val[0].toString() === 'hi', 'data')
const decoded = val[0] instanceof Uint8Array ? new TextDecoder().decode(val[0]) : val[0].toString()
t.ok(decoded === 'hi', 'data')
t.end()
})
tape('txt-scalar-buffer', function (t) {
const data = Buffer.from([0, 1, 2, 3, 4, 5])
const data = new Uint8Array([0, 1, 2, 3, 4, 5])
const buf = packet.txt.encode(data)
const val = packet.txt.decode(buf)
t.ok(val.length === 1, 'array length')
t.ok(val[0].equals(data), 'data')
t.ok(compare(t, val[0], data), 'data')
t.end()
})
@ -50,7 +51,7 @@ tape('txt-invalid-data', function (t) {
})
tape('null', function (t) {
testEncoder(t, packet.null, Buffer.from([0, 1, 2, 3, 4, 5]))
testEncoder(t, packet.null, new Uint8Array([0, 1, 2, 3, 4, 5]))
t.end()
})
@ -259,7 +260,7 @@ tape('response', function (t) {
answers: [{
type: 'NULL',
name: 'hello.null.com',
data: Buffer.from([1, 2, 3, 4, 5])
data: new Uint8Array([1, 2, 3, 4, 5])
}]
})
@ -315,7 +316,7 @@ tape('rcode', function (t) {
tape('name_encoding', function (t) {
let data = 'foo.example.com'
const buf = Buffer.allocUnsafe(255)
const buf = new Uint8Array(255)
let offset = 0
packet.name.encode(data, buf, offset)
t.ok(packet.name.encode.bytes === 17, 'name encoding length matches')
@ -384,40 +385,40 @@ tape('name_encoding', function (t) {
tape('name_decoding', function (t) {
// The two most significant bits of a valid label header must be either both zero or both one
t.throws(function () { packet.name.decode(Buffer.from([0x80])) }, /Cannot decode name \(bad label\)$/)
t.throws(function () { packet.name.decode(Buffer.from([0xb0])) }, /Cannot decode name \(bad label\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0x80])) }, /Cannot decode name \(bad label\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0xb0])) }, /Cannot decode name \(bad label\)$/)
// Ensure there's enough buffer to read
t.throws(function () { packet.name.decode(Buffer.from([])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(Buffer.from([0x01, 0x00])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(Buffer.from([0x01])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(Buffer.from([0xc0])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0x01, 0x00])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0x01])) }, /Cannot decode name \(buffer overflow\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0xc0])) }, /Cannot decode name \(buffer overflow\)$/)
// Allow only pointers backwards
t.throws(function () { packet.name.decode(Buffer.from([0xc0, 0x00])) }, /Cannot decode name \(bad pointer\)$/)
t.throws(function () { packet.name.decode(Buffer.from([0xc0, 0x01])) }, /Cannot decode name \(bad pointer\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0xc0, 0x00])) }, /Cannot decode name \(bad pointer\)$/)
t.throws(function () { packet.name.decode(new Uint8Array([0xc0, 0x01])) }, /Cannot decode name \(bad pointer\)$/)
// A name can be only 253 characters (when connected with dots)
const maxLength = Buffer.alloc(255)
maxLength.fill(Buffer.from([0x01, 0x61]), 0, 254)
const maxLength = new Uint8Array(255)
maxLength.fill(new Uint8Array([0x01, 0x61]), 0, 254)
t.ok(packet.name.decode(maxLength) === new Array(127).fill('a').join('.'))
const tooLong = Buffer.alloc(256)
tooLong.fill(Buffer.from([0x01, 0x61]))
const tooLong = new Uint8Array(256)
tooLong.fill(new Uint8Array([0x01, 0x61]))
t.throws(function () { packet.name.decode(tooLong) }, /Cannot decode name \(name too long\)$/)
// Ensure jumps don't reset the total length counter
const tooLongWithJump = Buffer.alloc(403)
tooLongWithJump.fill(Buffer.from([0x01, 0x61]), 0, 200)
tooLongWithJump.fill(Buffer.from([0x01, 0x61]), 201, 401)
const tooLongWithJump = new Uint8Array(403)
tooLongWithJump.fill(new Uint8Array([0x01, 0x61]), 0, 200)
tooLongWithJump.fill(new Uint8Array([0x01, 0x61]), 201, 401)
tooLongWithJump.set([0xc0, 0x00], 401)
t.throws(function () { packet.name.decode(tooLongWithJump, 201) }, /Cannot decode name \(name too long\)$/)
// Ensure a jump to a null byte doesn't add extra dots
t.ok(packet.name.decode(Buffer.from([0x00, 0x01, 0x61, 0xc0, 0x00]), 1) === 'a')
t.ok(packet.name.decode(new Uint8Array([0x00, 0x01, 0x61, 0xc0, 0x00]), 1) === 'a')
// Ensure deeply nested pointers don't cause "Maximum call stack size exceeded" errors
const buf = Buffer.alloc(16386)
const buf = new Uint8Array(16386)
for (let i = 0; i < 16384; i += 2) {
buf.writeUInt16BE(0xc000 | i, i + 2)
}
@ -519,7 +520,7 @@ tape('dnskey', function (t) {
testEncoder(t, packet.dnskey, {
flags: packet.dnskey.SECURE_ENTRYPOINT | packet.dnskey.ZONE_KEY,
algorithm: 1,
key: Buffer.from([0, 1, 2, 3, 4, 5])
key: new Uint8Array([0, 1, 2, 3, 4, 5])
})
t.end()
})
@ -534,12 +535,12 @@ tape('rrsig', function (t) {
inception: 1233,
keyTag: 2345,
signersName: 'foo.com',
signature: Buffer.from([0, 1, 2, 3, 4, 5])
signature: new Uint8Array([0, 1, 2, 3, 4, 5])
}
testEncoder(t, packet.rrsig, testRRSIG)
// Check the signature length is correct with extra junk at the end
const buf = Buffer.allocUnsafe(packet.rrsig.encodingLength(testRRSIG) + 4)
const buf = new Uint8Array(packet.rrsig.encodingLength(testRRSIG) + 4)
packet.rrsig.encode(testRRSIG, buf)
const val2 = packet.rrsig.decode(buf)
t.ok(compare(t, testRRSIG, val2))
@ -589,7 +590,7 @@ tape('nsec', function (t) {
})
// Test with the sample NSEC from https://tools.ietf.org/html/rfc4034#section-4.3
var sampleNSEC = Buffer.from('003704686f7374076578616d706c6503636f6d00' +
var sampleNSEC = new Uint8Array('003704686f7374076578616d706c6503636f6d00' +
'0006400100000003041b000000000000000000000000000000000000000000000' +
'000000020', 'hex')
var decoded = packet.nsec.decode(sampleNSEC)
@ -608,8 +609,8 @@ tape('nsec3', function (t) {
algorithm: 1,
flags: 0,
iterations: 257,
salt: Buffer.from([42, 42, 42]),
nextDomain: Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
salt: new Uint8Array([42, 42, 42]),
nextDomain: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
rrtypes: ['A', 'DNSKEY', 'CAA', 'DLV']
})
t.end()
@ -620,7 +621,7 @@ tape('ds', function (t) {
keyTag: 1234,
algorithm: 1,
digestType: 1,
digest: Buffer.from([0, 1, 2, 3, 4, 5])
digest: new Uint8Array([0, 1, 2, 3, 4, 5])
})
t.end()
})
@ -642,13 +643,13 @@ tape('tlsa', function (t) {
usage: 3,
selector: 1,
matchingType: 1,
certificate: Buffer.from([0, 1, 2, 3, 4, 5])
certificate: new Uint8Array([0, 1, 2, 3, 4, 5])
})
t.end()
})
const unhexlify = (hex) => {
return Buffer.from(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) }))
return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) }))
}
// <HTTPS SVCB test cases>
@ -1176,7 +1177,7 @@ tape('google resolver SVCB real world', function (t) {
// </HTTPS SVCB test cases>
tape('unpack', function (t) {
const buf = Buffer.from([
const buf = new Uint8Array([
0x00, 0x79,
0xde, 0xad, 0x85, 0x00, 0x00, 0x01, 0x00, 0x01,
0x00, 0x02, 0x00, 0x02, 0x02, 0x6f, 0x6a, 0x05,
@ -1256,7 +1257,7 @@ function testEncoder (t, rpacket, val) {
t.ok(compare(t, val, val3), 'decoded object match on re-encode')
t.ok(compare(t, val2, val3), 're-encoded decoded object match on re-encode')
const bigger = Buffer.allocUnsafe(buf2.length + 10)
const bigger = new Uint8Array(buf2.length + 10)
const buf3 = rpacket.encode(val, bigger, 10)
const val4 = rpacket.decode(buf3, 10)
@ -1267,7 +1268,13 @@ function testEncoder (t, rpacket, val) {
}
function compare (t, a, b) {
if (Buffer.isBuffer(a)) return a.toString('hex') === b.toString('hex')
if (a instanceof Uint8Array && b instanceof Uint8Array) {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false
}
return true
}
if (typeof a === 'object' && a && b) {
const keys = Object.keys(a)
for (let i = 0; i < keys.length; i++) {