Class: Cabriolet::KWAJ::Compressor

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

Overview

Compressor creates KWAJ compressed files

KWAJ files support multiple compression methods:

  • NONE: Direct copy

  • XOR: XOR with 0xFF “encryption”

  • SZDD: LZSS compression

  • MSZIP: DEFLATE compression

KWAJ headers contain optional fields controlled by flag bits:

  • Uncompressed length (4 bytes)

  • Filename (up to 9 bytes, null-terminated)

  • File extension (up to 4 bytes, null-terminated)

  • Extra data (2 bytes length + variable data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil) ⇒ Compressor

Initialize a new KWAJ compressor

Parameters:

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

    Custom I/O system or nil for default



25
26
27
# File 'lib/cabriolet/kwaj/compressor.rb', line 25

def initialize(io_system = nil)
  @io_system = io_system || System::IOSystem.new
end

Instance Attribute Details

#io_systemObject (readonly)

Returns the value of attribute io_system.



19
20
21
# File 'lib/cabriolet/kwaj/compressor.rb', line 19

def io_system
  @io_system
end

Instance Method Details

#compress(input_file, output_file, **options) ⇒ Integer

Compress a file to KWAJ format

Parameters:

  • input_file (String)

    Path to input file

  • output_file (String)

    Path to output KWAJ file

  • options (Hash)

    Compression options

Options Hash (**options):

  • :compression (Symbol)

    Compression type (:none, :xor, :szdd, :mszip), default: :szdd

  • :include_length (Boolean)

    Include uncompressed length in header

  • :filename (String)

    Original filename to embed

  • :extra_data (String)

    Extra data to include

Returns:

  • (Integer)

    Bytes written to output file

Raises:

  • (Error)

    if compression fails



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
74
75
76
77
78
79
80
# File 'lib/cabriolet/kwaj/compressor.rb', line 42

def compress(input_file, output_file, **options)
  compression_type = options.fetch(:compression, :szdd)
  include_length = options.fetch(:include_length, false)
  filename = options[:filename]
  extra_data = options[:extra_data]

  validate_compression_type(compression_type)

  input_handle = @io_system.open(input_file, Constants::MODE_READ)
  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)

  begin
    # Get input size
    input_size = @io_system.seek(input_handle, 0, Constants::SEEK_END)
    @io_system.seek(input_handle, 0, Constants::SEEK_START)

    # Write header
    header_bytes = write_header(
      output_handle,
      compression_type,
      input_size,
      include_length,
      filename,
      extra_data,
    )

    # Compress data
    compressed_bytes = compress_data_stream(
      compression_type,
      input_handle,
      output_handle,
    )

    header_bytes + compressed_bytes
  ensure
    @io_system.close(input_handle) if input_handle
    @io_system.close(output_handle) if output_handle
  end
end

#compress_data(data, output_file, **options) ⇒ Integer

Compress data from memory to KWAJ format

Parameters:

  • data (String)

    Input data to compress

  • output_file (String)

    Path to output KWAJ file

  • options (Hash)

    Compression options

Options Hash (**options):

  • :compression (Symbol)

    Compression type (:none, :xor, :szdd, :mszip), default: :szdd

  • :include_length (Boolean)

    Include uncompressed length in header

  • :filename (String)

    Original filename to embed

  • :extra_data (String)

    Extra data to include

Returns:

  • (Integer)

    Bytes written to output file

Raises:

  • (Error)

    if compression fails



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cabriolet/kwaj/compressor.rb', line 95

def compress_data(data, output_file, **options)
  compression_type = options.fetch(:compression, :szdd)
  include_length = options.fetch(:include_length, false)
  filename = options[:filename]
  extra_data = options[:extra_data]

  validate_compression_type(compression_type)

  input_handle = System::MemoryHandle.new(data)
  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)

  begin
    # Write header
    header_bytes = write_header(
      output_handle,
      compression_type,
      data.bytesize,
      include_length,
      filename,
      extra_data,
    )

    # Compress data
    compressed_bytes = compress_data_stream(
      compression_type,
      input_handle,
      output_handle,
    )

    header_bytes + compressed_bytes
  ensure
    @io_system.close(output_handle) if output_handle
  end
end