class HMap::MapFileReader

hmap file reader

Attributes

bucktes[R]

@return [Hash<HMap::HMapBucket => HMap::HMapBucketStr>] an array of the file's bucktes @note bucktes are provided in order of ascending offset.

filename[R]

@return [String, nil] the filename loaded from, or nil if loaded from a binary

string
header[R]

@return [HMap::HMapHeader]

swapped[R]

@return true/false the swapped of the mapfile

Public Class Methods

new(path) click to toggle source
# 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_fields() click to toggle source

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() click to toggle source

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
populate_and_check_magic() click to toggle source

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
populate_buckets() { |bucket| ... } click to toggle source

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
populate_hmap_header() click to toggle source

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