2018-03-26 23:14:23 +01:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
import Operation from "../Operation" ;
2018-04-02 17:10:51 +01:00
import { fromBase64 , ALPHABET _OPTIONS } from "../lib/Base64" ;
2018-03-26 23:14:23 +01:00
/ * *
* From Base64 operation
* /
class FromBase64 extends Operation {
/ * *
2018-04-02 17:10:51 +01:00
* FromBase64 constructor
2018-03-26 23:14:23 +01:00
* /
constructor ( ) {
super ( ) ;
2018-04-02 17:10:51 +01:00
this . name = "From Base64" ;
this . module = "Default" ;
2018-03-26 23:14:23 +01:00
this . description = "Base64 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers.<br><br>This operation decodes data from an ASCII Base64 string back into its raw format.<br><br>e.g. <code>aGVsbG8=</code> becomes <code>hello</code>" ;
2018-08-21 19:07:13 +01:00
this . infoURL = "https://wikipedia.org/wiki/Base64" ;
2018-04-02 17:10:51 +01:00
this . inputType = "string" ;
this . outputType = "byteArray" ;
this . args = [
2018-03-26 23:14:23 +01:00
{
name : "Alphabet" ,
type : "editableOption" ,
value : ALPHABET _OPTIONS
} ,
{
name : "Remove non-alphabet chars" ,
type : "boolean" ,
value : true
}
] ;
2018-05-20 16:49:42 +01:00
this . patterns = [
{
match : "^(?:[A-Z\\d+/]{4})+(?:[A-Z\\d+/]{2}==|[A-Z\\d+/]{3}=)?$" ,
flags : "i" ,
args : [ "A-Za-z0-9+/=" , false ]
} ,
{
match : "^[A-Z\\d\\-_]{20,}$" ,
flags : "i" ,
args : [ "A-Za-z0-9-_" , false ]
} ,
{
match : "^(?:[A-Z\\d+\\-]{4}){5,}(?:[A-Z\\d+\\-]{2}==|[A-Z\\d+\\-]{3}=)?$" ,
flags : "i" ,
args : [ "A-Za-z0-9+\\-=" , false ]
} ,
{
match : "^(?:[A-Z\\d./]{4}){5,}(?:[A-Z\\d./]{2}==|[A-Z\\d./]{3}=)?$" ,
flags : "i" ,
args : [ "./0-9A-Za-z=" , false ]
} ,
{
match : "^[A-Z\\d_.]{20,}$" ,
flags : "i" ,
args : [ "A-Za-z0-9_." , false ]
} ,
{
match : "^(?:[A-Z\\d._]{4}){5,}(?:[A-Z\\d._]{2}--|[A-Z\\d._]{3}-)?$" ,
flags : "i" ,
args : [ "A-Za-z0-9._-" , false ]
} ,
{
match : "^(?:[A-Z\\d+/]{4}){5,}(?:[A-Z\\d+/]{2}==|[A-Z\\d+/]{3}=)?$" ,
flags : "i" ,
args : [ "0-9a-zA-Z+/=" , false ]
} ,
{
match : "^(?:[A-Z\\d+/]{4}){5,}(?:[A-Z\\d+/]{2}==|[A-Z\\d+/]{3}=)?$" ,
flags : "i" ,
args : [ "0-9A-Za-z+/=" , false ]
} ,
{
match : "^[ !\"#$%&'()*+,\\-./\\d:;<=>?@A-Z[\\\\\\]^_]{20,}$" ,
flags : "" ,
args : [ " -_" , false ]
} ,
{
match : "^[A-Z\\d+\\-]{20,}$" ,
flags : "i" ,
args : [ "+\\-0-9A-Za-z" , false ]
} ,
{
match : "^[!\"#$%&'()*+,\\-0-689@A-NP-VX-Z[`a-fh-mp-r]{20,}$" ,
flags : "" ,
args : [ "!-,-0-689@A-NP-VX-Z[`a-fh-mp-r" , false ]
} ,
{
match : "^(?:[N-ZA-M\\d+/]{4}){5,}(?:[N-ZA-M\\d+/]{2}==|[N-ZA-M\\d+/]{3}=)?$" ,
flags : "i" ,
args : [ "N-ZA-Mn-za-m0-9+/=" , false ]
} ,
{
match : "^[A-Z\\d./]{20,}$" ,
flags : "i" ,
args : [ "./0-9A-Za-z" , false ]
} ,
] ;
2018-03-26 23:14:23 +01:00
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
2018-04-02 17:10:51 +01:00
const [ alphabet , removeNonAlphChars ] = args ;
2018-03-26 23:14:23 +01:00
2018-04-02 17:10:51 +01:00
return fromBase64 ( input , alphabet , "byteArray" , removeNonAlphChars ) ;
2018-03-26 23:14:23 +01:00
}
/ * *
* Highlight to Base64
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlight ( pos , args ) {
pos [ 0 ] . start = Math . ceil ( pos [ 0 ] . start / 4 * 3 ) ;
pos [ 0 ] . end = Math . floor ( pos [ 0 ] . end / 4 * 3 ) ;
return pos ;
}
/ * *
* Highlight from Base64
*
* @ param { Object [ ] } pos
* @ param { number } pos [ ] . start
* @ param { number } pos [ ] . end
* @ param { Object [ ] } args
* @ returns { Object [ ] } pos
* /
highlightReverse ( pos , args ) {
pos [ 0 ] . start = Math . floor ( pos [ 0 ] . start / 3 * 4 ) ;
pos [ 0 ] . end = Math . ceil ( pos [ 0 ] . end / 3 * 4 ) ;
return pos ;
}
}
export default FromBase64 ;