author | Michael Krelin <hacker@klever.net> | 2008-06-29 16:08:01 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-06-29 16:11:44 (UTC) |
commit | 12837594b705ad10fdadfd0ba1bfc2249b3b1264 (patch) (unidiff) | |
tree | e0502e678b09668ec59828237ebf5972014d0ce0 | |
parent | 362678728b8232c9490e14ba14ff323d9a92d6be (diff) | |
download | libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.zip libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.gz libopkele-12837594b705ad10fdadfd0ba1bfc2249b3b1264.tar.bz2 |
Fixed w3c to unix timestamp conversion for FreeBSD
Thanks to Göran Löwkrantz for pointing both to the problem and possible
solution.
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | lib/util.cc | 21 |
2 files changed, 19 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac index 3194718..3484146 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1,44 +1,45 @@ | |||
1 | AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net]) | 1 | AC_INIT([libopkele], [2.0], [libopkele-bugs@klever.net]) |
2 | AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) | 2 | AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) |
3 | AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) | 3 | AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) |
4 | AM_INIT_AUTOMAKE([dist-bzip2]) | 4 | AM_INIT_AUTOMAKE([dist-bzip2]) |
5 | 5 | ||
6 | AC_PROG_INSTALL | 6 | AC_PROG_INSTALL |
7 | AC_PROG_CXX | 7 | AC_PROG_CXX |
8 | AC_PROG_CC | 8 | AC_PROG_CC |
9 | AC_PROG_LIBTOOL | 9 | AC_PROG_LIBTOOL |
10 | PKG_PROG_PKG_CONFIG | 10 | PKG_PROG_PKG_CONFIG |
11 | 11 | ||
12 | AC_HEADER_STDC | 12 | AC_HEADER_STDC |
13 | AC_CHECK_FUNCS([timegm]) | ||
13 | 14 | ||
14 | AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) | 15 | AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) |
15 | 16 | ||
16 | AC_MSG_CHECKING([for source tree version]) | 17 | AC_MSG_CHECKING([for source tree version]) |
17 | if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then | 18 | if headrev=$(cd $srcdir && git rev-parse --verify HEAD 2>/dev/null) ; then |
18 | PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)" | 19 | PACKAGE_SRC_VERSION="$(cd $srcdir && git describe --tags $headrev)" |
19 | test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \ | 20 | test "$PACKAGE_SRC_VERSION" = "$PACKAGE_VERSION" \ |
20 | -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}" | 21 | -o "${PACKAGE_SRC_VERSION#${PACKAGE_VERSION}-}" != "$PACKAGE_SRC_VERSION" || PACKAGE_SRC_VERSION="${PACKAGE_VERSION}:${PACKAGE_SRC_VERSION}" |
21 | ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty" | 22 | ( cd $srcdir && git diff-index $headrev | read dirt ) && PACKAGE_SRC_VERSION="${PACKAGE_SRC_VERSION}-dirty" |
22 | else | 23 | else |
23 | PACKAGE_SRC_VERSION="$PACKAGE_VERSION" | 24 | PACKAGE_SRC_VERSION="$PACKAGE_VERSION" |
24 | fi | 25 | fi |
25 | AC_MSG_RESULT([$PACKAGE_SRC_VERSION]) | 26 | AC_MSG_RESULT([$PACKAGE_SRC_VERSION]) |
26 | AC_SUBST([PACKAGE_SRC_VERSION]) | 27 | AC_SUBST([PACKAGE_SRC_VERSION]) |
27 | AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version]) | 28 | AC_DEFINE_UNQUOTED([PACKAGE_SRC_VERSION],["$PACKAGE_SRC_VERSION"],[more or less precise source tree version]) |
28 | 29 | ||
29 | tr1_mem_std="false" | 30 | tr1_mem_std="false" |
30 | tr1_mem_boost="false" | 31 | tr1_mem_boost="false" |
31 | AC_CHECK_SHAREDPTR(std::tr1,tr1/memory,[ tr1_mem_std=true ]) | 32 | AC_CHECK_SHAREDPTR(std::tr1,tr1/memory,[ tr1_mem_std=true ]) |
32 | AC_CHECK_SHAREDPTR(boost,boost/shared_ptr.hpp,[ tr1_mem_boost=true ]) | 33 | AC_CHECK_SHAREDPTR(boost,boost/shared_ptr.hpp,[ tr1_mem_boost=true ]) |
33 | tr1_mem="" | 34 | tr1_mem="" |
34 | AC_ARG_WITH([tr1-memory], | 35 | AC_ARG_WITH([tr1-memory], |
35 | AC_HELP_STRING([--with-tr1-memory=<boost|std>],[select tr1/memory (shared_ptr<>) implementation to use]), | 36 | AC_HELP_STRING([--with-tr1-memory=<boost|std>],[select tr1/memory (shared_ptr<>) implementation to use]), |
36 | [ tr1_mem="$withval" ] | 37 | [ tr1_mem="$withval" ] |
37 | ) | 38 | ) |
38 | AC_MSG_CHECKING([for tr1/memory implementation to use]) | 39 | AC_MSG_CHECKING([for tr1/memory implementation to use]) |
39 | test -z "$tr1_mem" && $tr1_mem_std && tr1_mem=std | 40 | test -z "$tr1_mem" && $tr1_mem_std && tr1_mem=std |
40 | test -z "$tr1_mem" && $tr1_mem_boost && tr1_mem=boost | 41 | test -z "$tr1_mem" && $tr1_mem_boost && tr1_mem=boost |
41 | if test -z "$tr1_mem" ; then | 42 | if test -z "$tr1_mem" ; then |
42 | AC_MSG_RESULT([none found]) | 43 | AC_MSG_RESULT([none found]) |
43 | else | 44 | else |
44 | AC_MSG_RESULT([$tr1_mem]) | 45 | AC_MSG_RESULT([$tr1_mem]) |
diff --git a/lib/util.cc b/lib/util.cc index d979502..a46ba2a 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -93,91 +93,106 @@ namespace opkele { | |||
93 | 93 | ||
94 | BIGNUM *dec_to_bignum(const string& dec) { | 94 | BIGNUM *dec_to_bignum(const string& dec) { |
95 | BIGNUM *rv = 0; | 95 | BIGNUM *rv = 0; |
96 | if(!BN_dec2bn(&rv,dec.c_str())) | 96 | if(!BN_dec2bn(&rv,dec.c_str())) |
97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
98 | return rv; | 98 | return rv; |
99 | } | 99 | } |
100 | 100 | ||
101 | string bignum_to_base64(const BIGNUM *bn) { | 101 | string bignum_to_base64(const BIGNUM *bn) { |
102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
103 | unsigned char *binptr = &(bin.front())+1; | 103 | unsigned char *binptr = &(bin.front())+1; |
104 | int l = BN_bn2bin(bn,binptr); | 104 | int l = BN_bn2bin(bn,binptr); |
105 | if(l && (*binptr)&0x80){ | 105 | if(l && (*binptr)&0x80){ |
106 | (*(--binptr)) = 0; ++l; | 106 | (*(--binptr)) = 0; ++l; |
107 | } | 107 | } |
108 | return encode_base64(binptr,l); | 108 | return encode_base64(binptr,l); |
109 | } | 109 | } |
110 | 110 | ||
111 | /* | 111 | /* |
112 | * w3c times | 112 | * w3c times |
113 | */ | 113 | */ |
114 | 114 | ||
115 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
116 | struct tm tm_t; | 116 | struct tm tm_t; |
117 | if(!gmtime_r(&t,&tm_t)) | 117 | if(!gmtime_r(&t,&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
119 | char rv[25]; | 119 | char rv[25]; |
120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
122 | return rv; | 122 | return rv; |
123 | } | 123 | } |
124 | 124 | ||
125 | #ifndef HAVE_TIMEGM | ||
126 | static time_t timegm(struct tm *t) { | ||
127 | char *tz = getenv("TZ"); | ||
128 | setenv("TZ","",1); tzset(); | ||
129 | time_t rv = mktime(t); | ||
130 | if(tz) | ||
131 | setenv("TZ",tz,1); | ||
132 | else | ||
133 | unsetenv("TZ"); | ||
134 | tzset(); | ||
135 | return rv; | ||
136 | } | ||
137 | #define timegm opkele::util::timegm | ||
138 | #endif /* HAVE_TIMEGM */ | ||
139 | |||
125 | time_t w3c_to_time(const string& w) { | 140 | time_t w3c_to_time(const string& w) { |
126 | int fraction; | 141 | int fraction; |
127 | struct tm tm_t; | 142 | struct tm tm_t; |
128 | memset(&tm_t,0,sizeof(tm_t)); | 143 | memset(&tm_t,0,sizeof(tm_t)); |
129 | if( ( | 144 | if( ( |
130 | sscanf( | 145 | sscanf( |
131 | w.c_str(), | 146 | w.c_str(), |
132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 147 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 148 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 149 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
135 | ) != 6 | 150 | ) != 6 |
136 | ) && ( | 151 | ) && ( |
137 | sscanf( | 152 | sscanf( |
138 | w.c_str(), | 153 | w.c_str(), |
139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 154 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 155 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 156 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
142 | &fraction | 157 | &fraction |
143 | ) != 7 | 158 | ) != 7 |
144 | ) ) | 159 | ) ) |
145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 160 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
146 | tm_t.tm_mon--; | 161 | tm_t.tm_mon--; |
147 | tm_t.tm_year-=1900; | 162 | tm_t.tm_year-=1900; |
148 | time_t rv = mktime(&tm_t); | 163 | time_t rv = timegm(&tm_t); |
149 | if(rv==(time_t)-1) | 164 | if(rv==(time_t)-1) |
150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 165 | throw failed_conversion(OPKELE_CP_ "failed to gmtime()"); |
151 | return rv-timezone; | 166 | return rv; |
152 | } | 167 | } |
153 | 168 | ||
154 | /* | 169 | /* |
155 | * | 170 | * |
156 | */ | 171 | */ |
157 | 172 | ||
158 | static inline bool isrfc3986unreserved(int c) { | 173 | static inline bool isrfc3986unreserved(int c) { |
159 | if(c<'-') return false; | 174 | if(c<'-') return false; |
160 | if(c<='.') return true; | 175 | if(c<='.') return true; |
161 | if(c<'0') return false; if(c<='9') return true; | 176 | if(c<'0') return false; if(c<='9') return true; |
162 | if(c<'A') return false; if(c<='Z') return true; | 177 | if(c<'A') return false; if(c<='Z') return true; |
163 | if(c<'_') return false; | 178 | if(c<'_') return false; |
164 | if(c=='_') return true; | 179 | if(c=='_') return true; |
165 | if(c<'a') return false; if(c<='z') return true; | 180 | if(c<'a') return false; if(c<='z') return true; |
166 | if(c=='~') return true; | 181 | if(c=='~') return true; |
167 | return false; | 182 | return false; |
168 | } | 183 | } |
169 | 184 | ||
170 | struct __url_encoder : public unary_function<char,void> { | 185 | struct __url_encoder : public unary_function<char,void> { |
171 | public: | 186 | public: |
172 | string& rv; | 187 | string& rv; |
173 | 188 | ||
174 | __url_encoder(string& r) : rv(r) { } | 189 | __url_encoder(string& r) : rv(r) { } |
175 | 190 | ||
176 | result_type operator()(argument_type c) { | 191 | result_type operator()(argument_type c) { |
177 | if(isrfc3986unreserved(c)) | 192 | if(isrfc3986unreserved(c)) |
178 | rv += c; | 193 | rv += c; |
179 | else{ | 194 | else{ |
180 | char tmp[4]; | 195 | char tmp[4]; |
181 | snprintf(tmp,sizeof(tmp),"%%%02X", | 196 | snprintf(tmp,sizeof(tmp),"%%%02X", |
182 | (c&0xff)); | 197 | (c&0xff)); |
183 | rv += tmp; | 198 | rv += tmp; |