Base64: Difference between revisions
(Base 64 Encoder Function written in Native NSIS) |
No edit summary |
||
Line 1: | Line 1: | ||
== BASE64 Encoding Function == | == BASE64 Encoding Function == | ||
As a result of needing to send data in a URL from my NSIS Installer, and because I didn't want to include another dependency, I wrote the below Base64 Encoder. | As a result of needing to send data in a URL from my NSIS Installer, and because I didn't want to include another dependency, I wrote the below Base64 Encoder. | ||
Line 9: | Line 8: | ||
; $0 now equals VEhJUyBXSUxMIEJFIEVOQ09ERUQ= | ; $0 now equals VEhJUyBXSUxMIEJFIEVOQ09ERUQ= | ||
Also there is a variant for encoding in a URL safe form, ${Base64_URLEncode}, which just uses a different encodinging table, (but a standard one,) from the original version. | Also there is a variant for encoding in a URL safe form, ${Base64_URLEncode}, which just uses a different encodinging table, (but a standard one,) from the original version. I don't know how this works with Binary Data, but it works great with string data, which you need to put into a consistent form. | ||
<highlight-nsis> | <highlight-nsis> | ||
!ifndef BASE64_NSH | !ifndef BASE64_NSH | ||
!define BASE64_NSH | !define BASE64_NSH | ||
!define BASE64_ENCODINGTABLE "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | !define BASE64_ENCODINGTABLE "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | ||
Line 30: | Line 26: | ||
!macro Base64_Encode _cleartext | !macro Base64_Encode _cleartext | ||
$ | push $R0 | ||
push $R1 | |||
push $R2 | |||
push $0 | |||
push $1 | |||
push $2 | |||
push $3 | |||
push $4 | |||
push $5 | |||
push $6 | |||
push $7 | |||
push `${_cleartext}` | push `${_cleartext}` | ||
push `${BASE64_ENCODINGTABLE}` | push `${BASE64_ENCODINGTABLE}` | ||
Call Base64_Encode | Call Base64_Encode | ||
Pop $BASE64TEMP | Pop $BASE64TEMP | ||
Pop $7 | |||
Pop $6 | |||
Pop $5 | |||
Pop $4 | |||
Pop $3 | |||
Pop $2 | |||
Pop $1 | |||
Pop $0 | |||
pop $R2 | |||
pop $R1 | |||
pop $R0 | |||
Push $BASE64TEMP | |||
!macroend | !macroend | ||
!macro Base64_URLEncode _cleartext | !macro Base64_URLEncode _cleartext | ||
$ | push $R0 | ||
push $R1 | |||
push $R2 | |||
push $0 | |||
push $1 | |||
push $2 | |||
push $3 | |||
push $4 | |||
push $5 | |||
push $6 | |||
push $7 | |||
push `${_cleartext}` | push `${_cleartext}` | ||
push `${BASE64_ENCODINGTABLEURL}` | push `${BASE64_ENCODINGTABLEURL}` | ||
Call Base64_Encode | Call Base64_Encode | ||
Pop $BASE64TEMP | Pop $BASE64TEMP | ||
Pop $7 | |||
Pop $6 | |||
Pop $5 | |||
Pop $4 | |||
Pop $3 | |||
Pop $2 | |||
Pop $1 | |||
Pop $0 | |||
pop $R2 | |||
pop $R1 | |||
pop $R0 | |||
Push $BASE64TEMP | Push $BASE64TEMP | ||
!macroend | !macroend | ||
Line 119: | Line 155: | ||
# If there is any padding required, we now write that here. | # If there is any padding required, we now write that here. | ||
${IF} $7 > 0 | ${IF} $7 > 0 | ||
${WHILE} $7 > 0 | |||
StrCpy $R1 "$R1${BASE64_PADDING}" | |||
IntOp $7 $7 - 1 | |||
${ENDWHILE} | |||
${ENDIF} | ${ENDIF} | ||
Revision as of 01:49, 26 September 2008
BASE64 Encoding Function
As a result of needing to send data in a URL from my NSIS Installer, and because I didn't want to include another dependency, I wrote the below Base64 Encoder.
Usage is as follows:
${Base64_Encode} "THIS WILL BE ENCODED" Pop $0
- $0 now equals VEhJUyBXSUxMIEJFIEVOQ09ERUQ=
Also there is a variant for encoding in a URL safe form, ${Base64_URLEncode}, which just uses a different encodinging table, (but a standard one,) from the original version. I don't know how this works with Binary Data, but it works great with string data, which you need to put into a consistent form.
<highlight-nsis> !ifndef BASE64_NSH !define BASE64_NSH
!define BASE64_ENCODINGTABLE "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" !define BASE64_ENCODINGTABLEURL "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
!define BASE64_PADDING "="
VAR OCTETVALUE VAR BASE64TEMP
!define Base64_Encode "!insertmacro Base64_Encode" !define Base64_URLEncode "!insertmacro Base64_URLEncode"
!macro Base64_Encode _cleartext push $R0
push $R1 push $R2 push $0 push $1 push $2 push $3 push $4 push $5 push $6 push $7
push `${_cleartext}` push `${BASE64_ENCODINGTABLE}` Call Base64_Encode Pop $BASE64TEMP
Pop $7 Pop $6 Pop $5 Pop $4 Pop $3 Pop $2 Pop $1 Pop $0 pop $R2 pop $R1 pop $R0 Push $BASE64TEMP
!macroend
!macro Base64_URLEncode _cleartext push $R0
push $R1 push $R2 push $0 push $1 push $2 push $3 push $4 push $5 push $6 push $7
push `${_cleartext}` push `${BASE64_ENCODINGTABLEURL}` Call Base64_Encode Pop $BASE64TEMP
Pop $7 Pop $6 Pop $5 Pop $4 Pop $3 Pop $2 Pop $1 Pop $0 pop $R2 pop $R1 pop $R0
Push $BASE64TEMP !macroend
Function Base64_Encode pop $R2 ; Encoding table pop $R0 ; Clear Text StrCpy "$R1" "" # The result
StrLen $1 "$R0" StrCpy $0 0
${WHILE} $0 < $1 # Copy 3 characters, and for each character push their value. StrCpy $OCTETVALUE 0
StrCpy $5 $0 StrCpy $4 "$R0" 1 $5 ${CharToASCII} $4 "$4"
IntOp $OCTETVALUE $4 << 16
IntOp $5 $5 + 1 ${IF} $5 < $1 StrCpy $4 "$R0" 1 $5 ${CharToASCII} $4 "$4"
IntOp $4 $4 << 8 IntOp $OCTETVALUE $OCTETVALUE + $4
IntOp $5 $5 + 1 ${IF} $5 < $1 StrCpy $4 "$R0" 1 $5 ${CharToASCII} $4 "$4"
IntOp $OCTETVALUE $OCTETVALUE + $4 ${ENDIF} ${ENDIF}
# Now take the 4 indexes from the encoding table, based on 6bits each of the octet's value. IntOp $4 $OCTETVALUE >> 18 IntOp $4 $4 & 63 StrCpy $5 "$R2" 1 $4 StrCpy $R1 "$R1$5"
IntOp $4 $OCTETVALUE >> 12 IntOp $4 $4 & 63 StrCpy $5 "$R2" 1 $4 StrCpy $R1 "$R1$5"
StrCpy $6 $0 StrCpy $7 2
IntOp $6 $6 + 1 ${IF} $6 < $1 IntOp $4 $OCTETVALUE >> 6 IntOp $4 $4 & 63 StrCpy $5 "$R2" 1 $4 StrCpy $R1 "$R1$5" IntOp $7 $7 - 1 ${ENDIF}
IntOp $6 $6 + 1 ${IF} $6 < $1 IntOp $4 $OCTETVALUE & 63 StrCpy $5 "$R2" 1 $4 StrCpy $R1 "$R1$5" IntOp $7 $7 - 1 ${ENDIF}
# If there is any padding required, we now write that here. ${IF} $7 > 0
${WHILE} $7 > 0
StrCpy $R1 "$R1${BASE64_PADDING}"
IntOp $7 $7 - 1 ${ENDWHILE}
${ENDIF}
IntOp $0 $0 + 3 ${ENDWHILE}
Push "$R1"
FunctionEnd
!endif ;BASE64_NSH