diff options
author | Werner Koch <[email protected]> | 2023-10-14 15:06:51 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2023-10-14 15:14:22 +0000 |
commit | a17363e992943244987dbab754b112c77d938b5d (patch) | |
tree | 51b2f749ed5c1dc67e937112cc3fef1728e9c024 /common/gettime.c | |
parent | gpgsm: Improvements for NDEF in the pkcs#12 parser (diff) | |
download | gnupg-a17363e992943244987dbab754b112c77d938b5d.tar.gz gnupg-a17363e992943244987dbab754b112c77d938b5d.zip |
common: New function scan_secondsstr.
* common/gettime.c (scan_secondsstr): New.
* common/t-gettime.c (test_scan_secondsstr):
(main): Call it.
Diffstat (limited to 'common/gettime.c')
-rw-r--r-- | common/gettime.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/common/gettime.c b/common/gettime.c index 2a9b71779..c3b0c6c6c 100644 --- a/common/gettime.c +++ b/common/gettime.c @@ -37,6 +37,7 @@ #ifdef HAVE_LANGINFO_H #include <langinfo.h> #endif +#include <stdint.h> /* We use uint64_t. */ #include "util.h" #include "i18n.h" @@ -172,6 +173,28 @@ make_timestamp (void) } +/* 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. |