Skip to content

Commit

Permalink
fix(decoder): handle 32bit byte strings and utf8 strings
Browse files Browse the repository at this point in the history
Fixes #19
  • Loading branch information
dignifiedquire committed Oct 26, 2017
1 parent 562f14b commit 7c5707c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
56 changes: 53 additions & 3 deletions src/decoder.asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ module.exports = function decodeAsm (stdlib, foreign, buffer) {
) | 0
}

function readUInt32 (n) {
n = n | 0

return (
(heap[n | 0] << 24) | (heap[(n + 1) | 0] << 16) | (heap[(n + 2) | 0] << 8) | heap[(n + 3) | 0]
) | 0
}

// -- Initial Byte Handlers

function INT_P (octet) {
Expand Down Expand Up @@ -266,7 +274,6 @@ module.exports = function decodeAsm (stdlib, foreign, buffer) {
var step = 0

step = (octet - 64) | 0

if (checkOffset(step | 0) | 0) {
return 1
}
Expand Down Expand Up @@ -337,10 +344,32 @@ module.exports = function decodeAsm (stdlib, foreign, buffer) {
function BYTE_STRING_32 (octet) {
octet = octet | 0

return 1
var start = 0
var end = 0
var length = 0

if (checkOffset(4) | 0) {
return 1
}

length = readUInt32((offset + 1) | 0) | 0
start = (offset + 5) | 0
end = (((offset + 5) | 0) + (length | 0)) | 0


if (checkOffset((length + 4) | 0) | 0) {
return 1
}

pushByteString(start | 0, end | 0)

offset = end | 0

return 0
}

function BYTE_STRING_64 (octet) {
// NOT IMPLEMENTED
octet = octet | 0

return 1
Expand Down Expand Up @@ -434,10 +463,31 @@ module.exports = function decodeAsm (stdlib, foreign, buffer) {
function UTF8_STRING_32 (octet) {
octet = octet | 0

return 1
var start = 0
var end = 0
var length = 0

if (checkOffset(4) | 0) {
return 1
}

length = readUInt32((offset + 1) | 0) | 0
start = (offset + 5) | 0
end = (((offset + 5) | 0) + (length | 0)) | 0

if (checkOffset((length + 4) | 0) | 0) {
return 1
}

pushUtf8String(start | 0, end | 0)

offset = end | 0

return 0
}

function UTF8_STRING_64 (octet) {
// NOT IMPLEMENTED
octet = octet | 0

return 1
Expand Down
24 changes: 24 additions & 0 deletions test/decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ describe('Decoder', function () {
).to.throw()
})
})

describe('large', () => {
const sizes = [
1024,
1024 * 63,
1024 * 64,
1024 * 128
]

sizes.forEach((size) => it(`decodes buffer of size ${size} bytes`, () => {
const input = Buffer.alloc(size)
input.fill('A')

expect(
cbor.decode(cbor.encode(input))
).to.be.eql(
input
)

const strRes = cbor.decode(cbor.encode(input.toString('utf8')))
expect(strRes.length).to.be.eql(size)
expect(strRes[0]).to.be.eql('A')
}))
})
})

function testGood (input, expected, desc) {
Expand Down

0 comments on commit 7c5707c

Please sign in to comment.