Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F18825986
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
83 KB
Subscribers
None
View Options
diff --git a/common/gettime.c b/common/gettime.c
index 2a9b71779..c3b0c6c6c 100644
--- a/common/gettime.c
+++ b/common/gettime.c
@@ -1,1024 +1,1047 @@
/* gettime.c - Wrapper for time functions
* Copyright (C) 1998, 2002, 2007, 2011 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* - 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.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* This file 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
+#include <stdint.h> /* We use uint64_t. */
#include "util.h"
#include "i18n.h"
#include "gettime.h"
#ifdef HAVE_W32_SYSTEM
#include <windows.h>
#endif
#ifdef HAVE_UNSIGNED_TIME_T
# define IS_INVALID_TIME_T(a) ((a) == (time_t)(-1))
#else
/* Error or 32 bit time_t and value after 2038-01-19. */
# define IS_INVALID_TIME_T(a) ((a) < 0)
#endif
static unsigned long timewarp;
static enum { NORMAL = 0, FROZEN, FUTURE, PAST } timemode;
/* Correction used to map to real Julian days. */
#define JD_DIFF 1721060L
/* Wrapper for the time(3). We use this here so we can fake the time
for tests */
time_t
gnupg_get_time (void)
{
time_t current = time (NULL);
if (current == (time_t)(-1))
log_fatal ("time() failed\n");
if (timemode == NORMAL)
return current;
else if (timemode == FROZEN)
return timewarp;
else if (timemode == FUTURE)
return current + timewarp;
else
return current - timewarp;
}
/* Wrapper around gmtime_r.
On systems without gmtime_r this implementation works within gnupg
because we use only one thread a time. FIXME: An independent
library may use gmtime in one of its own thread (or via
npth_enter/npth_leave) - in this case we run into a problem. The
solution would be to use a mutex here. */
struct tm *
gnupg_gmtime (const time_t *timep, struct tm *result)
{
#ifdef HAVE_GMTIME_R
return gmtime_r (timep, result);
#else
struct tm *tp;
tp = gmtime (timep);
if (tp)
memcpy (result, tp, sizeof *result);
return tp;
#endif
}
/* Return the current time (possibly faked) in ISO format. */
void
gnupg_get_isotime (gnupg_isotime_t timebuf)
{
time_t atime = gnupg_get_time ();
struct tm *tp;
struct tm tmbuf;
tp = gnupg_gmtime (&atime, &tmbuf);
if (!tp)
*timebuf = 0;
else
snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec);
}
/* Set the time to NEWTIME so that gnupg_get_time returns a time
starting with this one. With FREEZE set to 1 the returned time
will never change. Just for completeness, a value of (time_t)-1
for NEWTIME gets you back to reality. Note that this is obviously
not thread-safe but this is not required. */
void
gnupg_set_time (time_t newtime, int freeze)
{
time_t current = time (NULL);
if ( newtime == (time_t)-1 || current == newtime)
{
timemode = NORMAL;
timewarp = 0;
}
else if (freeze)
{
timemode = FROZEN;
timewarp = newtime == (time_t)-1 ? current : newtime;
}
else if (newtime > current)
{
timemode = FUTURE;
timewarp = newtime - current;
}
else
{
timemode = PAST;
timewarp = current - newtime;
}
}
/* Returns true when we are in timewarp mode */
int
gnupg_faked_time_p (void)
{
return timemode;
}
/* This function is used by gpg because OpenPGP defines the timestamp
as an unsigned 32 bit value. */
u32
make_timestamp (void)
{
time_t t = gnupg_get_time ();
return (u32)t;
}
+/* Specialized version of atoi which returns an u32 instead of an int
+ * and caps the result at 2^32-2. Leading white space is skipped,
+ * scanning stops at at the first non-convertable byte. Note that we
+ * do not cap at 2^32-1 because that value is often used as error
+ * return. */
+u32
+scan_secondsstr (const char *string)
+{
+ uint64_t value = 0;
+
+ while (*string == ' ' || *string == '\t')
+ string++;
+ for (; *string >= '0' && *string <= '9'; string++)
+ {
+ value *= 10;
+ value += atoi_1 (string);
+ if (value >= (u32)(-1))
+ return (u32)(-1) - 1;
+ }
+ return (u32)value;
+}
+
/****************
* Scan a date string and return a timestamp.
* The only supported format is "yyyy-mm-dd"
* Returns 0 for an invalid date.
*/
u32
scan_isodatestr( const char *string )
{
int year, month, day;
struct tm tmbuf;
time_t stamp;
int i;
if( strlen(string) != 10 || string[4] != '-' || string[7] != '-' )
return 0;
for( i=0; i < 4; i++ )
if( !digitp (string+i) )
return 0;
if( !digitp (string+5) || !digitp(string+6) )
return 0;
if( !digitp(string+8) || !digitp(string+9) )
return 0;
year = atoi(string);
month = atoi(string+5);
day = atoi(string+8);
/* some basic checks */
if( year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 )
return 0;
memset( &tmbuf, 0, sizeof tmbuf );
tmbuf.tm_mday = day;
tmbuf.tm_mon = month-1;
tmbuf.tm_year = year - 1900;
tmbuf.tm_isdst = -1;
stamp = mktime( &tmbuf );
if( stamp == (time_t)-1 )
return 0;
return stamp;
}
int
isotime_p (const char *string)
{
const char *s;
int i;
if (!*string)
return 0;
for (s=string, i=0; i < 8; i++, s++)
if (!digitp (s))
return 0;
if (*s != 'T')
return 0;
for (s++, i=9; i < 15; i++, s++)
if (!digitp (s))
return 0;
if (*s == 'Z')
s++;
if ( !(!*s || (isascii (*s) && isspace(*s)) || *s == ':' || *s == ','))
return 0; /* Wrong delimiter. */
return 1;
}
/* Scan a string and return true if the string represents the human
readable format of an ISO time. This format is:
yyyy-mm-dd[ hh[:mm[:ss]]]
Scanning stops at the second space or at a comma. If DATE_ONLY is
true the time part is not expected and the scanning stops at the
first space or at a comma. */
int
isotime_human_p (const char *string, int date_only)
{
const char *s;
int i;
if (!*string)
return 0;
for (s=string, i=0; i < 4; i++, s++)
if (!digitp (s))
return 0;
if (*s != '-')
return 0;
s++;
if (!digitp (s) || !digitp (s+1) || s[2] != '-')
return 0;
i = atoi_2 (s);
if (i < 1 || i > 12)
return 0;
s += 3;
if (!digitp (s) || !digitp (s+1))
return 0;
i = atoi_2 (s);
if (i < 1 || i > 31)
return 0;
s += 2;
if (!*s || *s == ',')
return 1; /* Okay; only date given. */
if (!spacep (s))
return 0;
if (date_only)
return 1; /* Okay; only date was requested. */
s++;
if (spacep (s))
return 1; /* Okay, second space stops scanning. */
if (!digitp (s) || !digitp (s+1))
return 0;
i = atoi_2 (s);
if (i < 0 || i > 23)
return 0;
s += 2;
if (!*s || *s == ',')
return 1; /* Okay; only date and hour given. */
if (*s != ':')
return 0;
s++;
if (!digitp (s) || !digitp (s+1))
return 0;
i = atoi_2 (s);
if (i < 0 || i > 59)
return 0;
s += 2;
if (!*s || *s == ',')
return 1; /* Okay; only date, hour and minute given. */
if (*s != ':')
return 0;
s++;
if (!digitp (s) || !digitp (s+1))
return 0;
i = atoi_2 (s);
if (i < 0 || i > 60)
return 0;
s += 2;
if (!*s || *s == ',' || spacep (s))
return 1; /* Okay; date, hour and minute and second given. */
return 0; /* Unexpected delimiter. */
}
/* Convert a standard isotime or a human readable variant into an
isotime structure. The allowed formats are those described by
isotime_p and isotime_human_p. The function returns 0 on failure
or the length of the scanned string on success. */
size_t
string2isotime (gnupg_isotime_t atime, const char *string)
{
gnupg_isotime_t dummyatime;
if (!atime)
atime = dummyatime;
atime[0] = 0;
if (isotime_p (string))
{
memcpy (atime, string, 15);
atime[15] = 0;
return 15;
}
if (!isotime_human_p (string, 0))
return 0;
atime[0] = string[0];
atime[1] = string[1];
atime[2] = string[2];
atime[3] = string[3];
atime[4] = string[5];
atime[5] = string[6];
atime[6] = string[8];
atime[7] = string[9];
atime[8] = 'T';
memset (atime+9, '0', 6);
atime[15] = 0;
if (!spacep (string+10))
return 10;
if (spacep (string+11))
return 11; /* As per def, second space stops scanning. */
atime[9] = string[11];
atime[10] = string[12];
if (string[13] != ':')
return 13;
atime[11] = string[14];
atime[12] = string[15];
if (string[16] != ':')
return 16;
atime[13] = string[17];
atime[14] = string[18];
return 19;
}
/* Scan an ISO timestamp and return an Epoch based timestamp. The
only supported format is "yyyymmddThhmmss[Z]" delimited by white
space, nul, a colon or a comma. Returns (time_t)(-1) for an
invalid string. */
time_t
isotime2epoch (const char *string)
{
int year, month, day, hour, minu, sec;
struct tm tmbuf;
if (!isotime_p (string))
return (time_t)(-1);
year = atoi_4 (string);
month = atoi_2 (string + 4);
day = atoi_2 (string + 6);
hour = atoi_2 (string + 9);
minu = atoi_2 (string + 11);
sec = atoi_2 (string + 13);
/* Basic checks. */
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31
|| hour > 23 || minu > 59 || sec > 61 )
return (time_t)(-1);
memset (&tmbuf, 0, sizeof tmbuf);
tmbuf.tm_sec = sec;
tmbuf.tm_min = minu;
tmbuf.tm_hour = hour;
tmbuf.tm_mday = day;
tmbuf.tm_mon = month-1;
tmbuf.tm_year = year - 1900;
tmbuf.tm_isdst = -1;
return timegm (&tmbuf);
}
/* Convert an Epoch time to an iso time stamp. */
void
epoch2isotime (gnupg_isotime_t timebuf, time_t atime)
{
if (atime == (time_t)(-1))
*timebuf = 0;
else
{
struct tm *tp;
#ifdef HAVE_GMTIME_R
struct tm tmbuf;
tp = gmtime_r (&atime, &tmbuf);
#else
tp = gmtime (&atime);
#endif
snprintf (timebuf, 16, "%04d%02d%02dT%02d%02d%02d",
1900 + tp->tm_year, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec);
}
}
/* Parse a short ISO date string (YYYY-MM-DD) into a TM structure.
Returns 0 on success. */
int
isodate_human_to_tm (const char *string, struct tm *t)
{
int year, month, day;
if (!isotime_human_p (string, 1))
return -1;
year = atoi_4 (string);
month = atoi_2 (string + 5);
day = atoi_2 (string + 8);
/* Basic checks. */
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31)
return -1;
memset (t, 0, sizeof *t);
t->tm_sec = 0;
t->tm_min = 0;
t->tm_hour = 0;
t->tm_mday = day;
t->tm_mon = month-1;
t->tm_year = year - 1900;
t->tm_isdst = -1;
return 0;
}
/* This function is a copy of gpgme/src/conversion.c:_gpgme_timegm.
If you change it, then update the other one too. */
#ifdef HAVE_W32_SYSTEM
static time_t
_win32_timegm (struct tm *tm)
{
/* This one is thread safe. */
SYSTEMTIME st;
FILETIME ft;
unsigned long long cnsecs;
st.wYear = tm->tm_year + 1900;
st.wMonth = tm->tm_mon + 1;
st.wDay = tm->tm_mday;
st.wHour = tm->tm_hour;
st.wMinute = tm->tm_min;
st.wSecond = tm->tm_sec;
st.wMilliseconds = 0; /* Not available. */
st.wDayOfWeek = 0; /* Ignored. */
/* System time is UTC thus the conversion is pretty easy. */
if (!SystemTimeToFileTime (&st, &ft))
{
gpg_err_set_errno (EINVAL);
return (time_t)(-1);
}
cnsecs = (((unsigned long long)ft.dwHighDateTime << 32)
| ft.dwLowDateTime);
cnsecs -= 116444736000000000ULL; /* The filetime epoch is 1601-01-01. */
return (time_t)(cnsecs / 10000000ULL);
}
#endif
/* Parse the string TIMESTAMP into a time_t. The string may either be
seconds since Epoch or in the ISO 8601 format like
"20390815T143012". Returns 0 for an empty string or seconds since
Epoch. Leading spaces are skipped. If ENDP is not NULL, it will
point to the next non-parsed character in TIMESTRING.
This function is a copy of
gpgme/src/conversion.c:_gpgme_parse_timestamp. If you change it,
then update the other one too. */
time_t
parse_timestamp (const char *timestamp, char **endp)
{
/* Need to skip leading spaces, because that is what strtoul does
but not our ISO 8601 checking code. */
while (*timestamp && *timestamp== ' ')
timestamp++;
if (!*timestamp)
return 0;
if (strlen (timestamp) >= 15 && timestamp[8] == 'T')
{
struct tm buf;
int year;
year = atoi_4 (timestamp);
if (year < 1900)
return (time_t)(-1);
if (endp)
*endp = (char*)(timestamp + 15);
/* Fixme: We would better use a configure test to see whether
mktime can handle dates beyond 2038. */
if (sizeof (time_t) <= 4 && year >= 2038)
return (time_t)2145914603; /* 2037-12-31 23:23:23 */
memset (&buf, 0, sizeof buf);
buf.tm_year = year - 1900;
buf.tm_mon = atoi_2 (timestamp+4) - 1;
buf.tm_mday = atoi_2 (timestamp+6);
buf.tm_hour = atoi_2 (timestamp+9);
buf.tm_min = atoi_2 (timestamp+11);
buf.tm_sec = atoi_2 (timestamp+13);
#ifdef HAVE_W32_SYSTEM
return _win32_timegm (&buf);
#else
#ifdef HAVE_TIMEGM
return timegm (&buf);
#else
{
time_t tim;
putenv ("TZ=UTC");
tim = mktime (&buf);
#ifdef __GNUC__
#warning fixme: we must somehow reset TZ here. It is not threadsafe anyway.
#endif
return tim;
}
#endif /* !HAVE_TIMEGM */
#endif /* !HAVE_W32_SYSTEM */
}
else
return (time_t)strtoul (timestamp, endp, 10);
}
u32
add_days_to_timestamp( u32 stamp, u16 days )
{
return stamp + days*86400L;
}
/****************
* Return a string with a time value in the form: x Y, n D, n H
*/
const char *
strtimevalue( u32 value )
{
static char buffer[30];
unsigned int years, days, hours, minutes;
value /= 60;
minutes = value % 60;
value /= 60;
hours = value % 24;
value /= 24;
days = value % 365;
value /= 365;
years = value;
sprintf(buffer,"%uy%ud%uh%um", years, days, hours, minutes );
if( years )
return buffer;
if( days )
return strchr( buffer, 'y' ) + 1;
return strchr( buffer, 'd' ) + 1;
}
/* Return a malloced string with the time elapsed between NOW and
SINCE. May return NULL on error. */
char *
elapsed_time_string (time_t since, time_t now)
{
char *result;
double diff;
unsigned long value;
unsigned int days, hours, minutes, seconds;
if (!now)
now = gnupg_get_time ();
diff = difftime (now, since);
if (diff < 0)
return xtrystrdup ("time-warp");
seconds = (unsigned long)diff % 60;
value = (unsigned long)(diff / 60);
minutes = value % 60;
value /= 60;
hours = value % 24;
value /= 24;
days = value % 365;
if (days)
result = xtryasprintf ("%ud%uh%um%us", days, hours, minutes, seconds);
else if (hours)
result = xtryasprintf ("%uh%um%us", hours, minutes, seconds);
else if (minutes)
result = xtryasprintf ("%um%us", minutes, seconds);
else
result = xtryasprintf ("%us", seconds);
return result;
}
/*
* Note: this function returns GMT
*/
const char *
strtimestamp (u32 stamp)
{
static char buffer[11+5];
struct tm *tp;
time_t atime = stamp;
if (IS_INVALID_TIME_T (atime))
{
strcpy (buffer, "????" "-??" "-??");
}
else
{
tp = gmtime( &atime );
snprintf (buffer, sizeof buffer, "%04d-%02d-%02d",
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
}
return buffer;
}
/*
* Note: this function returns GMT
*/
const char *
isotimestamp (u32 stamp)
{
static char buffer[25+5];
struct tm *tp;
time_t atime = stamp;
if (IS_INVALID_TIME_T (atime))
{
strcpy (buffer, "????" "-??" "-??" " " "??" ":" "??" ":" "??");
}
else
{
tp = gmtime ( &atime );
snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec);
}
return buffer;
}
/****************
* Note: this function returns local time
*/
const char *
asctimestamp (u32 stamp)
{
static char buffer[80];
#if defined (HAVE_STRFTIME) && defined (HAVE_NL_LANGINFO)
static char fmt[80];
#endif
struct tm *tp;
time_t atime = stamp;
if (IS_INVALID_TIME_T (atime))
{
strcpy (buffer, "????" "-??" "-??");
return buffer;
}
tp = localtime( &atime );
#ifdef HAVE_STRFTIME
# if defined(HAVE_NL_LANGINFO)
mem2str( fmt, nl_langinfo(D_T_FMT), DIM(fmt)-3 );
if (!strstr( fmt, "%Z" ))
strcat( fmt, " %Z");
/* NOTE: gcc -Wformat-noliteral will complain here. I have found no
way to suppress this warning. */
strftime (buffer, DIM(buffer)-1, fmt, tp);
# else
# if HAVE_W32_SYSTEM
{
static int done;
if (!done)
{
/* The locale names as used by Windows are in the form
* "German_Germany.1252" or "German_Austria.1252" with
* alternate names similar to Unix, e.g. "de-DE". However
* that is the theory. On current Windows and Mingw the
* alternate names do not work. We would need a table to map
* them from the short names as provided by gpgrt to the long
* names and append some code page. For now we use "" and
* take the locale from the user's system settings. Thus the
* standard Unix envvars don't work for time and may mismatch
* with the string translations. The new UCRT available since
* 2018 has a lot of additional support but that will for sure
* break other things. We should move to ISO strings to get
* rid of such problems. */
setlocale (LC_TIME, "");
done = 1;
/* log_debug ("LC_ALL now '%s'\n", setlocale (LC_ALL, NULL)); */
/* log_debug ("LC_TIME now '%s'\n", setlocale (LC_TIME, NULL)); */
}
}
# endif
/* FIXME: we should check whether the locale appends a " %Z" These
* locales from glibc don't put the " %Z": fi_FI hr_HR ja_JP lt_LT
* lv_LV POSIX ru_RU ru_SU sv_FI sv_SE zh_CN. */
strftime (buffer, DIM(buffer)-1, "%c %Z", tp);
# endif
buffer[DIM(buffer)-1] = 0;
#else
mem2str( buffer, asctime(tp), DIM(buffer) );
#endif
return buffer;
}
/* Return the timestamp STAMP in RFC-2822 format. This is always done
* in the C locale. We return the gmtime to avoid computing the
* timezone. The caller must release the returned string.
*
* Example: "Mon, 27 Jun 2016 1:42:00 +0000".
*/
char *
rfctimestamp (u32 stamp)
{
time_t atime = stamp;
struct tm tmbuf, *tp;
if (IS_INVALID_TIME_T (atime))
{
gpg_err_set_errno (EINVAL);
return NULL;
}
tp = gnupg_gmtime (&atime, &tmbuf);
if (!tp)
return NULL;
return xtryasprintf ("%.3s, %02d %.3s %04d %02d:%02d:%02d +0000",
&"SunMonTueWedThuFriSat"[(tp->tm_wday%7)*3],
tp->tm_mday,
&"JanFebMarAprMayJunJulAugSepOctNovDec"
[(tp->tm_mon%12)*3],
tp->tm_year + 1900,
tp->tm_hour,
tp->tm_min,
tp->tm_sec);
}
static int
days_per_year (int y)
{
int s ;
s = !(y % 4);
if ( !(y % 100))
if ((y%400))
s = 0;
return s ? 366 : 365;
}
static int
days_per_month (int y, int m)
{
int s;
switch(m)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31 ;
case 2:
s = !(y % 4);
if (!(y % 100))
if ((y % 400))
s = 0;
return s? 29 : 28 ;
case 4: case 6: case 9: case 11:
return 30;
}
BUG();
}
/* Convert YEAR, MONTH and DAY into the Julian date. We assume that
it is already noon. We do not support dates before 1582-10-15. */
static unsigned long
date2jd (int year, int month, int day)
{
unsigned long jd;
jd = 365L * year + 31 * (month-1) + day + JD_DIFF;
if (month < 3)
year-- ;
else
jd -= (4 * month + 23) / 10;
jd += year / 4 - ((year / 100 + 1) *3) / 4;
return jd ;
}
/* Convert a Julian date back to YEAR, MONTH and DAY. Return day of
the year or 0 on error. This function uses some more or less
arbitrary limits, most important is that days before 1582 are not
supported. */
static int
jd2date (unsigned long jd, int *year, int *month, int *day)
{
int y, m, d;
long delta;
if (!jd)
return 0 ;
if (jd < 1721425 || jd > 2843085)
return 0;
y = (jd - JD_DIFF) / 366;
d = m = 1;
while ((delta = jd - date2jd (y, m, d)) > days_per_year (y))
y++;
m = (delta / 31) + 1;
while( (delta = jd - date2jd (y, m, d)) > days_per_month (y,m))
if (++m > 12)
{
m = 1;
y++;
}
d = delta + 1 ;
if (d > days_per_month (y, m))
{
d = 1;
m++;
}
if (m > 12)
{
m = 1;
y++;
}
if (year)
*year = y;
if (month)
*month = m;
if (day)
*day = d ;
return (jd - date2jd (y, 1, 1)) + 1;
}
/* Check that the 15 bytes in ATIME represent a valid ISO time. Note
that this function does not expect a string but a plain 15 byte
isotime buffer. */
gpg_error_t
check_isotime (const gnupg_isotime_t atime)
{
int i;
const char *s;
if (!*atime)
return gpg_error (GPG_ERR_NO_VALUE);
for (s=atime, i=0; i < 8; i++, s++)
if (!digitp (s))
return gpg_error (GPG_ERR_INV_TIME);
if (*s != 'T')
return gpg_error (GPG_ERR_INV_TIME);
for (s++, i=9; i < 15; i++, s++)
if (!digitp (s))
return gpg_error (GPG_ERR_INV_TIME);
return 0;
}
/* Dump the ISO time T to the log stream without a LF. */
void
dump_isotime (const gnupg_isotime_t t)
{
if (!t || !*t)
log_printf ("%s", _("[none]"));
else
log_printf ("%.4s-%.2s-%.2s %.2s:%.2s:%s",
t, t+4, t+6, t+9, t+11, t+13);
}
/* Copy one ISO date to another, this is inline so that we can do a
minimal sanity check. A null date (empty string) is allowed. */
void
gnupg_copy_time (gnupg_isotime_t d, const gnupg_isotime_t s)
{
if (*s)
{
if ((strlen (s) != 15 || s[8] != 'T'))
BUG();
memcpy (d, s, 15);
d[15] = 0;
}
else
*d = 0;
}
/* Add SECONDS to ATIME. SECONDS may not be negative and is limited
to about the equivalent of 62 years which should be more then
enough for our purposes. */
gpg_error_t
add_seconds_to_isotime (gnupg_isotime_t atime, int nseconds)
{
gpg_error_t err;
int year, month, day, hour, minute, sec, ndays;
unsigned long jd;
err = check_isotime (atime);
if (err)
return err;
if (nseconds < 0 || nseconds >= (0x7fffffff - 61) )
return gpg_error (GPG_ERR_INV_VALUE);
year = atoi_4 (atime+0);
month = atoi_2 (atime+4);
day = atoi_2 (atime+6);
hour = atoi_2 (atime+9);
minute= atoi_2 (atime+11);
sec = atoi_2 (atime+13);
if (year <= 1582) /* The julian date functions don't support this. */
return gpg_error (GPG_ERR_INV_VALUE);
sec += nseconds;
minute += sec/60;
sec %= 60;
hour += minute/60;
minute %= 60;
ndays = hour/24;
hour %= 24;
jd = date2jd (year, month, day) + ndays;
jd2date (jd, &year, &month, &day);
if (year > 9999 || month > 12 || day > 31
|| year < 0 || month < 1 || day < 1)
return gpg_error (GPG_ERR_INV_VALUE);
snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
year, month, day, hour, minute, sec);
return 0;
}
gpg_error_t
add_days_to_isotime (gnupg_isotime_t atime, int ndays)
{
gpg_error_t err;
int year, month, day, hour, minute, sec;
unsigned long jd;
err = check_isotime (atime);
if (err)
return err;
if (ndays < 0 || ndays >= 9999*366 )
return gpg_error (GPG_ERR_INV_VALUE);
year = atoi_4 (atime+0);
month = atoi_2 (atime+4);
day = atoi_2 (atime+6);
hour = atoi_2 (atime+9);
minute= atoi_2 (atime+11);
sec = atoi_2 (atime+13);
if (year <= 1582) /* The julian date functions don't support this. */
return gpg_error (GPG_ERR_INV_VALUE);
jd = date2jd (year, month, day) + ndays;
jd2date (jd, &year, &month, &day);
if (year > 9999 || month > 12 || day > 31
|| year < 0 || month < 1 || day < 1)
return gpg_error (GPG_ERR_INV_VALUE);
snprintf (atime, 16, "%04d%02d%02dT%02d%02d%02d",
year, month, day, hour, minute, sec);
return 0;
}
diff --git a/common/gettime.h b/common/gettime.h
index 4f7199f92..18f65ab1a 100644
--- a/common/gettime.h
+++ b/common/gettime.h
@@ -1,75 +1,76 @@
/* gettime.h - Wrapper for time functions
* Copyright (C) 2010, 2012 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* - 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.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* This file 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#ifndef GNUPG_COMMON_GETTIME_H
#define GNUPG_COMMON_GETTIME_H
#include <time.h> /* We need time_t. */
#include <gpg-error.h> /* We need gpg_error_t. */
/* A type to hold the ISO time. Note that this is the same as
the KSBA type ksba_isotime_t. */
typedef char gnupg_isotime_t[16];
/* Constant string of 16-byte, which is compatible to the type
gnupg_iso_time_t. */
#define GNUPG_ISOTIME_NONE \
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
time_t gnupg_get_time (void);
struct tm *gnupg_gmtime (const time_t *timep, struct tm *result);
void gnupg_get_isotime (gnupg_isotime_t timebuf);
void gnupg_set_time (time_t newtime, int freeze);
int gnupg_faked_time_p (void);
u32 make_timestamp (void);
char *elapsed_time_string (time_t since, time_t now);
+u32 scan_secondsstr (const char *string);
u32 scan_isodatestr (const char *string);
int isotime_p (const char *string);
int isotime_human_p (const char *string, int date_only);
size_t string2isotime (gnupg_isotime_t atime, const char *string);
time_t isotime2epoch (const char *string);
void epoch2isotime (gnupg_isotime_t timebuf, time_t atime);
int isodate_human_to_tm (const char *string, struct tm *t);
time_t parse_timestamp (const char *timestamp, char **endp);
u32 add_days_to_timestamp (u32 stamp, u16 days);
const char *strtimevalue (u32 stamp);
const char *strtimestamp (u32 stamp); /* GMT */
const char *isotimestamp (u32 stamp); /* GMT */
const char *asctimestamp (u32 stamp); /* localized */
char *rfctimestamp (u32 stamp); /* RFC format, malloced. */
gpg_error_t add_seconds_to_isotime (gnupg_isotime_t atime, int nseconds);
gpg_error_t add_days_to_isotime (gnupg_isotime_t atime, int ndays);
gpg_error_t check_isotime (const gnupg_isotime_t atime);
void dump_isotime (const gnupg_isotime_t atime);
void gnupg_copy_time (gnupg_isotime_t d, const gnupg_isotime_t s);
#endif /*GNUPG_COMMON_GETTIME_H*/
diff --git a/common/stringhelp.c b/common/stringhelp.c
index 5407653de..9a2265258 100644
--- a/common/stringhelp.c
+++ b/common/stringhelp.c
@@ -1,1854 +1,1854 @@
/* stringhelp.c - standard string helper functions
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
* 2008, 2009, 2010 Free Software Foundation, Inc.
* Copyright (C) 2014 Werner Koch
* Copyright (C) 2015, 2021 g10 Code GmbH
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute and/or modify this
* part of GnuPG under the terms of either
*
* - 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.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* GnuPG 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
* General Public License for more details.
*
* You should have received a copies of the GNU General Public License
* and the GNU Lesser General Public License along with this program;
* if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later)
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_W32_SYSTEM
# ifdef HAVE_WINSOCK2_H
# include <winsock2.h>
# endif
# include <windows.h>
#endif
#include <limits.h>
#include "util.h"
#include "common-defs.h"
#include "utf8conv.h"
#include "sysutils.h"
#include "stringhelp.h"
#define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
/* Sometimes we want to avoid mixing slashes and backslashes on W32
and prefer backslashes. There is usual no problem with mixing
them, however a very few W32 API calls can't grok plain slashes.
Printing filenames with mixed slashes also looks a bit strange.
This function has no effext on POSIX. */
static inline char *
change_slashes (char *name)
{
#ifdef HAVE_DOSISH_SYSTEM
char *p;
if (strchr (name, '\\'))
{
for (p=name; *p; p++)
if (*p == '/')
*p = '\\';
}
#endif /*HAVE_DOSISH_SYSTEM*/
return name;
}
/*
* Check whether STRING starts with KEYWORD. The keyword is
* delimited by end of string, a space or a tab. Returns NULL if not
* found or a pointer into STRING to the next non-space character
* after the KEYWORD (which may be end of string).
*/
char *
has_leading_keyword (const char *string, const char *keyword)
{
size_t n = strlen (keyword);
if (!strncmp (string, keyword, n)
&& (!string[n] || string[n] == ' ' || string[n] == '\t'))
{
string += n;
while (*string == ' ' || *string == '\t')
string++;
return (char*)string;
}
return NULL;
}
/*
* Look for the substring SUB in buffer and return a pointer to that
* substring in BUFFER or NULL if not found.
* Comparison is case-insensitive.
*/
const char *
memistr (const void *buffer, size_t buflen, const char *sub)
{
const unsigned char *buf = buffer;
const unsigned char *t = (const unsigned char *)buffer;
const unsigned char *s = (const unsigned char *)sub;
size_t n = buflen;
for ( ; n ; t++, n-- )
{
if ( toupper (*t) == toupper (*s) )
{
for ( buf=t++, buflen = n--, s++;
n && toupper (*t) == toupper (*s); t++, s++, n-- )
;
if (!*s)
return (const char*)buf;
t = buf;
s = (const unsigned char *)sub ;
n = buflen;
}
}
return NULL;
}
const char *
ascii_memistr ( const void *buffer, size_t buflen, const char *sub )
{
const unsigned char *buf = buffer;
const unsigned char *t = (const unsigned char *)buf;
const unsigned char *s = (const unsigned char *)sub;
size_t n = buflen;
for ( ; n ; t++, n-- )
{
if (ascii_toupper (*t) == ascii_toupper (*s) )
{
for ( buf=t++, buflen = n--, s++;
n && ascii_toupper (*t) == ascii_toupper (*s); t++, s++, n-- )
;
if (!*s)
return (const char*)buf;
t = (const unsigned char *)buf;
s = (const unsigned char *)sub ;
n = buflen;
}
}
return NULL;
}
/* This is a case-sensitive version of our memistr. I wonder why no
* standard function memstr exists but we better do not use the name
* memstr to avoid future conflicts.
*/
const char *
gnupg_memstr (const void *buffer, size_t buflen, const char *sub)
{
const unsigned char *buf = buffer;
const unsigned char *t = (const unsigned char *)buf;
const unsigned char *s = (const unsigned char *)sub;
size_t n = buflen;
for ( ; n ; t++, n-- )
{
if (*t == *s)
{
for (buf = t++, buflen = n--, s++; n && *t ==*s; t++, s++, n--)
;
if (!*s)
return (const char*)buf;
t = (const unsigned char *)buf;
s = (const unsigned char *)sub ;
n = buflen;
}
}
return NULL;
}
/* This function is similar to strncpy(). However it won't copy more
* than N - 1 characters and makes sure that a '\0' is appended. With
* N given as 0, nothing will happen. With DEST given as NULL, memory
* will be allocated using xmalloc (i.e. if it runs out of core the
* function terminates). Returns DEST or a pointer to the allocated
* memory.
*/
char *
mem2str (char *dest, const void *src, size_t n)
{
char *d;
const char *s;
if (n)
{
if (!dest)
dest = xmalloc (n);
d = dest;
s = src ;
for (n--; n && *s; n--)
*d++ = *s++;
*d = '\0' ;
}
return dest;
}
/****************
* remove leading and trailing white spaces
*/
char *
trim_spaces( char *str )
{
char *string, *p, *mark;
string = str;
/* find first non space character */
for( p=string; *p && isspace( *(byte*)p ) ; p++ )
;
/* move characters */
for( (mark = NULL); (*string = *p); string++, p++ )
if( isspace( *(byte*)p ) ) {
if( !mark )
mark = string ;
}
else
mark = NULL ;
if( mark )
*mark = '\0' ; /* remove trailing spaces */
return str ;
}
/* Same as trim_spaces but only consider, space, tab, cr and lf as space. */
char *
ascii_trim_spaces (char *str)
{
char *string, *p, *mark;
string = str;
/* Find first non-ascii space character. */
for (p=string; *p && ascii_isspace (*p); p++)
;
/* Move characters. */
for (mark=NULL; (*string = *p); string++, p++ )
{
if (ascii_isspace (*p))
{
if (!mark)
mark = string;
}
else
mark = NULL ;
}
if (mark)
*mark = '\0' ; /* Remove trailing spaces. */
return str ;
}
/****************
* remove trailing white spaces
*/
char *
trim_trailing_spaces( char *string )
{
char *p, *mark;
for( mark = NULL, p = string; *p; p++ ) {
if( isspace( *(byte*)p ) ) {
if( !mark )
mark = p;
}
else
mark = NULL;
}
if( mark )
*mark = '\0' ;
return string ;
}
unsigned
trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
{
byte *p, *mark;
unsigned n;
for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
if( strchr(trimchars, *p ) ) {
if( !mark )
mark = p;
}
else
mark = NULL;
}
if( mark ) {
*mark = 0;
return mark - line;
}
return len;
}
/****************
* remove trailing white spaces and return the length of the buffer
*/
unsigned
trim_trailing_ws( byte *line, unsigned len )
{
return trim_trailing_chars( line, len, " \t\r\n" );
}
size_t
length_sans_trailing_chars (const unsigned char *line, size_t len,
const char *trimchars )
{
const unsigned char *p, *mark;
size_t n;
for( mark=NULL, p=line, n=0; n < len; n++, p++ )
{
if (strchr (trimchars, *p ))
{
if( !mark )
mark = p;
}
else
mark = NULL;
}
if (mark)
return mark - line;
return len;
}
/*
* Return the length of line ignoring trailing white-space.
*/
size_t
length_sans_trailing_ws (const unsigned char *line, size_t len)
{
return length_sans_trailing_chars (line, len, " \t\r\n");
}
/*
* Extract from a given path the filename component. This function
* terminates the process on memory shortage.
*/
char *
make_basename(const char *filepath, const char *inputpath)
{
#ifdef __riscos__
return riscos_make_basename(filepath, inputpath);
#else
char *p;
(void)inputpath; /* Only required for riscos. */
if ( !(p=strrchr(filepath, '/')) )
#ifdef HAVE_DOSISH_SYSTEM
if ( !(p=strrchr(filepath, '\\')) )
#endif
#ifdef HAVE_DRIVE_LETTERS
if ( !(p=strrchr(filepath, ':')) )
#endif
{
return xstrdup(filepath);
}
return xstrdup(p+1);
#endif
}
/*
* Extract from a given filename the path prepended to it. If there
* isn't a path prepended to the filename, a dot is returned ('.').
* This function terminates the process on memory shortage.
*/
char *
make_dirname(const char *filepath)
{
char *dirname;
int dirname_length;
char *p;
if ( !(p=strrchr(filepath, '/')) )
#ifdef HAVE_DOSISH_SYSTEM
if ( !(p=strrchr(filepath, '\\')) )
#endif
#ifdef HAVE_DRIVE_LETTERS
if ( !(p=strrchr(filepath, ':')) )
#endif
{
return xstrdup(".");
}
dirname_length = p-filepath;
dirname = xmalloc(dirname_length+1);
strncpy(dirname, filepath, dirname_length);
dirname[dirname_length] = 0;
return dirname;
}
static char *
get_pwdir (int xmode, const char *name)
{
char *result = NULL;
#ifdef HAVE_PWD_H
struct passwd *pwd = NULL;
if (name)
{
#ifdef HAVE_GETPWNAM
/* Fixme: We should use getpwnam_r if available. */
pwd = getpwnam (name);
#endif
}
else
{
#ifdef HAVE_GETPWUID
/* Fixme: We should use getpwuid_r if available. */
pwd = getpwuid (getuid());
#endif
}
if (pwd)
{
if (xmode)
result = xstrdup (pwd->pw_dir);
else
result = xtrystrdup (pwd->pw_dir);
}
#else /*!HAVE_PWD_H*/
/* No support at all. */
(void)xmode;
(void)name;
#endif /*HAVE_PWD_H*/
return result;
}
/* xmode 0 := Return NULL on error
1 := Terminate on error
2 := Make sure that name is absolute; return NULL on error
3 := Make sure that name is absolute; terminate on error
*/
static char *
do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
{
const char *argv[32];
int argc;
size_t n;
int skip = 1;
char *home_buffer = NULL;
char *name, *home, *p;
int want_abs;
want_abs = !!(xmode & 2);
xmode &= 1;
n = strlen (first_part) + 1;
argc = 0;
while ( (argv[argc] = va_arg (arg_ptr, const char *)) )
{
n += strlen (argv[argc]) + 1;
if (argc >= DIM (argv)-1)
{
if (xmode)
BUG ();
gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
}
n++;
home = NULL;
if (*first_part == '~')
{
if (first_part[1] == '/' || !first_part[1])
{
/* This is the "~/" or "~" case. */
home = getenv("HOME");
if (!home)
home = home_buffer = get_pwdir (xmode, NULL);
if (home && *home)
n += strlen (home);
}
else
{
/* This is the "~username/" or "~username" case. */
char *user;
if (xmode)
user = xstrdup (first_part+1);
else
{
user = xtrystrdup (first_part+1);
if (!user)
return NULL;
}
p = strchr (user, '/');
if (p)
*p = 0;
skip = 1 + strlen (user);
home = home_buffer = get_pwdir (xmode, user);
xfree (user);
if (home)
n += strlen (home);
else
skip = 1;
}
}
if (xmode)
name = xmalloc (n);
else
{
name = xtrymalloc (n);
if (!name)
{
xfree (home_buffer);
return NULL;
}
}
if (home)
p = stpcpy (stpcpy (name, home), first_part + skip);
else
p = stpcpy (name, first_part);
xfree (home_buffer);
for (argc=0; argv[argc]; argc++)
{
/* Avoid a leading double slash if the first part was "/". */
if (!argc && name[0] == '/' && !name[1])
p = stpcpy (p, argv[argc]);
else
p = stpcpy (stpcpy (p, "/"), argv[argc]);
}
if (want_abs)
{
#ifdef HAVE_DRIVE_LETTERS
p = strchr (name, ':');
if (p)
p++;
else
p = name;
#else
p = name;
#endif
if (*p != '/'
#ifdef HAVE_DRIVE_LETTERS
&& *p != '\\'
#endif
)
{
home = gnupg_getcwd ();
if (!home)
{
if (xmode)
{
fprintf (stderr, "\nfatal: getcwd failed: %s\n",
strerror (errno));
exit(2);
}
xfree (name);
return NULL;
}
n = strlen (home) + 1 + strlen (name) + 1;
if (xmode)
home_buffer = xmalloc (n);
else
{
home_buffer = xtrymalloc (n);
if (!home_buffer)
{
xfree (home);
xfree (name);
return NULL;
}
}
if (p == name)
p = home_buffer;
else /* Windows case. */
{
memcpy (home_buffer, p, p - name + 1);
p = home_buffer + (p - name + 1);
}
/* Avoid a leading double slash if the cwd is "/". */
if (home[0] == '/' && !home[1])
strcpy (stpcpy (p, "/"), name);
else
strcpy (stpcpy (stpcpy (p, home), "/"), name);
xfree (home);
xfree (name);
name = home_buffer;
/* Let's do a simple compression to catch the most common
case of using "." for gpg's --homedir option. */
n = strlen (name);
if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
name[n-2] = 0;
}
}
return change_slashes (name);
}
/* Construct a filename from the NULL terminated list of parts. Tilde
expansion is done for the first argument. This function terminates
the process on memory shortage. */
char *
make_filename (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = do_make_filename (1, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Construct a filename from the NULL terminated list of parts. Tilde
expansion is done for the first argument. This function may return
NULL on error. */
char *
make_filename_try (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = do_make_filename (0, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Construct an absolute filename from the NULL terminated list of
parts. Tilde expansion is done for the first argument. This
function terminates the process on memory shortage. */
char *
make_absfilename (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = do_make_filename (3, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Construct an absolute filename from the NULL terminated list of
parts. Tilde expansion is done for the first argument. This
function may return NULL on error. */
char *
make_absfilename_try (const char *first_part, ... )
{
va_list arg_ptr;
char *result;
va_start (arg_ptr, first_part);
result = do_make_filename (2, first_part, arg_ptr);
va_end (arg_ptr);
return result;
}
/* Compare whether the filenames are identical. This is a
special version of strcmp() taking the semantics of filenames in
account. Note that this function works only on the supplied names
without considering any context like the current directory. See
also same_file_p(). */
int
compare_filenames (const char *a, const char *b)
{
#ifdef HAVE_DOSISH_SYSTEM
for ( ; *a && *b; a++, b++ )
{
if (*a != *b
&& (toupper (*(const unsigned char*)a)
!= toupper (*(const unsigned char*)b) )
&& !((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/')))
break;
}
if ((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/'))
return 0;
else
return (toupper (*(const unsigned char*)a)
- toupper (*(const unsigned char*)b));
#else
return strcmp(a,b);
#endif
}
/* Convert a base-10 number in STRING into a 64 bit unsigned int
* value. Leading white spaces are skipped but no error checking is
- * done. Thus it is similar to atoi(). */
+ * done. Thus it is similar to atoi(). See also scan_secondsstr. */
uint64_t
string_to_u64 (const char *string)
{
uint64_t val = 0;
while (spacep (string))
string++;
for (; digitp (string); string++)
{
val *= 10;
val += *string - '0';
}
return val;
}
/* Convert 2 hex characters at S to a byte value. Return this value
or -1 if there is an error. */
int
hextobyte (const char *s)
{
int c;
if ( *s >= '0' && *s <= '9' )
c = 16 * (*s - '0');
else if ( *s >= 'A' && *s <= 'F' )
c = 16 * (10 + *s - 'A');
else if ( *s >= 'a' && *s <= 'f' )
c = 16 * (10 + *s - 'a');
else
return -1;
s++;
if ( *s >= '0' && *s <= '9' )
c += *s - '0';
else if ( *s >= 'A' && *s <= 'F' )
c += 10 + *s - 'A';
else if ( *s >= 'a' && *s <= 'f' )
c += 10 + *s - 'a';
else
return -1;
return c;
}
/* Given a string containing an UTF-8 encoded text, return the number
of characters in this string. It differs from strlen in that it
only counts complete UTF-8 characters. SIZE is the maximum length
of the string in bytes. If SIZE is -1, then a NUL character is
taken to be the end of the string. Note, that this function does
not take combined characters into account. */
size_t
utf8_charcount (const char *s, int len)
{
size_t n;
if (len == 0)
return 0;
for (n=0; *s; s++)
{
if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
n++;
if (len != -1)
{
len --;
if (len == 0)
break;
}
}
return n;
}
/****************************************************
********** W32 specific functions ****************
****************************************************/
#ifdef HAVE_W32_SYSTEM
const char *
w32_strerror (int ec)
{
static char strerr[256];
if (ec == -1)
ec = (int)GetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
strerr, DIM (strerr)-1, NULL);
{
/* Strip the CR,LF - we want just the string. */
size_t n = strlen (strerr);
if (n > 2 && strerr[n-2] == '\r' && strerr[n-1] == '\n' )
strerr[n-2] = 0;
}
return strerr;
}
#endif /*HAVE_W32_SYSTEM*/
/****************************************************
******** Locale insensitive ctype functions ********
****************************************************/
/* FIXME: replace them by a table lookup and macros */
int
ascii_isupper (int c)
{
return c >= 'A' && c <= 'Z';
}
int
ascii_islower (int c)
{
return c >= 'a' && c <= 'z';
}
int
ascii_toupper (int c)
{
if (c >= 'a' && c <= 'z')
c &= ~0x20;
return c;
}
int
ascii_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
c |= 0x20;
return c;
}
/* Lowercase all ASCII characters in S. */
char *
ascii_strlwr (char *s)
{
char *p = s;
for (p=s; *p; p++ )
if (isascii (*p) && *p >= 'A' && *p <= 'Z')
*p |= 0x20;
return s;
}
/* Upcase all ASCII characters in S. */
char *
ascii_strupr (char *s)
{
char *p = s;
for (p=s; *p; p++ )
if (isascii (*p) && *p >= 'a' && *p <= 'z')
*p &= ~0x20;
return s;
}
int
ascii_strcasecmp( const char *a, const char *b )
{
if (a == b)
return 0;
for (; *a && *b; a++, b++) {
if (*a != *b && ascii_toupper(*a) != ascii_toupper(*b))
break;
}
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
}
int
ascii_strncasecmp (const char *a, const char *b, size_t n)
{
const unsigned char *p1 = (const unsigned char *)a;
const unsigned char *p2 = (const unsigned char *)b;
unsigned char c1, c2;
if (p1 == p2 || !n )
return 0;
do
{
c1 = ascii_tolower (*p1);
c2 = ascii_tolower (*p2);
if ( !--n || c1 == '\0')
break;
++p1;
++p2;
}
while (c1 == c2);
return c1 - c2;
}
int
ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n )
{
const char *a = a_arg;
const char *b = b_arg;
if (a == b)
return 0;
for ( ; n; n--, a++, b++ )
{
if( *a != *b && ascii_toupper (*a) != ascii_toupper (*b) )
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
}
return 0;
}
int
ascii_strcmp( const char *a, const char *b )
{
if (a == b)
return 0;
for (; *a && *b; a++, b++) {
if (*a != *b )
break;
}
return *a == *b? 0 : (*(signed char *)a - *(signed char *)b);
}
void *
ascii_memcasemem (const void *haystack, size_t nhaystack,
const void *needle, size_t nneedle)
{
if (!nneedle)
return (void*)haystack; /* finding an empty needle is really easy */
if (nneedle <= nhaystack)
{
const char *a = haystack;
const char *b = a + nhaystack - nneedle;
for (; a <= b; a++)
{
if ( !ascii_memcasecmp (a, needle, nneedle) )
return (void *)a;
}
}
return NULL;
}
/*********************************************
********** missing string functions *********
*********************************************/
#ifndef HAVE_STPCPY
char *
stpcpy(char *a,const char *b)
{
while( *b )
*a++ = *b++;
*a = 0;
return (char*)a;
}
#endif
#ifndef HAVE_STRPBRK
/* Find the first occurrence in S of any character in ACCEPT.
Code taken from glibc-2.6/string/strpbrk.c (LGPLv2.1+) and modified. */
char *
strpbrk (const char *s, const char *accept)
{
while (*s != '\0')
{
const char *a = accept;
while (*a != '\0')
if (*a++ == *s)
return (char *) s;
++s;
}
return NULL;
}
#endif /*!HAVE_STRPBRK*/
#ifndef HAVE_STRSEP
/* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
char *
strsep (char **stringp, const char *delim)
{
char *begin, *end;
begin = *stringp;
if (begin == NULL)
return NULL;
/* A frequent case is when the delimiter string contains only one
character. Here we don't need to call the expensive 'strpbrk'
function and instead work using 'strchr'. */
if (delim[0] == '\0' || delim[1] == '\0')
{
char ch = delim[0];
if (ch == '\0')
end = NULL;
else
{
if (*begin == ch)
end = begin;
else if (*begin == '\0')
end = NULL;
else
end = strchr (begin + 1, ch);
}
}
else
/* Find the end of the token. */
end = strpbrk (begin, delim);
if (end)
{
/* Terminate the token and set *STRINGP past NUL character. */
*end++ = '\0';
*stringp = end;
}
else
/* No more delimiters; this is the last token. */
*stringp = NULL;
return begin;
}
#endif /*HAVE_STRSEP*/
#ifndef HAVE_STRLWR
char *
strlwr(char *s)
{
char *p;
for(p=s; *p; p++ )
*p = tolower(*p);
return s;
}
#endif
#ifndef HAVE_STRCASECMP
int
strcasecmp( const char *a, const char *b )
{
for( ; *a && *b; a++, b++ ) {
if( *a != *b && toupper(*a) != toupper(*b) )
break;
}
return *(const byte*)a - *(const byte*)b;
}
#endif
/****************
* mingw32/cpd has a memicmp()
*/
#ifndef HAVE_MEMICMP
int
memicmp( const char *a, const char *b, size_t n )
{
for( ; n; n--, a++, b++ )
if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
return *(const byte *)a - *(const byte*)b;
return 0;
}
#endif
#ifndef HAVE_MEMRCHR
void *
memrchr (const void *buffer, int c, size_t n)
{
const unsigned char *p = buffer;
for (p += n; n ; n--)
if (*--p == c)
return (void *)p;
return NULL;
}
#endif /*HAVE_MEMRCHR*/
/* Percent-escape the string STR by replacing colons with '%3a'. If
EXTRA is not NULL all characters in EXTRA are also escaped. */
static char *
do_percent_escape (const char *str, const char *extra, int die)
{
int i, j;
char *ptr;
if (!str)
return NULL;
for (i=j=0; str[i]; i++)
if (str[i] == ':' || str[i] == '%' || str[i] == '\n'
|| (extra && strchr (extra, str[i])))
j++;
if (die)
ptr = xmalloc (i + 2 * j + 1);
else
{
ptr = xtrymalloc (i + 2 * j + 1);
if (!ptr)
return NULL;
}
i = 0;
while (*str)
{
if (*str == ':')
{
ptr[i++] = '%';
ptr[i++] = '3';
ptr[i++] = 'a';
}
else if (*str == '%')
{
ptr[i++] = '%';
ptr[i++] = '2';
ptr[i++] = '5';
}
else if (*str == '\n')
{
/* The newline is problematic in a line-based format. */
ptr[i++] = '%';
ptr[i++] = '0';
ptr[i++] = 'a';
}
else if (extra && strchr (extra, *str))
{
ptr[i++] = '%';
ptr[i++] = tohex_lower ((*str>>4)&15);
ptr[i++] = tohex_lower (*str&15);
}
else
ptr[i++] = *str;
str++;
}
ptr[i] = '\0';
return ptr;
}
/* Percent-escape the string STR by replacing colons with '%3a'. If
EXTRA is not NULL all characters in EXTRA are also escaped. This
function terminates the process on memory shortage. */
char *
percent_escape (const char *str, const char *extra)
{
return do_percent_escape (str, extra, 1);
}
/* Same as percent_escape but return NULL instead of exiting on memory
error. */
char *
try_percent_escape (const char *str, const char *extra)
{
return do_percent_escape (str, extra, 0);
}
/* Same as strconcat but takes a va_list. Returns EINVAL if the list
* is too long, all other errors are due to an ENOMEM condition. */
char *
vstrconcat (const char *s1, va_list arg_ptr)
{
const char *argv[48];
size_t argc;
size_t needed;
char *buffer, *p;
argc = 0;
argv[argc++] = s1;
needed = strlen (s1);
while (((argv[argc] = va_arg (arg_ptr, const char *))))
{
needed += strlen (argv[argc]);
if (argc >= DIM (argv)-1)
{
gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
}
needed++;
buffer = xtrymalloc (needed);
if (buffer)
{
for (p = buffer, argc=0; argv[argc]; argc++)
p = stpcpy (p, argv[argc]);
}
return buffer;
}
/* Concatenate the string S1 with all the following strings up to a
NULL. Returns a malloced buffer with the new string or NULL on a
malloc error or if too many arguments are given. */
char *
strconcat (const char *s1, ...)
{
va_list arg_ptr;
char *result;
if (!s1)
result = xtrystrdup ("");
else
{
va_start (arg_ptr, s1);
result = vstrconcat (s1, arg_ptr);
va_end (arg_ptr);
}
return result;
}
/* Same as strconcat but terminate the process with an error message
if something goes wrong. */
char *
xstrconcat (const char *s1, ...)
{
va_list arg_ptr;
char *result;
if (!s1)
result = xstrdup ("");
else
{
va_start (arg_ptr, s1);
result = vstrconcat (s1, arg_ptr);
va_end (arg_ptr);
}
if (!result)
{
if (errno == EINVAL)
fputs ("\nfatal: too many args for xstrconcat\n", stderr);
else
fputs ("\nfatal: out of memory\n", stderr);
exit (2);
}
return result;
}
/* Split a string into fields at DELIM. REPLACEMENT is the character
to replace the delimiter with (normally: '\0' so that each field is
NUL terminated). The caller is responsible for freeing the result.
Note: this function modifies STRING! If you need the original
value, then you should pass a copy to this function.
If malloc fails, this function returns NULL. */
char **
strsplit (char *string, char delim, char replacement, int *count)
{
int fields = 1;
char *t;
char **result;
/* First, count the number of fields. */
for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
fields ++;
result = xtrycalloc ((fields + 1), sizeof (*result));
if (! result)
return NULL;
result[0] = string;
fields = 1;
for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
{
result[fields ++] = t + 1;
*t = replacement;
}
if (count)
*count = fields;
return result;
}
/* Tokenize STRING using the set of delimiters in DELIM. Leading
* spaces and tabs are removed from all tokens. The caller must xfree
* the result.
*
* Returns: A malloced and NULL delimited array with the tokens. On
* memory error NULL is returned and ERRNO is set.
*/
static char **
do_strtokenize (const char *string, const char *delim, int trim)
{
const char *s;
size_t fields;
size_t bytes, n;
char *buffer;
char *p, *px, *pend;
char **result;
/* Count the number of fields. */
for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
fields++;
fields++; /* Add one for the terminating NULL. */
/* Allocate an array for all fields, a terminating NULL, and space
for a copy of the string. */
bytes = fields * sizeof *result;
if (bytes / sizeof *result != fields)
{
gpg_err_set_errno (ENOMEM);
return NULL;
}
n = strlen (string) + 1;
bytes += n;
if (bytes < n)
{
gpg_err_set_errno (ENOMEM);
return NULL;
}
result = xtrymalloc (bytes);
if (!result)
return NULL;
buffer = (char*)(result + fields);
/* Copy and parse the string. */
strcpy (buffer, string);
for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
{
*pend = 0;
if (trim)
{
while (spacep (p))
p++;
for (px = pend - 1; px >= p && spacep (px); px--)
*px = 0;
}
result[n++] = p;
}
if (trim)
{
while (spacep (p))
p++;
for (px = p + strlen (p) - 1; px >= p && spacep (px); px--)
*px = 0;
}
result[n++] = p;
result[n] = NULL;
log_assert ((char*)(result + n + 1) == buffer);
return result;
}
/* Tokenize STRING using the set of delimiters in DELIM. Leading
* spaces and tabs are removed from all tokens. The caller must xfree
* the result.
*
* Returns: A malloced and NULL delimited array with the tokens. On
* memory error NULL is returned and ERRNO is set.
*/
char **
strtokenize (const char *string, const char *delim)
{
return do_strtokenize (string, delim, 1);
}
/* Same as strtokenize but does not trim leading and trailing spaces
* from the fields. */
char **
strtokenize_nt (const char *string, const char *delim)
{
return do_strtokenize (string, delim, 0);
}
/* Split a string into space delimited fields and remove leading and
* trailing spaces from each field. A pointer to each field is stored
* in ARRAY. Stop splitting at ARRAYSIZE fields. The function
* modifies STRING. The number of parsed fields is returned.
* Example:
*
* char *fields[2];
* if (split_fields (string, fields, DIM (fields)) < 2)
* return // Not enough args.
* foo (fields[0]);
* foo (fields[1]);
*/
int
split_fields (char *string, const char **array, int arraysize)
{
int n = 0;
const char *p;
char *pend;
for (p = string; *p == ' '; p++)
;
do
{
if (n == arraysize)
break;
array[n++] = p;
pend = strchr (p, ' ');
if (!pend)
break;
*pend++ = 0;
for (p = pend; *p == ' '; p++)
;
}
while (*p);
return n;
}
/* Split a string into colon delimited fields A pointer to each field
* is stored in ARRAY. Stop splitting at ARRAYSIZE fields. The
* function modifies STRING. The number of parsed fields is returned.
* Note that leading and trailing spaces are not removed from the fields.
* Example:
*
* char *fields[2];
* if (split_fields (string, fields, DIM (fields)) < 2)
* return // Not enough args.
* foo (fields[0]);
* foo (fields[1]);
*/
int
split_fields_colon (char *string, const char **array, int arraysize)
{
int n = 0;
const char *p;
char *pend;
p = string;
do
{
if (n == arraysize)
break;
array[n++] = p;
pend = strchr (p, ':');
if (!pend)
break;
*pend++ = 0;
p = pend;
}
while (*p);
return n;
}
/* Version number parsing. */
/* This function parses the first portion of the version number S and
stores it in *NUMBER. On success, this function returns a pointer
into S starting with the first character, which is not part of the
initial number portion; on failure, NULL is returned. */
static const char*
parse_version_number (const char *s, int *number)
{
int val = 0;
if (*s == '0' && digitp (s+1))
return NULL; /* Leading zeros are not allowed. */
for (; digitp (s); s++)
{
val *= 10;
val += *s - '0';
}
*number = val;
return val < 0 ? NULL : s;
}
/* This function breaks up the complete string-representation of the
version number S, which is of the following structure: <major
number>.<minor number>[.<micro number>]<patch level>. The major,
minor, and micro number components will be stored in *MAJOR, *MINOR
and *MICRO. If MICRO is not given 0 is used instead.
On success, the last component, the patch level, will be returned;
in failure, NULL will be returned. */
static const char *
parse_version_string (const char *s, int *major, int *minor, int *micro)
{
s = parse_version_number (s, major);
if (!s || *s != '.')
return NULL;
s++;
s = parse_version_number (s, minor);
if (!s)
return NULL;
if (*s == '.')
{
s++;
s = parse_version_number (s, micro);
if (!s)
return NULL;
}
else
*micro = 0;
return s; /* Patchlevel. */
}
/* Compare the version string MY_VERSION to the version string
* REQ_VERSION. Returns -1, 0, or 1 if MY_VERSION is found,
* respectively, to be less than, to match, or be greater than
* REQ_VERSION. This function works for three and two part version
* strings; for a two part version string the micro part is assumed to
* be 0. Patch levels are compared as strings. If a version number
* is invalid INT_MIN is returned. If REQ_VERSION is given as NULL
* the function returns 0 if MY_VERSION is parsable version string. */
int
compare_version_strings (const char *my_version, const char *req_version)
{
int my_major, my_minor, my_micro;
int rq_major, rq_minor, rq_micro;
const char *my_patch, *rq_patch;
int result;
if (!my_version)
return INT_MIN;
my_patch = parse_version_string (my_version, &my_major, &my_minor, &my_micro);
if (!my_patch)
return INT_MIN;
if (!req_version)
return 0; /* MY_VERSION can be parsed. */
rq_patch = parse_version_string (req_version, &rq_major, &rq_minor,&rq_micro);
if (!rq_patch)
return INT_MIN;
if (my_major == rq_major)
{
if (my_minor == rq_minor)
{
if (my_micro == rq_micro)
result = strcmp (my_patch, rq_patch);
else
result = my_micro - rq_micro;
}
else
result = my_minor - rq_minor;
}
else
result = my_major - rq_major;
return !result? 0 : result < 0 ? -1 : 1;
}
/* Format a string so that it fits within about TARGET_COLS columns.
* TEXT_IN is copied to a new buffer, which is returned. Normally,
* target_cols will be 72 and max_cols is 80. On error NULL is
* returned and ERRNO is set. */
char *
format_text (const char *text_in, int target_cols, int max_cols)
{
/* const int do_debug = 0; */
/* The character under consideration. */
char *p;
/* The start of the current line. */
char *line;
/* The last space that we saw. */
char *last_space = NULL;
int last_space_cols = 0;
int copied_last_space = 0;
char *text;
text = xtrystrdup (text_in);
if (!text)
return NULL;
p = line = text;
while (1)
{
/* The number of columns including any trailing space. */
int cols;
p = p + strcspn (p, "\n ");
if (! p)
/* P now points to the NUL character. */
p = &text[strlen (text)];
if (*p == '\n')
/* Pass through any newlines. */
{
p ++;
line = p;
last_space = NULL;
last_space_cols = 0;
copied_last_space = 1;
continue;
}
/* Have a space or a NUL. Note: we don't count the trailing
space. */
cols = utf8_charcount (line, (uintptr_t) p - (uintptr_t) line);
if (cols < target_cols)
{
if (! *p)
/* Nothing left to break. */
break;
last_space = p;
last_space_cols = cols;
p ++;
/* Skip any immediately following spaces. If we break:
"... foo bar ..." between "foo" and "bar" then we want:
"... foo\nbar ...", which means that the left space has
to be the first space after foo, not the last space
before bar. */
while (*p == ' ')
p ++;
}
else
{
int cols_with_left_space;
int cols_with_right_space;
int left_penalty;
int right_penalty;
cols_with_left_space = last_space_cols;
cols_with_right_space = cols;
/* if (do_debug) */
/* log_debug ("Breaking: '%.*s'\n", */
/* (int) ((uintptr_t) p - (uintptr_t) line), line); */
/* The number of columns away from TARGET_COLS. We prefer
to underflow than to overflow. */
left_penalty = target_cols - cols_with_left_space;
right_penalty = 2 * (cols_with_right_space - target_cols);
if (cols_with_right_space > max_cols)
/* Add a large penalty for each column that exceeds
max_cols. */
right_penalty += 4 * (cols_with_right_space - max_cols);
/* if (do_debug) */
/* log_debug ("Left space => %d cols (penalty: %d); " */
/* "right space => %d cols (penalty: %d)\n", */
/* cols_with_left_space, left_penalty, */
/* cols_with_right_space, right_penalty); */
if (last_space_cols && left_penalty <= right_penalty)
{
/* Prefer the left space. */
/* if (do_debug) */
/* log_debug ("Breaking at left space.\n"); */
p = last_space;
}
else
{
/* if (do_debug) */
/* log_debug ("Breaking at right space.\n"); */
}
if (! *p)
break;
*p = '\n';
p ++;
if (*p == ' ')
{
int spaces;
for (spaces = 1; p[spaces] == ' '; spaces ++)
;
memmove (p, &p[spaces], strlen (&p[spaces]) + 1);
}
line = p;
last_space = NULL;
last_space_cols = 0;
copied_last_space = 0;
}
}
/* Chop off any trailing space. */
trim_trailing_chars (text, strlen (text), " ");
/* If we inserted the trailing newline, then remove it. */
if (! copied_last_space && *text && text[strlen (text) - 1] == '\n')
text[strlen (text) - 1] = '\0';
return text;
}
/* Substitute variables in STRING and return a new string. GETVAL is
* a function which maps NAME to its value; that value is a string
* which may not change during the execution time of this function.
* If GETVAL returns NULL substitute_vars returns NULL and the caller
* may inspect ERRNO for the reason. In all other error cases this
* function also returns NULL. Caller must free the returned string. */
char *
substitute_vars (const char *string,
const char *(*getval)(void *cookie, const char *name),
void *cookie)
{
char *line, *p, *pend;
const char *value;
size_t valuelen, n;
char *result = NULL;
result = line = xtrystrdup (string);
if (!result)
return NULL; /* Ooops */
while (*line)
{
p = strchr (line, '$');
if (!p)
goto leave; /* No or no more variables. */
if (p[1] == '$') /* Escaped dollar sign. */
{
memmove (p, p+1, strlen (p+1)+1);
line = p + 1;
continue;
}
if (p[1] == '{')
{
int count = 0;
for (pend=p+2; *pend; pend++)
{
if (*pend == '{')
count++;
else if (*pend == '}')
{
if (--count < 0)
break;
}
}
if (!*pend)
goto leave; /* Unclosed - don't substitute. */
}
else
{
for (pend = p+1; *pend && (alnump (pend) || *pend == '_'); pend++)
;
}
if (p[1] == '{' && *pend == '}')
{
int save = *pend;
*pend = 0;
value = getval (cookie, p+2);
*pend++ = save;
}
else
{
int save = *pend;
*pend = 0;
value = getval (cookie, p+1);
*pend = save;
}
if (!value)
{
xfree (result);
return NULL;
}
valuelen = strlen (value);
if (valuelen <= pend - p)
{
memcpy (p, value, valuelen);
p += valuelen;
n = pend - p;
if (n)
memmove (p, p+n, strlen (p+n)+1);
line = p;
}
else
{
char *src = result;
char *dst;
dst = xtrymalloc (strlen (src) + valuelen + 1);
if (!dst)
{
xfree (result);
return NULL;
}
n = p - src;
memcpy (dst, src, n);
memcpy (dst + n, value, valuelen);
n += valuelen;
strcpy (dst + n, pend);
line = dst + n;
xfree (result);
result = dst;
}
}
leave:
return result;
}
/* Helper for substitute_envvars. */
static const char *
subst_getenv (void *cookie, const char *name)
{
const char *s;
(void)cookie;
s = getenv (name);
return s? s : "";
}
/* Substitute environment variables in STRING and return a new string.
* On error the function returns NULL. */
char *
substitute_envvars (const char *string)
{
return substitute_vars (string, subst_getenv, NULL);
}
diff --git a/common/t-gettime.c b/common/t-gettime.c
index 13cb1a2f7..76c305204 100644
--- a/common/t-gettime.c
+++ b/common/t-gettime.c
@@ -1,277 +1,327 @@
/* t-gettime.c - Module test for gettime.c
* Copyright (C) 2007, 2011 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#include "util.h"
/* In case we do not have stdint.h and no other version of that
* conversion macro provide shortcut it. */
#ifndef UINTMAX_C
#define UINTMAX_C (c) (c)
#endif
#define pass() do { ; } while(0)
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
__FILE__,__LINE__, (a)); \
errcount++; \
} while(0)
static int verbose;
static int errcount;
#define INVALID ((time_t)(-1))
+static void
+test_scan_secondsstr (void)
+{
+ struct { const char *string; u32 expected; } array [] = {
+ { "", 0 },
+ { "0", 0 },
+ { " 0", 0 },
+ { " 0x", 0 },
+ { " 1", 1 },
+ { "-1", 0 },
+ { " -1", 0 },
+ { "2", 2 },
+ { "11", 11 },
+ { "011", 11 },
+ { "3600 ", 3600 },
+ { "65535", 65535 },
+ { "65536", 65536 },
+ { "65537", 65537 },
+ { "4294967289", 4294967289 },
+ { "4294967290", 4294967290 },
+ { "4294967293", 4294967293 },
+ { "4294967295", 4294967294 },
+ { "4294967296", 4294967294 },
+ { "4294967297", 4294967294 },
+ { "4294967298", 4294967294 },
+ { "4294967299", 4294967294 },
+ { "4294967300", 4294967294 },
+ { "5294967300", 4294967294 },
+ { "9999999999", 4294967294 },
+ { "99999999999",4294967294 },
+ { NULL, 0 }
+ };
+ int idx;
+ u32 val;
+
+ for (idx=0; array[idx].string; idx++)
+ {
+ val = scan_secondsstr (array[idx].string);
+ if (val != array[idx].expected )
+ {
+ fail (idx);
+ if (verbose)
+ fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
+ array[idx].string, (unsigned long)array[idx].expected,
+ (unsigned long)val);
+ }
+ }
+}
+
+
static void
test_isotime2epoch (void)
{
struct { const char *string; time_t expected; } array [] = {
{ "19700101T000001", 1 },
{ "19700101T235959", 86399 },
{ "19980815T143712", 903191832 },
{ "19700101T000000", 0 },
{ "19691231T235959", INVALID },
{ "19000101T000000", INVALID },
{ "", INVALID },
{ "19000101T00000", INVALID },
{ "20010101t123456", INVALID },
{ "20010101T123456", 978352496 },
{ "20070629T160000", 1183132800 },
{ "20070629T160000:", 1183132800 },
{ "20070629T160000,", 1183132800 },
{ "20070629T160000 ", 1183132800 },
{ "20070629T160000\n", 1183132800 },
{ "20070629T160000.", INVALID },
#if SIZEOF_TIME_T > 4
{ "21060207T062815", (time_t)UINTMAX_C(0x0ffffffff) },
{ "21060207T062816", (time_t)UINTMAX_C(0x100000000) },
{ "21060207T062817", (time_t)UINTMAX_C(0x100000001) },
{ "21060711T120001", (time_t)UINTMAX_C(4308292801) },
#endif /*SIZEOF_TIME_T > 4*/
{ NULL, 0 }
};
int idx;
time_t val;
gnupg_isotime_t tbuf;
for (idx=0; array[idx].string; idx++)
{
val = isotime2epoch (array[idx].string);
if (val != array[idx].expected )
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' exp: %ld got: %ld\n",
array[idx].string, (long)array[idx].expected,
(long)val);
}
if (array[idx].expected != INVALID)
{
epoch2isotime (tbuf, val);
if (strlen (tbuf) != 15)
{
if (verbose)
fprintf (stderr, "string '%s', time-t %ld, revert: '%s'\n",
array[idx].string, (long)val, tbuf);
fail (idx);
}
if (strncmp (array[idx].string, tbuf, 15))
fail (idx);
}
}
}
-
static void
test_string2isotime (void)
{
struct {
const char *string;
size_t result;
const char *expected;
} array [] = {
{ "19700101T000001", 15, "19700101T000001" },
{ "19700101T235959", 15, "19700101T235959" },
{ "19980815T143712", 15, "19980815T143712" },
{ "19700101T000000", 15, "19700101T000000" },
{ "19691231T235959", 15, "19691231T235959" },
{ "19000101T000000", 15, "19000101T000000" },
{ "", 0, "" },
{ "19000101T00000", 0, "" },
{ "20010101t123456", 0, "" },
{ "20010101T123456", 15, "20010101T123456" },
{ "20070629T160000", 15, "20070629T160000" },
{ "20070629T160000:", 15, "20070629T160000" },
{ "20070629T160000,", 15, "20070629T160000" },
{ "20070629T160000 ", 15, "20070629T160000" },
{ "20070629T160000\n", 15,"20070629T160000" },
{ "20070629T160000.", 0, "" },
{ "1066-03-20", 10, "10660320T000000" },
{ "1066-03-20,", 10, "10660320T000000" },
{ "1066-03-20:", 0, "" },
{ "1066-03-20 00", 13, "10660320T000000" },
{ "1066-03-20 01", 13, "10660320T010000" },
{ "1066-03-20 23", 13, "10660320T230000" },
{ "1066-03-20 24", 0, "" },
{ "1066-03-20 00:", 0, "" },
{ "1066-03-20 00:3", 0, "" },
{ "1066-03-20 00:31", 16, "10660320T003100" },
{ "1066-03-20 00:31:47", 19, "10660320T003147" },
{ "1066-03-20 00:31:47 ", 19, "10660320T003147" },
{ "1066-03-20 00:31:47,", 19, "10660320T003147" },
{ "1066-03-20 00:31:47:", 0, "" },
{ "1-03-20 00:31:47:", 0, "" },
{ "10-03-20 00:31:47:", 0, "" },
{ "106-03-20 00:31:47:", 0, "" },
{ "1066-23-20 00:31:47:", 0, "" },
{ "1066-00-20 00:31:47:", 0, "" },
{ "1066-0-20 00:31:47:", 0, "" },
{ "1066-01-2 00:31:47:", 0, "" },
{ "1066-01-2 00:31:47:", 0, "" },
{ "1066-01-32 00:31:47:", 0, "" },
{ "1066-01-00 00:31:47:", 0, "" },
{ "1066-03-20 00:31:47:",11, "10660320T000000" },
{ "1066-03-2000:31:47:", 0, "" },
{ "10666-03-20 00:31:47:", 0, "" },
{ NULL, 0 }
};
int idx;
size_t result;
gnupg_isotime_t tbuf;
for (idx=0; array[idx].string; idx++)
{
result = string2isotime (tbuf, array[idx].string);
if (result != array[idx].result)
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' expected: %d, got: %d\n",
array[idx].string, (int)array[idx].result, (int)result);
}
else if (result && strlen (tbuf) != 15)
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' invalid isotime returned\n",
array[idx].string);
}
else if (result && strcmp (array[idx].expected, tbuf))
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' bad isotime '%s' returned\n",
array[idx].string, tbuf);
}
}
}
static void
test_isodate_human_to_tm (void)
{
struct {
const char *string;
int okay;
int year, mon, mday;
} array [] = {
{ "1970-01-01", 1, 1970, 1, 1 },
{ "1970-02-01", 1, 1970, 2, 1 },
{ "1970-12-31", 1, 1970, 12, 31 },
{ "1971-01-01", 1, 1971, 1, 1 },
{ "1998-08-15", 1, 1998, 8, 15 },
{ "2015-04-10", 1, 2015, 4, 10 },
{ "2015-04-10 11:30",1, 2015, 4, 10 },
{ "1969-12-31", 0, 0, 0, 0 },
{ "1900-01-01", 0, 0, 0, 0 },
{ "", 0, 0, 0, 0 },
{ "1970-12-32", 0, 0, 0, 0 },
{ "1970-13-01", 0, 0, 0, 0 },
{ "1970-01-00", 0, 0, 0, 0 },
{ "1970-00-01", 0, 0, 0, 0 },
{ "1970-00-01", 0, 0, 0, 0 },
{ "1970", 0, 0, 0, 0 },
{ "1970-01", 0, 0, 0, 0 },
{ "1970-01-1", 0, 0, 0, 0 },
{ "1970-1--01", 0, 0, 0, 0 },
{ "1970-01-01,", 1, 1970, 1, 1 },
{ "1970-01-01 ", 1, 1970, 1, 1 },
{ "1970-01-01\t", 1, 1970, 1, 1 },
{ "1970-01-01;", 0, 0, 0, 0 },
{ "1970-01-01:", 0, 0, 0, 0 },
{ "1970_01-01", 0, 0, 0, 0 },
{ "1970-01_01", 0, 0, 0, 0 },
{ NULL, 0 }
};
int idx;
int okay;
struct tm tmbuf;
for (idx=0; array[idx].string; idx++)
{
okay = !isodate_human_to_tm (array[idx].string, &tmbuf);
if (okay != array[idx].okay)
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' expected: %d, got: %d\n",
array[idx].string, (int)array[idx].okay, okay);
}
else if (!okay)
;
else if (tmbuf.tm_year + 1900 != array[idx].year
|| tmbuf.tm_mon +1 != array[idx].mon
|| tmbuf.tm_mday != array[idx].mday)
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' returned %04d-%02d-%02d\n",
array[idx].string,
tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday);
}
else if (tmbuf.tm_sec || tmbuf.tm_min || tmbuf.tm_hour
|| tmbuf.tm_isdst != -1)
{
fail (idx);
if (verbose)
fprintf (stderr, "string '%s' returned bad time part\n",
array[idx].string);
}
}
}
int
main (int argc, char **argv)
{
if (argc > 1 && !strcmp (argv[1], "--verbose"))
verbose = 1;
+ test_scan_secondsstr ();
test_isotime2epoch ();
test_string2isotime ();
test_isodate_human_to_tm ();
return !!errcount;
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Dec 23, 3:49 PM (1 d, 6 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
71/b3/2e5960e6d40aff5cc8e2808009b2
Attached To
rG GnuPG
Event Timeline
Log In to Comment