2021-09-11 17:03:59 +02:00
/ * *
* @ author Thomas Weißschuh [ thomas @ t - 8 ch . de ]
* @ copyright Crown Copyright 2021
* @ license Apache - 2.0
* /
2021-09-17 08:32:08 +02:00
import { ALPHABET , highlightToBase45 , highlightFromBase45 } from "../lib/Base45.mjs" ;
2021-09-11 17:03:59 +02:00
import Operation from "../Operation.mjs" ;
import Utils from "../Utils.mjs" ;
/ * *
* To Base45 operation
* /
class ToBase45 extends Operation {
/ * *
* ToBase45 constructor
* /
constructor ( ) {
super ( ) ;
this . name = "To Base45" ;
this . module = "Default" ;
this . description = "Base45 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. The high number base results in shorter strings than with the decimal or hexadecimal system. Base45 is optimized for usage with QR codes." ;
this . infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems" ;
this . inputType = "ArrayBuffer" ;
this . outputType = "string" ;
this . args = [
{
name : "Alphabet" ,
type : "string" ,
2022-03-28 17:15:37 +02:00
value : ALPHABET
2021-09-11 17:03:59 +02:00
}
] ;
2021-09-17 08:32:08 +02:00
this . highlight = highlightToBase45 ;
this . highlightReverse = highlightFromBase45 ;
2021-09-11 17:03:59 +02:00
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
input = new Uint8Array ( input ) ;
2022-03-28 17:15:37 +02:00
const alphabet = Utils . expandAlphRange ( args [ 0 ] ) ;
2021-09-11 17:03:59 +02:00
if ( ! input ) return "" ;
const res = [ ] ;
for ( const pair of Utils . chunked ( input , 2 ) ) {
let b = 0 ;
for ( const e of pair ) {
b *= 256 ;
b += e ;
}
let chars = 0 ;
do {
2022-03-28 17:15:37 +02:00
res . push ( alphabet [ b % 45 ] ) ;
2021-09-11 17:03:59 +02:00
chars ++ ;
b = Math . floor ( b / 45 ) ;
} while ( b > 0 ) ;
if ( chars < 2 ) {
res . push ( "0" ) ;
}
}
return res . join ( "" ) ;
}
}
export default ToBase45 ;