Class: Ciphr::Functions::Radix::Radix

Inherits:
InvertibleFunction show all
Defined in:
lib/ciphr/functions/base_radix.rb

Instance Attribute Summary

Attributes inherited from InvertibleFunction

#invert

Attributes inherited from Function

#args, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InvertibleFunction

invertable?

Methods inherited from Function

inherited, #initialize, invertable?, #prepend, #read

Constructor Details

This class inherits a constructor from Ciphr::Functions::Function

Class Method Details

.alignedObject



161
162
163
# File 'lib/ciphr/functions/base_radix.rb', line 161

def self.aligned
  :right
end

.paramsObject



169
170
171
# File 'lib/ciphr/functions/base_radix.rb', line 169

def self.params
  [:input]
end

.variantsObject



165
166
167
# File 'lib/ciphr/functions/base_radix.rb', line 165

def self.variants
  (2..36).map{|r| [["r#{r}","rad#{r}","radix#{r}"], {:radix => r}]}
end

Instance Method Details

#applyObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ciphr/functions/base_radix.rb', line 173

def apply
  radix = options[:radix]
  input = @args[0]
  if !invert     
    num = 0      
    while chunk = input.read(1)
      num = (num << 8) + chunk.bytes.to_a[0]
    end
    Proc.new do
      begin
        num && num.to_s(radix)
      ensure
        num = nil
      end
    end
  else
    num = input.read().to_i(radix)
    bytes = []
    while num > 0
      bytes.unshift(num & 0xff)
      num = num >> 8
    end
    Proc.new do
      begin
        bytes && bytes.pack("c*")
      ensure
        bytes = nil
      end
    end
  end
end