Class: CZTop::Z85

Inherits:
Object
  • Object
show all
Extended by:
HasFFIDelegate::ClassMethods
Includes:
HasFFIDelegate
Defined in:
lib/cztop/z85.rb

Overview

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

Use this class to encode to and from the Z85 encoding scheme.

Direct Known Subclasses

Padded

Defined Under Namespace

Classes: Padded, Pipe

Instance Attribute Summary

Attributes included from HasFFIDelegate

#ffi_delegate

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasFFIDelegate::ClassMethods

ffi_delegate, from_ffi_delegate

Methods included from HasFFIDelegate

#attach_ffi_delegate, #from_ffi_delegate, #raise_zmq_err, raise_zmq_err, #to_ptr

Constructor Details

#initializeZ85

Returns a new instance of Z85.


44
45
46
47
# 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

Class Method Details

.decode(input) ⇒ String

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

Parameters:

  • input (String)

    Z85 encoded data

Returns:

  • (String)

    original data as binary string

Raises:

  • (ArgumentError)

    if input length isn't divisible by 5 with no remainder

  • (SystemCallError)

    if this fails


31
32
33
# File 'lib/cztop/z85.rb', line 31

def decode(input)
  default.decode(input)
end

.defaultZ85 (private)

Default instance of CZTop::Z85.

Returns:

  • (Z85)

    memoized default instance


39
40
41
# File 'lib/cztop/z85.rb', line 39

def default
  @default ||= Z85.new
end

.encode(input) ⇒ String

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

Parameters:

  • input (String)

    possibly binary input data

Returns:

  • (String)

    Z85 encoded data as ASCII string

Raises:

  • (ArgumentError)

    if input length isn't divisible by 4 with no remainder

  • (SystemCallError)

    if this fails


19
20
21
# File 'lib/cztop/z85.rb', line 19

def encode(input)
  default.encode(input)
end

Instance Method Details

#decode(input) ⇒ String

Decodes from Z85.

Parameters:

  • input (String)

    Z85 encoded data

Returns:

  • (String)

    original data as binary string

Raises:

  • (ArgumentError)

    if input length isn't divisible by 5 with no remainder

  • (SystemCallError)

    if this fails


71
72
73
74
75
76
77
# 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) ⇒ String

Encodes to Z85.

Parameters:

  • input (String)

    possibly binary input data

Returns:

  • (String)

    Z85 encoded data as ASCII string

Raises:

  • (ArgumentError)

    if input length isn't divisible by 4 with no remainder

  • (SystemCallError)

    if this fails


55
56
57
58
59
60
61
62
63
# 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