2018-05-06 13:18:41 +01:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import Utils from "../Utils.mjs" ;
import { toHex } from "../lib/Hex.mjs" ;
2018-05-06 13:18:41 +01:00
/**
* To Hex Content operation
*/
class ToHexContent extends Operation {
/**
* ToHexContent constructor
*/
constructor () {
super ();
this . name = "To Hex Content" ;
this . module = "Default" ;
2018-08-21 19:07:13 +01:00
this . description = "Converts special characters in a string to hexadecimal. This format is used by SNORT for representing hex within ASCII text.<br><br>e.g. <code>foo=bar</code> becomes <code>foo|3d|bar</code>." ;
this . infoURL = "http://manual-snort-org.s3-website-us-east-1.amazonaws.com/node32.html#SECTION00451000000000000000" ;
2019-07-29 17:09:46 +01:00
this . inputType = "ArrayBuffer" ;
2018-05-06 13:18:41 +01:00
this . outputType = "string" ;
this . args = [
{
"name" : "Convert" ,
"type" : "option" ,
"value" : [ "Only special chars" , "Only special chars including spaces" , "All chars" ]
},
{
"name" : "Print spaces between bytes" ,
"type" : "boolean" ,
"value" : false
}
];
}
/**
2019-07-29 17:09:46 +01:00
* @param {ArrayBuffer} input
2018-05-06 13:18:41 +01:00
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
2019-07-29 17:09:46 +01:00
input = new Uint8Array ( input );
2018-05-06 13:18:41 +01:00
const convert = args [ 0 ];
const spaces = args [ 1 ];
if ( convert === "All chars" ) {
let result = "|" + toHex ( input ) + "|" ;
if ( ! spaces ) result = result . replace ( / /g , "" );
return result ;
}
let output = "" ,
inHex = false ,
b ;
const convertSpaces = convert === "Only special chars including spaces" ;
for ( let i = 0 ; i < input . length ; i ++ ) {
b = input [ i ];
if (( b === 32 && convertSpaces ) || ( b < 48 && b !== 32 ) || ( b > 57 && b < 65 ) || ( b > 90 && b < 97 ) || b > 122 ) {
if ( ! inHex ) {
output += "|" ;
inHex = true ;
} else if ( spaces ) output += " " ;
output += toHex ([ b ]);
} else {
if ( inHex ) {
output += "|" ;
inHex = false ;
}
output += Utils . chr ( input [ i ]);
}
}
if ( inHex ) output += "|" ;
return output ;
}
}
export default ToHexContent ;