diff --git a/c-api/Cargo.toml b/c-api/Cargo.toml index 5eb40fed..304bba3c 100644 --- a/c-api/Cargo.toml +++ b/c-api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lolhtml" +name = "lol_html_c_api" version = "1.1.2" authors = ["Ivan Nikulin ", "Joshua Nelson "] edition = "2021" @@ -17,4 +17,5 @@ panic = "abort" lto = true [lib] +name = "lolhtml" crate-type = ["staticlib", "cdylib", "rlib"] diff --git a/c-api/c-tests/Cargo.toml b/c-api/c-tests/Cargo.toml index b055bc06..4fdb6ef1 100644 --- a/c-api/c-tests/Cargo.toml +++ b/c-api/c-tests/Cargo.toml @@ -9,7 +9,7 @@ publish = false [dependencies] lol_html = { path = "../../" } -lolhtml = { path = "../" } +lol_html_c_api = { path = "../" } libc = "0.2.139" [build-dependencies] diff --git a/c-api/src/comment.rs b/c-api/src/comment.rs index 81c4e056..9422e80b 100644 --- a/c-api/src/comment.rs +++ b/c-api/src/comment.rs @@ -1,12 +1,12 @@ use super::*; #[no_mangle] -pub extern "C" fn lol_html_comment_text_get(comment: *const Comment) -> Str { +pub unsafe extern "C" fn lol_html_comment_text_get(comment: *const Comment) -> Str { Str::new(to_ref!(comment).text()) } #[no_mangle] -pub extern "C" fn lol_html_comment_text_set( +pub unsafe extern "C" fn lol_html_comment_text_set( comment: *mut Comment, text: *const c_char, text_len: size_t, @@ -60,11 +60,14 @@ pub extern "C" fn lol_html_comment_is_removed(comment: *const Comment) -> bool { } #[no_mangle] -pub extern "C" fn lol_html_comment_user_data_set(comment: *mut Comment, user_data: *mut c_void) { +pub unsafe extern "C" fn lol_html_comment_user_data_set( + comment: *mut Comment, + user_data: *mut c_void, +) { to_ref_mut!(comment).set_user_data(user_data); } #[no_mangle] -pub extern "C" fn lol_html_comment_user_data_get(comment: *const Comment) -> *mut c_void { +pub unsafe extern "C" fn lol_html_comment_user_data_get(comment: *const Comment) -> *mut c_void { get_user_data!(comment) } diff --git a/c-api/src/doctype.rs b/c-api/src/doctype.rs index cfa30e4c..2c92e27f 100644 --- a/c-api/src/doctype.rs +++ b/c-api/src/doctype.rs @@ -1,27 +1,30 @@ use super::*; #[no_mangle] -pub extern "C" fn lol_html_doctype_name_get(doctype: *const Doctype) -> Str { +pub unsafe extern "C" fn lol_html_doctype_name_get(doctype: *const Doctype) -> Str { Str::from_opt(to_ref!(doctype).name()) } #[no_mangle] -pub extern "C" fn lol_html_doctype_public_id_get(doctype: *const Doctype) -> Str { +pub unsafe extern "C" fn lol_html_doctype_public_id_get(doctype: *const Doctype) -> Str { Str::from_opt(to_ref!(doctype).public_id()) } #[no_mangle] -pub extern "C" fn lol_html_doctype_system_id_get(doctype: *const Doctype) -> Str { +pub unsafe extern "C" fn lol_html_doctype_system_id_get(doctype: *const Doctype) -> Str { Str::from_opt(to_ref!(doctype).system_id()) } #[no_mangle] -pub extern "C" fn lol_html_doctype_user_data_set(doctype: *mut Doctype, user_data: *mut c_void) { +pub unsafe extern "C" fn lol_html_doctype_user_data_set( + doctype: *mut Doctype, + user_data: *mut c_void, +) { to_ref_mut!(doctype).set_user_data(user_data); } #[no_mangle] -pub extern "C" fn lol_html_doctype_user_data_get(doctype: *const Doctype) -> *mut c_void { +pub unsafe extern "C" fn lol_html_doctype_user_data_get(doctype: *const Doctype) -> *mut c_void { get_user_data!(doctype) } diff --git a/c-api/src/element.rs b/c-api/src/element.rs index 897b70a6..16f82c21 100644 --- a/c-api/src/element.rs +++ b/c-api/src/element.rs @@ -2,21 +2,23 @@ use super::*; use std::slice::Iter; #[no_mangle] -pub extern "C" fn lol_html_element_tag_name_get(element: *const Element) -> Str { +pub unsafe extern "C" fn lol_html_element_tag_name_get(element: *const Element) -> Str { let element = to_ref!(element); Str::new(element.tag_name()) } #[no_mangle] -pub extern "C" fn lol_html_element_tag_name_get_preserve_case(element: *const Element) -> Str { +pub unsafe extern "C" fn lol_html_element_tag_name_get_preserve_case( + element: *const Element, +) -> Str { let element = to_ref!(element); Str::new(element.tag_name_preserve_case()) } #[no_mangle] -pub extern "C" fn lol_html_element_tag_name_set( +pub unsafe extern "C" fn lol_html_element_tag_name_set( element: *mut Element, name: *const c_char, name_len: size_t, @@ -52,7 +54,7 @@ pub extern "C" fn lol_html_element_namespace_uri_get(element: *mut Element) -> * } #[no_mangle] -pub extern "C" fn lol_html_attributes_iterator_get<'r, 't>( +pub unsafe extern "C" fn lol_html_attributes_iterator_get<'r, 't>( element: *const Element<'r, 't>, ) -> *mut Iter<'r, Attribute<'t>> { let attributes = to_ref!(element).attributes(); @@ -61,7 +63,7 @@ pub extern "C" fn lol_html_attributes_iterator_get<'r, 't>( } #[no_mangle] -pub extern "C" fn lol_html_attributes_iterator_next<'t>( +pub unsafe extern "C" fn lol_html_attributes_iterator_next<'t>( iterator: *mut Iter<'_, Attribute<'t>>, ) -> *const Attribute<'t> { let iterator = to_ref_mut!(iterator); @@ -73,33 +75,35 @@ pub extern "C" fn lol_html_attributes_iterator_next<'t>( } #[no_mangle] -pub extern "C" fn lol_html_attributes_iterator_free(iterator: *mut Iter) { +pub unsafe extern "C" fn lol_html_attributes_iterator_free(iterator: *mut Iter) { drop(to_box!(iterator)); } #[no_mangle] -pub extern "C" fn lol_html_attribute_name_get(attribute: *const Attribute) -> Str { +pub unsafe extern "C" fn lol_html_attribute_name_get(attribute: *const Attribute) -> Str { let attribute = to_ref!(attribute); Str::new(attribute.name()) } #[no_mangle] -pub extern "C" fn lol_html_attribute_name_get_preserve_case(attribute: *const Attribute) -> Str { +pub unsafe extern "C" fn lol_html_attribute_name_get_preserve_case( + attribute: *const Attribute, +) -> Str { let attribute = to_ref!(attribute); Str::new(attribute.name_preserve_case()) } #[no_mangle] -pub extern "C" fn lol_html_attribute_value_get(attribute: *const Attribute) -> Str { +pub unsafe extern "C" fn lol_html_attribute_value_get(attribute: *const Attribute) -> Str { let attribute = to_ref!(attribute); Str::new(attribute.value()) } #[no_mangle] -pub extern "C" fn lol_html_element_get_attribute( +pub unsafe extern "C" fn lol_html_element_get_attribute( element: *const Element, name: *const c_char, name_len: size_t, @@ -111,7 +115,7 @@ pub extern "C" fn lol_html_element_get_attribute( } #[no_mangle] -pub extern "C" fn lol_html_element_has_attribute( +pub unsafe extern "C" fn lol_html_element_has_attribute( element: *const Element, name: *const c_char, name_len: size_t, @@ -127,7 +131,7 @@ pub extern "C" fn lol_html_element_has_attribute( } #[no_mangle] -pub extern "C" fn lol_html_element_set_attribute( +pub unsafe extern "C" fn lol_html_element_set_attribute( element: *mut Element, name: *const c_char, name_len: size_t, @@ -144,7 +148,7 @@ pub extern "C" fn lol_html_element_set_attribute( } #[no_mangle] -pub extern "C" fn lol_html_element_remove_attribute( +pub unsafe extern "C" fn lol_html_element_remove_attribute( element: *mut Element, name: *const c_char, name_len: size_t, @@ -233,19 +237,22 @@ pub extern "C" fn lol_html_element_is_removed(element: *mut Element) -> bool { } #[no_mangle] -pub extern "C" fn lol_html_element_user_data_set(element: *mut Element, user_data: *mut c_void) { +pub unsafe extern "C" fn lol_html_element_user_data_set( + element: *mut Element, + user_data: *mut c_void, +) { to_ref_mut!(element).set_user_data(user_data); } #[no_mangle] -pub extern "C" fn lol_html_element_user_data_get(element: *mut Element) -> *mut c_void { +pub unsafe extern "C" fn lol_html_element_user_data_get(element: *mut Element) -> *mut c_void { get_user_data!(element) } type EndTagHandler = unsafe extern "C" fn(*mut EndTag, *mut c_void) -> RewriterDirective; #[no_mangle] -pub extern "C" fn lol_html_element_add_end_tag_handler( +pub unsafe extern "C" fn lol_html_element_add_end_tag_handler( element: *mut Element, handler: EndTagHandler, user_data: *mut c_void, @@ -267,7 +274,7 @@ pub extern "C" fn lol_html_element_add_end_tag_handler( } #[no_mangle] -pub extern "C" fn lol_html_element_clear_end_tag_handlers(element: *mut Element) { +pub unsafe extern "C" fn lol_html_element_clear_end_tag_handlers(element: *mut Element) { let element = to_ref_mut!(element); if let Some(handlers) = element.end_tag_handlers() { handlers.clear(); @@ -300,19 +307,19 @@ pub extern "C" fn lol_html_end_tag_remove(end_tag: *mut EndTag) { } #[no_mangle] -pub extern "C" fn lol_html_end_tag_name_get(end_tag: *mut EndTag) -> Str { +pub unsafe extern "C" fn lol_html_end_tag_name_get(end_tag: *mut EndTag) -> Str { let tag = to_ref_mut!(end_tag); Str::new(tag.name()) } #[no_mangle] -pub extern "C" fn lol_html_end_tag_name_get_preserve_case(end_tag: *mut EndTag) -> Str { +pub unsafe extern "C" fn lol_html_end_tag_name_get_preserve_case(end_tag: *mut EndTag) -> Str { let tag = to_ref_mut!(end_tag); Str::new(tag.name_preserve_case()) } #[no_mangle] -pub extern "C" fn lol_html_end_tag_name_set( +pub unsafe extern "C" fn lol_html_end_tag_name_set( end_tag: *mut EndTag, name: *const c_char, len: size_t, diff --git a/c-api/src/rewriter.rs b/c-api/src/rewriter.rs index 23188507..18c174d1 100644 --- a/c-api/src/rewriter.rs +++ b/c-api/src/rewriter.rs @@ -35,7 +35,7 @@ impl OutputSink for ExternOutputSink { } #[no_mangle] -pub extern "C" fn lol_html_rewriter_build( +pub unsafe extern "C" fn lol_html_rewriter_build( builder: *mut HtmlRewriterBuilder, encoding: *const c_char, encoding_len: size_t, @@ -67,7 +67,7 @@ pub extern "C" fn lol_html_rewriter_build( } #[no_mangle] -pub extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags( +pub unsafe extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags( builder: *mut HtmlRewriterBuilder, encoding: *const c_char, encoding_len: size_t, @@ -99,7 +99,7 @@ pub extern "C" fn unstable_lol_html_rewriter_build_with_esi_tags( } #[no_mangle] -pub extern "C" fn lol_html_rewriter_write( +pub unsafe extern "C" fn lol_html_rewriter_write( rewriter: *mut HtmlRewriter, chunk: *const c_char, chunk_len: size_t, @@ -116,7 +116,7 @@ pub extern "C" fn lol_html_rewriter_write( } #[no_mangle] -pub extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int { +pub unsafe extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int { let rewriter = to_ref_mut!(rewriter) .0 .take() // Using `take()` allows calling `free()` afterwards (it will be a no-op). @@ -128,7 +128,7 @@ pub extern "C" fn lol_html_rewriter_end(rewriter: *mut HtmlRewriter) -> c_int { } #[no_mangle] -pub extern "C" fn lol_html_rewriter_free(rewriter: *mut HtmlRewriter) { +pub unsafe extern "C" fn lol_html_rewriter_free(rewriter: *mut HtmlRewriter) { // SAFETY: `to_box` includes a check that `rewriter` is non-null. // The caller is required to ensure that `rewriter` is aligned and that `free` has not been called before. // NOTE: if `end()` was called before, it is valid (but not recommended) to call `free()` more than once. diff --git a/c-api/src/rewriter_builder.rs b/c-api/src/rewriter_builder.rs index 07718da5..e219087c 100644 --- a/c-api/src/rewriter_builder.rs +++ b/c-api/src/rewriter_builder.rs @@ -117,12 +117,12 @@ impl HtmlRewriterBuilder { } #[no_mangle] -pub extern "C" fn lol_html_rewriter_builder_new() -> *mut HtmlRewriterBuilder { +pub unsafe extern "C" fn lol_html_rewriter_builder_new() -> *mut HtmlRewriterBuilder { to_ptr_mut(HtmlRewriterBuilder::default()) } #[no_mangle] -pub extern "C" fn lol_html_rewriter_builder_add_document_content_handlers( +pub unsafe extern "C" fn lol_html_rewriter_builder_add_document_content_handlers( builder: *mut HtmlRewriterBuilder, doctype_handler: Option, doctype_handler_user_data: *mut c_void, @@ -146,7 +146,7 @@ pub extern "C" fn lol_html_rewriter_builder_add_document_content_handlers( } #[no_mangle] -pub extern "C" fn lol_html_rewriter_builder_add_element_content_handlers( +pub unsafe extern "C" fn lol_html_rewriter_builder_add_element_content_handlers( builder: *mut HtmlRewriterBuilder, selector: *const Selector, element_handler: Option, @@ -171,6 +171,6 @@ pub extern "C" fn lol_html_rewriter_builder_add_element_content_handlers( } #[no_mangle] -pub extern "C" fn lol_html_rewriter_builder_free(builder: *mut HtmlRewriterBuilder) { +pub unsafe extern "C" fn lol_html_rewriter_builder_free(builder: *mut HtmlRewriterBuilder) { drop(to_box!(builder)); } diff --git a/c-api/src/selector.rs b/c-api/src/selector.rs index b41b5582..9e39963f 100644 --- a/c-api/src/selector.rs +++ b/c-api/src/selector.rs @@ -1,7 +1,7 @@ use super::*; #[no_mangle] -pub extern "C" fn lol_html_selector_parse( +pub unsafe extern "C" fn lol_html_selector_parse( selector: *const c_char, selector_len: size_t, ) -> *mut Selector { @@ -12,6 +12,6 @@ pub extern "C" fn lol_html_selector_parse( } #[no_mangle] -pub extern "C" fn lol_html_selector_free(selector: *mut Selector) { +pub unsafe extern "C" fn lol_html_selector_free(selector: *mut Selector) { drop(to_box!(selector)); } diff --git a/c-api/src/string.rs b/c-api/src/string.rs index 3010fd4c..fc707056 100644 --- a/c-api/src/string.rs +++ b/c-api/src/string.rs @@ -17,10 +17,10 @@ impl Str { } } - #[inline] /// Convert an `Option` to a C-style string. /// /// If `string` is `None`, `data` will be set to `NULL`. + #[inline] #[must_use] pub fn from_opt(string: Option) -> Self { match string { @@ -45,6 +45,6 @@ impl Drop for Str { } #[no_mangle] -pub extern "C" fn lol_html_str_free(string: Str) { +pub unsafe extern "C" fn lol_html_str_free(string: Str) { drop(string); } diff --git a/c-api/src/text_chunk.rs b/c-api/src/text_chunk.rs index 76074b3a..43c5eecd 100644 --- a/c-api/src/text_chunk.rs +++ b/c-api/src/text_chunk.rs @@ -18,7 +18,9 @@ impl TextChunkContent { } #[no_mangle] -pub extern "C" fn lol_html_text_chunk_content_get(chunk: *mut TextChunk) -> TextChunkContent { +pub unsafe extern "C" fn lol_html_text_chunk_content_get( + chunk: *mut TextChunk, +) -> TextChunkContent { TextChunkContent::new(to_ref!(chunk)) } @@ -68,11 +70,14 @@ pub extern "C" fn lol_html_text_chunk_is_removed(chunk: *const TextChunk) -> boo } #[no_mangle] -pub extern "C" fn lol_html_text_chunk_user_data_set(chunk: *mut TextChunk, user_data: *mut c_void) { +pub unsafe extern "C" fn lol_html_text_chunk_user_data_set( + chunk: *mut TextChunk, + user_data: *mut c_void, +) { to_ref_mut!(chunk).set_user_data(user_data); } #[no_mangle] -pub extern "C" fn lol_html_text_chunk_user_data_get(chunk: *const TextChunk) -> *mut c_void { +pub unsafe extern "C" fn lol_html_text_chunk_user_data_get(chunk: *const TextChunk) -> *mut c_void { get_user_data!(chunk) } diff --git a/fuzz/test_case/Cargo.toml b/fuzz/test_case/Cargo.toml index 0845db67..45244769 100644 --- a/fuzz/test_case/Cargo.toml +++ b/fuzz/test_case/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] lol_html = { path = "../../" } -lolhtml = { path = "../../c-api" } +lol_html_c_api = { path = "../../c-api" } encoding_rs = "0.8.13" libc = "0.2.61" rand = "0.8.4" diff --git a/js-api/Cargo.toml b/js-api/Cargo.toml index 1740bb91..3e18663a 100644 --- a/js-api/Cargo.toml +++ b/js-api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lol-html-js-api" +name = "lol_html_js_api" version = "1.1.1" authors = ["Ivan Nikulin "] edition = "2021"