Allocate dc_kml_t in a rusty way

This commit is contained in:
Alexander Krotov
2019-07-24 23:37:56 +03:00
committed by Floris Bruynooghe
parent b0ef825e67
commit ec6cc5c355
4 changed files with 53 additions and 47 deletions

View File

@@ -481,8 +481,8 @@ pub unsafe fn dc_kml_parse(
context: &Context,
content: *const libc::c_char,
content_bytes: size_t,
) -> *mut dc_kml_t {
let mut kml: *mut dc_kml_t = calloc(1, ::std::mem::size_of::<dc_kml_t>()) as *mut dc_kml_t;
) -> dc_kml_t {
let mut kml = dc_kml_t::new();
let mut content_nullterminated: *mut libc::c_char = 0 as *mut libc::c_char;
let mut saxparser: dc_saxparser_t = dc_saxparser_t {
starttag_cb: None,
@@ -499,8 +499,11 @@ pub unsafe fn dc_kml_parse(
} else {
content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int);
if !content_nullterminated.is_null() {
(*kml).locations = dc_array_new_typed(1, 100 as size_t);
dc_saxparser_init(&mut saxparser, kml as *mut libc::c_void);
kml.locations = dc_array_new_typed(1, 100 as size_t);
dc_saxparser_init(
&mut saxparser,
&mut kml as *mut dc_kml_t as *mut libc::c_void,
);
dc_saxparser_set_tag_handler(
&mut saxparser,
Some(kml_starttag_cb),
@@ -641,7 +644,6 @@ pub unsafe fn dc_kml_unref(kml: *mut dc_kml_t) {
}
dc_array_unref((*kml).locations);
free((*kml).addr as *mut libc::c_void);
free(kml as *mut libc::c_void);
}
pub unsafe fn dc_job_do_DC_JOB_MAYBE_SEND_LOCATIONS(context: &Context, _job: *mut dc_job_t) {

View File

@@ -56,8 +56,8 @@ pub struct dc_mimeparser_t<'a> {
pub context: &'a Context,
pub reports: *mut carray,
pub is_system_message: libc::c_int,
pub location_kml: *mut dc_kml_t,
pub message_kml: *mut dc_kml_t,
pub location_kml: Option<dc_kml_t>,
pub message_kml: Option<dc_kml_t>,
}
// deprecated
@@ -83,8 +83,8 @@ pub unsafe fn dc_mimeparser_new(context: &Context) -> dc_mimeparser_t {
context,
reports: carray_new(16i32 as libc::c_uint),
is_system_message: 0,
location_kml: std::ptr::null_mut(),
message_kml: std::ptr::null_mut(),
location_kml: None,
message_kml: None,
}
}
@@ -134,11 +134,15 @@ pub unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) {
(*mimeparser).decrypting_failed = 0i32;
dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper);
dc_kml_unref((*mimeparser).location_kml);
(*mimeparser).location_kml = 0 as *mut dc_kml_t;
if let Some(location_kml) = (*mimeparser).location_kml.as_mut() {
dc_kml_unref(location_kml as *mut dc_kml_t);
}
(*mimeparser).location_kml = None;
dc_kml_unref((*mimeparser).message_kml);
(*mimeparser).message_kml = 0 as *mut dc_kml_t;
if let Some(message_kml) = (*mimeparser).message_kml.as_mut() {
dc_kml_unref(message_kml as *mut dc_kml_t);
}
(*mimeparser).message_kml = None;
}
unsafe fn dc_mimepart_unref(mut mimepart: *mut dc_mimepart_t) {
@@ -1392,11 +1396,11 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
4,
) == 0i32
{
(*mimeparser).location_kml = dc_kml_parse(
(*mimeparser).location_kml = Some(dc_kml_parse(
(*mimeparser).context,
decoded_data,
decoded_data_bytes,
);
));
current_block = 8795901732489102124;
} else if strncmp(
desired_filename,
@@ -1411,11 +1415,11 @@ unsafe fn dc_mimeparser_add_single_part_if_known(
4,
) == 0i32
{
(*mimeparser).message_kml = dc_kml_parse(
(*mimeparser).message_kml = Some(dc_kml_parse(
(*mimeparser).context,
decoded_data,
decoded_data_bytes,
);
));
current_block = 8795901732489102124;
} else {
dc_replace_bad_utf8_chars(desired_filename);

View File

@@ -454,7 +454,7 @@ pub unsafe fn dc_receive_imf(
}
let part = carray_get(mime_parser.parts, i as libc::c_uint) as *mut dc_mimepart_t;
if !(0 != (*part).is_meta) {
if !mime_parser.location_kml.is_null()
if !mime_parser.location_kml.is_none()
&& icnt == 1
&& !(*part).msg.is_null()
&& (strcmp(
@@ -772,18 +772,18 @@ pub unsafe fn dc_receive_imf(
i = i.wrapping_add(1)
}
}
if !mime_parser.message_kml.is_null() && chat_id > 9 as libc::c_uint {
if !mime_parser.message_kml.is_none() && chat_id > 9 as libc::c_uint {
let mut location_id_written = false;
let mut send_event = false;
if !mime_parser.message_kml.is_null()
if !mime_parser.message_kml.is_none()
&& chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint
{
let newest_location_id: uint32_t = dc_save_locations(
context,
chat_id,
from_id,
(*mime_parser.message_kml).locations,
mime_parser.message_kml.unwrap().locations,
1,
);
if 0 != newest_location_id && 0 == hidden {
@@ -793,20 +793,23 @@ pub unsafe fn dc_receive_imf(
}
}
if !mime_parser.location_kml.is_null()
if !mime_parser.location_kml.is_none()
&& chat_id > DC_CHAT_ID_LAST_SPECIAL as libc::c_uint
{
let contact = dc_get_contact(context, from_id);
if !(*mime_parser.location_kml).addr.is_null()
if !mime_parser.location_kml.as_ref().unwrap().addr.is_null()
&& !contact.is_null()
&& !(*contact).addr.is_null()
&& strcasecmp((*contact).addr, (*mime_parser.location_kml).addr) == 0
&& strcasecmp(
(*contact).addr,
mime_parser.location_kml.as_ref().unwrap().addr,
) == 0
{
let newest_location_id = dc_save_locations(
context,
chat_id,
from_id,
(*mime_parser.location_kml).locations,
mime_parser.location_kml.as_ref().unwrap().locations,
0,
);
if newest_location_id != 0 && hidden == 0 && !location_id_written {

View File

@@ -837,34 +837,31 @@ fn test_dc_kml_parse() {
b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Document addr=\"user@example.org\">\n<Placemark><Timestamp><when>2019-03-06T21:09:57Z</when></Timestamp><Point><coordinates accuracy=\"32.000000\">9.423110,53.790302</coordinates></Point></Placemark>\n<PlaceMARK>\n<Timestamp><WHEN > \n\t2018-12-13T22:11:12Z\t</wHeN></Timestamp><Point><coordinates aCCuracy=\"2.500000\"> 19.423110 \t , \n 63.790302\n </coordinates></Point></Placemark>\n</Document>\n</kml>\x00"
as *const u8 as *const libc::c_char;
let kml: *mut dc_kml_t = dc_kml_parse(&context.ctx, xml, strlen(xml));
let mut kml = dc_kml_parse(&context.ctx, xml, strlen(xml));
assert!(!(*kml).addr.is_null());
assert_eq!(
as_str((*kml).addr as *const libc::c_char),
"user@example.org",
);
assert!(!kml.addr.is_null());
assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",);
assert_eq!(dc_array_get_cnt((*kml).locations), 2);
assert_eq!(dc_array_get_cnt(kml.locations), 2);
assert!(dc_array_get_latitude((*kml).locations, 0) > 53.6f64);
assert!(dc_array_get_latitude((*kml).locations, 0) < 53.8f64);
assert!(dc_array_get_longitude((*kml).locations, 0) > 9.3f64);
assert!(dc_array_get_longitude((*kml).locations, 0) < 9.5f64);
assert!(dc_array_get_accuracy((*kml).locations, 0) > 31.9f64);
assert!(dc_array_get_accuracy((*kml).locations, 0) < 32.1f64);
assert_eq!(dc_array_get_timestamp((*kml).locations, 0), 1551906597);
assert!(dc_array_get_latitude(kml.locations, 0) > 53.6f64);
assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64);
assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64);
assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64);
assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64);
assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64);
assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597);
assert!(dc_array_get_latitude((*kml).locations, 1) > 63.6f64);
assert!(dc_array_get_latitude((*kml).locations, 1) < 63.8f64);
assert!(dc_array_get_longitude((*kml).locations, 1) > 19.3f64);
assert!(dc_array_get_longitude((*kml).locations, 1) < 19.5f64);
assert!(dc_array_get_accuracy((*kml).locations, 1) > 2.4f64);
assert!(dc_array_get_accuracy((*kml).locations, 1) < 2.6f64);
assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64);
assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64);
assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64);
assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64);
assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64);
assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64);
assert_eq!(dc_array_get_timestamp((*kml).locations, 1), 1544739072);
assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072);
dc_kml_unref(kml);
dc_kml_unref(&mut kml);
}
}