Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function resolveDynamicExtensionObjectV(
opaque: OpaqueStructure,
extraDataType: ExtraDataTypeManager
): ExtensionObject {
try {
const namespaceUri = extraDataType.namespaceArray[opaque.nodeId.namespace];
const expandedNodeId = ExpandedNodeId.fromNodeId(opaque.nodeId, namespaceUri);
const typeDictionary = extraDataType.getTypeDictionaryForNamespace(opaque.nodeId.namespace);
const Constructor = extraDataType.getExtensionObjectConstructorFromBinaryEncoding(opaque.nodeId);
const object = new Constructor();
const stream = new BinaryStream(opaque.buffer);
object.decode(stream);
return object;
} catch (err) {
// tslint:disable-next-line:no-console
console.log("resolveDynamicExtensionObjectV err = ", err);
return opaque;
}
}
export function writeTCPMessageHeader(msgType: string, chunkType: string, totalLength: number, stream: OutputBinaryStream) {
if (stream instanceof Buffer) {
stream = new BinaryStream(stream);
}
assert(is_valid_msg_type(msgType));
assert(["A", "F", "C"].indexOf(chunkType) !== -1);
stream.writeUInt8(msgType.charCodeAt(0));
stream.writeUInt8(msgType.charCodeAt(1));
stream.writeUInt8(msgType.charCodeAt(2));
// Chunk type
stream.writeUInt8(chunkType.charCodeAt(0)); // reserved
stream.writeUInt32(totalLength);
}
private _append(chunk: Buffer): boolean {
if (this._hasReceivedError) {
// the message builder is in error mode and further message chunks should be discarded.
return false;
}
this.messageChunks.push(chunk);
this.totalMessageSize += chunk.length;
const binaryStream = new BinaryStream(chunk);
if (!this._read_headers(binaryStream)) {
return false;
}
assert(binaryStream.length >= 12);
// verify message chunk length
if (this.messageHeader.length !== chunk.length) {
// tslint:disable:max-line-length
return this._report_error(
`Invalid messageChunk size: the provided chunk is ${chunk.length} bytes long but header specifies ${this.messageHeader.length}`);
}
// the start of the message body block
const offsetBodyStart = binaryStream.length;
public chunkSecureMessage(
msgType: string,
options: ChunkMessageOptions,
message: BaseUAObject,
messageChunkCallback: MessageCallbackFunc) {
assert(_.isFunction(messageChunkCallback));
// calculate message size ( with its encodingDefaultBinary)
const binSize = message.binaryStoreSize() + 4;
const stream = new BinaryStream(binSize);
this._stream = stream;
encodeExpandedNodeId(message.schema.encodingDefaultBinary!, stream);
message.encode(stream);
let securityHeader;
if (msgType === "OPN") {
securityHeader = this.securityHeader;
} else {
securityHeader = new SymmetricAlgorithmSecurityHeader({tokenId: options.tokenId});
}
const chunkManager = new SecureMessageChunkManager(
msgType, options, securityHeader, this.sequenceNumberGenerator
);
public writeSequenceHeader(buffer: Buffer) {
const stream = new BinaryStream(buffer);
// write Sequence Header -----------------
this.sequenceHeader.sequenceNumber = this.sequenceNumberGenerator.next();
this.sequenceHeader.encode(stream);
assert(stream.length === 8);
}
MessageBuilderBase.prototype._append = function (message_chunk) {
if (this.status_error) {
// the message builder is in error mode and further message chunks should be discarded.
return false;
}
this.message_chunks.push(message_chunk);
this.total_message_size += message_chunk.length;
const binaryStream = new BinaryStream(message_chunk);
if (!this._read_headers(binaryStream)) {
return false;
}
assert(binaryStream.length >= 12);
// verify message chunk length
if (this.messageHeader.length !== message_chunk.length) {
return this._report_error("Invalid messageChunk size: " +
"the provided chunk is " + message_chunk.length + " bytes long " +
"but header specifies " + this.messageHeader.length);
}
// the start of the message body block
const offsetBodyStart = binaryStream.length;
function encoded(obj: BaseUAObject) {
const stream = new BinaryStream(obj.binaryStoreSize());
obj.encode(stream);
return stream.buffer.toString("hex");
}
encoded(obj1).should.eql(encoded(obj2));
export function analyze_object_binary_encoding(obj: BaseUAObject) {
assert(obj);
const size = obj.binaryStoreSize();
console.log("-------------------------------------------------");
console.log(" size = ", size);
const stream = new BinaryStream(size);
obj.encode(stream);
stream.rewind();
console.log("-------------------------------------------------");
if (stream.buffer.length < 256) {
console.log(hexDump(stream.buffer));
console.log("-------------------------------------------------");
}
const reloadedObject = new (obj.constructor as any)();
analyzePacket(stream.buffer, reloadedObject, 0);
}
function verify_message_chunk(message_chunk) {
assert(message_chunk);
assert(message_chunk instanceof Buffer);
const header = readMessageHeader(new BinaryStream(message_chunk));
if (message_chunk.length !== header.length) {
throw new Error(" chunk length = " + message_chunk.length + " message length " + header.length);
}
}
export function analyzePacket(buffer: Buffer, objMessage: any, padding: number, offset?: number, customOptions?: AnalyzePacketOptions) {
const stream = new BinaryStream(buffer);
_internalAnalyzePacket(buffer, stream, objMessage, padding, customOptions, offset);
}