Avoid duplicate ends_with_dirsep functions

Refactor them into inline ossl_ends_with_dirsep function in
internal/cryptlib.h.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13306)
This commit is contained in:
Tomas Mraz 2020-11-03 18:15:46 +01:00
parent 122e81f070
commit 69d16b70cf
4 changed files with 55 additions and 30 deletions

View File

@ -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<path> ends with a directory
separator in platform agnostic way.
=head1 RETURN VALUES
ossl_ends_with_dirsep() returns 1 if the I<path> 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<https://www.openssl.org/source/license.html>.
=cut

View File

@ -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 */;

View File

@ -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

View File

@ -24,6 +24,7 @@
#include <openssl/params.h>
#include <openssl/decoder.h>
#include <openssl/store.h> /* 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 */;