refactor(mmime): rustify mailimf_data_time

This commit is contained in:
dignifiedquire
2019-09-28 14:09:53 -06:00
parent 2abfd037ca
commit c78753e8fa
6 changed files with 204 additions and 221 deletions

View File

@@ -207,15 +207,16 @@ unsafe fn display_mime_fields(mut fields: *mut mailmime_fields) {
unsafe fn display_date_time(mut d: *mut mailimf_date_time) { unsafe fn display_date_time(mut d: *mut mailimf_date_time) {
print!( print!(
"{:02}/{:02}/{:02} {:02}:{:02}:{:02} +{:04}", "{:02}/{:02}/{:02} {:02}:{:02}:{:02} +{:04}",
(*d).dt_day, (*d).day,
(*d).dt_month, (*d).month,
(*d).dt_year, (*d).year,
(*d).dt_hour, (*d).hour,
(*d).dt_min, (*d).min,
(*d).dt_sec, (*d).sec,
(*d).dt_zone, (*d).zone,
); );
} }
unsafe fn display_orig_date(mut orig_date: *mut mailimf_orig_date) { unsafe fn display_orig_date(mut orig_date: *mut mailimf_orig_date) {
display_date_time((*orig_date).dt_date_time); display_date_time((*orig_date).dt_date_time);
} }

View File

@@ -2849,16 +2849,16 @@ pub unsafe fn mailimf_date_time_parse(
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut day_of_week: libc::c_int = 0; let mut day_of_week: libc::c_int = 0;
let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time; let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time;
let mut day: libc::c_int = 0; let mut day = 0;
let mut month: libc::c_int = 0; let mut month = 0;
let mut year: libc::c_int = 0; let mut year = 0;
let mut hour: libc::c_int = 0; let mut hour = 0;
let mut min: libc::c_int = 0; let mut min = 0;
let mut sec: libc::c_int = 0; let mut sec = 0;
let mut zone: libc::c_int = 0; let mut zone = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
day_of_week = -1i32; day_of_week = -1;
r = mailimf_day_of_week_parse(message, length, &mut cur_token, &mut day_of_week); r = mailimf_day_of_week_parse(message, length, &mut cur_token, &mut day_of_week);
if r == MAILIMF_NO_ERROR as libc::c_int { if r == MAILIMF_NO_ERROR as libc::c_int {
r = mailimf_comma_parse(message, length, &mut cur_token); r = mailimf_comma_parse(message, length, &mut cur_token);
@@ -2870,9 +2870,9 @@ pub unsafe fn mailimf_date_time_parse(
} else if r != MAILIMF_ERROR_PARSE as libc::c_int { } else if r != MAILIMF_ERROR_PARSE as libc::c_int {
return r; return r;
} }
day = 0i32; day = 0;
month = 0i32; month = 0;
year = 0i32; year = 0;
r = mailimf_date_parse( r = mailimf_date_parse(
message, message,
length, length,
@@ -2897,10 +2897,10 @@ pub unsafe fn mailimf_date_time_parse(
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
hour = 0i32; hour = 0;
min = 0i32; min = 0;
sec = 0i32; sec = 0;
zone = 0i32; zone = 0;
r = mailimf_time_parse( r = mailimf_time_parse(
message, message,
length, length,
@@ -2925,16 +2925,16 @@ unsafe fn mailimf_time_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut phour: *mut libc::c_int, mut phour: *mut u32,
mut pmin: *mut libc::c_int, mut pmin: *mut u32,
mut psec: *mut libc::c_int, mut psec: *mut u32,
mut pzone: *mut libc::c_int, mut pzone: *mut i32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut hour: libc::c_int = 0; let mut hour = 0;
let mut min: libc::c_int = 0; let mut min = 0;
let mut sec: libc::c_int = 0; let mut sec = 0;
let mut zone: libc::c_int = 0; let mut zone = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
r = mailimf_cfws_parse(message, length, &mut cur_token); r = mailimf_cfws_parse(message, length, &mut cur_token);
@@ -2959,7 +2959,7 @@ unsafe fn mailimf_time_parse(
r = mailimf_zone_parse(message, length, &mut cur_token, &mut zone); r = mailimf_zone_parse(message, length, &mut cur_token, &mut zone);
if !(r == MAILIMF_NO_ERROR as libc::c_int) { if !(r == MAILIMF_NO_ERROR as libc::c_int) {
if r == MAILIMF_ERROR_PARSE as libc::c_int { if r == MAILIMF_ERROR_PARSE as libc::c_int {
zone = 0i32 zone = 0
} else { } else {
return r; return r;
} }
@@ -2969,31 +2969,32 @@ unsafe fn mailimf_time_parse(
*psec = sec; *psec = sec;
*pzone = zone; *pzone = zone;
*indx = cur_token; *indx = cur_token;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_zone_parse( unsafe fn mailimf_zone_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut i32,
) -> libc::c_int { ) -> libc::c_int {
let mut zone: libc::c_int = 0; let mut zone = 0;
let mut sign: libc::c_int = 0; let mut sign: libc::c_int = 0;
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
let mut value: uint32_t = 0; let mut value = 0;
cur_token = *indx; cur_token = *indx;
if cur_token.wrapping_add(1i32 as libc::size_t) < length { if cur_token.wrapping_add(1i32 as libc::size_t) < length {
if *message.offset(cur_token as isize) as libc::c_int == 'U' as i32 if *message.offset(cur_token as isize) as libc::c_int == 'U' as i32
&& *message.offset(cur_token.wrapping_add(1i32 as libc::size_t) as isize) as libc::c_int && *message.offset(cur_token.wrapping_add(1i32 as libc::size_t) as isize) as libc::c_int
== 'T' as i32 == 'T' as i32
{ {
*result = 1i32; *result = 1;
*indx = cur_token.wrapping_add(2i32 as libc::size_t); *indx = cur_token.wrapping_add(2i32 as libc::size_t);
return MAILIMF_NO_ERROR as libc::c_int; return MAILIMF_NO_ERROR as libc::c_int;
} }
} }
zone = 0i32; zone = 0;
if cur_token.wrapping_add(2i32 as libc::size_t) < length { if cur_token.wrapping_add(2i32 as libc::size_t) < length {
let mut state: libc::c_int = 0; let mut state: libc::c_int = 0;
state = STATE_ZONE_1 as libc::c_int; state = STATE_ZONE_1 as libc::c_int;
@@ -3024,7 +3025,7 @@ unsafe fn mailimf_zone_parse(
as size_t as size_t; as size_t as size_t;
state = STATE_ZONE_CONT as libc::c_int state = STATE_ZONE_CONT as libc::c_int
} else { } else {
zone = 0i32; zone = 0;
state = STATE_ZONE_OK as libc::c_int state = STATE_ZONE_OK as libc::c_int
} }
} else { } else {
@@ -3032,19 +3033,19 @@ unsafe fn mailimf_zone_parse(
} }
} }
69 => { 69 => {
zone = -5i32; zone = -5;
state = STATE_ZONE_2 as libc::c_int state = STATE_ZONE_2 as libc::c_int
} }
67 => { 67 => {
zone = -6i32; zone = -6;
state = STATE_ZONE_2 as libc::c_int state = STATE_ZONE_2 as libc::c_int
} }
77 => { 77 => {
zone = -7i32; zone = -7;
state = STATE_ZONE_2 as libc::c_int state = STATE_ZONE_2 as libc::c_int
} }
80 => { 80 => {
zone = -8i32; zone = -8;
state = STATE_ZONE_2 as libc::c_int state = STATE_ZONE_2 as libc::c_int
} }
_ => state = STATE_ZONE_CONT as libc::c_int, _ => state = STATE_ZONE_CONT as libc::c_int,
@@ -3066,7 +3067,7 @@ unsafe fn mailimf_zone_parse(
as libc::c_int as libc::c_int
== 'T' as i32 == 'T' as i32
{ {
zone *= 100i32; zone *= 100;
state = STATE_ZONE_OK as libc::c_int state = STATE_ZONE_OK as libc::c_int
} else { } else {
state = STATE_ZONE_ERR as libc::c_int state = STATE_ZONE_ERR as libc::c_int
@@ -3107,7 +3108,7 @@ unsafe fn mailimf_zone_parse(
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
zone = value.wrapping_mul(sign as libc::c_uint) as libc::c_int; zone = value.wrapping_mul(sign as libc::c_uint) as i32;
*indx = cur_token; *indx = cur_token;
*result = zone; *result = zone;
return MAILIMF_NO_ERROR as libc::c_int; return MAILIMF_NO_ERROR as libc::c_int;
@@ -3192,13 +3193,13 @@ unsafe fn mailimf_time_of_day_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut phour: *mut libc::c_int, mut phour: *mut u32,
mut pmin: *mut libc::c_int, mut pmin: *mut u32,
mut psec: *mut libc::c_int, mut psec: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut hour: libc::c_int = 0; let mut hour = 0;
let mut min: libc::c_int = 0; let mut min = 0;
let mut sec: libc::c_int = 0; let mut sec = 0;
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
@@ -3221,7 +3222,7 @@ unsafe fn mailimf_time_of_day_parse(
return r; return r;
} }
} else if r == MAILIMF_ERROR_PARSE as libc::c_int { } else if r == MAILIMF_ERROR_PARSE as libc::c_int {
sec = 0i32 sec = 0;
} else { } else {
return r; return r;
} }
@@ -3235,72 +3236,75 @@ unsafe fn mailimf_second_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut second: uint32_t = 0; let mut second = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
r = mailimf_number_parse(message, length, indx, &mut second); r = mailimf_number_parse(message, length, indx, &mut second);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
*result = second as libc::c_int; *result = second as u32;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_minute_parse( unsafe fn mailimf_minute_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut minute: uint32_t = 0; let mut minute = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
r = mailimf_number_parse(message, length, indx, &mut minute); r = mailimf_number_parse(message, length, indx, &mut minute);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
*result = minute as libc::c_int; *result = minute as u32;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_hour_parse( unsafe fn mailimf_hour_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut hour: uint32_t = 0; let mut hour = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
r = mailimf_number_parse(message, length, indx, &mut hour); r = mailimf_number_parse(message, length, indx, &mut hour);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
*result = hour as libc::c_int; *result = hour as u32;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_broken_date_parse( unsafe fn mailimf_broken_date_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut pday: *mut libc::c_int, mut pday: *mut u32,
mut pmonth: *mut libc::c_int, mut pmonth: *mut u32,
mut pyear: *mut libc::c_int, mut pyear: *mut i32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut day: libc::c_int = 0; let mut day = 0;
let mut month: libc::c_int = 0; let mut month = 0;
let mut year: libc::c_int = 0; let mut year = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
month = 1i32; month = 1;
r = mailimf_month_parse(message, length, &mut cur_token, &mut month); r = mailimf_month_parse(message, length, &mut cur_token, &mut month);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
day = 1i32; day = 1;
r = mailimf_day_parse(message, length, &mut cur_token, &mut day); r = mailimf_day_parse(message, length, &mut cur_token, &mut day);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
year = 2001i32; year = 2001;
r = mailimf_year_parse(message, length, &mut cur_token, &mut year); r = mailimf_year_parse(message, length, &mut cur_token, &mut year);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
@@ -3315,9 +3319,9 @@ unsafe fn mailimf_year_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut i32,
) -> libc::c_int { ) -> libc::c_int {
let mut number: uint32_t = 0; let mut number = 0;
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
@@ -3330,17 +3334,18 @@ unsafe fn mailimf_year_parse(
return r; return r;
} }
*indx = cur_token; *indx = cur_token;
*result = number as libc::c_int; *result = number as i32;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_day_parse( unsafe fn mailimf_day_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut day: uint32_t = 0; let mut day = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
r = mailimf_cfws_parse(message, length, &mut cur_token); r = mailimf_cfws_parse(message, length, &mut cur_token);
@@ -3351,18 +3356,19 @@ unsafe fn mailimf_day_parse(
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
*result = day as libc::c_int; *result = day as u32;
*indx = cur_token; *indx = cur_token;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_month_parse( unsafe fn mailimf_month_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut month: libc::c_int = 0; let mut month = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
r = mailimf_cfws_parse(message, length, &mut cur_token); r = mailimf_cfws_parse(message, length, &mut cur_token);
@@ -3375,38 +3381,41 @@ unsafe fn mailimf_month_parse(
} }
*result = month; *result = month;
*indx = cur_token; *indx = cur_token;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
unsafe fn mailimf_month_name_parse( unsafe fn mailimf_month_name_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut result: *mut libc::c_int, mut result: *mut u32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut month: libc::c_int = 0; let mut month: u32 = 0;
let mut guessed_month: libc::c_int = 0;
let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
guessed_month = guess_month(message, length, cur_token);
if guessed_month == -1i32 { let guessed_month = guess_month(message, length, cur_token);
if guessed_month < 0 {
return MAILIMF_ERROR_PARSE as libc::c_int; return MAILIMF_ERROR_PARSE as libc::c_int;
} }
r = mailimf_token_case_insensitive_len_parse(
let r = mailimf_token_case_insensitive_len_parse(
message, message,
length, length,
&mut cur_token, &mut cur_token,
month_names[(guessed_month - 1i32) as usize].str_0, month_names[(guessed_month - 1) as usize].str_0,
strlen(month_names[(guessed_month - 1i32) as usize].str_0), strlen(month_names[(guessed_month - 1) as usize].str_0),
); );
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
month = guessed_month;
month = guessed_month as u32;
*result = month; *result = month;
*indx = cur_token; *indx = cur_token;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
/* /*
month-name = "Jan" / "Feb" / "Mar" / "Apr" / month-name = "Jan" / "Feb" / "Mar" / "Apr" /
"May" / "Jun" / "Jul" / "Aug" / "May" / "Jun" / "Jul" / "Aug" /
@@ -3543,27 +3552,27 @@ unsafe fn mailimf_date_parse(
mut message: *const libc::c_char, mut message: *const libc::c_char,
mut length: size_t, mut length: size_t,
mut indx: *mut size_t, mut indx: *mut size_t,
mut pday: *mut libc::c_int, mut pday: *mut u32,
mut pmonth: *mut libc::c_int, mut pmonth: *mut u32,
mut pyear: *mut libc::c_int, mut pyear: *mut i32,
) -> libc::c_int { ) -> libc::c_int {
let mut cur_token: size_t = 0; let mut cur_token: size_t = 0;
let mut day: libc::c_int = 0; let mut day = 0;
let mut month: libc::c_int = 0; let mut month = 0;
let mut year: libc::c_int = 0; let mut year = 0;
let mut r: libc::c_int = 0; let mut r: libc::c_int = 0;
cur_token = *indx; cur_token = *indx;
day = 1i32; day = 1;
r = mailimf_day_parse(message, length, &mut cur_token, &mut day); r = mailimf_day_parse(message, length, &mut cur_token, &mut day);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
month = 1i32; month = 1;
r = mailimf_month_parse(message, length, &mut cur_token, &mut month); r = mailimf_month_parse(message, length, &mut cur_token, &mut month);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
} }
year = 2001i32; year = 2001;
r = mailimf_year_parse(message, length, &mut cur_token, &mut year); r = mailimf_year_parse(message, length, &mut cur_token, &mut year);
if r != MAILIMF_NO_ERROR as libc::c_int { if r != MAILIMF_NO_ERROR as libc::c_int {
return r; return r;
@@ -3631,7 +3640,7 @@ unsafe fn mailimf_day_name_parse(
day_of_week = guessed_day; day_of_week = guessed_day;
*result = day_of_week; *result = day_of_week;
*indx = cur_token; *indx = cur_token;
return MAILIMF_NO_ERROR as libc::c_int; MAILIMF_NO_ERROR as libc::c_int
} }
static mut day_names: [mailimf_token_value; 7] = [ static mut day_names: [mailimf_token_value; 7] = [
mailimf_token_value { mailimf_token_value {

View File

@@ -4,48 +4,35 @@ use crate::clist::*;
use crate::other::*; use crate::other::*;
/* /*
IMPORTANT NOTE: IMPORTANT NOTE:
All allocation functions will take as argument allocated data All allocation functions will take as argument allocated data
and will store these data in the structure they will allocate. and will store these data in the structure they will allocate.
Data should be persistant during all the use of the structure Data should be persistant during all the use of the structure
and will be freed by the free function of the structure and will be freed by the free function of the structure
allocation functions will return NULL on failure allocation functions will return NULL on failure
*/ */
/*
mailimf_date_time is a date
- day is the day of month (1 to 31) /// Date
/// - day is the day of month (1 to 31)
- month (1 to 12) /// - month (1 to 12)
/// - year (4 digits)
- year (4 digits) /// - hour (0 to 23)
/// - min (0 to 59)
- hour (0 to 23) /// - sec (0 to 59)
/// - zone (this is the decimal value that we can read, for example:
- min (0 to 59) // for "-0200", the value is -200)
#[derive(Clone, Debug)]
- sec (0 to 59)
- zone (this is the decimal value that we can read, for example:
for "-0200", the value is -200)
*/
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct mailimf_date_time { pub struct mailimf_date_time {
pub dt_day: libc::c_int, pub day: u32,
pub dt_month: libc::c_int, pub month: u32,
pub dt_year: libc::c_int, pub year: i32,
pub dt_hour: libc::c_int, pub hour: u32,
pub dt_min: libc::c_int, pub min: u32,
pub dt_sec: libc::c_int, pub sec: u32,
pub dt_zone: libc::c_int, pub zone: i32,
} }
/* this is the type of address */
/* if this is a group
(group_name: address1@domain1,
address2@domain2; ) */
/// An address, either for a mailbox or a group. /// An address, either for a mailbox or a group.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@@ -438,34 +425,35 @@ pub const MAILIMF_FIELD_RETURN_PATH: unnamed_2 = 1;
pub type unnamed_2 = libc::c_uint; pub type unnamed_2 = libc::c_uint;
/* on parse error */ /* on parse error */
pub const MAILIMF_FIELD_NONE: unnamed_2 = 0; pub const MAILIMF_FIELD_NONE: unnamed_2 = 0;
#[no_mangle]
pub unsafe fn mailimf_date_time_new( pub unsafe fn mailimf_date_time_new(
mut dt_day: libc::c_int, day: u32,
mut dt_month: libc::c_int, month: u32,
mut dt_year: libc::c_int, year: i32,
mut dt_hour: libc::c_int, hour: u32,
mut dt_min: libc::c_int, min: u32,
mut dt_sec: libc::c_int, sec: u32,
mut dt_zone: libc::c_int, zone: i32,
) -> *mut mailimf_date_time { ) -> *mut mailimf_date_time {
let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time; let dt = mailimf_date_time {
date_time = malloc(::std::mem::size_of::<mailimf_date_time>() as libc::size_t) day,
as *mut mailimf_date_time; month,
if date_time.is_null() { year,
return 0 as *mut mailimf_date_time; hour,
} min,
(*date_time).dt_day = dt_day; sec,
(*date_time).dt_month = dt_month; zone,
(*date_time).dt_year = dt_year; };
(*date_time).dt_hour = dt_hour;
(*date_time).dt_min = dt_min; Box::into_raw(Box::new(dt))
(*date_time).dt_sec = dt_sec;
(*date_time).dt_zone = dt_zone;
return date_time;
} }
pub unsafe fn mailimf_date_time_free(mut date_time: *mut mailimf_date_time) { pub unsafe fn mailimf_date_time_free(date_time: *mut mailimf_date_time) {
free(date_time as *mut libc::c_void); if date_time.is_null() {
return;
}
let _ = Box::from_raw(date_time);
} }
pub fn mailimf_address_new_mailbox(ad_mailbox: *mut mailimf_mailbox) -> *mut mailimf_address { pub fn mailimf_address_new_mailbox(ad_mailbox: *mut mailimf_mailbox) -> *mut mailimf_address {

View File

@@ -1581,22 +1581,18 @@ unsafe fn mailimf_date_time_write_driver(
mut col: *mut libc::c_int, mut col: *mut libc::c_int,
mut date_time: *mut mailimf_date_time, mut date_time: *mut mailimf_date_time,
) -> libc::c_int { ) -> libc::c_int {
let wday = dayofweek( let wday = dayofweek((*date_time).year, (*date_time).month, (*date_time).day);
(*date_time).dt_year,
(*date_time).dt_month,
(*date_time).dt_day,
);
let date_str = format!( let date_str = format!(
"{}, {} {} {} {:02}:{:02}:{:02} {:+05}", "{}, {} {} {} {:02}:{:02}:{:02} {:+05}",
week_of_day_str[wday as usize], wday,
(*date_time).dt_day, (*date_time).day,
month_str[((*date_time).dt_month - 1i32) as usize], month_str[((*date_time).month - 1) as usize],
(*date_time).dt_year, (*date_time).year,
(*date_time).dt_hour, (*date_time).hour,
(*date_time).dt_min, (*date_time).min,
(*date_time).dt_sec, (*date_time).sec,
(*date_time).dt_zone, (*date_time).zone,
); );
let date_str_c = std::ffi::CString::new(date_str).unwrap(); let date_str_c = std::ffi::CString::new(date_str).unwrap();
let r = mailimf_string_write_driver( let r = mailimf_string_write_driver(
@@ -1611,27 +1607,16 @@ unsafe fn mailimf_date_time_write_driver(
} }
return MAILIMF_NO_ERROR as libc::c_int; return MAILIMF_NO_ERROR as libc::c_int;
} }
static mut month_str: [&'static str; 12] = [ static mut month_str: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
]; ];
static mut week_of_day_str: [&'static str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
/* 0 = Sunday */ fn dayofweek(year: i32, month: u32, day: u32) -> String {
/* y > 1752 */ chrono::NaiveDate::from_ymd(year, month, day)
unsafe fn dayofweek( .format("%a")
mut year: libc::c_int, .to_string()
mut month: libc::c_int,
mut day: libc::c_int,
) -> libc::c_int {
static mut offset: [libc::c_int; 12] = [
0i32, 3i32, 2i32, 5i32, 0i32, 3i32, 5i32, 1i32, 4i32, 6i32, 2i32, 4i32,
];
year -= (month < 3i32) as libc::c_int;
return (year + year / 4i32 - year / 100i32
+ year / 400i32
+ offset[(month - 1i32) as usize]
+ day)
% 7i32;
} }
unsafe fn mailimf_resent_msg_id_write_driver( unsafe fn mailimf_resent_msg_id_write_driver(
mut do_write: Option< mut do_write: Option<
unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int,

View File

@@ -1646,12 +1646,12 @@ pub fn mailimf_get_date(t: i64) -> *mut mailimf_date_time {
unsafe { unsafe {
mailimf_date_time_new( mailimf_date_time_new(
lt.day() as libc::c_int, lt.day(),
lt.month() as libc::c_int, lt.month(),
lt.year() as libc::c_int, lt.year(),
lt.hour() as libc::c_int, lt.hour(),
lt.minute() as libc::c_int, lt.minute(),
lt.second() as libc::c_int, lt.second(),
off, off,
) )
} }
@@ -1691,16 +1691,16 @@ mod tests {
let now_local = Local.from_utc_datetime(&now_utc.naive_local()); let now_local = Local.from_utc_datetime(&now_utc.naive_local());
let t_local = now_local.timestamp(); let t_local = now_local.timestamp();
let converted = unsafe { *mailimf_get_date(t_local as i64) }; let converted = unsafe { &*mailimf_get_date(t_local as i64) };
assert_eq!(converted.dt_day as u32, now_local.day()); assert_eq!(converted.day, now_local.day());
assert_eq!(converted.dt_month as u32, now_local.month()); assert_eq!(converted.month, now_local.month());
assert_eq!(converted.dt_year, now_local.year()); assert_eq!(converted.year, now_local.year());
assert_eq!(converted.dt_hour as u32, now_local.hour()); assert_eq!(converted.hour, now_local.hour());
assert_eq!(converted.dt_min as u32, now_local.minute()); assert_eq!(converted.min, now_local.minute());
assert_eq!(converted.dt_sec as u32, now_local.second()); assert_eq!(converted.sec, now_local.second());
assert_eq!( assert_eq!(
converted.dt_zone, converted.zone,
(now_local.offset().local_minus_utc() / (60 * 60)) * 100 (now_local.offset().local_minus_utc() / (60 * 60)) * 100
); );
} }

View File

@@ -219,24 +219,24 @@ pub(crate) fn dc_str_to_color(s: impl AsRef<str>) -> u32 {
/* the result is UTC or DC_INVALID_TIMESTAMP */ /* the result is UTC or DC_INVALID_TIMESTAMP */
pub(crate) fn dc_timestamp_from_date(date_time: *mut mailimf_date_time) -> i64 { pub(crate) fn dc_timestamp_from_date(date_time: *mut mailimf_date_time) -> i64 {
assert!(!date_time.is_null()); assert!(!date_time.is_null());
let dt = unsafe { *date_time }; let dt = unsafe { &*date_time };
let sec = dt.dt_sec; let sec = dt.sec;
let min = dt.dt_min; let min = dt.min;
let hour = dt.dt_hour; let hour = dt.hour;
let day = dt.dt_day; let day = dt.day;
let month = dt.dt_month; let month = dt.month;
let year = dt.dt_year; let year = dt.year;
let ts = chrono::NaiveDateTime::new( let ts = chrono::NaiveDateTime::new(
chrono::NaiveDate::from_ymd(year, month as u32, day as u32), chrono::NaiveDate::from_ymd(year, month, day),
chrono::NaiveTime::from_hms(hour as u32, min as u32, sec as u32), chrono::NaiveTime::from_hms(hour, min, sec),
); );
let (zone_hour, zone_min) = if dt.dt_zone >= 0 { let (zone_hour, zone_min) = if dt.zone >= 0 {
(dt.dt_zone / 100, dt.dt_zone % 100) (dt.zone / 100, dt.zone % 100)
} else { } else {
(-(-dt.dt_zone / 100), -(-dt.dt_zone % 100)) (-(-dt.zone / 100), -(-dt.zone % 100))
}; };
ts.timestamp() - (zone_hour * 3600 + zone_min * 60) as i64 ts.timestamp() - (zone_hour * 3600 + zone_min * 60) as i64