Module: Sabrina::Lz77

Defined in:
lib/sabrina/lz77.rb

Overview

An utility module for compressing and decompressing data in a GBA-compliant LZ77 format.

This has mostly been ported directly from Gen III Hacking Suite‘s corresponding tool by thekaratekid552 and contributors.

Credit goes to thekaratekid552, Jambo51, Shiny Quagsire, DoesntKnowHowToPlay, Interdpth.

Class Method Summary collapse

Class Method Details

.compress(data) ⇒ String

Compresses the supplied stream of bytes as GBA-compliant Lz77 data.

Parameters:

  • data (String)

Returns:

  • (String)

    the compressed data.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sabrina/lz77.rb', line 81

def compress(data)
  compressed = "\x10"
  compressed <<
    Bytestream.from_hex(format('%06X', data.length)).to_b.reverse

  index = 0
  w = 0xFFF
  window = ''
  lookahead = ''

  loop do
    bits = ''
    check = nil
    current_chunk = ''

    8.times do
      window = (index < w ? data[0, index] : data[(index % w)..index])
      lookahead = data[index..-1]

      if lookahead.nil? || lookahead.empty?
        unless bits.empty?
          while bits.length < 8
            bits << '0'
            current_chunk << "\x00"
          end
          compressed <<
            Bytestream.from_hex(format('%02x', bits.to_i(2))).to_b <<
            current_chunk
        end
        break
      end

      check = window.index(lookahead[0..2])
      if check
        bits << '1'
        length = 2
        store_length = 0
        store_check = 0
        while check && length < 18
          store_length = length
          length += 1
          store_check = check
          check = window.index(lookahead[0, length])
        end
        index += store_length
        store_length -= 3
        position = window.length - 1 - store_check
        store_length = store_length << 12
        current_chunk <<
          Bytestream.from_hex(format('%04X', (store_length | position))).to_b
      else
        index += 1
        bits << '0'
        current_chunk << lookahead[0]
      end
    end # 8.times

    if lookahead.nil? || lookahead.empty?
      unless bits.empty?
        while bits.length < 8
          bits << '0'
          current_chunk << "\x00"
        end
        compressed <<
          Bytestream.from_hex(format('%02x', bits.to_i(2))).to_b <<
          current_chunk
      end
      break
    end

    compressed <<
      Bytestream.from_hex(format('%02x', bits.to_i(2))).to_b <<
      current_chunk
  end # loop

  compressed << "\x00" until compressed.length % 4 == 0
  compressed
end

.uncompress(rom, offset) ⇒ Hash

Decompresses data from offset in the ROM file as Lz77. This returns a hash consisting of the uncompressed :stream and the :original_length of the compressed data, but the latter value is currently inaccurate and should not be relied upon for wiping old data.

Parameters:

  • rom (Rom)
  • offset (Integer)

Returns:

  • (Hash)

    contains the uncompressed data as :stream, the estimated original compressed length as :original_length, and the original compressed data as :original_stream.



24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sabrina/lz77.rb', line 24

def uncompress(rom, offset)
  f = rom.file
  f.seek(offset)

  test = f.read(1)
  unless test == "\x10"
    fail "Offset #{offset} in #{rom.filename} does not appear" \
      " to be lz77 data. (Found #{ format('%02X', test.unpack('C')) })"
  end

  target_length = Bytestream.from_bytes(f.read(3).reverse).to_i

  data = ''

  loop do
    bit_field = format('%08b', f.read(1).unpack('C').first)

    bit_field.each_char do |x|
      if data.length >= target_length
        compressed_length = f.pos - offset
        compressed_length += 1 until compressed_length % 4 == 0
        original = rom.read(offset, compressed_length)
        return {
          stream: data.slice(0, target_length),
          original_stream: original,
          original_length: compressed_length
        }
      end
      next data << f.read(1) if x == '0'

      r5 = f.read(1).unpack('C').first
      store = r5
      r6 = 3
      r3 = (r5 >> 4) + r6
      r6 = store
      r5 = r6 & 0xF
      r12 = r5 << 8
      r6 = f.read(1).unpack('C').first
      r5 = r6 | r12
      r12 = r5 + 1

      r3.times do
        unless (r5 = data[-r12])
          fail "Decompression failed at offset #{offset} in" \
            " #{rom.filename}. Possible corrupted file or unknown" \
            " compression method. #{[bit_field, r3, r5, r6, r12]}"
        end
        data << r5
      end
    end
  end
end