Permalink
Browse files

tlib#hash#CRC32B(): Implementation of CRC32B checksum in vimscript (u…

…sed only if +ruby isn't available)
  • Loading branch information...
1 parent c41691d commit ca2c5b48c8f731ad5f1ce70fa28f4cda22caa2fc @tomtom committed Dec 18, 2012
Showing with 39 additions and 40 deletions.
  1. +1 −1 autoload/tlib/bitwise.vim
  2. +38 −39 autoload/tlib/hash.vim
@@ -1,6 +1,6 @@
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
-" @Revision: 93
+" @Revision: 95
function! tlib#bitwise#Num2Bits(num) "{{{3
View
@@ -1,53 +1,52 @@
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
-" @Revision: 203
+" @Revision: 209
-function! tlib#hash#CRC32(chars) "{{{3
+function! tlib#hash#CRC32B(chars) "{{{3
if has('ruby')
let rv = ''
ruby << EOR
require 'zlib'
VIM::command('let rv = "%08x"' % Zlib.crc32(VIM::evaluate("a:chars")))
EOR
- " elseif has('python')
- " elseif has('perl')
- " elseif has('tcl')
+ " elseif has('python')
+ " elseif has('perl')
+ " elseif has('tcl')
else
- throw "tlib: No implementation for CRC32"
- " if !exists('s:crc_table')
- " let sum = 0.0
- " for exponent in [0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26, 32]
- " let sum += tlib#bitwise#Bits2Num(repeat([0], 32 - exponent) + [1])
- " endfor
- " let divisor = tlib#bitwise#Num2Bits(sum)
- " " TLogVAR divisor
- " let s:crc_table = []
- " for octet in range(256)
- " let remainder = tlib#bitwise#Num2Bits(octet)
- " for i in range(8)
- " if get(remainder, i) != 0
- " let remainder = tlib#bitwise#XOR(remainder, tlib#bitwise#ShiftLeft(copy(divisor), i), "bits")
- " endif
- " endfor
- " let remainder = tlib#bitwise#ShiftRight(remainder, 8)
- " " TLogVAR octet, remainder
- " call add(s:crc_table, remainder)
- " endfor
- " endif
- " let xFFFF_FFFF = repeat([1], 32)
- " let crc = tlib#bitwise#XOR([0], xFFFF_FFFF, 'bits')
- " for char in split(a:chars, '\zs')
- " let octet = char2nr(char)
- " let v0 = tlib#bitwise#ShiftRight(crc, 8)
- " let r1 = tlib#bitwise#AND(crc, xFFFF_FFFF, 'bits')
- " let i1 = tlib#bitwise#XOR(r1, octet, 'bits')
- " let i2 = tlib#bitwise#Bits2Num(tlib#bitwise#AND(i1, 0xff, 'bits'))
- " let r2 = s:crc_table[i2]
- " let crc = tlib#bitwise#XOR(r1, r2, 'bits')
- " endfor
- " let crc = tlib#bitwise#XOR(crc, xFFFF_FFFF, 'bits')
- " let rv = tlib#bitwise#Bits2Num(crc, 16)
+ " based on http://rosettacode.org/wiki/CRC-32
+ if !exists('s:crc_table')
+ let sum = 0.0
+ for exponent in [0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26, 32]
+ let exp = tlib#bitwise#Bits2Num(repeat([0], 32 - exponent) + [1], 10.0)
+ let sum += exp
+ endfor
+ let divisor = tlib#bitwise#Num2Bits(sum)
+ let s:crc_table = []
+ for octet in range(256)
+ let remainder = tlib#bitwise#Num2Bits(octet)
+ for i in range(8)
+ if get(remainder, i) != 0
+ let remainder = tlib#bitwise#XOR(remainder, tlib#bitwise#ShiftLeft(divisor, i), "bits")
+ endif
+ endfor
+ let remainder = tlib#bitwise#ShiftRight(remainder, 8)
+ call add(s:crc_table, remainder)
+ endfor
+ endif
+ let xFFFF_FFFF = repeat([1], 32)
+ let crc = tlib#bitwise#XOR([0], xFFFF_FFFF, 'bits')
+ for char in split(a:chars, '\zs')
+ let octet = char2nr(char)
+ let r1 = tlib#bitwise#ShiftRight(crc, 8)
+ let i0 = tlib#bitwise#AND(crc, xFFFF_FFFF, 'bits')
+ let i1 = tlib#bitwise#XOR(i0, octet, 'bits')
+ let i2 = tlib#bitwise#Bits2Num(tlib#bitwise#AND(i1, 0xff, 'bits'))
+ let r2 = s:crc_table[i2]
+ let crc = tlib#bitwise#XOR(r1, r2, 'bits')
+ endfor
+ let crc = tlib#bitwise#XOR(crc, xFFFF_FFFF, 'bits')
+ let rv = tlib#bitwise#Bits2Num(crc, 16)
endif
return rv
endf

0 comments on commit ca2c5b4

Please sign in to comment.