Class: Origami::Filter::ASCIIHex

Inherits:
Object
  • Object
show all
Includes:
Origami::Filter
Defined in:
lib/origami/filters/ascii.rb

Overview

Class representing a filter used to encode and decode data written into hexadecimal.

Constant Summary collapse

EOD =

:nodoc:

">"

Constants included from Origami::Filter

A85, AHx, CCF, Fl, RL

Instance Method Summary collapse

Methods included from Origami::Filter

included, #initialize

Instance Method Details

#decode(string) ⇒ Object

Decodes given data writen into upcase hexadecimal representation.

string

The data to decode.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/origami/filters/ascii.rb', line 46

def decode(string)
  input = string.include?(EOD) ? string[0...string.index(EOD)] : string
  digits = input.delete(" \f\t\r\n\0")

  # Ensure every digit is in the hexadecimal charset.
  unless digits =~ /^\h*$/
    digits = digits.match(/^\h*/).to_s

    raise InvalidASCIIHexStringError.new("Invalid characters", input_data: string, decoded_data: [digits].pack('H*'))
  end

  [digits].pack "H*"
end

#encode(stream) ⇒ Object

Encodes given data into upcase hexadecimal representation.

stream

The data to encode.



38
39
40
# File 'lib/origami/filters/ascii.rb', line 38

def encode(stream)
  stream.unpack("H*").join.upcase
end