2018-04-02 19:06:48 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2020-02-13 15:17:43 +01:00
import { toHex , TO _HEX _DELIM _OPTIONS } from "../lib/Hex.mjs" ;
2019-07-09 13:23:59 +02:00
import Utils from "../Utils.mjs" ;
2018-04-02 19:06:48 +02:00
/ * *
* To Hex operation
* /
class ToHex extends Operation {
/ * *
* ToHex constructor
* /
constructor ( ) {
super ( ) ;
this . name = "To Hex" ;
this . module = "Default" ;
this . description = "Converts the input string to hexadecimal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σ ο υ </code> becomes <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code>" ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Hexadecimal" ;
2018-04-02 19:06:48 +02:00
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
this . args = [
{
name : "Delimiter" ,
type : "option" ,
2020-02-13 15:17:43 +01:00
value : TO _HEX _DELIM _OPTIONS
2020-01-15 01:14:43 +01:00
} ,
{
name : "Bytes per line" ,
type : "number" ,
value : 0
2018-04-02 19:06:48 +02:00
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2020-01-18 14:55:32 +01:00
let delim , comma ;
if ( args [ 0 ] === "0x with comma" ) {
delim = "0x" ;
comma = "," ;
} else {
delim = Utils . charRep ( args [ 0 ] || "Space" ) ;
}
2020-01-17 19:47:46 +01:00
const lineSize = args [ 1 ] ;
return toHex ( new Uint8Array ( input ) , delim , 2 , comma , lineSize ) ;
2018-04-02 19:06:48 +02:00
}
/ * *
* Highlight to Hex
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlight ( pos , args ) {
2022-05-30 20:43:59 +02:00
let delim , commaLen = 0 ;
2020-01-18 14:55:32 +01:00
if ( args [ 0 ] === "0x with comma" ) {
delim = "0x" ;
commaLen = 1 ;
} else {
delim = Utils . charRep ( args [ 0 ] || "Space" ) ;
}
const lineSize = args [ 1 ] ,
2022-07-10 23:01:22 +02:00
len = delim . length + commaLen ;
2018-04-02 19:06:48 +02:00
2020-01-16 00:29:18 +01:00
const countLF = function ( p ) {
// Count the number of LFs from 0 upto p
return ( p / lineSize | 0 ) - ( p >= lineSize && p % lineSize === 0 ) ;
} ;
2018-04-02 19:06:48 +02:00
2020-01-16 00:29:18 +01:00
pos [ 0 ] . start = pos [ 0 ] . start * ( 2 + len ) + countLF ( pos [ 0 ] . start ) ;
pos [ 0 ] . end = pos [ 0 ] . end * ( 2 + len ) + countLF ( pos [ 0 ] . end ) ;
2022-05-30 20:43:59 +02:00
// if the delimiters are not prepended, trim the trailing delimiter
2020-01-16 00:29:18 +01:00
if ( ! ( delim === "0x" || delim === "\\x" ) ) {
pos [ 0 ] . end -= delim . length ;
}
// if there is comma, trim the trailing comma
2020-01-18 14:55:32 +01:00
pos [ 0 ] . end -= commaLen ;
2018-04-02 19:06:48 +02:00
return pos ;
}
/ * *
* Highlight from Hex
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlightReverse ( pos , args ) {
2022-07-10 23:01:22 +02:00
let delim , commaLen = 0 ;
2020-01-18 14:55:32 +01:00
if ( args [ 0 ] === "0x with comma" ) {
delim = "0x" ;
commaLen = 1 ;
} else {
delim = Utils . charRep ( args [ 0 ] || "Space" ) ;
}
const lineSize = args [ 1 ] ,
2022-07-10 23:01:22 +02:00
len = delim . length + commaLen ,
2018-04-02 19:06:48 +02:00
width = len + 2 ;
2020-01-17 13:48:21 +01:00
const countLF = function ( p ) {
// Count the number of LFs from 0 up to p
const lineLength = width * lineSize ;
return ( p / lineLength | 0 ) - ( p >= lineLength && p % lineLength === 0 ) ;
} ;
2018-04-02 19:06:48 +02:00
2020-01-17 13:48:21 +01:00
pos [ 0 ] . start = pos [ 0 ] . start === 0 ? 0 : Math . round ( ( pos [ 0 ] . start - countLF ( pos [ 0 ] . start ) ) / width ) ;
pos [ 0 ] . end = pos [ 0 ] . end === 0 ? 0 : Math . ceil ( ( pos [ 0 ] . end - countLF ( pos [ 0 ] . end ) ) / width ) ;
2018-04-02 19:06:48 +02:00
return pos ;
}
}
export default ToHex ;