/
nokogiri.c
278 lines (230 loc) · 8.3 KB
/
nokogiri.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <nokogiri.h>
VALUE mNokogiri ;
VALUE mNokogiriGumbo ;
VALUE mNokogiriHtml ;
VALUE mNokogiriHtmlSax ;
VALUE mNokogiriHtml5 ;
VALUE mNokogiriXml ;
VALUE mNokogiriXmlSax ;
VALUE mNokogiriXmlXpath ;
VALUE mNokogiriXslt ;
VALUE cNokogiriSyntaxError;
VALUE cNokogiriXmlCharacterData;
VALUE cNokogiriXmlElement;
VALUE cNokogiriXmlXpathSyntaxError;
void noko_init_xml_attr();
void noko_init_xml_attribute_decl();
void noko_init_xml_cdata();
void noko_init_xml_comment();
void noko_init_xml_document();
void noko_init_xml_document_fragment();
void noko_init_xml_dtd();
void noko_init_xml_element_content();
void noko_init_xml_element_decl();
void noko_init_xml_encoding_handler();
void noko_init_xml_entity_decl();
void noko_init_xml_entity_reference();
void noko_init_xml_namespace();
void noko_init_xml_node();
void noko_init_xml_node_set();
void noko_init_xml_processing_instruction();
void noko_init_xml_reader();
void noko_init_xml_relax_ng();
void noko_init_xml_sax_parser();
void noko_init_xml_sax_parser_context();
void noko_init_xml_sax_push_parser();
void noko_init_xml_schema();
void noko_init_xml_syntax_error();
void noko_init_xml_text();
void noko_init_xml_xpath_context();
void noko_init_xslt_stylesheet();
void noko_init_html_document();
void noko_init_html_element_description();
void noko_init_html_entity_lookup();
void noko_init_html_sax_parser_context();
void noko_init_html_sax_push_parser();
void noko_init_gumbo();
void noko_init_test_global_handlers();
static ID id_read, id_write;
#ifndef HAVE_VASPRINTF
/*
* Thank you Geoffroy Couprie for this implementation of vasprintf!
*/
int
vasprintf(char **strp, const char *fmt, va_list ap)
{
/* Mingw32/64 have a broken vsnprintf implementation that fails when
* using a zero-byte limit in order to retrieve the required size for malloc.
* So we use a one byte buffer instead.
*/
char tmp[1];
int len = vsnprintf(tmp, 1, fmt, ap) + 1;
char *res = (char *)malloc((unsigned int)len);
if (res == NULL) {
return -1;
}
*strp = res;
return vsnprintf(res, (unsigned int)len, fmt, ap);
}
#endif
static VALUE
read_check(VALUE val)
{
VALUE *args = (VALUE *)val;
return rb_funcall(args[0], id_read, 1, args[1]);
}
static VALUE
read_failed(VALUE arg, VALUE exc)
{
return Qundef;
}
int
noko_io_read(void *ctx, char *buffer, int len)
{
VALUE string, args[2];
size_t str_len, safe_len;
args[0] = (VALUE)ctx;
args[1] = INT2NUM(len);
string = rb_rescue(read_check, (VALUE)args, read_failed, 0);
if (NIL_P(string)) { return 0; }
if (string == Qundef) { return -1; }
if (TYPE(string) != T_STRING) { return -1; }
str_len = (size_t)RSTRING_LEN(string);
safe_len = str_len > (size_t)len ? (size_t)len : str_len;
memcpy(buffer, StringValuePtr(string), safe_len);
return (int)safe_len;
}
static VALUE
write_check(VALUE val)
{
VALUE *args = (VALUE *)val;
return rb_funcall(args[0], id_write, 1, args[1]);
}
static VALUE
write_failed(VALUE arg, VALUE exc)
{
return Qundef;
}
int
noko_io_write(void *ctx, char *buffer, int len)
{
VALUE args[2], size;
args[0] = (VALUE)ctx;
args[1] = rb_str_new(buffer, (long)len);
size = rb_rescue(write_check, (VALUE)args, write_failed, 0);
if (size == Qundef) { return -1; }
return NUM2INT(size);
}
int
noko_io_close(void *ctx)
{
return 0;
}
void
Init_nokogiri()
{
mNokogiri = rb_define_module("Nokogiri");
mNokogiriGumbo = rb_define_module_under(mNokogiri, "Gumbo");
mNokogiriHtml = rb_define_module_under(mNokogiri, "HTML");
mNokogiriHtmlSax = rb_define_module_under(mNokogiriHtml, "SAX");
mNokogiriHtml5 = rb_define_module_under(mNokogiri, "HTML5");
mNokogiriXml = rb_define_module_under(mNokogiri, "XML");
mNokogiriXmlSax = rb_define_module_under(mNokogiriXml, "SAX");
mNokogiriXmlXpath = rb_define_module_under(mNokogiriXml, "XPath");
mNokogiriXslt = rb_define_module_under(mNokogiri, "XSLT");
rb_const_set(mNokogiri, rb_intern("LIBXML_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXML_DOTTED_VERSION));
rb_const_set(mNokogiri, rb_intern("LIBXML_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xmlParserVersion));
rb_const_set(mNokogiri, rb_intern("LIBXSLT_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXSLT_DOTTED_VERSION));
rb_const_set(mNokogiri, rb_intern("LIBXSLT_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xsltEngineVersion));
#ifdef NOKOGIRI_PACKAGED_LIBRARIES
rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qtrue);
# ifdef NOKOGIRI_PRECOMPILED_LIBRARIES
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qtrue);
# else
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qfalse);
# endif
rb_const_set(mNokogiri, rb_intern("LIBXML2_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXML2_PATCHES), " "));
rb_const_set(mNokogiri, rb_intern("LIBXSLT_PATCHES"), rb_str_split(NOKOGIRI_STR_NEW2(NOKOGIRI_LIBXSLT_PATCHES), " "));
#else
rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qfalse);
rb_const_set(mNokogiri, rb_intern("PRECOMPILED_LIBRARIES"), Qfalse);
rb_const_set(mNokogiri, rb_intern("LIBXML2_PATCHES"), Qnil);
rb_const_set(mNokogiri, rb_intern("LIBXSLT_PATCHES"), Qnil);
#endif
#ifdef LIBXML_ICONV_ENABLED
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qtrue);
#else
rb_const_set(mNokogiri, rb_intern("LIBXML_ICONV_ENABLED"), Qfalse);
#endif
#ifdef NOKOGIRI_OTHER_LIBRARY_VERSIONS
rb_const_set(mNokogiri, rb_intern("OTHER_LIBRARY_VERSIONS"), NOKOGIRI_STR_NEW2(NOKOGIRI_OTHER_LIBRARY_VERSIONS));
#endif
#if defined(_WIN32) && !defined(NOKOGIRI_PACKAGED_LIBRARIES)
/*
* We choose *not* to do use Ruby's memory management functions with windows DLLs because of this
* issue in libxml 2.9.12:
*
* https://github.com/sparklemotion/nokogiri/issues/2241
*
* If the atexit() issue gets fixed in a future version of libxml2, then we may be able to skip
* this config only for the specific libxml2 versions 2.9.12.
*
* Alternatively, now that Ruby has a generational GC, it might be OK to let libxml2 use its
* default memory management functions (recall that this config was introduced to reduce memory
* bloat and allow Ruby to GC more often); but we should *really* test with production workloads
* before making that kind of a potentially-invasive change.
*/
rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("default"));
#else
rb_const_set(mNokogiri, rb_intern("LIBXML_MEMORY_MANAGEMENT"), NOKOGIRI_STR_NEW2("ruby"));
xmlMemSetup((xmlFreeFunc)ruby_xfree, (xmlMallocFunc)ruby_xmalloc, (xmlReallocFunc)ruby_xrealloc, ruby_strdup);
#endif
xmlInitParser();
exsltRegisterAll();
if (xsltExtModuleFunctionLookup((xmlChar*)"date-time", EXSLT_DATE_NAMESPACE)) {
rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qtrue);
} else {
rb_const_set(mNokogiri, rb_intern("LIBXSLT_DATETIME_ENABLED"), Qfalse);
}
cNokogiriSyntaxError = rb_define_class_under(mNokogiri, "SyntaxError", rb_eStandardError);
noko_init_xml_syntax_error();
assert(cNokogiriXmlSyntaxError);
cNokogiriXmlXpathSyntaxError = rb_define_class_under(mNokogiriXmlXpath, "SyntaxError", cNokogiriXmlSyntaxError);
noko_init_xml_element_content();
noko_init_xml_encoding_handler();
noko_init_xml_namespace();
noko_init_xml_node_set();
noko_init_xml_reader();
noko_init_xml_sax_parser();
noko_init_xml_xpath_context();
noko_init_xslt_stylesheet();
noko_init_html_element_description();
noko_init_html_entity_lookup();
noko_init_xml_schema();
noko_init_xml_relax_ng();
noko_init_xml_sax_parser_context();
noko_init_html_sax_parser_context();
noko_init_xml_sax_push_parser();
noko_init_html_sax_push_parser();
noko_init_xml_node();
noko_init_xml_attr();
noko_init_xml_attribute_decl();
noko_init_xml_dtd();
noko_init_xml_element_decl();
noko_init_xml_entity_decl();
noko_init_xml_entity_reference();
noko_init_xml_processing_instruction();
assert(cNokogiriXmlNode);
cNokogiriXmlElement = rb_define_class_under(mNokogiriXml, "Element", cNokogiriXmlNode);
cNokogiriXmlCharacterData = rb_define_class_under(mNokogiriXml, "CharacterData", cNokogiriXmlNode);
noko_init_xml_comment();
noko_init_xml_text();
noko_init_xml_cdata();
noko_init_xml_document_fragment();
noko_init_xml_document();
noko_init_html_document();
noko_init_gumbo();
noko_init_test_global_handlers();
id_read = rb_intern("read");
id_write = rb_intern("write");
}