From b3058a5e3e538365a5ba5b1755e337786ac58f3e Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Fri, 6 Nov 2020 09:41:49 -0800 Subject: [PATCH] shared: minimal.h: STRTOINT --- src/dll/fuse/fuse_opt.c | 64 ++++------------------------------------- src/fsptool/fsptool.c | 38 +++--------------------- src/shared/ku/posix.c | 56 +----------------------------------- src/shared/um/minimal.h | 56 ++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 148 deletions(-) diff --git a/src/dll/fuse/fuse_opt.c b/src/dll/fuse/fuse_opt.c index 6cbec664..f36ccb5d 100644 --- a/src/dll/fuse/fuse_opt.c +++ b/src/dll/fuse/fuse_opt.c @@ -30,60 +30,6 @@ #define fsp_fuse_opt_match_exact ((const char *)1) /* exact option match */ #define fsp_fuse_opt_match_next ((const char *)2) /* option match, value is next arg */ -static long long strtoint(const char *p, int base, int is_signed) -{ - long long v; - int maxdig, maxalp, sign = +1; - - if (is_signed) - { - if ('+' == *p) - p++; - else if ('-' == *p) - p++, sign = -1; - } - - if (0 == base) - { - if ('0' == *p) - { - p++; - if ('x' == *p || 'X' == *p) - { - p++; - base = 16; - } - else - base = 8; - } - else - { - base = 10; - } - } - - maxdig = 10 < base ? '9' : (base - 1) + '0'; - maxalp = 10 < base ? (base - 1 - 10) + 'a' : 0; - - for (v = 0; *p; p++) - { - int c = *p; - - if ('0' <= c && c <= maxdig) - v = base * v + (c - '0'); - else - { - c |= 0x20; - if ('a' <= c && c <= maxalp) - v = base * v + (c - 'a') + 10; - else - break; - } - } - - return sign * v; -} - static void fsp_fuse_opt_match_templ( const char *templ, const char **pspec, const char **parg) @@ -281,19 +227,19 @@ static int fsp_fuse_opt_process_arg(struct fsp_fuse_env *env, z++; break; case 'd': - llv = strtoint(argl, 10, 1); + llv = strtollint(argl, 0, 10, 1); goto ivar; case 'i': - llv = strtoint(argl, 0, 1); + llv = strtollint(argl, 0, 0, 1); goto ivar; case 'o': - llv = strtoint(argl, 8, 0); + llv = strtollint(argl, 0, 8, 0); goto ivar; case 'u': - llv = strtoint(argl, 10, 0); + llv = strtollint(argl, 0, 10, 0); goto ivar; case 'x': case 'X': - llv = strtoint(argl, 16, 0); + llv = strtollint(argl, 0, 16, 0); ivar: if (z) VAR(data, opt, size_t) = (size_t)llv; diff --git a/src/fsptool/fsptool.c b/src/fsptool/fsptool.c index 4cb989ee..dc96569c 100644 --- a/src/fsptool/fsptool.c +++ b/src/fsptool/fsptool.c @@ -55,36 +55,6 @@ static void printlog(HANDLE h, const char *format, ...) va_end(ap); } -static unsigned wcstoint(const wchar_t *p, const wchar_t **endp, int base) -{ - unsigned v; - int maxdig, maxalp; - - maxdig = 10 < base ? '9' : (base - 1) + '0'; - maxalp = 10 < base ? (base - 1 - 10) + 'a' : 0; - - for (v = 0; *p; p++) - { - int c = *p; - - if ('0' <= c && c <= maxdig) - v = base * v + (c - '0'); - else - { - c |= 0x20; - if ('a' <= c && c <= maxalp) - v = base * v + (c - 'a') + 10; - else - break; - } - } - - if (0 != endp) - *endp = (wchar_t *)p; - - return v; -} - static void usage(void) { fatal(ERROR_INVALID_PARAMETER, @@ -380,7 +350,7 @@ static NTSTATUS id_uid(PWSTR UidStr) UINT32 Uid; NTSTATUS Result; - Uid = wcstoint(UidStr, &UidStr, 10); + Uid = wcstouint(UidStr, &UidStr, 10, 0); if (L'\0' != *UidStr) return STATUS_INVALID_PARAMETER; @@ -525,13 +495,13 @@ static NTSTATUS perm_mode(PWSTR PermStr) UINT32 Uid, Gid, Mode; NTSTATUS Result; - Uid = wcstoint(PermStr, &PermStr, 10); + Uid = wcstouint(PermStr, &PermStr, 10, 0); if (L':' != *PermStr) return STATUS_INVALID_PARAMETER; - Gid = wcstoint(PermStr + 1, &PermStr, 10); + Gid = wcstouint(PermStr + 1, &PermStr, 10, 0); if (L':' != *PermStr) return STATUS_INVALID_PARAMETER; - Mode = wcstoint(PermStr + 1, &PermStr, 8); + Mode = wcstouint(PermStr + 1, &PermStr, 8, 0); if (L'\0' != *PermStr) return STATUS_INVALID_PARAMETER; diff --git a/src/shared/ku/posix.c b/src/shared/ku/posix.c index 03e5f784..f53d8fd5 100644 --- a/src/shared/ku/posix.c +++ b/src/shared/ku/posix.c @@ -121,60 +121,6 @@ static struct ULONG FspTrustedDomainCount; static INIT_ONCE FspPosixInitOnce = INIT_ONCE_STATIC_INIT; #if !defined(_KERNEL_MODE) -static long long wcstoint(const wchar_t* p, int base, int is_signed) -{ - long long v; - int maxdig, maxalp, sign = +1; - - if (is_signed) - { - if ('+' == *p) - p++; - else if ('-' == *p) - p++, sign = -1; - } - - if (0 == base) - { - if ('0' == *p) - { - p++; - if ('x' == *p || 'X' == *p) - { - p++; - base = 16; - } - else - base = 8; - } - else - { - base = 10; - } - } - - maxdig = 10 < base ? '9' : (base - 1) + '0'; - maxalp = 10 < base ? (base - 1 - 10) + 'a' : 0; - - for (v = 0; *p; p++) - { - int c = *p; - - if ('0' <= c && c <= maxdig) - v = base * v + (c - '0'); - else - { - c |= 0x20; - if ('a' <= c && c <= maxalp) - v = base * v + (c - 'a') + 10; - else - break; - } - } - - return sign * v; -} - static ULONG FspPosixInitializeTrustPosixOffsets(VOID) { PVOID Ldap = 0; @@ -197,7 +143,7 @@ static ULONG FspPosixInitializeTrustPosixOffsets(VOID) LdapResult = FspLdapGetTrustPosixOffset(Ldap, DefaultNamingContext, FspTrustedDomains[I].DnsDomainName, &TrustPosixOffsetString); if (0 == LdapResult) - FspTrustedDomains[I].TrustPosixOffset = (ULONG)wcstoint(TrustPosixOffsetString, 10, 1); + FspTrustedDomains[I].TrustPosixOffset = wcstouint(TrustPosixOffsetString, 0, 10, 1); } LdapResult = 0; diff --git a/src/shared/um/minimal.h b/src/shared/um/minimal.h index 94930e9e..5f89f25f 100644 --- a/src/shared/um/minimal.h +++ b/src/shared/um/minimal.h @@ -130,6 +130,62 @@ WINFSP_SHARED_MINIMAL_STRNCMP(invariant_wcsnicmp, wchar_t, invariant_toupper) #undef WINFSP_SHARED_MINIMAL_STRCMP #undef WINFSP_SHARED_MINIMAL_STRNCMP +#define WINFSP_SHARED_MINIMAL_STRTOINT(NAME, CTYPE, ITYPE)\ + static inline\ + ITYPE NAME(const CTYPE *p, const CTYPE **endp, int base, int is_signed)\ + {\ + ITYPE v;\ + int maxdig, maxalp, sign = +1;\ + if (is_signed)\ + {\ + if ('+' == *p)\ + p++;\ + else if ('-' == *p)\ + p++, sign = -1;\ + }\ + if (0 == base)\ + {\ + if ('0' == *p)\ + {\ + p++;\ + if ('x' == *p || 'X' == *p)\ + {\ + p++;\ + base = 16;\ + }\ + else\ + base = 8;\ + }\ + else\ + {\ + base = 10;\ + }\ + }\ + maxdig = 10 < base ? '9' : (base - 1) + '0';\ + maxalp = 10 < base ? (base - 1 - 10) + 'a' : 0;\ + for (v = 0; *p; p++)\ + {\ + int c = *p;\ + if ('0' <= c && c <= maxdig)\ + v = base * v + (c - '0');\ + else\ + {\ + c |= 0x20;\ + if ('a' <= c && c <= maxalp)\ + v = base * v + (c - 'a') + 10;\ + else\ + break;\ + }\ + }\ + if (0 != endp)\ + *endp = (CTYPE *)p;\ + return sign * v;\ + } +WINFSP_SHARED_MINIMAL_STRTOINT(strtouint, char, unsigned int) +WINFSP_SHARED_MINIMAL_STRTOINT(strtollint, char, long long int) +WINFSP_SHARED_MINIMAL_STRTOINT(wcstouint, wchar_t, unsigned int) +WINFSP_SHARED_MINIMAL_STRTOINT(wcstollint, wchar_t, long long int) + static inline void *MemAlloc(size_t Size) { return HeapAlloc(GetProcessHeap(), 0, Size);