Add method to test for algorithm support.
[shibboleth/cpp-xmltooling.git] / xmltooling / internal.h
1 /*
2  *  Copyright 2001-2010 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  *  internal.h - internally visible classes
19  */
20
21 #ifndef __xmltooling_internal_h__
22 #define __xmltooling_internal_h__
23
24 #ifdef WIN32
25 # define _CRT_SECURE_NO_DEPRECATE 1
26 # define _CRT_NONSTDC_NO_DEPRECATE 1
27 #endif
28
29 // Export public APIs.
30 #define XMLTOOLING_EXPORTS
31
32 // eventually we might be able to support autoconf via cygwin...
33 #if defined (_MSC_VER) || defined(__BORLANDC__)
34 # include "config_win32.h"
35 #else
36 # include "config.h"
37 #endif
38
39 #include "base.h"
40 #include "XMLToolingConfig.h"
41 #include "util/ParserPool.h"
42
43 #include <vector>
44 #ifndef XMLTOOLING_NO_XMLSEC
45     #include <xsec/framework/XSECProvider.hpp>
46 #endif
47
48 #define XMLTOOLING_LOGCAT "XMLTooling"
49
50 // Macros for path and directory separators.
51 #if defined __CYGWIN32__ && !defined __CYGWIN__
52    /* For backwards compatibility with Cygwin b19 and
53       earlier, we define __CYGWIN__ here, so that
54       we can rely on checking just for that macro. */
55 #  define __CYGWIN__  __CYGWIN32__
56 #endif
57
58 #if defined _WIN32 && !defined __CYGWIN__
59    /* Use Windows separators on all _WIN32 defining
60       environments, except Cygwin. */
61 #  define DIR_SEPARATOR_CHAR        '\\'
62 #  define DIR_SEPARATOR_STR         "\\"
63 #  define PATH_SEPARATOR_CHAR       ';'
64 #  define PATH_SEPARATOR_STR        ";"
65 #endif
66 #ifndef DIR_SEPARATOR_CHAR
67    /* Assume that not having this is an indicator that all
68       are missing. */
69 #  define DIR_SEPARATOR_CHAR        '/'
70 #  define DIR_SEPARATOR_STR         "/"
71 #  define PATH_SEPARATOR_CHAR       ':'
72 #  define PATH_SEPARATOR_STR        ":"
73 #endif /* !DIR_SEPARATOR_CHAR */
74
75 namespace xmltooling {
76     
77     /// @cond OFF
78     class XMLToolingInternalConfig : public XMLToolingConfig
79     {
80     public:
81         XMLToolingInternalConfig() : m_lock(nullptr), m_parserPool(nullptr), m_validatingPool(nullptr) {
82 #ifndef XMLTOOLING_NO_XMLSEC
83             m_xsecProvider=nullptr;
84 #endif
85         }
86
87         static XMLToolingInternalConfig& getInternalConfig();
88
89         // global per-process setup and shutdown of runtime
90         bool init();
91         void term();
92
93         // global mutex available to library applications
94         Lockable* lock();
95         void unlock();
96
97         // configuration
98         bool load_library(const char* path, void* context=nullptr);
99         bool log_config(const char* config=nullptr);
100
101         // parser access
102         ParserPool& getParser() const {
103             return *m_parserPool;
104         }
105
106         ParserPool& getValidatingParser() const {
107             return *m_validatingPool;
108         }
109
110 #ifndef XMLTOOLING_NO_XMLSEC
111         XSECCryptoX509CRL* X509CRL() const;
112
113         std::pair<const char*,unsigned int> mapXMLAlgorithmToKeyAlgorithm(const XMLCh* xmlAlgorithm) const {
114 # ifdef HAVE_GOOD_STL
115             algmap_t::const_iterator i = m_algorithmMap.find(xmlAlgorithm);
116 # else
117             auto_ptr_char alg(xmlAlgorithm);
118             algmap_t::const_iterator i = m_algorithmMap.find(alg.get());
119 # endif
120             if (i==m_algorithmMap.end())
121                 return std::pair<const char*,unsigned int>(nullptr,0);
122             return std::make_pair(i->second.first.c_str(), i->second.second);
123         }
124
125         void registerXMLAlgorithm(const XMLCh* xmlAlgorithm, const char* keyAlgorithm, unsigned int size=0) {
126 # ifdef HAVE_GOOD_STL
127             m_algorithmMap[xmlAlgorithm] = std::pair<std::string,unsigned int>(keyAlgorithm,size);
128 # else
129             auto_ptr_char alg(xmlAlgorithm);
130             m_algorithmMap[alg.get()] = std::pair<std::string,unsigned int>(keyAlgorithm,size);
131 # endif
132         }
133
134         bool isXMLAlgorithmSupported(const XMLCh* xmlAlgorithm);
135         void registerXMLAlgorithms();
136
137         XSECProvider* m_xsecProvider;
138     private:
139 # ifdef HAVE_GOOD_STL
140         typedef std::map< xstring,std::pair<std::string,unsigned int> > algmap_t;
141 # else
142         typedef std::map< std::string,std::pair<std::string,unsigned int> > algmap_t;
143 # endif
144         algmap_t m_algorithmMap;
145 #endif
146
147     private:
148         std::vector<void*> m_libhandles;
149         void* m_lock;
150         ParserPool* m_parserPool;
151         ParserPool* m_validatingPool;
152     };
153     
154 #ifndef XMLTOOLING_NO_XMLSEC
155     void log_openssl();
156 #endif
157     
158     /// @endcond
159
160 };
161
162 #endif /* __xmltooling_internal_h__ */