Class: Owasp::Esapi::Codec::BaseCodec

Inherits:
Object
  • Object
show all
Defined in:
lib/codec/base_codec.rb

Constant Summary collapse

START_CODE_POINT =

start range of valid code points

0x000
END_CODE_POINT =

ending range of valid code points

0x10fff
@@hex_codes =

:nodoc:

[]

Instance Method Summary collapse

Instance Method Details

#decode(input) ⇒ Object

Decode a String that was encoded using the encode method in this Class



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/codec/base_codec.rb', line 66

def decode(input)
  decoded_string = ''
  seekable = PushableString.new(input.dup)
  while seekable.next?
    t = decode_char(seekable)
    if t.nil?
      decoded_string << seekable.next
    else
      decoded_string << t
    end
  end
  decoded_string
end

#decode_char(input) ⇒ Object

Returns the decoded version of the next character from the input string and advances the current character in the PushableString. If the current character is not encoded, this method MUST reset the PushableString.



83
84
85
# File 'lib/codec/base_codec.rb', line 83

def decode_char(input)
  input
end

#encode(immune, input) ⇒ Object

Encode a String so that it can be safely used in a specific context. immune is an arry or string that contains character tobe ignore



39
40
41
42
43
44
45
46
47
# File 'lib/codec/base_codec.rb', line 39

def encode(immune, input)
  return nil if input.nil?
  encoded_string = ''
  encoded_string.encode!(Encoding::UTF_8)
  input.encode(Encoding::UTF_8).chars do |c|
    encoded_string << encode_char(immune,c)
  end
  encoded_string
end

#encode_char(immune, input) ⇒ Object

Default implementation that should be overridden in specific codecs.



50
51
52
# File 'lib/codec/base_codec.rb', line 50

def encode_char(immune, input)
  input
end

#hex(c) ⇒ Object

Helper method for codecs to get the hex value of a character



55
56
57
58
59
60
61
62
63
# File 'lib/codec/base_codec.rb', line 55

def hex(c)
  return nil if c.nil?
  b = c[0].ord
  if b < 0xff
    @@hex_codes[b]
  else
    b.to_h
  end
end

#min(a, b) ⇒ Object

Basic min method



88
89
90
91
92
93
94
# File 'lib/codec/base_codec.rb', line 88

def min(a,b) #:nodoc:
  if a > b
    return b
  else
    return a
  end
end