Class: KoreFileFormat::Decompressor

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

Instance Method Summary collapse

Instance Method Details

#decompress(data) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kore_fileformat/decompressor.rb', line 5

def decompress(data)
  raise ArgumentError, "data cannot be nil" if data.nil?

  data_bytes = data.is_a?(String) ? data.bytes : data
  input = FFI::MemoryPointer.new(:uchar, data_bytes.length)
  input.put_array_of_uchar(0, data_bytes)

  output_size = (data_bytes.length * 4).to_i
  output = FFI::MemoryPointer.new(:uchar, output_size)
  decompressed_size = FFI::MemoryPointer.new(:int)

  result = Native.decompress_data(
    input, data_bytes.length,
    output, output_size,
    decompressed_size
  )

  raise CompressionError, "Decompression failed with code: #{result}" if result != 0

  output.get_array_of_uchar(0, decompressed_size.read_int).pack("c*")
end