From 7926159c807cc740c056e130cf34a0dd08061ba6 Mon Sep 17 00:00:00 2001
From: Joyee Cheung <joyeec9h3@gmail.com>
Date: Wed, 19 Dec 2018 03:05:49 +0800
Subject: [PATCH] src: move GetOpenSSLVersion into node_metadata.cc

Instead of implementing it in node_crypto.cc even though the only
place that needs it is the `Metadata::Versions` constructor.

PR-URL: https://github.com/nodejs/node/pull/25115
Reviewed-By: Steven R Loomis <srloomis@us.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
---
 src/node_crypto.cc   | 15 ---------------
 src/node_crypto.h    |  1 -
 src/node_metadata.cc | 23 ++++++++++++++++++++---
 3 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index 9ec65004aaa2a2..73e2ad0e0608e2 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -6364,21 +6364,6 @@ void Initialize(Local<Object> target,
 #endif  // OPENSSL_NO_SCRYPT
 }
 
-constexpr int search(const char* s, int n, int c) {
-  return *s == c ? n : search(s + 1, n + 1, c);
-}
-
-std::string GetOpenSSLVersion() {
-  // sample openssl version string format
-  // for reference: "OpenSSL 1.1.0i 14 Aug 2018"
-  char buf[128];
-  const int start = search(OPENSSL_VERSION_TEXT, 0, ' ') + 1;
-  const int end = search(OPENSSL_VERSION_TEXT + start, start, ' ');
-  const int len = end - start;
-  snprintf(buf, sizeof(buf), "%.*s", len, &OPENSSL_VERSION_TEXT[start]);
-  return std::string(buf);
-}
-
 }  // namespace crypto
 }  // namespace node
 
diff --git a/src/node_crypto.h b/src/node_crypto.h
index ef8f2bce2e99f4..1b950846a7240c 100644
--- a/src/node_crypto.h
+++ b/src/node_crypto.h
@@ -94,7 +94,6 @@ extern int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx);
 extern void UseExtraCaCerts(const std::string& file);
 
 void InitCryptoOnce();
-std::string GetOpenSSLVersion();
 
 class SecureContext : public BaseObject {
  public:
diff --git a/src/node_metadata.cc b/src/node_metadata.cc
index 822b6a490d13d2..3ddec4b36afc1b 100644
--- a/src/node_metadata.cc
+++ b/src/node_metadata.cc
@@ -9,8 +9,8 @@
 #include "zlib.h"
 
 #if HAVE_OPENSSL
-#include "node_crypto.h"
-#endif
+#include <openssl/opensslv.h>
+#endif  // HAVE_OPENSSL
 
 namespace node {
 
@@ -18,6 +18,23 @@ namespace per_process {
 Metadata metadata;
 }
 
+#if HAVE_OPENSSL
+constexpr int search(const char* s, int n, int c) {
+  return *s == c ? n : search(s + 1, n + 1, c);
+}
+
+std::string GetOpenSSLVersion() {
+  // sample openssl version string format
+  // for reference: "OpenSSL 1.1.0i 14 Aug 2018"
+  char buf[128];
+  const int start = search(OPENSSL_VERSION_TEXT, 0, ' ') + 1;
+  const int end = search(OPENSSL_VERSION_TEXT + start, start, ' ');
+  const int len = end - start;
+  snprintf(buf, sizeof(buf), "%.*s", len, &OPENSSL_VERSION_TEXT[start]);
+  return std::string(buf);
+}
+#endif  // HAVE_OPENSSL
+
 Metadata::Versions::Versions() {
   node = NODE_VERSION_STRING;
   v8 = v8::V8::GetVersion();
@@ -31,7 +48,7 @@ Metadata::Versions::Versions() {
   http_parser = http_parser_version;
 
 #if HAVE_OPENSSL
-  openssl = crypto::GetOpenSSLVersion();
+  openssl = GetOpenSSLVersion();
 #endif
 }