Class: ByteBoozer2::Cruncher

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/byteboozer2/cruncher.rb

Overview

This class implements ByteBoozer's 2.0 crunching algorithm.

Defined Under Namespace

Classes: Matches, NewNode, RleInfo

Constant Summary collapse

DECRUNCHER =
[
  0x0b, 0x08, 0x00, 0x00, 0x9e, 0x32, 0x30, 0x36, 0x31, 0x00, 0x00, 0x00, 0x78, 0xa9, 0x34, 0x85,
  0x01, 0xa2, 0xb7, 0xbd, 0x1e, 0x08, 0x95, 0x0f, 0xca, 0xd0, 0xf8, 0x4c, 0x10, 0x00, 0xbd, 0xd6,
  0x07, 0x9d, 0x00, 0xff, 0xe8, 0xd0, 0xf7, 0xc6, 0x12, 0xc6, 0x15, 0xa5, 0x12, 0xc9, 0x07, 0xb0,
  0xed, 0x20, 0xa0, 0x00, 0xb0, 0x17, 0x20, 0x8e, 0x00, 0x85, 0x36, 0xa0, 0x00, 0x20, 0xad, 0x00,
  0x91, 0x77, 0xc8, 0xc0, 0x00, 0xd0, 0xf6, 0x20, 0x83, 0x00, 0xc8, 0xf0, 0xe4, 0x20, 0x8e, 0x00,
  0xaa, 0xe8, 0xf0, 0x71, 0x86, 0x7b, 0xa9, 0x00, 0xe0, 0x03, 0x2a, 0x20, 0x9b, 0x00, 0x20, 0x9b,
  0x00, 0xaa, 0xb5, 0xbf, 0xf0, 0x07, 0x20, 0x9b, 0x00, 0xb0, 0xfb, 0x30, 0x07, 0x49, 0xff, 0xa8,
  0x20, 0xad, 0x00, 0xae, 0xa0, 0xff, 0x65, 0x77, 0x85, 0x74, 0x98, 0x65, 0x78, 0x85, 0x75, 0xa0,
  0x00, 0xb9, 0xad, 0xde, 0x99, 0x00, 0x00, 0xc8, 0xc0, 0x00, 0xd0, 0xf5, 0x20, 0x83, 0x00, 0xd0,
  0xa0, 0x18, 0x98, 0x65, 0x77, 0x85, 0x77, 0x90, 0x02, 0xe6, 0x78, 0x60, 0xa9, 0x01, 0x20, 0xa0,
  0x00, 0x90, 0x05, 0x20, 0x9b, 0x00, 0x10, 0xf6, 0x60, 0x20, 0xa0, 0x00, 0x2a, 0x60, 0x06, 0xbe,
  0xd0, 0x08, 0x48, 0x20, 0xad, 0x00, 0x2a, 0x85, 0xbe, 0x68, 0x60, 0xad, 0xed, 0xfe, 0xe6, 0xae,
  0xd0, 0x02, 0xe6, 0xaf, 0x60, 0xa9, 0x37, 0x85, 0x01, 0x4c, 0x00, 0x00, 0x80, 0xdf, 0xfb, 0x00,
  0x80, 0xef, 0xfd, 0x80, 0xf0
].freeze
DECRUNCHER_LENGTH =
DECRUNCHER.length
MEM_SIZE =
0x10000
NUM_BITS_SHORT_0 =
3
NUM_BITS_SHORT_1 =
6
NUM_BITS_SHORT_2 =
8
NUM_BITS_SHORT_3 =
10
NUM_BITS_LONG_0 =
4
NUM_BITS_LONG_1 =
7
NUM_BITS_LONG_2 =
10
NUM_BITS_LONG_3 =
13
LEN_SHORT_0 =
1 << NUM_BITS_SHORT_0
LEN_SHORT_1 =
1 << NUM_BITS_SHORT_1
LEN_SHORT_2 =
1 << NUM_BITS_SHORT_2
LEN_SHORT_3 =
1 << NUM_BITS_SHORT_3
LEN_LONG_0 =
1 << NUM_BITS_LONG_0
LEN_LONG_1 =
1 << NUM_BITS_LONG_1
LEN_LONG_2 =
1 << NUM_BITS_LONG_2
LEN_LONG_3 =
1 << NUM_BITS_LONG_3
MAX_OFFSET =
LEN_LONG_3
MAX_OFFSET_SHORT =
LEN_SHORT_3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Cruncher

Returns a new instance of Cruncher.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/byteboozer2/cruncher.rb', line 26

def initialize(data, options = {})
  @data       = data
  @executable = options[:executable] || false
  @relocated  = options[:relocated]  || false
  @address    = options[:address]    || 0x0000

  raise ArgumentError unless valid?
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



11
12
13
# File 'lib/byteboozer2/cruncher.rb', line 11

def address
  @address
end

#resultObject (readonly)

Returns the value of attribute result.



11
12
13
# File 'lib/byteboozer2/cruncher.rb', line 11

def result
  @result
end

Class Method Details

.crunchObject



22
23
24
# File 'lib/byteboozer2/cruncher.rb', line 22

def self.crunch(*)
  new(*).crunch
end

Instance Method Details

#crunchObject



99
100
101
# File 'lib/byteboozer2/cruncher.rb', line 99

def crunch
  @result if crunched?
end

#crunched?Boolean

Returns:

  • (Boolean)


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/byteboozer2/cruncher.rb', line 35

def crunched?
  @ibuf_size = @data.length - 2

  # Load ibuf and clear context
  @ibuf     = @data[2..]
  @context  = Array.new(@ibuf_size) { new_node }
  @link     = Array.new(@ibuf_size) { 0 }
  @rle_info = Array.new(@ibuf_size) { RleInfo.new(value: 0, value_after: 0, length: 0) }

  setup_help_structures
  find_matches
  @obuf = Array.new(MEM_SIZE) { 0 }
  margin = write_output

  pack_len = @put
  file_len = @put
  decr_len = 0
  if @executable
    decr_len = DECRUNCHER_LENGTH
    file_len += decr_len + 2
  else
    file_len += 4
  end

  @result = Array.new(file_len) { 0 }

  if @executable
    start_address  = 0x10000 - pack_len
    transf_address = file_len + 0x6ff

    decr_code[0x1f] = transf_address & 0xff # Transfer from...
    decr_code[0x20] = transf_address >> 8
    decr_code[0xbc] = start_address & 0xff # Depack from...
    decr_code[0xbd] = start_address >> 8
    decr_code[0x85] = @data[0] # Depack to...
    decr_code[0x86] = @data[1]
    decr_code[0xca] = @address & 0xff # Jump to...
    decr_code[0xcb] = @address >> 8

    @result[0] = 0x01
    @result[1] = 0x08

    @result[2, decr_len] = decr_code

    @result[2 + decr_len, @put] = @obuf[0, @put]
  else # Not executable...
    # Experimantal decision of start address
    # start_address = 0xfffa - pack_len - 2
    start_address = (@data[1] << 8) | @data[0]
    start_address += (@ibuf_size - pack_len - 2 + margin)

    start_address = @address - pack_len - 2 if @relocated

    @result[0] = start_address & 0xff # Load address
    @result[1] = start_address >> 8
    @result[2] = @data[0] # Depack to address
    @result[3] = @data[1]

    @result[4, @put] = @obuf[0, @put]
  end

  true
end