Module: Base85::Ascii85

Extended by:
ModuleMethods
Defined in:
lib/base85/ascii85.rb

Overview

Ascii85 base85 encoding. This encoding uses ! to u ASCII characters to encode data. z is used to represent 32-bit 0 value. The encoded string is surrounded by <~ and ~> markers.

Constant Summary collapse

BASE_ASCII_VALUE =

ASCII value for character !

"!".ord
SPECIAL_Z =

Ascii85 defines z as a special character

true
START_MARKER =

ASCII85 start marker

"<~"
END_MARKER =

ASCII85 end marker

"~>"

Constants included from ModuleMethods

ModuleMethods::LUT

Class Method Summary collapse

Methods included from ModuleMethods

decode, encode, encode_word

Class Method Details

.decode(data) ⇒ String

Decode data using Ascii85 encoding

Parameters:

  • data (String)

    Encoded string, may be prefixed by <~ and suffixed by ~>

Returns:

  • (String)

    Decoded data

Raises:



51
52
53
54
55
# File 'lib/base85/ascii85.rb', line 51

def self.decode(data)
  start = data.start_with?(START_MARKER) ? 2 : 0
  stop = data.end_with?(END_MARKER) ? -3 : -1
  super(data[start..stop])
end

.decode_char(char) ⇒ Integer

Decode a character from Ascii85 and give its index in alphabet

Parameters:

  • char (String)

    a acharacter from Ascii85 alphabet

Returns:

  • (Integer)

    index of this character in alphabet

Raises:

  • DecodeError char is not known in alphabet



38
39
40
41
42
43
44
45
# File 'lib/base85/ascii85.rb', line 38

def self.decode_char(char)
  case char
  when "!".."u" # rubocop:disable Lint/MixedCaseRange
    char.ord - BASE_ASCII_VALUE
  else
    raise DecodeError, "unknown character '#{char}'"
  end
end

.encode(data) ⇒ String

Encode data using Ascii85 encoding

Parameters:

  • data (String)

Returns:

  • (String)

    encoded data, surrounded by <~ and ~> markers



30
31
32
# File 'lib/base85/ascii85.rb', line 30

def self.encode(data)
  "#{START_MARKER}#{super}#{END_MARKER}"
end

.encode_value(value) ⇒ String

Encode a value (0..84 integer) to a Ascii85 character

Parameters:

  • value (Integer)

Returns:

  • (String)


23
24
25
# File 'lib/base85/ascii85.rb', line 23

def self.encode_value(value)
  (BASE_ASCII_VALUE + value).chr
end