class CZTop::Z85

Represents a CZMQ::FFI::Zarmour in Z85 mode.

Use this class to encode to and from the Z85 encoding scheme. @see rfc.zeromq.org/spec:32

Public Class Methods

decode(input) click to toggle source

Same as {Z85#decode}, but without the need to create an instance first.

@param input [String] Z85 encoded data @return [String] original data as binary string @raise [ArgumentError] if input length isn't divisible by 5 with no

remainder

@raise [SystemCallError] if this fails

# File lib/cztop/z85.rb, line 31
def decode(input)
  default.decode(input)
end
encode(input) click to toggle source

Same as {Z85#encode}, but without the need to create an instance first.

@param input [String] possibly binary input data @return [String] Z85 encoded data as ASCII string @raise [ArgumentError] if input length isn't divisible by 4 with no

remainder

@raise [SystemCallError] if this fails

# File lib/cztop/z85.rb, line 19
def encode(input)
  default.encode(input)
end
new() click to toggle source
# File lib/cztop/z85.rb, line 44
def initialize
  attach_ffi_delegate(CZMQ::FFI::Zarmour.new)
  ffi_delegate.set_mode(CZMQ::FFI::Zarmour::MODE_Z85)
end

Private Class Methods

default() click to toggle source

Default instance of {Z85}. @return [Z85] memoized default instance

# File lib/cztop/z85.rb, line 39
def default
  @default ||= Z85.new
end

Public Instance Methods

decode(input) click to toggle source

Decodes from Z85. @param input [String] Z85 encoded data @return [String] original data as binary string @raise [ArgumentError] if input length isn't divisible by 5 with no

remainder

@raise [SystemCallError] if this fails

# File lib/cztop/z85.rb, line 71
def decode(input)
  raise ArgumentError, "wrong input length" if input.bytesize % 5 > 0
  zchunk = ffi_delegate.decode(input)
  raise_zmq_err if zchunk.null?
  decoded_string = zchunk.data.read_string(zchunk.size - 1)
  return decoded_string
end
encode(input) click to toggle source

Encodes to Z85. @param input [String] possibly binary input data @return [String] Z85 encoded data as ASCII string @raise [ArgumentError] if input length isn't divisible by 4 with no

remainder

@raise [SystemCallError] if this fails

# File lib/cztop/z85.rb, line 55
def encode(input)
  raise ArgumentError, "wrong input length" if input.bytesize % 4 > 0
  input = input.dup.force_encoding(Encoding::BINARY)
  ptr = ffi_delegate.encode(input, input.bytesize)
  raise_zmq_err if ptr.null?
  z85 = ptr.read_string
  z85.encode!(Encoding::ASCII)
  return z85
end