Sha256: 7558403af9fc3164553fe6042a4344ecc42444d0590365c6c76050ca980924ea

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# encoding: ascii-8bit

# Copyright 2023 OpenC3, Inc.
# All Rights Reserved.
#
# Licensed for Evaluation and Educational Use
#
# This file may only be used commercially under the terms of a commercial license
# purchased from OpenC3, Inc.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# The development of this software was funded in-whole or in-part by MethaneSAT LLC.

class CfdpChecksum
  def initialize
    @checksum = 0
  end

  def add(offset, data)
    front_pad_bytes = offset % 4
    if front_pad_bytes != 0
      data = ("\x00" * front_pad_bytes) << data
    end
    end_pad_bytes = 4 - data.length % 4
    if end_pad_bytes != 4
      data = data + ("\x00" * end_pad_bytes)
    end
    values = data.unpack('N*')
    values.each do |value|
      @checksum += value
    end
    return @checksum
  end

  # Expected to be calculated as we go using add, so file unused
  def checksum(file, full_checksum_needed)
    if full_checksum_needed
      file.rewind
      data = file.read
      @checksum = 0
      add(0, data)
    end
    return @checksum & 0xFFFFFFFF
  end

  def check(file, other_checksum, full_checksum_needed)
    checksum(file, full_checksum_needed) == other_checksum
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
openc3-cosmos-cfdp-1.0.2 microservices/CFDP/app/models/cfdp_checksum.rb
openc3-cosmos-cfdp-1.0.1 microservices/CFDP/app/models/cfdp_checksum.rb
openc3-cosmos-cfdp-1.0.0 microservices/CFDP/app/models/cfdp_checksum.rb