Z85

Z85 Encoding functions. More...

Functions

byte * pcp_padfour (byte *src, size_t srclen, size_t *dstlen)
 Zero-pad some input data. More...
 
size_t pcp_unpadfour (byte *src, size_t srclen)
 Unpad padded input data. More...
 
byte * pcp_z85_decode (PCPCTX *ptx, char *z85block, size_t *dstlen)
 Decode data from Z85 encoding. More...
 
char * pcp_z85_encode (byte *raw, size_t srclen, size_t *dstlen, int doblock)
 Encode data to Z85 encoding. More...
 
char * pcp_readz85file (PCPCTX *ptx, FILE *infile)
 Read a Z85 encoded file. More...
 
char * pcp_readz85string (PCPCTX *ptx, byte *input, size_t bufsize)
 Read a Z85 encoded string. More...
 
uint8_t is_utf8 (const byte *bytes)
 Check if a binary array is utf8. More...
 
size_t _buffer_is_binary (byte *buf, size_t len)
 Determine if a buffer is binary or ascii. More...
 
uint8_t _parse_zchar (Buffer *z, uint8_t c, uint8_t is_comment)
 Determine if a char is a Z85 character. More...
 

Detailed Description

Z85 Encoding functions.

The Z85 encoding format is described here: ZeroMQ Spec.32. It's part of ZeroMQ. Z85 is based on ASCII85 with a couple of modifications (portability, readability etc).

To fulfil the requirements of the ZeroMQ Z85 functions, PCP does some additional preparations of raw input before actually doing the encoding, since the input for zmq_z85_encode() must be divisible by 4. Therefore we pad the input with zeroes and remove them after decoding.

Function Documentation

size_t _buffer_is_binary ( byte *  buf,
size_t  len 
)

Determine if a buffer is binary or ascii.

Parameters
[in]bufThe buffer to check.
[in]lenLen of the buffer.
Returns
Returns 0 if the input is ascii or a number > 0 if it contains binary data.
uint8_t _parse_zchar ( Buffer z,
uint8_t  c,
uint8_t  is_comment 
)

Determine if a char is a Z85 character.

Parameters
[out]zBuffer object where to put the char if it's z85 and not inside a comment.
[in]cThe char to check.
[in]is_commentDenotes if we're currently within a comment.
Returns
Returns 1 if a comment starts or 0 otherwise.
uint8_t is_utf8 ( const byte *  bytes)

Check if a binary array is utf8.

Based on http://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c by Christoph Gärtner

Modified to only check for one utf8 char. The given sequence must have at least 4 bytes length. No boundary checks are being made.

Parameters
[in]bytesA byte sequence with 4 or more bytes length.
Returns
Returns 0 if the sequence is not utf8 or a number greater than 1 indicating the size of the utf8 char.
byte* pcp_padfour ( byte *  src,
size_t  srclen,
size_t *  dstlen 
)

Zero-pad some input data.

This function allocates new memory for the returned data. It puts the original pointer into it and adds a number of zeros so that the result has a size divisable by 4.

Parameters
[in]srcUnpadded data.
[in]srclenSize of unpadded data.
[out]dstlenReturned size of padded data (pointer to int).
Returns
Returns a pointer to the padded data.
char* pcp_readz85file ( PCPCTX ptx,
FILE *  infile 
)

Read a Z85 encoded file.

Reads a file and returns the raw Z85 encoded string. It ignores newlines, comments and Headerstrings.

Parameters
[in]ptxpcp context object.
[in]infileFILE stream to read from.
Returns
Raw Z85 encoded string with comments, headers and newlines removed.
char* pcp_readz85string ( PCPCTX ptx,
byte *  input,
size_t  bufsize 
)

Read a Z85 encoded string.

Parses the given input string and returns the raw Z85 encoded string. It ignores newlines, comments and Headerstrings.

Parameters
[in]ptxpcp context object.
[in]inputZ85 encoded string.
[in]bufsizeSize of the string.
Returns
Raw Z85 encoded string with comments, headers and newlines removed.
size_t pcp_unpadfour ( byte *  src,
size_t  srclen 
)

Unpad padded input data.

It just calculates the size of the unpadded result (size - all trailing zeroes). Doesn't allocate any memory or modify anything.

Parameters
[in]srcPadded data.
[in]srclenSize of padded data.
Returns
Returns the unpadded size of the data.
byte* pcp_z85_decode ( PCPCTX ptx,
char *  z85block,
size_t *  dstlen 
)

Decode data from Z85 encoding.

The input z85block may contain newlines which will be removed.

Parameters
[in]ptxpcp context object.
[in]z85blockThe Z85 encoded string.
[in]dstlenReturned size of decoded data (pointer to int).
Returns
Returns a newly allocated pointer to the decoded data. If decoding failed, returns NULL. Check fatals_if_any().
char* pcp_z85_encode ( byte *  raw,
size_t  srclen,
size_t *  dstlen,
int  doblock 
)

Encode data to Z85 encoding.

Beside Z85 encoding it also adds a newline everiy 72 characters. It allocates the memory for the returned char pointer. The caller is responsible the free() it.

Parameters
[in]rawPointer to raw data.
[in]srclenSize of the data.
[out]dstlenReturned size of encoded data (pointer to int).
[in]doblockIf set to 1, turn the encoded data into a 72 chars wide block.
Returns
Returns a string (char array) containing the Z85 encoded data.