From 430d97ddc6e68e62cc0f2e77319aaa92876f51eb Mon Sep 17 00:00:00 2001 From: defanor Date: Sat, 7 Oct 2023 20:29:00 +0300 Subject: Use file descriptors instead of streams, update Rust sources File descriptors tend to be easier to handle with FFI. --- src/rexmpp.rs | 4 ---- src/rexmpp_random.rs | 5 +++++ src/rexmpp_rust.rs | 1 + src/rexmpp_xml.c | 16 +++++++--------- src/rexmpp_xml.h | 4 ++-- src/rexmpp_xml.rs | 5 +++-- 6 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 src/rexmpp_random.rs (limited to 'src') diff --git a/src/rexmpp.rs b/src/rexmpp.rs index 1e1082a..5531ca2 100644 --- a/src/rexmpp.rs +++ b/src/rexmpp.rs @@ -89,10 +89,6 @@ pub enum TLSPolicy { Avoid } -extern { - pub fn rexmpp_gen_id (s: *mut Rexmpp) -> *mut c_char; -} - type IQCallback = unsafe extern "C" fn (s: *mut Rexmpp, cb_data: *mut c_void, request: *mut rexmpp_xml::RexmppXML, response: *mut rexmpp_xml::RexmppXML, diff --git a/src/rexmpp_random.rs b/src/rexmpp_random.rs new file mode 100644 index 0000000..dbabc71 --- /dev/null +++ b/src/rexmpp_random.rs @@ -0,0 +1,5 @@ +use std::os::raw::{c_char}; + +extern { + pub fn rexmpp_random_id () -> *mut c_char; +} diff --git a/src/rexmpp_rust.rs b/src/rexmpp_rust.rs index 04bbcca..ba80598 100644 --- a/src/rexmpp_rust.rs +++ b/src/rexmpp_rust.rs @@ -4,4 +4,5 @@ mod rexmpp_xml_parser; mod rexmpp_dns; mod rexmpp_tcp; mod rexmpp_socks; +mod rexmpp_random; mod rexmpp; diff --git a/src/rexmpp_xml.c b/src/rexmpp_xml.c index 96b283d..442a49e 100644 --- a/src/rexmpp_xml.c +++ b/src/rexmpp_xml.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "rexmpp.h" #include "rexmpp_utf8.h" #include "rexmpp_xml.h" @@ -545,7 +546,7 @@ rexmpp_xml_t *rexmpp_xml_parse (const char *str, int str_len) { return builder.root; } -rexmpp_xml_t *rexmpp_xml_read_fd (FILE *fd) { +rexmpp_xml_t *rexmpp_xml_read_fd (int fd) { struct rexmpp_xml_builder builder = { NULL, NULL }; rexmpp_xml_parser_ctx_t parser = rexmpp_xml_parser_new(&builder_sax, &builder); @@ -553,16 +554,13 @@ rexmpp_xml_t *rexmpp_xml_read_fd (FILE *fd) { return NULL; } - char *buf; - size_t init_len = 0; + char buf[4096]; ssize_t buf_len = 0; do { - buf = NULL; - buf_len = getline(&buf, &init_len, fd); + buf_len = read(fd, buf, 4096); if (buf_len > 0) { rexmpp_xml_parser_feed(parser, buf, buf_len, 0); } - free(buf); } while (buf_len > 0 && ! (builder.root != NULL && builder.current == NULL) ); @@ -578,12 +576,12 @@ rexmpp_xml_t *rexmpp_xml_read_fd (FILE *fd) { } rexmpp_xml_t *rexmpp_xml_read_file (const char *path) { - FILE *fd = fopen(path, "r"); - if (fd == NULL) { + int fd = open(path, O_RDONLY); + if (fd < 0) { return NULL; } rexmpp_xml_t *node = rexmpp_xml_read_fd(fd); - fclose(fd); + close(fd); return node; } diff --git a/src/rexmpp_xml.h b/src/rexmpp_xml.h index 8f6d974..38142ae 100644 --- a/src/rexmpp_xml.h +++ b/src/rexmpp_xml.h @@ -233,10 +233,10 @@ rexmpp_xml_t *rexmpp_xml_parse (const char *str, int str_len); /** @brief Reads XML from a file stream, reading the stream line by line. - @param[in] fd A file stream + @param[in] fd A file descriptor @returns Parsed XML, or NULL on failure. */ -rexmpp_xml_t *rexmpp_xml_read_fd (FILE *fd); +rexmpp_xml_t *rexmpp_xml_read_fd (int fd); /** @brief Reads XML from a file diff --git a/src/rexmpp_xml.rs b/src/rexmpp_xml.rs index 8938e57..90e312b 100644 --- a/src/rexmpp_xml.rs +++ b/src/rexmpp_xml.rs @@ -8,6 +8,7 @@ use std::fs::File; use std::io::Write; use super::{rexmpp}; +use super::{rexmpp_random}; // extern { // fn rexmpp_xml_parse (str: *mut c_char, str_len: c_int) -> *mut RexmppXML; @@ -585,13 +586,13 @@ fn rexmpp_xml_serialize (node_ptr: *const RexmppXML, #[no_mangle] pub extern "C" -fn rexmpp_xml_add_id (s: *mut rexmpp::Rexmpp, node: *mut RexmppXML) +fn rexmpp_xml_add_id (node: *mut RexmppXML) -> *mut RexmppXML { match CString::new("id") { Err(_) => return ptr::null_mut(), Ok(id_cstr) => { - let buf = unsafe { rexmpp::rexmpp_gen_id(s) }; + let buf = unsafe { rexmpp_random::rexmpp_random_id() }; if buf == ptr::null_mut() { return ptr::null_mut(); } -- cgit v1.2.3