Skip to content

Commit

Permalink
Fix endianness of IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Oct 26, 2021
1 parent 32dc030 commit e6b5682
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ const ipv6 = {
decode (state) {
if (state.end - state.start < 16) throw new Error('Out of bounds')
return (
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16) + ':' +
c.uint16.decode(state).toString(16)
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16) + ':' +
(state.buffer[state.start++] * 256 + state.buffer[state.start++]).toString(16)
)
}
}
Expand Down Expand Up @@ -106,8 +106,8 @@ function encodeIPv6 (state, string) {
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10)
}

state.buffer[state.start++] = n
state.buffer[state.start++] = n >>> 8
state.buffer[state.start++] = n

if (i < string.length && string.charCodeAt(i) === /* : */ 0x3a) {
i++
Expand Down
12 changes: 6 additions & 6 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ test('ipv4', (t) => {

test('ipv6', (t) => {
const ip = '1:2:3:4:5:6:7:8'
const buf = Buffer.from([1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0])
const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8])

t.alike(c.encode(ipv6, ip), buf)
t.alike(c.decode(ipv6, buf), ip)

t.test('abbreviated', (t) => {
const buf = Buffer.from([1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 8, 0])
const buf = Buffer.from([0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 8])

t.alike(c.encode(ipv6, '1:2::7:8'), buf)
t.alike(c.decode(ipv6, buf), '1:2:0:0:0:0:7:8')
})

t.test('prefix abbreviated', (t) => {
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8, 0])
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8])

t.alike(c.encode(ipv6, '::5:6:7:8'), buf)
t.alike(c.decode(ipv6, buf), '0:0:0:0:5:6:7:8')
})

t.test('suffix abbreviated', (t) => {
const buf = Buffer.from([1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0])
const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0])

t.alike(c.encode(ipv6, '1:2:3:4::'), buf)
t.alike(c.decode(ipv6, buf), '1:2:3:4:0:0:0:0')
Expand All @@ -55,14 +55,14 @@ test('ipv6', (t) => {
})

t.test('lowercase hex', (t) => {
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xcd, 0xab])
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd])

t.alike(c.encode(ipv6, '::abcd'), buf)
t.alike(c.decode(ipv6, buf), '0:0:0:0:0:0:0:abcd')
})

t.test('uppercase hex', (t) => {
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xcd, 0xab])
const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd])

t.alike(c.encode(ipv6, '::ABCD'), buf)
t.alike(c.decode(ipv6, buf), '0:0:0:0:0:0:0:abcd')
Expand Down

0 comments on commit e6b5682

Please sign in to comment.