From: Tomas Mraz Date: Tue, 3 Nov 2020 17:15:46 +0000 (+0100) Subject: Avoid duplicate ends_with_dirsep functions X-Git-Tag: openssl-3.0.0-alpha9~119 X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=commitdiff_plain;h=69d16b70cf84f0e290990de424274fde20420b78;hp=122e81f0705e74a2019c482e5122bbd9195ea1af Avoid duplicate ends_with_dirsep functions Refactor them into inline ossl_ends_with_dirsep function in internal/cryptlib.h. Reviewed-by: Matt Caswell (Merged from https://github.com/openssl/openssl/pull/13306) --- diff --git a/doc/internal/man3/ossl_ends_with_dirsep.pod b/doc/internal/man3/ossl_ends_with_dirsep.pod new file mode 100644 index 0000000000..1924ab50f0 --- /dev/null +++ b/doc/internal/man3/ossl_ends_with_dirsep.pod @@ -0,0 +1,38 @@ +=pod + +=head1 NAME + +ossl_ends_with_dirsep - internal function to detect whether a path +ends with directory separator + +=head1 SYNOPSIS + + #include "internal/cryptlib.h" + + int ossl_ends_with_dirsep(const char *path); + +=head1 DESCRIPTION + +ossl_ends_with_dirsep() detects whether the I ends with a directory +separator in platform agnostic way. + +=head1 RETURN VALUES + +ossl_ends_with_dirsep() returns 1 if the I ends with a directory +separator, 0 otherwise. + +=head1 HISTORY + +The function described here was added in OpenSSL 3.0. + +=head1 COPYRIGHT + +Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. + +Licensed under the Apache License 2.0 (the "License"). You may not use +this file except in compliance with the License. You can obtain a copy +in the file LICENSE in the source distribution or at +L. + +=cut + diff --git a/engines/e_loader_attic.c b/engines/e_loader_attic.c index 4f238b9cb2..176c159c8c 100644 --- a/engines/e_loader_attic.c +++ b/engines/e_loader_attic.c @@ -1424,27 +1424,13 @@ static int file_read_asn1(BIO *bp, unsigned char **data, long *len) return 1; } -static int ends_with_dirsep(const char *uri) -{ - if (*uri != '\0') - uri += strlen(uri) - 1; -#if defined(__VMS) - if (*uri == ']' || *uri == '>' || *uri == ':') - return 1; -#elif defined(_WIN32) - if (*uri == '\\') - return 1; -#endif - return *uri == '/'; -} - static int file_name_to_uri(OSSL_STORE_LOADER_CTX *ctx, const char *name, char **data) { assert(name != NULL); assert(data != NULL); { - const char *pathsep = ends_with_dirsep(ctx->uri) ? "" : "/"; + const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/"; long calculated_length = strlen(ctx->uri) + strlen(pathsep) + strlen(name) + 1 /* \0 */; diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h index e070618547..f1c6ddfd30 100644 --- a/include/internal/cryptlib.h +++ b/include/internal/cryptlib.h @@ -253,4 +253,18 @@ char *openssl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep); unsigned char *openssl_hexstr2buf_sep(const char *str, long *buflen, const char sep); +static ossl_inline int ossl_ends_with_dirsep(const char *path) +{ + if (*path != '\0') + path += strlen(path) - 1; +# if defined __VMS + if (*path == ']' || *path == '>' || *path == ':') + return 1; +# elif defined _WIN32 + if (*path == '\\') + return 1; +# endif + return *path == '/'; +} + #endif diff --git a/providers/implementations/storemgmt/file_store.c b/providers/implementations/storemgmt/file_store.c index 3b6c50c9e5..5607f169cc 100644 --- a/providers/implementations/storemgmt/file_store.c +++ b/providers/implementations/storemgmt/file_store.c @@ -24,6 +24,7 @@ #include #include #include /* The OSSL_STORE_INFO type numbers */ +#include "internal/cryptlib.h" #include "internal/o_dir.h" #include "crypto/pem.h" /* For PVK and "blob" PEM headers */ #include "crypto/decoder.h" @@ -647,27 +648,13 @@ static int file_load_file(struct file_ctx_st *ctx, * -------------------------------------- */ -static int ends_with_dirsep(const char *uri) -{ - if (*uri != '\0') - uri += strlen(uri) - 1; -#if defined(__VMS) - if (*uri == ']' || *uri == '>' || *uri == ':') - return 1; -#elif defined(_WIN32) - if (*uri == '\\') - return 1; -#endif - return *uri == '/'; -} - static char *file_name_to_uri(struct file_ctx_st *ctx, const char *name) { char *data = NULL; assert(name != NULL); { - const char *pathsep = ends_with_dirsep(ctx->uri) ? "" : "/"; + const char *pathsep = ossl_ends_with_dirsep(ctx->uri) ? "" : "/"; long calculated_length = strlen(ctx->uri) + strlen(pathsep) + strlen(name) + 1 /* \0 */;