-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Expand file tree
/
Copy pathobj_map.c
More file actions
517 lines (455 loc) · 16 KB
/
Copy pathobj_map.c
File metadata and controls
517 lines (455 loc) · 16 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Christian Stocker <chregu@php.net> |
| Rob Richards <rrichards@php.net> |
| Niels Dossche <nielsdos@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
#include "php_dom.h"
#include "obj_map.h"
static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap)
{
if (objmap->cached_obj) {
OBJ_RELEASE(&objmap->cached_obj->std);
objmap->cached_obj = NULL;
objmap->cached_obj_index = 0;
}
}
static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap)
{
objmap_cache_release_cached_obj(objmap);
objmap->cached_length = -1;
}
/**************************
* === Length methods === *
**************************/
static zend_long dom_map_get_xmlht_length(dom_nnodemap_object *map)
{
/* Note: if there are, for example, no entities or notations then the hash table can be NULL. */
return map->ht ? xmlHashSize(map->ht) : 0;
}
static zend_long dom_map_get_nodeset_length(dom_nnodemap_object *map)
{
return zend_hash_num_elements(map->array);
}
static zend_long dom_map_get_prop_length(dom_nnodemap_object *map)
{
zend_long count = 0;
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
if (nodep) {
for (xmlAttrPtr curnode = nodep->properties; curnode; curnode = curnode->next) {
count++;
}
}
return count;
}
static zend_long dom_map_get_nodes_length(dom_nnodemap_object *map)
{
zend_long count = 0;
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
if (nodep) {
for (xmlNodePtr curnode = dom_nodelist_iter_start_first_child(nodep); curnode; curnode = curnode->next) {
count++;
}
}
return count;
}
static zend_long dom_map_get_elements_length(dom_nnodemap_object *map)
{
zend_long count = 0;
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
if (nodep) {
for (xmlNodePtr curnode = dom_nodelist_iter_start_first_child(nodep); curnode; curnode = curnode->next) {
if (curnode->type == XML_ELEMENT_NODE) {
count++;
}
}
}
return count;
}
static zend_long dom_map_get_by_tag_name_length(dom_nnodemap_object *map)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
zend_long count = 0;
if (nodep) {
xmlNodePtr basep = nodep;
nodep = php_dom_first_child_of_container_node(basep);
dom_get_elements_by_tag_name_ns_raw(
basep, nodep, map->ns, map->local, map->local_lower, &count, ZEND_LONG_MAX - 1 /* because of <= */);
}
return count;
}
static zend_long dom_map_get_zero_length(dom_nnodemap_object *map)
{
return 0;
}
/************************
* === Item lookups === *
************************/
static void dom_ret_node_to_zobj(dom_nnodemap_object *map, xmlNodePtr node, zval *return_value)
{
if (node) {
DOM_RET_OBJ(node, map->baseobj);
} else {
RETURN_NULL();
}
}
static void dom_map_get_entity_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr node = map->ht ? php_dom_libxml_hash_iter(map->ht, index) : NULL;
dom_ret_node_to_zobj(map, node, return_value);
}
static void dom_map_get_notation_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr node = map->ht ? php_dom_libxml_hash_iter(map->ht, index) : NULL;
if (node) {
xmlNotation *notation = (xmlNotation *) node;
node = create_notation(notation->name, notation->PublicID, notation->SystemID);
}
dom_ret_node_to_zobj(map, node, return_value);
}
static void dom_map_get_nodeset_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
zval *entry = zend_hash_index_find(map->array, index);
if (entry) {
RETURN_COPY(entry);
} else {
RETURN_NULL();
}
}
typedef struct dom_node_idx_pair {
xmlNodePtr node;
zend_long index;
} dom_node_idx_pair;
static dom_node_idx_pair dom_obj_map_get_start_point(dom_nnodemap_object *map, xmlNodePtr basep, zend_long index)
{
dom_node_idx_pair ret;
ZEND_ASSERT(basep != NULL);
/* For now we're only able to use cache for forward search.
* TODO: in the future we could extend the logic of the node list such that backwards searches
* are also possible. */
bool restart = true;
zend_long relative_index = index;
if (index >= map->cached_obj_index && map->cached_obj && !php_dom_is_cache_tag_stale_from_node(&map->cache_tag, basep)) {
xmlNodePtr cached_obj_xml_node = dom_object_get_node(map->cached_obj);
/* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
* forgot an invalidation somewhere. Take the defensive programming approach and invalidate
* it here if it's NULL (except in debug mode where we would want to catch this). */
if (UNEXPECTED(cached_obj_xml_node == NULL)) {
#if ZEND_DEBUG
ZEND_UNREACHABLE();
#endif
reset_objmap_cache(map);
} else {
restart = false;
relative_index -= map->cached_obj_index;
basep = cached_obj_xml_node;
}
}
ret.node = restart ? NULL : basep;
ret.index = relative_index;
return ret;
}
static void dom_map_cache_obj(dom_nnodemap_object *map, xmlNodePtr itemnode, zend_long index, zval *return_value)
{
/* Hold additional reference for the cache, must happen before releasing the cache
* because we might be the last reference holder.
* Instead of storing and copying zvals, we store the object pointer directly.
* This saves us some bytes because a pointer is smaller than a zval.
* This also means we have to manually refcount the objects here, and remove the reference count
* in reset_objmap_cache() and the destructor. */
dom_object *cached_obj = Z_DOMOBJ_P(return_value);
GC_ADDREF(&cached_obj->std);
/* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
if (php_dom_is_cache_tag_stale_from_node(&map->cache_tag, itemnode)) {
php_dom_mark_cache_tag_up_to_date_from_node(&map->cache_tag, itemnode);
reset_objmap_cache(map);
} else {
objmap_cache_release_cached_obj(map);
}
map->cached_obj_index = index;
map->cached_obj = cached_obj;
}
static void dom_map_get_attributes_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
xmlNodePtr itemnode = NULL;
if (nodep && index >= 0) {
ZEND_ASSERT(nodep->type == XML_ELEMENT_NODE);
itemnode = (xmlNodePtr) nodep->properties;
for (; index > 0 && itemnode; itemnode = itemnode->next, index--);
}
dom_ret_node_to_zobj(map, itemnode, return_value);
}
static void dom_map_get_nodes_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
xmlNodePtr itemnode = NULL;
if (nodep && index >= 0) {
dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index);
itemnode = start_point.node ? start_point.node : dom_nodelist_iter_start_first_child(nodep);
for (; start_point.index > 0 && itemnode; itemnode = itemnode->next, start_point.index--);
}
dom_ret_node_to_zobj(map, itemnode, return_value);
if (itemnode) {
dom_map_cache_obj(map, itemnode, index, return_value);
}
}
static void dom_map_get_elements_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
xmlNodePtr itemnode = NULL;
if (nodep && index >= 0) {
dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index);
if (start_point.node) {
/* Guaranteed to be an element */
itemnode = start_point.node;
} else {
/* Fetch first element child */
itemnode = nodep->children;
while (itemnode && itemnode->type != XML_ELEMENT_NODE) {
itemnode = itemnode->next;
}
}
for (; start_point.index > 0 && itemnode; --start_point.index) {
do {
itemnode = itemnode->next;
} while (itemnode && itemnode->type != XML_ELEMENT_NODE);
}
if (itemnode && itemnode->type != XML_ELEMENT_NODE) {
itemnode = NULL;
}
}
dom_ret_node_to_zobj(map, itemnode, return_value);
if (itemnode) {
dom_map_cache_obj(map, itemnode, index, return_value);
}
}
static void dom_map_get_by_tag_name_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
xmlNodePtr itemnode = NULL;
if (nodep && index >= 0) {
zend_long count = 0;
dom_node_idx_pair start_point = dom_obj_map_get_start_point(map, nodep, index);
itemnode = start_point.node ? start_point.node : php_dom_first_child_of_container_node(nodep);
itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, itemnode, map->ns, map->local, map->local_lower, &count, start_point.index);
}
dom_ret_node_to_zobj(map, itemnode, return_value);
if (itemnode) {
dom_map_cache_obj(map, itemnode, index, return_value);
}
}
static void dom_map_get_null_item(dom_nnodemap_object *map, zend_long index, zval *return_value)
{
RETURN_NULL();
}
/***********************
* === Common APIs === *
***********************/
zend_long php_dom_get_nodelist_length(dom_object *obj)
{
dom_nnodemap_object *objmap = obj->ptr;
if (objmap->handler->use_cache) {
xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
if (!nodep) {
return 0;
}
if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
if (objmap->cached_length >= 0) {
return objmap->cached_length;
}
/* Only the length is out-of-date, the cache tag is still valid.
* Therefore, only overwrite the length and keep the currently cached object. */
} else {
php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
reset_objmap_cache(objmap);
}
}
zend_long count = objmap->handler->length(objmap);
if (objmap->handler->use_cache) {
objmap->cached_length = count;
}
return count;
}
void php_dom_create_obj_map(dom_object *basenode, dom_object *intern, xmlHashTablePtr ht, zend_string *local, zend_string *ns, const php_dom_obj_map_handler *handler)
{
dom_nnodemap_object *mapptr = intern->ptr;
ZEND_ASSERT(basenode != NULL);
GC_ADDREF(&basenode->std);
xmlDocPtr doc = basenode->document ? basenode->document->ptr : NULL;
mapptr->handler = handler;
mapptr->baseobj = basenode;
mapptr->ht = ht;
if (EXPECTED(doc != NULL)) {
mapptr->dict = doc->dict;
xmlDictReference(doc->dict);
}
const xmlChar* tmp;
if (local) {
int len = (int) ZSTR_LEN(local);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(local), len)) != NULL) {
mapptr->local = BAD_CAST tmp;
} else {
mapptr->local = BAD_CAST ZSTR_VAL(zend_string_copy(local));
mapptr->release_local = true;
}
mapptr->local_lower = zend_string_tolower(local);
}
if (ns) {
int len = (int) ZSTR_LEN(ns);
if (doc != NULL && (tmp = xmlDictExists(doc->dict, (const xmlChar *)ZSTR_VAL(ns), len)) != NULL) {
mapptr->ns = BAD_CAST tmp;
} else {
mapptr->ns = BAD_CAST ZSTR_VAL(zend_string_copy(ns));
mapptr->release_ns = true;
}
}
}
void php_dom_obj_map_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
{
if (EXPECTED(objmap)) {
objmap->handler->get_item(objmap, index, return_value);
} else {
RETURN_NULL();
}
}
void php_dom_obj_map_get_named_item_into_zval(dom_nnodemap_object *objmap, const zend_string *named, const char *ns, zval *return_value)
{
xmlNodePtr itemnode = objmap->handler->get_named_item(objmap, named, ns);
if (itemnode) {
DOM_RET_OBJ(itemnode, objmap->baseobj);
} else {
RETURN_NULL();
}
}
/**********************
* === Named item === *
**********************/
static xmlNodePtr dom_map_get_named_item_entity(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
return xmlHashLookup(map->ht, BAD_CAST ZSTR_VAL(named));
}
static bool dom_map_has_named_item_xmlht(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
return dom_map_get_named_item_entity(map, named, ns) != NULL;
}
static xmlNodePtr dom_map_get_named_item_notation(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
xmlNotationPtr notation = xmlHashLookup(map->ht, BAD_CAST ZSTR_VAL(named));
if (notation) {
return create_notation(notation->name, notation->PublicID, notation->SystemID);
}
return NULL;
}
static xmlNodePtr dom_map_get_named_item_prop(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
xmlNodePtr nodep = dom_object_get_node(map->baseobj);
if (nodep) {
if (php_dom_follow_spec_intern(map->baseobj)) {
return (xmlNodePtr) php_dom_get_attribute_node(nodep, BAD_CAST ZSTR_VAL(named), ZSTR_LEN(named));
} else {
if (ns) {
return (xmlNodePtr) xmlHasNsProp(nodep, BAD_CAST ZSTR_VAL(named), BAD_CAST ns);
} else {
return (xmlNodePtr) xmlHasProp(nodep, BAD_CAST ZSTR_VAL(named));
}
}
}
return NULL;
}
static bool dom_map_has_named_item_prop(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
return dom_map_get_named_item_prop(map, named, ns) != NULL;
}
static xmlNodePtr dom_map_get_named_item_null(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
return NULL;
}
static bool dom_map_has_named_item_null(dom_nnodemap_object *map, const zend_string *named, const char *ns)
{
return false;
}
/**************************
* === Handler tables === *
**************************/
const php_dom_obj_map_handler php_dom_obj_map_attributes = {
.length = dom_map_get_prop_length,
.get_item = dom_map_get_attributes_item,
.get_named_item = dom_map_get_named_item_prop,
.has_named_item = dom_map_has_named_item_prop,
.use_cache = false,
.nameless = false,
};
const php_dom_obj_map_handler php_dom_obj_map_by_tag_name = {
.length = dom_map_get_by_tag_name_length,
.get_item = dom_map_get_by_tag_name_item,
.get_named_item = dom_map_get_named_item_null,
.has_named_item = dom_map_has_named_item_null,
.use_cache = true,
.nameless = true,
};
const php_dom_obj_map_handler php_dom_obj_map_child_nodes = {
.length = dom_map_get_nodes_length,
.get_item = dom_map_get_nodes_item,
.get_named_item = dom_map_get_named_item_null,
.has_named_item = dom_map_has_named_item_null,
.use_cache = true,
.nameless = true,
};
const php_dom_obj_map_handler php_dom_obj_map_nodeset = {
.length = dom_map_get_nodeset_length,
.get_item = dom_map_get_nodeset_item,
.get_named_item = dom_map_get_named_item_null,
.has_named_item = dom_map_has_named_item_null,
.use_cache = false,
.nameless = true,
};
const php_dom_obj_map_handler php_dom_obj_map_entities = {
.length = dom_map_get_xmlht_length,
.get_item = dom_map_get_entity_item,
.get_named_item = dom_map_get_named_item_entity,
.has_named_item = dom_map_has_named_item_xmlht,
.use_cache = false,
.nameless = false,
};
const php_dom_obj_map_handler php_dom_obj_map_notations = {
.length = dom_map_get_xmlht_length,
.get_item = dom_map_get_notation_item,
.get_named_item = dom_map_get_named_item_notation,
.has_named_item = dom_map_has_named_item_xmlht,
.use_cache = false,
.nameless = false,
};
const php_dom_obj_map_handler php_dom_obj_map_child_elements = {
.length = dom_map_get_elements_length,
.get_item = dom_map_get_elements_item,
.get_named_item = dom_map_get_named_item_null,
.has_named_item = dom_map_has_named_item_null,
.use_cache = true,
.nameless = true,
};
const php_dom_obj_map_handler php_dom_obj_map_noop = {
.length = dom_map_get_zero_length,
.get_item = dom_map_get_null_item,
.get_named_item = dom_map_get_named_item_null,
.has_named_item = dom_map_has_named_item_null,
.use_cache = false,
.nameless = true,
};
#endif