Class: CZTop::Z85::Padded

Inherits:
CZTop::Z85 show all
Defined in:
lib/cztop/z85/padded.rb

Overview

Note:

Warning: Z85 doesn’t have a standardized padding procedure. So other implementations won’t automatically recognize and chop off the padding. Only use this if you really need padding, like when you can’t guarantee the input for #encode is always a multiple of 4 bytes.

Z85 with simple padding. This allows you to #encode input of any length.

Padding Scheme

If the data to be encoded is empty (0 bytes), it is encoded to the empty string, just like in Z85.

Otherwise, a small padding sequence of 1 to 4 (identical) bytes is appended to the binary data. Its last byte denotes the number of padding bytes. This padding is done even if the length of the binary input is a multiple of 4 bytes. This is similar to PKCS#7 padding.

+----------------------------------------+------------+
|                 binary data            |   padding  |
|           any number of bytes          |  1-4 bytes |
+----------------------------------------+------------+

The resulting blob is encoded using #encode.

When decoding, #decode does the inverse. After decoding, it checks the last byte to determine the number of bytes of padding used, and chops those off.

Instance Attribute Summary

Attributes included from HasFFIDelegate

#ffi_delegate

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CZTop::Z85

#initialize

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

This class inherits a constructor from CZTop::Z85

Class Method Details

.decode(input) ⇒ String

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

Parameters:

  • input (String)

    Z85 encoded data (including padding, or empty string)

Returns:

  • (String)

    original data as binary string

Raises:

  • (SystemCallError)

    if this fails



56
57
58
# File 'lib/cztop/z85/padded.rb', line 56

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, including encoded length and padding

Raises:

  • (SystemCallError)

    if this fails



44
45
46
# File 'lib/cztop/z85/padded.rb', line 44

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

Instance Method Details

#decode(input) ⇒ String

Decodes from Z85 with padding.

Parameters:

  • input (String)

    Z85 encoded data (including padding, or empty string)

Returns:

  • (String)

    original data as binary string

Raises:

  • (SystemCallError)

    if this fails



96
97
98
99
100
101
102
103
104
105
# File 'lib/cztop/z85/padded.rb', line 96

def decode(input)
  return super if input.empty?

  decoded = super

  # last byte contains number of padding bytes
  padding_bytes = decoded.byteslice(-1).ord

  decoded.byteslice(0...-padding_bytes)
end

#encode(input) ⇒ String

Encododes to Z85, with padding if needed.

Parameters:

  • input (String)

    possibly binary input data

Returns:

  • (String)

    Z85 encoded data as ASCII string, including padding

Raises:

  • (SystemCallError)

    if this fails



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cztop/z85/padded.rb', line 75

def encode(input)
  return super if input.empty?

  padding_bytes = 4 - (input.bytesize % 4)

  # if 0, make it 4. we MUST append padding.
  padding_bytes = 4 if padding_bytes.zero?

  # generate and append padding
  padding = [padding_bytes].pack('C') * padding_bytes

  super("#{input}#{padding}")
end