Advertisement
SReject

hex2mime

May 11th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 3.59 KB | None | 0 0
  1. ;; $hex2mime(input, n, &output)
  2. ;;   Converts the input hex-encoded value to base64/mime and outputs the result
  3. ;;
  4. ;;   input - Required
  5. ;;     The input hex to convert to base64
  6. ;;
  7. ;;   n - Optional
  8. ;;     if n is 0 the input is plain-text (default)
  9. ;;     if n is 1 the input is a bvar
  10. ;;
  11. ;;   &output - Optional
  12. ;;     bvar to put the output into
  13. ;;     If specified, the number of bytes stored in the bvar is returned
  14. ;;     Contents of &output will be overwritten
  15.  
  16. alias hex2mime {
  17.  
  18.   if (!$0 || $0 > 3) {
  19.     echo -cgt info * $!hex2mime: Invalid parameters
  20.     halt
  21.   }
  22.  
  23.   var %inBVar
  24.  
  25.   /*===============
  26.     DEDUCE INPUTS
  27.   ===============*/  
  28.  
  29.   ; Assume: $hex2mime(input, type, &output)
  30.   var %input   = $1
  31.   var %inType  = $2
  32.   var %output  = $3
  33.   var %outType = 1
  34.  
  35.   ;; $hex2mime(input_as_text)
  36.   if ($0 == 1) {
  37.     %intype  = 0
  38.     %outtype = 0
  39.   }
  40.  
  41.   ;; $hex2mime(input_as_text, &output)
  42.   elseif ($0 == 2 && $regex($2, /^&\S+$/)) {
  43.     %intype  = 0
  44.     %output  = $2
  45.     %outtype = 1
  46.   }
  47.  
  48.   ;; $hex2mime(input, type)
  49.   else if ($0 == 2) {
  50.     %intype  = $2
  51.     %outtype = 0
  52.   }
  53.  
  54.  
  55.  
  56.   /*=================
  57.     VALIDATE INPUTS
  58.   =================*/
  59.   ;; validate type
  60.   if (%intype !isnum 0-1 || . isin %intype) {
  61.     echo -cgt info * $!hex2mime: Invalid parameters
  62.     halt
  63.   }
  64.  
  65.  
  66.   ;; validate input
  67.   if (%intype == 1) {
  68.     if (!$regex(%input, /^&\S+$/)) {
  69.       echo -cgt info * $!hex2mime: Invalid parameters
  70.       halt
  71.     }
  72.     %inBVar = %input
  73.   }
  74.   else {
  75.     %inBVar = $ticks $+ 0000
  76.     while ($bvar(&hex2mime $+ %inBVar)) {
  77.       inc %inBVar
  78.     }
  79.     %inBVar = &hex2mime $+ %inBVar
  80.     bset -t %inBVar 1 %input
  81.   }
  82.  
  83.  
  84.   ;; validate &output
  85.   if (%outtype == 1 && !$regex(%output, /^&\S+$/)) {
  86.     echo -cgt info * $!hex2mime: Invalid parameters
  87.     halt
  88.   }
  89.   elseif (%outtype == 0) {
  90.     %output = $ticks $+ 0000
  91.     while ($bvar(&hex2mime $+ %output)) {
  92.       inc %output
  93.     }
  94.     %output = &hex2mime $+ %output
  95.   }
  96.  
  97.  
  98.   /*============
  99.     CONVERSION
  100.   ============*/
  101.  
  102.   ;; clear output bvar
  103.   bunset %output
  104.  
  105.  
  106.   ; setup for loop
  107.   var %pos    = 1
  108.   var %len    = $bvar(%inBVar, 0)
  109.   var %outpos = 1
  110.   var %peek, %peek2
  111.  
  112.   ; loop to convert from hex to dec-binary
  113.   while (%pos <= %len) {
  114.     %peek = $bvar(%inBVar, %pos, 1).text
  115.  
  116.     ;; ignore white space between 2-character hex values
  117.     if (%peek isin $crlf $chr(9)) {
  118.       inc %pos
  119.       continue
  120.     }
  121.  
  122.     ;; validate first hex character
  123.     if (%peek !isin abcdef0123456789) {
  124.       echo -cgt info * $!hex2mime: Input invalid: expected hex character at %pos instead found: %peek
  125.       halt
  126.     }
  127.  
  128.     ;; validate second hex character    
  129.     inc %pos
  130.     %peek2 = $bvar(%inBVar, %pos, 1).text
  131.     if (%pos === %len || %peek2 isin $crlf $chr(9)) {
  132.       echo -cgt info * $!hex2mime: Input invalid: naked hex value at %pos
  133.       halt
  134.     }
  135.     if ($bvar(%inBVar, %pos, 1).text !isin abcdef0123456789) {
  136.       echo -cgt info * $!hex2mime: Input invalid: expected hex character at %pos instead found: %peek2
  137.       halt
  138.     }
  139.  
  140.     ;; Valid; convert to decimal and store in output bvar
  141.     bset %output %outpos $base(%peek $+ %peek2, 16, 10)
  142.     inc %outpos
  143.  
  144.     inc %pos
  145.   }
  146.  
  147.   ; encode output bvar as base64/mime
  148.   noop $encode(%output, mb)
  149.  
  150.  
  151.  
  152.   /*============
  153.     RESULT
  154.   ============*/
  155.   if (%outType == 1) {
  156.     return $bvar(%output, 0)
  157.   }
  158.   elseif ($bvar(%output, 0) > 4000) {
  159.     echo -cgt info * $!hex2mime: Output line to long
  160.     halt
  161.   }
  162.   else {
  163.     return $bvar(%output, 1-).text
  164.   }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement