Skip to content

Commit 9ff4bd4

Browse files
committed
Merge branch 'streams-api' into process-globals
2 parents 025f603 + b5adbb9 commit 9ff4bd4

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

allowed_bindings.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,13 @@ bind! {
284284
zend_is_auto_global,
285285
zend_llist_get_next_ex,
286286
zend_llist_get_prev_ex,
287-
sapi_globals_offset,
287+
php_register_url_stream_wrapper,
288+
php_stream_locate_url_wrapper,
289+
php_unregister_url_stream_wrapper,
290+
php_unregister_url_stream_wrapper_volatile,
291+
php_register_url_stream_wrapper_volatile,
292+
php_stream_wrapper,
293+
php_stream_stdio_ops,
288294
zend_atomic_bool_store,
289295
zend_interrupt_function,
290296
zend_eval_string,

src/zend/ex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ffi::{zend_execute_data, ZEND_MM_ALIGNMENT, ZEND_MM_ALIGNMENT_MASK};
1+
use crate::ffi::{_zend_function, zend_execute_data, ZEND_MM_ALIGNMENT, ZEND_MM_ALIGNMENT_MASK};
22

33
use crate::{
44
args::ArgParser,

src/zend/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod ini_entry_def;
1111
mod linked_list;
1212
mod module;
1313
mod try_catch;
14+
mod streams;
1415

1516
use crate::{
1617
error::Result,
@@ -35,6 +36,7 @@ pub use module::ModuleEntry;
3536
#[cfg(feature = "embed")]
3637
pub(crate) use try_catch::panic_wrapper;
3738
pub use try_catch::{bailout, try_catch};
39+
pub use streams::*;
3840

3941
// Used as the format string for `php_printf`.
4042
const FORMAT_STR: &[u8] = b"%s\0";

src/zend/streams.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use std::ptr::{self, NonNull};
2+
3+
use crate::{
4+
ffi::{
5+
php_register_url_stream_wrapper, php_register_url_stream_wrapper_volatile, php_stream,
6+
php_stream_context, php_stream_locate_url_wrapper, php_stream_wrapper,
7+
php_stream_wrapper_ops, php_unregister_url_stream_wrapper,
8+
php_unregister_url_stream_wrapper_volatile, zend_string,
9+
},
10+
types::ZendStr,
11+
};
12+
13+
pub type StreamWrapper = php_stream_wrapper;
14+
15+
pub type StreamOpener = unsafe extern "C" fn(
16+
*mut StreamWrapper,
17+
*const std::ffi::c_char,
18+
*const std::ffi::c_char,
19+
i32,
20+
*mut *mut zend_string,
21+
*mut php_stream_context,
22+
i32,
23+
*const std::ffi::c_char,
24+
u32,
25+
*const std::ffi::c_char,
26+
u32,
27+
) -> *mut Stream;
28+
29+
impl StreamWrapper {
30+
pub fn get(name: &str) -> Option<&Self> {
31+
unsafe {
32+
let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
33+
Some(NonNull::new(result)?.as_ref())
34+
}
35+
}
36+
37+
pub fn get_mut(name: &str) -> Option<&mut Self> {
38+
unsafe {
39+
let result = php_stream_locate_url_wrapper(name.as_ptr().cast(), ptr::null_mut(), 0);
40+
Some(NonNull::new(result)?.as_mut())
41+
}
42+
}
43+
44+
pub fn register(self, name: &str) -> Result<Self, ()> {
45+
// We have to convert it to a static so owned streamwrapper doesn't get dropped.
46+
let copy = Box::new(self);
47+
let copy = Box::leak(copy);
48+
let name = std::ffi::CString::new(name).unwrap();
49+
let result = unsafe { php_register_url_stream_wrapper(name.as_ptr(), copy) };
50+
if result == 0 {
51+
Ok(*copy)
52+
} else {
53+
Err(())
54+
}
55+
}
56+
57+
pub fn register_volatile(self, name: &str) -> Result<Self, ()> {
58+
// We have to convert it to a static so owned streamwrapper doesn't get dropped.
59+
let copy = Box::new(self);
60+
let copy = Box::leak(copy);
61+
let name = ZendStr::new(name, false);
62+
let result =
63+
unsafe { php_register_url_stream_wrapper_volatile((*name).as_ptr() as _, copy) };
64+
if result == 0 {
65+
Ok(*copy)
66+
} else {
67+
Err(())
68+
}
69+
}
70+
71+
pub fn unregister(name: &str) -> Result<(), ()> {
72+
let name = std::ffi::CString::new(name).unwrap();
73+
match unsafe { php_unregister_url_stream_wrapper(name.as_ptr()) } {
74+
0 => Ok(()),
75+
_ => Err(()),
76+
}
77+
}
78+
79+
pub fn unregister_volatile(name: &str) -> Result<(), ()> {
80+
let name = ZendStr::new(name, false);
81+
match unsafe { php_unregister_url_stream_wrapper_volatile((*name).as_ptr() as _) } {
82+
0 => Ok(()),
83+
_ => Err(()),
84+
}
85+
}
86+
87+
pub fn wops(&self) -> &php_stream_wrapper_ops {
88+
unsafe { &*self.wops }
89+
}
90+
91+
pub fn wops_mut(&mut self) -> &mut php_stream_wrapper_ops {
92+
unsafe { &mut *(self.wops as *mut php_stream_wrapper_ops) }
93+
}
94+
}
95+
96+
pub type Stream = php_stream;
97+
98+
pub type StreamWrapperOps = php_stream_wrapper_ops;
99+
100+
impl StreamWrapperOps {}

0 commit comments

Comments
 (0)