Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const Parser = require('binary-parser').Parser;
var stop = new Parser();
// version
var versionParser = new Parser()
.skip(4).int32('major')
.skip(4).int32('minor')
.skip(4).int32('patch');
// package info
var packageInfoParser = new Parser()
.skip(4).int32('userLen')
.string('user', { length: 'userLen' })
.skip(4).int32('nameLen')
.string('name', { length: 'nameLen' });
};
// imports
var importPathItemParser = new Parser()
.skip(4).int32('itemLen')
.string('item', { length: 'itemLen' });
var singleImportParser = new Parser()
.skip(4).int32('count')
.array('path', {
type: importPathItemParser,
length: 'count'
});
var importsParser = new Parser()
.skip(4).int32('count')
.array('values', {
type: singleImportParser,
length: 'count'
});
var importsFormatter = function(v) {
return v.values.map(function(iv) {
return {
path: iv.path.map(function(pv) {
return pv.item;
})
}
});
};
/*
Parses actual game data
Please note that TimeSlotBlocks do not fully parse included actions.
They should be parsed block by block manually
afterwards to ensure proper error handling.
*/
import { Parser } from 'binary-parser'
import { CommandDataBlock } from './actions'
import { chatModeFormatter } from './formatters'
// 0x17
const LeaveGameBlock = new Parser()
.string('reason', { length: 4, encoding: 'hex' })
.int8('playerId')
.string('result', { length: 4, encoding: 'hex' })
.skip(4)
// 0x1A
const FirstStartBlock = new Parser()
.skip(4)
// 0x1B
const SecondStartBlock = new Parser()
.skip(4)
// 0x1C
const ThirdStartBlock = new Parser()
.skip(4)
}
: {
type: at.type,
msgvar: at.fork.msgvar,
msgnode: at.fork.msgnode
};
}
nodeParser
.int8('tag')
.choice('cell', {
tag: 'tag',
choices: {
// Lambda a b
0: Parser.start().nest('lambda', {
type: Parser.start().nest('left', { type: 'node' })
.nest('right', { type: 'node' })
}),
// Var a
1: Parser.start().nest('var', { type: variableParser,
formatter: variableFormatter }),
// Type a
2: Parser.start().nest('type', { type: typeParser,
formatter: typeFormatter }),
// App a b
3: Parser.start().nest('app', {
type: Parser.start().nest('subject', { type: 'node' })
.nest('object', { type: appRightSideParser,
formatter: appRightSideFormatter })
}),
// Record a b
4: Parser.start().nest('record', { type: recordParser,
// Module import
const fs = require('fs');
const Parser = require('binary-parser').Parser;
const readComment = Parser.start().array('comment', {
type: 'uint8',
readUntil: function (a, b) {
return b.readUInt16LE() === 0 || b.length < 3;
},
formatter: function (a) {
return a.map(v => String.fromCharCode(parseInt(v, 10).toString(10))).join('');
}
}).skip(2);
const readMove = Parser.start()
.uint8('move', {
formatter: (flag) => [flag % 16 - 1, Math.ceil(flag / 16) - 1]
})
.bit1('down') // 128 Has Siblings
.bit1('right') // 64 Has a child node, into the subtree
.bit1('hz4')
import { Parser } from 'binary-parser';
import { ConstantPoolInfo } from './ConstantPoolParser';
import { AttributeInfo, ClassMemberInfo, InterfaceInfo } from './ClassMembers';
const JVM_CLASS_FILE_MAGIC_NUMBER = 0xcafebabe;
export const ClassFileParser =
Parser.start()
// This is the default endian type for binary-parser but it's better to be safe than sorry.
.endianess('big')
.uint32('magic', { assert: JVM_CLASS_FILE_MAGIC_NUMBER })
.uint16('minor_version')
.uint16('major_version')
.uint16('constant_pool_count')
.array('constant_pool', {
type: ConstantPoolInfo,
length: function () {
let lastIdx = this.constant_pool.length - 1;
let lastEntry = this.constant_pool[lastIdx];
// Quote from the JVM class file spec (Chapter 4.4.5):
//
// > All 8-byte constants take up two entries in the constant_pool
// > table of the class file. If a CONSTANT_Long_info or CONSTANT_Double_info
// Module import
const fs = require('fs');
const Parser = require('binary-parser').Parser;
const readComment = Parser.start().array('comment', {
type: 'uint8',
readUntil: function (a, b) {
return b.readUInt16LE() === 0 || b.length < 3;
},
formatter: function (a) {
return a.map(v => String.fromCharCode(parseInt(v, 10).toString(10))).join('');
}
}).skip(2);
const readMove = Parser.start()
.uint8('move', {
formatter: (flag) => [flag % 16 - 1, Math.ceil(flag / 16) - 1]
})
.bit1('down') // 128 Has Siblings
.bit1('right') // 64 Has a child node, into the subtree
.bit1('hz4')
.bit1('mark')
.bit1('hasComment')
.bit1('hz2')
.bit1('hz1',)
.bit1('extension')
.choice('comment', {
tag: 'hasComment',
choices: {
0: Parser.start(),
1: readComment
const readMove = Parser.start()
.uint8('move', {
formatter: (flag) => [flag % 16 - 1, Math.ceil(flag / 16) - 1]
})
.bit1('down') // 128 Has Siblings
.bit1('right') // 64 Has a child node, into the subtree
.bit1('hz4')
.bit1('mark')
.bit1('hasComment')
.bit1('hz2')
.bit1('hz1',)
.bit1('extension')
.choice('comment', {
tag: 'hasComment',
choices: {
0: Parser.start(),
1: readComment
}
}
);
const header = Parser.start()
.endianess('little')
.uint8('open', {assert: 255})
.string('type', {
length: 6,
assert: 'RenLib'
})
.uint8('open', {assert: 255})
.int8('major', {assert: 3})
.int8('minor')
return this.setNextParser('uint64', fieldName, Object.assign({}, options, {
formatter: function (recv_time) {
var UInt64 = require('cuint').UINT64;
var low = buffer.readUInt32LE(offset);
offset += 4;
var high = buffer.readUInt32LE(offset);
offset += 4;
return new UInt64(low, high);
}
}));
}
/**
* Replace the original compile function to pass in `require` (???)
*/
Parser.prototype.compile = function() {
var compiledCode = this.getCode();
this.compiled = function (buffer, callback, constructorFn) {
// Need to statically provide dependencies for webpack.
var _require = function (x) { if (x === 'cuint') return require('cuint'); throw new Error('Unknown module required: ' + x); };
// Needed for browser support. Webpack will polyfill Buffer, but we need it to
// be accessible in this eval'd context.
if (typeof window !== 'undefined' && typeof window.Buffer === 'undefined') window.Buffer = Buffer;
return (new Function('buffer', 'callback', 'constructorFn', 'require', compiledCode)).call(this, buffer, callback, constructorFn, _require);
};
};
module.exports = Parser;
var exportPathParser = new Parser()
.skip(4).int32('count')
.array('path', {
type: exportPathItemParser,
length: 'count'
})
.skip(1);
function exportPathFormatter(v) {
if (!v.path) return v;
return v.path.map(function(data) {
return data.item;
});
}
var singleExportParser = new Parser()
.int8('type')
.skip(4).int32('nameLen')
.string('name', { length: 'nameLen' })
.choice('values', {
tag: 'type',
choices: {
0: stop,
1: stop,
2: exportPathParser
},
formatter: exportPathFormatter
});
var exportsParser = new Parser()
.skip(4).int32('count')
.array('values', {