2018-10-10 15:56:20 +02:00
/ * *
* @ author gchq77703 [ ]
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2018
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
import TLVParser from "../lib/TLVParser.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2018-10-10 15:56:20 +02:00
/ * *
* 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" ;
2019-07-29 18:09:46 +02:00
this . inputType = "ArrayBuffer" ;
2018-10-10 15:56:20 +02:00
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
}
] ;
}
/ * *
2019-07-29 18:09:46 +02:00
* @ param { ArrayBuffer } input
2018-10-10 15:56:20 +02:00
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const [ bytesInKey , bytesInLength , basicEncodingRules ] = args ;
2019-07-29 18:09:46 +02:00
input = new Uint8Array ( input ) ;
2018-10-10 15:56:20 +02:00
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 ;