module Fit4Ruby::CRC16
Public Instance Methods
compute_crc(io, start_pos, end_pos)
click to toggle source
# File lib/fit4ruby/CRC16.rb, line 29 def compute_crc(io, start_pos, end_pos) crc_table = [ 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 ] io.seek(start_pos) crc = 0 while io.pos < end_pos if io.eof? raise IOError, "Premature end of file" end byte = io.readbyte 0.upto(1) do |i| tmp = crc_table[crc & 0xF] crc = (crc >> 4) & 0x0FFF crc = crc ^ tmp ^ crc_table[(byte >> (4 * i)) & 0xF] end end crc end
write_crc(io, start_pos, end_pos)
click to toggle source
# File lib/fit4ruby/CRC16.rb, line 18 def write_crc(io, start_pos, end_pos) # Compute the checksum over the data section of the file and append it # to the file. Ideally, we should compute the CRC from data in memory # instead of the file data. crc = compute_crc(io, start_pos, end_pos) io.seek(end_pos) BinData::Uint16le.new(crc).write(io) crc end