class HMap::MapFileReader
hmap file reader
Attributes
@return [Hash<HMap::HMapBucket => HMap::HMapBucketStr>] an array of the file's bucktes @note bucktes are provided in order of ascending offset.
@return [String, nil] the filename loaded from, or nil if loaded from a binary
string
@return [HMap::HMapHeader]
@return true/false the swapped of the mapfile
Public Class Methods
# File lib/cocoapods-hmap/hmap_reader.rb, line 38 def initialize(path) raise ArgumentError, "#{path}: no such file" unless File.file?(path) @filename = path @raw_data = File.open(@filename, 'rb', &:read) populate_fields puts description end
Public Instance Methods
Populate the instance's fields with the raw HMap
data. @return [void] @note This method is public, but should (almost) never need to be called.
# File lib/cocoapods-hmap/hmap_reader.rb, line 50 def populate_fields @header = populate_hmap_header string_t = @raw_data[header.strings_offset..-1] @bucktes = populate_buckets do |bucket| bucket_s = bucket.to_a.map do |key| string_t[key..-1].match(/[^\0]+/)[0] end HMapBucketStr.new(*bucket_s) end end
Private Instance Methods
description
# File lib/cocoapods-hmap/hmap_reader.rb, line 103 def description sum = " Header map: #{filename}\n" + header.description bucktes.each_with_index do |buckte_h, index| sum += "\t- Bucket: #{index}" + Utils.safe_encode(buckte_h.values[0].description, 'UTF-8') unless buckte_h.nil? sum end sum end
Read just the file's magic number and check its validity. @return [Integer] the magic @raise [MagicError] if the magic is not valid HMap
magic @api private
# File lib/cocoapods-hmap/hmap_reader.rb, line 78 def populate_and_check_magic magic = @raw_data[0..3].unpack1('N') raise MagicError, magic unless Utils.magic?(magic) version = @raw_data[4..5].unpack1('n') @swapped = Utils.swapped_magic?(magic, version) end
All buckets in the file. @return [Array<HMap::HMapBucket>] an array of buckets @api private
# File lib/cocoapods-hmap/hmap_reader.rb, line 89 def populate_buckets bucket_offset = header.class.bytesize bucktes = [] header.num_buckets.times do |i| bucket = HMapBucket.new_from_bin(swapped, @raw_data[bucket_offset, HMapBucket.bytesize]) bucket_offset += HMapBucket.bytesize next if bucket.key == HEADER_CONST[:HMAP_EMPTY_BUCKT_KEY] bucktes[i] = { bucket => yield(bucket) } end bucktes end
The file's HMapheader structure. @return [HMap::HMapHeader] @raise [TruncatedFileError] if the file is too small to have a valid header @api private
# File lib/cocoapods-hmap/hmap_reader.rb, line 67 def populate_hmap_header raise TruncatedFileError if @raw_data.size < HMapHeader.bytesize + 8 * HMapBucket.bytesize populate_and_check_magic HMapHeader.new_from_bin(swapped, @raw_data[0, HMapHeader.bytesize]) end