From add067ed7eb19a6b0c6603049ee7ab026dd1f189 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 7 Mar 2013 21:17:55 +0900 Subject: [PATCH] Provide timegm replacement and android build fix --- configure.ac | 15 ++++++++-- src/Makefile.am | 10 +++---- src/timegm.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ src/timegm.h | 48 +++++++++++++++++++++++++++++++ src/util.cc | 2 ++ 5 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 src/timegm.c create mode 100644 src/timegm.h diff --git a/configure.ac b/configure.ac index 29e97e7..00687a6 100644 --- a/configure.ac +++ b/configure.ac @@ -85,16 +85,26 @@ LIBS_OLD=$LIBS # Search for dlsym function, which is used in tests. Linux needs -ldl, # but netbsd does not need it. AC_SEARCH_LIBS([dlsym], [dl]) -TESTS_LIBS=$LIBS $TESTS_LIBS +TESTS_LIBS="$LIBS $TESTS_LIBS" LIBS=$LIBS_OLD LIBS_OLD=$LIBS AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define to 1 if you have the `clock_gettime`.])]) -SRC_LIBS=$LIBS $SRC_LIBS +SRC_LIBS="$LIBS $SRC_LIBS" LIBS=$LIBS_OLD +case "$host" in + *android*) + # android does not need -pthread, but needs followng 2 libs for C++ + SRC_LIBS="$SRC_LIBS -lstdc++ -lsupc++" + ;; + *) + SRC_LIBS="$SRC_LIBS -pthread" + ;; +esac + # zlib PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3]) LIBS="$ZLIB_LIBS $LIBS" @@ -211,6 +221,7 @@ AC_CHECK_FUNCS([ \ getpwnam \ memmove \ memset \ + timegm \ ]) AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no]) diff --git a/src/Makefile.am b/src/Makefile.am index a819abf..d0aac3e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,8 +26,8 @@ if ENABLE_SRC AM_CFLAGS = -Wall AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \ @OPENSSL_CFLAGS@ @XML_CPPFLAGS@ @LIBEVENT_OPENSSL_CFLAGS@ @DEFS@ -AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@ \ - -pthread +AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@ + LDADD = $(top_builddir)/lib/libspdylay.la bin_PROGRAMS = spdycat spdyd @@ -38,8 +38,8 @@ if HAVE_LIBEVENT_OPENSSL bin_PROGRAMS += shrpx endif # HAVE_LIBEVENT_OPENSSL -HELPER_OBJECTS = util.cc spdylay_ssl.cc -HELPER_HFILES = util.h spdylay_ssl.h spdylay_config.h +HELPER_OBJECTS = util.cc timegm.c spdylay_ssl.cc +HELPER_HFILES = util.h timegm.h spdylay_ssl.h spdylay_config.h EVENT_OBJECTS = EVENT_HFILES = EventPoll.h EventPollEvent.h @@ -75,7 +75,7 @@ spdyd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} \ if HAVE_LIBEVENT_OPENSSL SHRPX_SRCS = \ - util.cc util.h base64.h \ + util.cc util.h timegm.c timegm.h base64.h \ shrpx_config.cc shrpx_config.h \ shrpx_error.h \ shrpx_listen_handler.cc shrpx_listen_handler.h \ diff --git a/src/timegm.c b/src/timegm.c new file mode 100644 index 0000000..cbc158f --- /dev/null +++ b/src/timegm.c @@ -0,0 +1,75 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2013 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "timegm.h" + +#ifndef HAVE_TIMEGM + +#include + +/* Counter the number of leap year in the range [0, y). The |y| is the + year, including century (e.g., 2012) */ +static int count_leap_year(int y) +{ + y -= 1; + return y/4-y/100+y/400; +} + +/* Returns nonzero if the |y| is the leap year. The |y| is the year, + including century (e.g., 2012) */ +static int is_leap_year(int y) +{ + return y%4 == 0 && (y%100 != 0 || y%400 == 0); +} + +/* The number of days before ith month begins */ +static int daysum[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 +}; + +/* Based on the algorithm of Python 2.7 calendar.timegm. */ +time_t timegm(struct tm *tm) +{ + int days; + int num_leap_year; + int64_t t; + if(tm->tm_mon > 11) { + return -1; + } + num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970); + days = (tm->tm_year - 70) * 365 + + num_leap_year + daysum[tm->tm_mon] + tm->tm_mday-1; + if(tm->tm_mon >= 2 && is_leap_year(tm->tm_year + 1900)) { + ++days; + } + t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec; + if(sizeof(time_t) == 4) { + if(t < INT32_MIN || t > INT32_MAX) { + return -1; + } + } + return t; +} + +#endif /* !HAVE_TIMEGM */ diff --git a/src/timegm.h b/src/timegm.h new file mode 100644 index 0000000..231d145 --- /dev/null +++ b/src/timegm.h @@ -0,0 +1,48 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2013 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef TIMEGM_H +#define TIMEGM_H + +#ifdef HAVE_CONFIG_H +# include +#endif // HAVE_CONFIG_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include + +#ifndef HAVE_TIMEGM + +time_t timegm(struct tm *tm); + +#endif /* HAVE_TIMEGM */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* TIMEGM_H */ diff --git a/src/util.cc b/src/util.cc index 1a3235a..9984142 100644 --- a/src/util.cc +++ b/src/util.cc @@ -29,6 +29,8 @@ #include #include +#include "timegm.h" + namespace spdylay { namespace util {