Class: ExtractTtc::Utilities::ChecksumCalculator
- Inherits:
-
Object
- Object
- ExtractTtc::Utilities::ChecksumCalculator
- Defined in:
- lib/extract_ttc/utilities/checksum_calculator.rb
Overview
ChecksumCalculator provides stateless utility methods for calculating font file checksums.
This class implements the TrueType/OpenType checksum algorithm which sums all uint32 values in a file. The checksum is used to verify file integrity and calculate the checksumAdjustment value stored in the ‘head’ table.
Class Method Summary collapse
-
.calculate_adjustment(file_checksum) ⇒ Integer
Calculate the checksum adjustment value for the ‘head’ table.
-
.calculate_file_checksum(file_path) ⇒ Integer
Calculate the checksum of an entire font file.
Class Method Details
.calculate_adjustment(file_checksum) ⇒ Integer
Calculate the checksum adjustment value for the ‘head’ table.
The checksum adjustment is stored at offset 8 in the ‘head’ table and is calculated as: CHECKSUM_ADJUSTMENT_MAGIC - file_checksum. This value ensures that the checksum of the entire font file equals the magic number.
54 55 56 |
# File 'lib/extract_ttc/utilities/checksum_calculator.rb', line 54 def self.calculate_adjustment(file_checksum) (Constants::CHECKSUM_ADJUSTMENT_MAGIC - file_checksum) & 0xFFFFFFFF end |
.calculate_file_checksum(file_path) ⇒ Integer
Calculate the checksum of an entire font file.
The checksum is calculated by summing all uint32 (4-byte) values in the file. Files that are not multiples of 4 bytes are padded with zeros. The sum is masked to 32 bits to prevent overflow.
35 36 37 38 39 |
# File 'lib/extract_ttc/utilities/checksum_calculator.rb', line 35 def self.calculate_file_checksum(file_path) File.open(file_path, "rb") do |file| calculate_checksum_from_io(file) end end |