|
| 1 | +//! Document canonicalization logic |
| 2 | +//! |
| 3 | +use std::ffi::{c_int, c_void, CString}; |
| 4 | +use std::os::raw; |
| 5 | +use std::ptr::null_mut; |
| 6 | + |
| 7 | +use crate::tree::c14n::*; |
| 8 | + |
| 9 | +use super::{ |
| 10 | + xmlAllocOutputBuffer, xmlC14NExecute, xmlC14NIsVisibleCallback, xmlChar, xmlNodePtr, |
| 11 | + xmlOutputBufferClose, xmlOutputBufferPtr, Document, |
| 12 | +}; |
| 13 | + |
| 14 | +impl Document { |
| 15 | + /// Canonicalize a document and return the results. |
| 16 | + pub fn canonicalize( |
| 17 | + &self, |
| 18 | + options: CanonicalizationOptions, |
| 19 | + callback: Option<(xmlNodePtr, xmlC14NIsVisibleCallback)>, |
| 20 | + ) -> Result<String, ()> { |
| 21 | + let document = (*self.0).borrow().doc_ptr; |
| 22 | + |
| 23 | + let mut ns_list_c = to_xml_string_vec(options.inclusive_ns_prefixes); |
| 24 | + let inclusive_ns_prefixes = ns_list_c.as_mut_ptr(); |
| 25 | + let with_comments = c_int::from(options.with_comments); |
| 26 | + |
| 27 | + let (is_visible_callback, user_data) = if let Some((node_ptr, visibility_callback)) = callback { |
| 28 | + (visibility_callback, node_ptr as *mut _) |
| 29 | + } else { |
| 30 | + (None, null_mut()) |
| 31 | + }; |
| 32 | + |
| 33 | + let mode = options.mode.into(); |
| 34 | + unsafe { |
| 35 | + let c_obuf = create_output_buffer(); |
| 36 | + |
| 37 | + let status = xmlC14NExecute( |
| 38 | + document, |
| 39 | + is_visible_callback, |
| 40 | + user_data, |
| 41 | + mode, |
| 42 | + inclusive_ns_prefixes, |
| 43 | + with_comments, |
| 44 | + c_obuf, |
| 45 | + ); |
| 46 | + |
| 47 | + let res = c_obuf_into_output(c_obuf); |
| 48 | + |
| 49 | + if status < 0 { |
| 50 | + Err(()) |
| 51 | + } else { |
| 52 | + Ok(res) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +unsafe fn c_obuf_into_output(c_obuf: xmlOutputBufferPtr) -> String { |
| 59 | + let ctx_ptr = (*c_obuf).context; |
| 60 | + let output = Box::from_raw(ctx_ptr as *mut String); |
| 61 | + |
| 62 | + (*c_obuf).context = std::ptr::null_mut::<c_void>(); |
| 63 | + |
| 64 | + xmlOutputBufferClose(c_obuf); |
| 65 | + |
| 66 | + *output |
| 67 | +} |
| 68 | + |
| 69 | +unsafe fn create_output_buffer() -> xmlOutputBufferPtr { |
| 70 | + let output = String::new(); |
| 71 | + let ctx_ptr = Box::into_raw(Box::new(output)); |
| 72 | + let encoder = std::ptr::null_mut(); |
| 73 | + |
| 74 | + let buf = xmlAllocOutputBuffer(encoder); |
| 75 | + |
| 76 | + (*buf).writecallback = Some(xml_write_io); |
| 77 | + (*buf).closecallback = Some(xml_close_io); |
| 78 | + (*buf).context = ctx_ptr as _; |
| 79 | + |
| 80 | + buf |
| 81 | +} |
| 82 | + |
| 83 | +unsafe extern "C" fn xml_close_io(_context: *mut raw::c_void) -> raw::c_int { |
| 84 | + 0 |
| 85 | +} |
| 86 | + |
| 87 | +unsafe extern "C" fn xml_write_io( |
| 88 | + io_ptr: *mut raw::c_void, |
| 89 | + buffer: *const raw::c_char, |
| 90 | + len: raw::c_int, |
| 91 | +) -> raw::c_int { |
| 92 | + if io_ptr.is_null() { |
| 93 | + 0 |
| 94 | + } else { |
| 95 | + let buf = std::slice::from_raw_parts_mut(buffer as *mut u8, len as usize); |
| 96 | + let buf = String::from_utf8_lossy(buf); |
| 97 | + let s2_ptr = io_ptr as *mut String; |
| 98 | + String::push_str(&mut *s2_ptr, &buf); |
| 99 | + |
| 100 | + len |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// Create a [Vec] of null-terminated [*mut xmlChar] strings |
| 105 | +fn to_xml_string_vec(vec: Vec<String>) -> Vec<*mut xmlChar> { |
| 106 | + vec |
| 107 | + .into_iter() |
| 108 | + .map(|s| CString::new(s).unwrap().into_raw() as *mut xmlChar) |
| 109 | + .chain(std::iter::once(std::ptr::null_mut())) |
| 110 | + .collect() |
| 111 | +} |
0 commit comments