Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #1134 - Read base64 raw string/email in Vim

Created: February 17, 2006 18:21 Complexity: intermediate Author: TonyLiu Version: 6.0 Karma: 5/10 Imported from: Tip#1134

Although this tip is very useful for me, but I think it is somehow

complex for needing a outter command tool.


The most important things today I do with the internet is receiving

and sending emails.


But sometimes I just got some base64 raw string like that:

aGFwcHkgdmltbWluZyA=


do you know what does it mean? -_-;

Maybe i need to copy&paste it into a .eml format file

and to use some email tools like OE to read it. 



But...thanks for VIM!

I can use a system command to read it in VIM directly.

Please follow me......


1. You need an outter command just like decode64.exe (3,072bytes)

to en/decode strings. this is a tool I produced with asm language.

.But unfortunately, I just test it under WIN2K/2003 only.

download it from here

http://iknowuknow2.bokee.com/inc/encode64.zip


After that, please put the decode64.exe/base64dll.dll into somewhere

your %path% enviromental variable included in, such as %SystemRoot%\System32\


2. You need write some script in your _vimrc

"""""""""""""""""""begin""""""""""""""""""""

vnoremap ;mme "ey:call CalcEncode64(1)<CR>

vnoremap ;mmd "ey:call CalcEncode64(0)<CR>

function! CalcEncode64(pattern) " #region

let has_equal = 0 


let l:rege=@e 
let l:regk=@k 
let l:regl=@l 
" 1=encode 0=decode 
let l:elen = strlen(@e) " TOTAL length 
let l:rightmark=strpart(@e, l:elen-4, 3) 


if l:rightmark == "===" 
let @e = strpart(@e,0,l:elen-4) 
let has_equal = 1 
endif 


if a:pattern == 1 
let @k = @e 
elseif a:pattern == 0 
let @k = substitute (@e, "\n", "", "g") 
else 
return "" 
endif 


let l:nsize = 1200 " nsize must be multiples of 4 (byte, for base64 code 4 byte is an unit) 
let l:len = strlen(@k) " TOTAL length 
let l:vleft=l:len/l:nsize " the .n block 
let l:vright=l:len/l:nsize " the .n block total 
let l:answerstr = "" 
while 0< l:vleft 
let l:answerstr = l:answerstr . s:normalExecode(l:vleft,l:nsize,l:len,l:vright,a:pattern) 
let l:vleft = l:vleft-1 
endwhile 


" last block < nsize byte 
let l:answerstr = l:answerstr . s:lastExecode(l:vleft,l:nsize,l:len,l:vright,a:pattern) 


let @k=l:answerstr 
if has_equal == 1 
:normal `>"kp 
else 
echo "answer = ".l:answerstr 
endif 
let @l=l:regl 
let @k=l:regk 
let @e=l:rege 

endfunction " #endregion

function! s:lastExecode(vleft,nsize,len,vright,pattern) " #region

let l:answer="" 
let l:nstart=a:nsize*a:vright 
let @l=strpart(@k, l:nstart, a:len-a:nsize*a:vright) 
let l:answer = s:ExeDecode(a:vleft,a:nsize,a:len,a:vright,a:pattern) 
return l:answer 

endfunction " #endregion

function! s:normalExecode(vleft,nsize,len,vright,pattern) " #region

let l:nstart=(a:vright-a:vleft)*a:nsize 
let @l=strpart(@k, l:nstart, a:nsize) 
let l:answer = s:ExeDecode(a:vleft,a:nsize,a:len,a:vright,a:pattern) 
return l:answer 

endfunction " #endregion

function! s:ExeDecode(vleft,nsize,len,vright,pattern) " #region

" 1=encode 0=decode 
" TODO: when encode,the string must NOT contains any char of pipe '

Comments

I omitted something .... sometimes the raw base64 string is encoded from UNICODE original string

if you want to read it please type

set enc=utf8

at first. type :help enc for help


Anonymous , February 17, 2006 18:33


See also VimTip1032 ( VimTip1032 ) 


anon , February 17, 2006 21:59


In fact,I cannot get 1032tip work now because I dont know how to use perl function under WIN32,Maybe need to learn more about it at first.

-_-;


TonyLiu , February 18, 2006 1:55


Maybe you like to install Ruby at some time in the future.

:ruby require "base64" 
:.rubydo $_=Base64.decode64 $_ 


vim at bertram dash scharpf dot de , February 18, 2006 2:26


Advertisement