forked from xmlsec/python-xmlsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxml.c
138 lines (111 loc) · 3.98 KB
/
lxml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2017 Ryan Leckey
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "common.h"
#include "lxml.h"
#include "exception.h"
#include <etree_defs.h>
#include <etree_api.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/dict.h>
#define XMLSEC_EXTRACT_VERSION(x, y) ((x / (y)) % 100)
#define XMLSEC_EXTRACT_MAJOR(x) XMLSEC_EXTRACT_VERSION(x, 100 * 100)
#define XMLSEC_EXTRACT_MINOR(x) XMLSEC_EXTRACT_VERSION(x, 100)
#define XMLSEC_EXTRACT_PATCH(x) XMLSEC_EXTRACT_VERSION(x, 1)
static long PyXmlSec_GetLibXmlVersionLong() {
return PyOS_strtol(xmlParserVersion, NULL, 10);
}
long PyXmlSec_GetLibXmlVersionMajor() {
return XMLSEC_EXTRACT_MAJOR(PyXmlSec_GetLibXmlVersionLong());
}
long PyXmlSec_GetLibXmlVersionMinor() {
return XMLSEC_EXTRACT_MINOR(PyXmlSec_GetLibXmlVersionLong());
}
long PyXmlSec_GetLibXmlVersionPatch() {
return XMLSEC_EXTRACT_PATCH(PyXmlSec_GetLibXmlVersionLong());
}
long PyXmlSec_GetLibXmlCompiledVersionMajor() {
return XMLSEC_EXTRACT_MAJOR(LIBXML_VERSION);
}
long PyXmlSec_GetLibXmlCompiledVersionMinor() {
return XMLSEC_EXTRACT_MINOR(LIBXML_VERSION);
}
long PyXmlSec_GetLibXmlCompiledVersionPatch() {
return XMLSEC_EXTRACT_PATCH(LIBXML_VERSION);
}
static int PyXmlSec_CheckLxmlLibraryVersion(void) {
// Make sure that the version of libxml2 lxml is using is the same as the one we are using. Because
// we pass trees between the two libraries, we need to make sure that they are using the same version
// of libxml2, or we could run into difficult to debug segfaults.
// See: https://github.com/xmlsec/python-xmlsec/issues/283
PyObject* lxml = NULL;
PyObject* version = NULL;
// Default to failure
int result = -1;
lxml = PyImport_ImportModule("lxml.etree");
if (lxml == NULL) {
goto FINALIZE;
}
version = PyObject_GetAttrString(lxml, "LIBXML_VERSION");
if (version == NULL) {
goto FINALIZE;
}
if (!PyTuple_Check(version) || PyTuple_Size(version) < 2) {
goto FINALIZE;
}
PyObject* major = PyTuple_GetItem(version, 0);
PyObject* minor = PyTuple_GetItem(version, 1);
if (PyErr_Occurred()) {
goto FINALIZE;
}
if (!PyLong_Check(major) || !PyLong_Check(minor)) {
goto FINALIZE;
}
long lxml_major = PyLong_AsLong(major);
long lxml_minor = PyLong_AsLong(minor);
long xmlsec_major = PyXmlSec_GetLibXmlVersionMajor();
long xmlsec_minor = PyXmlSec_GetLibXmlVersionMinor();
if (PyErr_Occurred()) {
goto FINALIZE;
}
if (lxml_major != xmlsec_major || lxml_minor != xmlsec_minor) {
goto FINALIZE;
}
result = 0;
FINALIZE:
// Cleanup our references, and return the result
Py_XDECREF(lxml);
Py_XDECREF(version);
// Clear any errors that may have occurred
PyErr_Clear();
return result;
}
int PyXmlSec_InitLxmlModule(void) {
if (PyXmlSec_CheckLxmlLibraryVersion() < 0) {
PyXmlSec_SetLastError("lxml & xmlsec libxml2 library version mismatch");
return -1;
}
return import_lxml__etree();
}
int PyXmlSec_IsElement(xmlNodePtr xnode) {
return _isElement(xnode);
}
PyXmlSec_LxmlElementPtr PyXmlSec_elementFactory(PyXmlSec_LxmlDocumentPtr doc, xmlNodePtr xnode) {
return elementFactory(doc, xnode);
}
int PyXmlSec_LxmlElementConverter(PyObject* o, PyXmlSec_LxmlElementPtr* p) {
PyXmlSec_LxmlElementPtr node = rootNodeOrRaise(o);
if (node == NULL) {
return 0;
}
*p = node;
// rootNodeOrRaise - increments ref-count, so need to compensate this.
Py_DECREF(node);
return 1;
}