Skip to content

Commit

Permalink
Properly escape low char bytes, kriszyp/lmdb-js#190
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Sep 25, 2022
1 parent 561ab7c commit 51a494b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function writeKey(key, target, position, inSequence) {
let i, c2
for (i = 0; i < strLength; i++) {
c1 = key.charCodeAt(i)
if (c1 < 0x80) {
if (c1 <= 4) {
target[position++] = 4
target[position++] = c1
} else if (c1 < 0x80) {
target[position++] = c1
} else if (c1 < 0x800) {
target[position++] = c1 >> 6 | 0xc0
Expand Down Expand Up @@ -250,10 +253,14 @@ function makeStringBuilder() {
let v = fromCharCode((i & 0xf) + 97) + fromCharCode((i >> 4) + 97)
stringBuildCode += `
let ${v} = source[position++]
if (!${v})
return fromCharCode(${previous})
else if (${v} >= 0x80)
${v} = finishUtf8(${v}, source)
if (${v} > 4) {
if (${v} >= 0x80) ${v} = finishUtf8(${v}, source)
} else {
if (${v} === 4)
${v} = source[position++]
else
return fromCharCode(${previous})
}
`
previous.push(v)
if (i == 1000000) // this just exists to prevent rollup from doing dead code elimination on finishUtf8
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ordered-binary",
"author": "Kris Zyp",
"version": "1.3.0",
"version": "1.4.0",
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
"license": "MIT",
"repository": {
Expand Down
6 changes: 4 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ suite('key buffers', () => {
assert.strictEqual(fromBufferKey(toBufferKey('4')), '4')
assert.strictEqual(fromBufferKey(toBufferKey('hello')), 'hello')
assert.strictEqual(fromBufferKey(toBufferKey('')), '')
assert.strictEqual(fromBufferKey(toBufferKey('\x00')), '\x00')
assert.strictEqual(fromBufferKey(toBufferKey('\x03test\x01\x00')), '\x03test\x01\x00')
})
test('string comparison', () => {
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
Expand All @@ -64,8 +66,8 @@ suite('key buffers', () => {
['hello', 5.25])
assert.deepEqual(fromBufferKey(toBufferKey([true, 1503579323825])),
[true, 1503579323825])
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'second'])),
[-0.2525, 'second'])
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, 'sec\x00nd'])),
[-0.2525, 'sec\x00nd'])
assert.deepEqual(fromBufferKey(toBufferKey([-0.2525, '2nd', '3rd'])),
[-0.2525, '2nd', '3rd'])
})
Expand Down

0 comments on commit 51a494b

Please sign in to comment.