2018-05-16 19:10:50 +02:00
/ * *
* Some parts taken from mimelib ( http : //github.com/andris9/mimelib)
* @ author Andris Reinman
* @ license MIT
*
* @ 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" ;
2018-05-16 19:10:50 +02:00
/ * *
* From Quoted Printable operation
* /
class FromQuotedPrintable extends Operation {
/ * *
* FromQuotedPrintable constructor
* /
constructor ( ) {
super ( ) ;
this . name = "From Quoted Printable" ;
this . module = "Default" ;
2019-09-03 17:21:58 +02:00
this . description = "Converts QP-encoded text back to standard text.<br><br>e.g. The quoted-printable encoded string <code>hello=20world</code> becomes <code>hello world</code>" ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Quoted-printable" ;
2018-05-16 19:10:50 +02:00
this . inputType = "string" ;
this . outputType = "byteArray" ;
this . args = [ ] ;
2020-02-25 12:27:03 +01:00
this . checks = {
input : {
regex : [
{
match : "^[\\x21-\\x3d\\x3f-\\x7e \\t]{0,76}(?:=[\\da-f]{2}|=\\r?\\n)(?:[\\x21-\\x3d\\x3f-\\x7e \\t]|=[\\da-f]{2}|=\\r?\\n)*$" ,
flags : "i" ,
args : [ ]
} ,
]
}
} ;
2018-05-16 19:10:50 +02:00
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { byteArray }
* /
run ( input , args ) {
const str = input . replace ( /=(?:\r?\n|$)/g , "" ) ;
const encodedBytesCount = ( str . match ( /=[\da-fA-F]{2}/g ) || [ ] ) . length ,
bufferLength = str . length - encodedBytesCount * 2 ,
buffer = new Array ( bufferLength ) ;
let chr , hex ,
bufferPos = 0 ;
for ( let i = 0 , len = str . length ; i < len ; i ++ ) {
chr = str . charAt ( i ) ;
if ( chr === "=" && ( hex = str . substr ( i + 1 , 2 ) ) && /[\da-fA-F]{2}/ . test ( hex ) ) {
buffer [ bufferPos ++ ] = parseInt ( hex , 16 ) ;
i += 2 ;
continue ;
}
buffer [ bufferPos ++ ] = chr . charCodeAt ( 0 ) ;
}
return buffer ;
}
}
export default FromQuotedPrintable ;