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, #to_ptr

Constructor Details

#initializeZ85

Returns a new instance of Z85.



50
51
52
53
# File 'lib/cztop/z85.rb', line 50

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



36
37
38
# File 'lib/cztop/z85.rb', line 36

def decode(input)
  default.decode(input)
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



23
24
25
# File 'lib/cztop/z85.rb', line 23

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



80
81
82
83
84
85
86
87
# File 'lib/cztop/z85.rb', line 80

def decode(input)
  return '' if input.empty?
  raise ArgumentError, 'wrong input length' if (input.bytesize % 5).positive?

  zchunk = ffi_delegate.decode(input)
  raise_zmq_err if zchunk.null?
  zchunk.data.read_string(zchunk.size - 1)
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



62
63
64
65
66
67
68
69
70
71
# File 'lib/cztop/z85.rb', line 62

def encode(input)
  raise ArgumentError, 'wrong input length' if (input.bytesize % 4).positive?

  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)
  z85
end