From e8400a43ca3f67e44e7593907353c6e5335c304e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 20 Jul 2009 14:55:35 +0000 Subject: [PATCH] Rename encode_int(64) to avoid polluting the global namespace. They're now called evtag_encode_int(64). The old names are available as macros in event2/tag_compat.h. Also, add unit tests for encode/decode_int64. svn:r1365 --- ChangeLog | 1 + event.h | 1 + event_tagging.c | 23 ++++++++++++++++------ include/Makefile.am | 4 ++-- include/event2/tag.h | 4 ++-- include/event2/tag_compat.h | 39 +++++++++++++++++++++++++++++++++++++ test/regress.c | 16 +++++++++++---- 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 include/event2/tag_compat.h diff --git a/ChangeLog b/ChangeLog index 2ffe32d7..83a3c590 100644 --- a/ChangeLog +++ b/ChangeLog @@ -44,6 +44,7 @@ Changes in 2.0.2-alpha: o Use AC_SEARCH_LIBS, not AC_CHECK_LIB to avoid needless library use. o Do not allow event_del(ev) to return while that event's callback is executing in another thread. This fixes a nasty race condition. o event_get_supported_methods() now lists methods that have been disabled with the EVENT_NO* environment options. + o Rename encode_int[64] to evtag_encode_int[64] to avoid polluting the global namespace. The old method names are still available as macros in event2/tag_compat.h. Changes in 2.0.1-alpha: o free minheap on event_base_free(); from Christopher Layne diff --git a/event.h b/event.h index b7d3b333..9139296c 100644 --- a/event.h +++ b/event.h @@ -194,6 +194,7 @@ typedef unsigned short u_short; #include #include #include +#include #ifdef __cplusplus } diff --git a/event_tagging.c b/event_tagging.c index c18b93f2..c5654d30 100644 --- a/event_tagging.c +++ b/event_tagging.c @@ -68,6 +68,7 @@ #include "util-internal.h" int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf); +int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf); int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag); int evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf); @@ -123,7 +124,7 @@ encode_int64_internal(ev_uint8_t *data, ev_uint64_t number) } void -encode_int(struct evbuffer *evbuf, ev_uint32_t number) +evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number) { ev_uint8_t data[5]; int len = encode_int_internal(data, number); @@ -131,7 +132,7 @@ encode_int(struct evbuffer *evbuf, ev_uint32_t number) } void -encode_int64(struct evbuffer *evbuf, ev_uint64_t number) +evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number) { ev_uint8_t data[9]; int len = encode_int64_internal(data, number); @@ -221,7 +222,7 @@ evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data, ev_uint32_t len) { evtag_encode_tag(evbuf, tag); - encode_int(evbuf, len); + evtag_encode_int(evbuf, len); evbuffer_add(evbuf, (void *)data, len); } @@ -230,7 +231,7 @@ evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, struct evbuffer *data) { evtag_encode_tag(evbuf, tag); - encode_int(evbuf, evbuffer_get_length(data)); + evtag_encode_int(evbuf, evbuffer_get_length(data)); evbuffer_add_buffer(evbuf, data); } @@ -242,7 +243,7 @@ evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer) int len = encode_int_internal(data, integer); evtag_encode_tag(evbuf, tag); - encode_int(evbuf, len); + evtag_encode_int(evbuf, len); evbuffer_add(evbuf, data, len); } @@ -254,7 +255,7 @@ evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag, int len = encode_int64_internal(data, integer); evtag_encode_tag(evbuf, tag); - encode_int(evbuf, len); + evtag_encode_int(evbuf, len); evbuffer_add(evbuf, data, len); } @@ -339,6 +340,16 @@ evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf) return (res == -1 ? -1 : 0); } +int +evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf) +{ + int res = decode_int64_internal(pnumber, evbuf, 0); + if (res != -1) + evbuffer_drain(evbuf, res); + + return (res == -1 ? -1 : 0); +} + int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag) { diff --git a/include/Makefile.am b/include/Makefile.am index 57f0c015..529eff1f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -5,7 +5,7 @@ AUTOMAKE_OPTIONS = foreign EXTRA_SRC = event2/buffer.h event2/buffer_compat.h \ event2/thread.h event2/bufferevent.h event2/bufferevent_compat.h \ event2/bufferevent_struct.h event2/event.h event2/event_compat.h \ - event2/event_struct.h event2/tag.h event2/util.h \ + event2/event_struct.h event2/tag.h event2/tag_compat.h event2/util.h \ event2/http.h event2/http_struct.h event2/http_compat.h \ event2/listener.h @@ -14,7 +14,7 @@ nobase_include_HEADERS = \ event2/thread.h event2/bufferevent.h \ event2/bufferevent_compat.h \ event2/bufferevent_struct.h event2/event.h event2/event_compat.h \ - event2/event_struct.h event2/tag.h event2/util.h \ + event2/event_struct.h event2/tag.h event2/tag_compat.h event2/util.h \ event2/http.h event2/http_struct.h event2/http_compat.h \ event2/rpc.h event2/rpc_struct.h event2/rpc_compat.h \ event2/dns.h event2/dns_struct.h event2/dns_compat.h \ diff --git a/include/event2/tag.h b/include/event2/tag.h index 0f967e02..ebed1a61 100644 --- a/include/event2/tag.h +++ b/include/event2/tag.h @@ -88,8 +88,8 @@ void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, @param evbuf evbuffer to store the encoded number @param number a 32-bit integer */ -void encode_int(struct evbuffer *evbuf, ev_uint32_t number); -void encode_int64(struct evbuffer *evbuf, ev_uint64_t number); +void evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number); +void evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number); void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer); diff --git a/include/event2/tag_compat.h b/include/event2/tag_compat.h new file mode 100644 index 00000000..4cb9f433 --- /dev/null +++ b/include/event2/tag_compat.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * Copyright (c) 2007-2009 Niels Provos and Nick Mathewson + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_TAG_COMPAT_H_ +#define _EVENT2_TAG_COMPAT_H_ + +/** @file tag_compat.h + + Obsolete/deprecated functions from tag.h; provided only for backwards + compatibility. + */ + +#define encode_int(evbuf, number) evtag_encode_int((evbuf), (number)) +#define encode_int64(evbuf, number) evtag_encode_int64((evbuf), (number)) + +#endif /* _EVENT2_TAG_H_ */ diff --git a/test/regress.c b/test/regress.c index 0d26eb01..64903b3e 100644 --- a/test/regress.c +++ b/test/regress.c @@ -1270,6 +1270,7 @@ test_multiple_events_for_same_fd(void) } int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf); +int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf); int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t number); int evtag_decode_tag(ev_uint32_t *pnumber, struct evbuffer *evbuf); @@ -1326,23 +1327,30 @@ evtag_int_test(void) 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000 }; ev_uint32_t integer; + ev_uint64_t big_int; int i; for (i = 0; i < TEST_MAX_INT; i++) { int oldlen, newlen; oldlen = EVBUFFER_LENGTH(tmp); - encode_int(tmp, integers[i]); - newlen = EVBUFFER_LENGTH(tmp); - TT_BLATHER(("encoded 0x%08x with %d bytes", + evtag_encode_int(tmp, integers[i]); + newlen = EVBUFFER_LENGTH(tmp); + TT_BLATHER(("encoded 0x%08x with %d bytes", (unsigned)integers[i], newlen - oldlen)); + big_int = integers[i]; + big_int *= 1000000000; /* 1 billion */ + evtag_encode_int64(tmp, big_int); } for (i = 0; i < TEST_MAX_INT; i++) { - tt_assert(evtag_decode_int(&integer, tmp) != -1); + tt_int_op(evtag_decode_int(&integer, tmp), !=, -1); tt_uint_op(integer, ==, integers[i]); + tt_int_op(evtag_decode_int64(&big_int, tmp), !=, -1); + tt_assert((big_int / 1000000000) == integers[i]); } tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0); + end: evbuffer_free(tmp); } -- 2.40.0