Class: HMap::HMapFileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ HMapFileReader

Returns a new instance of HMapFileReader.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 20

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

Instance Attribute Details

#bucktesHash<HMap::HMapBucket => HMap::HMapBucketStr> (readonly)

Note:

bucktes are provided in order of ascending offset.

Returns an array of the file’s bucktes.



18
19
20
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 18

def bucktes
  @bucktes
end

#filenameString? (readonly)



6
7
8
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 6

def filename
  @filename
end

#headerHMap::HMapHeader (readonly)



14
15
16
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 14

def header
  @header
end

#swappedObject (readonly)



11
12
13
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 11

def swapped
  @swapped
end

Instance Method Details

#descriptionObject



82
83
84
85
86
87
88
89
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 82

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_magicInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read just the file’s magic number and check its validity.

Raises:

  • (MagicError)

    if the magic is not valid HMap magic



58
59
60
61
62
63
64
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 58

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_bucketsArray<HMap::HMapBucket>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All buckets in the file.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 69

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_fieldsvoid

Note:

This method is public, but should (almost) never need to be called.

This method returns an undefined value.

Populate the instance’s fields with the raw HMap data.



32
33
34
35
36
37
38
39
40
41
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 32

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

#populate_hmap_headerHMap::HMapHeader

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The file’s HMapheader structure.

Raises:

  • (TruncatedFileError)

    if the file is too small to have a valid header



47
48
49
50
51
52
# File 'lib/cocoapods-hmap-prebuilt/hmap_file_reader.rb', line 47

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