mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 05:56:31 +03:00
* refactor: safe sql access * Clean up the worst rebase mistakes * Some more progress on the rebase fallout and this branch * upgrade and compile again * cleanup from rebase * example of how to prepare now * rebase fixes * add sql.query_map * less preparation * more improvements in sql code * fix string truncation * more prepare conversions * most prep done * fix tests * fix ffi * fix last prepares * fix segfaults and some queries * use r2d2 pool * fix dc_job sql call, to reduce contention * try newer rust * No more vararg printing (drop dc_log_) * ignore expected errors * fix: uses exists instead of execute where needed * fix: get_contacts logic was broken * fix: contact creation * test on 32bit linux * ci: try running 32bit without cross * undo 32bit tests * refactor: rename dc_sqlite3 to sql * fix: safer string conversions * more string fixes * try fixing appveyor build to 64bit * chore(ci): hardcode target * chore(ci): appveyor * some cleanup work * try fix darwin * fix and improve sql escaping * fix various bugs * fix chat deletion * refactor: cleanup config values and move to their own file * refactor: move more methods onto the sql struct * dont panic on failed state loading * first round of cr * one more cr fix * stop using strange defaults * remove unused escapes
106 lines
2.6 KiB
Rust
106 lines
2.6 KiB
Rust
use crate::types::*;
|
|
|
|
pub use libc::{
|
|
calloc, exit, free, malloc, memcmp, memcpy, memmove, memset, realloc, strcat, strchr, strcmp,
|
|
strcpy, strcspn, strlen, strncmp, strncpy, strrchr, strspn, strstr, strtol, system,
|
|
};
|
|
|
|
pub unsafe fn strdup(s: *const libc::c_char) -> *mut libc::c_char {
|
|
if s.is_null() {
|
|
return std::ptr::null_mut();
|
|
}
|
|
|
|
let slen = strlen(s);
|
|
let result = malloc(slen + 1);
|
|
if result.is_null() {
|
|
return std::ptr::null_mut();
|
|
}
|
|
|
|
memcpy(result, s as *const _, slen + 1);
|
|
result as *mut _
|
|
}
|
|
|
|
pub fn strndup(s: *const libc::c_char, n: libc::c_ulong) -> *mut libc::c_char {
|
|
if s.is_null() {
|
|
return std::ptr::null_mut();
|
|
}
|
|
|
|
let end = std::cmp::min(n as usize, unsafe { strlen(s) });
|
|
unsafe {
|
|
let result = malloc(end + 1);
|
|
memcpy(result, s as *const _, end);
|
|
std::ptr::write_bytes(result.offset(end as isize), b'\x00', 1);
|
|
|
|
result as *mut _
|
|
}
|
|
}
|
|
|
|
extern "C" {
|
|
pub fn clock() -> libc::clock_t;
|
|
pub fn qsort(
|
|
__base: *mut libc::c_void,
|
|
__nel: size_t,
|
|
__width: size_t,
|
|
__compar: Option<
|
|
unsafe extern "C" fn(_: *const libc::c_void, _: *const libc::c_void) -> libc::c_int,
|
|
>,
|
|
);
|
|
|
|
// -- DC Methods
|
|
pub fn dc_mprintf(format: *const libc::c_char, _: ...) -> *mut libc::c_char;
|
|
}
|
|
|
|
pub(crate) unsafe fn strcasecmp(s1: *const libc::c_char, s2: *const libc::c_char) -> libc::c_int {
|
|
let s1 = std::ffi::CStr::from_ptr(s1)
|
|
.to_string_lossy()
|
|
.to_lowercase();
|
|
let s2 = std::ffi::CStr::from_ptr(s2)
|
|
.to_string_lossy()
|
|
.to_lowercase();
|
|
if s1 == s2 {
|
|
0
|
|
} else {
|
|
1
|
|
}
|
|
}
|
|
|
|
pub(crate) unsafe fn strncasecmp(
|
|
s1: *const libc::c_char,
|
|
s2: *const libc::c_char,
|
|
n: libc::size_t,
|
|
) -> libc::c_int {
|
|
let s1 = std::ffi::CStr::from_ptr(s1)
|
|
.to_string_lossy()
|
|
.to_lowercase();
|
|
let s2 = std::ffi::CStr::from_ptr(s2)
|
|
.to_string_lossy()
|
|
.to_lowercase();
|
|
let m1 = std::cmp::min(n, s1.len());
|
|
let m2 = std::cmp::min(n, s2.len());
|
|
|
|
if s1[..m1] == s2[..m2] {
|
|
0
|
|
} else {
|
|
1
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::dc_tools::to_string;
|
|
|
|
#[test]
|
|
fn test_strndup() {
|
|
unsafe {
|
|
let res = strndup(b"helloworld\x00" as *const u8 as *const libc::c_char, 4);
|
|
assert_eq!(
|
|
to_string(res),
|
|
to_string(b"hell\x00" as *const u8 as *const libc::c_char)
|
|
);
|
|
assert_eq!(strlen(res), 4);
|
|
free(res as *mut _);
|
|
}
|
|
}
|
|
}
|