Class: RELI::Malbolge

Inherits:
Object
  • Object
show all
Defined in:
lib/reli/malbolge.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Malbolge

Returns a new instance of Malbolge.



3
4
# File 'lib/reli/malbolge.rb', line 3

def initialize(options = {})
end

Instance Method Details

#crypt1(m) ⇒ Object



64
65
66
67
68
# File 'lib/reli/malbolge.rb', line 64

def crypt1(m)
  m = get m if m.class == Array
  "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI.v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha"
    .split(//)[(m - 33 + @c).modulo 94].ord
end

#crypt2(m) ⇒ Object



70
71
72
73
# File 'lib/reli/malbolge.rb', line 70

def crypt2(m)
  "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@"
    .split(//)[((get m) - 33).modulo 94].ord
end

#crz(a, b) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/reli/malbolge.rb', line 75

def crz(a, b)
  a = get a if a.class == Array
  [a, b].map { |item|
    sprintf("%010d", item.to_s(3).to_i).split(//).map { |char| char.ord - 0x30 }
  }.transpose.map { |items|
    ([1, 0, 0, 1, 0, 2, 2, 2, 1][items[0] * 3 + items[1]] + 0x30).chr
  }.join.to_i(3)
end

#get(addr) ⇒ Object



12
13
14
15
16
# File 'lib/reli/malbolge.rb', line 12

def get(addr)
  a = addr[0]
  @memory[a] = get_orid a if a >= @memory.length
  return @memory[a]
end

#get_orid(a) ⇒ Object

lazy evaluation



7
8
9
10
# File 'lib/reli/malbolge.rb', line 7

def get_orid(a)
  @oridinal_memory[a] = crz get_orid(a - 2), get_orid(a - 1) if a >= @oridinal_memory.length
  return @oridinal_memory[a]
end

#inc(n) ⇒ Object



89
90
91
92
93
# File 'lib/reli/malbolge.rb', line 89

def inc(n)
  n += 1
  n = 0 if n == 3**10
  return n
end

#rotrObject



84
85
86
87
# File 'lib/reli/malbolge.rb', line 84

def rotr()
  @memory[@d] += ((get [@d]).modulo 3) * 3 ** 10
  @memory[@d] /= 3
end

#run(code_) ⇒ Object



18
19
20
21
22
23
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
# File 'lib/reli/malbolge.rb', line 18

def run(code_)
  code = code_.split(//u).map(&:ord)
  @a = 0
  @c = 0
  @d = 0

  # load step
  @buffer = []
  @oridinal_memory = @memory = []
  for char in code
    case crypt1 char
    when 42, 47, 60, 105, 106, 111, 112, 118
      @oridinal_memory[@c] = char
    else
      # error
      next
    end
    @c += 1
  end

  # run
  @c = 0
  while (crypt1 [@c]) != 118
    case crypt1 [@c]
    when 106
      @d = get [@d]
    when 104
      @c = get [@d]
    when 42
      rotr
      @a = get [@d]
    when 112
      @a = @memory[@d] = crz([@d], @a)
    when 60
      @buffer[@buffer.length] = @a
    when 47
      @a = $stdin.getc.ord
    end
    @memory[@c] = crypt2 [@c]
    @c = inc @c
    @d = inc @d
  end

  @buffer.map { |a| (a.modulo 256).chr}.join
end