mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 15:26:16 -04:00
Tidied up TLV operation and tests
This commit is contained in:
parent
a15af602e0
commit
e6b89d571e
7 changed files with 148 additions and 146 deletions
76
src/core/operations/ParseTLV.mjs
Normal file
76
src/core/operations/ParseTLV.mjs
Normal file
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* @author gchq77703 []
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import TLVParser from "../lib/TLVParser";
|
||||
import OperationError from "../errors/OperationError";
|
||||
|
||||
/**
|
||||
* Parse TLV operation
|
||||
*/
|
||||
class ParseTLV extends Operation {
|
||||
|
||||
/**
|
||||
* ParseTLV constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Parse TLV";
|
||||
this.module = "Default";
|
||||
this.description = "Converts a Type-Length-Value (TLV) encoded string into a JSON object. Can optionally include a <code>Key</code> / <code>Type</code> entry. <br><br>Tags: Key-Length-Value, KLV, Length-Value, LV";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Type-length-value";
|
||||
this.inputType = "byteArray";
|
||||
this.outputType = "JSON";
|
||||
this.args = [
|
||||
{
|
||||
name: "Type/Key size",
|
||||
type: "number",
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
name: "Length size",
|
||||
type: "number",
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
name: "Use BER",
|
||||
type: "boolean",
|
||||
value: false
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {byteArray} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [bytesInKey, bytesInLength, basicEncodingRules] = args;
|
||||
|
||||
if (bytesInKey <= 0 && bytesInLength <= 0)
|
||||
throw new OperationError("Type or Length size must be greater than 0");
|
||||
|
||||
const tlv = new TLVParser(input, { bytesInLength, basicEncodingRules });
|
||||
|
||||
const data = [];
|
||||
|
||||
while (!tlv.atEnd()) {
|
||||
const key = bytesInKey ? tlv.getValue(bytesInKey) : undefined;
|
||||
const length = tlv.getLength();
|
||||
const value = tlv.getValue(length);
|
||||
|
||||
data.push({ key, length, value });
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ParseTLV;
|
Loading…
Add table
Add a link
Reference in a new issue