Skip to content

Commit

Permalink
encode string bytelength instead of strlength
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 24, 2024
1 parent 935a07c commit 95dc61c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/core/src/submodules/cbor/cbor-encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ export function encode(input: any): void {
} else if (typeof input === "object") {
const keys = Object.keys(input);
encodeHeader(majorMap, keys.length);
for (let i = 0; i < keys.length; i++) {
encodeString(keys[i]);
encode(input[keys[i]]);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
encodeString(key);
encode(input[key]);
}
return;
}
Expand All @@ -188,10 +189,14 @@ export function encode(input: any): void {
}

function encodeString(input: string) {
ensureSpace(input.length * 8);
encodeHeader(majorUtf8String, input.length);
if (USE_BUFFER && (data as BufferWithUtf8Write).utf8Write) {
cursor += (data as BufferWithUtf8Write).utf8Write(input, cursor);
ensureSpace(input.length * 4);
encodeHeader(majorUtf8String, Buffer.byteLength(input));
if (USE_BUFFER) {
if ((data as BufferWithUtf8Write).utf8Write) {
cursor += (data as BufferWithUtf8Write).write(input, cursor);
} else {
cursor += (data as Buffer).write(input, cursor);
}
} else if (input.length < 200) {
for (let i = 0; i < input.length; ++i) {
const c = input.charCodeAt(i);
Expand Down

0 comments on commit 95dc61c

Please sign in to comment.