Namespace all lhash internal symbols.

LHASH_OF is only used internally (and can later be changed to be a C++
template). As such, its symbols can just all be moved into our
namespace.

Here I put the entire .cc file in the namespace block, as lhash has no
public symbols at all, and as it also contains structs that can't be
defined in the namespace.

Down from 974 to 906 unintended exported symbols.

Bug: 42220000
Change-Id: I6333a8c86b185326fa88a1a0b382a3421f201699
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/86247
Commit-Queue: Rudolf Polzer <rpolzer@google.com>
Reviewed-by: Xiangfei Ding <xfding@google.com>
diff --git a/crypto/asn1/a_strnid.cc b/crypto/asn1/a_strnid.cc
index 4502450..18ae288 100644
--- a/crypto/asn1/a_strnid.cc
+++ b/crypto/asn1/a_strnid.cc
@@ -29,8 +29,14 @@
 #include "internal.h"
 
 
+using namespace bssl;
+
+BSSL_NAMESPACE_BEGIN
+
 DEFINE_LHASH_OF(ASN1_STRING_TABLE)
 
+BSSL_NAMESPACE_END
+
 static LHASH_OF(ASN1_STRING_TABLE) *string_tables = nullptr;
 static CRYPTO_MUTEX string_tables_lock = CRYPTO_MUTEX_INIT;
 
diff --git a/crypto/conf/conf.cc b/crypto/conf/conf.cc
index 5b7b2f7..6836af9 100644
--- a/crypto/conf/conf.cc
+++ b/crypto/conf/conf.cc
@@ -27,6 +27,8 @@
 #include "internal.h"
 
 
+using namespace bssl;
+
 struct conf_section_st {
   char *name;
   // values contains non-owning pointers to the values in the section.
diff --git a/crypto/conf/internal.h b/crypto/conf/internal.h
index ea7a073..f1a29bc 100644
--- a/crypto/conf/internal.h
+++ b/crypto/conf/internal.h
@@ -19,16 +19,16 @@
 
 #include "../lhash/internal.h"
 
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
 
 typedef struct conf_section_st CONF_SECTION;
 
+BSSL_NAMESPACE_BEGIN
+
 DEFINE_LHASH_OF(CONF_SECTION)
 DEFINE_LHASH_OF(CONF_VALUE)
 
+BSSL_NAMESPACE_END
+
 struct conf_st {
   LHASH_OF(CONF_VALUE) *values;
   LHASH_OF(CONF_SECTION) *sections;
@@ -48,8 +48,4 @@
     int (*list_cb)(const char *elem, size_t len, void *usr), void *arg);
 
 
-#if defined(__cplusplus)
-}  // extern C
-#endif
-
 #endif  // OPENSSL_HEADER_CRYPTO_CONF_INTERNAL_H
diff --git a/crypto/lhash/internal.h b/crypto/lhash/internal.h
index e8e0b04..42931a6 100644
--- a/crypto/lhash/internal.h
+++ b/crypto/lhash/internal.h
@@ -17,18 +17,15 @@
 
 #include <openssl/base.h>
 
-#if defined(__cplusplus)
-extern "C" {
-#endif
 
+BSSL_NAMESPACE_BEGIN
 
 // lhash is a traditional, chaining hash table that automatically expands and
 // contracts as needed. One should not use the lh_* functions directly, rather
 // use the type-safe macro wrappers:
 //
 // A hash table of a specific type of object has type |LHASH_OF(type)|. This
-// can be defined (once) with |DEFINE_LHASH_OF(type)| and declared where needed
-// with |DECLARE_LHASH_OF(type)|. For example:
+// can be defined (once) with |DEFINE_LHASH_OF(type)|.
 //
 //   struct foo {
 //     int bar;
@@ -45,8 +42,7 @@
 // C++ template without any macros.
 
 
-#define LHASH_OF(type) struct type##_lhash_st
-#define DECLARE_LHASH_OF(type) LHASH_OF(type);
+#define LHASH_OF(type) struct bssl::type##_lhash_st
 
 // lhash_cmp_func is a comparison function that returns a value equal, or not
 // equal, to zero depending on whether |*a| is equal, or not equal to |*b|,
@@ -120,6 +116,8 @@
                                          void (*func)(void *, void *),
                                          void *arg);
 
+// DEFINE_LHASH_OF creates (inline) definitions of hash table. It must be used
+// from within the bssl namespace.
 #define DEFINE_LHASH_OF(type)                                                  \
   /* We disable MSVC C4191 in this macro, which warns when pointers are cast   \
    * to the wrong type. While the cast itself is valid, it is often a bug      \
@@ -131,7 +129,7 @@
   OPENSSL_MSVC_PRAGMA(warning(push))                                           \
   OPENSSL_MSVC_PRAGMA(warning(disable : 4191))                                 \
                                                                                \
-  DECLARE_LHASH_OF(type)                                                       \
+  struct type##_lhash_st;                                                      \
                                                                                \
   typedef int (*lhash_##type##_cmp_func)(const type *, const type *);          \
   typedef uint32_t (*lhash_##type##_hash_func)(const type *);                  \
@@ -219,9 +217,6 @@
                                                                                \
   OPENSSL_MSVC_PRAGMA(warning(pop))
 
-
-#if defined(__cplusplus)
-}  // extern C
-#endif
+BSSL_NAMESPACE_END
 
 #endif  // OPENSSL_HEADER_CRYPTO_LHASH_INTERNAL_H
diff --git a/crypto/lhash/lhash.cc b/crypto/lhash/lhash.cc
index 7fab7fa..a2cfc0c 100644
--- a/crypto/lhash/lhash.cc
+++ b/crypto/lhash/lhash.cc
@@ -22,6 +22,8 @@
 #include "internal.h"
 
 
+BSSL_NAMESPACE_BEGIN
+
 // kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|.
 static const size_t kMinNumBuckets = 16;
 
@@ -305,3 +307,5 @@
   // resizing is done here.
   lh_maybe_resize(lh);
 }
+
+BSSL_NAMESPACE_END
diff --git a/crypto/lhash/lhash_test.cc b/crypto/lhash/lhash_test.cc
index 350957f..1f4ba68 100644
--- a/crypto/lhash/lhash_test.cc
+++ b/crypto/lhash/lhash_test.cc
@@ -30,10 +30,12 @@
 #include "internal.h"
 
 
-namespace {
+BSSL_NAMESPACE_BEGIN
 
 DEFINE_LHASH_OF(char)
 
+namespace {
+
 static std::unique_ptr<char[]> RandString() {
   unsigned len = 1 + (rand() % 3);
   auto ret = std::make_unique<char[]>(len + 1);
@@ -144,3 +146,4 @@
 }
 
 }  // namespace
+BSSL_NAMESPACE_END
diff --git a/crypto/obj/obj.cc b/crypto/obj/obj.cc
index eded589..c5d9b04 100644
--- a/crypto/obj/obj.cc
+++ b/crypto/obj/obj.cc
@@ -33,8 +33,14 @@
 #include "obj_dat.h"
 
 
+using namespace bssl;
+
+BSSL_NAMESPACE_BEGIN
+
 DEFINE_LHASH_OF(ASN1_OBJECT)
 
+BSSL_NAMESPACE_END
+
 static CRYPTO_MUTEX global_added_lock = CRYPTO_MUTEX_INIT;
 // These globals are protected by |global_added_lock|.
 static LHASH_OF(ASN1_OBJECT) *global_added_by_data = nullptr;
diff --git a/crypto/pool/internal.h b/crypto/pool/internal.h
index d04e5c1..7f7e856 100644
--- a/crypto/pool/internal.h
+++ b/crypto/pool/internal.h
@@ -19,13 +19,12 @@
 #include "../lhash/internal.h"
 
 
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
+BSSL_NAMESPACE_BEGIN
 
 DEFINE_LHASH_OF(CRYPTO_BUFFER)
 
+BSSL_NAMESPACE_END
+
 struct crypto_buffer_st {
   CRYPTO_BUFFER_POOL *pool;
   uint8_t *data;
@@ -40,9 +39,4 @@
   const uint64_t hash_key[2];
 };
 
-
-#if defined(__cplusplus)
-}  // extern C
-#endif
-
 #endif  // OPENSSL_HEADER_CRYPTO_POOL_INTERNAL_H
diff --git a/crypto/pool/pool.cc b/crypto/pool/pool.cc
index 13524f7..665f74f 100644
--- a/crypto/pool/pool.cc
+++ b/crypto/pool/pool.cc
@@ -26,6 +26,8 @@
 #include "internal.h"
 
 
+using namespace bssl;
+
 static uint32_t CRYPTO_BUFFER_hash(const CRYPTO_BUFFER *buf) {
   return (uint32_t)SIPHASH_24(buf->pool->hash_key, buf->data, buf->len);
 }
diff --git a/ssl/internal.h b/ssl/internal.h
index 9048ad3..e08b5f1 100644
--- a/ssl/internal.h
+++ b/ssl/internal.h
@@ -3762,7 +3762,7 @@
   bssl::UniquePtr<bssl::SSLCipherPreferenceList> cipher_list;
 
   X509_STORE *cert_store = nullptr;
-  LHASH_OF(bssl::SSL_SESSION) *sessions = nullptr;
+  LHASH_OF(SSL_SESSION) *sessions = nullptr;
   // Most session-ids that will be cached, default is
   // SSL_SESSION_CACHE_MAX_SIZE_DEFAULT. 0 is unlimited.
   unsigned long session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;