Module: Tins::FileBinary
- Defined in:
- lib/tins/file_binary.rb
Overview
A module for detecting and analyzing binary files based on content patterns
This module provides functionality to determine whether a file contains binary data by examining its content against various thresholds for null bytes and high-order bits. It’s useful for identifying files that are not plain text, such as images, executables, or other binary formats.
The detection is performed by scanning a specified portion of the file and calculating the percentage of bytes that match binary criteria.
Defined Under Namespace
Modules: ClassMethods, Constants
Class Attribute Summary collapse
-
.default_options ⇒ Object
Accessor for default options hash.
Class Method Summary collapse
-
.included(modul) ⇒ Object
Module inclusion hook that extends the including class with ClassMethods.
Instance Method Summary collapse
-
#ascii?(options = {}) ⇒ Boolean?
Returns the logical opposite of binary? - true if file is not binary (ASCII/text).
-
#binary?(options = {}) ⇒ Boolean?
Determines if a file is considered binary based on content analysis.
Class Attribute Details
.default_options ⇒ Object
Accessor for default options hash
33 34 35 |
# File 'lib/tins/file_binary.rb', line 33 def @default_options end |
Class Method Details
.included(modul) ⇒ Object
Module inclusion hook that extends the including class with ClassMethods
114 115 116 117 118 119 |
# File 'lib/tins/file_binary.rb', line 114 def self.included(modul) modul.instance_eval do extend ClassMethods end super end |
Instance Method Details
#ascii?(options = {}) ⇒ Boolean?
Returns the logical opposite of binary? - true if file is not binary (ASCII/text)
104 105 106 107 108 109 |
# File 'lib/tins/file_binary.rb', line 104 def ascii?( = {}) case binary?() when true then false when false then true end end |
#binary?(options = {}) ⇒ Boolean?
Determines if a file is considered binary based on content analysis
A file is classified as binary if either:
-
The percentage of null bytes exceeds the configured threshold
-
The percentage of binary bytes (bytes with high-order bit set) exceeds the configured threshold
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tins/file_binary.rb', line 74 def binary?( = {}) = FileBinary..merge() old_pos = tell seek [:offset], Constants::SEEK_SET data = read [:buffer_size] !data or data.empty? and return nil data_size = data.size data.count(Constants::ZERO_RE).to_f / data_size > [:percentage_zeros] / 100.0 and return true data.count(Constants::BINARY_RE).to_f / data_size > [:percentage_binary] / 100.0 ensure old_pos and seek old_pos, Constants::SEEK_SET end |