Permalink
Browse files

Clean up implementation of Base64 encoding

Apparently I merged without so much as a glance.  Highlights:

* Formatting cleanup
* Make implementation constants private
* Don't blow up if input isn't Base64
  • Loading branch information...
1 parent e801372 commit 76a22b6278aea8f8d3667ba937a34b506d8b21cb @tpope committed Jun 29, 2012
Showing with 42 additions and 43 deletions.
  1. +42 −43 plugin/unimpaired.vim
View
@@ -186,63 +186,62 @@ function! s:UrlDecode(str)
return substitute(str,'%\(\x\x\)','\=nr2char("0x".submatch(1))','g')
endfunction
-let g:unimpaired_base64_chars = map(range(char2nr('A'),char2nr('Z')),'nr2char(v:val)')
- \ + map(range(char2nr('a'),char2nr('z')),'nr2char(v:val)')
- \ + map(range(char2nr('0'),char2nr('9')),'nr2char(v:val)')
- \ + ['+','/']
+let s:base64_chars = map(range(char2nr('A'),char2nr('Z')),'nr2char(v:val)')
+ \ + map(range(char2nr('a'),char2nr('z')),'nr2char(v:val)')
+ \ + map(range(char2nr('0'),char2nr('9')),'nr2char(v:val)')
+ \ + ['+','/']
-let g:unimpaired_base64_filler = '='
-let g:unimpaired_base64_reverse_map = {}
+let s:base64_filler = '='
+let s:base64_lookup = {}
let s:pos = 0
-for s:char in g:unimpaired_base64_chars
- let g:unimpaired_base64_reverse_map[s:char] = s:pos
- let s:pos = s:pos + 1
+for s:char in s:base64_chars
+ let s:base64_lookup[s:char] = s:pos
+ let s:pos += 1
endfor
unlet s:pos
function! s:Base64Encode(str)
" Respect current file encoding
- let to_be_encoded=a:str
+ let input = a:str
let encoded = ''
- while len(to_be_encoded) > 2
- let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
- \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )+char2nr(to_be_encoded[1])/16]
- \ . g:unimpaired_base64_chars[4 *(char2nr(to_be_encoded[1])%16)+char2nr(to_be_encoded[2])/64]
- \ . g:unimpaired_base64_chars[char2nr(to_be_encoded[2])%64]
- let to_be_encoded = to_be_encoded[3:]
+ while len(input) > 2
+ let encoded .= s:base64_chars[char2nr(input[0])/4]
+ \ . s:base64_chars[16*(char2nr(input[0])%4 )+char2nr(input[1])/16]
+ \ . s:base64_chars[4 *(char2nr(input[1])%16)+char2nr(input[2])/64]
+ \ . s:base64_chars[char2nr(input[2])%64]
+ let input = input[3:]
endwhile
- if len(to_be_encoded) == 2
- let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
- \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )+char2nr(to_be_encoded[1])/16]
- \ . g:unimpaired_base64_chars[4 *(char2nr(to_be_encoded[1])%16)]
- \ . g:unimpaired_base64_filler
- elseif len(to_be_encoded) == 1
- let encoded .= g:unimpaired_base64_chars[char2nr(to_be_encoded[0])/4]
- \ . g:unimpaired_base64_chars[16*(char2nr(to_be_encoded[0])%4 )]
- \ . g:unimpaired_base64_filler
- \ . g:unimpaired_base64_filler
+ if len(input) == 2
+ let encoded .= s:base64_chars[char2nr(input[0])/4]
+ \ . s:base64_chars[16*(char2nr(input[0])%4 )+char2nr(input[1])/16]
+ \ . s:base64_chars[4 *(char2nr(input[1])%16)]
+ \ . s:base64_filler
+ elseif len(input) == 1
+ let encoded .= s:base64_chars[char2nr(input[0])/4]
+ \ . s:base64_chars[16*(char2nr(input[0])%4 )]
+ \ . s:base64_filler
+ \ . s:base64_filler
endif
return encoded
endfunction
function! s:Base64Decode(str)
- let to_be_decoded=a:str
- let decoded=''
- while len(to_be_decoded) % 4 == 0
- let decoded .= nr2char( 4 * (g:unimpaired_base64_reverse_map[to_be_decoded[0]] )
- \ + (g:unimpaired_base64_reverse_map[to_be_decoded[1]] / 16 )
- \)
- if to_be_decoded[2] !=# g:unimpaired_base64_filler
- let decoded .= nr2char(16 * (g:unimpaired_base64_reverse_map[to_be_decoded[1]] % 16) + (g:unimpaired_base64_reverse_map[to_be_decoded[2]]/4))
- if to_be_decoded[3] !=# g:unimpaired_base64_filler
- let decoded .= nr2char(64 * (g:unimpaired_base64_reverse_map[to_be_decoded[2]] % 4) + g:unimpaired_base64_reverse_map[to_be_decoded[3]])
- endif
- endif
- let to_be_decoded = to_be_decoded[4:]
- if empty(to_be_decoded)
- return decoded
- endif
- endwhile
+ if len(a:str) % 4 != 0
+ return a:str
+ endif
+ let input = a:str
+ let decoded = ''
+ while !empty(input)
+ let decoded .= nr2char(4 * s:base64_lookup[input[0]] + (s:base64_lookup[input[1]] / 16))
+ if input[2] !=# s:base64_filler
+ let decoded .= nr2char(16 * (s:base64_lookup[input[1]] % 16) + (s:base64_lookup[input[2]]/4))
+ if input[3] !=# s:base64_filler
+ let decoded .= nr2char(64 * (s:base64_lookup[input[2]] % 4) + s:base64_lookup[input[3]])
+ endif
+ endif
+ let input = input[4:]
+ endwhile
+ return decoded
endfunction
" HTML entities {{{2

0 comments on commit 76a22b6

Please sign in to comment.