@@ -2183,17 +2183,37 @@ sxe_object_new(zend_class_entry *ce)
2183
2183
}
2184
2184
/* }}} */
2185
2185
2186
+ static void sxe_create_obj_from_doc (zval * return_value , xmlDocPtr docp , zend_class_entry * ce , zend_string * ns , bool isprefix )
2187
+ {
2188
+ if (!docp ) {
2189
+ RETURN_FALSE ;
2190
+ }
2191
+
2192
+ zend_function * fptr_count ;
2193
+ if (!ce ) {
2194
+ ce = ce_SimpleXMLElement ;
2195
+ fptr_count = NULL ;
2196
+ } else {
2197
+ fptr_count = php_sxe_find_fptr_count (ce );
2198
+ }
2199
+ php_sxe_object * sxe = php_sxe_object_new (ce , fptr_count );
2200
+ sxe -> iter .nsprefix = ZSTR_LEN (ns ) ? zend_string_copy (ns ) : NULL ;
2201
+ sxe -> iter .isprefix = isprefix ;
2202
+ php_libxml_increment_doc_ref ((php_libxml_node_object * )sxe , docp );
2203
+ php_libxml_increment_node_ptr ((php_libxml_node_object * )sxe , xmlDocGetRootElement (docp ), NULL );
2204
+
2205
+ RETURN_OBJ (& sxe -> zo );
2206
+ }
2207
+
2186
2208
/* {{{ Load a filename and return a simplexml_element object to allow for processing */
2187
2209
PHP_FUNCTION (simplexml_load_file )
2188
2210
{
2189
- php_sxe_object * sxe ;
2190
2211
char * filename ;
2191
2212
size_t filename_len ;
2192
2213
xmlDocPtr docp ;
2193
2214
zend_string * ns = zend_empty_string ;
2194
2215
zend_long options = 0 ;
2195
2216
zend_class_entry * ce = ce_SimpleXMLElement ;
2196
- zend_function * fptr_count ;
2197
2217
bool isprefix = 0 ;
2198
2218
2199
2219
if (zend_parse_parameters (ZEND_NUM_ARGS (), "p|C!lSb" , & filename , & filename_len , & ce , & options , & ns , & isprefix ) == FAILURE ) {
@@ -2209,37 +2229,70 @@ PHP_FUNCTION(simplexml_load_file)
2209
2229
docp = xmlReadFile (filename , NULL , (int )options );
2210
2230
PHP_LIBXML_RESTORE_GLOBALS (read_file );
2211
2231
2212
- if (!docp ) {
2213
- RETURN_FALSE ;
2232
+ sxe_create_obj_from_doc (return_value , docp , ce , ns , isprefix );
2233
+ }
2234
+ /* }}} */
2235
+
2236
+ static int sxe_stream_read (void * context , char * buffer , int len )
2237
+ {
2238
+ zend_resource * resource = context ;
2239
+ if (EXPECTED (resource -> ptr )) {
2240
+ php_stream * stream = resource -> ptr ;
2241
+ return php_stream_read (stream , buffer , len );
2214
2242
}
2243
+ return -1 ;
2244
+ }
2215
2245
2216
- if (!ce ) {
2217
- ce = ce_SimpleXMLElement ;
2218
- fptr_count = NULL ;
2219
- } else {
2220
- fptr_count = php_sxe_find_fptr_count (ce );
2246
+ PHP_FUNCTION (simplexml_load_stream )
2247
+ {
2248
+ zval * stream_zv ;
2249
+ php_stream * stream ;
2250
+ xmlDocPtr docp ;
2251
+ zend_string * ns = zend_empty_string ;
2252
+ zend_long options = 0 ;
2253
+ zend_class_entry * ce = ce_SimpleXMLElement ;
2254
+ bool isprefix = 0 ;
2255
+ const char * encoding = NULL ;
2256
+ const char * document_uri = NULL ;
2257
+ size_t encoding_len , document_uri_len ;
2258
+
2259
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "r|p!p!C!lSb" ,
2260
+ & stream_zv , & encoding , & encoding_len , & document_uri , & document_uri_len , & ce , & options , & ns , & isprefix ) == FAILURE ) {
2261
+ RETURN_THROWS ();
2221
2262
}
2222
- sxe = php_sxe_object_new (ce , fptr_count );
2223
- sxe -> iter .nsprefix = ZSTR_LEN (ns ) ? zend_string_copy (ns ) : NULL ;
2224
- sxe -> iter .isprefix = isprefix ;
2225
- php_libxml_increment_doc_ref ((php_libxml_node_object * )sxe , docp );
2226
- php_libxml_increment_node_ptr ((php_libxml_node_object * )sxe , xmlDocGetRootElement (docp ), NULL );
2227
2263
2228
- RETURN_OBJ (& sxe -> zo );
2264
+ php_stream_from_res (stream , Z_RES_P (stream_zv ));
2265
+
2266
+ if (!php_libxml_is_valid_encoding (encoding )) {
2267
+ zend_argument_value_error (2 , "must be a valid character encoding" );
2268
+ RETURN_THROWS ();
2269
+ }
2270
+
2271
+ if (ZEND_LONG_EXCEEDS_INT (options )) {
2272
+ zend_argument_value_error (5 , "is too large" );
2273
+ RETURN_THROWS ();
2274
+ }
2275
+
2276
+ if (encoding ) {
2277
+ options |= XML_PARSE_IGNORE_ENC ;
2278
+ }
2279
+
2280
+ PHP_LIBXML_SANITIZE_GLOBALS (read_file );
2281
+ docp = xmlReadIO (sxe_stream_read , NULL , stream -> res , document_uri , encoding , (int ) options );
2282
+ PHP_LIBXML_RESTORE_GLOBALS (read_file );
2283
+
2284
+ sxe_create_obj_from_doc (return_value , docp , ce , ns , isprefix );
2229
2285
}
2230
- /* }}} */
2231
2286
2232
2287
/* {{{ Load a string and return a simplexml_element object to allow for processing */
2233
2288
PHP_FUNCTION (simplexml_load_string )
2234
2289
{
2235
- php_sxe_object * sxe ;
2236
2290
char * data ;
2237
2291
size_t data_len ;
2238
2292
xmlDocPtr docp ;
2239
2293
zend_string * ns = zend_empty_string ;
2240
2294
zend_long options = 0 ;
2241
2295
zend_class_entry * ce = ce_SimpleXMLElement ;
2242
- zend_function * fptr_count ;
2243
2296
bool isprefix = 0 ;
2244
2297
2245
2298
if (zend_parse_parameters (ZEND_NUM_ARGS (), "s|C!lSb" , & data , & data_len , & ce , & options , & ns , & isprefix ) == FAILURE ) {
@@ -2263,23 +2316,7 @@ PHP_FUNCTION(simplexml_load_string)
2263
2316
docp = xmlReadMemory (data , (int )data_len , NULL , NULL , (int )options );
2264
2317
PHP_LIBXML_RESTORE_GLOBALS (read_memory );
2265
2318
2266
- if (!docp ) {
2267
- RETURN_FALSE ;
2268
- }
2269
-
2270
- if (!ce ) {
2271
- ce = ce_SimpleXMLElement ;
2272
- fptr_count = NULL ;
2273
- } else {
2274
- fptr_count = php_sxe_find_fptr_count (ce );
2275
- }
2276
- sxe = php_sxe_object_new (ce , fptr_count );
2277
- sxe -> iter .nsprefix = ZSTR_LEN (ns ) ? zend_string_copy (ns ) : NULL ;
2278
- sxe -> iter .isprefix = isprefix ;
2279
- php_libxml_increment_doc_ref ((php_libxml_node_object * )sxe , docp );
2280
- php_libxml_increment_node_ptr ((php_libxml_node_object * )sxe , xmlDocGetRootElement (docp ), NULL );
2281
-
2282
- RETURN_OBJ (& sxe -> zo );
2319
+ sxe_create_obj_from_doc (return_value , docp , ce , ns , isprefix );
2283
2320
}
2284
2321
/* }}} */
2285
2322
0 commit comments