Add ossl_is_absolute_path function to detect absolute paths
authorTomas Mraz <tmraz@fedoraproject.org>
Tue, 3 Nov 2020 17:34:16 +0000 (18:34 +0100)
committerTomas Mraz <tmraz@fedoraproject.org>
Wed, 11 Nov 2020 15:06:30 +0000 (16:06 +0100)
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13306)

doc/internal/man3/ossl_ends_with_dirsep.pod
include/internal/cryptlib.h

index 1924ab50f06455934a875ef2edd00a0141b5463b..d19ce7a3b97c4da3ce55ce9d453040cc8e972f54 100644 (file)
@@ -2,8 +2,8 @@
 
 =head1 NAME
 
-ossl_ends_with_dirsep - internal function to detect whether a path
-ends with directory separator
+ossl_ends_with_dirsep, ossl_is_absolute_path
+- internal functions to work with paths
 
 =head1 SYNOPSIS
 
@@ -11,19 +11,26 @@ ends with directory separator
 
   int ossl_ends_with_dirsep(const char *path);
 
+  int ossl_is_absolute_path(const char *path);
+
 =head1 DESCRIPTION
 
 ossl_ends_with_dirsep() detects whether the I<path> ends with a directory
-separator in platform agnostic way.
+separator in a platform agnostic way.
+
+ossl_is_absolute_path() detects whether the I<path> is absolute path in
+a platform agnostic way.
 
 =head1 RETURN VALUES
 
 ossl_ends_with_dirsep() returns 1 if the I<path> ends with a directory
 separator, 0 otherwise.
 
+ossl_is_absolute_path() returns 1 if the I<path> is absolute, 0 otherwise.
+
 =head1 HISTORY
 
-The function described here was added in OpenSSL 3.0.
+The functions described here were added in OpenSSL 3.0.
 
 =head1 COPYRIGHT
 
index f1c6ddfd301494cbcd8ec791cb574be9e1836d0e..eae10dfb6cb389cbc78881254239962d8476f912 100644 (file)
@@ -267,4 +267,20 @@ static ossl_inline int ossl_ends_with_dirsep(const char *path)
     return *path == '/';
 }
 
+static ossl_inline int ossl_is_absolute_path(const char *path)
+{
+# if defined __VMS
+    if (strchr(path, ':') != NULL
+        || ((path[0] == '[' || path[0] == '<')
+            && path[1] != '.' && path[1] != '-'
+            && path[1] != ']' && path[1] != '>'))
+        return 1;
+# elif defined _WIN32
+    if (path[0] == '\\'
+        || (path[0] != '\0' && path[1] == ':'))
+        return 1;
+# endif
+    return path[0] == '/';
+}
+
 #endif