From 576e3ca5e0f0d67f673e1ef30ec1071d19cb6b3a Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Sun, 15 Sep 2013 20:07:33 +0200 Subject: [PATCH] Added z85 codec to ZMQ API * Removed redundant Z85 code and include files from project * Simplified use of headers in test cases (now they all just use testutil.hpp) * Export zmq_z85_encode() and zmq_z85_decode() in API * Added man pages for these two functions --- doc/Makefile.am | 3 +- doc/zmq_curve.txt | 2 + doc/zmq_z85_decode.txt | 50 ++++++++++ doc/zmq_z85_encode.txt | 56 +++++++++++ include/zmq.h | 6 ++ include/zmq_utils.h | 5 + src/Makefile.am | 1 - src/options.cpp | 14 +-- src/z85_codec.hpp | 109 ---------------------- src/zmq_utils.cpp | 91 +++++++++++++++++- tests/test_conflate.cpp | 2 - tests/test_connect_delay.cpp | 6 -- tests/test_connect_resolve.cpp | 3 - tests/test_ctx_destroy.cpp | 3 - tests/test_ctx_options.cpp | 2 - tests/test_disconnect_inproc.cpp | 2 - tests/test_fork.cpp | 10 +- tests/test_hwm.cpp | 3 - tests/test_inproc_connect_before_bind.cpp | 2 - tests/test_invalid_rep.cpp | 4 +- tests/test_iov.cpp | 5 - tests/test_last_endpoint.cpp | 2 - tests/test_linger.cpp | 3 - tests/test_monitor.cpp | 4 - tests/test_msg_flags.cpp | 2 - tests/test_pair_inproc.cpp | 1 - tests/test_pair_ipc.cpp | 1 - tests/test_pair_tcp.cpp | 1 - tests/test_probe_router.cpp | 3 - tests/test_req_request_ids.cpp | 2 - tests/test_req_strict.cpp | 3 - tests/test_reqrep_device.cpp | 3 - tests/test_reqrep_inproc.cpp | 1 - tests/test_reqrep_ipc.cpp | 1 - tests/test_reqrep_tcp.cpp | 1 - tests/test_router_mandatory.cpp | 3 - tests/test_security_curve.cpp | 4 - tests/test_security_null.cpp | 3 - tests/test_security_plain.cpp | 3 - tests/test_shutdown_stress.cpp | 4 - tests/test_spec_dealer.cpp | 2 - tests/test_spec_pushpull.cpp | 2 - tests/test_spec_rep.cpp | 2 - tests/test_spec_req.cpp | 3 - tests/test_spec_router.cpp | 2 - tests/test_stream.cpp | 2 - tests/test_sub_forward.cpp | 4 - tests/test_system.cpp | 15 ++- tests/test_term_endpoint.cpp | 4 - tests/test_timeo.cpp | 5 - tests/testutil.hpp | 14 ++- tools/curve_keygen.c | 46 +-------- tools/z85_codec.h | 109 ---------------------- 53 files changed, 237 insertions(+), 397 deletions(-) create mode 100644 doc/zmq_z85_decode.txt create mode 100644 doc/zmq_z85_encode.txt delete mode 100644 src/z85_codec.hpp delete mode 100644 tools/z85_codec.h diff --git a/doc/Makefile.am b/doc/Makefile.am index 026d76b7..446e07df 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -8,7 +8,8 @@ MAN3 = zmq_bind.3 zmq_unbind.3 zmq_connect.3 zmq_disconnect.3 zmq_close.3 \ zmq_getsockopt.3 zmq_setsockopt.3 \ zmq_socket.3 zmq_socket_monitor.3 zmq_poll.3 \ zmq_errno.3 zmq_strerror.3 zmq_version.3 zmq_proxy.3 \ - zmq_sendmsg.3 zmq_recvmsg.3 zmq_init.3 zmq_term.3 + zmq_sendmsg.3 zmq_recvmsg.3 zmq_init.3 zmq_term.3 \ + zmq_z85_encode.3 zmq_z85_decode.3 MAN7 = zmq.7 zmq_tcp.7 zmq_pgm.7 zmq_epgm.7 zmq_inproc.7 zmq_ipc.7 \ zmq_null.7 zmq_plain.7 zmq_curve.7 diff --git a/doc/zmq_curve.txt b/doc/zmq_curve.txt index a3ac30da..e1fe6971 100644 --- a/doc/zmq_curve.txt +++ b/doc/zmq_curve.txt @@ -78,6 +78,8 @@ secret: SEE ALSO -------- +linkzmq:zmq_z85_encode[3] +linkzmq:zmq_z85_decode[3] linkzmq:zmq_setsockopt[3] linkzmq:zmq_null[7] linkzmq:zmq_plain[7] diff --git a/doc/zmq_z85_decode.txt b/doc/zmq_z85_decode.txt new file mode 100644 index 00000000..708434dd --- /dev/null +++ b/doc/zmq_z85_decode.txt @@ -0,0 +1,50 @@ +zmq_z85_decode(3) +================= + + +NAME +---- +zmq_z85_decode - decode a binary key from Z85 printable text + + +SYNOPSIS +-------- +*uint8_t *zmq_z85_decode (uint8_t *dest, char *string);* + + +DESCRIPTION +----------- +The _zmq_z85_decode()_ function shall decode 'string' into 'dest'. +The length of 'string' shall be divisible by 5. 'dest' must be large +enough for the decoded value (0.8 x strlen (string)). + +The encoding shall follow the ZMQ RFC 32 specification. + + +RETURN VALUE +------------ +The _zmq_z85_decode()_ function shall return 'dest' if successful, else it +shall return NULL. + + +EXAMPLE +------- +.Decoding a CURVE key +---- +#include +char decoded [] = "rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7"; +uint8_t public_key [32]; +zmq_z85_decode (public_key, decoded); +---- + + +SEE ALSO +-------- +linkzmq:zmq_z85_decode[3] +linkzmq:zmq_curve[7] + + +AUTHORS +------- +This page was written by the 0MQ community. To make a change please +read the 0MQ Contribution Policy at . diff --git a/doc/zmq_z85_encode.txt b/doc/zmq_z85_encode.txt new file mode 100644 index 00000000..e9ca5553 --- /dev/null +++ b/doc/zmq_z85_encode.txt @@ -0,0 +1,56 @@ +zmq_z85_encode(3) +================= + + +NAME +---- +zmq_z85_encode - encode a binary key as Z85 printable text + + +SYNOPSIS +-------- +*char *zmq_z85_encode (char *dest, uint8_t *data, size_t size);* + + +DESCRIPTION +----------- +The _zmq_z85_encode()_ function shall encode the binary block specified +by 'data' and 'size' into a string in 'dest'. The size of the binary block +must be divisible by 4. The 'dest' must have sufficient space for size * 1.25 +plus 1 for a null terminator. A 32-byte CURVE key is encoded as 40 ASCII +characters plus a null terminator. + +The encoding shall follow the ZMQ RFC 32 specification. + + +RETURN VALUE +------------ +The _zmq_z85_encode()_ function shall return 'dest' if successful, else it +shall return NULL. + + +EXAMPLE +------- +.Encoding a CURVE key +---- +#include +uint8_t public_key [32]; +uint8_t secret_key [32]; +int rc = crypto_box_keypair (public_key, secret_key); +assert (rc == 0); +char encoded [41]; +zmq_z85_encode (encoded, public_key, 32); +puts (encoded); +---- + + +SEE ALSO +-------- +linkzmq:zmq_z85_decode[3] +linkzmq:zmq_curve[7] + + +AUTHORS +------- +This page was written by the 0MQ community. To make a change please +read the 0MQ Contribution Policy at . diff --git a/include/zmq.h b/include/zmq.h index a3fbc17a..3cb95262 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -386,6 +386,12 @@ ZMQ_EXPORT int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout); ZMQ_EXPORT int zmq_proxy (void *frontend, void *backend, void *capture); +/* Encode a binary key as printable text using ZMQ RFC 32 */ +ZMQ_EXPORT char *zmq_z85_encode (char *dest, uint8_t *data, size_t size); + +/* Encode a binary key from printable text per ZMQ RFC 32 */ +ZMQ_EXPORT uint8_t *zmq_z85_decode (uint8_t *dest, char *string); + /* Deprecated aliases */ #define ZMQ_STREAMER 1 #define ZMQ_FORWARDER 2 diff --git a/include/zmq_utils.h b/include/zmq_utils.h index b5326099..25acbfd0 100644 --- a/include/zmq_utils.h +++ b/include/zmq_utils.h @@ -20,6 +20,11 @@ #ifndef __ZMQ_UTILS_H_INCLUDED__ #define __ZMQ_UTILS_H_INCLUDED__ +#include +#include +#include +#include + #ifdef __cplusplus extern "C" { #endif diff --git a/src/Makefile.am b/src/Makefile.am index 57bd08a4..0f508644 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -90,7 +90,6 @@ libzmq_la_SOURCES = \ ypipe.hpp \ ypipe_flat.hpp \ yqueue.hpp \ - z85_codec.hpp \ address.cpp \ clock.cpp \ ctx.cpp \ diff --git a/src/options.cpp b/src/options.cpp index 9dd52511..84f36a7f 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -21,7 +21,7 @@ #include "options.hpp" #include "err.hpp" -#include "z85_codec.hpp" +#include "../include/zmq_utils.h" zmq::options_t::options_t () : sndhwm (1000), @@ -310,7 +310,7 @@ int zmq::options_t::setsockopt (int option_, const void *optval_, } else if (optvallen_ == CURVE_KEYSIZE_Z85) { - Z85_decode (curve_public_key, (char *) optval_); + zmq_z85_decode (curve_public_key, (char *) optval_); mechanism = ZMQ_CURVE; return 0; } @@ -324,7 +324,7 @@ int zmq::options_t::setsockopt (int option_, const void *optval_, } else if (optvallen_ == CURVE_KEYSIZE_Z85) { - Z85_decode (curve_secret_key, (char *) optval_); + zmq_z85_decode (curve_secret_key, (char *) optval_); mechanism = ZMQ_CURVE; return 0; } @@ -339,7 +339,7 @@ int zmq::options_t::setsockopt (int option_, const void *optval_, } else if (optvallen_ == CURVE_KEYSIZE_Z85) { - Z85_decode (curve_server_key, (char *) optval_); + zmq_z85_decode (curve_server_key, (char *) optval_); as_server = 0; mechanism = ZMQ_CURVE; return 0; @@ -591,7 +591,7 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_) } else if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) { - Z85_encode ((char *) optval_, curve_public_key, CURVE_KEYSIZE); + zmq_z85_encode ((char *) optval_, curve_public_key, CURVE_KEYSIZE); return 0; } break; @@ -603,7 +603,7 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_) } else if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) { - Z85_encode ((char *) optval_, curve_secret_key, CURVE_KEYSIZE); + zmq_z85_encode ((char *) optval_, curve_secret_key, CURVE_KEYSIZE); return 0; } break; @@ -615,7 +615,7 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_) } else if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) { - Z85_encode ((char *) optval_, curve_server_key, CURVE_KEYSIZE); + zmq_z85_encode ((char *) optval_, curve_server_key, CURVE_KEYSIZE); return 0; } break; diff --git a/src/z85_codec.hpp b/src/z85_codec.hpp deleted file mode 100644 index cb063ebb..00000000 --- a/src/z85_codec.hpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - -// Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding - -#ifndef __ZMQ_Z85_CODEC_HPP_INCLUDED__ -#define __ZMQ_Z85_CODEC_HPP_INCLUDED__ - -// Maps base 256 to base 85 -static char encoder [85 + 1] = { - "0123456789" "abcdefghij" "klmnopqrst" "uvwxyzABCD" - "EFGHIJKLMN" "OPQRSTUVWX" "YZ.-:+=^!/" "*?&<>()[]{" - "}@%$#" -}; - -// Maps base 85 to base 256 -// We chop off lower 32 and higher 128 ranges -static uint8_t decoder [96] = { - 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, - 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x40, 0x00, 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, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, - 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 -}; - -// -------------------------------------------------------------------------- -// 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. - -static char * -Z85_encode (char *dest, uint8_t *data, size_t size) -{ - assert (size % 4 == 0); - 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; - } - } - assert (char_nbr == size * 5 / 4); - 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. - -static uint8_t * -Z85_decode (uint8_t *dest, char *string) -{ - assert (strlen (string) % 5 == 0); - unsigned int byte_nbr = 0; - unsigned int char_nbr = 0; - uint32_t value = 0; - while (char_nbr < strlen (string)) { - // Accumulate value in base 85 - value = value * 85 + decoder [(uint8_t) string [char_nbr++] - 32]; - if (char_nbr % 5 == 0) { - // Output value in base 256 - unsigned int divisor = 256 * 256 * 256; - while (divisor) { - dest [byte_nbr++] = value / divisor % 256; - divisor /= 256; - } - value = 0; - } - } - assert (byte_nbr == strlen (string) * 4 / 5); - return dest; -} - -#endif diff --git a/src/zmq_utils.cpp b/src/zmq_utils.cpp index 4b7e64ee..9cef5dec 100644 --- a/src/zmq_utils.cpp +++ b/src/zmq_utils.cpp @@ -19,14 +19,11 @@ #include "platform.hpp" -#include "../include/zmq_utils.h" - -#include - -#include "stdint.hpp" #include "clock.hpp" #include "err.hpp" #include "thread.hpp" +#include +#include "../include/zmq_utils.h" #if !defined ZMQ_HAVE_WINDOWS #include @@ -72,3 +69,87 @@ void zmq_threadclose(void* thread) pThread->stop(); delete pThread; } + +// Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding + +// Maps base 256 to base 85 +static char encoder [85 + 1] = { + "0123456789" "abcdefghij" "klmnopqrst" "uvwxyzABCD" + "EFGHIJKLMN" "OPQRSTUVWX" "YZ.-:+=^!/" "*?&<>()[]{" + "}@%$#" +}; + +// Maps base 85 to base 256 +// We chop off lower 32 and higher 128 ranges +static uint8_t decoder [96] = { + 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, + 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x40, 0x00, 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, + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, + 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, + 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 +}; + +// -------------------------------------------------------------------------- +// 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. + +char *zmq_z85_encode (char *dest, uint8_t *data, size_t size) +{ + assert (size % 4 == 0); + 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; + } + } + assert (char_nbr == size * 5 / 4); + 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. + +uint8_t *zmq_z85_decode (uint8_t *dest, char *string) +{ + assert (strlen (string) % 5 == 0); + unsigned int byte_nbr = 0; + unsigned int char_nbr = 0; + uint32_t value = 0; + while (char_nbr < strlen (string)) { + // Accumulate value in base 85 + value = value * 85 + decoder [(uint8_t) string [char_nbr++] - 32]; + if (char_nbr % 5 == 0) { + // Output value in base 256 + unsigned int divisor = 256 * 256 * 256; + while (divisor) { + dest [byte_nbr++] = value / divisor % 256; + divisor /= 256; + } + value = 0; + } + } + assert (byte_nbr == strlen (string) * 4 / 5); + return dest; +} diff --git a/tests/test_conflate.cpp b/tests/test_conflate.cpp index 3bc9d0dd..b7e3331b 100644 --- a/tests/test_conflate.cpp +++ b/tests/test_conflate.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include "../include/zmq_utils.h" #include "testutil.hpp" int main (int argc, char *argv []) diff --git a/tests/test_connect_delay.cpp b/tests/test_connect_delay.cpp index 77ed5fd1..a9e7aede 100644 --- a/tests/test_connect_delay.cpp +++ b/tests/test_connect_delay.cpp @@ -17,12 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_connect_resolve.cpp b/tests/test_connect_resolve.cpp index 99fb4adb..4c49e8f3 100644 --- a/tests/test_connect_resolve.cpp +++ b/tests/test_connect_resolve.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_ctx_destroy.cpp b/tests/test_ctx_destroy.cpp index a78a05a9..7b3c5691 100644 --- a/tests/test_ctx_destroy.cpp +++ b/tests/test_ctx_destroy.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include #include "testutil.hpp" static void receiver (void *socket) diff --git a/tests/test_ctx_options.cpp b/tests/test_ctx_options.cpp index 2fde234c..1c107440 100644 --- a/tests/test_ctx_options.cpp +++ b/tests/test_ctx_options.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_disconnect_inproc.cpp b/tests/test_disconnect_inproc.cpp index fe2a72b0..49c9e5c5 100644 --- a/tests/test_disconnect_inproc.cpp +++ b/tests/test_disconnect_inproc.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" /// Initialize a zeromq message with a given null-terminated string diff --git a/tests/test_fork.cpp b/tests/test_fork.cpp index a0f563ef..637b6e98 100644 --- a/tests/test_fork.cpp +++ b/tests/test_fork.cpp @@ -17,17 +17,9 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" -#include -#include -#include -#include -const char* address = "tcp://127.0.0.1:6571"; +const char *address = "tcp://127.0.0.1:6571"; #define NUM_MESSAGES 5 diff --git a/tests/test_hwm.cpp b/tests/test_hwm.cpp index ab5b4500..b1d871ee 100644 --- a/tests/test_hwm.cpp +++ b/tests/test_hwm.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include -#include #include "testutil.hpp" const int MAX_SENDS = 10000; diff --git a/tests/test_inproc_connect_before_bind.cpp b/tests/test_inproc_connect_before_bind.cpp index 71e228c7..e3090fd2 100644 --- a/tests/test_inproc_connect_before_bind.cpp +++ b/tests/test_inproc_connect_before_bind.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq_utils.h" -#include #include "testutil.hpp" static void pusher (void *ctx) diff --git a/tests/test_invalid_rep.cpp b/tests/test_invalid_rep.cpp index 54a32e81..8f166422 100644 --- a/tests/test_invalid_rep.cpp +++ b/tests/test_invalid_rep.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include #include "testutil.hpp" int main (void) @@ -75,7 +73,7 @@ int main (void) // Check whether we've got the valid reply. rc = zmq_recv (req_socket, body, sizeof (body), 0); assert (rc == 1); - assert (body [0] == 'b'); + assert (body [0] == 'b'); // Tear down the wiring. rc = zmq_close (router_socket); diff --git a/tests/test_iov.cpp b/tests/test_iov.cpp index 6d647123..2eef04ac 100644 --- a/tests/test_iov.cpp +++ b/tests/test_iov.cpp @@ -17,11 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include -#include #include "testutil.hpp" // XSI vector I/O diff --git a/tests/test_last_endpoint.cpp b/tests/test_last_endpoint.cpp index dc836869..2343c753 100644 --- a/tests/test_last_endpoint.cpp +++ b/tests/test_last_endpoint.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include #include "testutil.hpp" static void do_bind_and_verify (void *s, const char *endpoint) diff --git a/tests/test_linger.cpp b/tests/test_linger.cpp index 72eba782..8263815e 100644 --- a/tests/test_linger.cpp +++ b/tests/test_linger.cpp @@ -21,9 +21,6 @@ // or alternatively we can change policy that issue test cases should be // added to the main build when they're useful. -- PH 2013/09/02 -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" #define URL "tcp://127.0.0.1:5560" diff --git a/tests/test_monitor.cpp b/tests/test_monitor.cpp index 26daf495..36b835a0 100644 --- a/tests/test_monitor.cpp +++ b/tests/test_monitor.cpp @@ -17,10 +17,6 @@ along with this program. If not, see . */ -#include -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include #include "testutil.hpp" // REQ socket events handled diff --git a/tests/test_msg_flags.cpp b/tests/test_msg_flags.cpp index 172724e5..9bdd9c4a 100644 --- a/tests/test_msg_flags.cpp +++ b/tests/test_msg_flags.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_pair_inproc.cpp b/tests/test_pair_inproc.cpp index 20ab071c..4eac5f5f 100644 --- a/tests/test_pair_inproc.cpp +++ b/tests/test_pair_inproc.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_pair_ipc.cpp b/tests/test_pair_ipc.cpp index 6ee23953..31b534b1 100644 --- a/tests/test_pair_ipc.cpp +++ b/tests/test_pair_ipc.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_pair_tcp.cpp b/tests/test_pair_tcp.cpp index ca79e503..f7e5aee5 100644 --- a/tests/test_pair_tcp.cpp +++ b/tests/test_pair_tcp.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_probe_router.cpp b/tests/test_probe_router.cpp index 6a2ae8c9..2f6930df 100644 --- a/tests/test_probe_router.cpp +++ b/tests/test_probe_router.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_req_request_ids.cpp b/tests/test_req_request_ids.cpp index 6f7f9490..9a83e9aa 100644 --- a/tests/test_req_request_ids.cpp +++ b/tests/test_req_request_ids.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_req_strict.cpp b/tests/test_req_strict.cpp index fe3969f2..71709098 100644 --- a/tests/test_req_strict.cpp +++ b/tests/test_req_strict.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_reqrep_device.cpp b/tests/test_reqrep_device.cpp index d5945668..9eb1db19 100644 --- a/tests/test_reqrep_device.cpp +++ b/tests/test_reqrep_device.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_reqrep_inproc.cpp b/tests/test_reqrep_inproc.cpp index 2172df12..addf2f7d 100644 --- a/tests/test_reqrep_inproc.cpp +++ b/tests/test_reqrep_inproc.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_reqrep_ipc.cpp b/tests/test_reqrep_ipc.cpp index c07117ae..f05f0e0f 100644 --- a/tests/test_reqrep_ipc.cpp +++ b/tests/test_reqrep_ipc.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_reqrep_tcp.cpp b/tests/test_reqrep_tcp.cpp index 8097b2ce..275f7537 100644 --- a/tests/test_reqrep_tcp.cpp +++ b/tests/test_reqrep_tcp.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_router_mandatory.cpp b/tests/test_router_mandatory.cpp index 512e3ed9..dcc6114f 100644 --- a/tests/test_router_mandatory.cpp +++ b/tests/test_router_mandatory.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_security_curve.cpp b/tests/test_security_curve.cpp index 17d6eec4..7a5ba05b 100644 --- a/tests/test_security_curve.cpp +++ b/tests/test_security_curve.cpp @@ -17,11 +17,7 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" -#include "../include/zmq_utils.h" -#include "platform.hpp" // Test keys from the zmq_curve man page static char client_public [] = "Yne@$w-vo. */ -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" static void diff --git a/tests/test_security_plain.cpp b/tests/test_security_plain.cpp index da37fb0f..99748d45 100644 --- a/tests/test_security_plain.cpp +++ b/tests/test_security_plain.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" static void diff --git a/tests/test_shutdown_stress.cpp b/tests/test_shutdown_stress.cpp index 4328ca85..39b42da2 100644 --- a/tests/test_shutdown_stress.cpp +++ b/tests/test_shutdown_stress.cpp @@ -17,10 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" #define THREAD_COUNT 100 diff --git a/tests/test_spec_dealer.cpp b/tests/test_spec_dealer.cpp index fe442d24..08b54290 100644 --- a/tests/test_spec_dealer.cpp +++ b/tests/test_spec_dealer.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" const char *bind_address = 0; diff --git a/tests/test_spec_pushpull.cpp b/tests/test_spec_pushpull.cpp index f9d50102..6c5c620e 100644 --- a/tests/test_spec_pushpull.cpp +++ b/tests/test_spec_pushpull.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" const char *bind_address = 0; diff --git a/tests/test_spec_rep.cpp b/tests/test_spec_rep.cpp index 2aadc172..291787d6 100644 --- a/tests/test_spec_rep.cpp +++ b/tests/test_spec_rep.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" const char *bind_address = 0; diff --git a/tests/test_spec_req.cpp b/tests/test_spec_req.cpp index 416c419d..1520beab 100644 --- a/tests/test_spec_req.cpp +++ b/tests/test_spec_req.cpp @@ -17,9 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" const char *bind_address = 0; diff --git a/tests/test_spec_router.cpp b/tests/test_spec_router.cpp index f733285c..22bc54b9 100644 --- a/tests/test_spec_router.cpp +++ b/tests/test_spec_router.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include -#include #include "testutil.hpp" const char *bind_address = 0; diff --git a/tests/test_stream.cpp b/tests/test_stream.cpp index 2de44b1a..2c7392c1 100644 --- a/tests/test_stream.cpp +++ b/tests/test_stream.cpp @@ -17,8 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include #include "testutil.hpp" // ZMTP protocol greeting structure diff --git a/tests/test_sub_forward.cpp b/tests/test_sub_forward.cpp index acf91314..53533086 100644 --- a/tests/test_sub_forward.cpp +++ b/tests/test_sub_forward.cpp @@ -17,10 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_system.cpp b/tests/test_system.cpp index 4f1ba523..e8dab5cc 100644 --- a/tests/test_system.cpp +++ b/tests/test_system.cpp @@ -17,16 +17,15 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "platform.hpp" +#include "testutil.hpp" + #if defined (ZMQ_HAVE_WINDOWS) -#include -#include +# include +# include #else -#include -#include -#include -#include +# include +# include +# include #endif #if defined (ZMQ_HAVE_WINDOWS) diff --git a/tests/test_term_endpoint.cpp b/tests/test_term_endpoint.cpp index ad43df51..6d9a5df0 100644 --- a/tests/test_term_endpoint.cpp +++ b/tests/test_term_endpoint.cpp @@ -17,10 +17,6 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" int main (void) diff --git a/tests/test_timeo.cpp b/tests/test_timeo.cpp index b30b6518..f47e0b56 100644 --- a/tests/test_timeo.cpp +++ b/tests/test_timeo.cpp @@ -17,13 +17,8 @@ along with this program. If not, see . */ -#include "../include/zmq.h" -#include "../include/zmq_utils.h" -#include -#include #include "testutil.hpp" - int main (void) { setup_test_environment(); diff --git a/tests/testutil.hpp b/tests/testutil.hpp index 2fb1cc78..eb749e85 100644 --- a/tests/testutil.hpp +++ b/tests/testutil.hpp @@ -21,15 +21,23 @@ #define __TESTUTIL_HPP_INCLUDED__ #include "../include/zmq.h" -#include +#include "../include/zmq_utils.h" +#include "../src/platform.hpp" #undef NDEBUG +#include #include #include +#include #if defined _WIN32 -#include -#pragma warning(disable:4996) +# include +# pragma warning(disable:4996) +#else +# include +# include +# include +# include #endif // Bounce a message from client to server and back diff --git a/tools/curve_keygen.c b/tools/curve_keygen.c index 5b52d69f..7a5da02e 100644 --- a/tools/curve_keygen.c +++ b/tools/curve_keygen.c @@ -24,51 +24,11 @@ along with this program. If not, see . */ -#include -#include -#include -#include "../src/platform.hpp" +#include #ifdef HAVE_LIBSODIUM # include #endif -// Maps base 256 to base 85 -static char encoder [85 + 1] = { - "0123456789" "abcdefghij" "klmnopqrst" "uvwxyzABCD" - "EFGHIJKLMN" "OPQRSTUVWX" "YZ.-:+=^!/" "*?&<>()[]{" - "}@%$#" -}; - -// -------------------------------------------------------------------------- -// 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. - -static char * -Z85_encode (char *dest, uint8_t *data, size_t size) -{ - assert (size % 4 == 0); - uint char_nbr = 0; - uint 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 - uint divisor = 85 * 85 * 85 * 85; - while (divisor) { - dest [char_nbr++] = encoder [value / divisor % 85]; - divisor /= 85; - } - value = 0; - } - } - assert (char_nbr == size * 5 / 4); - dest [char_nbr] = 0; - return dest; -} - int main (void) { #ifdef HAVE_LIBSODIUM @@ -91,11 +51,11 @@ int main (void) assert (rc == 0); char encoded [41]; - Z85_encode (encoded, public_key, 32); + zmq_z85_encode (encoded, public_key, 32); puts ("\n== CURVE PUBLIC KEY =="); puts (encoded); - Z85_encode (encoded, secret_key, 32); + zmq_z85_encode (encoded, secret_key, 32); puts ("\n== CURVE SECRET KEY =="); puts (encoded); diff --git a/tools/z85_codec.h b/tools/z85_codec.h deleted file mode 100644 index 6e198a32..00000000 --- a/tools/z85_codec.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - -// Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding - -#include -#include -#include -#include - -// Maps base 256 to base 85 -static char encoder [85 + 1] = { - "0123456789" "abcdefghij" "klmnopqrst" "uvwxyzABCD" - "EFGHIJKLMN" "OPQRSTUVWX" "YZ.-:+=^!/" "*?&<>()[]{" - "}@%$#" -}; - -// Maps base 85 to base 256 -// We chop off lower 32 and higher 128 ranges -static uint8_t decoder [96] = { - 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, - 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x40, 0x00, 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, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, - 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 -}; - -// -------------------------------------------------------------------------- -// 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. - -static char * -Z85_encode (char *dest, uint8_t *data, size_t size) -{ - assert (size % 4 == 0); - uint char_nbr = 0; - uint 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 - uint divisor = 85 * 85 * 85 * 85; - while (divisor) { - dest [char_nbr++] = encoder [value / divisor % 85]; - divisor /= 85; - } - value = 0; - } - } - assert (char_nbr == size * 5 / 4); - 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. - -static uint8_t * -Z85_decode (uint8_t *dest, char *string) -{ - assert (strlen (string) % 5 == 0); - uint byte_nbr = 0; - uint char_nbr = 0; - uint32_t value = 0; - while (char_nbr < strlen (string)) { - // Accumulate value in base 85 - value = value * 85 + decoder [(uint8_t) string [char_nbr++] - 32]; - if (char_nbr % 5 == 0) { - // Output value in base 256 - uint divisor = 256 * 256 * 256; - while (divisor) { - dest [byte_nbr++] = value / divisor % 256; - divisor /= 256; - } - value = 0; - } - } - assert (byte_nbr == strlen (string) * 4 / 5); - return dest; -}