Class: Ffc16

Inherits:
Object
  • Object
show all
Defined in:
lib/ffc16.rb

Constant Summary collapse

MODX =
4102

Class Method Summary collapse

Class Method Details

.checksum(buf) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ffc16.rb', line 5

def self.checksum(buf)

  inx = 0
  end_buf = buf.length

  c0 = 0
  c1 = 0

  loop do

    end_chunk = (inx + MODX < end_buf) ?  inx + MODX : end_buf

    loop do

      c0 += 0xff & buf[inx].ord
      c1 += c0

      inx += 1
      break unless inx < end_chunk
    end

    c0 %= 255
    c1 %= 255

    break unless inx < end_buf

  end

  c1 << 8 | c0
end