Jump to content

Recommended Posts

Posted (edited)

Yeasterday I found this:

https://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript

https://www.motobit.com/tips/detpg_quoted-printable-encode/

https://www.motobit.com/tips/detpg_quoted-printable-decode/

Here is AutoIt Version:

#Region - QuotedPrintableEncode_Binary
;~ https://www.motobit.com/tips/detpg_quoted-printable-encode/

; This article contains a short function for quoted printable encoding, using CDO.Message object.
; You can use this function in ASP or .VBS files (wsh - windows scripting host files), or directly in VBA (visual basic 5, 6, Word, Excel, Access and Outlook scripting).
; A source data of this function is a String variable and charset parameter of destination data.
; The source string (16bit VBScript BSTR variable) is first converted to a destination charset, using ADODB.Stream (GetDecodedContentStream).
; If the destination charset is not specified, the ADODB.Stream uses "iso-8859-1" by default.
; The EncodedContentStream then converts the binary data to a Quoted-Printable output string.

; VBScript QuotedPrintable encoding
; 2005 Antonin Foller http://www.motobit.com
; $s_SourceString - string variable with source data, BSTR type
; $s_CharSet - $s_CharSet of the destination data
Func _QuotedPrintable_Encode($s_SourceString, $s_CharSet)
    ;Create CDO.$oCDOMessage object For the encoding.
    Local $oCDOMessage = ObjCreate("CDO.Message")

    ; Set the encoding
    $oCDOMessage.BodyPart.ContentTransferEncoding = "quoted-printable"

    ; Get the data $oStream To write source string data
    ; As ADODB.$oStream
    Local $oStream = $oCDOMessage.BodyPart.GetDecodedContentStream

    ; Set the $s_CharSet For the destination data, If required
    If StringLen($s_CharSet) > 0 Then $oStream.CharSet = $s_CharSet

    ; Write the VBScript string To the $oStream.
    $oStream.WriteText($s_SourceString)

    ; Store the data To the $oCDOMessage BodyPart
    $oStream.Flush

    ; Get an encoded $oStream
    $oStream = $oCDOMessage.BodyPart.GetEncodedContentStream

    ; read the encoded data As a string
    Return $oStream.ReadText

    ; You can use Read method To get a binary data.
    ; $oStream.Type = 1
    ; Return $oStream.Read
EndFunc   ;==>QuotedPrintableEncode

; Next is a binary variant of the function, with bytearray (VT_UI1 VT_ARRAY) as input and output.
; You can simply modify these two functions for combination of binary and string input and output parameters.
; This Func is used on Quoted-printable encoder online sample page.

; VBScript QuotedPrintableEncode_Binary encoding
; 2005 Antonin Foller http://www.motobit.com
Func _QuotedPrintable_Encode_Binary($dSourceBinary)
    ; Create CDO.$oCDOMessage object For the encoding.
    Local $oCDOMessage = ObjCreate("CDO.Message")

    ; Set the encoding
    $oCDOMessage.BodyPart.ContentTransferEncoding = "quoted-printable"

    ; Get the data $oStream To write source string data
    ; As ADODB.$oStream
    Local $oStream = $oCDOMessage.BodyPart.GetDecodedContentStream

    ; Set the type of the $oStream To adTypeBinary.
    $oStream.Type = 1
    ; Write the VBScript string To the $oStream.
    $oStream.Write($dSourceBinary)

    ; Store the data To the $oCDOMessage BodyPart
    $oStream.Flush

    ; Get an encoded $oStream
    $oStream = $oCDOMessage.BodyPart.GetEncodedContentStream

    ; Set the type of the $oStream To adTypeBinary.
    $oStream.Type = 1

    ; You can use Read method To get a binary data.
    Return $oStream.Read
EndFunc   ;==>QuotedPrintableEncode_Binary

#EndRegion - _QuotedPrintable_Encode_Binary

#Region - _QuotedPrintable_Decode_Binary
;~ https://www.motobit.com/tips/detpg_quoted-printable-decode/

; This article contains a short Func for quoted printable decoding, using CDO.Message object.
; You can use this Func in ASP or .VBS files (wsh - windows scripting host files), or directly in VBA (visual basic 5, 6, Word, Excel, Access and Outlook scripting).

; VBScript QuotedPrintableDecode decoding Function
; 2005 Antonin Foller http://www.motobit.com
Func _QuotedPrintable_Decode($s_SourceData, $s_CharSet)
    ; Create CDO.Message object For the encoding.
    Local $oCDO_Message = ObjCreate("CDO.Message")

    ; Set the encoding
    $oCDO_Message.BodyPart.ContentTransferEncoding = "quoted-printable"

    ; Get the data $oStream To write source string data
    ; As ADODB.$oStream
    Local $oStream = $oCDO_Message.BodyPart.GetEncodedContentStream
    If VarGetType($s_SourceData) = 'String' Then
        ; Set $s_CharSet To base windows $s_CharSet
        $oStream.CharSet = "windows-1250"

        ; Write the VBScript string To the $oStream.
        $oStream.WriteText($s_SourceData)
    Else
        ; Set the type of the $oStream To adTypeBinary.
        $oStream.Type = 1

        ; Write the source binary data To the $oStream.
        $oStream.Write($s_SourceData)
    EndIf

    ; Store the data To the $oCDO_Message BodyPart
    $oStream.Flush

    ; Get an encoded $oStream
    $oStream = $oCDO_Message.BodyPart.GetDecodedContentStream

    ; Set the type of the $oStream To adTypeBinary.
    $oStream.CharSet = $s_CharSet

    ; You can use Read method To get a binary data.
    Return $oStream.ReadText
EndFunc   ;==>_QuotedPrintable_Decode

; Next is a binary variant of the function, with bytearray (VT_UI1 VT_ARRAY) as output.
; The _QuotedPrintable_Decode_Binary then converts the binary data to a Quoted-Printable output string.
; Output of this Func are binary decoded data (you can use it, for example, as a data parameter of Response.
; BinaryWrite method) You can simply modify these two functions for combination of binary and string input and output parameters.
; This Func is used on Quoted-printable decoder online sample page.

; VBScript _QuotedPrintable_Decode_Binary decoding Function
; 2005 Antonin Foller http://www.motobit.com
Func _QuotedPrintable_Decode_Binary($s_SourceData)
    ; Create CDO.Message object For the encoding.
    Local $oCDO_Message = ObjCreate("CDO.Message")

    ; Set the encoding
    $oCDO_Message.BodyPart.ContentTransferEncoding = "quoted-printable"

    ; Get the data $oStream To write source string data
    ; As ADODB.$oStream
    Local $oStream = $oCDO_Message.BodyPart.GetEncodedContentStream
    If VarGetType($s_SourceData) = 'String' Then
        ; Write the VBScript string To the $oStream.
        $oStream.Write($s_SourceData)
    Else
        ; Set the type of the $oStream To adTypeBinary.
        $oStream.Type = 1
        ; Write the source binary data To the $oStream.
        $oStream.Write($s_SourceData)
    EndIf

    ; Store the data To the $oCDO_Message BodyPart
    $oStream.Flush

    ; Get an encoded $oStream
    $oStream = $oCDO_Message.BodyPart.GetDecodedContentStream

    ; Set the type of the $oStream To adTypeBinary.
    $oStream.Type = 1

    ; You can use Read method To get a binary data.
    Return $oStream.Read
EndFunc   ;==>_QuotedPrintable_Decode_Binary
#EndRegion - _QuotedPrintable_Decode_Binary

 

REMARK: License note from: https://www.motobit.com/tips/detpg_quoted-printable-decode/ and https://www.motobit.com/tips/detpg_quoted-printable-encode/:

  Quote
The source code on this page and other samples at https://www.motobit.com/tips/ are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
<A
 Href="https://www.motobit.com/tips/detpg_quoted-printable-decode/"
 Title="This article contains a short function
	for quoted printable decoding, using
	CDO.Message object."
>Quoted-printable decode VBScript function.</A>
Expand  

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • mLipok changed the title to quoted-printable.au3 UDF
Posted

I get errors with this script.

"D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3"(24,70) : warning: $oStream: possibly used before declaration.
    Local $oStream = $oCDOMessage.BodyPart.GetDecodedContent($oStream)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3 (78,12) : error: syntax error
    Return =
~~~~~~~~~~~^
D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3 (160,9) : error: Statement cannot be just an expression.
    Set $oStream
~~~~~~~~^
D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3 (24,70) : error: $oStream: undeclared global variable.
    Local $oStream = $oCDOMessage.BodyPart.GetDecodedContent($oStream)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3 (160,9) : error: Set(): undefined function.
    Set $oStream
~~~~~~~~^
D:\AutoIt\Scripts\Temporary & Arrange\Au3Script_16-01-2021,11;01;37.au3 - 4 error(s), 1 warning(s)
>Exit code: 2    Time: 0.07674

I use this to decode quoted-printable:

ConsoleWrite(_Encoding_QuotedPrintableDecode('=C0=F3=F2=EE=E8=F2') & @CRLF)

Func _Encoding_QuotedPrintableDecode($sString)
    Local $iSymbolFound = 2, $sRet = ''
    
    $sString = StringRegExpReplace($sString, '=\r\n', '')
    
    For $i = 1 To StringLen($sString)
        If StringRegExp(StringMid($sString, $i, 3), '=[A-Fa-f0-9]{2}') Then
            $iSymbolFound = 0
            $sRet &= BinaryToString('0x' & StringMid($sString, $i + 1, 2))
        Else
            $iSymbolFound += 1
            
            If $iSymbolFound = 3 Then
                $iSymbolFound = 2
                $sRet &= StringMid($sString, $i, 1)
            EndIf
        EndIf
    Next
    
    If $sRet = '' Then
        Return SetError(1, 0, '')
    EndIf
    
    Return $sRet
EndFunc

 

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted
  On 1/16/2021 at 9:17 AM, MrCreatoR said:

I get errors with this script.

Expand  

Fixed in OP.

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...