Class: Cabriolet::KWAJ::Decompressor

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

Overview

Decompressor is the main interface for KWAJ file operations

KWAJ files support multiple compression methods:

  • NONE: Direct copy

  • XOR: XOR with 0xFF then copy

  • SZDD: LZSS compression

  • LZH: LZSS with Huffman (not fully implemented)

  • MSZIP: DEFLATE compression

Constant Summary collapse

DEFAULT_BUFFER_SIZE =

Input buffer size for decompression

2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil) ⇒ Decompressor

Initialize a new KWAJ decompressor

Parameters:

  • io_system (System::IOSystem, nil) (defaults to: nil)

    Custom I/O system or nil for default



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

def initialize(io_system = nil)
  @io_system = io_system || System::IOSystem.new
  @parser = Parser.new(@io_system)
  @buffer_size = DEFAULT_BUFFER_SIZE
end

Instance Attribute Details

#buffer_sizeObject

Returns the value of attribute buffer_size.



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

def buffer_size
  @buffer_size
end

#io_systemObject (readonly)

Returns the value of attribute io_system.



14
15
16
# File 'lib/cabriolet/kwaj/decompressor.rb', line 14

def io_system
  @io_system
end

#parserObject (readonly)

Returns the value of attribute parser.



14
15
16
# File 'lib/cabriolet/kwaj/decompressor.rb', line 14

def parser
  @parser
end

Instance Method Details

#auto_output_filename(input_path, header) ⇒ String

Generate output filename from input filename and header

Parameters:

Returns:

  • (String)

    Suggested output filename



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cabriolet/kwaj/decompressor.rb', line 116

def auto_output_filename(input_path, header)
  # Use embedded filename if available
  if header.filename && !header.filename.empty?
    dir = ::File.dirname(input_path)
    return ::File.join(dir, header.filename)
  end

  # Fall back to removing extension
  base = ::File.basename(input_path, ".*")
  dir = ::File.dirname(input_path)
  ::File.join(dir, base)
end

#close(_header) ⇒ void

This method returns an undefined value.

Close a KWAJ file (no-op for compatibility)

Parameters:



43
44
45
46
47
# File 'lib/cabriolet/kwaj/decompressor.rb', line 43

def close(_header)
  # No resources to free in the header itself
  # File handles are managed separately during extraction
  nil
end

#decompress(input_path, output_path = nil) ⇒ Integer

One-shot decompression from input file to output file

Parameters:

  • input_path (String)

    Path to compressed KWAJ file

  • output_path (String, nil) (defaults to: nil)

    Path to output file, or nil to auto-detect

Returns:

  • (Integer)

    Number of bytes written

Raises:

  • (Errors::ParseError)

    if input is not valid KWAJ

  • (Errors::DecompressionError)

    if decompression fails



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cabriolet/kwaj/decompressor.rb', line 95

def decompress(input_path, output_path = nil)
  # Parse header
  header = open(input_path)

  # Auto-detect output filename if not provided
  output_path ||= auto_output_filename(input_path, header)

  # Extract
  bytes_written = extract(header, input_path, output_path)

  # Close (no-op but kept for API consistency)
  close(header)

  bytes_written
end

#extract(header, filename, output_path) ⇒ Integer

Extract a KWAJ file to output

Parameters:

  • header (Models::KWAJHeader)

    KWAJ header from open()

  • filename (String)

    Input filename

  • output_path (String)

    Where to write the decompressed file

Returns:

  • (Integer)

    Number of bytes written

Raises:

  • (Errors::DecompressionError)

    if decompression fails



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cabriolet/kwaj/decompressor.rb', line 56

def extract(header, filename, output_path)
  raise ArgumentError, "Header must not be nil" unless header
  raise ArgumentError, "Output path must not be nil" unless output_path

  input_handle = @io_system.open(filename, Constants::MODE_READ)
  output_handle = @io_system.open(output_path, Constants::MODE_WRITE)

  begin
    # Seek to compressed data start
    @io_system.seek(
      input_handle, header.data_offset, Constants::SEEK_START
    )

    # Decompress based on compression type
    bytes_written = decompress_data(
      header, input_handle, output_handle
    )

    # Verify decompressed size if known
    if header.length && bytes_written != header.length
      warn "[Cabriolet] WARNING: decompressed #{bytes_written} bytes, " \
           "expected #{header.length} bytes"
    end

    bytes_written
  ensure
    @io_system.close(input_handle) if input_handle
    @io_system.close(output_handle) if output_handle
  end
end

#open(filename) ⇒ Models::KWAJHeader

Open and parse a KWAJ file

Parameters:

  • filename (String)

    Path to the KWAJ file

Returns:

Raises:

  • (Errors::ParseError)

    if the file is not a valid KWAJ



35
36
37
# File 'lib/cabriolet/kwaj/decompressor.rb', line 35

def open(filename)
  @parser.parse(filename)
end