Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ProtoDef skipChecks flag to allow full NBT array size support #91

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ Minecraft Java Edition uses big-endian format, and Bedrock uses little-endian.

Returns a buffer with a serialized nbt `value`.

### parseUncompressed(data, format='big')
### parseUncompressed(data, format='big', options?= {noArraySizeCheck?: boolean})

Takes a buffer `data` and returns a parsed nbt value.

The `options` parameter is optional. When `noArraySizeCheck` is `true`, an array size check is disabled which allows for parsing of large arrays.

### parseAs(data, type, options?= {noArraySizeCheck?: boolean})

Takes a buffer `data` and returns a parsed nbt value. If the buffer is gzipped, it will unzip the data first.

The `options` parameter is optional. When `noArraySizeCheck` is `true`, an array size check is disabled which allows for parsing of large arrays.


### simplify(nbt)

Expand Down
11 changes: 9 additions & 2 deletions nbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ function writeUncompressed (value, proto = 'big') {
return protos[proto].createPacketBuffer('nbt', value)
}

function parseUncompressed (data, proto = 'big') {
function parseUncompressed (data, proto = 'big', options = {}) {
if (proto === true) proto = 'little'

protos[proto].setVariable('noArraySizeCheck', options.noArraySizeCheck)

return protos[proto].parsePacketBuffer('nbt', data, data.startOffset).data
}

Expand All @@ -70,7 +73,7 @@ const hasGzipHeader = function (data) {
const hasBedrockLevelHeader = (data) =>
data[1] === 0 && data[2] === 0 && data[3] === 0

async function parseAs (data, type) {
async function parseAs (data, type, options = {}) {
if (hasGzipHeader(data)) {
data = await new Promise((resolve, reject) => {
zlib.gunzip(data, (error, uncompressed) => {
Expand All @@ -79,7 +82,11 @@ async function parseAs (data, type) {
})
})
}

protos[type].setVariable('noArraySizeCheck', options.noArraySizeCheck)

const parsed = protos[type].parsePacketBuffer('nbt', data, data.startOffset)

parsed.metadata.buffer = data
parsed.type = type
return parsed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"fix": "standard --fix"
},
"dependencies": {
"protodef": "^1.9.0"
"protodef": "bdkopen/node-protodef#add-skip-array-checks-flag"
},
"license": "MIT"
}
7 changes: 6 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ declare module 'prismarine-nbt'{
[TagType.LongArray]: LongArray;
}

interface ParseOptions {
noArraySizeCheck?: boolean;
}

export type NBTFormat = 'big' | 'little' | 'littleVarint'

export type NBT = Tags['compound'] & {name: string};
Expand All @@ -74,7 +78,8 @@ declare module 'prismarine-nbt'{
size: number
}
export function writeUncompressed(value: NBT, format?: NBTFormat): Buffer;
export function parseUncompressed(value: Buffer, format?: NBTFormat): NBT;
export function parseUncompressed(value: Buffer, format?: NBTFormat, options?: ParseOptions): NBT;
export function parseAs(value: Buffer, type: NBTFormat, options?: ParseOptions): Promise<{parsed: NBT, type: NBTFormat, metadata: Metadata}>;

export function parse(data: Buffer, nbtType?: NBTFormat): Promise<{parsed: NBT, type: NBTFormat, metadata: Metadata}>;
export function simplify(data: Tags[TagType]): any
Expand Down
Loading