| 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 |
er |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
s. |
| 40 |
|
| 41 |
use std::ffi::{CStr, c_char, c_int, c_void}; |
| 42 |
use std::sync::{Arc, Mutex, OnceLock}; |
| 43 |
|
| 44 |
use crate::bindings::xmlRegisterInputCallbacks; |
| 45 |
|
| 46 |
type MatchFn = Box<dyn Fn(&str) -> bool + Send + Sync + 'static>; |
| 47 |
type OpenFn = Box<dyn Fn(&str) -> Option<Vec<u8>> + Send + Sync + 'static>; |
| 48 |
|
| 49 |
struct Callback { |
| 50 |
match_url: MatchFn, |
| 51 |
open: OpenFn, |
| 52 |
} |
| 53 |
|
| 54 |
fn callbacks() -> &'static Mutex<Vec<Arc<Callback>>> { |
| 55 |
static CALLBACKS: OnceLock<Mutex<Vec<Arc<Callback>>>> = OnceLock::new(); |
| 56 |
CALLBACKS.get_or_init(|| Mutex::new(Vec::new())) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
fn snapshot() -> Vec<Arc<Callback>> { |
| 64 |
callbacks().lock().unwrap().clone() |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
or |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub fn register_input_callback<M, O>(match_url: M, open: O) |
| 99 |
where |
| 100 |
M: Fn(&str) -> bool + Send + Sync + 'static, |
| 101 |
O: Fn(&str) -> Option<Vec<u8>> + Send + Sync + 'static, |
| 102 |
{ |
| 103 |
callbacks().lock().unwrap().push(Arc::new(Callback { |
| 104 |
match_url: Box::new(match_url), |
| 105 |
open: Box::new(open), |
| 106 |
})); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static REGISTERED: OnceLock<()> = OnceLock::new(); |
| 112 |
REGISTERED.get_or_init(|| { |
| 113 |
crate::init_parser(); |
| 114 |
unsafe { |
| 115 |
xmlRegisterInputCallbacks( |
| 116 |
Some(trampoline_match), |
| 117 |
Some(trampoline_open), |
| 118 |
Some(trampoline_read), |
| 119 |
Some(trampoline_close), |
| 120 |
); |
| 121 |
} |
| 122 |
}); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
struct OpenState { |
| 128 |
bytes: Vec<u8>, |
| 129 |
position: usize, |
| 130 |
} |
| 131 |
|
| 132 |
unsafe extern "C" fn trampoline_match(filename: *const c_char) -> c_int { |
| 133 |
if filename.is_null() { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
let url = match unsafe { CStr::from_ptr(filename) }.to_str() { |
| 139 |
Ok(s) => s, |
| 140 |
Err(_) => return 0, |
| 141 |
}; |
| 142 |
|
| 143 |
for cb in snapshot().iter().rev() { |
| 144 |
if (cb.match_url)(url) { |
| 145 |
return 1; |
| 146 |
} |
| 147 |
} |
| 148 |
0 |
| 149 |
} |
| 150 |
|
| 151 |
unsafe extern "C" fn trampoline_open(filename: *const c_char) -> *mut c_void { |
| 152 |
if filename.is_null() { |
| 153 |
return std::ptr::null_mut(); |
| 154 |
} |
| 155 |
|
| 156 |
let url = match unsafe { CStr::from_ptr(filename) }.to_str() { |
| 157 |
Ok(s) => s, |
| 158 |
Err(_) => return std::ptr::null_mut(), |
| 159 |
}; |
| 160 |
s. |
| 161 |
for cb in snapshot().iter().rev() { |
| 162 |
if !(cb.match_url)(url) { |
| 163 |
continue; |
| 164 |
} |
| 165 |
if let Some(bytes) = (cb.open)(url) { |
| 166 |
return Box::into_raw(Box::new(OpenState { bytes, position: 0 })) as *mut c_void; |
| 167 |
} |
| 168 |
} |
| 169 |
std::ptr::null_mut() |
| 170 |
} |
| 171 |
|
| 172 |
unsafe extern "C" fn trampoline_read( |
| 173 |
context: *mut c_void, |
| 174 |
buffer: *mut c_char, |
| 175 |
len: c_int, |
| 176 |
) -> c_int { |
| 177 |
if context.is_null() || buffer.is_null() || len <= 0 { |
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
let state = unsafe { &mut *(context as *mut OpenState) }; |
| 183 |
let remaining = state.bytes.len().saturating_sub(state.position); |
| 184 |
let n = remaining.min(len as usize); |
| 185 |
if n == 0 { |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
unsafe { |
| 190 |
std::ptr::copy_nonoverlapping( |
| 191 |
state.bytes.as_ptr().add(state.position), |
| 192 |
buffer as *mut u8, |
| 193 |
n, |
| 194 |
); |
| 195 |
} |
| 196 |
state.position += n; |
| 197 |
n as c_int |
| 198 |
} |
| 199 |
|
| 200 |
unsafe extern "C" fn trampoline_close(context: *mut c_void) -> c_int { |
| 201 |
if context.is_null() { |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
let _state = unsafe { Box::from_raw(context as *mut OpenState) }; |
| 206 |
0 |
| 207 |
} |
| 208 |
|
| 209 |
#[cfg(test)] |
| 210 |
mod tests { |
| 211 |
use super::*; |
| 212 |
use crate::bindings::{xmlFreeDoc, xmlReadFile}; |
| 213 |
use std::ffi::CString; |
| 214 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 215 |
|
| 216 |
static SAMPLE_XML: &[u8] = br#"<?xml version="1.0"?> |
| 217 |
<root attr="ok"><child/></root>"#; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
`. |
| 222 |
fn read_file_via_libxml2(url: &str) -> bool { |
| 223 |
let c = CString::new(url).unwrap(); |
| 224 |
unsafe { |
| 225 |
let doc = xmlReadFile(c.as_ptr(), std::ptr::null(), 0); |
| 226 |
if doc.is_null() { |
| 227 |
return false; |
| 228 |
} |
| 229 |
xmlFreeDoc(doc); |
| 230 |
true |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#[test] |
| 239 |
fn input_callback_scenarios() { |
| 240 |
register_input_callback( |
| 241 |
|url| url.starts_with("embed:///"), |
| 242 |
|url| (url == "embed:///sample.xml").then(|| SAMPLE_XML.to_vec()), |
| 243 |
); |
| 244 |
|
| 245 |
|
| 246 |
assert!(read_file_via_libxml2("embed:///sample.xml")); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
assert!(!read_file_via_libxml2("embed:///unknown.xml")); |
| 251 |
|
| 252 |
he |
| 253 |
|
| 254 |
assert!(!read_file_via_libxml2("/nonexistent/definitely/missing.xml")); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
register_input_callback( |
| 259 |
|url| url == "reentrant:///outer", |
| 260 |
|_url| { |
| 261 |
let _ = read_file_via_libxml2("embed:///sample.xml"); |
| 262 |
Some(SAMPLE_XML.to_vec()) |
| 263 |
}, |
| 264 |
); |
| 265 |
assert!(read_file_via_libxml2("reentrant:///outer")); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
static FIRST_OPENED: AtomicUsize = AtomicUsize::new(0); |
| 270 |
static SECOND_OPENED: AtomicUsize = AtomicUsize::new(0); |
| 271 |
register_input_callback( |
| 272 |
|url| url == "ordered:///x", |
| 273 |
|_| { |
| 274 |
FIRST_OPENED.fetch_add(1, Ordering::SeqCst); |
| 275 |
Some(b"<a>first</a>".to_vec()) |
| 276 |
}, |
| 277 |
); |
| 278 |
register_input_callback( |
| 279 |
|url| url == "ordered:///x", |
| 280 |
|_| { |
| 281 |
SECOND_OPENED.fetch_add(1, Ordering::SeqCst); |
| 282 |
Some(SAMPLE_XML.to_vec()) |
| 283 |
}, |
| 284 |
); |
| 285 |
assert!(read_file_via_libxml2("ordered:///x")); |
| 286 |
assert_eq!( |
| 287 |
SECOND_OPENED.load(Ordering::SeqCst), |
| 288 |
1, |
| 289 |
"newest registration should run", |
| 290 |
); |
| 291 |
assert_eq!( |
| 292 |
FIRST_OPENED.load(Ordering::SeqCst), |
| 293 |
0, |
| 294 |
"older registration should not be consulted", |
| 295 |
); |
| 296 |
} |
| 297 |
} |