Tidied up TLV operation and tests

This commit is contained in:
n1474335 2018-10-10 13:56:20 +00:00
parent a15af602e0
commit e6b89d571e
7 changed files with 148 additions and 146 deletions

View file

@ -54,7 +54,7 @@
"From MessagePack",
"To Braille",
"From Braille",
"LV Decode"
"Parse TLV"
]
},
{

View file

@ -1,5 +1,8 @@
/**
* Parser for Type-length-value data.
*
* @author gchq77703 []
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
@ -11,12 +14,15 @@ const defaults = {
};
/**
* Length Value library
* TLVParser library
*/
export default class LengthValue {
export default class TLVParser {
/**
* LengthValue constructor
* TLVParser constructor
*
* @param {byteArray} input
* @param {Object} options
*/
constructor(input, options) {
this.input = input;
@ -24,7 +30,7 @@ export default class LengthValue {
}
/**
* @returns {Number}
* @returns {number}
*/
getLength() {
if (this.basicEncodingRules) {
@ -48,13 +54,14 @@ export default class LengthValue {
}
/**
* @param {Number} length
* @returns {Number[]}
* @param {number} length
* @returns {number[]}
*/
getValue(length) {
const value = [];
for (let i = 0; i < length; i++) {
if (this.location > this.input.length) return value;
value.push(this.input[this.location]);
this.location++;
}
@ -63,7 +70,7 @@ export default class LengthValue {
}
/**
* @returns {Boolean}
* @returns {boolean}
*/
atEnd() {
return this.input.length <= this.location;

View file

@ -1,81 +0,0 @@
/**
* @author gchq77703 []
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Operation from "../Operation";
import LengthValue from "../lib/LengthValue";
/**
* From LV Decode operation
*/
class LVDecode extends Operation {
/**
* LVDecode constructor
*/
constructor() {
super();
this.name = "LV Decode";
this.module = "Default";
this.description = "Converts a Length-Value (LV) encoded string into a JSON object. Can optionally include a <code>Key</code> / <code>Type</code> entry.";
this.infoURL = "https://wikipedia.org/wiki/KLV";
this.inputType = "byteArray";
this.outputType = "JSON";
this.args = [
{
name: "Bytes in Key Value",
type: "option",
value: [
"0 Bytes (No Key)",
"1 Byte",
"2 Bytes",
"4 Bytes"
]
},
{
name: "Bytes in Length Value",
type: "option",
value: [
"1 Byte",
"2 Bytes",
"4 Bytes"
]
},
{
name: "Use Basic Encoding Rules",
type: "boolean"
}
];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const bytesInKey = parseInt(args[0].split(" ")[0], 10);
const bytesInLength = parseInt(args[1].split(" ")[0], 10);
const basicEncodingRules = args[2];
const lv = new LengthValue(input, { bytesInLength, basicEncodingRules });
const data = [];
while (!lv.atEnd()) {
const key = bytesInKey ? lv.getValue(bytesInKey) : undefined;
const length = lv.getLength();
const value = lv.getValue(length);
data.push({ key, length, value });
}
return data;
}
}
export default LVDecode;

View 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;