Class: Cabriolet::KWAJ::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/kwaj/parser.rb

Overview

Parser reads and parses KWAJ file headers

KWAJ files support multiple compression methods and have variable-length headers with optional fields determined by flag bits.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system) ⇒ Parser

Initialize a new parser

Parameters:



15
16
17
# File 'lib/cabriolet/kwaj/parser.rb', line 15

def initialize(io_system)
  @io_system = io_system
end

Instance Attribute Details

#io_systemObject (readonly)

Returns the value of attribute io_system.



10
11
12
# File 'lib/cabriolet/kwaj/parser.rb', line 10

def io_system
  @io_system
end

Instance Method Details

#parse(filename) ⇒ Models::KWAJHeader

Parse a KWAJ file and return header information

Parameters:

  • filename (String)

    Path to the KWAJ file

Returns:

Raises:

  • (Errors::ParseError)

    if the file is not a valid KWAJ



24
25
26
27
28
29
# File 'lib/cabriolet/kwaj/parser.rb', line 24

def parse(filename)
  handle = @io_system.open(filename, Constants::MODE_READ)
  header = parse_handle(handle)
  @io_system.close(handle)
  header
end

#parse_handle(handle) ⇒ Models::KWAJHeader

Parse KWAJ header from an already-open handle

Parameters:

Returns:

Raises:

  • (Errors::ParseError)

    if not a valid KWAJ



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cabriolet/kwaj/parser.rb', line 36

def parse_handle(handle)
  # Read base header (14 bytes)
  base_data = @io_system.read(handle, 14)
  raise ParseError, "Cannot read KWAJ header" if base_data.bytesize < 14

  # Parse base header
  base = Binary::KWAJStructures::BaseHeader.read(base_data)

  # Verify signature
  unless Binary::KWAJStructures.valid_signature?(
    base.signature1, base.signature2
  )
    raise ParseError, "Invalid KWAJ signature"
  end

  # Create header model
  header = Models::KWAJHeader.new
  header.comp_type = base.comp_method
  header.data_offset = base.data_offset
  header.headers = base.flags

  # Parse optional headers based on flags
  parse_optional_headers(handle, header)

  header
end