1258945Sroberto/*
2258945Sroberto * include/ssl_applink.c -- common NTP code for openssl/applink.c
3258945Sroberto *
4258945Sroberto * Each program which uses OpenSSL should include this file in _one_
5258945Sroberto * of its source files and call ssl_applink() before any OpenSSL
6258945Sroberto * functions.
7258945Sroberto */
8258945Sroberto
9258945Sroberto#if defined(OPENSSL) && defined(SYS_WINNT)
10258945Sroberto# ifdef _MSC_VER
11258945Sroberto#  pragma warning(push)
12258945Sroberto#  pragma warning(disable: 4152)
13316722Sdelphij#  ifndef OPENSSL_NO_AUTOLINK
14316722Sdelphij#   include "msvc_ssl_autolib.h"
15316722Sdelphij#  endif
16258945Sroberto# endif
17316722Sdelphij# if OPENSSL_VERSION_NUMBER < 0x10100000L
18316722Sdelphij#  include <openssl/applink.c>
19316722Sdelphij# endif
20258945Sroberto# ifdef _MSC_VER
21258945Sroberto#  pragma warning(pop)
22258945Sroberto# endif
23258945Sroberto#endif
24258945Sroberto
25258945Sroberto#if defined(OPENSSL) && defined(_MSC_VER) && defined(_DEBUG)
26258945Sroberto#define WRAP_DBG_MALLOC
27258945Sroberto#endif
28258945Sroberto
29258945Sroberto#ifdef WRAP_DBG_MALLOC
30330567Sgordonstatic void *wrap_dbg_malloc(size_t s, const char *f, int l);
31330567Sgordonstatic void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l);
32330567Sgordonstatic void wrap_dbg_free(void *p);
33330567Sgordonstatic void wrap_dbg_free_ex(void *p, const char *f, int l);
34258945Sroberto#endif
35258945Sroberto
36258945Sroberto
37258945Sroberto#if defined(OPENSSL) && defined(SYS_WINNT)
38316722Sdelphij
39258945Srobertovoid ssl_applink(void);
40258945Sroberto
41258945Srobertovoid
42258945Srobertossl_applink(void)
43258945Sroberto{
44316722Sdelphij#if OPENSSL_VERSION_NUMBER >= 0x10100000L
45330567Sgordon
46316722Sdelphij#   ifdef WRAP_DBG_MALLOC
47316722Sdelphij	CRYPTO_set_mem_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free_ex);
48316722Sdelphij#   else
49316722Sdelphij	OPENSSL_malloc_init();
50316722Sdelphij#   endif
51330567Sgordon
52330567Sgordon#  else
53330567Sgordon
54316722Sdelphij#   ifdef WRAP_DBG_MALLOC
55258945Sroberto	CRYPTO_set_mem_ex_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free);
56316722Sdelphij#   else
57258945Sroberto	CRYPTO_malloc_init();
58316722Sdelphij#   endif
59330567Sgordon
60316722Sdelphij#endif /* OpenSSL version cascade */
61258945Sroberto}
62258945Sroberto#else	/* !OPENSSL || !SYS_WINNT */
63258945Sroberto#define ssl_applink()	do {} while (0)
64258945Sroberto#endif
65258945Sroberto
66258945Sroberto
67258945Sroberto#ifdef WRAP_DBG_MALLOC
68258945Sroberto/*
69258945Sroberto * OpenSSL malloc overriding uses different parameters
70258945Sroberto * for DEBUG malloc/realloc/free (lacking block type).
71258945Sroberto * Simple wrappers convert.
72258945Sroberto */
73330567Sgordonstatic void *wrap_dbg_malloc(size_t s, const char *f, int l)
74258945Sroberto{
75258945Sroberto	void *ret;
76258945Sroberto
77258945Sroberto	ret = _malloc_dbg(s, _NORMAL_BLOCK, f, l);
78258945Sroberto	return ret;
79258945Sroberto}
80258945Sroberto
81330567Sgordonstatic void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l)
82258945Sroberto{
83258945Sroberto	void *ret;
84258945Sroberto
85258945Sroberto	ret = _realloc_dbg(p, s, _NORMAL_BLOCK, f, l);
86258945Sroberto	return ret;
87258945Sroberto}
88258945Sroberto
89330567Sgordonstatic void wrap_dbg_free(void *p)
90258945Sroberto{
91258945Sroberto	_free_dbg(p, _NORMAL_BLOCK);
92258945Sroberto}
93316722Sdelphij
94330567Sgordonstatic void wrap_dbg_free_ex(void *p, const char *f, int l)
95316722Sdelphij{
96316722Sdelphij	(void)f;
97316722Sdelphij	(void)l;
98316722Sdelphij	_free_dbg(p, _NORMAL_BLOCK);
99316722Sdelphij}
100258945Sroberto#endif	/* WRAP_DBG_MALLOC */
101