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.



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

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.



40
41
42
# File 'lib/origami/filters/ascii.rb', line 40

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