Class: Cabriolet::LIT::ContentEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/lit/content_encoder.rb

Overview

Encodes LIT content data (NameList, manifest)

Class Method Summary collapse

Class Method Details

.build_manifest_data(manifest) ⇒ String

Build manifest data from manifest structure

Parameters:

  • manifest (Hash)

    Manifest structure with mappings

Returns:

  • (String)

    Binary manifest data



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cabriolet/lit/content_encoder.rb', line 38

def self.build_manifest_data(manifest)
  data = +""

  # For simplicity: single directory entry
  data += [0].pack("C") # Empty directory name = end of directories

  # Write 4 groups
  terminator = [0].pack("C")
  4.times do |group|
    # Get mappings for this group
    group_mappings = manifest[:mappings].select { |m| m[:group] == group }

    data += [group_mappings.size].pack("V")

    group_mappings.each do |mapping|
      data += [mapping[:offset]].pack("V")

      # Internal name
      data += [mapping[:internal_name].bytesize].pack("C")
      data += mapping[:internal_name]

      # Original name
      data += [mapping[:original_name].bytesize].pack("C")
      data += mapping[:original_name]

      # Content type
      data += [mapping[:content_type].bytesize].pack("C")
      data += mapping[:content_type]

      # Terminator
      data += terminator
    end
  end

  data
end

.build_namelist_data(sections) ⇒ String

Build NameList data from sections

Parameters:

  • sections (Array<Hash>)

    Sections array

Returns:

  • (String)

    Binary NameList data



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cabriolet/lit/content_encoder.rb', line 11

def self.build_namelist_data(sections)
  data = +""
  data += [0].pack("v") # Initial field

  # Write number of sections
  data += [sections.size].pack("v")

  # Write each section name
  null_terminator = [0].pack("v")
  sections.each do |section|
    name = section[:name]
    # Convert to UTF-16LE
    name_utf16 = name.encode("UTF-16LE").force_encoding("ASCII-8BIT")
    name_length = name_utf16.bytesize / 2

    data += [name_length].pack("v")
    data += name_utf16
    data += null_terminator
  end

  data
end