Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; $hex2mime(input, n, &output)
- ;; Converts the input hex-encoded value to base64/mime and outputs the result
- ;;
- ;; input - Required
- ;; The input hex to convert to base64
- ;;
- ;; n - Optional
- ;; if n is 0 the input is plain-text (default)
- ;; if n is 1 the input is a bvar
- ;;
- ;; &output - Optional
- ;; bvar to put the output into
- ;; If specified, the number of bytes stored in the bvar is returned
- ;; Contents of &output will be overwritten
- alias hex2mime {
- if (!$0 || $0 > 3) {
- echo -cgt info * $!hex2mime: Invalid parameters
- halt
- }
- var %inBVar
- /*===============
- DEDUCE INPUTS
- ===============*/
- ; Assume: $hex2mime(input, type, &output)
- var %input = $1
- var %inType = $2
- var %output = $3
- var %outType = 1
- ;; $hex2mime(input_as_text)
- if ($0 == 1) {
- %intype = 0
- %outtype = 0
- }
- ;; $hex2mime(input_as_text, &output)
- elseif ($0 == 2 && $regex($2, /^&\S+$/)) {
- %intype = 0
- %output = $2
- %outtype = 1
- }
- ;; $hex2mime(input, type)
- else if ($0 == 2) {
- %intype = $2
- %outtype = 0
- }
- /*=================
- VALIDATE INPUTS
- =================*/
- ;; validate type
- if (%intype !isnum 0-1 || . isin %intype) {
- echo -cgt info * $!hex2mime: Invalid parameters
- halt
- }
- ;; validate input
- if (%intype == 1) {
- if (!$regex(%input, /^&\S+$/)) {
- echo -cgt info * $!hex2mime: Invalid parameters
- halt
- }
- %inBVar = %input
- }
- else {
- %inBVar = $ticks $+ 0000
- while ($bvar(&hex2mime $+ %inBVar)) {
- inc %inBVar
- }
- %inBVar = &hex2mime $+ %inBVar
- bset -t %inBVar 1 %input
- }
- ;; validate &output
- if (%outtype == 1 && !$regex(%output, /^&\S+$/)) {
- echo -cgt info * $!hex2mime: Invalid parameters
- halt
- }
- elseif (%outtype == 0) {
- %output = $ticks $+ 0000
- while ($bvar(&hex2mime $+ %output)) {
- inc %output
- }
- %output = &hex2mime $+ %output
- }
- /*============
- CONVERSION
- ============*/
- ;; clear output bvar
- bunset %output
- ; setup for loop
- var %pos = 1
- var %len = $bvar(%inBVar, 0)
- var %outpos = 1
- var %peek, %peek2
- ; loop to convert from hex to dec-binary
- while (%pos <= %len) {
- %peek = $bvar(%inBVar, %pos, 1).text
- ;; ignore white space between 2-character hex values
- if (%peek isin $crlf $chr(9)) {
- inc %pos
- continue
- }
- ;; validate first hex character
- if (%peek !isin abcdef0123456789) {
- echo -cgt info * $!hex2mime: Input invalid: expected hex character at %pos instead found: %peek
- halt
- }
- ;; validate second hex character
- inc %pos
- %peek2 = $bvar(%inBVar, %pos, 1).text
- if (%pos === %len || %peek2 isin $crlf $chr(9)) {
- echo -cgt info * $!hex2mime: Input invalid: naked hex value at %pos
- halt
- }
- if ($bvar(%inBVar, %pos, 1).text !isin abcdef0123456789) {
- echo -cgt info * $!hex2mime: Input invalid: expected hex character at %pos instead found: %peek2
- halt
- }
- ;; Valid; convert to decimal and store in output bvar
- bset %output %outpos $base(%peek $+ %peek2, 16, 10)
- inc %outpos
- inc %pos
- }
- ; encode output bvar as base64/mime
- noop $encode(%output, mb)
- /*============
- RESULT
- ============*/
- if (%outType == 1) {
- return $bvar(%output, 0)
- }
- elseif ($bvar(%output, 0) > 4000) {
- echo -cgt info * $!hex2mime: Output line to long
- halt
- }
- else {
- return $bvar(%output, 1-).text
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement