Convert stack.h to use inline functions.

Instead of a script which generates macros, emit static inlines in
individual header (or C files). This solves a few issues with the
original setup:

- The documentation was off. We match the documentation now.

- The stack macros did not check constness; see some of the fixes in
  crypto/x509.

- Type errors did not look like usual type errors.

- Any type which participated in STACK_OF had to be made partially
  public. This allows stack types to be defined an internal header or
  even an individual file.

- One could not pass sk_FOO_free into something which expects a function
  pointer.

Thanks to upstream's 411abf2dd37974a5baa54859c1abcd287b3c1181 for the
idea.

Change-Id: Ie5431390ccad761c17596b0e93941b0d7a68f904
Reviewed-on: https://boringssl-review.googlesource.com/16087
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/asn1/a_strnid.c b/crypto/asn1/a_strnid.c
index c558bce..379a79f 100644
--- a/crypto/asn1/a_strnid.c
+++ b/crypto/asn1/a_strnid.c
@@ -62,6 +62,9 @@
 #include <openssl/err.h>
 #include <openssl/mem.h>
 #include <openssl/obj.h>
+#include <openssl/stack.h>
+
+DEFINE_STACK_OF(ASN1_STRING_TABLE)
 
 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
 static void st_free(ASN1_STRING_TABLE *tbl);
diff --git a/crypto/ex_data.c b/crypto/ex_data.c
index 20b762e..8555e23 100644
--- a/crypto/ex_data.c
+++ b/crypto/ex_data.c
@@ -121,6 +121,8 @@
 #include "internal.h"
 
 
+DEFINE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
+
 struct crypto_ex_data_func_st {
   long argl;  /* Arbitary long */
   void *argp; /* Arbitary void pointer */
diff --git a/crypto/internal.h b/crypto/internal.h
index f58f1cf..21a1675 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -110,6 +110,7 @@
 #define OPENSSL_HEADER_CRYPTO_INTERNAL_H
 
 #include <openssl/ex_data.h>
+#include <openssl/stack.h>
 #include <openssl/thread.h>
 
 #include <string.h>
@@ -504,6 +505,8 @@
 
 typedef struct crypto_ex_data_func_st CRYPTO_EX_DATA_FUNCS;
 
+DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
+
 /* CRYPTO_EX_DATA_CLASS tracks the ex_indices registered for a type which
  * supports ex_data. It should defined as a static global within the module
  * which defines that type. */
diff --git a/crypto/stack/make_macros.sh b/crypto/stack/make_macros.sh
deleted file mode 100644
index 3c3691b..0000000
--- a/crypto/stack/make_macros.sh
+++ /dev/null
@@ -1,114 +0,0 @@
-#!/bin/sh
-
-include_dir=../../include/openssl
-
-cat > "${include_dir}/stack_macros.h" << EOF
-/* Copyright (c) 2014, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#if !defined(IN_STACK_H)
-#error "Don't include this file directly. Include stack.h."
-#endif
-
-EOF
-
-output_stack () {
-  type=$1
-  ptrtype=$2
-
-  cat >> "${include_dir}/stack_macros.h" << EOF
-/* ${type} */
-#define sk_${type}_new(comp)\\
-  ((STACK_OF(${type})*) sk_new(CHECKED_CAST(stack_cmp_func, int (*) (const ${ptrtype} *a, const ${ptrtype} *b), comp)))
-
-#define sk_${type}_new_null()\\
-  ((STACK_OF(${type})*) sk_new_null())
-
-#define sk_${type}_num(sk)\\
-  sk_num(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk))
-
-#define sk_${type}_zero(sk)\\
-  sk_zero(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk));
-
-#define sk_${type}_value(sk, i)\\
-  ((${ptrtype}) sk_value(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk), (i)))
-
-#define sk_${type}_set(sk, i, p)\\
-  ((${ptrtype}) sk_set(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (i), CHECKED_CAST(void*, ${ptrtype}, p)))
-
-#define sk_${type}_free(sk)\\
-  sk_free(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk))
-
-#define sk_${type}_pop_free(sk, free_func)\\
-  sk_pop_free(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void (*) (void*), void (*) (${ptrtype}), free_func))
-
-#define sk_${type}_insert(sk, p, where)\\
-  sk_insert(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p), (where))
-
-#define sk_${type}_delete(sk, where)\\
-  ((${ptrtype}) sk_delete(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (where)))
-
-#define sk_${type}_delete_ptr(sk, p)\\
-  ((${ptrtype}) sk_delete_ptr(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p)))
-
-#define sk_${type}_find(sk, out_index, p)\\
-  sk_find(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), (out_index), CHECKED_CAST(void*, ${ptrtype}, p))
-
-#define sk_${type}_shift(sk)\\
-  ((${ptrtype}) sk_shift(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk)))
-
-#define sk_${type}_push(sk, p)\\
-  sk_push(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(void*, ${ptrtype}, p))
-
-#define sk_${type}_pop(sk)\\
-  ((${ptrtype}) sk_pop(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk)))
-
-#define sk_${type}_dup(sk)\\
-  ((STACK_OF(${type})*) sk_dup(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk)))
-
-#define sk_${type}_sort(sk)\\
-  sk_sort(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk))
-
-#define sk_${type}_is_sorted(sk)\\
-  sk_is_sorted(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk))
-
-#define sk_${type}_set_cmp_func(sk, comp)\\
-  ((int (*) (const ${type} **a, const ${type} **b)) sk_set_cmp_func(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(stack_cmp_func, int (*) (const ${type} **a, const ${type} **b), comp)))
-
-#define sk_${type}_deep_copy(sk, copy_func, free_func)\\
-((STACK_OF(${type})*) sk_deep_copy(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk), CHECKED_CAST(void* (*) (void*), ${ptrtype} (*) (${ptrtype}), copy_func), CHECKED_CAST(void (*) (void*), void (*) (${ptrtype}), free_func)))
-
-EOF
-}
-
-stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* STACK_OF:' | sed -e 's/.*STACK_OF://' -e 's/ .*//')
-const_stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* CONST_STACK_OF:' | sed -e 's/.*CONST_STACK_OF://' -e 's/ .*//')
-special_stack_types=$(cat "${include_dir}/stack.h" | grep '^ \* SPECIAL_STACK_OF:' | sed -e 's/.*SPECIAL_STACK_OF://' -e 's/ .*//')
-
-for type in $stack_types; do
-  echo Stack of ${type}
-  output_stack "${type}" "${type} *"
-done
-
-for type in $const_stack_types; do
-  echo Stack of ${type}
-  output_stack "${type}" "const ${type} *"
-done
-
-for type in $special_stack_types; do
-  echo Stack of ${type}
-  output_stack "${type}" "${type}"
-done
-
-clang-format -i "${include_dir}/stack_macros.h"
diff --git a/crypto/x509/by_dir.c b/crypto/x509/by_dir.c
index e68ca5a..d72f9ec 100644
--- a/crypto/x509/by_dir.c
+++ b/crypto/x509/by_dir.c
@@ -84,8 +84,8 @@
     STACK_OF(BY_DIR_ENTRY) *dirs;
 } BY_DIR;
 
-DECLARE_STACK_OF(BY_DIR_HASH)
-DECLARE_STACK_OF(BY_DIR_ENTRY)
+DEFINE_STACK_OF(BY_DIR_HASH)
+DEFINE_STACK_OF(BY_DIR_ENTRY)
 
 static int dir_ctrl(X509_LOOKUP *ctx, int cmd, const char *argp, long argl,
                     char **ret);
diff --git a/crypto/x509/x_name.c b/crypto/x509/x_name.c
index 4abdc91..e6eeb95 100644
--- a/crypto/x509/x_name.c
+++ b/crypto/x509/x_name.c
@@ -71,7 +71,7 @@
 
 
 typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY;
-DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
+DEFINE_STACK_OF(STACK_OF_X509_NAME_ENTRY)
 
 /*
  * Maximum length of X509_NAME: much larger than anything we should
diff --git a/crypto/x509v3/pcy_int.h b/crypto/x509v3/pcy_int.h
index 1e76503..fc6e20a 100644
--- a/crypto/x509v3/pcy_int.h
+++ b/crypto/x509v3/pcy_int.h
@@ -59,7 +59,7 @@
 
 typedef struct X509_POLICY_DATA_st X509_POLICY_DATA;
 
-DECLARE_STACK_OF(X509_POLICY_DATA)
+DEFINE_STACK_OF(X509_POLICY_DATA)
 
 /* Internal structures */
 
@@ -207,7 +207,7 @@
                                const ASN1_OBJECT *id);
 
 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
-                                 const X509_POLICY_DATA *data,
+                                 X509_POLICY_DATA *data,
                                  X509_POLICY_NODE *parent,
                                  X509_POLICY_TREE *tree);
 void policy_node_free(X509_POLICY_NODE *node);
diff --git a/crypto/x509v3/pcy_node.c b/crypto/x509v3/pcy_node.c
index cf4e79d..b3edfe4 100644
--- a/crypto/x509v3/pcy_node.c
+++ b/crypto/x509v3/pcy_node.c
@@ -107,7 +107,7 @@
 }
 
 X509_POLICY_NODE *level_add_node(X509_POLICY_LEVEL *level,
-                                 const X509_POLICY_DATA *data,
+                                 X509_POLICY_DATA *data,
                                  X509_POLICY_NODE *parent,
                                  X509_POLICY_TREE *tree)
 {
diff --git a/crypto/x509v3/pcy_tree.c b/crypto/x509v3/pcy_tree.c
index a588107..71f9a6d 100644
--- a/crypto/x509v3/pcy_tree.c
+++ b/crypto/x509v3/pcy_tree.c
@@ -306,7 +306,7 @@
 }
 
 static int tree_link_matching_nodes(X509_POLICY_LEVEL *curr,
-                                    const X509_POLICY_DATA *data)
+                                    X509_POLICY_DATA *data)
 {
     X509_POLICY_LEVEL *last = curr - 1;
     X509_POLICY_NODE *node;