diff --git a/include/pcp/zmq_z85.h b/include/pcp/zmq_z85.h index e1b0b9a..7f1d4cc 100644 --- a/include/pcp/zmq_z85.h +++ b/include/pcp/zmq_z85.h @@ -20,12 +20,15 @@ #ifndef __ZMQ_Z85_H_INCLUDED__ #define __ZMQ_Z85_H_INCLUDED__ +#define __STDC_LIMIT_MACROS + #include #include #include #include +#include -uint8_t *zmq_z85_decode (uint8_t *dest, char *string); -char *zmq_z85_encode (char *dest, uint8_t *data, size_t size); +uint8_t *zmq_z85_decode (uint8_t *dest, const char *string); +char *zmq_z85_encode (char *dest, const uint8_t *data, size_t size); #endif diff --git a/libpcp/z85.c b/libpcp/z85.c index aa347ee..0ce5c33 100755 --- a/libpcp/z85.c +++ b/libpcp/z85.c @@ -231,6 +231,9 @@ char *pcp_z85_encode(byte *raw, size_t srclen, size_t *dstlen, int doblock) { zlen = (outlen * 5 / 4) + 1; z85 = ucmalloc(zlen + 5); /* plus space for pad blob */ z85 = zmq_z85_encode(z85, padded, outlen); + if (z85 == NULL) { + perror("zmq_z85_encode *z85 failed!"); + } if(srclen %4 != 0) free(padded); @@ -239,7 +242,9 @@ char *pcp_z85_encode(byte *raw, size_t srclen, size_t *dstlen, int doblock) { padblob = ucmalloc(6); rpad = urmalloc(4); rpad[3] = (int)padlen; - zmq_z85_encode(padblob, rpad, 4); + if(zmq_z85_encode(padblob, rpad, 4) == NULL) { + perror("zmq_z85_encode *padblob failed!"); + } /* append pad blob to encoded output */ memcpy(&z85[zlen-1], padblob, 5); diff --git a/libpcp/zmq_z85.c b/libpcp/zmq_z85.c index 3c4fa6a..fc8345c 100644 --- a/libpcp/zmq_z85.c +++ b/libpcp/zmq_z85.c @@ -1,5 +1,5 @@ /* - Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file + Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file This file is part of 0MQ. @@ -28,21 +28,22 @@ static char encoder [85 + 1] = { "}@%$#" }; -/* Maps base 85 to base 256 */ -/* We chop off lower 32 and higher 128 ranges */ +/* Maps base 85 to base 256 + We chop off lower 32 and higher 128 ranges + 0xFF denotes invalid characters within this range */ static uint8_t decoder [96] = { - 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, - 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, + 0xFF, 0x44, 0xFF, 0x54, 0x53, 0x52, 0x48, 0xFF, + 0x4B, 0x4C, 0x46, 0x41, 0xFF, 0x3F, 0x3E, 0x45, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47, + 0x08, 0x09, 0x40, 0xFF, 0x49, 0x42, 0x4A, 0x47, 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, - 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00, - 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, + 0x3B, 0x3C, 0x3D, 0x4D, 0xFF, 0x4E, 0x43, 0xFF, + 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, - 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 + 0x21, 0x22, 0x23, 0x4F, 0xFF, 0x50, 0xFF, 0xFF }; /* -------------------------------------------------------------------------- */ @@ -50,53 +51,72 @@ static uint8_t decoder [96] = { /* size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns */ /* dest. Size must be a multiple of 4. */ -char *zmq_z85_encode (char *dest, uint8_t *data, size_t size) +/* -------------------------------------------------------------------------- + Encode a binary frame as a string; destination string MUST be at least + size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns + dest. Size must be a multiple of 4. + Returns NULL and sets errno = EINVAL for invalid input. */ + +char *zmq_z85_encode (char *dest, const uint8_t *data, size_t size) { - if (size % 4 != 0) - return NULL; /* !assert */ - - unsigned int char_nbr = 0; - unsigned int byte_nbr = 0; - uint32_t value = 0; - while (byte_nbr < size) { - /* Accumulate value in base 256 (binary) */ - value = value * 256 + data [byte_nbr++]; - if (byte_nbr % 4 == 0) { - /* Output value in base 85 */ - unsigned int divisor = 85 * 85 * 85 * 85; - while (divisor) { - dest [char_nbr++] = encoder [value / divisor % 85]; - divisor /= 85; - } - value = 0; + if (size % 4 != 0) { + errno = EINVAL; + return NULL; } - } - if (char_nbr != size * 5 / 4) - return NULL; /* !assert */ - - dest [char_nbr] = 0; - return dest; + unsigned int char_nbr = 0; + unsigned int byte_nbr = 0; + uint32_t value = 0; + while (byte_nbr < size) { + // Accumulate value in base 256 (binary) + value = value * 256 + data [byte_nbr++]; + if (byte_nbr % 4 == 0) { + // Output value in base 85 + unsigned int divisor = 85 * 85 * 85 * 85; + while (divisor) { + dest [char_nbr++] = encoder [value / divisor % 85]; + divisor /= 85; + } + value = 0; + } + } + if (char_nbr != size * 5 / 4) { + return NULL; /* !assert */ + } + dest [char_nbr] = 0; + return dest; } - -/* -------------------------------------------------------------------------- */ -/* Decode an encoded string into a binary frame; dest must be at least */ -/* strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string) */ -/* must be a multiple of 5. */ +// -------------------------------------------------------------------------- +// Decode an encoded string into a binary frame; dest must be at least +// strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string) +// must be a multiple of 5. +// Returns NULL and sets errno = EINVAL for invalid input. -uint8_t *zmq_z85_decode (uint8_t *dest, char *string) +uint8_t *zmq_z85_decode (uint8_t *dest, const char *string) { - if (strlen (string) % 5 != 0) - return NULL; /* !assert */ unsigned int byte_nbr = 0; unsigned int char_nbr = 0; uint32_t value = 0; - size_t string_len = strlen (string); - while (char_nbr < string_len) { - /* Accumulate value in base 85 */ - value = value * 85 + decoder [(uint8_t) string [char_nbr++] - 32]; + while (string[char_nbr]) { + // Accumulate value in base 85 + if (UINT32_MAX / 85 < value) { + // Invalid z85 encoding, represented value exceeds 0xffffffff + goto error_inval; + } + value *= 85; + uint8_t index = string [char_nbr++] - 32; + if (index >= sizeof(decoder)) { + // Invalid z85 encoding, character outside range + goto error_inval; + } + uint32_t summand = decoder [index]; + if (summand == 0xFF || summand > (UINT32_MAX - value)) { + // Invalid z85 encoding, invalid character or represented value exceeds 0xffffffff + goto error_inval; + } + value += summand; if (char_nbr % 5 == 0) { - /* Output value in base 256 */ + // Output value in base 256 unsigned int divisor = 256 * 256 * 256; while (divisor) { dest [byte_nbr++] = value / divisor % 256; @@ -105,7 +125,15 @@ uint8_t *zmq_z85_decode (uint8_t *dest, char *string) value = 0; } } - if (byte_nbr != strlen (string) * 4 / 5) - return NULL; /* !assert */ + if (char_nbr % 5 != 0) { + goto error_inval; + } + if (byte_nbr != strlen (string) * 4 / 5) { + goto error_inval; + } return dest; + +error_inval: + errno = EINVAL; + return NULL; }