diff --git a/Cargo.lock b/Cargo.lock index 690d54f18..84abbbcc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -502,7 +502,7 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lettre 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", - "mmime 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "mmime 0.1.2", "native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "num-derive 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1258,7 +1258,6 @@ dependencies = [ [[package]] name = "mmime" version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "charset 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3056,7 +3055,6 @@ dependencies = [ "checksum miniz_oxide 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7108aff85b876d06f22503dcce091e29f76733b2bfdd91eebce81f5e68203a10" "checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" -"checksum mmime 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "befb13ecb83a4344d5c268a36463fc1b3526a78bfded316ddfaa955b5d018b91" "checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4dbdc256eaac2e3bd236d93ad999d3479ef775c863dbda3068c4006a92eec51b" diff --git a/Cargo.toml b/Cargo.toml index 7ff3c62bc..f870f2df7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ license = "MPL" [dependencies] deltachat_derive = { path = "./deltachat_derive" } +mmime = { version = "0.1.2", path = "./mmime" } + libc = "0.2.51" pgp = { version = "0.2", default-features = false } hex = "0.3.2" @@ -20,7 +22,6 @@ num-traits = "0.2.6" native-tls = "0.2.3" lettre = "0.9.0" imap = { git = "https://github.com/jonhoo/rust-imap", rev = "281d2eb8ab50dc656ceff2ae749ca5045f334e15" } -mmime = "0.1.2" base64 = "0.10" charset = "0.1" percent-encoding = "2.0" @@ -59,6 +60,7 @@ proptest = "0.9.4" members = [ "deltachat-ffi", "deltachat_derive", + "mmime", ] [[example]] diff --git a/appveyor.yml b/appveyor.yml index 23a513f60..132892d49 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,7 +13,7 @@ install: build: false test_script: - - cargo test --release + - cargo test --release --all cache: - target diff --git a/ci_scripts/run-rust-test.sh b/ci_scripts/run-rust-test.sh index 1b922f1b4..5a8e20c7d 100755 --- a/ci_scripts/run-rust-test.sh +++ b/ci_scripts/run-rust-test.sh @@ -31,7 +31,7 @@ fi if [[ $NORUN == "1" ]]; then export CARGO_SUBCMD="build" else - export CARGO_SUBCMD="test" + export CARGO_SUBCMD="test --all" export OPT="${OPT} " export OPT_RELEASE="${OPT_RELEASE} " export OPT_RELEASE_IGNORED="${OPT_RELEASE} -- --ignored" @@ -39,4 +39,5 @@ fi # Run all the test configurations: $CARGO_CMD $CARGO_SUBCMD $OPT +$CARGO_CMD $CARGO_SUBCMD $OPT_RELEASE $CARGO_CMD $CARGO_SUBCMD $OPT_RELEASE_IGNORED diff --git a/mmime/.circleci/config.yml b/mmime/.circleci/config.yml new file mode 100644 index 000000000..74a108d88 --- /dev/null +++ b/mmime/.circleci/config.yml @@ -0,0 +1,42 @@ +# copied from http://koushiro.me/2019/04/30/Building-and-Testing-Rust-projects-on-CircleCI/ + +version: 2.1 +jobs: + build: + docker: + - image: ubuntu:18.04 + + working_directory: ~/deltachat-core-rust + + steps: + - checkout + + - run: + name: Setup build environment + command: | + apt update + apt install -y curl build-essential autoconf libtool git python pkg-config + # this will pick default toolchain from `rust-toolchain` file + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --no-modify-path --default-toolchain none -y; + source $HOME/.cargo/env + no_output_timeout: 1800s + + - run: + name: Format + command: | + export PATH=~/.cargo/bin:$PATH + rustup component add rustfmt + cargo fmt -- --check + + - run: + name: Test + command: | + export PATH=~/.cargo/bin:$PATH + export RUST_BACKTRACE=1 + cargo test + +workflows: + version: 2.1 + build: + jobs: + - build diff --git a/mmime/Cargo.toml b/mmime/Cargo.toml new file mode 100644 index 000000000..ddef4c84a --- /dev/null +++ b/mmime/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "mmime" +version = "0.1.2" +authors = ["dignifiedquire "] +edition = "2018" +license = "MIT OR Apache-2.0" +homepage = "https://github.com/deltachat/deltachat-core-rust" +repository = "https://github.com/deltachat/deltachat-core-rust" +readme = "README.md" +description = "Mime parsing for email" + + +keywords = ["mail", "mim", "email", "imap", "smtp"] +categories = ["std", "email"] + +[dependencies] +libc = "0.2.54" +charset = "0.1.2" +memmap = "0.7.0" +lazy_static = "1.3.0" +rand = "0.6.5" +chrono = "0.4.6" +hex = "0.3.2" diff --git a/mmime/LICENSE-APACHE b/mmime/LICENSE-APACHE new file mode 100644 index 000000000..f8e5e5ea0 --- /dev/null +++ b/mmime/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/mmime/LICENSE-MIT b/mmime/LICENSE-MIT new file mode 100644 index 000000000..468cd79a8 --- /dev/null +++ b/mmime/LICENSE-MIT @@ -0,0 +1,23 @@ +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/mmime/LICENSE.md b/mmime/LICENSE.md new file mode 100644 index 000000000..878517643 --- /dev/null +++ b/mmime/LICENSE.md @@ -0,0 +1,4 @@ +This library is primarly distributed under the terms of both the MIT license and +the Apache License (Version 2.0). + +See LICENSE-MIT and LICENSE-APACHE for details. diff --git a/mmime/README.md b/mmime/README.md new file mode 100644 index 000000000..16858f979 --- /dev/null +++ b/mmime/README.md @@ -0,0 +1,16 @@ +# mmime + +[![CircleCI build status][circle-shield]][circle] [![Appveyor build status][appveyor-shield]][appveyor] [![License][license-shield]][license] + +> mmmmmmime parsing + +Base code was compiled using c2rust from libetpan. + + + +[circle-shield]: https://img.shields.io/circleci/project/github/dignifiedquire/mmime/master.svg?style=flat-square +[circle]: https://circleci.com/gh/dignifiedquire/mmime/ +[appveyor-shield]: https://ci.appveyor.com/api/projects/status/l26co5rba32knrlu/branch/master?style=flat-square +[appveyor]: https://ci.appveyor.com/project/dignifiedquire/mmime/branch/master +[license-shield]: https://img.shields.io/badge/License-MIT%2FApache2.0-green.svg?style=flat-square +[license]: https://github.com/rpgp/rpgp/blob/master/LICENSE.md diff --git a/mmime/src/charconv.rs b/mmime/src/charconv.rs new file mode 100644 index 000000000..c9f551e23 --- /dev/null +++ b/mmime/src/charconv.rs @@ -0,0 +1,32 @@ +use crate::other::*; +use libc; +use std::ffi::{CStr, CString}; + +pub const MAIL_CHARCONV_ERROR_CONV: libc::c_uint = 3; +pub const MAIL_CHARCONV_ERROR_MEMORY: libc::c_uint = 2; +pub const MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: libc::c_uint = 1; +pub const MAIL_CHARCONV_NO_ERROR: libc::c_uint = 0; + +pub unsafe fn charconv( + tocode: *const libc::c_char, + fromcode: *const libc::c_char, + s: *const libc::c_char, + length: size_t, + result: *mut *mut libc::c_char, +) -> libc::c_int { + assert!(!fromcode.is_null(), "invalid fromcode"); + assert!(!s.is_null(), "invalid input string"); + if let Some(encoding) = + charset::Charset::for_label(CStr::from_ptr(fromcode).to_str().unwrap().as_bytes()) + { + let data = std::slice::from_raw_parts(s as *const u8, strlen(s)); + + let (res, _, _) = encoding.decode(data); + let res_c = CString::new(res.as_bytes()).unwrap(); + *result = strdup(res_c.as_ptr()) as *mut _; + + MAIL_CHARCONV_NO_ERROR as libc::c_int + } else { + MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET as libc::c_int + } +} diff --git a/mmime/src/chash.rs b/mmime/src/chash.rs new file mode 100644 index 000000000..301b35ae9 --- /dev/null +++ b/mmime/src/chash.rs @@ -0,0 +1,427 @@ +use libc; + +use crate::other::*; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct chashdatum { + pub data: *mut libc::c_void, + pub len: libc::c_uint, +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct chash { + pub size: libc::c_uint, + pub count: libc::c_uint, + pub copyvalue: libc::c_int, + pub copykey: libc::c_int, + pub cells: *mut *mut chashcell, +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct chashcell { + pub func: libc::c_uint, + pub key: chashdatum, + pub value: chashdatum, + pub next: *mut chashcell, +} + +pub type chashiter = chashcell; +/* Allocates a new (empty) hash using this initial size and the given flags, + specifying which data should be copied in the hash. + CHASH_COPYNONE : Keys/Values are not copied. + CHASH_COPYKEY : Keys are dupped and freed as needed in the hash. + CHASH_COPYVALUE : Values are dupped and freed as needed in the hash. + CHASH_COPYALL : Both keys and values are dupped in the hash. +*/ +pub unsafe fn chash_new(mut size: libc::c_uint, mut flags: libc::c_int) -> *mut chash { + let mut h: *mut chash = 0 as *mut chash; + h = malloc(::std::mem::size_of::() as libc::size_t) as *mut chash; + if h.is_null() { + return 0 as *mut chash; + } + if size < 13i32 as libc::c_uint { + size = 13i32 as libc::c_uint + } + (*h).count = 0i32 as libc::c_uint; + (*h).cells = calloc( + size as libc::size_t, + ::std::mem::size_of::<*mut chashcell>() as libc::size_t, + ) as *mut *mut chashcell; + if (*h).cells.is_null() { + free(h as *mut libc::c_void); + return 0 as *mut chash; + } + (*h).size = size; + (*h).copykey = flags & 1i32; + (*h).copyvalue = flags & 2i32; + return h; +} + +/* Frees a hash */ +pub unsafe fn chash_free(mut hash: *mut chash) { + let mut indx: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut next: *mut chashiter = 0 as *mut chashiter; + indx = 0i32 as libc::c_uint; + while indx < (*hash).size { + iter = *(*hash).cells.offset(indx as isize); + while !iter.is_null() { + next = (*iter).next; + if 0 != (*hash).copykey { + free((*iter).key.data); + } + if 0 != (*hash).copyvalue { + free((*iter).value.data); + } + free(iter as *mut libc::c_void); + iter = next + } + indx = indx.wrapping_add(1) + } + free((*hash).cells as *mut libc::c_void); + free(hash as *mut libc::c_void); +} + +/* Removes all elements from a hash */ +pub unsafe fn chash_clear(mut hash: *mut chash) { + let mut indx: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut next: *mut chashiter = 0 as *mut chashiter; + indx = 0i32 as libc::c_uint; + while indx < (*hash).size { + iter = *(*hash).cells.offset(indx as isize); + while !iter.is_null() { + next = (*iter).next; + if 0 != (*hash).copykey { + free((*iter).key.data); + } + if 0 != (*hash).copyvalue { + free((*iter).value.data); + } + free(iter as *mut libc::c_void); + iter = next + } + indx = indx.wrapping_add(1) + } + memset( + (*hash).cells as *mut libc::c_void, + 0i32, + ((*hash).size as libc::size_t) + .wrapping_mul(::std::mem::size_of::<*mut chashcell>() as libc::size_t), + ); + (*hash).count = 0i32 as libc::c_uint; +} +/* Adds an entry in the hash table. +Length can be 0 if key/value are strings. +If an entry already exists for this key, it is replaced, and its value +is returned. Otherwise, the data pointer will be NULL and the length +field be set to TRUE or FALSe to indicate success or failure. */ +pub unsafe fn chash_set( + mut hash: *mut chash, + mut key: *mut chashdatum, + mut value: *mut chashdatum, + mut oldvalue: *mut chashdatum, +) -> libc::c_int { + let mut current_block: u64; + let mut func: libc::c_uint = 0; + let mut indx: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut cell: *mut chashiter = 0 as *mut chashiter; + let mut r: libc::c_int = 0; + if (*hash).count > (*hash).size.wrapping_mul(3i32 as libc::c_uint) { + r = chash_resize( + hash, + (*hash) + .count + .wrapping_div(3i32 as libc::c_uint) + .wrapping_mul(2i32 as libc::c_uint) + .wrapping_add(1i32 as libc::c_uint), + ); + if r < 0i32 { + current_block = 17701753836843438419; + } else { + current_block = 7095457783677275021; + } + } else { + current_block = 7095457783677275021; + } + match current_block { + 7095457783677275021 => { + func = chash_func((*key).data as *const libc::c_char, (*key).len); + indx = func.wrapping_rem((*hash).size); + iter = *(*hash).cells.offset(indx as isize); + loop { + if iter.is_null() { + current_block = 17788412896529399552; + break; + } + if (*iter).key.len == (*key).len + && (*iter).func == func + && 0 == memcmp((*iter).key.data, (*key).data, (*key).len as libc::size_t) + { + /* found, replacing entry */ + if 0 != (*hash).copyvalue { + let mut data: *mut libc::c_char = 0 as *mut libc::c_char; + data = chash_dup((*value).data, (*value).len); + if data.is_null() { + current_block = 17701753836843438419; + break; + } + free((*iter).value.data); + (*iter).value.data = data as *mut libc::c_void; + (*iter).value.len = (*value).len + } else { + if !oldvalue.is_null() { + (*oldvalue).data = (*iter).value.data; + (*oldvalue).len = (*iter).value.len + } + (*iter).value.data = (*value).data; + (*iter).value.len = (*value).len + } + if 0 == (*hash).copykey { + (*iter).key.data = (*key).data + } + if !oldvalue.is_null() { + (*oldvalue).data = (*value).data; + (*oldvalue).len = (*value).len + } + return 0i32; + } else { + iter = (*iter).next + } + } + match current_block { + 17701753836843438419 => {} + _ => { + if !oldvalue.is_null() { + (*oldvalue).data = 0 as *mut libc::c_void; + (*oldvalue).len = 0i32 as libc::c_uint + } + cell = malloc(::std::mem::size_of::() as libc::size_t) + as *mut chashcell; + if !cell.is_null() { + if 0 != (*hash).copykey { + (*cell).key.data = + chash_dup((*key).data, (*key).len) as *mut libc::c_void; + if (*cell).key.data.is_null() { + current_block = 4267898785354516004; + } else { + current_block = 7226443171521532240; + } + } else { + (*cell).key.data = (*key).data; + current_block = 7226443171521532240; + } + match current_block { + 7226443171521532240 => { + (*cell).key.len = (*key).len; + if 0 != (*hash).copyvalue { + (*cell).value.data = + chash_dup((*value).data, (*value).len) as *mut libc::c_void; + if (*cell).value.data.is_null() { + if 0 != (*hash).copykey { + free((*cell).key.data); + } + current_block = 4267898785354516004; + } else { + current_block = 6717214610478484138; + } + } else { + (*cell).value.data = (*value).data; + current_block = 6717214610478484138; + } + match current_block { + 4267898785354516004 => {} + _ => { + (*cell).value.len = (*value).len; + (*cell).func = func; + (*cell).next = *(*hash).cells.offset(indx as isize); + let ref mut fresh0 = *(*hash).cells.offset(indx as isize); + *fresh0 = cell; + (*hash).count = (*hash).count.wrapping_add(1); + return 0i32; + } + } + } + _ => {} + } + free(cell as *mut libc::c_void); + } + } + } + } + _ => {} + } + return -1i32; +} +#[inline] +unsafe fn chash_dup(mut data: *const libc::c_void, mut len: libc::c_uint) -> *mut libc::c_char { + let mut r: *mut libc::c_void = 0 as *mut libc::c_void; + r = malloc(len as libc::size_t) as *mut libc::c_char as *mut libc::c_void; + if r.is_null() { + return 0 as *mut libc::c_char; + } + memcpy(r, data, len as libc::size_t); + return r as *mut libc::c_char; +} + +#[inline] +unsafe fn chash_func(mut key: *const libc::c_char, mut len: libc::c_uint) -> libc::c_uint { + let mut c: libc::c_uint = 5381i32 as libc::c_uint; + let mut k: *const libc::c_char = key; + loop { + let fresh1 = len; + len = len.wrapping_sub(1); + if !(0 != fresh1) { + break; + } + let fresh2 = k; + k = k.offset(1); + c = (c << 5i32) + .wrapping_add(c) + .wrapping_add(*fresh2 as libc::c_uint) + } + return c; +} + +/* Resizes the hash table to the passed size. */ +pub unsafe fn chash_resize(mut hash: *mut chash, mut size: libc::c_uint) -> libc::c_int { + let mut cells: *mut *mut chashcell = 0 as *mut *mut chashcell; + let mut indx: libc::c_uint = 0; + let mut nindx: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut next: *mut chashiter = 0 as *mut chashiter; + if (*hash).size == size { + return 0i32; + } + cells = calloc( + size as libc::size_t, + ::std::mem::size_of::<*mut chashcell>() as libc::size_t, + ) as *mut *mut chashcell; + if cells.is_null() { + return -1i32; + } + indx = 0i32 as libc::c_uint; + while indx < (*hash).size { + iter = *(*hash).cells.offset(indx as isize); + while !iter.is_null() { + next = (*iter).next; + nindx = (*iter).func.wrapping_rem(size); + (*iter).next = *cells.offset(nindx as isize); + let ref mut fresh3 = *cells.offset(nindx as isize); + *fresh3 = iter; + iter = next + } + indx = indx.wrapping_add(1) + } + free((*hash).cells as *mut libc::c_void); + (*hash).size = size; + (*hash).cells = cells; + return 0i32; +} + +/* Retrieves the data associated to the key if it is found in the hash table. +The data pointer and the length will be NULL if not found*/ +pub unsafe fn chash_get( + mut hash: *mut chash, + mut key: *mut chashdatum, + mut result: *mut chashdatum, +) -> libc::c_int { + let mut func: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + func = chash_func((*key).data as *const libc::c_char, (*key).len); + iter = *(*hash) + .cells + .offset(func.wrapping_rem((*hash).size) as isize); + while !iter.is_null() { + if (*iter).key.len == (*key).len + && (*iter).func == func + && 0 == memcmp((*iter).key.data, (*key).data, (*key).len as libc::size_t) + { + *result = (*iter).value; + return 0i32; + } + iter = (*iter).next + } + return -1i32; +} +/* Removes the entry associated to this key if it is found in the hash table, +and returns its contents if not dupped (otherwise, pointer will be NULL +and len TRUE). If entry is not found both pointer and len will be NULL. */ +pub unsafe fn chash_delete( + mut hash: *mut chash, + mut key: *mut chashdatum, + mut oldvalue: *mut chashdatum, +) -> libc::c_int { + /* chashdatum result = { NULL, TRUE }; */ + let mut func: libc::c_uint = 0; + let mut indx: libc::c_uint = 0; + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut old: *mut chashiter = 0 as *mut chashiter; + func = chash_func((*key).data as *const libc::c_char, (*key).len); + indx = func.wrapping_rem((*hash).size); + old = 0 as *mut chashiter; + iter = *(*hash).cells.offset(indx as isize); + while !iter.is_null() { + if (*iter).key.len == (*key).len + && (*iter).func == func + && 0 == memcmp((*iter).key.data, (*key).data, (*key).len as libc::size_t) + { + if !old.is_null() { + (*old).next = (*iter).next + } else { + let ref mut fresh4 = *(*hash).cells.offset(indx as isize); + *fresh4 = (*iter).next + } + if 0 != (*hash).copykey { + free((*iter).key.data); + } + if 0 != (*hash).copyvalue { + free((*iter).value.data); + } else if !oldvalue.is_null() { + (*oldvalue).data = (*iter).value.data; + (*oldvalue).len = (*iter).value.len + } + free(iter as *mut libc::c_void); + (*hash).count = (*hash).count.wrapping_sub(1); + return 0i32; + } + old = iter; + iter = (*iter).next + } + return -1i32; +} +/* Returns an iterator to the first non-empty entry of the hash table */ +pub unsafe fn chash_begin(mut hash: *mut chash) -> *mut chashiter { + let mut iter: *mut chashiter = 0 as *mut chashiter; + let mut indx: libc::c_uint = 0i32 as libc::c_uint; + iter = *(*hash).cells.offset(0isize); + while iter.is_null() { + indx = indx.wrapping_add(1); + if indx >= (*hash).size { + return 0 as *mut chashiter; + } + iter = *(*hash).cells.offset(indx as isize) + } + return iter; +} +/* Returns the next non-empty entry of the hash table */ +pub unsafe fn chash_next(mut hash: *mut chash, mut iter: *mut chashiter) -> *mut chashiter { + let mut indx: libc::c_uint = 0; + if iter.is_null() { + return 0 as *mut chashiter; + } + indx = (*iter).func.wrapping_rem((*hash).size); + iter = (*iter).next; + while iter.is_null() { + indx = indx.wrapping_add(1); + if indx >= (*hash).size { + return 0 as *mut chashiter; + } + iter = *(*hash).cells.offset(indx as isize) + } + return iter; +} diff --git a/mmime/src/clist.rs b/mmime/src/clist.rs new file mode 100644 index 000000000..eb228d8b9 --- /dev/null +++ b/mmime/src/clist.rs @@ -0,0 +1,202 @@ +use libc; + +use crate::other::*; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct clistcell { + pub data: *mut libc::c_void, + pub previous: *mut clistcell, + pub next: *mut clistcell, +} + +#[derive(Clone)] +#[repr(C)] +pub struct clist { + pub first: *mut clistcell, + pub last: *mut clistcell, + pub count: libc::c_int, +} + +impl Default for clist { + fn default() -> Self { + Self { + first: std::ptr::null_mut(), + last: std::ptr::null_mut(), + count: 0, + } + } +} + +impl Drop for clist { + fn drop(&mut self) { + unsafe { + let mut l1 = self.first; + while !l1.is_null() { + let l2 = (*l1).next; + free(l1 as *mut libc::c_void); + l1 = l2 + } + } + } +} + +pub type clistiter = clistcell; +pub struct CListIterator { + cur: *mut clistiter, +} +impl Iterator for CListIterator { + type Item = *mut libc::c_void; + fn next(&mut self) -> Option { + unsafe { + if self.cur.is_null() { + None + } else { + let data = (*self.cur).data; + self.cur = (*self.cur).next; + Some(data) + } + } + } +} + +impl IntoIterator for &clist { + type Item = *mut libc::c_void; + type IntoIter = CListIterator; + fn into_iter(self) -> Self::IntoIter { + return CListIterator { cur: self.first }; + } +} + +pub type clist_func = + Option ()>; + +/* Allocate a new pointer list */ +pub fn clist_new() -> *mut clist { + Box::into_raw(Box::new(Default::default())) +} +/* Destroys a list. Data pointed by data pointers is NOT freed. */ +pub unsafe fn clist_free(mut lst: *mut clist) { + Box::from_raw(lst); +} +/* Inserts this data pointer after the element pointed by the iterator */ +pub unsafe fn clist_insert_after( + mut lst: *mut clist, + mut iter: *mut clistiter, + mut data: *mut libc::c_void, +) -> libc::c_int { + let mut c: *mut clistcell = 0 as *mut clistcell; + c = malloc(::std::mem::size_of::() as libc::size_t) as *mut clistcell; + if c.is_null() { + return -1i32; + } + (*c).data = data; + (*lst).count += 1; + if (*lst).first == (*lst).last && (*lst).last.is_null() { + (*c).next = 0 as *mut clistcell; + (*c).previous = (*c).next; + (*lst).last = c; + (*lst).first = (*lst).last; + return 0i32; + } + if iter.is_null() { + (*c).previous = (*lst).last; + (*(*c).previous).next = c; + (*c).next = 0 as *mut clistcell; + (*lst).last = c; + return 0i32; + } + (*c).previous = iter; + (*c).next = (*iter).next; + if !(*c).next.is_null() { + (*(*c).next).previous = c + } else { + (*lst).last = c + } + (*(*c).previous).next = c; + return 0i32; +} +/* Deletes the element pointed by the iterator. +Returns an iterator to the next element. */ +pub unsafe fn clist_delete(mut lst: *mut clist, mut iter: *mut clistiter) -> *mut clistiter { + let mut ret: *mut clistiter = 0 as *mut clistiter; + if iter.is_null() { + return 0 as *mut clistiter; + } + if !(*iter).previous.is_null() { + (*(*iter).previous).next = (*iter).next + } else { + (*lst).first = (*iter).next + } + if !(*iter).next.is_null() { + (*(*iter).next).previous = (*iter).previous; + ret = (*iter).next + } else { + (*lst).last = (*iter).previous; + ret = 0 as *mut clistiter + } + free(iter as *mut libc::c_void); + (*lst).count -= 1; + return ret; +} +pub unsafe fn clist_foreach( + mut lst: *mut clist, + mut func: clist_func, + mut data: *mut libc::c_void, +) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*lst).first; + while !cur.is_null() { + func.expect("non-null function pointer")((*cur).data, data); + cur = (*cur).next + } +} + +pub unsafe fn clist_nth_data(mut lst: *mut clist, mut indx: libc::c_int) -> *mut libc::c_void { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = internal_clist_nth(lst, indx); + if cur.is_null() { + return 0 as *mut libc::c_void; + } + return (*cur).data; +} +#[inline] +unsafe fn internal_clist_nth(mut lst: *mut clist, mut indx: libc::c_int) -> *mut clistiter { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*lst).first; + while indx > 0i32 && !cur.is_null() { + cur = (*cur).next; + indx -= 1 + } + if cur.is_null() { + return 0 as *mut clistiter; + } + return cur; +} + +pub unsafe fn clist_nth(mut lst: *mut clist, mut indx: libc::c_int) -> *mut clistiter { + return internal_clist_nth(lst, indx); +} + +#[cfg(test)] +mod tests { + use super::*; + use std::ptr; + #[test] + fn test_clist_iterator() { + unsafe { + let mut c = clist_new(); + assert!(!c.is_null()); + clist_insert_after(c, ptr::null_mut(), clist_nth as _); + assert_eq!((*c).count, 1); + + /* Only one iteration */ + for data in &*c { + assert_eq!(data, clist_nth as _); + } + assert_eq!((*c).count, 1); + + clist_free(c); + } + } +} diff --git a/mmime/src/lib.rs b/mmime/src/lib.rs new file mode 100644 index 000000000..be8e049b3 --- /dev/null +++ b/mmime/src/lib.rs @@ -0,0 +1,478 @@ +#![deny(clippy::correctness)] +// TODO: make all of these errors, such that clippy actually passes. +#![warn(clippy::all, clippy::perf, clippy::not_unsafe_ptr_arg_deref)] +// This is nice, but for now just annoying. +#![allow(clippy::unreadable_literal)] +#![feature(ptr_wrapping_offset_from)] +#![allow(unused_attributes)] +#![allow(unused_variables)] +#![allow(mutable_transmutes)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(unused_assignments)] +#![allow(unused_mut)] +#![allow(unused_must_use)] +#![feature(extern_types)] +#![feature(const_raw_ptr_to_usize_cast)] + +pub mod charconv; +pub mod chash; +pub mod clist; +pub mod mailimf; +pub mod mailimf_types; +pub mod mailimf_types_helper; +pub mod mailimf_write_generic; +pub mod mailmime; +pub mod mailmime_content; +pub mod mailmime_decode; +pub mod mailmime_disposition; +pub mod mailmime_types; +pub mod mailmime_types_helper; +pub mod mailmime_write_generic; +pub mod mailmime_write_mem; +pub mod mmapstring; +pub mod other; + +pub use self::charconv::*; +pub use self::chash::*; +pub use self::clist::*; +pub use self::mailimf::*; +pub use self::mailimf_types::*; +pub use self::mailimf_types_helper::*; +pub use self::mailimf_write_generic::*; +pub use self::mailmime::*; +pub use self::mailmime_content::*; +pub use self::mailmime_decode::*; +pub use self::mailmime_disposition::*; +pub use self::mailmime_types::*; +pub use self::mailmime_types_helper::*; +pub use self::mailmime_write_generic::*; +pub use self::mailmime_write_mem::*; +pub use self::mmapstring::*; +pub use self::other::*; + +#[cfg(test)] +mod tests { + use super::*; + use crate::mailmime_types::{mailmime, mailmime_content, mailmime_disposition}; + use std::ffi::CStr; + + #[test] + fn mailmime_parse_test() { + unsafe { + let data = "MIME-Version: 1.0\ + Content-Type: multipart/mixed; boundary=frontier\ + \ + This is a message with multiple parts in MIME format.\ + --frontier\ + Content-Type: text/plain\ + \ + This is the body of the message.\ + --frontier\ + Content-Type: application/octet-stream\ + Content-Transfer-Encoding: base64\ + \ + PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\ + Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\ + --frontier--"; + let c_data = std::ffi::CString::new(data).unwrap(); + + let mut current_index = 0; + let mut mime = std::ptr::null_mut(); + let res = crate::mailmime_content::mailmime_parse( + c_data.as_ptr(), + data.len() as usize, + &mut current_index, + &mut mime, + ); + + assert_eq!(res, MAIL_NO_ERROR as libc::c_int); + assert!(!mime.is_null()); + + display_mime(mime); + + mailmime_types::mailmime_free(mime); + } + } + + unsafe fn display_mime(mut mime: *mut mailmime) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + println!("{}", (*mime).mm_type); + + match (*mime).mm_type { + 1 => { + println!("single part"); + } + 2 => { + println!("multipart"); + } + 3 => println!("message"), + _ => {} + } + if !(*mime).mm_mime_fields.is_null() { + if !(*(*(*mime).mm_mime_fields).fld_list).first.is_null() { + print!("MIME headers begin"); + display_mime_fields((*mime).mm_mime_fields); + println!("MIME headers end"); + } + } + display_mime_content((*mime).mm_content_type); + match (*mime).mm_type { + 1 => { + display_mime_data((*mime).mm_data.mm_single); + } + 2 => { + cur = (*(*mime).mm_data.mm_multipart.mm_mp_list).first; + while !cur.is_null() { + display_mime( + (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime, + ); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + 3 => { + if !(*mime).mm_data.mm_message.mm_fields.is_null() { + if !(*(*(*mime).mm_data.mm_message.mm_fields).fld_list) + .first + .is_null() + { + println!("headers begin"); + display_fields((*mime).mm_data.mm_message.mm_fields); + println!("headers end"); + } + if !(*mime).mm_data.mm_message.mm_msg_mime.is_null() { + display_mime((*mime).mm_data.mm_message.mm_msg_mime); + } + } + } + _ => {} + }; + } + + unsafe fn display_mime_content(mut content_type: *mut mailmime_content) { + print!("type: "); + display_mime_type((*content_type).ct_type); + println!( + "/{}", + CStr::from_ptr((*content_type).ct_subtype).to_str().unwrap() + ); + } + unsafe fn display_mime_type(mut type_0: *mut mailmime_type) { + match (*type_0).tp_type { + 1 => { + display_mime_discrete_type((*type_0).tp_data.tp_discrete_type); + } + 2 => { + display_mime_composite_type((*type_0).tp_data.tp_composite_type); + } + _ => {} + }; + } + unsafe fn display_mime_composite_type(mut ct: *mut mailmime_composite_type) { + match (*ct).ct_type { + 1 => { + print!("message"); + } + 2 => { + print!("multipart"); + } + 3 => { + print!("{}", CStr::from_ptr((*ct).ct_token).to_str().unwrap()); + } + _ => {} + }; + } + unsafe fn display_mime_discrete_type(mut discrete_type: *mut mailmime_discrete_type) { + match (*discrete_type).dt_type { + 1 => { + print!("text"); + } + 2 => { + print!("image"); + } + 3 => { + print!("audio"); + } + 4 => { + print!("video"); + } + 5 => { + print!("application"); + } + 6 => { + print!("{}", (*discrete_type).dt_extension as u8 as char); + } + _ => {} + }; + } + unsafe fn display_mime_data(mut data: *mut mailmime_data) { + match (*data).dt_type { + 0 => { + println!( + "data : {} bytes", + (*data).dt_data.dt_text.dt_length as libc::c_uint, + ); + } + 1 => { + println!( + "data (file) : {}", + CStr::from_ptr((*data).dt_data.dt_filename) + .to_str() + .unwrap() + ); + } + _ => {} + }; + } + unsafe fn display_mime_dsp_parm(mut param: *mut mailmime_disposition_parm) { + match (*param).pa_type { + 0 => { + println!( + "filename: {}", + CStr::from_ptr((*param).pa_data.pa_filename) + .to_str() + .unwrap() + ); + } + _ => {} + }; + } + unsafe fn display_mime_disposition(mut disposition: *mut mailmime_disposition) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*disposition).dsp_parms).first; + while !cur.is_null() { + let mut param: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + param = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_disposition_parm; + display_mime_dsp_parm(param); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + unsafe fn display_mime_field(mut field: *mut mailmime_field) { + match (*field).fld_type { + 1 => { + print!("content-type: "); + display_mime_content((*field).fld_data.fld_content); + println!(""); + } + 6 => { + display_mime_disposition((*field).fld_data.fld_disposition); + } + _ => {} + }; + } + unsafe fn display_mime_fields(mut fields: *mut mailmime_fields) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_field; + display_mime_field(field); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + unsafe fn display_date_time(mut d: *mut mailimf_date_time) { + print!( + "{:02}/{:02}/{:02} {:02}:{:02}:{:02} +{:04}", + (*d).dt_day, + (*d).dt_month, + (*d).dt_year, + (*d).dt_hour, + (*d).dt_min, + (*d).dt_sec, + (*d).dt_zone, + ); + } + unsafe fn display_orig_date(mut orig_date: *mut mailimf_orig_date) { + display_date_time((*orig_date).dt_date_time); + } + unsafe fn display_mailbox(mut mb: *mut mailimf_mailbox) { + if !(*mb).mb_display_name.is_null() { + print!( + "{}", + CStr::from_ptr((*mb).mb_display_name).to_str().unwrap() + ); + } + print!("<{}>", CStr::from_ptr((*mb).mb_addr_spec).to_str().unwrap()); + } + unsafe fn display_mailbox_list(mut mb_list: *mut mailimf_mailbox_list) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*mb_list).mb_list).first; + while !cur.is_null() { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + mb = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_mailbox; + display_mailbox(mb); + if !if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + .is_null() + { + print!(", "); + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + unsafe fn display_group(mut group: *mut mailimf_group) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + print!( + "{}: ", + CStr::from_ptr((*group).grp_display_name).to_str().unwrap() + ); + cur = (*(*(*group).grp_mb_list).mb_list).first; + while !cur.is_null() { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + mb = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_mailbox; + display_mailbox(mb); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + print!("; "); + } + unsafe fn display_address(mut a: *mut mailimf_address) { + match (*a).ad_type { + 2 => { + display_group((*a).ad_data.ad_group); + } + 1 => { + display_mailbox((*a).ad_data.ad_mailbox); + } + _ => {} + }; + } + unsafe fn display_address_list(mut addr_list: *mut mailimf_address_list) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*addr_list).ad_list).first; + while !cur.is_null() { + let mut addr: *mut mailimf_address = 0 as *mut mailimf_address; + addr = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_address; + display_address(addr); + if !if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + .is_null() + { + print!(", "); + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + unsafe fn display_from(mut from: *mut mailimf_from) { + display_mailbox_list((*from).frm_mb_list); + } + unsafe fn display_to(mut to: *mut mailimf_to) { + display_address_list((*to).to_addr_list); + } + unsafe fn display_cc(mut cc: *mut mailimf_cc) { + display_address_list((*cc).cc_addr_list); + } + unsafe fn display_subject(mut subject: *mut mailimf_subject) { + print!("{}", CStr::from_ptr((*subject).sbj_value).to_str().unwrap()); + } + unsafe fn display_field(mut field: *mut mailimf_field) { + match (*field).fld_type { + 9 => { + print!("Date: "); + display_orig_date((*field).fld_data.fld_orig_date); + println!(""); + } + 10 => { + print!("From: "); + display_from((*field).fld_data.fld_from); + println!(""); + } + 13 => { + print!("To: "); + display_to((*field).fld_data.fld_to); + println!(""); + } + 14 => { + print!("Cc: "); + display_cc((*field).fld_data.fld_cc); + println!(""); + } + 19 => { + print!("Subject: "); + display_subject((*field).fld_data.fld_subject); + println!(""); + } + 16 => { + println!( + "Message-ID: {}", + CStr::from_ptr((*(*field).fld_data.fld_message_id).mid_value) + .to_str() + .unwrap(), + ); + } + _ => {} + }; + } + unsafe fn display_fields(mut fields: *mut mailimf_fields) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut f: *mut mailimf_field = 0 as *mut mailimf_field; + f = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_field; + display_field(f); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } +} diff --git a/mmime/src/mailimf.rs b/mmime/src/mailimf.rs new file mode 100644 index 000000000..d94a2b0b4 --- /dev/null +++ b/mmime/src/mailimf.rs @@ -0,0 +1,5918 @@ +use libc; +use libc::toupper; + +use crate::clist::*; +use crate::mailimf_types::*; +use crate::mailmime_decode::*; +use crate::mailmime_types::*; +use crate::mmapstring::*; +use crate::other::*; + +pub const UNSTRUCTURED_START: libc::c_uint = 0; +pub const UNSTRUCTURED_LF: libc::c_uint = 2; +pub const UNSTRUCTURED_CR: libc::c_uint = 1; +pub const UNSTRUCTURED_WSP: libc::c_uint = 3; +pub const UNSTRUCTURED_OUT: libc::c_uint = 4; + +pub const STATE_ZONE_ERR: libc::c_uint = 4; +pub const STATE_ZONE_OK: libc::c_uint = 3; +pub const STATE_ZONE_3: libc::c_uint = 2; +pub const STATE_ZONE_2: libc::c_uint = 1; +pub const STATE_ZONE_CONT: libc::c_uint = 5; +pub const STATE_ZONE_1: libc::c_uint = 0; + +pub const MONTH_A: libc::c_uint = 5; +pub const MONTH_MA: libc::c_uint = 4; +pub const MONTH_M: libc::c_uint = 3; +pub const MONTH_JU: libc::c_uint = 2; +pub const MONTH_J: libc::c_uint = 1; +pub const MONTH_START: libc::c_uint = 0; +pub const DAY_NAME_S: libc::c_uint = 2; +pub const DAY_NAME_T: libc::c_uint = 1; +pub const DAY_NAME_START: libc::c_uint = 0; +pub const HEADER_RES: libc::c_uint = 5; +pub const HEADER_S: libc::c_uint = 4; +pub const HEADER_RE: libc::c_uint = 3; +pub const HEADER_R: libc::c_uint = 2; +pub const HEADER_C: libc::c_uint = 1; +pub const HEADER_START: libc::c_uint = 0; + +/* +day-name = "Mon" / "Tue" / "Wed" / "Thu" / + "Fri" / "Sat" / "Sun" +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_token_value { + pub value: libc::c_int, + pub str_0: *mut libc::c_char, +} + +/* + mailimf_message_parse will parse the given message + + @param message this is a string containing the message content + @param length this is the size of the given string + @param indx this is a pointer to the start of the message in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_message_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_message, +) -> libc::c_int { + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut body: *mut mailimf_body = 0 as *mut mailimf_body; + let mut msg: *mut mailimf_message = 0 as *mut mailimf_message; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_fields_parse(message, length, &mut cur_token, &mut fields); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_body_parse(message, length, &mut cur_token, &mut body); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + msg = mailimf_message_new(fields, body); + if msg.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mailimf_body_free(body); + } else { + *indx = cur_token; + *result = msg; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_fields_free(fields); + } + } + return res; +} +/* + mailimf_body_parse will parse the given text part of a message + + @param message this is a string containing the message text part + @param length this is the size of the given string + @param indx this is a pointer to the start of the message text part in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_body_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_body, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut body: *mut mailimf_body = 0 as *mut mailimf_body; + cur_token = *indx; + body = mailimf_body_new( + message.offset(cur_token as isize), + length.wrapping_sub(cur_token), + ); + if body.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + cur_token = length; + *result = body; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_crlf_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_char_parse(message, length, &mut cur_token, '\r' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_char_parse(message, length, &mut cur_token, '\n' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_char_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut token: libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if *message.offset(cur_token as isize) as libc::c_int == token as libc::c_int { + cur_token = cur_token.wrapping_add(1); + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } else { + return MAILIMF_ERROR_PARSE as libc::c_int; + }; +} +/* + mailimf_fields_parse will parse the given header fields + + @param message this is a string containing the header fields + @param length this is the size of the given string + @param indx this is a pointer to the start of the header fields in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_fields_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_fields, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + list = 0 as *mut clist; + r = mailimf_struct_multiple_parse( + message, + length, + &mut cur_token, + &mut list, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut mailimf_field, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_field_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_field_free)), + ); + /* + if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { + res = r; + goto err; + } + */ + match r { + 0 => { + /* do nothing */ + current_block = 11050875288958768710; + } + 1 => { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 6724962012950341805; + } else { + current_block = 11050875288958768710; + } + } + _ => { + res = r; + current_block = 6724962012950341805; + } + } + match current_block { + 11050875288958768710 => { + fields = mailimf_fields_new(list); + if fields.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !list.is_null() { + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_field_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } else { + *result = fields; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + return res; +} +unsafe fn mailimf_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_field, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut type_0: libc::c_int = 0; + let mut return_path: *mut mailimf_return = 0 as *mut mailimf_return; + let mut resent_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut resent_from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut resent_sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut resent_to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut resent_cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut resent_bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut resent_msg_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut orig_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut reply_to: *mut mailimf_reply_to = 0 as *mut mailimf_reply_to; + let mut to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut message_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut in_reply_to: *mut mailimf_in_reply_to = 0 as *mut mailimf_in_reply_to; + let mut references: *mut mailimf_references = 0 as *mut mailimf_references; + let mut subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + let mut comments: *mut mailimf_comments = 0 as *mut mailimf_comments; + let mut keywords: *mut mailimf_keywords = 0 as *mut mailimf_keywords; + let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut guessed_type: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + return_path = 0 as *mut mailimf_return; + resent_date = 0 as *mut mailimf_orig_date; + resent_from = 0 as *mut mailimf_from; + resent_sender = 0 as *mut mailimf_sender; + resent_to = 0 as *mut mailimf_to; + resent_cc = 0 as *mut mailimf_cc; + resent_bcc = 0 as *mut mailimf_bcc; + resent_msg_id = 0 as *mut mailimf_message_id; + orig_date = 0 as *mut mailimf_orig_date; + from = 0 as *mut mailimf_from; + sender = 0 as *mut mailimf_sender; + reply_to = 0 as *mut mailimf_reply_to; + to = 0 as *mut mailimf_to; + cc = 0 as *mut mailimf_cc; + bcc = 0 as *mut mailimf_bcc; + message_id = 0 as *mut mailimf_message_id; + in_reply_to = 0 as *mut mailimf_in_reply_to; + references = 0 as *mut mailimf_references; + subject = 0 as *mut mailimf_subject; + comments = 0 as *mut mailimf_comments; + keywords = 0 as *mut mailimf_keywords; + optional_field = 0 as *mut mailimf_optional_field; + guessed_type = guess_header_type(message, length, cur_token); + type_0 = MAILIMF_FIELD_NONE as libc::c_int; + match guessed_type { + 9 => { + r = mailimf_orig_date_parse(message, length, &mut cur_token, &mut orig_date); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILIMF_FIELD_ORIG_DATE as libc::c_int; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 10 => { + r = mailimf_from_parse(message, length, &mut cur_token, &mut from); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 11 => { + r = mailimf_sender_parse(message, length, &mut cur_token, &mut sender); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 12 => { + r = mailimf_reply_to_parse(message, length, &mut cur_token, &mut reply_to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 13 => { + r = mailimf_to_parse(message, length, &mut cur_token, &mut to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 14 => { + r = mailimf_cc_parse(message, length, &mut cur_token, &mut cc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 15 => { + r = mailimf_bcc_parse(message, length, &mut cur_token, &mut bcc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 16 => { + r = mailimf_message_id_parse(message, length, &mut cur_token, &mut message_id); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 17 => { + r = mailimf_in_reply_to_parse(message, length, &mut cur_token, &mut in_reply_to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 18 => { + r = mailimf_references_parse(message, length, &mut cur_token, &mut references); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 19 => { + r = mailimf_subject_parse(message, length, &mut cur_token, &mut subject); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 20 => { + r = mailimf_comments_parse(message, length, &mut cur_token, &mut comments); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 21 => { + r = mailimf_keywords_parse(message, length, &mut cur_token, &mut keywords); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 1 => { + r = mailimf_return_parse(message, length, &mut cur_token, &mut return_path); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 2 => { + r = mailimf_resent_date_parse(message, length, &mut cur_token, &mut resent_date); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 3 => { + r = mailimf_resent_from_parse(message, length, &mut cur_token, &mut resent_from); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 4 => { + r = mailimf_resent_sender_parse(message, length, &mut cur_token, &mut resent_sender); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 5 => { + r = mailimf_resent_to_parse(message, length, &mut cur_token, &mut resent_to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 6 => { + r = mailimf_resent_cc_parse(message, length, &mut cur_token, &mut resent_cc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 7 => { + r = mailimf_resent_bcc_parse(message, length, &mut cur_token, &mut resent_bcc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + 8 => { + r = mailimf_resent_msg_id_parse(message, length, &mut cur_token, &mut resent_msg_id); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 9846950269610550213; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9846950269610550213; + } else { + /* do nothing */ + res = r; + current_block = 10956553358380301606; + } + } + _ => { + current_block = 9846950269610550213; + } + } + match current_block { + 9846950269610550213 => { + if type_0 == MAILIMF_FIELD_NONE as libc::c_int { + r = mailimf_optional_field_parse( + message, + length, + &mut cur_token, + &mut optional_field, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 10956553358380301606; + } else { + type_0 = MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int; + current_block = 2920409193602730479; + } + } else { + current_block = 2920409193602730479; + } + match current_block { + 10956553358380301606 => {} + _ => { + field = mailimf_field_new( + type_0, + return_path, + resent_date, + resent_from, + resent_sender, + resent_to, + resent_cc, + resent_bcc, + resent_msg_id, + orig_date, + from, + sender, + reply_to, + to, + cc, + bcc, + message_id, + in_reply_to, + references, + subject, + comments, + keywords, + optional_field, + ); + if field.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !return_path.is_null() { + mailimf_return_free(return_path); + } + if !resent_date.is_null() { + mailimf_orig_date_free(resent_date); + } + if !resent_from.is_null() { + mailimf_from_free(resent_from); + } + if !resent_sender.is_null() { + mailimf_sender_free(resent_sender); + } + if !resent_to.is_null() { + mailimf_to_free(resent_to); + } + if !resent_cc.is_null() { + mailimf_cc_free(resent_cc); + } + if !resent_bcc.is_null() { + mailimf_bcc_free(resent_bcc); + } + if !resent_msg_id.is_null() { + mailimf_message_id_free(resent_msg_id); + } + if !orig_date.is_null() { + mailimf_orig_date_free(orig_date); + } + if !from.is_null() { + mailimf_from_free(from); + } + if !sender.is_null() { + mailimf_sender_free(sender); + } + if !reply_to.is_null() { + mailimf_reply_to_free(reply_to); + } + if !to.is_null() { + mailimf_to_free(to); + } + if !cc.is_null() { + mailimf_cc_free(cc); + } + if !bcc.is_null() { + mailimf_bcc_free(bcc); + } + if !message_id.is_null() { + mailimf_message_id_free(message_id); + } + if !in_reply_to.is_null() { + mailimf_in_reply_to_free(in_reply_to); + } + if !references.is_null() { + mailimf_references_free(references); + } + if !subject.is_null() { + mailimf_subject_free(subject); + } + if !comments.is_null() { + mailimf_comments_free(comments); + } + if !keywords.is_null() { + mailimf_keywords_free(keywords); + } + if !optional_field.is_null() { + mailimf_optional_field_free(optional_field); + } + } else { + *result = field; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + _ => {} + } + return res; +} +unsafe fn mailimf_optional_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_optional_field, +) -> libc::c_int { + let mut name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_field_name_parse(message, length, &mut cur_token, &mut name); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstructured_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + optional_field = mailimf_optional_field_new(name, value); + if optional_field.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = optional_field; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_unstructured_free(value); + } + } + mailimf_field_name_free(name); + } + return res; +} +unsafe fn mailimf_unstrict_crlf_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + mailimf_cfws_parse(message, length, &mut cur_token); + r = mailimf_char_parse(message, length, &mut cur_token, '\r' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_char_parse(message, length, &mut cur_token, '\n' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_cfws_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut has_comment: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + has_comment = 0i32; + loop { + r = mailimf_cfws_fws_comment_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } else { + has_comment = 1i32 + } + } + if 0 == has_comment { + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* internal use, exported for MIME */ +pub unsafe fn mailimf_fws_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut final_token: size_t = 0; + let mut fws_1: libc::c_int = 0; + let mut fws_2: libc::c_int = 0; + let mut fws_3: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + fws_1 = 0i32; + loop { + r = mailimf_wsp_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } else { + fws_1 = 1i32 + } + } + final_token = cur_token; + r = mailimf_crlf_parse(message, length, &mut cur_token); + match r { + 0 => fws_2 = 1i32, + 1 => fws_2 = 0i32, + _ => return r, + } + fws_3 = 0i32; + if 0 != fws_2 { + loop { + r = mailimf_wsp_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } else { + fws_3 = 1i32 + } + } + } + if 0 == fws_1 && 0 == fws_3 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if 0 == fws_3 { + cur_token = final_token + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn mailimf_wsp_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if *message.offset(cur_token as isize) as libc::c_int != ' ' as i32 + && *message.offset(cur_token as isize) as libc::c_int != '\t' as i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + cur_token = cur_token.wrapping_add(1); + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +[FWS] comment +*/ +#[inline] +unsafe fn mailimf_cfws_fws_comment_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_comment_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn mailimf_comment_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_oparenth_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + loop { + r = mailimf_comment_fws_ccontent_parse(message, length, &mut cur_token); + if !(r != MAILIMF_NO_ERROR as libc::c_int) { + continue; + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_cparenth_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_cparenth_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_char_parse(message, length, indx, ')' as i32 as libc::c_char); +} +#[inline] +unsafe fn mailimf_comment_fws_ccontent_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_ccontent_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn mailimf_ccontent_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut ch: libc::c_char = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if 0 != is_ctext(*message.offset(cur_token as isize)) { + cur_token = cur_token.wrapping_add(1) + } else { + r = mailimf_quoted_pair_parse(message, length, &mut cur_token, &mut ch); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_comment_parse(message, length, &mut cur_token) + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn mailimf_quoted_pair_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token.wrapping_add(1i32 as libc::size_t) >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if *message.offset(cur_token as isize) as libc::c_int != '\\' as i32 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + cur_token = cur_token.wrapping_add(1); + *result = *message.offset(cur_token as isize); + cur_token = cur_token.wrapping_add(1); + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +ctext = NO-WS-CTL / ; Non white space controls + + %d33-39 / ; The rest of the US-ASCII + %d42-91 / ; characters not including "(", + %d93-126 ; ")", or "\" +*/ +#[inline] +unsafe fn is_ctext(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if 0 != is_no_ws_ctl(ch) { + return 1i32; + } + if (uch as libc::c_int) < 33i32 { + return 0i32; + } + if uch as libc::c_int == 40i32 || uch as libc::c_int == 41i32 { + return 0i32; + } + if uch as libc::c_int == 92i32 { + return 0i32; + } + if uch as libc::c_int == 127i32 { + return 0i32; + } + return 1i32; +} +/* ************************************************************************ */ +/* RFC 2822 grammar */ +/* +NO-WS-CTL = %d1-8 / ; US-ASCII control characters + %d11 / ; that do not include the + %d12 / ; carriage return, line feed, + %d14-31 / ; and white space characters + %d127 +*/ +#[inline] +unsafe fn is_no_ws_ctl(mut ch: libc::c_char) -> libc::c_int { + if ch as libc::c_int == 9i32 || ch as libc::c_int == 10i32 || ch as libc::c_int == 13i32 { + return 0i32; + } + if ch as libc::c_int == 127i32 { + return 1i32; + } + return (ch as libc::c_int >= 1i32 && ch as libc::c_int <= 31i32) as libc::c_int; +} +unsafe fn mailimf_oparenth_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_char_parse(message, length, indx, '(' as i32 as libc::c_char); +} +unsafe fn mailimf_unstructured_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut state: libc::c_int = 0; + let mut begin: size_t = 0; + let mut terminal: size_t = 0; + let mut str: *mut libc::c_char = 0 as *mut libc::c_char; + cur_token = *indx; + loop { + let mut r: libc::c_int = 0; + r = mailimf_wsp_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + continue; + } + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } + state = UNSTRUCTURED_START as libc::c_int; + begin = cur_token; + terminal = cur_token; + while state != UNSTRUCTURED_OUT as libc::c_int { + match state { + 0 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + terminal = cur_token; + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 1 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 2 => { + if cur_token >= length { + state = UNSTRUCTURED_OUT as libc::c_int + } else { + match *message.offset(cur_token as isize) as libc::c_int { + 9 | 32 => state = UNSTRUCTURED_WSP as libc::c_int, + _ => state = UNSTRUCTURED_OUT as libc::c_int, + } + } + } + 3 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + str = malloc( + terminal + .wrapping_sub(begin) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if str.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + strncpy( + str, + message.offset(begin as isize), + terminal.wrapping_sub(begin), + ); + *str.offset(terminal.wrapping_sub(begin) as isize) = '\u{0}' as i32 as libc::c_char; + *indx = terminal; + *result = str; + return MAILIMF_NO_ERROR as libc::c_int; +} + +unsafe fn mailimf_colon_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, ':' as i32 as libc::c_char); +} + +pub unsafe fn mailimf_unstrict_char_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut token: libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_char_parse(message, length, &mut cur_token, token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_field_name_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut field_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut end: size_t = 0; + cur_token = *indx; + end = cur_token; + if end >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + while 0 != is_ftext(*message.offset(end as isize)) { + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end == cur_token { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + field_name = malloc( + end.wrapping_sub(cur_token) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if field_name.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + strncpy( + field_name, + message.offset(cur_token as isize), + end.wrapping_sub(cur_token), + ); + *field_name.offset(end.wrapping_sub(cur_token) as isize) = '\u{0}' as i32 as libc::c_char; + cur_token = end; + *indx = cur_token; + *result = field_name; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +field-name = 1*ftext +*/ +#[inline] +unsafe fn is_ftext(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if (uch as libc::c_int) < 33i32 { + return 0i32; + } + if uch as libc::c_int == 58i32 { + return 0i32; + } + return 1i32; +} +unsafe fn mailimf_resent_msg_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_message_id, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut message_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-Message-ID\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-Message-ID\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_msg_id_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + message_id = mailimf_message_id_new(value); + if message_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = message_id; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_msg_id_free(value); + } + } + } + return res; +} + +pub unsafe fn mailimf_msg_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut msg_id: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_lower_parse(message, length, &mut cur_token); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_addr_spec_msg_id_parse(message, length, &mut cur_token, &mut msg_id); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + *result = msg_id; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } else if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_lower_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + current_block = 2668756484064249700; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 2668756484064249700; + } else { + // ok + res = r; + current_block = 9394595304415473402; + } + match current_block { + 9394595304415473402 => {} + _ => { + r = mailimf_addr_spec_msg_id_parse(message, length, &mut cur_token, &mut msg_id); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_greater_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + free(msg_id as *mut libc::c_void); + res = r + } else { + r = mailimf_greater_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int + || r == MAILIMF_ERROR_PARSE as libc::c_int + { + current_block = 6450636197030046351; + } else { + // ok + free(msg_id as *mut libc::c_void); + res = r; + current_block = 9394595304415473402; + } + match current_block { + 9394595304415473402 => {} + _ => { + *result = msg_id; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + } + } + } + return res; +} +unsafe fn mailimf_greater_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '>' as i32 as libc::c_char); +} +/* +for msg id +addr-spec = local-part "@" domain +*/ +unsafe fn mailimf_addr_spec_msg_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut addr_spec: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut begin: size_t = 0; + let mut end: size_t = 0; + let mut final_0: libc::c_int = 0; + let mut count: size_t = 0; + let mut src: *const libc::c_char = 0 as *const libc::c_char; + let mut dest: *mut libc::c_char = 0 as *mut libc::c_char; + let mut i: size_t = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + end = cur_token; + if end >= length { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + begin = cur_token; + final_0 = 0i32; + loop { + match *message.offset(end as isize) as libc::c_int { + 62 | 13 | 10 => final_0 = 1i32, + _ => {} + } + if 0 != final_0 { + break; + } + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end == begin { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + addr_spec = malloc( + end.wrapping_sub(cur_token) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if addr_spec.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + count = end.wrapping_sub(cur_token); + src = message.offset(cur_token as isize); + dest = addr_spec; + i = 0i32 as size_t; + while i < count { + if *src as libc::c_int != ' ' as i32 && *src as libc::c_int != '\t' as i32 { + *dest = *src; + dest = dest.offset(1isize) + } + src = src.offset(1isize); + i = i.wrapping_add(1) + } + *dest = '\u{0}' as i32 as libc::c_char; + cur_token = end; + *result = addr_spec; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + return res; +} +unsafe fn mailimf_lower_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '<' as i32 as libc::c_char); +} + +pub unsafe fn mailimf_token_case_insensitive_len_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut token: *mut libc::c_char, + mut token_length: size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token + .wrapping_add(token_length) + .wrapping_sub(1i32 as libc::size_t) + >= length + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if strncasecmp(message.offset(cur_token as isize), token, token_length) == 0i32 { + cur_token = (cur_token as libc::size_t).wrapping_add(token_length) as size_t as size_t; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } else { + return MAILIMF_ERROR_PARSE as libc::c_int; + }; +} +unsafe fn mailimf_resent_bcc_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_bcc, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + bcc = 0 as *mut mailimf_bcc; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-Bcc\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-Bcc\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + addr_list = 0 as *mut mailimf_address_list; + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + bcc = mailimf_bcc_new(addr_list); + if bcc.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = bcc; + *indx = cur_token; + return 1i32; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +/* + mailimf_address_list_parse will parse the given address list + + @param message this is a string containing the address list + @param length this is the size of the given string + @param indx this is a pointer to the start of the address list in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_address_list_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_address_list, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut address_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_struct_list_parse( + message, + length, + &mut cur_token, + &mut list, + ',' as i32 as libc::c_char, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut mailimf_address, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_address_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_address_free)), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + address_list = mailimf_address_list_new(list); + if address_list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + clist_foreach( + list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_address_free), + ), + 0 as *mut libc::c_void, + ); + clist_free(list); + } else { + *result = address_list; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +/* + mailimf_address_parse will parse the given address + + @param message this is a string containing the address + @param length this is the size of the given string + @param indx this is a pointer to the start of the address in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_address_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_address, +) -> libc::c_int { + let mut type_0: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut mailbox: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + let mut group: *mut mailimf_group = 0 as *mut mailimf_group; + let mut address: *mut mailimf_address = 0 as *mut mailimf_address; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + mailbox = 0 as *mut mailimf_mailbox; + group = 0 as *mut mailimf_group; + type_0 = MAILIMF_ADDRESS_ERROR as libc::c_int; + r = mailimf_group_parse(message, length, &mut cur_token, &mut group); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILIMF_ADDRESS_GROUP as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_mailbox_parse(message, length, &mut cur_token, &mut mailbox); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILIMF_ADDRESS_MAILBOX as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + address = mailimf_address_new(type_0, mailbox, group); + if address.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !mailbox.is_null() { + mailimf_mailbox_free(mailbox); + } + if !group.is_null() { + mailimf_group_free(group); + } + } else { + *result = address; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +/* + mailimf_mailbox_parse will parse the given address + + @param message this is a string containing the mailbox + @param length this is the size of the given string + @param indx this is a pointer to the start of the mailbox in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_mailbox_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_mailbox, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut display_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut mailbox: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + let mut addr_spec: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + display_name = 0 as *mut libc::c_char; + addr_spec = 0 as *mut libc::c_char; + r = mailimf_name_addr_parse( + message, + length, + &mut cur_token, + &mut display_name, + &mut addr_spec, + ); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_addr_spec_parse(message, length, &mut cur_token, &mut addr_spec) + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + mailbox = mailimf_mailbox_new(display_name, addr_spec); + if mailbox.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !display_name.is_null() { + mailimf_display_name_free(display_name); + } + if !addr_spec.is_null() { + mailimf_addr_spec_free(addr_spec); + } + } else { + *result = mailbox; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +unsafe fn mailimf_addr_spec_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut addr_spec: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut begin: size_t = 0; + let mut end: size_t = 0; + let mut final_0: libc::c_int = 0; + let mut count: size_t = 0; + let mut src: *const libc::c_char = 0 as *const libc::c_char; + let mut dest: *mut libc::c_char = 0 as *mut libc::c_char; + let mut i: size_t = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + end = cur_token; + if end >= length { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + begin = cur_token; + final_0 = 0i32; + loop { + match *message.offset(end as isize) as libc::c_int { + 62 | 44 | 13 | 10 | 40 | 41 | 58 | 59 => final_0 = 1i32, + _ => {} + } + if 0 != final_0 { + break; + } + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end == begin { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + addr_spec = malloc( + end.wrapping_sub(cur_token) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if addr_spec.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + count = end.wrapping_sub(cur_token); + src = message.offset(cur_token as isize); + dest = addr_spec; + i = 0i32 as size_t; + while i < count { + if *src as libc::c_int != ' ' as i32 && *src as libc::c_int != '\t' as i32 { + *dest = *src; + dest = dest.offset(1isize) + } + src = src.offset(1isize); + i = i.wrapping_add(1) + } + *dest = '\u{0}' as i32 as libc::c_char; + cur_token = end; + *result = addr_spec; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + return res; +} +unsafe fn mailimf_name_addr_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut pdisplay_name: *mut *mut libc::c_char, + mut pangle_addr: *mut *mut libc::c_char, +) -> libc::c_int { + let mut display_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut angle_addr: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + display_name = 0 as *mut libc::c_char; + angle_addr = 0 as *mut libc::c_char; + r = mailimf_display_name_parse(message, length, &mut cur_token, &mut display_name); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_angle_addr_parse(message, length, &mut cur_token, &mut angle_addr); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + if !display_name.is_null() { + mailimf_display_name_free(display_name); + } + } else { + *pdisplay_name = display_name; + *pangle_addr = angle_addr; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +unsafe fn mailimf_angle_addr_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut addr_spec: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_lower_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_addr_spec_parse(message, length, &mut cur_token, &mut addr_spec); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_greater_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + free(addr_spec as *mut libc::c_void); + return r; + } + *result = addr_spec; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_display_name_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_phrase_parse(message, length, indx, result); +} +unsafe fn mailimf_phrase_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut gphrase: *mut MMAPString = 0 as *mut MMAPString; + let mut word: *mut libc::c_char = 0 as *mut libc::c_char; + let mut first: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut str: *mut libc::c_char = 0 as *mut libc::c_char; + let mut has_missing_closing_quote: libc::c_int = 0; + cur_token = *indx; + has_missing_closing_quote = 0i32; + gphrase = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); + if gphrase.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + first = 1i32; + loop { + let mut missing_quote: libc::c_int = 0i32; + r = mailimf_fws_word_parse( + message, + length, + &mut cur_token, + &mut word, + &mut missing_quote, + ); + if 0 != missing_quote { + has_missing_closing_quote = 1i32 + } + if r == MAILIMF_NO_ERROR as libc::c_int { + if 0 == first { + if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() { + mailimf_word_free(word); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 17261756273978092585; + break; + } + } + if mmap_string_append(gphrase, word).is_null() { + mailimf_word_free(word); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 17261756273978092585; + break; + } else { + mailimf_word_free(word); + first = 0i32 + } + } else { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 11636175345244025579; + break; + } + res = r; + current_block = 17261756273978092585; + break; + } + } + match current_block { + 11636175345244025579 => { + if 0 != first { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + if 0 != has_missing_closing_quote { + r = mailimf_char_parse( + message, + length, + &mut cur_token, + '\"' as i32 as libc::c_char, + ) + } + str = strdup((*gphrase).str_0); + if str.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mmap_string_free(gphrase); + *result = str; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + mmap_string_free(gphrase); + } + return res; +} + +pub unsafe fn mailimf_fws_word_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut p_missing_closing_quote: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut word: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut missing_closing_quote: libc::c_int = 0; + cur_token = *indx; + missing_closing_quote = 0i32; + r = mailimf_fws_atom_for_word_parse( + message, + length, + &mut cur_token, + &mut word, + &mut missing_closing_quote, + ); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_fws_quoted_string_parse(message, length, &mut cur_token, &mut word) + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = word; + *indx = cur_token; + *p_missing_closing_quote = missing_closing_quote; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_fws_quoted_string_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut gstr: *mut MMAPString = 0 as *mut MMAPString; + let mut ch: libc::c_char = 0; + let mut str: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_dquote_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + gstr = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); + if gstr.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + r = mailimf_fws_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + if mmap_string_append_c(gstr, ' ' as i32 as libc::c_char).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 15096897878952122875; + break; + } + } else if r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r; + current_block = 15096897878952122875; + break; + } + r = mailimf_qcontent_parse(message, length, &mut cur_token, &mut ch); + if r == MAILIMF_NO_ERROR as libc::c_int { + if !mmap_string_append_c(gstr, ch).is_null() { + continue; + } + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 15096897878952122875; + break; + } else { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 5494826135382683477; + break; + } + res = r; + current_block = 15096897878952122875; + break; + } + } + match current_block { + 5494826135382683477 => { + r = mailimf_dquote_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + str = strdup((*gstr).str_0); + if str.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mmap_string_free(gstr); + *indx = cur_token; + *result = str; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + mmap_string_free(gstr); + } + } + } + return res; +} +unsafe fn mailimf_dquote_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_char_parse(message, length, indx, '\"' as i32 as libc::c_char); +} +unsafe fn mailimf_qcontent_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut ch: libc::c_char = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if 0 != is_qtext(*message.offset(cur_token as isize)) { + ch = *message.offset(cur_token as isize); + cur_token = cur_token.wrapping_add(1) + } else { + r = mailimf_quoted_pair_parse(message, length, &mut cur_token, &mut ch); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + *result = ch; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +dot-atom = [CFWS] dot-atom-text [CFWS] +*/ +/* +dot-atom-text = 1*atext *("." 1*atext) +*/ +/* +qtext = NO-WS-CTL / ; Non white space controls + + %d33 / ; The rest of the US-ASCII + %d35-91 / ; characters not including "\" + %d93-126 ; or the quote character +*/ +#[inline] +unsafe fn is_qtext(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if 0 != is_no_ws_ctl(ch) { + return 1i32; + } + if (uch as libc::c_int) < 33i32 { + return 0i32; + } + if uch as libc::c_int == 34i32 { + return 0i32; + } + if uch as libc::c_int == 92i32 { + return 0i32; + } + if uch as libc::c_int == 127i32 { + return 0i32; + } + return 1i32; +} +unsafe fn mailimf_fws_atom_for_word_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut p_missing_closing_quote: *mut libc::c_int, +) -> libc::c_int { + let mut end: size_t = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut word: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word; + let mut has_fwd: libc::c_int = 0; + let mut missing_closing_quote: libc::c_int = 0; + let mut atom: *mut libc::c_char = 0 as *mut libc::c_char; + cur_token = *indx; + missing_closing_quote = 0i32; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + end = cur_token; + r = mailmime_encoded_word_parse( + message, + length, + &mut cur_token, + &mut word, + &mut has_fwd, + &mut missing_closing_quote, + ); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + return mailimf_fws_atom_parse(message, length, indx, result); + } + mailmime_encoded_word_free(word); + atom = malloc( + cur_token + .wrapping_sub(end) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if atom.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + strncpy( + atom, + message.offset(end as isize), + cur_token.wrapping_sub(end), + ); + *atom.offset(cur_token.wrapping_sub(end) as isize) = '\u{0}' as i32 as libc::c_char; + *result = atom; + *indx = cur_token; + *p_missing_closing_quote = missing_closing_quote; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + return res; +} + +pub unsafe fn mailimf_fws_atom_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut atom: *mut libc::c_char = 0 as *mut libc::c_char; + let mut end: size_t = 0; + cur_token = *indx; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + end = cur_token; + if end >= length { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + while 0 != is_atext(*message.offset(end as isize)) { + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end == cur_token { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + atom = malloc( + end.wrapping_sub(cur_token) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if atom.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + strncpy( + atom, + message.offset(cur_token as isize), + end.wrapping_sub(cur_token), + ); + *atom.offset(end.wrapping_sub(cur_token) as isize) = + '\u{0}' as i32 as libc::c_char; + cur_token = end; + *indx = cur_token; + *result = atom; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + return res; +} +/* +atext = ALPHA / DIGIT / ; Any character except controls, + "!" / "#" / ; SP, and specials. + "$" / "%" / ; Used for atoms + "&" / "'" / + "*" / "+" / + "-" / "/" / + "=" / "?" / + "^" / "_" / + "`" / "{" / + "|" / "}" / + "~" +*/ +#[inline] +unsafe fn is_atext(mut ch: libc::c_char) -> libc::c_int { + match ch as libc::c_int { + 32 | 9 | 10 | 13 | 60 | 62 | 44 | 34 | 58 | 59 => return 0i32, + _ => return 1i32, + }; +} +unsafe fn mailimf_group_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_group, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut display_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut mailbox_list: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + let mut group: *mut mailimf_group = 0 as *mut mailimf_group; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut list: *mut clist = 0 as *mut clist; + cur_token = *indx; + mailbox_list = 0 as *mut mailimf_mailbox_list; + r = mailimf_display_name_parse(message, length, &mut cur_token, &mut display_name); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_mailbox_list_parse(message, length, &mut cur_token, &mut mailbox_list); + match r { + 0 => { + current_block = 1608152415753874203; + } + 1 => { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 14904789583098922708; + } else { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14904789583098922708; + } else { + mailbox_list = mailimf_mailbox_list_new(list); + if mailbox_list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + clist_free(list); + current_block = 14904789583098922708; + } else { + current_block = 1608152415753874203; + } + } + } + } + _ => { + res = r; + current_block = 14904789583098922708; + } + } + match current_block { + 14904789583098922708 => {} + _ => { + r = mailimf_semi_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + group = mailimf_group_new(display_name, mailbox_list); + if group.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *indx = cur_token; + *result = group; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + if !mailbox_list.is_null() { + mailimf_mailbox_list_free(mailbox_list); + } + } + } + } + mailimf_display_name_free(display_name); + } + return res; +} + +unsafe fn mailimf_semi_colon_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, ';' as i32 as libc::c_char); +} + +/* + mailimf_mailbox_list_parse will parse the given mailbox list + + @param message this is a string containing the mailbox list + @param length this is the size of the given string + @param indx this is a pointer to the start of the mailbox list in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_mailbox_list_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_mailbox_list, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut mailbox_list: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_struct_list_parse( + message, + length, + &mut cur_token, + &mut list, + ',' as i32 as libc::c_char, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut mailimf_mailbox, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_mailbox_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_mailbox_free)), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + mailbox_list = mailimf_mailbox_list_new(list); + if mailbox_list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + clist_foreach( + list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_mailbox_free), + ), + 0 as *mut libc::c_void, + ); + clist_free(list); + } else { + *result = mailbox_list; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +unsafe fn mailimf_struct_list_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut clist, + mut symbol: libc::c_char, + mut parser: Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + mut destructor: Option libc::c_int>, +) -> libc::c_int { + let mut current_block: u64; + let mut struct_list: *mut clist = 0 as *mut clist; + let mut cur_token: size_t = 0; + let mut value: *mut libc::c_void = 0 as *mut libc::c_void; + let mut final_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = parser.expect("non-null function pointer")( + message, + length, + &mut cur_token, + &mut value as *mut *mut libc::c_void as *mut libc::c_void, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + struct_list = clist_new(); + if struct_list.is_null() { + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + r = clist_insert_after(struct_list, (*struct_list).last, value); + if r < 0i32 { + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + final_token = cur_token; + loop { + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, symbol); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9853141518545631134; + break; + } + res = r; + current_block = 17524159567010234572; + break; + } else { + r = parser.expect("non-null function pointer")( + message, + length, + &mut cur_token, + &mut value as *mut *mut libc::c_void as *mut libc::c_void, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 9853141518545631134; + break; + } + res = r; + current_block = 17524159567010234572; + break; + } else { + r = clist_insert_after(struct_list, (*struct_list).last, value); + if r < 0i32 { + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 17524159567010234572; + break; + } else { + final_token = cur_token + } + } + } + } + match current_block { + 17524159567010234572 => {} + _ => { + *result = struct_list; + *indx = final_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + clist_foreach( + struct_list, + ::std::mem::transmute::< + Option libc::c_int>, + clist_func, + >(destructor), + 0 as *mut libc::c_void, + ); + clist_free(struct_list); + } + } + return res; +} +unsafe fn mailimf_resent_cc_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_cc, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-Cc\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-Cc\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + cc = mailimf_cc_new(addr_list); + if cc.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = cc; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +unsafe fn mailimf_resent_to_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_to, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-To\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-To\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + to = mailimf_to_new(addr_list); + if to.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = to; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +unsafe fn mailimf_resent_sender_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_sender, +) -> libc::c_int { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + let mut sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = length; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-Sender\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-Sender\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_mailbox_parse(message, length, &mut cur_token, &mut mb); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + sender = mailimf_sender_new(mb); + if sender.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = sender; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_mailbox_free(mb); + } + } + } + return res; +} +unsafe fn mailimf_resent_from_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_from, +) -> libc::c_int { + let mut mb_list: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + let mut from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-From\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-From\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_mailbox_list_parse(message, length, &mut cur_token, &mut mb_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + from = mailimf_from_new(mb_list); + if from.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = from; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_mailbox_list_free(mb_list); + } + } + } + return res; +} +unsafe fn mailimf_resent_date_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_orig_date, +) -> libc::c_int { + let mut orig_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Resent-Date\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Resent-Date\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_date_time_parse(message, length, &mut cur_token, &mut date_time); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + orig_date = mailimf_orig_date_new(date_time); + if orig_date.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = orig_date; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_date_time_free(date_time); + } + } + } + return res; +} +/* + mailimf_date_time_parse will parse the given RFC 2822 date + + @param message this is a string containing the date + @param length this is the size of the given string + @param indx this is a pointer to the start of the date in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_date_time_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_date_time, +) -> libc::c_int { + let mut cur_token: size_t = 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 day: libc::c_int = 0; + let mut month: libc::c_int = 0; + let mut year: libc::c_int = 0; + let mut hour: libc::c_int = 0; + let mut min: libc::c_int = 0; + let mut sec: libc::c_int = 0; + let mut zone: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + day_of_week = -1i32; + r = mailimf_day_of_week_parse(message, length, &mut cur_token, &mut day_of_week); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = mailimf_comma_parse(message, length, &mut cur_token); + if !(r == MAILIMF_ERROR_PARSE as libc::c_int) { + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } else if r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + day = 0i32; + month = 0i32; + year = 0i32; + r = mailimf_date_parse( + message, + length, + &mut cur_token, + &mut day, + &mut month, + &mut year, + ); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_broken_date_parse( + message, + length, + &mut cur_token, + &mut day, + &mut month, + &mut year, + ) + } else if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + hour = 0i32; + min = 0i32; + sec = 0i32; + zone = 0i32; + r = mailimf_time_parse( + message, + length, + &mut cur_token, + &mut hour, + &mut min, + &mut sec, + &mut zone, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + date_time = mailimf_date_time_new(day, month, year, hour, min, sec, zone); + if date_time.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + *indx = cur_token; + *result = date_time; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_time_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut phour: *mut libc::c_int, + mut pmin: *mut libc::c_int, + mut psec: *mut libc::c_int, + mut pzone: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut hour: libc::c_int = 0; + let mut min: libc::c_int = 0; + let mut sec: libc::c_int = 0; + let mut zone: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_time_of_day_parse( + message, + length, + &mut cur_token, + &mut hour, + &mut min, + &mut sec, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_zone_parse(message, length, &mut cur_token, &mut zone); + if !(r == MAILIMF_NO_ERROR as libc::c_int) { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + zone = 0i32 + } else { + return r; + } + } + *phour = hour; + *pmin = min; + *psec = sec; + *pzone = zone; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_zone_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut zone: libc::c_int = 0; + let mut sign: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut value: uint32_t = 0; + cur_token = *indx; + 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 + && *message.offset(cur_token.wrapping_add(1i32 as libc::size_t) as isize) as libc::c_int + == 'T' as i32 + { + *result = 1i32; + *indx = cur_token.wrapping_add(2i32 as libc::size_t); + return MAILIMF_NO_ERROR as libc::c_int; + } + } + zone = 0i32; + if cur_token.wrapping_add(2i32 as libc::size_t) < length { + let mut state: libc::c_int = 0; + state = STATE_ZONE_1 as libc::c_int; + while state <= 2i32 { + match state { + 0 => match *message.offset(cur_token as isize) as libc::c_int { + 71 => { + if *message.offset(cur_token.wrapping_add(1i32 as libc::size_t) as isize) + as libc::c_int + == 'M' as i32 + && *message + .offset(cur_token.wrapping_add(2i32 as libc::size_t) as isize) + as libc::c_int + == 'T' as i32 + { + if cur_token.wrapping_add(3i32 as libc::size_t) < length + && (*message + .offset(cur_token.wrapping_add(3i32 as libc::size_t) as isize) + as libc::c_int + == '+' as i32 + || *message.offset( + cur_token.wrapping_add(3i32 as libc::size_t) as isize + ) as libc::c_int + == '-' as i32) + { + cur_token = (cur_token as libc::size_t) + .wrapping_add(3i32 as libc::size_t) + as size_t as size_t; + state = STATE_ZONE_CONT as libc::c_int + } else { + zone = 0i32; + state = STATE_ZONE_OK as libc::c_int + } + } else { + state = STATE_ZONE_ERR as libc::c_int + } + } + 69 => { + zone = -5i32; + state = STATE_ZONE_2 as libc::c_int + } + 67 => { + zone = -6i32; + state = STATE_ZONE_2 as libc::c_int + } + 77 => { + zone = -7i32; + state = STATE_ZONE_2 as libc::c_int + } + 80 => { + zone = -8i32; + state = STATE_ZONE_2 as libc::c_int + } + _ => state = STATE_ZONE_CONT as libc::c_int, + }, + 1 => { + match *message.offset(cur_token.wrapping_add(1i32 as libc::size_t) as isize) + as libc::c_int + { + 83 => state = STATE_ZONE_3 as libc::c_int, + 68 => { + zone += 1; + state = STATE_ZONE_3 as libc::c_int + } + _ => state = STATE_ZONE_ERR as libc::c_int, + } + } + 2 => { + if *message.offset(cur_token.wrapping_add(2i32 as libc::size_t) as isize) + as libc::c_int + == 'T' as i32 + { + zone *= 100i32; + state = STATE_ZONE_OK as libc::c_int + } else { + state = STATE_ZONE_ERR as libc::c_int + } + } + _ => {} + } + } + match state { + 3 => { + *result = zone; + *indx = cur_token.wrapping_add(3i32 as libc::size_t); + return MAILIMF_NO_ERROR as libc::c_int; + } + 4 => return MAILIMF_ERROR_PARSE as libc::c_int, + _ => {} + } + } + sign = 1i32; + r = mailimf_plus_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + sign = 1i32 + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_minus_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + sign = -1i32 + } + } + if !(r == MAILIMF_NO_ERROR as libc::c_int) { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + sign = 1i32 + } else { + return r; + } + } + r = mailimf_number_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + zone = value.wrapping_mul(sign as libc::c_uint) as libc::c_int; + *indx = cur_token; + *result = zone; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_number_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut uint32_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut digit: libc::c_int = 0; + let mut number: uint32_t = 0; + let mut parsed: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + parsed = 0i32; + number = 0i32 as uint32_t; + loop { + r = mailimf_digit_parse(message, length, &mut cur_token, &mut digit); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } else { + number = (number as libc::c_uint).wrapping_mul(10i32 as libc::c_uint) as uint32_t + as uint32_t; + number = (number as libc::c_uint).wrapping_add(digit as libc::c_uint) as uint32_t + as uint32_t; + parsed = 1i32 + } + } + if 0 == parsed { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + *result = number; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_digit_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if 0 != is_digit(*message.offset(cur_token as isize)) { + *result = *message.offset(cur_token as isize) as libc::c_int - '0' as i32; + cur_token = cur_token.wrapping_add(1); + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } else { + return MAILIMF_ERROR_PARSE as libc::c_int; + }; +} +/* *************************************************************** */ +#[inline] +unsafe fn is_digit(mut ch: libc::c_char) -> libc::c_int { + return (ch as libc::c_int >= '0' as i32 && ch as libc::c_int <= '9' as i32) as libc::c_int; +} +unsafe fn mailimf_minus_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '-' as i32 as libc::c_char); +} +unsafe fn mailimf_plus_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '+' as i32 as libc::c_char); +} +unsafe fn mailimf_time_of_day_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut phour: *mut libc::c_int, + mut pmin: *mut libc::c_int, + mut psec: *mut libc::c_int, +) -> libc::c_int { + let mut hour: libc::c_int = 0; + let mut min: libc::c_int = 0; + let mut sec: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_hour_parse(message, length, &mut cur_token, &mut hour); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_minute_parse(message, length, &mut cur_token, &mut min); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_colon_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = mailimf_second_parse(message, length, &mut cur_token, &mut sec); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + sec = 0i32 + } else { + return r; + } + *phour = hour; + *pmin = min; + *psec = sec; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_second_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut second: uint32_t = 0; + let mut r: libc::c_int = 0; + r = mailimf_number_parse(message, length, indx, &mut second); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = second as libc::c_int; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_minute_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut minute: uint32_t = 0; + let mut r: libc::c_int = 0; + r = mailimf_number_parse(message, length, indx, &mut minute); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = minute as libc::c_int; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_hour_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut hour: uint32_t = 0; + let mut r: libc::c_int = 0; + r = mailimf_number_parse(message, length, indx, &mut hour); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = hour as libc::c_int; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_broken_date_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut pday: *mut libc::c_int, + mut pmonth: *mut libc::c_int, + mut pyear: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut day: libc::c_int = 0; + let mut month: libc::c_int = 0; + let mut year: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + month = 1i32; + r = mailimf_month_parse(message, length, &mut cur_token, &mut month); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + day = 1i32; + r = mailimf_day_parse(message, length, &mut cur_token, &mut day); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + year = 2001i32; + r = mailimf_year_parse(message, length, &mut cur_token, &mut year); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *pday = day; + *pmonth = month; + *pyear = year; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_year_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut number: uint32_t = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_number_parse(message, length, &mut cur_token, &mut number); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = number as libc::c_int; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_day_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut day: uint32_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_number_parse(message, length, &mut cur_token, &mut day); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = day as libc::c_int; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_month_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut month: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_month_name_parse(message, length, &mut cur_token, &mut month); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = month; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_month_name_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut month: libc::c_int = 0; + let mut guessed_month: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + guessed_month = guess_month(message, length, cur_token); + if guessed_month == -1i32 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + month_names[(guessed_month - 1i32) as usize].str_0, + strlen(month_names[(guessed_month - 1i32) as usize].str_0), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + month = guessed_month; + *result = month; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +month-name = "Jan" / "Feb" / "Mar" / "Apr" / + "May" / "Jun" / "Jul" / "Aug" / + "Sep" / "Oct" / "Nov" / "Dec" +*/ +static mut month_names: [mailimf_token_value; 12] = [ + mailimf_token_value { + value: 1i32, + str_0: b"Jan\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 2i32, + str_0: b"Feb\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 3i32, + str_0: b"Mar\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 4i32, + str_0: b"Apr\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 5i32, + str_0: b"May\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 6i32, + str_0: b"Jun\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 7i32, + str_0: b"Jul\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 8i32, + str_0: b"Aug\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 9i32, + str_0: b"Sep\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 10i32, + str_0: b"Oct\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 11i32, + str_0: b"Nov\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 12i32, + str_0: b"Dec\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, +]; +unsafe fn guess_month( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + state = MONTH_START as libc::c_int; + loop { + if indx >= length { + return -1i32; + } + match state { + 0 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 74 => state = MONTH_J as libc::c_int, + 70 => return 2i32, + 77 => state = MONTH_M as libc::c_int, + 65 => state = MONTH_A as libc::c_int, + 83 => return 9i32, + 79 => return 10i32, + 78 => return 11i32, + 68 => return 12i32, + _ => return -1i32, + } + } + 1 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 65 => return 1i32, + 85 => state = MONTH_JU as libc::c_int, + _ => return -1i32, + } + } + 2 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 78 => return 6i32, + 76 => return 7i32, + _ => return -1i32, + } + } + 3 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 65 => state = MONTH_MA as libc::c_int, + _ => return -1i32, + } + } + 4 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 89 => return 5i32, + 82 => return 3i32, + _ => return -1i32, + } + } + 5 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 80 => return 4i32, + 85 => return 8i32, + _ => return -1i32, + } + } + _ => {} + } + indx = indx.wrapping_add(1) + } +} + +unsafe fn mailimf_date_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut pday: *mut libc::c_int, + mut pmonth: *mut libc::c_int, + mut pyear: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut day: libc::c_int = 0; + let mut month: libc::c_int = 0; + let mut year: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + day = 1i32; + r = mailimf_day_parse(message, length, &mut cur_token, &mut day); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + month = 1i32; + r = mailimf_month_parse(message, length, &mut cur_token, &mut month); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + year = 2001i32; + r = mailimf_year_parse(message, length, &mut cur_token, &mut year); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *pday = day; + *pmonth = month; + *pyear = year; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_comma_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, ',' as i32 as libc::c_char); +} +unsafe fn mailimf_day_of_week_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut day_of_week: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_day_name_parse(message, length, &mut cur_token, &mut day_of_week); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = day_of_week; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_day_name_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut day_of_week: libc::c_int = 0; + let mut guessed_day: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + guessed_day = guess_day_name(message, length, cur_token); + if guessed_day == -1i32 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + day_names[(guessed_day - 1i32) as usize].str_0, + strlen(day_names[(guessed_day - 1i32) as usize].str_0), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + day_of_week = guessed_day; + *result = day_of_week; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +static mut day_names: [mailimf_token_value; 7] = [ + mailimf_token_value { + value: 1i32, + str_0: b"Mon\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 2i32, + str_0: b"Tue\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 3i32, + str_0: b"Wed\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 4i32, + str_0: b"Thu\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 5i32, + str_0: b"Fri\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 6i32, + str_0: b"Sat\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, + mailimf_token_value { + value: 7i32, + str_0: b"Sun\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + }, +]; +unsafe fn guess_day_name( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + state = DAY_NAME_START as libc::c_int; + loop { + if indx >= length { + return -1i32; + } + match state { + 0 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 77 => return 1i32, + 84 => state = DAY_NAME_T as libc::c_int, + 87 => return 3i32, + 70 => return 5i32, + 83 => state = DAY_NAME_S as libc::c_int, + _ => return -1i32, + } + } + 1 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 85 => return 2i32, + 72 => return 4i32, + _ => return -1i32, + } + } + 2 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 65 => return 6i32, + 85 => return 7i32, + _ => return -1i32, + } + } + _ => {} + } + indx = indx.wrapping_add(1) + } +} +unsafe fn mailimf_return_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_return, +) -> libc::c_int { + let mut path: *mut mailimf_path = 0 as *mut mailimf_path; + let mut return_path: *mut mailimf_return = 0 as *mut mailimf_return; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Return-Path\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Return-Path\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + path = 0 as *mut mailimf_path; + r = mailimf_path_parse(message, length, &mut cur_token, &mut path); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + return_path = mailimf_return_new(path); + if return_path.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = return_path; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_path_free(path); + } + } + } + return res; +} +unsafe fn mailimf_path_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_path, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut addr_spec: *mut libc::c_char = 0 as *mut libc::c_char; + let mut path: *mut mailimf_path = 0 as *mut mailimf_path; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + addr_spec = 0 as *mut libc::c_char; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_lower_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_addr_spec_parse(message, length, &mut cur_token, &mut addr_spec); + match r { + 0 => { + current_block = 2370887241019905314; + } + 1 => { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 14973541214802992465; + } else { + current_block = 2370887241019905314; + } + } + _ => return r, + } + match current_block { + 14973541214802992465 => {} + _ => { + r = mailimf_greater_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + path = mailimf_path_new(addr_spec); + if path.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if addr_spec.is_null() { + mailimf_addr_spec_free(addr_spec); + } + } else { + *indx = cur_token; + *result = path; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + } + } + return res; +} +unsafe fn mailimf_keywords_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_keywords, +) -> libc::c_int { + let mut keywords: *mut mailimf_keywords = 0 as *mut mailimf_keywords; + let mut list: *mut clist = 0 as *mut clist; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Keywords\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Keywords\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_struct_list_parse( + message, + length, + &mut cur_token, + &mut list, + ',' as i32 as libc::c_char, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut libc::c_char, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_phrase_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_phrase_free)), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + keywords = mailimf_keywords_new(list); + if keywords.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = keywords; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_phrase_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } + } + return res; +} +unsafe fn mailimf_comments_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_comments, +) -> libc::c_int { + let mut comments: *mut mailimf_comments = 0 as *mut mailimf_comments; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Comments\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Comments\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstructured_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + comments = mailimf_comments_new(value); + if comments.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = comments; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_unstructured_free(value); + } + } + } + return res; +} +unsafe fn mailimf_subject_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_subject, +) -> libc::c_int { + let mut subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Subject\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Subject\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstructured_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + subject = mailimf_subject_new(value); + if subject.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = subject; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_unstructured_free(value); + } + } + } + return res; +} + +/* exported for IMAP */ +pub unsafe fn mailimf_references_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_references, +) -> libc::c_int { + let mut references: *mut mailimf_references = 0 as *mut mailimf_references; + let mut cur_token: size_t = 0; + let mut msg_id_list: *mut clist = 0 as *mut clist; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"References\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"References\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_msg_id_list_parse(message, length, &mut cur_token, &mut msg_id_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + references = mailimf_references_new(msg_id_list); + if references.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = references; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + clist_foreach( + msg_id_list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_msg_id_free)), + 0 as *mut libc::c_void, + ); + clist_free(msg_id_list); + } + } + } + return res; +} + +pub unsafe fn mailimf_msg_id_list_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut clist, +) -> libc::c_int { + return mailimf_struct_multiple_parse( + message, + length, + indx, + result, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut libc::c_char, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_unstrict_msg_id_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_msg_id_free)), + ); +} +unsafe fn mailimf_unstrict_msg_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut msgid: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_parse_unwanted_msg_id(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_msg_id_parse(message, length, &mut cur_token, &mut msgid); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_parse_unwanted_msg_id(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + free(msgid as *mut libc::c_void); + return r; + } + *result = msgid; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_parse_unwanted_msg_id( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut word: *mut libc::c_char = 0 as *mut libc::c_char; + let mut token_parsed: libc::c_int = 0; + cur_token = *indx; + token_parsed = 1i32; + while 0 != token_parsed { + token_parsed = 0i32; + r = mailimf_word_parse(message, length, &mut cur_token, &mut word); + if r == MAILIMF_NO_ERROR as libc::c_int { + mailimf_word_free(word); + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_semi_colon_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_comma_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_plus_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_colon_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_point_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + r = mailimf_at_sign_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + token_parsed = 1i32 + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_at_sign_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '@' as i32 as libc::c_char); +} +unsafe fn mailimf_point_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + return mailimf_unstrict_char_parse(message, length, indx, '.' as i32 as libc::c_char); +} + +pub unsafe fn mailimf_word_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut word: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_atom_parse(message, length, &mut cur_token, &mut word); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_quoted_string_parse(message, length, &mut cur_token, &mut word) + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *result = word; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_quoted_string_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut gstr: *mut MMAPString = 0 as *mut MMAPString; + let mut ch: libc::c_char = 0; + let mut str: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailimf_dquote_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + gstr = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); + if gstr.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + r = mailimf_fws_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + if mmap_string_append_c(gstr, ' ' as i32 as libc::c_char).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14861901777624095282; + break; + } + } else if r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r; + current_block = 14861901777624095282; + break; + } + r = mailimf_qcontent_parse(message, length, &mut cur_token, &mut ch); + if r == MAILIMF_NO_ERROR as libc::c_int { + if !mmap_string_append_c(gstr, ch).is_null() { + continue; + } + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14861901777624095282; + break; + } else { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 5494826135382683477; + break; + } + res = r; + current_block = 14861901777624095282; + break; + } + } + match current_block { + 5494826135382683477 => { + r = mailimf_dquote_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + str = strdup((*gstr).str_0); + if str.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mmap_string_free(gstr); + *indx = cur_token; + *result = str; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + mmap_string_free(gstr); + } + } + } + return res; +} + +pub unsafe fn mailimf_atom_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut atom: *mut libc::c_char = 0 as *mut libc::c_char; + let mut end: size_t = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + end = cur_token; + if end >= length { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + while 0 != is_atext(*message.offset(end as isize)) { + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end == cur_token { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + atom = malloc( + end.wrapping_sub(cur_token) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if atom.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + strncpy( + atom, + message.offset(cur_token as isize), + end.wrapping_sub(cur_token), + ); + *atom.offset(end.wrapping_sub(cur_token) as isize) = + '\u{0}' as i32 as libc::c_char; + cur_token = end; + *indx = cur_token; + *result = atom; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + return res; +} +unsafe fn mailimf_struct_multiple_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut clist, + mut parser: Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + mut destructor: Option libc::c_int>, +) -> libc::c_int { + let mut current_block: u64; + let mut struct_list: *mut clist = 0 as *mut clist; + let mut cur_token: size_t = 0; + let mut value: *mut libc::c_void = 0 as *mut libc::c_void; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = parser.expect("non-null function pointer")( + message, + length, + &mut cur_token, + &mut value as *mut *mut libc::c_void as *mut libc::c_void, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + struct_list = clist_new(); + if struct_list.is_null() { + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + r = clist_insert_after(struct_list, (*struct_list).last, value); + if r < 0i32 { + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + r = parser.expect("non-null function pointer")( + message, + length, + &mut cur_token, + &mut value as *mut *mut libc::c_void as *mut libc::c_void, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 11057878835866523405; + break; + } + res = r; + current_block = 8222683242185098763; + break; + } else { + r = clist_insert_after(struct_list, (*struct_list).last, value); + if !(r < 0i32) { + continue; + } + destructor.expect("non-null function pointer")(value); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 8222683242185098763; + break; + } + } + match current_block { + 8222683242185098763 => {} + _ => { + *result = struct_list; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + clist_foreach( + struct_list, + ::std::mem::transmute::< + Option libc::c_int>, + clist_func, + >(destructor), + 0 as *mut libc::c_void, + ); + clist_free(struct_list); + } + } + return res; +} +unsafe fn mailimf_in_reply_to_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_in_reply_to, +) -> libc::c_int { + let mut in_reply_to: *mut mailimf_in_reply_to = 0 as *mut mailimf_in_reply_to; + let mut cur_token: size_t = 0; + let mut msg_id_list: *mut clist = 0 as *mut clist; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"In-Reply-To\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"In-Reply-To\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_msg_id_list_parse(message, length, &mut cur_token, &mut msg_id_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + in_reply_to = mailimf_in_reply_to_new(msg_id_list); + if in_reply_to.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = in_reply_to; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + clist_foreach( + msg_id_list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_msg_id_free)), + 0 as *mut libc::c_void, + ); + clist_free(msg_id_list); + } + } + } + return res; +} +unsafe fn mailimf_message_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_message_id, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut message_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Message-ID\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Message-ID\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_msg_id_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + message_id = mailimf_message_id_new(value); + if message_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = message_id; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_msg_id_free(value); + } + } + } + return res; +} +unsafe fn mailimf_bcc_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_bcc, +) -> libc::c_int { + let mut current_block: u64; + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + addr_list = 0 as *mut mailimf_address_list; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Bcc\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Bcc\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + match r { + 0 => { + /* do nothing */ + current_block = 2838571290723028321; + } + 1 => { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 15260376711225273221; + } else { + current_block = 2838571290723028321; + } + } + _ => { + res = r; + current_block = 15260376711225273221; + } + } + match current_block { + 15260376711225273221 => {} + _ => { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + bcc = mailimf_bcc_new(addr_list); + if bcc.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = bcc; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + } + } + if !addr_list.is_null() { + mailimf_address_list_free(addr_list); + } + return res; +} +unsafe fn mailimf_cc_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_cc, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Cc\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Cc\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + cc = mailimf_cc_new(addr_list); + if cc.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = cc; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +unsafe fn mailimf_to_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_to, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"To\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"To\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + to = mailimf_to_new(addr_list); + if to.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = to; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +unsafe fn mailimf_reply_to_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_reply_to, +) -> libc::c_int { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + let mut reply_to: *mut mailimf_reply_to = 0 as *mut mailimf_reply_to; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Reply-To\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Reply-To\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_address_list_parse(message, length, &mut cur_token, &mut addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + reply_to = mailimf_reply_to_new(addr_list); + if reply_to.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = reply_to; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_address_list_free(addr_list); + } + } + } + return res; +} +unsafe fn mailimf_sender_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_sender, +) -> libc::c_int { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + let mut sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Sender\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Sender\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_mailbox_parse(message, length, &mut cur_token, &mut mb); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + sender = mailimf_sender_new(mb); + if sender.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = sender; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_mailbox_free(mb); + } + } + } + return res; +} +unsafe fn mailimf_from_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_from, +) -> libc::c_int { + let mut mb_list: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + let mut from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"From\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"From\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_colon_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_mailbox_list_parse(message, length, &mut cur_token, &mut mb_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + from = mailimf_from_new(mb_list); + if from.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = from; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + mailimf_mailbox_list_free(mb_list); + } + } + } + return res; +} +unsafe fn mailimf_orig_date_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_orig_date, +) -> libc::c_int { + let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time; + let mut orig_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"Date:\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"Date:\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_date_time_parse(message, length, &mut cur_token, &mut date_time); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_ignore_unstructured_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + orig_date = mailimf_orig_date_new(date_time); + if orig_date.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = orig_date; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + mailimf_date_time_free(date_time); + } + } + return res; +} +unsafe fn mailimf_ignore_unstructured_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut state: libc::c_int = 0; + let mut terminal: size_t = 0; + cur_token = *indx; + state = UNSTRUCTURED_START as libc::c_int; + terminal = cur_token; + while state != UNSTRUCTURED_OUT as libc::c_int { + match state { + 0 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + terminal = cur_token; + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 1 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 2 => { + if cur_token >= length { + state = UNSTRUCTURED_OUT as libc::c_int + } else { + match *message.offset(cur_token as isize) as libc::c_int { + 9 | 32 => state = UNSTRUCTURED_WSP as libc::c_int, + _ => state = UNSTRUCTURED_OUT as libc::c_int, + } + } + } + 3 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + *indx = terminal; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn guess_header_type( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut r: libc::c_int = 0; + state = HEADER_START as libc::c_int; + loop { + if indx >= length { + return MAILIMF_FIELD_NONE as libc::c_int; + } + match state { + 0 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 66 => return MAILIMF_FIELD_BCC as libc::c_int, + 67 => state = HEADER_C as libc::c_int, + 68 => return MAILIMF_FIELD_ORIG_DATE as libc::c_int, + 70 => return MAILIMF_FIELD_FROM as libc::c_int, + 73 => return MAILIMF_FIELD_IN_REPLY_TO as libc::c_int, + 75 => return MAILIMF_FIELD_KEYWORDS as libc::c_int, + 77 => return MAILIMF_FIELD_MESSAGE_ID as libc::c_int, + 82 => state = HEADER_R as libc::c_int, + 84 => return MAILIMF_FIELD_TO as libc::c_int, + 83 => state = HEADER_S as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + 1 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 79 => return MAILIMF_FIELD_COMMENTS as libc::c_int, + 67 => return MAILIMF_FIELD_CC as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + 2 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 69 => state = HEADER_RE as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + 3 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 70 => return MAILIMF_FIELD_REFERENCES as libc::c_int, + 80 => return MAILIMF_FIELD_REPLY_TO as libc::c_int, + 83 => state = HEADER_RES as libc::c_int, + 84 => return MAILIMF_FIELD_RETURN_PATH as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + 4 => { + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 69 => return MAILIMF_FIELD_SENDER as libc::c_int, + 85 => return MAILIMF_FIELD_SUBJECT as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + 5 => { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut indx, + b"ent-\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"ent-\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return MAILIMF_FIELD_NONE as libc::c_int; + } + if indx >= length { + return MAILIMF_FIELD_NONE as libc::c_int; + } + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 68 => return MAILIMF_FIELD_RESENT_DATE as libc::c_int, + 70 => return MAILIMF_FIELD_RESENT_FROM as libc::c_int, + 83 => return MAILIMF_FIELD_RESENT_SENDER as libc::c_int, + 84 => return MAILIMF_FIELD_RESENT_TO as libc::c_int, + 67 => return MAILIMF_FIELD_RESENT_CC as libc::c_int, + 66 => return MAILIMF_FIELD_RESENT_BCC as libc::c_int, + 77 => return MAILIMF_FIELD_RESENT_MSG_ID as libc::c_int, + _ => return MAILIMF_FIELD_NONE as libc::c_int, + } + } + _ => {} + } + indx = indx.wrapping_add(1) + } +} +/* + mailimf_envelope_fields_parse will parse the given fields (Date, + From, Sender, Reply-To, To, Cc, Bcc, Message-ID, In-Reply-To, + References and Subject) + + @param message this is a string containing the header fields + @param length this is the size of the given string + @param indx this is a pointer to the start of the header fields in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ + +pub unsafe fn mailimf_envelope_fields_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_fields, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + let mut elt: *mut mailimf_field = 0 as *mut mailimf_field; + r = mailimf_envelope_field_parse(message, length, &mut cur_token, &mut elt); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after(list, (*list).last, elt as *mut libc::c_void); + if !(r < 0i32) { + continue; + } + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 894413572976700158; + break; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_ignore_field_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + continue; + } + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 2719512138335094285; + break; + } + res = r; + current_block = 894413572976700158; + break; + } else { + res = r; + current_block = 894413572976700158; + break; + } + } + match current_block { + 2719512138335094285 => { + fields = mailimf_fields_new(list); + if fields.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = fields; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + if !list.is_null() { + clist_foreach( + list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_field_free), + ), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } + return res; +} +/* + mailimf_ignore_field_parse will skip the given field + + @param message this is a string containing the header field + @param length this is the size of the given string + @param indx this is a pointer to the start of the header field in + the given string, (* indx) is modified to point at the end + of the parsed data + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_ignore_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut has_field: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut state: libc::c_int = 0; + let mut terminal: size_t = 0; + has_field = 0i32; + cur_token = *indx; + terminal = cur_token; + state = UNSTRUCTURED_START as libc::c_int; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 13 => return MAILIMF_ERROR_PARSE as libc::c_int, + 10 => return MAILIMF_ERROR_PARSE as libc::c_int, + _ => {} + } + while state != UNSTRUCTURED_OUT as libc::c_int { + match state { + 0 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + 58 => { + has_field = 1i32; + state = UNSTRUCTURED_START as libc::c_int + } + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 1 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = UNSTRUCTURED_LF as libc::c_int, + 58 => { + has_field = 1i32; + state = UNSTRUCTURED_START as libc::c_int + } + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + 2 => { + if cur_token >= length { + terminal = cur_token; + state = UNSTRUCTURED_OUT as libc::c_int + } else { + match *message.offset(cur_token as isize) as libc::c_int { + 9 | 32 => state = UNSTRUCTURED_WSP as libc::c_int, + _ => { + terminal = cur_token; + state = UNSTRUCTURED_OUT as libc::c_int + } + } + } + } + 3 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = UNSTRUCTURED_CR as libc::c_int, + 10 => state = UNSTRUCTURED_LF as libc::c_int, + 58 => { + has_field = 1i32; + state = UNSTRUCTURED_START as libc::c_int + } + _ => state = UNSTRUCTURED_START as libc::c_int, + } + } + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + if 0 == has_field { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + *indx = terminal; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +static int mailimf_ftext_parse(const char * message, size_t length, + size_t * indx, gchar * result) +{ + return mailimf_typed_text_parse(message, length, indx, result, is_ftext); +} +*/ +unsafe fn mailimf_envelope_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_field, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut type_0: libc::c_int = 0; + let mut orig_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut reply_to: *mut mailimf_reply_to = 0 as *mut mailimf_reply_to; + let mut to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut message_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut in_reply_to: *mut mailimf_in_reply_to = 0 as *mut mailimf_in_reply_to; + let mut references: *mut mailimf_references = 0 as *mut mailimf_references; + let mut subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut guessed_type: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + orig_date = 0 as *mut mailimf_orig_date; + from = 0 as *mut mailimf_from; + sender = 0 as *mut mailimf_sender; + reply_to = 0 as *mut mailimf_reply_to; + to = 0 as *mut mailimf_to; + cc = 0 as *mut mailimf_cc; + bcc = 0 as *mut mailimf_bcc; + message_id = 0 as *mut mailimf_message_id; + in_reply_to = 0 as *mut mailimf_in_reply_to; + references = 0 as *mut mailimf_references; + subject = 0 as *mut mailimf_subject; + optional_field = 0 as *mut mailimf_optional_field; + guessed_type = guess_header_type(message, length, cur_token); + type_0 = MAILIMF_FIELD_NONE as libc::c_int; + match guessed_type { + 9 => { + r = mailimf_orig_date_parse(message, length, &mut cur_token, &mut orig_date); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 10 => { + r = mailimf_from_parse(message, length, &mut cur_token, &mut from); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 11 => { + r = mailimf_sender_parse(message, length, &mut cur_token, &mut sender); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 12 => { + r = mailimf_reply_to_parse(message, length, &mut cur_token, &mut reply_to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 13 => { + r = mailimf_to_parse(message, length, &mut cur_token, &mut to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 14 => { + r = mailimf_cc_parse(message, length, &mut cur_token, &mut cc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 15 => { + r = mailimf_bcc_parse(message, length, &mut cur_token, &mut bcc); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 16 => { + r = mailimf_message_id_parse(message, length, &mut cur_token, &mut message_id); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 17 => { + r = mailimf_in_reply_to_parse(message, length, &mut cur_token, &mut in_reply_to); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 18 => { + r = mailimf_references_parse(message, length, &mut cur_token, &mut references); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + 19 => { + r = mailimf_subject_parse(message, length, &mut cur_token, &mut subject); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 4488496028633655612; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 4488496028633655612; + } else { + /* do nothing */ + res = r; + current_block = 15605017197726313499; + } + } + _ => { + current_block = 4488496028633655612; + } + } + match current_block { + 4488496028633655612 => { + if type_0 == MAILIMF_FIELD_NONE as libc::c_int { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + field = mailimf_field_new( + type_0, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + orig_date, + from, + sender, + reply_to, + to, + cc, + bcc, + message_id, + in_reply_to, + references, + subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + optional_field, + ); + if field.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !orig_date.is_null() { + mailimf_orig_date_free(orig_date); + } + if !from.is_null() { + mailimf_from_free(from); + } + if !sender.is_null() { + mailimf_sender_free(sender); + } + if !reply_to.is_null() { + mailimf_reply_to_free(reply_to); + } + if !to.is_null() { + mailimf_to_free(to); + } + if !cc.is_null() { + mailimf_cc_free(cc); + } + if !bcc.is_null() { + mailimf_bcc_free(bcc); + } + if !message_id.is_null() { + mailimf_message_id_free(message_id); + } + if !in_reply_to.is_null() { + mailimf_in_reply_to_free(in_reply_to); + } + if !references.is_null() { + mailimf_references_free(references); + } + if !subject.is_null() { + mailimf_subject_free(subject); + } + if !optional_field.is_null() { + mailimf_optional_field_free(optional_field); + } + } else { + *result = field; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + return res; +} +/* + mailimf_envelope_fields will parse the given fields (Date, + From, Sender, Reply-To, To, Cc, Bcc, Message-ID, In-Reply-To, + References and Subject), other fields will be added as optional + fields. + + @param message this is a string containing the header fields + @param length this is the size of the given string + @param indx this is a pointer to the start of the header fields in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_envelope_and_optional_fields_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_fields, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + list = 0 as *mut clist; + r = mailimf_struct_multiple_parse( + message, + length, + &mut cur_token, + &mut list, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut mailimf_field, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_envelope_or_optional_field_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_field_free)), + ); + match r { + 0 => { + /* do nothing */ + current_block = 11050875288958768710; + } + 1 => { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 7755940856643933760; + } else { + current_block = 11050875288958768710; + } + } + _ => { + res = r; + current_block = 7755940856643933760; + } + } + match current_block { + 11050875288958768710 => { + fields = mailimf_fields_new(list); + if fields.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !list.is_null() { + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_field_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } else { + *result = fields; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + return res; +} +unsafe fn mailimf_envelope_or_optional_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + r = mailimf_envelope_field_parse(message, length, indx, result); + if r == MAILIMF_NO_ERROR as libc::c_int { + return MAILIMF_NO_ERROR as libc::c_int; + } + cur_token = *indx; + r = mailimf_optional_field_parse(message, length, &mut cur_token, &mut optional_field); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + field = mailimf_field_new( + MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + optional_field, + ); + if field.is_null() { + mailimf_optional_field_free(optional_field); + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + *result = field; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +/* + mailimf_envelope_fields will parse the given fields as optional + fields. + + @param message this is a string containing the header fields + @param length this is the size of the given string + @param indx this is a pointer to the start of the header fields in + the given string, (* indx) is modified to point at the end + of the parsed data + @param result the result of the parse operation is stored in + (* result) + + @return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error +*/ +pub unsafe fn mailimf_optional_fields_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_fields, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + list = 0 as *mut clist; + r = mailimf_struct_multiple_parse( + message, + length, + &mut cur_token, + &mut list, + ::std::mem::transmute::< + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut *mut mailimf_field, + ) -> libc::c_int, + >, + Option< + unsafe fn( + _: *const libc::c_char, + _: size_t, + _: *mut size_t, + _: *mut libc::c_void, + ) -> libc::c_int, + >, + >(Some(mailimf_only_optional_field_parse)), + ::std::mem::transmute::< + Option ()>, + Option libc::c_int>, + >(Some(mailimf_field_free)), + ); + match r { + 0 => { + /* do nothing */ + current_block = 11050875288958768710; + } + 1 => { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 4409055091581443388; + } else { + current_block = 11050875288958768710; + } + } + _ => { + res = r; + current_block = 4409055091581443388; + } + } + match current_block { + 11050875288958768710 => { + fields = mailimf_fields_new(list); + if fields.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !list.is_null() { + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_field_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } else { + *result = fields; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + return res; +} +unsafe fn mailimf_only_optional_field_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailimf_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut optional_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + cur_token = *indx; + r = mailimf_optional_field_parse(message, length, &mut cur_token, &mut optional_field); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + field = mailimf_field_new( + MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + optional_field, + ); + if field.is_null() { + mailimf_optional_field_free(optional_field); + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + *result = field; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_custom_string_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut is_custom_char: Option libc::c_int>, +) -> libc::c_int { + let mut begin: size_t = 0; + let mut end: size_t = 0; + let mut gstr: *mut libc::c_char = 0 as *mut libc::c_char; + begin = *indx; + end = begin; + if end >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + while 0 != is_custom_char.expect("non-null function pointer")(*message.offset(end as isize)) { + end = end.wrapping_add(1); + if end >= length { + break; + } + } + if end != begin { + gstr = + malloc(end.wrapping_sub(begin).wrapping_add(1i32 as libc::size_t)) as *mut libc::c_char; + if gstr.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + strncpy( + gstr, + message.offset(begin as isize), + end.wrapping_sub(begin), + ); + *gstr.offset(end.wrapping_sub(begin) as isize) = '\u{0}' as i32 as libc::c_char; + *indx = end; + *result = gstr; + return MAILIMF_NO_ERROR as libc::c_int; + } else { + return MAILIMF_ERROR_PARSE as libc::c_int; + }; +} diff --git a/mmime/src/mailimf_types.rs b/mmime/src/mailimf_types.rs new file mode 100644 index 000000000..99ddf068c --- /dev/null +++ b/mmime/src/mailimf_types.rs @@ -0,0 +1,1182 @@ +use libc; + +use crate::clist::*; +use crate::other::*; + +/* + IMPORTANT NOTE: + + All allocation functions will take as argument allocated data + and will store these data in the structure they will allocate. + Data should be persistant during all the use of the structure + and will be freed by the free function of the structure + + allocation functions will return NULL on failure +*/ +/* + mailimf_date_time is a date + + - day is the day of month (1 to 31) + + - month (1 to 12) + + - year (4 digits) + + - hour (0 to 23) + + - min (0 to 59) + + - 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 dt_day: libc::c_int, + pub dt_month: libc::c_int, + pub dt_year: libc::c_int, + pub dt_hour: libc::c_int, + pub dt_min: libc::c_int, + pub dt_sec: libc::c_int, + pub dt_zone: libc::c_int, +} +/* this is the type of address */ +pub type unnamed = libc::c_uint; +/* if this is a group +(group_name: address1@domain1, + address2@domain2; ) */ +pub const MAILIMF_ADDRESS_GROUP: unnamed = 2; +/* if this is a mailbox (mailbox@domain) */ +pub const MAILIMF_ADDRESS_MAILBOX: unnamed = 1; +/* on parse error */ +pub const MAILIMF_ADDRESS_ERROR: unnamed = 0; +/* + mailimf_address is an address + + - type can be MAILIMF_ADDRESS_MAILBOX or MAILIMF_ADDRESS_GROUP + + - mailbox is a mailbox if type is MAILIMF_ADDRESS_MAILBOX + + - group is a group if type is MAILIMF_ADDRESS_GROUP +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_address { + pub ad_type: libc::c_int, + pub ad_data: unnamed_0, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_0 { + pub ad_mailbox: *mut mailimf_mailbox, + pub ad_group: *mut mailimf_group, +} +/* + mailimf_group is a group + + - display_name is the name that will be displayed for this group, + for example 'group_name' in + 'group_name: address1@domain1, address2@domain2;', should be allocated + with malloc() + + - mb_list is a list of mailboxes +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_group { + pub grp_display_name: *mut libc::c_char, + pub grp_mb_list: *mut mailimf_mailbox_list, +} +/* + mailimf_mailbox_list is a list of mailboxes + + - list is a list of mailboxes +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_mailbox_list { + pub mb_list: *mut clist, +} +/* + mailimf_mailbox is a mailbox + + - display_name is the name that will be displayed for this mailbox, + for example 'name' in '"name" , + should be allocated with malloc() + + - addr_spec is the mailbox, for example 'mailbox@domain' + in '"name" , should be allocated with malloc() +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_mailbox { + pub mb_display_name: *mut libc::c_char, + pub mb_addr_spec: *mut libc::c_char, +} +/* + mailimf_address_list is a list of addresses + + - list is a list of addresses +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_address_list { + pub ad_list: *mut clist, +} +/* + mailimf_body is the text part of a message + + - text is the beginning of the text part, it is a substring + of an other string + + - size is the size of the text part +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_body { + pub bd_text: *const libc::c_char, + pub bd_size: size_t, +} +/* + mailimf_message is the content of the message + + - msg_fields is the header fields of the message + + - msg_body is the text part of the message +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_message { + pub msg_fields: *mut mailimf_fields, + pub msg_body: *mut mailimf_body, +} +/* + mailimf_fields is a list of header fields + + - fld_list is a list of header fields +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_fields { + pub fld_list: *mut clist, +} +/* + mailimf_field is a field + + - fld_type is the type of the field + + - fld_data.fld_return_path is the parsed content of the Return-Path + field if type is MAILIMF_FIELD_RETURN_PATH + + - fld_data.fld_resent_date is the parsed content of the Resent-Date field + if type is MAILIMF_FIELD_RESENT_DATE + + - fld_data.fld_resent_from is the parsed content of the Resent-From field + + - fld_data.fld_resent_sender is the parsed content of the Resent-Sender field + + - fld_data.fld_resent_to is the parsed content of the Resent-To field + + - fld_data.fld_resent_cc is the parsed content of the Resent-Cc field + + - fld_data.fld_resent_bcc is the parsed content of the Resent-Bcc field + + - fld_data.fld_resent_msg_id is the parsed content of the Resent-Message-ID + field + + - fld_data.fld_orig_date is the parsed content of the Date field + + - fld_data.fld_from is the parsed content of the From field + + - fld_data.fld_sender is the parsed content of the Sender field + + - fld_data.fld_reply_to is the parsed content of the Reply-To field + + - fld_data.fld_to is the parsed content of the To field + + - fld_data.fld_cc is the parsed content of the Cc field + + - fld_data.fld_bcc is the parsed content of the Bcc field + + - fld_data.fld_message_id is the parsed content of the Message-ID field + + - fld_data.fld_in_reply_to is the parsed content of the In-Reply-To field + + - fld_data.fld_references is the parsed content of the References field + + - fld_data.fld_subject is the content of the Subject field + + - fld_data.fld_comments is the content of the Comments field + + - fld_data.fld_keywords is the parsed content of the Keywords field + + - fld_data.fld_optional_field is an other field and is not parsed +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_field { + pub fld_type: libc::c_int, + pub fld_data: unnamed_1, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_1 { + pub fld_return_path: *mut mailimf_return, + pub fld_resent_date: *mut mailimf_orig_date, + pub fld_resent_from: *mut mailimf_from, + pub fld_resent_sender: *mut mailimf_sender, + pub fld_resent_to: *mut mailimf_to, + pub fld_resent_cc: *mut mailimf_cc, + pub fld_resent_bcc: *mut mailimf_bcc, + pub fld_resent_msg_id: *mut mailimf_message_id, + pub fld_orig_date: *mut mailimf_orig_date, + pub fld_from: *mut mailimf_from, + pub fld_sender: *mut mailimf_sender, + pub fld_reply_to: *mut mailimf_reply_to, + pub fld_to: *mut mailimf_to, + pub fld_cc: *mut mailimf_cc, + pub fld_bcc: *mut mailimf_bcc, + pub fld_message_id: *mut mailimf_message_id, + pub fld_in_reply_to: *mut mailimf_in_reply_to, + pub fld_references: *mut mailimf_references, + pub fld_subject: *mut mailimf_subject, + pub fld_comments: *mut mailimf_comments, + pub fld_keywords: *mut mailimf_keywords, + pub fld_optional_field: *mut mailimf_optional_field, +} +/* + mailimf_optional_field is a non-parsed field + + - fld_name is the name of the field + + - fld_value is the value of the field +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_optional_field { + pub fld_name: *mut libc::c_char, + pub fld_value: *mut libc::c_char, +} +/* + mailimf_keywords is the parsed Keywords field + + - kw_list is the list of keywords +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_keywords { + pub kw_list: *mut clist, +} +/* + mailimf_comments is the parsed Comments field + + - cm_value is the value of the field +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_comments { + pub cm_value: *mut libc::c_char, +} +/* + mailimf_subject is the parsed Subject field + + - sbj_value is the value of the field +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_subject { + pub sbj_value: *mut libc::c_char, +} +/* + mailimf_references is the parsed References field + + - msg_id_list is the list of message identifiers +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_references { + pub mid_list: *mut clist, +} +/* + mailimf_in_reply_to is the parsed In-Reply-To field + + - mid_list is the list of message identifers +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_in_reply_to { + pub mid_list: *mut clist, +} +/* + mailimf_message_id is the parsed Message-ID field + + - mid_value is the message identifier +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_message_id { + pub mid_value: *mut libc::c_char, +} +/* + mailimf_bcc is the parsed Bcc field + + - bcc_addr_list is the parsed addres list +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_bcc { + pub bcc_addr_list: *mut mailimf_address_list, +} +/* + mailimf_cc is the parsed Cc field + + - cc_addr_list is the parsed addres list +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_cc { + pub cc_addr_list: *mut mailimf_address_list, +} +/* + mailimf_to is the parsed To field + + - to_addr_list is the parsed address list +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_to { + pub to_addr_list: *mut mailimf_address_list, +} +/* + mailimf_reply_to is the parsed Reply-To field + + - rt_addr_list is the parsed address list +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_reply_to { + pub rt_addr_list: *mut mailimf_address_list, +} +/* + mailimf_sender is the parsed Sender field + + - snd_mb is the parsed mailbox +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_sender { + pub snd_mb: *mut mailimf_mailbox, +} +/* + mailimf_from is the parsed From field + + - mb_list is the parsed mailbox list +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_from { + pub frm_mb_list: *mut mailimf_mailbox_list, +} +/* + mailimf_orig_date is the parsed Date field + + - date_time is the parsed date +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_orig_date { + pub dt_date_time: *mut mailimf_date_time, +} +/* + mailimf_return is the parsed Return-Path field + + - ret_path is the parsed value of Return-Path +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_return { + pub ret_path: *mut mailimf_path, +} +/* + mailimf_path is the parsed value of Return-Path + + - pt_addr_spec is a mailbox +*/ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailimf_path { + pub pt_addr_spec: *mut libc::c_char, +} +/* other field */ +pub const MAILIMF_FIELD_OPTIONAL_FIELD: unnamed_2 = 22; +/* Keywords */ +pub const MAILIMF_FIELD_KEYWORDS: unnamed_2 = 21; +/* Comments */ +pub const MAILIMF_FIELD_COMMENTS: unnamed_2 = 20; +/* Subject */ +pub const MAILIMF_FIELD_SUBJECT: unnamed_2 = 19; +/* References */ +pub const MAILIMF_FIELD_REFERENCES: unnamed_2 = 18; +/* In-Reply-To */ +pub const MAILIMF_FIELD_IN_REPLY_TO: unnamed_2 = 17; +/* Message-ID */ +pub const MAILIMF_FIELD_MESSAGE_ID: unnamed_2 = 16; +/* Bcc */ +pub const MAILIMF_FIELD_BCC: unnamed_2 = 15; +/* Cc */ +pub const MAILIMF_FIELD_CC: unnamed_2 = 14; +/* To */ +pub const MAILIMF_FIELD_TO: unnamed_2 = 13; +/* Reply-To */ +pub const MAILIMF_FIELD_REPLY_TO: unnamed_2 = 12; +/* Sender */ +pub const MAILIMF_FIELD_SENDER: unnamed_2 = 11; +/* From */ +pub const MAILIMF_FIELD_FROM: unnamed_2 = 10; +/* Date */ +pub const MAILIMF_FIELD_ORIG_DATE: unnamed_2 = 9; +/* Resent-Message-ID */ +pub const MAILIMF_FIELD_RESENT_MSG_ID: unnamed_2 = 8; +/* Resent-Bcc */ +pub const MAILIMF_FIELD_RESENT_BCC: unnamed_2 = 7; +/* Resent-Cc */ +pub const MAILIMF_FIELD_RESENT_CC: unnamed_2 = 6; +/* Resent-To */ +pub const MAILIMF_FIELD_RESENT_TO: unnamed_2 = 5; +/* Resent-Sender */ +pub const MAILIMF_FIELD_RESENT_SENDER: unnamed_2 = 4; +/* Resent-From */ +pub const MAILIMF_FIELD_RESENT_FROM: unnamed_2 = 3; +/* Resent-Date */ +pub const MAILIMF_FIELD_RESENT_DATE: unnamed_2 = 2; +/* Return-Path */ +pub const MAILIMF_FIELD_RETURN_PATH: unnamed_2 = 1; +/* this is a type of field */ +pub type unnamed_2 = libc::c_uint; +/* on parse error */ +pub const MAILIMF_FIELD_NONE: unnamed_2 = 0; +#[no_mangle] +pub unsafe fn mailimf_date_time_new( + mut dt_day: libc::c_int, + mut dt_month: libc::c_int, + mut dt_year: libc::c_int, + mut dt_hour: libc::c_int, + mut dt_min: libc::c_int, + mut dt_sec: libc::c_int, + mut dt_zone: libc::c_int, +) -> *mut mailimf_date_time { + let mut date_time: *mut mailimf_date_time = 0 as *mut mailimf_date_time; + date_time = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_date_time; + if date_time.is_null() { + return 0 as *mut mailimf_date_time; + } + (*date_time).dt_day = dt_day; + (*date_time).dt_month = dt_month; + (*date_time).dt_year = dt_year; + (*date_time).dt_hour = dt_hour; + (*date_time).dt_min = dt_min; + (*date_time).dt_sec = dt_sec; + (*date_time).dt_zone = dt_zone; + return date_time; +} +#[no_mangle] +pub unsafe fn mailimf_date_time_free(mut date_time: *mut mailimf_date_time) { + free(date_time as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_address_new( + mut ad_type: libc::c_int, + mut ad_mailbox: *mut mailimf_mailbox, + mut ad_group: *mut mailimf_group, +) -> *mut mailimf_address { + let mut address: *mut mailimf_address = 0 as *mut mailimf_address; + address = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_address; + if address.is_null() { + return 0 as *mut mailimf_address; + } + (*address).ad_type = ad_type; + match ad_type { + 1 => (*address).ad_data.ad_mailbox = ad_mailbox, + 2 => (*address).ad_data.ad_group = ad_group, + _ => {} + } + return address; +} +#[no_mangle] +pub unsafe fn mailimf_address_free(mut address: *mut mailimf_address) { + match (*address).ad_type { + 1 => { + mailimf_mailbox_free((*address).ad_data.ad_mailbox); + } + 2 => { + mailimf_group_free((*address).ad_data.ad_group); + } + _ => {} + } + free(address as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_group_free(mut group: *mut mailimf_group) { + if !(*group).grp_mb_list.is_null() { + mailimf_mailbox_list_free((*group).grp_mb_list); + } + mailimf_display_name_free((*group).grp_display_name); + free(group as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_display_name_free(mut display_name: *mut libc::c_char) { + mailimf_phrase_free(display_name); +} +#[no_mangle] +pub unsafe fn mailimf_phrase_free(mut phrase: *mut libc::c_char) { + free(phrase as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_mailbox_list_free(mut mb_list: *mut mailimf_mailbox_list) { + clist_foreach( + (*mb_list).mb_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_mailbox_free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*mb_list).mb_list); + free(mb_list as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_mailbox_free(mut mailbox: *mut mailimf_mailbox) { + if !(*mailbox).mb_display_name.is_null() { + mailimf_display_name_free((*mailbox).mb_display_name); + } + mailimf_addr_spec_free((*mailbox).mb_addr_spec); + free(mailbox as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_addr_spec_free(mut addr_spec: *mut libc::c_char) { + free(addr_spec as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_mailbox_new( + mut mb_display_name: *mut libc::c_char, + mut mb_addr_spec: *mut libc::c_char, +) -> *mut mailimf_mailbox { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + mb = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_mailbox; + if mb.is_null() { + return 0 as *mut mailimf_mailbox; + } + (*mb).mb_display_name = mb_display_name; + (*mb).mb_addr_spec = mb_addr_spec; + return mb; +} +#[no_mangle] +pub unsafe fn mailimf_group_new( + mut grp_display_name: *mut libc::c_char, + mut grp_mb_list: *mut mailimf_mailbox_list, +) -> *mut mailimf_group { + let mut group: *mut mailimf_group = 0 as *mut mailimf_group; + group = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_group; + if group.is_null() { + return 0 as *mut mailimf_group; + } + (*group).grp_display_name = grp_display_name; + (*group).grp_mb_list = grp_mb_list; + return group; +} +#[no_mangle] +pub unsafe fn mailimf_mailbox_list_new(mut mb_list: *mut clist) -> *mut mailimf_mailbox_list { + let mut mbl: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + mbl = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_mailbox_list; + if mbl.is_null() { + return 0 as *mut mailimf_mailbox_list; + } + (*mbl).mb_list = mb_list; + return mbl; +} + +pub unsafe fn mailimf_address_list_new(mut ad_list: *mut clist) -> *mut mailimf_address_list { + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + addr_list = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_address_list; + if addr_list.is_null() { + return 0 as *mut mailimf_address_list; + } + (*addr_list).ad_list = ad_list; + return addr_list; +} +#[no_mangle] +pub unsafe fn mailimf_address_list_free(mut addr_list: *mut mailimf_address_list) { + clist_foreach( + (*addr_list).ad_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_address_free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*addr_list).ad_list); + free(addr_list as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_body_new( + mut bd_text: *const libc::c_char, + mut bd_size: size_t, +) -> *mut mailimf_body { + let mut body: *mut mailimf_body = 0 as *mut mailimf_body; + body = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_body; + if body.is_null() { + return 0 as *mut mailimf_body; + } + (*body).bd_text = bd_text; + (*body).bd_size = bd_size; + return body; +} +#[no_mangle] +pub unsafe fn mailimf_body_free(mut body: *mut mailimf_body) { + free(body as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_message_new( + mut msg_fields: *mut mailimf_fields, + mut msg_body: *mut mailimf_body, +) -> *mut mailimf_message { + let mut message: *mut mailimf_message = 0 as *mut mailimf_message; + message = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_message; + if message.is_null() { + return 0 as *mut mailimf_message; + } + (*message).msg_fields = msg_fields; + (*message).msg_body = msg_body; + return message; +} +#[no_mangle] +pub unsafe fn mailimf_message_free(mut message: *mut mailimf_message) { + mailimf_body_free((*message).msg_body); + mailimf_fields_free((*message).msg_fields); + free(message as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_fields_free(mut fields: *mut mailimf_fields) { + if !(*fields).fld_list.is_null() { + clist_foreach( + (*fields).fld_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailimf_field_free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*fields).fld_list); + } + free(fields as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_field_free(mut field: *mut mailimf_field) { + match (*field).fld_type { + 1 => { + mailimf_return_free((*field).fld_data.fld_return_path); + } + 2 => { + mailimf_orig_date_free((*field).fld_data.fld_resent_date); + } + 3 => { + mailimf_from_free((*field).fld_data.fld_resent_from); + } + 4 => { + mailimf_sender_free((*field).fld_data.fld_resent_sender); + } + 5 => { + mailimf_to_free((*field).fld_data.fld_resent_to); + } + 6 => { + mailimf_cc_free((*field).fld_data.fld_resent_cc); + } + 7 => { + mailimf_bcc_free((*field).fld_data.fld_resent_bcc); + } + 8 => { + mailimf_message_id_free((*field).fld_data.fld_resent_msg_id); + } + 9 => { + mailimf_orig_date_free((*field).fld_data.fld_orig_date); + } + 10 => { + mailimf_from_free((*field).fld_data.fld_from); + } + 11 => { + mailimf_sender_free((*field).fld_data.fld_sender); + } + 12 => { + mailimf_reply_to_free((*field).fld_data.fld_reply_to); + } + 13 => { + mailimf_to_free((*field).fld_data.fld_to); + } + 14 => { + mailimf_cc_free((*field).fld_data.fld_cc); + } + 15 => { + mailimf_bcc_free((*field).fld_data.fld_bcc); + } + 16 => { + mailimf_message_id_free((*field).fld_data.fld_message_id); + } + 17 => { + mailimf_in_reply_to_free((*field).fld_data.fld_in_reply_to); + } + 18 => { + mailimf_references_free((*field).fld_data.fld_references); + } + 19 => { + mailimf_subject_free((*field).fld_data.fld_subject); + } + 20 => { + mailimf_comments_free((*field).fld_data.fld_comments); + } + 21 => { + mailimf_keywords_free((*field).fld_data.fld_keywords); + } + 22 => { + mailimf_optional_field_free((*field).fld_data.fld_optional_field); + } + _ => {} + } + free(field as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_optional_field_free(mut opt_field: *mut mailimf_optional_field) { + mailimf_field_name_free((*opt_field).fld_name); + mailimf_unstructured_free((*opt_field).fld_value); + free(opt_field as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_unstructured_free(mut unstructured: *mut libc::c_char) { + free(unstructured as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_field_name_free(mut field_name: *mut libc::c_char) { + free(field_name as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_keywords_free(mut keywords: *mut mailimf_keywords) { + clist_foreach( + (*keywords).kw_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailimf_phrase_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*keywords).kw_list); + free(keywords as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_comments_free(mut comments: *mut mailimf_comments) { + mailimf_unstructured_free((*comments).cm_value); + free(comments as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_subject_free(mut subject: *mut mailimf_subject) { + mailimf_unstructured_free((*subject).sbj_value); + free(subject as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_references_free(mut references: *mut mailimf_references) { + clist_foreach( + (*references).mid_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailimf_msg_id_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*references).mid_list); + free(references as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_msg_id_free(mut msg_id: *mut libc::c_char) { + free(msg_id as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_in_reply_to_free(mut in_reply_to: *mut mailimf_in_reply_to) { + clist_foreach( + (*in_reply_to).mid_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailimf_msg_id_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*in_reply_to).mid_list); + free(in_reply_to as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_message_id_free(mut message_id: *mut mailimf_message_id) { + if !(*message_id).mid_value.is_null() { + mailimf_msg_id_free((*message_id).mid_value); + } + free(message_id as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_bcc_free(mut bcc: *mut mailimf_bcc) { + if !(*bcc).bcc_addr_list.is_null() { + mailimf_address_list_free((*bcc).bcc_addr_list); + } + free(bcc as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_cc_free(mut cc: *mut mailimf_cc) { + if !(*cc).cc_addr_list.is_null() { + mailimf_address_list_free((*cc).cc_addr_list); + } + free(cc as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_to_free(mut to: *mut mailimf_to) { + if !(*to).to_addr_list.is_null() { + mailimf_address_list_free((*to).to_addr_list); + } + free(to as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_reply_to_free(mut reply_to: *mut mailimf_reply_to) { + if !(*reply_to).rt_addr_list.is_null() { + mailimf_address_list_free((*reply_to).rt_addr_list); + } + free(reply_to as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_sender_free(mut sender: *mut mailimf_sender) { + if !(*sender).snd_mb.is_null() { + mailimf_mailbox_free((*sender).snd_mb); + } + free(sender as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_from_free(mut from: *mut mailimf_from) { + if !(*from).frm_mb_list.is_null() { + mailimf_mailbox_list_free((*from).frm_mb_list); + } + free(from as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_orig_date_free(mut orig_date: *mut mailimf_orig_date) { + if !(*orig_date).dt_date_time.is_null() { + mailimf_date_time_free((*orig_date).dt_date_time); + } + free(orig_date as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_return_free(mut return_path: *mut mailimf_return) { + mailimf_path_free((*return_path).ret_path); + free(return_path as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_path_free(mut path: *mut mailimf_path) { + if !(*path).pt_addr_spec.is_null() { + mailimf_addr_spec_free((*path).pt_addr_spec); + } + free(path as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_fields_new(mut fld_list: *mut clist) -> *mut mailimf_fields { + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + fields = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_fields; + if fields.is_null() { + return 0 as *mut mailimf_fields; + } + (*fields).fld_list = fld_list; + return fields; +} +#[no_mangle] +pub unsafe fn mailimf_field_new( + mut fld_type: libc::c_int, + mut fld_return_path: *mut mailimf_return, + mut fld_resent_date: *mut mailimf_orig_date, + mut fld_resent_from: *mut mailimf_from, + mut fld_resent_sender: *mut mailimf_sender, + mut fld_resent_to: *mut mailimf_to, + mut fld_resent_cc: *mut mailimf_cc, + mut fld_resent_bcc: *mut mailimf_bcc, + mut fld_resent_msg_id: *mut mailimf_message_id, + mut fld_orig_date: *mut mailimf_orig_date, + mut fld_from: *mut mailimf_from, + mut fld_sender: *mut mailimf_sender, + mut fld_reply_to: *mut mailimf_reply_to, + mut fld_to: *mut mailimf_to, + mut fld_cc: *mut mailimf_cc, + mut fld_bcc: *mut mailimf_bcc, + mut fld_message_id: *mut mailimf_message_id, + mut fld_in_reply_to: *mut mailimf_in_reply_to, + mut fld_references: *mut mailimf_references, + mut fld_subject: *mut mailimf_subject, + mut fld_comments: *mut mailimf_comments, + mut fld_keywords: *mut mailimf_keywords, + mut fld_optional_field: *mut mailimf_optional_field, +) -> *mut mailimf_field { + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + field = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_field; + if field.is_null() { + return 0 as *mut mailimf_field; + } + (*field).fld_type = fld_type; + match fld_type { + 1 => (*field).fld_data.fld_return_path = fld_return_path, + 2 => (*field).fld_data.fld_resent_date = fld_resent_date, + 3 => (*field).fld_data.fld_resent_from = fld_resent_from, + 4 => (*field).fld_data.fld_resent_sender = fld_resent_sender, + 5 => (*field).fld_data.fld_resent_to = fld_resent_to, + 6 => (*field).fld_data.fld_resent_cc = fld_resent_cc, + 7 => (*field).fld_data.fld_resent_bcc = fld_resent_bcc, + 8 => (*field).fld_data.fld_resent_msg_id = fld_resent_msg_id, + 9 => (*field).fld_data.fld_orig_date = fld_orig_date, + 10 => (*field).fld_data.fld_from = fld_from, + 11 => (*field).fld_data.fld_sender = fld_sender, + 12 => (*field).fld_data.fld_reply_to = fld_reply_to, + 13 => (*field).fld_data.fld_to = fld_to, + 14 => (*field).fld_data.fld_cc = fld_cc, + 15 => (*field).fld_data.fld_bcc = fld_bcc, + 16 => (*field).fld_data.fld_message_id = fld_message_id, + 17 => (*field).fld_data.fld_in_reply_to = fld_in_reply_to, + 18 => (*field).fld_data.fld_references = fld_references, + 19 => (*field).fld_data.fld_subject = fld_subject, + 20 => (*field).fld_data.fld_comments = fld_comments, + 21 => (*field).fld_data.fld_keywords = fld_keywords, + 22 => (*field).fld_data.fld_optional_field = fld_optional_field, + _ => {} + } + return field; +} +#[no_mangle] +pub unsafe fn mailimf_orig_date_new( + mut dt_date_time: *mut mailimf_date_time, +) -> *mut mailimf_orig_date { + let mut orig_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + orig_date = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_orig_date; + if orig_date.is_null() { + return 0 as *mut mailimf_orig_date; + } + (*orig_date).dt_date_time = dt_date_time; + return orig_date; +} +#[no_mangle] +pub unsafe fn mailimf_from_new(mut frm_mb_list: *mut mailimf_mailbox_list) -> *mut mailimf_from { + let mut from: *mut mailimf_from = 0 as *mut mailimf_from; + from = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_from; + if from.is_null() { + return 0 as *mut mailimf_from; + } + (*from).frm_mb_list = frm_mb_list; + return from; +} +#[no_mangle] +pub unsafe fn mailimf_sender_new(mut snd_mb: *mut mailimf_mailbox) -> *mut mailimf_sender { + let mut sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + sender = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_sender; + if sender.is_null() { + return 0 as *mut mailimf_sender; + } + (*sender).snd_mb = snd_mb; + return sender; +} +#[no_mangle] +pub unsafe fn mailimf_reply_to_new( + mut rt_addr_list: *mut mailimf_address_list, +) -> *mut mailimf_reply_to { + let mut reply_to: *mut mailimf_reply_to = 0 as *mut mailimf_reply_to; + reply_to = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_reply_to; + if reply_to.is_null() { + return 0 as *mut mailimf_reply_to; + } + (*reply_to).rt_addr_list = rt_addr_list; + return reply_to; +} +#[no_mangle] +pub unsafe fn mailimf_to_new(mut to_addr_list: *mut mailimf_address_list) -> *mut mailimf_to { + let mut to: *mut mailimf_to = 0 as *mut mailimf_to; + to = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_to; + if to.is_null() { + return 0 as *mut mailimf_to; + } + (*to).to_addr_list = to_addr_list; + return to; +} +#[no_mangle] +pub unsafe fn mailimf_cc_new(mut cc_addr_list: *mut mailimf_address_list) -> *mut mailimf_cc { + let mut cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + cc = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_cc; + if cc.is_null() { + return 0 as *mut mailimf_cc; + } + (*cc).cc_addr_list = cc_addr_list; + return cc; +} +#[no_mangle] +pub unsafe fn mailimf_bcc_new(mut bcc_addr_list: *mut mailimf_address_list) -> *mut mailimf_bcc { + let mut bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + bcc = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_bcc; + if bcc.is_null() { + return 0 as *mut mailimf_bcc; + } + (*bcc).bcc_addr_list = bcc_addr_list; + return bcc; +} +#[no_mangle] +pub unsafe fn mailimf_message_id_new(mut mid_value: *mut libc::c_char) -> *mut mailimf_message_id { + let mut message_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + message_id = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_message_id; + if message_id.is_null() { + return 0 as *mut mailimf_message_id; + } + (*message_id).mid_value = mid_value; + return message_id; +} +#[no_mangle] +pub unsafe fn mailimf_in_reply_to_new(mut mid_list: *mut clist) -> *mut mailimf_in_reply_to { + let mut in_reply_to: *mut mailimf_in_reply_to = 0 as *mut mailimf_in_reply_to; + in_reply_to = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_in_reply_to; + if in_reply_to.is_null() { + return 0 as *mut mailimf_in_reply_to; + } + (*in_reply_to).mid_list = mid_list; + return in_reply_to; +} +/* != NULL */ +#[no_mangle] +pub unsafe fn mailimf_references_new(mut mid_list: *mut clist) -> *mut mailimf_references { + let mut ref_0: *mut mailimf_references = 0 as *mut mailimf_references; + ref_0 = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_references; + if ref_0.is_null() { + return 0 as *mut mailimf_references; + } + (*ref_0).mid_list = mid_list; + return ref_0; +} +#[no_mangle] +pub unsafe fn mailimf_subject_new(mut sbj_value: *mut libc::c_char) -> *mut mailimf_subject { + let mut subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + subject = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_subject; + if subject.is_null() { + return 0 as *mut mailimf_subject; + } + (*subject).sbj_value = sbj_value; + return subject; +} +#[no_mangle] +pub unsafe fn mailimf_comments_new(mut cm_value: *mut libc::c_char) -> *mut mailimf_comments { + let mut comments: *mut mailimf_comments = 0 as *mut mailimf_comments; + comments = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_comments; + if comments.is_null() { + return 0 as *mut mailimf_comments; + } + (*comments).cm_value = cm_value; + return comments; +} +#[no_mangle] +pub unsafe fn mailimf_keywords_new(mut kw_list: *mut clist) -> *mut mailimf_keywords { + let mut keywords: *mut mailimf_keywords = 0 as *mut mailimf_keywords; + keywords = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_keywords; + if keywords.is_null() { + return 0 as *mut mailimf_keywords; + } + (*keywords).kw_list = kw_list; + return keywords; +} +#[no_mangle] +pub unsafe fn mailimf_return_new(mut ret_path: *mut mailimf_path) -> *mut mailimf_return { + let mut return_path: *mut mailimf_return = 0 as *mut mailimf_return; + return_path = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_return; + if return_path.is_null() { + return 0 as *mut mailimf_return; + } + (*return_path).ret_path = ret_path; + return return_path; +} +#[no_mangle] +pub unsafe fn mailimf_path_new(mut pt_addr_spec: *mut libc::c_char) -> *mut mailimf_path { + let mut path: *mut mailimf_path = 0 as *mut mailimf_path; + path = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailimf_path; + if path.is_null() { + return 0 as *mut mailimf_path; + } + (*path).pt_addr_spec = pt_addr_spec; + return path; +} +#[no_mangle] +pub unsafe fn mailimf_optional_field_new( + mut fld_name: *mut libc::c_char, + mut fld_value: *mut libc::c_char, +) -> *mut mailimf_optional_field { + let mut opt_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + opt_field = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailimf_optional_field; + if opt_field.is_null() { + return 0 as *mut mailimf_optional_field; + } + (*opt_field).fld_name = fld_name; + (*opt_field).fld_value = fld_value; + return opt_field; +} +/* internal use */ +#[no_mangle] +pub unsafe fn mailimf_atom_free(mut atom: *mut libc::c_char) { + free(atom as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_dot_atom_free(mut dot_atom: *mut libc::c_char) { + free(dot_atom as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_dot_atom_text_free(mut dot_atom: *mut libc::c_char) { + free(dot_atom as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_quoted_string_free(mut quoted_string: *mut libc::c_char) { + free(quoted_string as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_word_free(mut word: *mut libc::c_char) { + free(word as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_angle_addr_free(mut angle_addr: *mut libc::c_char) { + free(angle_addr as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_local_part_free(mut local_part: *mut libc::c_char) { + free(local_part as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_domain_free(mut domain: *mut libc::c_char) { + free(domain as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_domain_literal_free(mut domain_literal: *mut libc::c_char) { + free(domain_literal as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_id_left_free(mut id_left: *mut libc::c_char) { + free(id_left as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_id_right_free(mut id_right: *mut libc::c_char) { + free(id_right as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_no_fold_quote_free(mut nfq: *mut libc::c_char) { + free(nfq as *mut libc::c_void); +} +#[no_mangle] +pub unsafe fn mailimf_no_fold_literal_free(mut nfl: *mut libc::c_char) { + free(nfl as *mut libc::c_void); +} diff --git a/mmime/src/mailimf_types_helper.rs b/mmime/src/mailimf_types_helper.rs new file mode 100644 index 000000000..8dc7ecf83 --- /dev/null +++ b/mmime/src/mailimf_types_helper.rs @@ -0,0 +1,89 @@ +use crate::clist::*; +use crate::mailimf_types::*; +use crate::other::*; + +/* + this function creates a new mailimf_fields structure with no fields +*/ +pub unsafe fn mailimf_fields_new_empty() -> *mut mailimf_fields { + let mut list: *mut clist = 0 as *mut clist; + let mut fields_list: *mut mailimf_fields = 0 as *mut mailimf_fields; + list = clist_new(); + if list.is_null() { + return 0 as *mut mailimf_fields; + } + fields_list = mailimf_fields_new(list); + if fields_list.is_null() { + return 0 as *mut mailimf_fields; + } + return fields_list; +} +/* + this function adds a field to the mailimf_fields structure + + @return MAILIMF_NO_ERROR will be returned on success, + other code will be returned otherwise +*/ +pub unsafe fn mailimf_fields_add( + mut fields: *mut mailimf_fields, + mut field: *mut mailimf_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = clist_insert_after( + (*fields).fld_list, + (*(*fields).fld_list).last, + field as *mut libc::c_void, + ); + if r < 0i32 { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +/* + mailimf_field_new_custom creates a new field of type optional + + @param name should be allocated with malloc() + @param value should be allocated with malloc() +*/ +pub unsafe fn mailimf_field_new_custom( + mut name: *mut libc::c_char, + mut value: *mut libc::c_char, +) -> *mut mailimf_field { + let mut opt_field: *mut mailimf_optional_field = 0 as *mut mailimf_optional_field; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + opt_field = mailimf_optional_field_new(name, value); + if !opt_field.is_null() { + field = mailimf_field_new( + MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + opt_field, + ); + if field.is_null() { + mailimf_optional_field_free(opt_field); + } else { + return field; + } + } + return 0 as *mut mailimf_field; +} diff --git a/mmime/src/mailimf_write_generic.rs b/mmime/src/mailimf_write_generic.rs new file mode 100644 index 000000000..8c813754b --- /dev/null +++ b/mmime/src/mailimf_write_generic.rs @@ -0,0 +1,2025 @@ +use crate::clist::*; +use crate::mailimf_types::*; +use crate::other::*; + +pub const STATE_WORD: libc::c_uint = 1; +pub const STATE_SPACE: libc::c_uint = 2; +pub const STATE_BEGIN: libc::c_uint = 0; + +/* + mailimf_string_write writes a string to a given stream + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param str is the string to write +*/ +pub unsafe fn mailimf_string_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut str: *const libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut count: size_t = 0; + let mut block_begin: *const libc::c_char = 0 as *const libc::c_char; + let mut p: *const libc::c_char = 0 as *const libc::c_char; + let mut done: libc::c_int = 0; + p = str; + block_begin = str; + count = 0i32 as size_t; + while length > 0i32 as libc::size_t { + if count >= 998i32 as libc::size_t { + r = flush_buf(do_write, data, block_begin, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = do_write.expect("non-null function pointer")( + data, + b"\r\n\x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 3]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + count = 0i32 as size_t; + block_begin = p; + *col = 0i32 + } + match *p as libc::c_int { + 10 => { + r = flush_buf(do_write, data, block_begin, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = do_write.expect("non-null function pointer")( + data, + b"\r\n\x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 3]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + p = p.offset(1isize); + length = length.wrapping_sub(1); + count = 0i32 as size_t; + block_begin = p; + *col = 0i32 + } + 13 => { + done = 0i32; + if length >= 2i32 as libc::size_t { + if *p.offset(1isize) as libc::c_int == '\n' as i32 { + r = flush_buf(do_write, data, block_begin, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = do_write.expect("non-null function pointer")( + data, + b"\r\n\x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 3]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + p = p.offset(2isize); + length = (length as libc::size_t).wrapping_sub(2i32 as libc::size_t) + as size_t as size_t; + count = 0i32 as size_t; + block_begin = p; + *col = 0i32; + done = 1i32 + } + } + if 0 == done { + r = flush_buf(do_write, data, block_begin, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = do_write.expect("non-null function pointer")( + data, + b"\r\n\x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 3]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + p = p.offset(1isize); + length = length.wrapping_sub(1); + count = 0i32 as size_t; + block_begin = p; + *col = 0i32 + } + } + _ => { + p = p.offset(1isize); + count = count.wrapping_add(1); + length = length.wrapping_sub(1) + } + } + } + r = flush_buf(do_write, data, block_begin, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *col = (*col as libc::size_t).wrapping_add(count) as libc::c_int as libc::c_int; + return MAILIMF_NO_ERROR as libc::c_int; +} +/* ************************ */ +#[inline] +unsafe fn flush_buf( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut str: *const libc::c_char, + mut length: size_t, +) -> libc::c_int { + if length != 0i32 as libc::size_t { + let mut r: libc::c_int = 0; + if length > 0i32 as libc::size_t { + r = do_write.expect("non-null function pointer")(data, str, length); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + mailimf_fields_write writes the fields to a given stream + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param fields is the fields to write +*/ + +pub unsafe fn mailimf_fields_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut fields: *mut mailimf_fields, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut r: libc::c_int = 0; + r = mailimf_field_write_driver( + do_write, + data, + col, + (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_field, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + mailimf_field_write writes a field to a given stream + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param field is the field to write +*/ +pub unsafe fn mailimf_field_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut field: *mut mailimf_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*field).fld_type { + 1 => { + r = mailimf_return_write_driver(do_write, data, col, (*field).fld_data.fld_return_path) + } + 2 => { + r = mailimf_resent_date_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_resent_date, + ) + } + 3 => { + r = mailimf_resent_from_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_resent_from, + ) + } + 4 => { + r = mailimf_resent_sender_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_resent_sender, + ) + } + 5 => { + r = mailimf_resent_to_write_driver(do_write, data, col, (*field).fld_data.fld_resent_to) + } + 6 => { + r = mailimf_resent_cc_write_driver(do_write, data, col, (*field).fld_data.fld_resent_cc) + } + 7 => { + r = mailimf_resent_bcc_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_resent_bcc, + ) + } + 8 => { + r = mailimf_resent_msg_id_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_resent_msg_id, + ) + } + 9 => { + r = mailimf_orig_date_write_driver(do_write, data, col, (*field).fld_data.fld_orig_date) + } + 10 => r = mailimf_from_write_driver(do_write, data, col, (*field).fld_data.fld_from), + 11 => r = mailimf_sender_write_driver(do_write, data, col, (*field).fld_data.fld_sender), + 12 => { + r = mailimf_reply_to_write_driver(do_write, data, col, (*field).fld_data.fld_reply_to) + } + 13 => r = mailimf_to_write_driver(do_write, data, col, (*field).fld_data.fld_to), + 14 => r = mailimf_cc_write_driver(do_write, data, col, (*field).fld_data.fld_cc), + 15 => r = mailimf_bcc_write_driver(do_write, data, col, (*field).fld_data.fld_bcc), + 16 => { + r = mailimf_message_id_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_message_id, + ) + } + 17 => { + r = mailimf_in_reply_to_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_in_reply_to, + ) + } + 18 => { + r = mailimf_references_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_references, + ) + } + 19 => r = mailimf_subject_write_driver(do_write, data, col, (*field).fld_data.fld_subject), + 20 => { + r = mailimf_comments_write_driver(do_write, data, col, (*field).fld_data.fld_comments) + } + 21 => { + r = mailimf_keywords_write_driver(do_write, data, col, (*field).fld_data.fld_keywords) + } + 22 => { + r = mailimf_optional_field_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_optional_field, + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_optional_field_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut field: *mut mailimf_optional_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + if strlen((*field).fld_name).wrapping_add(2i32 as libc::size_t) > 998i32 as libc::size_t { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*field).fld_name, + strlen((*field).fld_name), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b": \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_header_string_write_driver( + do_write, + data, + col, + (*field).fld_value, + strlen((*field).fld_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + mailimf_header_string_write writes a header value and fold the header + if needed. + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param str is the string to write +*/ +pub unsafe fn mailimf_header_string_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut str: *const libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut p: *const libc::c_char = 0 as *const libc::c_char; + let mut word_begin: *const libc::c_char = 0 as *const libc::c_char; + let mut first: libc::c_int = 0; + state = STATE_BEGIN as libc::c_int; + p = str; + word_begin = p; + first = 1i32; + while length > 0i32 as libc::size_t { + match state { + 0 => match *p as libc::c_int { + 13 | 10 | 32 | 9 => { + p = p.offset(1isize); + length = length.wrapping_sub(1) + } + _ => { + word_begin = p; + state = STATE_WORD as libc::c_int + } + }, + 2 => match *p as libc::c_int { + 13 | 10 | 32 | 9 => { + p = p.offset(1isize); + length = length.wrapping_sub(1) + } + _ => { + word_begin = p; + state = STATE_WORD as libc::c_int + } + }, + 1 => match *p as libc::c_int { + 13 | 10 | 32 | 9 => { + if p.wrapping_offset_from(word_begin) as libc::c_int + + *col as libc::c_int + + 1i32 as libc::c_int + > 72i32 as libc::c_int + { + mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 4]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + } else if 0 == first { + mailimf_string_write_driver( + do_write, + data, + col, + b" \x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + } + first = 0i32; + mailimf_string_write_driver( + do_write, + data, + col, + word_begin, + p.wrapping_offset_from(word_begin) as libc::c_int as size_t, + ); + state = STATE_SPACE as libc::c_int + } + _ => { + if p.wrapping_offset_from(word_begin) as libc::c_int + *col as libc::c_int + >= 998i32 as libc::c_int + { + mailimf_string_write_driver( + do_write, + data, + col, + word_begin, + p.wrapping_offset_from(word_begin) as libc::c_int as size_t, + ); + mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 4]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + word_begin = p + } + p = p.offset(1isize); + length = length.wrapping_sub(1) + } + }, + _ => {} + } + } + if state == STATE_WORD as libc::c_int { + if p.wrapping_offset_from(word_begin) as libc::c_int + *col as libc::c_int + >= 72i32 as libc::c_int + { + mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + (::std::mem::size_of::<[libc::c_char; 4]>() as libc::size_t) + .wrapping_sub(1i32 as libc::size_t), + ); + } else if 0 == first { + mailimf_string_write_driver( + do_write, + data, + col, + b" \x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + } + first = 0i32; + mailimf_string_write_driver( + do_write, + data, + col, + word_begin, + p.wrapping_offset_from(word_begin) as libc::c_int as size_t, + ); + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_keywords_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut keywords: *mut mailimf_keywords, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut first: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Keywords: \x00" as *const u8 as *const libc::c_char, + 10i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + first = 1i32; + cur = (*(*keywords).kw_list).first; + while !cur.is_null() { + let mut keyword: *mut libc::c_char = 0 as *mut libc::c_char; + let mut len: size_t = 0; + keyword = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut libc::c_char; + len = strlen(keyword); + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b", \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + first = 0i32 + } + r = mailimf_header_string_write_driver(do_write, data, col, keyword, len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_comments_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut comments: *mut mailimf_comments, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Comments: \x00" as *const u8 as *const libc::c_char, + 10i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_header_string_write_driver( + do_write, + data, + col, + (*comments).cm_value, + strlen((*comments).cm_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_subject_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut subject: *mut mailimf_subject, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Subject: \x00" as *const u8 as *const libc::c_char, + 9i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_header_string_write_driver( + do_write, + data, + col, + (*subject).sbj_value, + strlen((*subject).sbj_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_references_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut references: *mut mailimf_references, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"References: \x00" as *const u8 as *const libc::c_char, + 12i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_msg_id_list_write_driver(do_write, data, col, (*references).mid_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_msg_id_list_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut mid_list: *mut clist, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut r: libc::c_int = 0; + let mut first: libc::c_int = 0; + first = 1i32; + cur = (*mid_list).first; + while !cur.is_null() { + let mut msgid: *mut libc::c_char = 0 as *mut libc::c_char; + let mut len: size_t = 0; + msgid = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut libc::c_char; + len = strlen(msgid); + if 0 == first { + if *col > 1i32 { + if (*col as libc::size_t).wrapping_add(len) >= 72i32 as libc::size_t { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + first = 1i32 + } + } + } + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b" \x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + first = 0i32 + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver(do_write, data, col, msgid, len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_in_reply_to_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut in_reply_to: *mut mailimf_in_reply_to, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"In-Reply-To: \x00" as *const u8 as *const libc::c_char, + 13i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_msg_id_list_write_driver(do_write, data, col, (*in_reply_to).mid_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_message_id_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut message_id: *mut mailimf_message_id, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Message-ID: \x00" as *const u8 as *const libc::c_char, + 12i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*message_id).mid_value, + strlen((*message_id).mid_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_bcc_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut bcc: *mut mailimf_bcc, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Bcc: \x00" as *const u8 as *const libc::c_char, + 5i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if !(*bcc).bcc_addr_list.is_null() { + r = mailimf_address_list_write_driver(do_write, data, col, (*bcc).bcc_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +pub unsafe fn mailimf_address_list_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut addr_list: *mut mailimf_address_list, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut r: libc::c_int = 0; + let mut first: libc::c_int = 0; + first = 1i32; + cur = (*(*addr_list).ad_list).first; + while !cur.is_null() { + let mut addr: *mut mailimf_address = 0 as *mut mailimf_address; + addr = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_address; + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b", \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + first = 0i32 + } + r = mailimf_address_write_driver(do_write, data, col, addr); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_address_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut addr: *mut mailimf_address, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*addr).ad_type { + 1 => { + r = mailimf_mailbox_write_driver(do_write, data, col, (*addr).ad_data.ad_mailbox); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 2 => { + r = mailimf_group_write_driver(do_write, data, col, (*addr).ad_data.ad_group); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + _ => {} + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_group_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut group: *mut mailimf_group, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_header_string_write_driver( + do_write, + data, + col, + (*group).grp_display_name, + strlen((*group).grp_display_name), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b": \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if !(*group).grp_mb_list.is_null() { + r = mailimf_mailbox_list_write_driver(do_write, data, col, (*group).grp_mb_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b";\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailimf_mailbox_list_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut mb_list: *mut mailimf_mailbox_list, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut r: libc::c_int = 0; + let mut first: libc::c_int = 0; + first = 1i32; + cur = (*(*mb_list).mb_list).first; + while !cur.is_null() { + let mut mb: *mut mailimf_mailbox = 0 as *mut mailimf_mailbox; + mb = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_mailbox; + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b", \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + first = 0i32 + } + r = mailimf_mailbox_write_driver(do_write, data, col, mb); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_mailbox_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut mb: *mut mailimf_mailbox, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut do_fold: libc::c_int = 0; + if !(*mb).mb_display_name.is_null() { + if 0 != is_atext((*mb).mb_display_name) { + r = mailimf_header_string_write_driver( + do_write, + data, + col, + (*mb).mb_display_name, + strlen((*mb).mb_display_name), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + if !(*mb).mb_display_name.is_null() { + if (*col as libc::size_t).wrapping_add(strlen((*mb).mb_display_name)) + >= 72i32 as libc::size_t + { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } + if strlen((*mb).mb_display_name) > (998i32 / 2i32) as libc::size_t { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*mb).mb_display_name, + strlen((*mb).mb_display_name), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + do_fold = 0i32; + if *col > 1i32 { + if (*col as libc::size_t) + .wrapping_add(strlen((*mb).mb_addr_spec)) + .wrapping_add(3i32 as libc::size_t) + >= 72i32 as libc::size_t + { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + do_fold = 1i32 + } + } + if 0 != do_fold { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ) + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b" <\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ) + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*mb).mb_addr_spec, + strlen((*mb).mb_addr_spec), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + if (*col as libc::size_t).wrapping_add(strlen((*mb).mb_addr_spec)) >= 72i32 as libc::size_t + { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*mb).mb_addr_spec, + strlen((*mb).mb_addr_spec), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + mailimf_quoted_string_write writes a string that is quoted + to a given stream + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param string is the string to quote and write +*/ +pub unsafe fn mailimf_quoted_string_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut string: *const libc::c_char, + mut len: size_t, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut i: size_t = 0; + r = do_write.expect("non-null function pointer")( + data, + b"\"\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + i = 0i32 as size_t; + while i < len { + match *string.offset(i as isize) as libc::c_int { + 92 | 34 => { + r = do_write.expect("non-null function pointer")( + data, + b"\\\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + r = do_write.expect("non-null function pointer")( + data, + &*string.offset(i as isize), + 1i32 as size_t, + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + *col += 2i32 + } + _ => { + r = do_write.expect("non-null function pointer")( + data, + &*string.offset(i as isize), + 1i32 as size_t, + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + *col += 1 + } + } + i = i.wrapping_add(1) + } + r = do_write.expect("non-null function pointer")( + data, + b"\"\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r == 0i32 { + return MAILIMF_ERROR_FILE as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +static int +atext = ALPHA / DIGIT / ; Any character except controls, + "!" / "#" / ; SP, and specials. + "$" / "%" / ; Used for atoms + "&" / "'" / + "*" / "+" / + "-" / "/" / + "=" / "?" / + "^" / "_" / + "`" / "{" / + "|" / "}" / + "~" +*/ +unsafe fn is_atext(mut s: *const libc::c_char) -> libc::c_int { + let mut p: *const libc::c_char = 0 as *const libc::c_char; + p = s; + while *p as libc::c_int != 0i32 { + if !(0 != isalpha(*p as libc::c_uchar as libc::c_int)) { + if !(0 != isdigit(*p as libc::c_uchar as libc::c_int)) { + match *p as libc::c_int { + 32 | 9 | 33 | 35 | 36 | 37 | 38 | 39 | 42 | 43 | 45 | 47 | 61 | 63 | 94 + | 95 | 96 | 123 | 124 | 125 | 126 => {} + _ => return 0i32, + } + } + } + p = p.offset(1isize) + } + return 1i32; +} + +unsafe fn mailimf_cc_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut cc: *mut mailimf_cc, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Cc: \x00" as *const u8 as *const libc::c_char, + 4i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_address_list_write_driver(do_write, data, col, (*cc).cc_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_to_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut to: *mut mailimf_to, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"To: \x00" as *const u8 as *const libc::c_char, + 4i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_address_list_write_driver(do_write, data, col, (*to).to_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_reply_to_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut reply_to: *mut mailimf_reply_to, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Reply-To: \x00" as *const u8 as *const libc::c_char, + 10i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_address_list_write_driver(do_write, data, col, (*reply_to).rt_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_sender_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut sender: *mut mailimf_sender, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Sender: \x00" as *const u8 as *const libc::c_char, + 8i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_mailbox_write_driver(do_write, data, col, (*sender).snd_mb); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_from_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut from: *mut mailimf_from, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"From: \x00" as *const u8 as *const libc::c_char, + 6i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_mailbox_list_write_driver(do_write, data, col, (*from).frm_mb_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * $Id: mailimf_write_generic.c,v 1.3 2006/05/22 13:39:42 hoa Exp $ + */ +unsafe fn mailimf_orig_date_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut date: *mut mailimf_orig_date, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Date: \x00" as *const u8 as *const libc::c_char, + 6i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_date_time_write_driver(do_write, data, col, (*date).dt_date_time); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_date_time_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut date_time: *mut mailimf_date_time, +) -> libc::c_int { + let wday = dayofweek( + (*date_time).dt_year, + (*date_time).dt_month, + (*date_time).dt_day, + ); + + let date_str = format!( + "{}, {} {} {} {:02}:{:02}:{:02} {:+05}", + week_of_day_str[wday as usize], + (*date_time).dt_day, + month_str[((*date_time).dt_month - 1i32) as usize], + (*date_time).dt_year, + (*date_time).dt_hour, + (*date_time).dt_min, + (*date_time).dt_sec, + (*date_time).dt_zone, + ); + let date_str_c = std::ffi::CString::new(date_str).unwrap(); + let r = mailimf_string_write_driver( + do_write, + data, + col, + date_str_c.as_ptr() as *mut _, + strlen(date_str_c.as_ptr()), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +static mut month_str: [&'static str; 12] = [ + "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 */ +/* y > 1752 */ +unsafe fn dayofweek( + mut year: libc::c_int, + 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( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut message_id: *mut mailimf_message_id, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-Message-ID: \x00" as *const u8 as *const libc::c_char, + 19i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*message_id).mid_value, + strlen((*message_id).mid_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_bcc_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut bcc: *mut mailimf_bcc, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-Bcc: \x00" as *const u8 as *const libc::c_char, + 12i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if !(*bcc).bcc_addr_list.is_null() { + r = mailimf_address_list_write_driver(do_write, data, col, (*bcc).bcc_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_cc_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut cc: *mut mailimf_cc, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-Cc: \x00" as *const u8 as *const libc::c_char, + 11i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_address_list_write_driver(do_write, data, col, (*cc).cc_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_to_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut to: *mut mailimf_to, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-To: \x00" as *const u8 as *const libc::c_char, + 11i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_address_list_write_driver(do_write, data, col, (*to).to_addr_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_sender_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut sender: *mut mailimf_sender, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-Sender: \x00" as *const u8 as *const libc::c_char, + 15i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_mailbox_write_driver(do_write, data, col, (*sender).snd_mb); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_from_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut from: *mut mailimf_from, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-From: \x00" as *const u8 as *const libc::c_char, + 13i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_mailbox_list_write_driver(do_write, data, col, (*from).frm_mb_list); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_resent_date_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut date: *mut mailimf_orig_date, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Resent-Date: \x00" as *const u8 as *const libc::c_char, + 13i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_date_time_write_driver(do_write, data, col, (*date).dt_date_time); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_return_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut return_path: *mut mailimf_return, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Return-Path: \x00" as *const u8 as *const libc::c_char, + 13i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_path_write_driver(do_write, data, col, (*return_path).ret_path); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailimf_path_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut path: *mut mailimf_path, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if !(*path).pt_addr_spec.is_null() { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*path).pt_addr_spec, + strlen((*path).pt_addr_spec), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + mailimf_envelope_fields_write writes only some fields to a given stream + + @param f is the stream + @param col (* col) is the column number where we will start to + write the text, the ending column will be stored in (* col) + @param fields is the fields to write +*/ +pub unsafe fn mailimf_envelope_fields_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut fields: *mut mailimf_fields, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut r: libc::c_int = 0; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_field; + if (*field).fld_type != MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int { + r = mailimf_field_write_driver(do_write, data, col, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} diff --git a/mmime/src/mailmime.rs b/mmime/src/mailmime.rs new file mode 100644 index 000000000..92e319f96 --- /dev/null +++ b/mmime/src/mailmime.rs @@ -0,0 +1,1137 @@ +use libc; + +use libc::toupper; + +use crate::clist::*; +use crate::mailimf::*; +use crate::mailimf_types::*; +use crate::mailmime_decode::*; +use crate::mailmime_disposition::*; +use crate::mailmime_types::*; +use crate::other::*; + +pub const MAILMIME_COMPOSITE_TYPE_EXTENSION: libc::c_uint = 3; +pub const MAILMIME_COMPOSITE_TYPE_MULTIPART: libc::c_uint = 2; +pub const MAILMIME_COMPOSITE_TYPE_MESSAGE: libc::c_uint = 1; +pub const MAILMIME_COMPOSITE_TYPE_ERROR: libc::c_uint = 0; + +pub const MAILMIME_TYPE_COMPOSITE_TYPE: libc::c_uint = 2; +pub const MAILMIME_TYPE_DISCRETE_TYPE: libc::c_uint = 1; +pub const MAILMIME_TYPE_ERROR: libc::c_uint = 0; +pub const FIELD_STATE_L: libc::c_uint = 3; +pub const FIELD_STATE_D: libc::c_uint = 2; +pub const FIELD_STATE_T: libc::c_uint = 1; +pub const FIELD_STATE_START: libc::c_uint = 0; + +pub const MAILMIME_DISCRETE_TYPE_EXTENSION: libc::c_uint = 6; +pub const MAILMIME_DISCRETE_TYPE_APPLICATION: libc::c_uint = 5; +pub const MAILMIME_DISCRETE_TYPE_VIDEO: libc::c_uint = 4; +pub const MAILMIME_DISCRETE_TYPE_AUDIO: libc::c_uint = 3; +pub const MAILMIME_DISCRETE_TYPE_IMAGE: libc::c_uint = 2; +pub const MAILMIME_DISCRETE_TYPE_TEXT: libc::c_uint = 1; +pub const MAILMIME_DISCRETE_TYPE_ERROR: libc::c_uint = 0; + +pub unsafe fn mailmime_content_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_content, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut type_0: *mut mailmime_type = 0 as *mut mailmime_type; + let mut subtype: *mut libc::c_char = 0 as *mut libc::c_char; + let mut parameters_list: *mut clist = 0 as *mut clist; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + mailimf_cfws_parse(message, length, &mut cur_token); + type_0 = 0 as *mut mailmime_type; + r = mailmime_type_parse(message, length, &mut cur_token, &mut type_0); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_char_parse( + message, + length, + &mut cur_token, + '/' as i32 as libc::c_char, + ); + match r { + 0 => { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r; + current_block = 10242373397628622958; + } else { + r = mailmime_subtype_parse(message, length, &mut cur_token, &mut subtype); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 10242373397628622958; + } else { + current_block = 1109700713171191020; + } + } + } + 1 => { + subtype = strdup(b"unknown\x00" as *const u8 as *const libc::c_char); + current_block = 1109700713171191020; + } + _ => { + res = r; + current_block = 10242373397628622958; + } + } + match current_block { + 1109700713171191020 => { + parameters_list = clist_new(); + if parameters_list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + let mut final_token: size_t = 0; + let mut parameter: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + final_token = cur_token; + r = mailimf_unstrict_char_parse( + message, + length, + &mut cur_token, + ';' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + cur_token = final_token; + current_block = 12497913735442871383; + break; + } else { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 6276274620003476740; + break; + } else { + r = mailmime_parameter_parse( + message, + length, + &mut cur_token, + &mut parameter, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after( + parameters_list, + (*parameters_list).last, + parameter as *mut libc::c_void, + ); + if !(r < 0i32) { + continue; + } + mailmime_parameter_free(parameter); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 5731074241326334034; + break; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + cur_token = final_token; + current_block = 12497913735442871383; + break; + } else { + res = r; + current_block = 6276274620003476740; + break; + } + } + } + } + match current_block { + 6276274620003476740 => {} + _ => { + match current_block { + 12497913735442871383 => { + content = + mailmime_content_new(type_0, subtype, parameters_list); + if content.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = content; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + clist_foreach( + parameters_list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailmime_parameter_free)), + 0 as *mut libc::c_void, + ); + clist_free(parameters_list); + } + } + } + mailmime_subtype_free(subtype); + } + _ => {} + } + mailmime_type_free(type_0); + } + return res; +} + +pub unsafe fn mailmime_parameter_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_parameter, +) -> libc::c_int { + let mut attribute: *mut libc::c_char = 0 as *mut libc::c_char; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut parameter: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailmime_attribute_parse(message, length, &mut cur_token, &mut attribute); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_unstrict_char_parse( + message, + length, + &mut cur_token, + '=' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + r = mailmime_value_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + parameter = mailmime_parameter_new(attribute, value); + if parameter.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mailmime_value_free(value); + } else { + *result = parameter; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + mailmime_attribute_free(attribute); + } + return res; +} + +pub unsafe fn mailmime_value_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_atom_parse(message, length, indx, result); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_quoted_string_parse(message, length, indx, result) + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * $Id: mailmime.c,v 1.29 2011/01/06 00:09:52 hoa Exp $ + */ +/* + RFC 2045 + RFC 2046 + RFC 2047 + RFC 2048 + RFC 2049 + RFC 2231 + RFC 2387 + RFC 2424 + RFC 2557 + + RFC 2183 Content-Disposition + + RFC 1766 Language +*/ +unsafe fn mailmime_attribute_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailmime_token_parse(message, length, indx, result); +} +unsafe fn mailmime_token_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut token: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_custom_string_parse(message, length, indx, token, Some(is_token)); +} +unsafe fn is_token(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if uch as libc::c_int > 0x7fi32 { + return 0i32; + } + if uch as libc::c_int == ' ' as i32 { + return 0i32; + } + if 0 != is_tspecials(ch) { + return 0i32; + } + return 1i32; +} +unsafe fn is_tspecials(mut ch: libc::c_char) -> libc::c_int { + match ch as libc::c_int { + 40 | 41 | 60 | 62 | 64 | 44 | 59 | 58 | 92 | 34 | 47 | 91 | 93 | 63 | 61 => return 1i32, + _ => return 0i32, + }; +} +unsafe fn mailmime_subtype_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailmime_extension_token_parse(message, length, indx, result); +} + +pub unsafe fn mailmime_extension_token_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailmime_token_parse(message, length, indx, result); +} +unsafe fn mailmime_type_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_type, +) -> libc::c_int { + let mut discrete_type: *mut mailmime_discrete_type = 0 as *mut mailmime_discrete_type; + let mut composite_type: *mut mailmime_composite_type = 0 as *mut mailmime_composite_type; + let mut cur_token: size_t = 0; + let mut mime_type: *mut mailmime_type = 0 as *mut mailmime_type; + let mut type_0: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + discrete_type = 0 as *mut mailmime_discrete_type; + composite_type = 0 as *mut mailmime_composite_type; + type_0 = MAILMIME_TYPE_ERROR as libc::c_int; + r = mailmime_composite_type_parse(message, length, &mut cur_token, &mut composite_type); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_TYPE_COMPOSITE_TYPE as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_discrete_type_parse(message, length, &mut cur_token, &mut discrete_type); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_TYPE_DISCRETE_TYPE as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + mime_type = mailmime_type_new(type_0, discrete_type, composite_type); + if mime_type.is_null() { + res = r; + if !discrete_type.is_null() { + mailmime_discrete_type_free(discrete_type); + } + if !composite_type.is_null() { + mailmime_composite_type_free(composite_type); + } + } else { + *result = mime_type; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +unsafe fn mailmime_discrete_type_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_discrete_type, +) -> libc::c_int { + let mut extension: *mut libc::c_char = 0 as *mut libc::c_char; + let mut type_0: libc::c_int = 0; + let mut discrete_type: *mut mailmime_discrete_type = 0 as *mut mailmime_discrete_type; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + extension = 0 as *mut libc::c_char; + type_0 = MAILMIME_DISCRETE_TYPE_ERROR as libc::c_int; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"text\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"text\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_TEXT as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"image\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"image\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_IMAGE as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"audio\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"audio\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_AUDIO as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"video\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"video\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_VIDEO as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"application\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"application\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_APPLICATION as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_extension_token_parse(message, length, &mut cur_token, &mut extension); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISCRETE_TYPE_EXTENSION as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + discrete_type = mailmime_discrete_type_new(type_0, extension); + if discrete_type.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mailmime_extension_token_free(extension); + } else { + *result = discrete_type; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +unsafe fn mailmime_composite_type_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_composite_type, +) -> libc::c_int { + let mut extension_token: *mut libc::c_char = 0 as *mut libc::c_char; + let mut type_0: libc::c_int = 0; + let mut ct: *mut mailmime_composite_type = 0 as *mut mailmime_composite_type; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + extension_token = 0 as *mut libc::c_char; + type_0 = MAILMIME_COMPOSITE_TYPE_ERROR as libc::c_int; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"message\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"message\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_COMPOSITE_TYPE_MESSAGE as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"multipart\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"multipart\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_COMPOSITE_TYPE_MULTIPART as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + ct = mailmime_composite_type_new(type_0, extension_token); + if ct.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !extension_token.is_null() { + mailmime_extension_token_free(extension_token); + } + } else { + *result = ct; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} + +pub unsafe fn mailmime_description_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_custom_string_parse(message, length, indx, result, Some(is_text)); +} +unsafe fn is_text(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if (uch as libc::c_int) < 1i32 { + return 0i32; + } + if uch as libc::c_int == 10i32 || uch as libc::c_int == 13i32 { + return 0i32; + } + return 1i32; +} + +pub unsafe fn mailmime_location_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_custom_string_parse(message, length, indx, result, Some(is_text)); +} + +pub unsafe fn mailmime_encoding_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_mechanism, +) -> libc::c_int { + return mailmime_mechanism_parse(message, length, indx, result); +} +unsafe fn mailmime_mechanism_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_mechanism, +) -> libc::c_int { + let mut token: *mut libc::c_char = 0 as *mut libc::c_char; + let mut type_0: libc::c_int = 0; + let mut mechanism: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + type_0 = MAILMIME_MECHANISM_ERROR as libc::c_int; + token = 0 as *mut libc::c_char; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"7bit\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"7bit\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_7BIT as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"8bit\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"8bit\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_8BIT as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"binary\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"binary\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_BINARY as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"quoted-printable\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"quoted-printable\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_QUOTED_PRINTABLE as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"base64\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"base64\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_BASE64 as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_token_parse(message, length, &mut cur_token, &mut token); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_MECHANISM_TOKEN as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + mechanism = mailmime_mechanism_new(type_0, token); + if mechanism.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !token.is_null() { + mailmime_token_free(token); + } + } else { + *result = mechanism; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} + +pub unsafe fn mailmime_field_parse( + mut field: *mut mailimf_optional_field, + mut result: *mut *mut mailmime_field, +) -> libc::c_int { + let mut name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut guessed_type: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut encoding: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + let mut id: *mut libc::c_char = 0 as *mut libc::c_char; + let mut description: *mut libc::c_char = 0 as *mut libc::c_char; + let mut version: uint32_t = 0; + let mut mime_field: *mut mailmime_field = 0 as *mut mailmime_field; + let mut language: *mut mailmime_language = 0 as *mut mailmime_language; + let mut disposition: *mut mailmime_disposition = 0 as *mut mailmime_disposition; + let mut location: *mut libc::c_char = 0 as *mut libc::c_char; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + name = (*field).fld_name; + value = (*field).fld_value; + cur_token = 0i32 as size_t; + content = 0 as *mut mailmime_content; + encoding = 0 as *mut mailmime_mechanism; + id = 0 as *mut libc::c_char; + description = 0 as *mut libc::c_char; + version = 0i32 as uint32_t; + disposition = 0 as *mut mailmime_disposition; + language = 0 as *mut mailmime_language; + location = 0 as *mut libc::c_char; + guessed_type = guess_field_type(name); + match guessed_type { + 1 => { + if strcasecmp( + name, + b"Content-Type\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + let mut cur_token_0: size_t = 0i32 as size_t; + let mut decoded_value: *mut libc::c_char = 0 as *mut libc::c_char; + r = mailmime_encoded_phrase_parse( + b"us-ascii\x00" as *const u8 as *const libc::c_char, + value, + strlen(value), + &mut cur_token_0, + b"utf-8\x00" as *const u8 as *const libc::c_char, + &mut decoded_value, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + cur_token_0 = 0i32 as size_t; + r = mailmime_content_parse(value, strlen(value), &mut cur_token_0, &mut content) + } else { + cur_token_0 = 0i32 as size_t; + r = mailmime_content_parse( + decoded_value, + strlen(decoded_value), + &mut cur_token_0, + &mut content, + ); + free(decoded_value as *mut libc::c_void); + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 2 => { + if strcasecmp( + name, + b"Content-Transfer-Encoding\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_encoding_parse(value, strlen(value), &mut cur_token, &mut encoding); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 3 => { + if strcasecmp(name, b"Content-ID\x00" as *const u8 as *const libc::c_char) != 0i32 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_id_parse(value, strlen(value), &mut cur_token, &mut id); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 4 => { + if strcasecmp( + name, + b"Content-Description\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_description_parse(value, strlen(value), &mut cur_token, &mut description); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 5 => { + if strcasecmp( + name, + b"MIME-Version\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_version_parse(value, strlen(value), &mut cur_token, &mut version); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 6 => { + if strcasecmp( + name, + b"Content-Disposition\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_disposition_parse(value, strlen(value), &mut cur_token, &mut disposition); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 7 => { + if strcasecmp( + name, + b"Content-Language\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_language_parse(value, strlen(value), &mut cur_token, &mut language); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 8 => { + if strcasecmp( + name, + b"Content-Location\x00" as *const u8 as *const libc::c_char, + ) != 0i32 + { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + r = mailmime_location_parse(value, strlen(value), &mut cur_token, &mut location); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + _ => return MAILIMF_ERROR_PARSE as libc::c_int, + } + mime_field = mailmime_field_new( + guessed_type, + content, + encoding, + id, + description, + version, + disposition, + language, + location, + ); + if mime_field.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !location.is_null() { + mailmime_location_free(location); + } + if !language.is_null() { + mailmime_language_free(language); + } + if !content.is_null() { + mailmime_content_free(content); + } + if !encoding.is_null() { + mailmime_encoding_free(encoding); + } + if !id.is_null() { + mailmime_id_free(id); + } + if !description.is_null() { + mailmime_description_free(description); + } + return res; + } else { + *result = mime_field; + return MAILIMF_NO_ERROR as libc::c_int; + }; +} + +pub unsafe fn mailmime_language_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_language, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut language: *mut mailmime_language = 0 as *mut mailmime_language; + cur_token = *indx; + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + let mut atom: *mut libc::c_char = 0 as *mut libc::c_char; + r = mailimf_unstrict_char_parse( + message, + length, + &mut cur_token, + ',' as i32 as libc::c_char, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = mailimf_atom_parse(message, length, &mut cur_token, &mut atom); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after(list, (*list).last, atom as *mut libc::c_void); + if !(r < 0i32) { + continue; + } + mailimf_atom_free(atom); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14533943604180559553; + break; + } else { + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 6669252993407410313; + break; + } + res = r; + current_block = 11601180562230609130; + break; + } + } else { + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 6669252993407410313; + break; + } + res = r; + current_block = 11601180562230609130; + break; + } + } + match current_block { + 11601180562230609130 => {} + _ => { + match current_block { + 6669252993407410313 => { + language = mailmime_language_new(list); + if language.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = language; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailimf_atom_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } + } + return res; +} + +pub unsafe fn mailmime_version_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut uint32_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut hi: uint32_t = 0; + let mut low: uint32_t = 0; + let mut version: uint32_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_number_parse(message, length, &mut cur_token, &mut hi); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '.' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_number_parse(message, length, &mut cur_token, &mut low); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + version = (hi << 16i32).wrapping_add(low); + *result = version; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_id_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_msg_id_parse(message, length, indx, result); +} +unsafe fn guess_field_type(mut name: *mut libc::c_char) -> libc::c_int { + let mut state: libc::c_int = 0; + if *name as libc::c_int == 'M' as i32 { + return MAILMIME_FIELD_VERSION as libc::c_int; + } + if strncasecmp( + name, + b"Content-\x00" as *const u8 as *const libc::c_char, + 8i32 as libc::size_t, + ) != 0i32 + { + return MAILMIME_FIELD_NONE as libc::c_int; + } + name = name.offset(8isize); + state = FIELD_STATE_START as libc::c_int; + loop { + match state { + 0 => { + match toupper(*name as libc::c_uchar as libc::c_int) as libc::c_char as libc::c_int + { + 84 => state = FIELD_STATE_T as libc::c_int, + 73 => return MAILMIME_FIELD_ID as libc::c_int, + 68 => state = FIELD_STATE_D as libc::c_int, + 76 => state = FIELD_STATE_L as libc::c_int, + _ => return MAILMIME_FIELD_NONE as libc::c_int, + } + } + 1 => { + match toupper(*name as libc::c_uchar as libc::c_int) as libc::c_char as libc::c_int + { + 89 => return MAILMIME_FIELD_TYPE as libc::c_int, + 82 => return MAILMIME_FIELD_TRANSFER_ENCODING as libc::c_int, + _ => return MAILMIME_FIELD_NONE as libc::c_int, + } + } + 2 => { + match toupper(*name as libc::c_uchar as libc::c_int) as libc::c_char as libc::c_int + { + 69 => return MAILMIME_FIELD_DESCRIPTION as libc::c_int, + 73 => return MAILMIME_FIELD_DISPOSITION as libc::c_int, + _ => return MAILMIME_FIELD_NONE as libc::c_int, + } + } + 3 => { + match toupper(*name as libc::c_uchar as libc::c_int) as libc::c_char as libc::c_int + { + 65 => return MAILMIME_FIELD_LANGUAGE as libc::c_int, + 79 => return MAILMIME_FIELD_LOCATION as libc::c_int, + _ => return MAILMIME_FIELD_NONE as libc::c_int, + } + } + _ => {} + } + name = name.offset(1isize) + } +} + +pub unsafe fn mailmime_fields_parse( + mut fields: *mut mailimf_fields, + mut result: *mut *mut mailmime_fields, +) -> libc::c_int { + let mut current_block: u64; + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut list: *mut clist = 0 as *mut clist; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + cur = (*(*fields).fld_list).first; + loop { + if cur.is_null() { + current_block = 1109700713171191020; + break; + } + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut mime_field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_field; + if (*field).fld_type == MAILIMF_FIELD_OPTIONAL_FIELD as libc::c_int { + r = mailmime_field_parse((*field).fld_data.fld_optional_field, &mut mime_field); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after(list, (*list).last, mime_field as *mut libc::c_void); + if r < 0i32 { + mailmime_field_free(mime_field); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 17592539310030730040; + break; + } + } else if !(r == MAILIMF_ERROR_PARSE as libc::c_int) { + /* do nothing */ + res = r; + current_block = 17592539310030730040; + break; + } + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + match current_block { + 1109700713171191020 => { + if (*list).first.is_null() { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + mime_fields = mailmime_fields_new(list); + if mime_fields.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = mime_fields; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + clist_foreach( + list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailmime_field_free), + ), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + return res; +} diff --git a/mmime/src/mailmime_content.rs b/mmime/src/mailmime_content.rs new file mode 100644 index 000000000..d71d77a07 --- /dev/null +++ b/mmime/src/mailmime_content.rs @@ -0,0 +1,2357 @@ +use crate::clist::*; +use crate::mailimf::*; +use crate::mailimf_types::*; +use crate::mailmime::*; +use crate::mailmime_types::*; +use crate::mailmime_types_helper::*; +use crate::mmapstring::*; +use crate::other::*; + +pub const MAILMIME_DEFAULT_TYPE_TEXT_PLAIN: libc::c_uint = 0; +pub const MULTIPART_NEXT_STATE_2: libc::c_uint = 2; +pub const MULTIPART_NEXT_STATE_1: libc::c_uint = 1; +pub const MULTIPART_NEXT_STATE_0: libc::c_uint = 0; +pub const MULTIPART_CLOSE_STATE_4: libc::c_uint = 4; +pub const MULTIPART_CLOSE_STATE_3: libc::c_uint = 3; +pub const MULTIPART_CLOSE_STATE_2: libc::c_uint = 2; +pub const MULTIPART_CLOSE_STATE_1: libc::c_uint = 1; +pub const MULTIPART_CLOSE_STATE_0: libc::c_uint = 0; +pub const BODY_PART_DASH2_STATE_0: libc::c_uint = 0; +pub const BODY_PART_DASH2_STATE_6: libc::c_uint = 6; +pub const BODY_PART_DASH2_STATE_5: libc::c_uint = 5; +pub const BODY_PART_DASH2_STATE_4: libc::c_uint = 4; +pub const BODY_PART_DASH2_STATE_2: libc::c_uint = 2; +pub const BODY_PART_DASH2_STATE_1: libc::c_uint = 1; +pub const BODY_PART_DASH2_STATE_3: libc::c_uint = 3; +pub const PREAMBLE_STATE_A: libc::c_uint = 1; +pub const PREAMBLE_STATE_E: libc::c_uint = 6; +pub const PREAMBLE_STATE_D: libc::c_uint = 5; +pub const PREAMBLE_STATE_A0: libc::c_uint = 0; +pub const PREAMBLE_STATE_C: libc::c_uint = 4; +pub const PREAMBLE_STATE_B: libc::c_uint = 3; +pub const PREAMBLE_STATE_A1: libc::c_uint = 2; +pub const MAILMIME_DEFAULT_TYPE_MESSAGE: libc::c_uint = 1; +pub const STATE_NORMAL: libc::c_uint = 0; +pub const STATE_CR: libc::c_uint = 3; +pub const STATE_CODED: libc::c_uint = 1; +pub const STATE_OUT: libc::c_uint = 2; + +pub unsafe fn mailmime_content_charset_get( + mut content: *mut mailmime_content, +) -> *mut libc::c_char { + let mut charset: *mut libc::c_char = 0 as *mut libc::c_char; + charset = mailmime_content_param_get( + content, + b"charset\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + ); + if charset.is_null() { + return b"us-ascii\x00" as *const u8 as *const libc::c_char as *mut libc::c_char; + } else { + return charset; + }; +} + +pub unsafe fn mailmime_content_param_get( + mut content: *mut mailmime_content, + mut name: *mut libc::c_char, +) -> *mut libc::c_char { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*content).ct_parameters).first; + while !cur.is_null() { + let mut param: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + param = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_parameter; + if strcasecmp((*param).pa_name, name) == 0i32 { + return (*param).pa_value; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return 0 as *mut libc::c_char; +} + +pub unsafe fn mailmime_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime, +) -> libc::c_int { + let mut mime: *mut mailmime = 0 as *mut mailmime; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut content_message: *mut mailmime_content = 0 as *mut mailmime_content; + let mut cur_token: size_t = 0; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut data_str: *const libc::c_char = 0 as *const libc::c_char; + let mut data_size: size_t = 0; + let mut bp_token: size_t = 0; + cur_token = *indx; + content_message = mailmime_get_content_message(); + if content_message.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mime_fields = mailmime_fields_new_empty(); + if mime_fields.is_null() { + mailmime_content_free(content_message); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + data_str = message.offset(cur_token as isize); + data_size = length.wrapping_sub(cur_token); + bp_token = 0i32 as size_t; + r = mailmime_parse_with_default( + data_str, + data_size, + &mut bp_token, + MAILMIME_DEFAULT_TYPE_TEXT_PLAIN as libc::c_int, + content_message, + mime_fields, + &mut mime, + ); + cur_token = (cur_token as libc::size_t).wrapping_add(bp_token) as size_t as size_t; + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_fields_free(mime_fields); + res = r; + mailmime_fields_free(mime_fields); + } else { + *indx = cur_token; + *result = mime; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + return res; +} +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * $Id: mailmime_content.c,v 1.47 2011/06/28 22:13:36 hoa Exp $ + */ +/* + RFC 2045 + RFC 2046 + RFC 2047 + + RFC 2231 +*/ +unsafe fn mailmime_parse_with_default( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut default_type: libc::c_int, + mut content_type: *mut mailmime_content, + mut mime_fields: *mut mailmime_fields, + mut result: *mut *mut mailmime, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut body_type: libc::c_int = 0; + let mut encoding: libc::c_int = 0; + let mut body: *mut mailmime_data = 0 as *mut mailmime_data; + let mut boundary: *mut libc::c_char = 0 as *mut libc::c_char; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut list: *mut clist = 0 as *mut clist; + let mut msg_mime: *mut mailmime = 0 as *mut mailmime; + let mut mime: *mut mailmime = 0 as *mut mailmime; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut preamble: *mut mailmime_data = 0 as *mut mailmime_data; + let mut epilogue: *mut mailmime_data = 0 as *mut mailmime_data; + preamble = 0 as *mut mailmime_data; + epilogue = 0 as *mut mailmime_data; + cur_token = *indx; + if content_type.is_null() { + if !mime_fields.is_null() { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*mime_fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_field; + if (*field).fld_type == MAILMIME_FIELD_TYPE as libc::c_int { + content_type = (*field).fld_data.fld_content; + (*field).fld_data.fld_content = 0 as *mut mailmime_content; + clist_delete((*mime_fields).fld_list, cur); + mailmime_field_free(field); + /* + there may be a leak due to the detached content type + in case the function fails + */ + break; + } else { + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + } + } + /* set default type if no content type */ + if content_type.is_null() { + /* content_type is detached, in any case, we will have to free it */ + if default_type == MAILMIME_DEFAULT_TYPE_TEXT_PLAIN as libc::c_int { + content_type = mailmime_get_content_text(); + if content_type.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 16594950150283576116; + } else { + current_block = 7828949454673616476; + } + } else { + /* message */ + body_type = MAILMIME_MESSAGE as libc::c_int; + content_type = mailmime_get_content_message(); + if content_type.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 16594950150283576116; + } else { + current_block = 7828949454673616476; + } + } + } else { + current_block = 7828949454673616476; + } + match current_block { + 7828949454673616476 => { + boundary = 0 as *mut libc::c_char; + match (*(*content_type).ct_type).tp_type { + 2 => match (*(*(*content_type).ct_type).tp_data.tp_composite_type).ct_type { + 2 => { + boundary = mailmime_extract_boundary(content_type); + if boundary.is_null() { + body_type = MAILMIME_SINGLE as libc::c_int + } else { + body_type = MAILMIME_MULTIPLE as libc::c_int + } + current_block = 11793792312832361944; + } + 1 => { + if strcasecmp( + (*content_type).ct_subtype, + b"rfc822\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + body_type = MAILMIME_MESSAGE as libc::c_int + } else { + body_type = MAILMIME_SINGLE as libc::c_int + } + current_block = 11793792312832361944; + } + _ => { + res = MAILIMF_ERROR_INVAL as libc::c_int; + current_block = 18099180955076792539; + } + }, + _ => { + body_type = MAILMIME_SINGLE as libc::c_int; + current_block = 11793792312832361944; + } + } + match current_block { + 11793792312832361944 => { + if !mime_fields.is_null() { + encoding = mailmime_transfer_encoding_get(mime_fields) + } else { + encoding = MAILMIME_MECHANISM_8BIT as libc::c_int + } + if body_type == MAILMIME_MESSAGE as libc::c_int { + match encoding { + 4 | 5 => body_type = MAILMIME_SINGLE as libc::c_int, + _ => {} + } + } + cur_token = *indx; + body = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + encoding, + 1i32, + message.offset(cur_token as isize), + length.wrapping_sub(cur_token), + 0 as *mut libc::c_char, + ); + if body.is_null() { + free(boundary as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + list = 0 as *mut clist; + msg_mime = 0 as *mut mailmime; + fields = 0 as *mut mailimf_fields; + match body_type { + 3 => { + let mut submime_fields: *mut mailmime_fields = + 0 as *mut mailmime_fields; + r = mailimf_envelope_and_optional_fields_parse( + message, + length, + &mut cur_token, + &mut fields, + ); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 18099180955076792539; + } else { + r = mailimf_crlf_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + mailimf_fields_free(fields); + res = r; + current_block = 18099180955076792539; + } else { + submime_fields = 0 as *mut mailmime_fields; + r = mailmime_fields_parse(fields, &mut submime_fields); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + mailimf_fields_free(fields); + res = r; + current_block = 18099180955076792539; + } else { + remove_unparsed_mime_headers(fields); + r = mailmime_parse_with_default( + message, + length, + &mut cur_token, + MAILMIME_DEFAULT_TYPE_TEXT_PLAIN as libc::c_int, + 0 as *mut mailmime_content, + submime_fields, + &mut msg_mime, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + current_block = 12065775993741208975; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + mailmime_fields_free(mime_fields); + msg_mime = 0 as *mut mailmime; + current_block = 12065775993741208975; + } else { + mailmime_fields_free(mime_fields); + res = r; + current_block = 18099180955076792539; + } + } + } + } + } + 2 => { + let mut default_subtype: libc::c_int = 0; + default_subtype = MAILMIME_DEFAULT_TYPE_TEXT_PLAIN as libc::c_int; + if !content_type.is_null() { + if strcasecmp( + (*content_type).ct_subtype, + b"digest\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + default_subtype = + MAILMIME_DEFAULT_TYPE_MESSAGE as libc::c_int + } + } + cur_token = *indx; + r = mailmime_multipart_body_parse( + message, + length, + &mut cur_token, + boundary, + default_subtype, + &mut list, + &mut preamble, + &mut epilogue, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + current_block = 4804377075063615140; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 18099180955076792539; + } else { + current_block = 4804377075063615140; + } + } else { + res = r; + current_block = 18099180955076792539; + } + match current_block { + 18099180955076792539 => {} + _ => { + free(boundary as *mut libc::c_void); + current_block = 12065775993741208975; + } + } + } + _ => { + /* do nothing */ + current_block = 12065775993741208975; + } + } + match current_block { + 18099180955076792539 => {} + _ => { + mime = mailmime_new( + body_type, + message, + length, + mime_fields, + content_type, + body, + preamble, + epilogue, + list, + fields, + msg_mime, + ); + /* preamble */ + /* epilogue */ + if mime.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !epilogue.is_null() { + mailmime_data_free(epilogue); + } + if !preamble.is_null() { + mailmime_data_free(preamble); + } + if !msg_mime.is_null() { + mailmime_free(msg_mime); + } + if !list.is_null() { + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some( + mailmime_free, + )), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } else { + *result = mime; + *indx = length; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + } + _ => {} + } + mailmime_content_free(content_type); + } + _ => {} + } + return res; +} +unsafe fn mailmime_multipart_body_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut boundary: *mut libc::c_char, + mut default_subtype: libc::c_int, + mut result: *mut *mut clist, + mut p_preamble: *mut *mut mailmime_data, + mut p_epilogue: *mut *mut mailmime_data, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut list: *mut clist = 0 as *mut clist; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut preamble_begin: size_t = 0; + let mut preamble_length: size_t = 0; + let mut preamble_end: size_t = 0; + let mut epilogue_begin: size_t = 0; + let mut epilogue_length: size_t = 0; + let mut preamble: *mut mailmime_data = 0 as *mut mailmime_data; + let mut epilogue: *mut mailmime_data = 0 as *mut mailmime_data; + let mut part_begin: size_t = 0; + let mut final_part: libc::c_int = 0; + preamble = 0 as *mut mailmime_data; + epilogue = 0 as *mut mailmime_data; + cur_token = *indx; + preamble_begin = cur_token; + preamble_end = preamble_begin; + r = mailmime_preamble_parse(message, length, &mut cur_token, 1i32); + if r == MAILIMF_NO_ERROR as libc::c_int { + loop { + preamble_end = cur_token; + r = mailmime_boundary_parse(message, length, &mut cur_token, boundary); + if r == MAILIMF_NO_ERROR as libc::c_int { + current_block = 16924917904204750491; + break; + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_preamble_parse(message, length, &mut cur_token, 0i32); + if r == MAILIMF_NO_ERROR as libc::c_int { + continue; + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 16924917904204750491; + break; + } + res = r; + current_block = 13657460241182544761; + break; + } else { + /* do nothing */ + res = r; + current_block = 13657460241182544761; + break; + } + } + } else { + current_block = 16924917904204750491; + } + match current_block { + 16924917904204750491 => { + preamble_end = (preamble_end as libc::size_t).wrapping_sub(2i32 as libc::size_t) + as size_t as size_t; + if preamble_end != preamble_begin { + if *message.offset(preamble_end.wrapping_sub(1i32 as libc::size_t) as isize) + as libc::c_int + == '\n' as i32 + { + preamble_end = preamble_end.wrapping_sub(1); + if preamble_end.wrapping_sub(1i32 as libc::size_t) >= preamble_begin { + if *message.offset(preamble_end.wrapping_sub(1i32 as libc::size_t) as isize) + as libc::c_int + == '\r' as i32 + { + preamble_end = preamble_end.wrapping_sub(1) + } + } + } else if *message.offset(preamble_end.wrapping_sub(1i32 as libc::size_t) as isize) + as libc::c_int + == '\r' as i32 + { + preamble_end = preamble_end.wrapping_sub(1) + } + } + preamble_length = preamble_end.wrapping_sub(preamble_begin); + part_begin = cur_token; + loop { + r = mailmime_lwsp_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r; + current_block = 13657460241182544761; + break; + } else { + r = mailimf_crlf_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + part_begin = cur_token + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + /* do nothing */ + current_block = 9353995356876505083; + break; + } else { + res = r; + current_block = 13657460241182544761; + break; + } + } + } + match current_block { + 13657460241182544761 => {} + _ => { + cur_token = part_begin; + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + final_part = 0i32; + loop { + if !(0 == final_part) { + current_block = 15447629348493591490; + break; + } + let mut bp_token: size_t = 0; + let mut mime_bp: *mut mailmime = 0 as *mut mailmime; + let mut data_str: *const libc::c_char = 0 as *const libc::c_char; + let mut data_size: size_t = 0; + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + r = mailmime_body_part_dash2_transport_crlf_parse( + message, + length, + &mut cur_token, + boundary, + &mut data_str, + &mut data_size, + ); + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_body_part_dash2_close_parse( + message, + length, + &mut cur_token, + boundary, + &mut data_str, + &mut data_size, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + final_part = 1i32 + } + } + if r == MAILIMF_NO_ERROR as libc::c_int { + bp_token = 0i32 as size_t; + r = mailimf_optional_fields_parse( + data_str, + data_size, + &mut bp_token, + &mut fields, + ); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 6612762688763383599; + break; + } else { + r = mailimf_crlf_parse(data_str, data_size, &mut bp_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + mailimf_fields_free(fields); + res = r; + current_block = 6612762688763383599; + break; + } else { + mime_fields = 0 as *mut mailmime_fields; + r = mailmime_fields_parse(fields, &mut mime_fields); + mailimf_fields_free(fields); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 6612762688763383599; + break; + } else { + r = mailmime_parse_with_default( + data_str, + data_size, + &mut bp_token, + default_subtype, + 0 as *mut mailmime_content, + mime_fields, + &mut mime_bp, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after( + list, + (*list).last, + mime_bp as *mut libc::c_void, + ); + if r < 0i32 { + mailmime_free(mime_bp); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 6612762688763383599; + break; + } else { + r = mailmime_multipart_next_parse( + message, + length, + &mut cur_token, + ); + r == MAILIMF_NO_ERROR as libc::c_int; + } + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + mailmime_fields_free(mime_fields); + current_block = 15447629348493591490; + break; + } else { + mailmime_fields_free(mime_fields); + res = r; + current_block = 6612762688763383599; + break; + } + } + } + } + } else { + /* do nothing */ + res = r; + current_block = 6612762688763383599; + break; + } + } + match current_block { + 15447629348493591490 => { + epilogue_begin = length; + /* parse transport-padding */ + loop { + r = mailmime_lwsp_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + res = r; + current_block = 6612762688763383599; + break; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13201766686570145889; + break; + } + } + match current_block { + 6612762688763383599 => {} + _ => { + r = mailimf_crlf_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + epilogue_begin = cur_token; + current_block = 1739363794695357236; + } else if r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r; + current_block = 6612762688763383599; + } else { + current_block = 1739363794695357236; + } + match current_block { + 6612762688763383599 => {} + _ => { + epilogue_length = + length.wrapping_sub(epilogue_begin); + if preamble_length != 0i32 as libc::size_t { + preamble = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + MAILMIME_MECHANISM_8BIT as libc::c_int, + 1i32, + message.offset(preamble_begin as isize), + preamble_length, + 0 as *mut libc::c_char, + ); + if preamble.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 6612762688763383599; + } else { + current_block = 5636883459695696059; + } + } else { + current_block = 5636883459695696059; + } + match current_block { + 6612762688763383599 => {} + _ => { + if epilogue_length != 0i32 as libc::size_t { + epilogue = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + MAILMIME_MECHANISM_8BIT + as libc::c_int, + 1i32, + message.offset( + epilogue_begin as isize, + ), + epilogue_length, + 0 as *mut libc::c_char, + ); + if epilogue.is_null() { + res = MAILIMF_ERROR_MEMORY + as libc::c_int; + current_block = 6612762688763383599; + } else { + current_block = 7337917895049117968; + } + } else { + current_block = 7337917895049117968; + } + match current_block { + 6612762688763383599 => {} + _ => { + cur_token = length; + *result = list; + *p_preamble = preamble; + *p_epilogue = epilogue; + *indx = cur_token; + return MAILIMF_NO_ERROR + as libc::c_int; + } + } + } + } + } + } + } + } + } + _ => {} + } + if !epilogue.is_null() { + mailmime_data_free(epilogue); + } + if !preamble.is_null() { + mailmime_data_free(preamble); + } + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailmime_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + } + } + } + _ => {} + } + return res; +} +unsafe fn mailmime_lwsp_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + while 0 != is_wsp(*message.offset(cur_token as isize)) { + cur_token = cur_token.wrapping_add(1); + if cur_token >= length { + break; + } + } + if cur_token == *indx { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn is_wsp(mut ch: libc::c_char) -> libc::c_int { + if ch as libc::c_int == ' ' as i32 || ch as libc::c_int == '\t' as i32 { + return 1i32; + } + return 0i32; +} + +pub unsafe fn mailmime_multipart_next_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut cur_token: size_t = 0; + cur_token = *indx; + state = MULTIPART_NEXT_STATE_0 as libc::c_int; + while state != MULTIPART_NEXT_STATE_2 as libc::c_int { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match state { + 0 => match *message.offset(cur_token as isize) as libc::c_int { + 32 => state = MULTIPART_NEXT_STATE_0 as libc::c_int, + 9 => state = MULTIPART_NEXT_STATE_0 as libc::c_int, + 13 => state = MULTIPART_NEXT_STATE_1 as libc::c_int, + 10 => state = MULTIPART_NEXT_STATE_2 as libc::c_int, + _ => return MAILIMF_ERROR_PARSE as libc::c_int, + }, + 1 => match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = MULTIPART_NEXT_STATE_2 as libc::c_int, + _ => return MAILIMF_ERROR_PARSE as libc::c_int, + }, + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_body_part_dash2_close_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut boundary: *mut libc::c_char, + mut result: *mut *const libc::c_char, + mut result_size: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut data_str: *const libc::c_char = 0 as *const libc::c_char; + let mut data_size: size_t = 0; + let mut begin_text: *const libc::c_char = 0 as *const libc::c_char; + let mut end_text: *const libc::c_char = 0 as *const libc::c_char; + cur_token = *indx; + begin_text = message.offset(cur_token as isize); + end_text = message.offset(cur_token as isize); + loop { + r = mailmime_body_part_dash2_parse( + message, + length, + &mut cur_token, + boundary, + &mut data_str, + &mut data_size, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + end_text = data_str.offset(data_size as isize) + } else { + return r; + } + /* + There's no MIME multipart close bounary. + Ignore the issue and succeed. + https://github.com/MailCore/mailcore2/issues/122 + */ + if cur_token >= length { + break; + } + r = mailmime_multipart_close_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + break; + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + } + *indx = cur_token; + *result = begin_text; + *result_size = end_text.wrapping_offset_from(begin_text) as size_t; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_multipart_close_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut cur_token: size_t = 0; + cur_token = *indx; + state = MULTIPART_CLOSE_STATE_0 as libc::c_int; + while state != MULTIPART_CLOSE_STATE_4 as libc::c_int { + match state { + 0 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = MULTIPART_CLOSE_STATE_1 as libc::c_int, + _ => return MAILIMF_ERROR_PARSE as libc::c_int, + } + } + 1 => { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = MULTIPART_CLOSE_STATE_2 as libc::c_int, + _ => return MAILIMF_ERROR_PARSE as libc::c_int, + } + } + 2 => { + if cur_token >= length { + state = MULTIPART_CLOSE_STATE_4 as libc::c_int + } else { + match *message.offset(cur_token as isize) as libc::c_int { + 32 => state = MULTIPART_CLOSE_STATE_2 as libc::c_int, + 9 => state = MULTIPART_CLOSE_STATE_2 as libc::c_int, + 13 => state = MULTIPART_CLOSE_STATE_3 as libc::c_int, + 10 => state = MULTIPART_CLOSE_STATE_4 as libc::c_int, + _ => state = MULTIPART_CLOSE_STATE_4 as libc::c_int, + } + } + } + 3 => { + if cur_token >= length { + state = MULTIPART_CLOSE_STATE_4 as libc::c_int + } else { + match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = MULTIPART_CLOSE_STATE_4 as libc::c_int, + _ => state = MULTIPART_CLOSE_STATE_4 as libc::c_int, + } + } + } + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_body_part_dash2_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut boundary: *mut libc::c_char, + mut result: *mut *const libc::c_char, + mut result_size: *mut size_t, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut size: size_t = 0; + let mut begin_text: size_t = 0; + let mut end_text: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + state = BODY_PART_DASH2_STATE_0 as libc::c_int; + begin_text = cur_token; + end_text = length; + while state != BODY_PART_DASH2_STATE_5 as libc::c_int { + if cur_token >= length { + break; + } + match state { + 0 => match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = BODY_PART_DASH2_STATE_1 as libc::c_int, + 10 => state = BODY_PART_DASH2_STATE_2 as libc::c_int, + _ => state = BODY_PART_DASH2_STATE_0 as libc::c_int, + }, + 1 => match *message.offset(cur_token as isize) as libc::c_int { + 10 => state = BODY_PART_DASH2_STATE_2 as libc::c_int, + _ => state = BODY_PART_DASH2_STATE_0 as libc::c_int, + }, + 2 => match *message.offset(cur_token as isize) as libc::c_int { + 45 => { + end_text = cur_token; + state = BODY_PART_DASH2_STATE_3 as libc::c_int + } + 13 => state = BODY_PART_DASH2_STATE_1 as libc::c_int, + 10 => state = BODY_PART_DASH2_STATE_2 as libc::c_int, + _ => state = BODY_PART_DASH2_STATE_0 as libc::c_int, + }, + 3 => match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = BODY_PART_DASH2_STATE_1 as libc::c_int, + 10 => state = BODY_PART_DASH2_STATE_2 as libc::c_int, + 45 => state = BODY_PART_DASH2_STATE_4 as libc::c_int, + _ => state = BODY_PART_DASH2_STATE_0 as libc::c_int, + }, + 4 => { + r = mailmime_boundary_parse(message, length, &mut cur_token, boundary); + if r == MAILIMF_NO_ERROR as libc::c_int { + state = BODY_PART_DASH2_STATE_5 as libc::c_int + } else { + state = BODY_PART_DASH2_STATE_6 as libc::c_int + } + } + _ => {} + } + if state != BODY_PART_DASH2_STATE_5 as libc::c_int + && state != BODY_PART_DASH2_STATE_6 as libc::c_int + { + cur_token = cur_token.wrapping_add(1) + } + if state == BODY_PART_DASH2_STATE_6 as libc::c_int { + state = BODY_PART_DASH2_STATE_0 as libc::c_int + } + } + size = end_text.wrapping_sub(begin_text); + if size >= 1i32 as libc::size_t { + if *message.offset(end_text.wrapping_sub(1i32 as libc::size_t) as isize) as libc::c_int + == '\r' as i32 + { + end_text = end_text.wrapping_sub(1); + size = size.wrapping_sub(1) + } else if size >= 1i32 as libc::size_t { + if *message.offset(end_text.wrapping_sub(1i32 as libc::size_t) as isize) as libc::c_int + == '\n' as i32 + { + end_text = end_text.wrapping_sub(1); + size = size.wrapping_sub(1); + if size >= 1i32 as libc::size_t { + if *message.offset(end_text.wrapping_sub(1i32 as libc::size_t) as isize) + as libc::c_int + == '\r' as i32 + { + end_text = end_text.wrapping_sub(1); + size = size.wrapping_sub(1) + } + } + } + } + } + size = end_text.wrapping_sub(begin_text); + if size == 0i32 as libc::size_t { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + *result = message.offset(begin_text as isize); + *result_size = size; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_boundary_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut boundary: *mut libc::c_char, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut len: size_t = 0; + cur_token = *indx; + len = strlen(boundary); + if cur_token.wrapping_add(len) >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + if strncmp(message.offset(cur_token as isize), boundary, len) != 0i32 { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + cur_token = (cur_token as libc::size_t).wrapping_add(len) as size_t as size_t; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_body_part_dash2_transport_crlf_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut boundary: *mut libc::c_char, + mut result: *mut *const libc::c_char, + mut result_size: *mut size_t, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut data_str: *const libc::c_char = 0 as *const libc::c_char; + let mut data_size: size_t = 0; + let mut begin_text: *const libc::c_char = 0 as *const libc::c_char; + let mut end_text: *const libc::c_char = 0 as *const libc::c_char; + cur_token = *indx; + begin_text = message.offset(cur_token as isize); + end_text = message.offset(cur_token as isize); + loop { + r = mailmime_body_part_dash2_parse( + message, + length, + &mut cur_token, + boundary, + &mut data_str, + &mut data_size, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + end_text = data_str.offset(data_size as isize) + } else { + return r; + } + loop { + r = mailmime_lwsp_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + continue; + } + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + break; + } + return r; + } + r = mailimf_crlf_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + break; + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + } else { + return r; + } + } + *indx = cur_token; + *result = begin_text; + *result_size = end_text.wrapping_offset_from(begin_text) as size_t; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_preamble_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut beol: libc::c_int, +) -> libc::c_int { + let mut state: libc::c_int = 0; + let mut cur_token: size_t = 0; + cur_token = *indx; + if 0 != beol { + state = PREAMBLE_STATE_A0 as libc::c_int + } else { + state = PREAMBLE_STATE_A as libc::c_int + } + while state != PREAMBLE_STATE_E as libc::c_int { + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match state { + 0 => match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = PREAMBLE_STATE_A1 as libc::c_int, + 13 => state = PREAMBLE_STATE_B as libc::c_int, + 10 => state = PREAMBLE_STATE_C as libc::c_int, + _ => state = PREAMBLE_STATE_A as libc::c_int, + }, + 1 => match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = PREAMBLE_STATE_B as libc::c_int, + 10 => state = PREAMBLE_STATE_C as libc::c_int, + _ => state = PREAMBLE_STATE_A as libc::c_int, + }, + 2 => match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = PREAMBLE_STATE_E as libc::c_int, + 13 => state = PREAMBLE_STATE_B as libc::c_int, + 10 => state = PREAMBLE_STATE_C as libc::c_int, + _ => state = PREAMBLE_STATE_A as libc::c_int, + }, + 3 => match *message.offset(cur_token as isize) as libc::c_int { + 13 => state = PREAMBLE_STATE_B as libc::c_int, + 10 => state = PREAMBLE_STATE_C as libc::c_int, + 45 => state = PREAMBLE_STATE_D as libc::c_int, + _ => state = PREAMBLE_STATE_A0 as libc::c_int, + }, + 4 => match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = PREAMBLE_STATE_D as libc::c_int, + 13 => state = PREAMBLE_STATE_B as libc::c_int, + 10 => state = PREAMBLE_STATE_C as libc::c_int, + _ => state = PREAMBLE_STATE_A0 as libc::c_int, + }, + 5 => match *message.offset(cur_token as isize) as libc::c_int { + 45 => state = PREAMBLE_STATE_E as libc::c_int, + _ => state = PREAMBLE_STATE_A as libc::c_int, + }, + _ => {} + } + cur_token = cur_token.wrapping_add(1) + } + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn remove_unparsed_mime_headers(mut fields: *mut mailimf_fields) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut delete: libc::c_int = 0; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailimf_field; + match (*field).fld_type { + 22 => { + delete = 0i32; + if strncasecmp( + (*(*field).fld_data.fld_optional_field).fld_name, + b"Content-\x00" as *const u8 as *const libc::c_char, + 8i32 as libc::size_t, + ) == 0i32 + { + let mut name: *mut libc::c_char = 0 as *mut libc::c_char; + name = (*(*field).fld_data.fld_optional_field) + .fld_name + .offset(8isize); + if strcasecmp(name, b"Type\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcasecmp( + name, + b"Transfer-Encoding\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + || strcasecmp(name, b"ID\x00" as *const u8 as *const libc::c_char) == 0i32 + || strcasecmp(name, b"Description\x00" as *const u8 as *const libc::c_char) + == 0i32 + || strcasecmp(name, b"Disposition\x00" as *const u8 as *const libc::c_char) + == 0i32 + || strcasecmp(name, b"Language\x00" as *const u8 as *const libc::c_char) + == 0i32 + { + delete = 1i32 + } + } else if strcasecmp( + (*(*field).fld_data.fld_optional_field).fld_name, + b"MIME-Version\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + delete = 1i32 + } + if 0 != delete { + cur = clist_delete((*fields).fld_list, cur); + mailimf_field_free(field); + } else { + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + _ => { + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + } +} + +pub unsafe fn mailmime_extract_boundary( + mut content_type: *mut mailmime_content, +) -> *mut libc::c_char { + let mut boundary: *mut libc::c_char = 0 as *mut libc::c_char; + boundary = mailmime_content_param_get( + content_type, + b"boundary\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + ); + if !boundary.is_null() { + let mut len: size_t = 0; + let mut new_boundary: *mut libc::c_char = 0 as *mut libc::c_char; + len = strlen(boundary); + new_boundary = malloc(len.wrapping_add(1i32 as libc::size_t)) as *mut libc::c_char; + if new_boundary.is_null() { + return 0 as *mut libc::c_char; + } + if *boundary.offset(0isize) as libc::c_int == '\"' as i32 { + strncpy( + new_boundary, + boundary.offset(1isize), + len.wrapping_sub(2i32 as libc::size_t), + ); + *new_boundary.offset(len.wrapping_sub(2i32 as libc::size_t) as isize) = + 0i32 as libc::c_char + } else { + strcpy(new_boundary, boundary); + } + boundary = new_boundary + } + return boundary; +} + +pub unsafe fn mailmime_get_section( + mut mime: *mut mailmime, + mut section: *mut mailmime_section, + mut result: *mut *mut mailmime, +) -> libc::c_int { + return mailmime_get_section_list(mime, (*(*section).sec_list).first, result); +} +unsafe fn mailmime_get_section_list( + mut mime: *mut mailmime, + mut list: *mut clistiter, + mut result: *mut *mut mailmime, +) -> libc::c_int { + let mut id: uint32_t = 0; + let mut data: *mut mailmime = 0 as *mut mailmime; + let mut submime: *mut mailmime = 0 as *mut mailmime; + if list.is_null() { + *result = mime; + return MAILIMF_NO_ERROR as libc::c_int; + } + id = *((if !list.is_null() { + (*list).data + } else { + 0 as *mut libc::c_void + }) as *mut uint32_t); + data = 0 as *mut mailmime; + match (*mime).mm_type { + 1 => return MAILIMF_ERROR_INVAL as libc::c_int, + 2 => { + data = clist_nth_data( + (*mime).mm_data.mm_multipart.mm_mp_list, + id.wrapping_sub(1i32 as libc::c_uint) as libc::c_int, + ) as *mut mailmime; + if data.is_null() { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + if !if !list.is_null() { + (*list).next + } else { + 0 as *mut clistcell + } + .is_null() + { + return mailmime_get_section_list( + data, + if !list.is_null() { + (*list).next + } else { + 0 as *mut clistcell + }, + result, + ); + } else { + *result = data; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + 3 => { + submime = (*mime).mm_data.mm_message.mm_msg_mime; + match (*submime).mm_type { + 2 => { + data = clist_nth_data( + (*submime).mm_data.mm_multipart.mm_mp_list, + id.wrapping_sub(1i32 as libc::c_uint) as libc::c_int, + ) as *mut mailmime; + if data.is_null() { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + return mailmime_get_section_list( + data, + if !list.is_null() { + (*list).next + } else { + 0 as *mut clistcell + }, + result, + ); + } + _ => { + if id != 1i32 as libc::c_uint { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + data = submime; + if data.is_null() { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + return mailmime_get_section_list( + data, + if !list.is_null() { + (*list).next + } else { + 0 as *mut clistcell + }, + result, + ); + } + } + } + _ => return MAILIMF_ERROR_INVAL as libc::c_int, + }; +} +/* decode */ +pub unsafe fn mailmime_base64_body_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, +) -> libc::c_int { + return mailmime_base64_body_parse_impl(message, length, indx, result, result_len, 0i32); +} +unsafe fn mailmime_base64_body_parse_impl( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, + mut partial: libc::c_int, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut last_full_token_end: size_t = 0; + let mut chunk: [libc::c_char; 4] = [0; 4]; + let mut chunk_index: libc::c_int = 0; + let mut out: [libc::c_char; 3] = [0; 3]; + let mut mmapstr: *mut MMAPString = 0 as *mut MMAPString; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut written: size_t = 0; + chunk[0usize] = 0i32 as libc::c_char; + chunk[1usize] = 0i32 as libc::c_char; + chunk[2usize] = 0i32 as libc::c_char; + chunk[3usize] = 0i32 as libc::c_char; + cur_token = *indx; + last_full_token_end = *indx; + chunk_index = 0i32; + written = 0i32 as size_t; + mmapstr = mmap_string_sized_new( + length + .wrapping_sub(cur_token) + .wrapping_mul(3i32 as libc::size_t) + .wrapping_div(4i32 as libc::size_t), + ); + if mmapstr.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + let mut value: libc::c_schar = 0; + value = -1i32 as libc::c_schar; + while value as libc::c_int == -1i32 { + if cur_token >= length { + break; + } + value = get_base64_value(*message.offset(cur_token as isize)); + cur_token = cur_token.wrapping_add(1) + } + if value as libc::c_int == -1i32 { + current_block = 8845338526596852646; + break; + } + chunk[chunk_index as usize] = value as libc::c_char; + chunk_index += 1; + if !(chunk_index == 4i32) { + continue; + } + out[0usize] = ((chunk[0usize] as libc::c_int) << 2i32 + | chunk[1usize] as libc::c_int >> 4i32) as libc::c_char; + out[1usize] = ((chunk[1usize] as libc::c_int) << 4i32 + | chunk[2usize] as libc::c_int >> 2i32) as libc::c_char; + out[2usize] = ((chunk[2usize] as libc::c_int) << 6i32 | chunk[3usize] as libc::c_int) + as libc::c_char; + chunk[0usize] = 0i32 as libc::c_char; + chunk[1usize] = 0i32 as libc::c_char; + chunk[2usize] = 0i32 as libc::c_char; + chunk[3usize] = 0i32 as libc::c_char; + chunk_index = 0i32; + last_full_token_end = cur_token; + if mmap_string_append_len(mmapstr, out.as_mut_ptr(), 3i32 as size_t).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 11891829943175634231; + break; + } else { + written = + (written as libc::size_t).wrapping_add(3i32 as libc::size_t) as size_t as size_t + } + } + match current_block { + 8845338526596852646 => { + if chunk_index != 0i32 && 0 == partial { + let mut len: size_t = 0; + len = 0i32 as size_t; + out[0usize] = ((chunk[0usize] as libc::c_int) << 2i32 + | chunk[1usize] as libc::c_int >> 4i32) + as libc::c_char; + len = len.wrapping_add(1); + if chunk_index >= 3i32 { + out[1usize] = ((chunk[1usize] as libc::c_int) << 4i32 + | chunk[2usize] as libc::c_int >> 2i32) + as libc::c_char; + len = len.wrapping_add(1) + } + if mmap_string_append_len(mmapstr, out.as_mut_ptr(), len).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 11891829943175634231; + } else { + written = (written as libc::size_t).wrapping_add(len) as size_t as size_t; + current_block = 16738040538446813684; + } + } else { + current_block = 16738040538446813684; + } + match current_block { + 11891829943175634231 => {} + _ => { + if 0 != partial { + cur_token = last_full_token_end + } + r = mmap_string_ref(mmapstr); + if r < 0i32 { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *indx = cur_token; + *result = (*mmapstr).str_0; + *result_len = written; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + _ => {} + } + mmap_string_free(mmapstr); + } + return res; +} +/* ************************************************************************* */ +/* MIME part decoding */ +unsafe fn get_base64_value(mut ch: libc::c_char) -> libc::c_schar { + if ch as libc::c_int >= 'A' as i32 && ch as libc::c_int <= 'Z' as i32 { + return (ch as libc::c_int - 'A' as i32) as libc::c_schar; + } + if ch as libc::c_int >= 'a' as i32 && ch as libc::c_int <= 'z' as i32 { + return (ch as libc::c_int - 'a' as i32 + 26i32) as libc::c_schar; + } + if ch as libc::c_int >= '0' as i32 && ch as libc::c_int <= '9' as i32 { + return (ch as libc::c_int - '0' as i32 + 52i32) as libc::c_schar; + } + match ch as libc::c_int { + 43 => return 62i32 as libc::c_schar, + 47 => return 63i32 as libc::c_schar, + 61 => return -1i32 as libc::c_schar, + _ => return -1i32 as libc::c_schar, + }; +} + +pub unsafe fn mailmime_quoted_printable_body_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, + mut in_header: libc::c_int, +) -> libc::c_int { + return mailmime_quoted_printable_body_parse_impl( + message, length, indx, result, result_len, in_header, 0i32, + ); +} +unsafe fn mailmime_quoted_printable_body_parse_impl( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, + mut in_header: libc::c_int, + mut partial: libc::c_int, +) -> libc::c_int { + let mut current_block: u64; + let mut cur_token: size_t = 0; + let mut state: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut ch: libc::c_char = 0; + let mut count: size_t = 0; + let mut start: *const libc::c_char = 0 as *const libc::c_char; + let mut mmapstr: *mut MMAPString = 0 as *mut MMAPString; + let mut res: libc::c_int = 0; + let mut written: size_t = 0; + state = STATE_NORMAL as libc::c_int; + cur_token = *indx; + count = 0i32 as size_t; + start = message.offset(cur_token as isize); + written = 0i32 as size_t; + mmapstr = mmap_string_sized_new(length.wrapping_sub(cur_token)); + if mmapstr.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + if !(state != STATE_OUT as libc::c_int) { + current_block = 12693738997172594219; + break; + } + if cur_token >= length { + state = STATE_OUT as libc::c_int; + if 0 != partial { + cur_token = length + } + current_block = 12693738997172594219; + break; + } else { + match state { + 1 => { + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) as size_t + as size_t; + count = 0i32 as size_t + } + } + match *message.offset(cur_token as isize) as libc::c_int { + 61 => { + if cur_token.wrapping_add(1i32 as libc::size_t) >= length { + if 0 != partial { + state = STATE_OUT as libc::c_int + } else { + state = STATE_NORMAL as libc::c_int; + start = message.offset(cur_token as isize); + cur_token = cur_token.wrapping_add(1); + count = count.wrapping_add(1) + } + } else { + match *message.offset( + cur_token.wrapping_add(1i32 as libc::size_t) as isize, + ) as libc::c_int + { + 10 => { + cur_token = (cur_token as libc::size_t) + .wrapping_add(2i32 as libc::size_t) + as size_t + as size_t; + start = message.offset(cur_token as isize); + state = STATE_NORMAL as libc::c_int + } + 13 => { + if cur_token.wrapping_add(2i32 as libc::size_t) + >= length + { + state = STATE_OUT as libc::c_int + } else { + if *message.offset( + cur_token.wrapping_add(2i32 as libc::size_t) + as isize, + ) + as libc::c_int + == '\n' as i32 + { + cur_token = (cur_token as libc::size_t) + .wrapping_add(3i32 as libc::size_t) + as size_t + as size_t + } else { + cur_token = (cur_token as libc::size_t) + .wrapping_add(2i32 as libc::size_t) + as size_t + as size_t + } + start = message.offset(cur_token as isize); + state = STATE_NORMAL as libc::c_int + } + } + _ => { + if cur_token.wrapping_add(2i32 as libc::size_t) + >= length + { + if 0 != partial { + state = STATE_OUT as libc::c_int + } else { + cur_token = cur_token.wrapping_add(1); + start = message.offset(cur_token as isize); + count = count.wrapping_add(1); + state = STATE_NORMAL as libc::c_int + } + } else { + ch = to_char( + message + .offset(cur_token as isize) + .offset(1isize), + ); + if mmap_string_append_c(mmapstr, ch).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13807130624542804568; + break; + } else { + cur_token = (cur_token as libc::size_t) + .wrapping_add(3i32 as libc::size_t) + as size_t + as size_t; + written = written.wrapping_add(1); + start = message.offset(cur_token as isize); + state = STATE_NORMAL as libc::c_int + } + } + } + } + } + } + _ => {} + } + } + 0 => { + /* end of STATE_ENCODED */ + match *message.offset(cur_token as isize) as libc::c_int { + 61 => { + state = STATE_CODED as libc::c_int; + current_block = 3024367268842933116; + } + 10 => { + /* flush before writing additionnal information */ + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) + as size_t + as size_t; + count = 0i32 as size_t + } + } + r = write_decoded_qp( + mmapstr, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t) + .wrapping_add(2i32 as libc::size_t) + as size_t + as size_t; + cur_token = cur_token.wrapping_add(1); + start = message.offset(cur_token as isize) + } + current_block = 3024367268842933116; + } + 13 => { + state = STATE_CR as libc::c_int; + cur_token = cur_token.wrapping_add(1); + current_block = 3024367268842933116; + } + 95 => { + if 0 != in_header { + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) + as size_t + as size_t; + count = 0i32 as size_t + } + } + if mmap_string_append_c(mmapstr, ' ' as i32 as libc::c_char) + .is_null() + { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13807130624542804568; + break; + } else { + written = written.wrapping_add(1); + cur_token = cur_token.wrapping_add(1); + start = message.offset(cur_token as isize) + } + current_block = 3024367268842933116; + } else { + /* WARINING : must be followed by switch default action */ + current_block = 9784205294207992806; + } + } + _ => { + current_block = 9784205294207992806; + } + } + match current_block { + 9784205294207992806 => { + if count >= 512i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) + as size_t + as size_t; + count = 0i32 as size_t; + start = message.offset(cur_token as isize) + } + } + count = count.wrapping_add(1); + cur_token = cur_token.wrapping_add(1) + } + _ => {} + } + } + 3 => { + /* end of STATE_NORMAL */ + match *message.offset(cur_token as isize) as libc::c_int { + 10 => { + /* flush before writing additionnal information */ + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) + as size_t + as size_t; + count = 0i32 as size_t + } + } + r = write_decoded_qp( + mmapstr, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t) + .wrapping_add(2i32 as libc::size_t) + as size_t + as size_t; + cur_token = cur_token.wrapping_add(1); + start = message.offset(cur_token as isize); + state = STATE_NORMAL as libc::c_int + } + } + _ => { + /* flush before writing additionnal information */ + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t).wrapping_add(count) + as size_t + as size_t; + count = 0i32 as size_t + } + } + start = message.offset(cur_token as isize); + r = write_decoded_qp( + mmapstr, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + break; + } else { + written = (written as libc::size_t) + .wrapping_add(2i32 as libc::size_t) + as size_t + as size_t; + state = STATE_NORMAL as libc::c_int + } + } + } + } + _ => {} + } + } + } + /* end of STATE_CR */ + match current_block { + 12693738997172594219 => { + if count > 0i32 as libc::size_t { + r = write_decoded_qp(mmapstr, start, count); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 13807130624542804568; + } else { + written = (written as libc::size_t).wrapping_add(count) as size_t as size_t; + count = 0i32 as size_t; + current_block = 9255187738567101705; + } + } else { + current_block = 9255187738567101705; + } + match current_block { + 13807130624542804568 => {} + _ => { + r = mmap_string_ref(mmapstr); + if r < 0i32 { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *indx = cur_token; + *result = (*mmapstr).str_0; + *result_len = written; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + _ => {} + } + mmap_string_free(mmapstr); + } + return res; +} +unsafe fn write_decoded_qp( + mut mmapstr: *mut MMAPString, + mut start: *const libc::c_char, + mut count: size_t, +) -> libc::c_int { + if mmap_string_append_len(mmapstr, start, count).is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn to_char(mut hexa: *const libc::c_char) -> libc::c_char { + return (hexa_to_char(*hexa.offset(0isize)) << 4i32 | hexa_to_char(*hexa.offset(1isize))) + as libc::c_char; +} +#[inline] +unsafe fn hexa_to_char(mut hexdigit: libc::c_char) -> libc::c_int { + if hexdigit as libc::c_int >= '0' as i32 && hexdigit as libc::c_int <= '9' as i32 { + return hexdigit as libc::c_int - '0' as i32; + } + if hexdigit as libc::c_int >= 'a' as i32 && hexdigit as libc::c_int <= 'f' as i32 { + return hexdigit as libc::c_int - 'a' as i32 + 10i32; + } + if hexdigit as libc::c_int >= 'A' as i32 && hexdigit as libc::c_int <= 'F' as i32 { + return hexdigit as libc::c_int - 'A' as i32 + 10i32; + } + return 0i32; +} + +pub unsafe fn mailmime_binary_body_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, +) -> libc::c_int { + let mut mmapstr: *mut MMAPString = 0 as *mut MMAPString; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + mmapstr = mmap_string_new_len( + message.offset(cur_token as isize), + length.wrapping_sub(cur_token), + ); + if mmapstr.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + r = mmap_string_ref(mmapstr); + if r < 0i32 { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mmap_string_free(mmapstr); + } else { + *indx = length; + *result = (*mmapstr).str_0; + *result_len = length.wrapping_sub(cur_token); + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} +/* +mailmime_part_parse() + +This function gets full MIME part for parsing at once. +It is not suitable, if we want parse incomplete message in a stream mode. + +@return the return code is one of MAILIMF_ERROR_XXX or + MAILIMF_NO_ERROR codes +*/ +pub unsafe fn mailmime_part_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut encoding: libc::c_int, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, +) -> libc::c_int { + return mailmime_part_parse_impl(message, length, indx, encoding, result, result_len, 0i32); +} +unsafe fn mailmime_part_parse_impl( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut encoding: libc::c_int, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, + mut partial: libc::c_int, +) -> libc::c_int { + match encoding { + 5 => { + return mailmime_base64_body_parse_impl( + message, length, indx, result, result_len, partial, + ) + } + 4 => { + return mailmime_quoted_printable_body_parse_impl( + message, length, indx, result, result_len, 0i32, partial, + ) + } + 1 | 2 | 3 | _ => { + return mailmime_binary_body_parse(message, length, indx, result, result_len) + } + }; +} +/* +mailmime_part_parse_partial() + +This function may parse incomplete MIME part (i.e. in streaming mode). +It stops when detect incomplete encoding unit at the end of data. +Position of the first unparsed byte will be returned in (*indx) value. + +For parsing last portion of data must be used mailmime_part_parse() version. + +@param message Message for unparsed data. +@param length Length of the unparsed data. +@param INOUT indx Index of first unparsed symbol in the message. +@param encoding Encoding of the input data. +@param result Parsed MIME part content. Must be freed with mmap_string_unref(). +@param result_len Length of parsed data. + +@return the return code is one of MAILIMF_ERROR_XXX or + MAILIMF_NO_ERROR codes + +Example Usage: +@code +uint32_t received = 0; +uint32_t partLength = bodystructure[partId]->length; +for (;;) { + bool isThisRangeLast; + struct imap_range_t range = { received, 1024*1024 }; + char *result; + size_t result_len; + int error = imap_fetch_part_range(uid, partId, range, &result, &result_len); + if (error != NoError) { + // handle network error + break; + } + + if (result_len == 0) { + // requested range is empty. part is completely fetched + break; + } + + isThisRangeLast = (received + result_len >= partLength); // determine that the received data is the last, + // may be more difficult (in case of invalid metadata on the server). + + char *decoded; + size_t decoded_len; + if (isThisRangeLast) { + uint32_t index = 0; + mailmime_part_parse(result, result_len, encoding, &index, &decoded, &decoded_len); + break; + } + else { + uint32_t index = 0; + mailmime_part_parse_partial(result, result_len, encoding, &index, &decoded, &decoded_len); + // we may have some non-decoded bytes at the end of chunk. + // in this case we just request it in the next chunk + received += index; + } +} +@endcode +*/ +pub unsafe fn mailmime_part_parse_partial( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut encoding: libc::c_int, + mut result: *mut *mut libc::c_char, + mut result_len: *mut size_t, +) -> libc::c_int { + return mailmime_part_parse_impl(message, length, indx, encoding, result, result_len, 1i32); +} + +pub unsafe fn mailmime_get_section_id( + mut mime: *mut mailmime, + mut result: *mut *mut mailmime_section, +) -> libc::c_int { + let mut current_block: u64; + let mut list: *mut clist = 0 as *mut clist; + let mut res: libc::c_int = 0; + let mut section_id: *mut mailmime_section = 0 as *mut mailmime_section; + let mut r: libc::c_int = 0; + if (*mime).mm_parent.is_null() { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 11086394427076829997; + } else { + section_id = mailmime_section_new(list); + if section_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 11086394427076829997; + } else { + current_block = 9441801433784995173; + } + } + } else { + let mut id: uint32_t = 0; + let mut p_id: *mut uint32_t = 0 as *mut uint32_t; + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut parent: *mut mailmime = 0 as *mut mailmime; + r = mailmime_get_section_id((*mime).mm_parent, &mut section_id); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 11086394427076829997; + } else { + parent = (*mime).mm_parent; + match (*parent).mm_type { + 2 => { + current_block = 12048923724182970853; + match current_block { + 14310756207842895454 => { + if (*mime).mm_type == MAILMIME_SINGLE as libc::c_int + || (*mime).mm_type == MAILMIME_MESSAGE as libc::c_int + { + p_id = malloc(::std::mem::size_of::() as libc::size_t) + as *mut uint32_t; + if p_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + *p_id = 1i32 as uint32_t; + r = clist_insert_after( + (*section_id).sec_list, + (*(*section_id).sec_list).last, + p_id as *mut libc::c_void, + ); + if r < 0i32 { + free(p_id as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + current_block = 9441801433784995173; + } + } + } else { + current_block = 9441801433784995173; + } + } + _ => { + id = 1i32 as uint32_t; + cur = (*(*parent).mm_data.mm_multipart.mm_mp_list).first; + while !cur.is_null() { + if if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + } == mime as *mut libc::c_void + { + break; + } + id = id.wrapping_add(1); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + p_id = malloc(::std::mem::size_of::() as libc::size_t) + as *mut uint32_t; + if p_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + *p_id = id; + r = clist_insert_after( + (*section_id).sec_list, + (*(*section_id).sec_list).last, + p_id as *mut libc::c_void, + ); + if r < 0i32 { + free(p_id as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + current_block = 9441801433784995173; + } + } + } + } + match current_block { + 9441801433784995173 => {} + _ => { + mailmime_section_free(section_id); + current_block = 11086394427076829997; + } + } + } + 3 => { + current_block = 14310756207842895454; + match current_block { + 14310756207842895454 => { + if (*mime).mm_type == MAILMIME_SINGLE as libc::c_int + || (*mime).mm_type == MAILMIME_MESSAGE as libc::c_int + { + p_id = malloc(::std::mem::size_of::() as libc::size_t) + as *mut uint32_t; + if p_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + *p_id = 1i32 as uint32_t; + r = clist_insert_after( + (*section_id).sec_list, + (*(*section_id).sec_list).last, + p_id as *mut libc::c_void, + ); + if r < 0i32 { + free(p_id as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + current_block = 9441801433784995173; + } + } + } else { + current_block = 9441801433784995173; + } + } + _ => { + id = 1i32 as uint32_t; + cur = (*(*parent).mm_data.mm_multipart.mm_mp_list).first; + while !cur.is_null() { + if if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + } == mime as *mut libc::c_void + { + break; + } + id = id.wrapping_add(1); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + p_id = malloc(::std::mem::size_of::() as libc::size_t) + as *mut uint32_t; + if p_id.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + *p_id = id; + r = clist_insert_after( + (*section_id).sec_list, + (*(*section_id).sec_list).last, + p_id as *mut libc::c_void, + ); + if r < 0i32 { + free(p_id as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 14847554122685898769; + } else { + current_block = 9441801433784995173; + } + } + } + } + match current_block { + 9441801433784995173 => {} + _ => { + mailmime_section_free(section_id); + current_block = 11086394427076829997; + } + } + } + _ => { + current_block = 9441801433784995173; + } + } + } + } + match current_block { + 11086394427076829997 => return res, + _ => { + *result = section_id; + return MAILIMF_NO_ERROR as libc::c_int; + } + }; +} diff --git a/mmime/src/mailmime_decode.rs b/mmime/src/mailmime_decode.rs new file mode 100644 index 000000000..166da4e17 --- /dev/null +++ b/mmime/src/mailmime_decode.rs @@ -0,0 +1,863 @@ +use libc; +use libc::toupper; + +use crate::charconv::*; +use crate::mailimf::*; +use crate::mailmime_content::*; +use crate::mailmime_types::*; +use crate::mmapstring::*; +use crate::other::*; + +pub const MAIL_CHARCONV_ERROR_CONV: libc::c_uint = 3; +pub const MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: libc::c_uint = 1; +pub const MAIL_CHARCONV_ERROR_MEMORY: libc::c_uint = 2; +pub const TYPE_WORD: libc::c_uint = 1; +pub const TYPE_ENCODED_WORD: libc::c_uint = 2; +pub const MAILMIME_ENCODING_Q: libc::c_uint = 1; +pub const MAILMIME_ENCODING_B: libc::c_uint = 0; +pub const TYPE_ERROR: libc::c_uint = 0; + +pub unsafe fn mailmime_encoded_phrase_parse( + mut default_fromcode: *const libc::c_char, + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut tocode: *const libc::c_char, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut gphrase: *mut MMAPString = 0 as *mut MMAPString; + let mut word: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word; + let mut first: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut str: *mut libc::c_char = 0 as *mut libc::c_char; + let mut wordutf8: *mut libc::c_char = 0 as *mut libc::c_char; + let mut type_0: libc::c_int = 0; + let mut missing_closing_quote: libc::c_int = 0; + cur_token = *indx; + gphrase = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); + if gphrase.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + first = 1i32; + type_0 = TYPE_ERROR as libc::c_int; + loop { + let mut has_fwd: libc::c_int = 0; + word = 0 as *mut mailmime_encoded_word; + r = mailmime_encoded_word_parse( + message, + length, + &mut cur_token, + &mut word, + &mut has_fwd, + &mut missing_closing_quote, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + if 0 == first && 0 != has_fwd { + if type_0 != TYPE_ENCODED_WORD as libc::c_int { + if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() { + mailmime_encoded_word_free(word); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } + } + } + type_0 = TYPE_ENCODED_WORD as libc::c_int; + wordutf8 = 0 as *mut libc::c_char; + r = charconv( + tocode, + (*word).wd_charset, + (*word).wd_text, + strlen((*word).wd_text), + &mut wordutf8, + ); + match r { + 2 => { + mailmime_encoded_word_free(word); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } + 1 => { + r = charconv( + tocode, + b"iso-8859-1\x00" as *const u8 as *const libc::c_char, + (*word).wd_text, + strlen((*word).wd_text), + &mut wordutf8, + ) + } + 3 => { + mailmime_encoded_word_free(word); + res = MAILIMF_ERROR_PARSE as libc::c_int; + current_block = 13246848547199022064; + break; + } + _ => {} + } + match r { + 2 => { + mailmime_encoded_word_free(word); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } + 3 => { + mailmime_encoded_word_free(word); + res = MAILIMF_ERROR_PARSE as libc::c_int; + current_block = 13246848547199022064; + break; + } + _ => { + if !wordutf8.is_null() { + if mmap_string_append(gphrase, wordutf8).is_null() { + mailmime_encoded_word_free(word); + free(wordutf8 as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } else { + free(wordutf8 as *mut libc::c_void); + } + } + mailmime_encoded_word_free(word); + first = 0i32 + } + } + } else if !(r == MAILIMF_ERROR_PARSE as libc::c_int) { + /* do nothing */ + res = r; + current_block = 13246848547199022064; + break; + } + if !(r == MAILIMF_ERROR_PARSE as libc::c_int) { + continue; + } + let mut raw_word: *mut libc::c_char = 0 as *mut libc::c_char; + raw_word = 0 as *mut libc::c_char; + r = mailmime_non_encoded_word_parse( + message, + length, + &mut cur_token, + &mut raw_word, + &mut has_fwd, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + if 0 == first && 0 != has_fwd { + if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() { + free(raw_word as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } + } + type_0 = TYPE_WORD as libc::c_int; + wordutf8 = 0 as *mut libc::c_char; + r = charconv( + tocode, + default_fromcode, + raw_word, + strlen(raw_word), + &mut wordutf8, + ); + match r { + 2 => { + free(raw_word as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } + 1 | 3 => { + free(raw_word as *mut libc::c_void); + res = MAILIMF_ERROR_PARSE as libc::c_int; + current_block = 13246848547199022064; + break; + } + _ => { + if mmap_string_append(gphrase, wordutf8).is_null() { + free(wordutf8 as *mut libc::c_void); + free(raw_word as *mut libc::c_void); + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } else { + free(wordutf8 as *mut libc::c_void); + free(raw_word as *mut libc::c_void); + first = 0i32 + } + } + } + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_fws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 5005389895767293342; + break; + } + if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13246848547199022064; + break; + } else { + first = 0i32; + current_block = 5005389895767293342; + break; + } + } else { + res = r; + current_block = 13246848547199022064; + break; + } + } + match current_block { + 5005389895767293342 => { + if 0 != first { + if cur_token != length { + res = MAILIMF_ERROR_PARSE as libc::c_int; + current_block = 13246848547199022064; + } else { + current_block = 7072655752890836508; + } + } else { + current_block = 7072655752890836508; + } + match current_block { + 13246848547199022064 => {} + _ => { + str = strdup((*gphrase).str_0); + if str.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mmap_string_free(gphrase); + *result = str; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + _ => {} + } + mmap_string_free(gphrase); + } + return res; +} +unsafe fn mailmime_non_encoded_word_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, + mut p_has_fwd: *mut libc::c_int, +) -> libc::c_int { + let mut end: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut res: libc::c_int = 0; + let mut text: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut begin: size_t = 0; + let mut state: libc::c_int = 0; + let mut has_fwd: libc::c_int = 0; + cur_token = *indx; + has_fwd = 0i32; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + has_fwd = 1i32 + } + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + begin = cur_token; + state = 0i32; + end = 0i32; + while !(cur_token >= length) { + let mut current_block_17: u64; + match *message.offset(cur_token as isize) as libc::c_int { + 32 | 9 | 13 | 10 => { + state = 0i32; + end = 1i32; + current_block_17 = 16924917904204750491; + } + 61 => { + state = 1i32; + current_block_17 = 16924917904204750491; + } + 63 => { + if state == 1i32 { + cur_token = cur_token.wrapping_sub(1); + end = 1i32 + } + current_block_17 = 10192508258555769664; + } + _ => { + current_block_17 = 10192508258555769664; + } + } + match current_block_17 { + 10192508258555769664 => state = 0i32, + _ => {} + } + if 0 != end { + break; + } + cur_token = cur_token.wrapping_add(1) + } + if cur_token.wrapping_sub(begin) == 0i32 as libc::size_t { + res = MAILIMF_ERROR_PARSE as libc::c_int + } else { + text = malloc( + cur_token + .wrapping_sub(begin) + .wrapping_add(1i32 as libc::size_t), + ) as *mut libc::c_char; + if text.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + memcpy( + text as *mut libc::c_void, + message.offset(begin as isize) as *const libc::c_void, + cur_token.wrapping_sub(begin), + ); + *text.offset(cur_token.wrapping_sub(begin) as isize) = + '\u{0}' as i32 as libc::c_char; + *indx = cur_token; + *result = text; + *p_has_fwd = has_fwd; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + return res; +} + +pub unsafe fn mailmime_encoded_word_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_encoded_word, + mut p_has_fwd: *mut libc::c_int, + mut p_missing_closing_quote: *mut libc::c_int, +) -> libc::c_int { + let mut current_block: u64; + /* + Parse the following, when a unicode character encoding is split. + =?UTF-8?B?4Lij4Liw4LmA4Lia4Li04LiU4LiE4Lin4Liy4Lih4Lih4Lix4LiZ4Liq4LmM?= + =?UTF-8?B?4LmA4LiV4LmH4Lih4Lie4Li04LiB4Lix4LiUIFRSQU5TRk9STUVSUyA0IOC4?= + =?UTF-8?B?oeC4seC4meC4quC5jOC4hOC4o+C4muC4l+C4uOC4geC4o+C4sOC4muC4miDg?= + =?UTF-8?B?uJfguLXguYjguYDguJTguLXguKLguKfguYPguJnguYDguKHguLfguK3guIfg?= + =?UTF-8?B?uYTguJfguKI=?= + Expected result: + ระเบิดความมันส์เต็มพิกัด TRANSFORMERS 4 มันส์ครบทุกระบบ ที่เดียวในเมืองไทย + libetpan result: + ระเบิดความมันส์เต็มพิกัด TRANSFORMERS 4 ?ันส์ครบทุกระบบ ??ี่เดียวในเมือง??ทย + + See https://github.com/dinhviethoa/libetpan/pull/211 + */ + let mut cur_token: size_t = 0; + let mut charset: *mut libc::c_char = 0 as *mut libc::c_char; + let mut encoding: libc::c_int = 0; + let mut body: *mut libc::c_char = 0 as *mut libc::c_char; + let mut old_body_len: size_t = 0; + let mut text: *mut libc::c_char = 0 as *mut libc::c_char; + let mut end_encoding: size_t = 0; + let mut lookfwd_cur_token: size_t = 0; + let mut lookfwd_charset: *mut libc::c_char = 0 as *mut libc::c_char; + let mut lookfwd_encoding: libc::c_int = 0; + let mut copy_len: size_t = 0; + let mut decoded_token: size_t = 0; + let mut decoded: *mut libc::c_char = 0 as *mut libc::c_char; + let mut decoded_len: size_t = 0; + let mut ew: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + let mut opening_quote: libc::c_int = 0; + let mut end: libc::c_int = 0; + let mut has_fwd: libc::c_int = 0; + let mut missing_closing_quote: libc::c_int = 0; + cur_token = *indx; + text = 0 as *mut libc::c_char; + lookfwd_charset = 0 as *mut libc::c_char; + missing_closing_quote = 0i32; + has_fwd = 0i32; + r = mailimf_fws_parse(message, length, &mut cur_token); + if r == MAILIMF_NO_ERROR as libc::c_int { + has_fwd = 1i32 + } + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + opening_quote = 0i32; + r = mailimf_char_parse(message, length, &mut cur_token, '\"' as i32 as libc::c_char); + if r == MAILIMF_NO_ERROR as libc::c_int { + opening_quote = 1i32; + current_block = 17788412896529399552; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 17788412896529399552; + } else { + /* do nothing */ + res = r; + current_block = 7995813543095296079; + } + match current_block { + 7995813543095296079 => {} + _ => { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"=?\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"=?\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailmime_charset_parse(message, length, &mut cur_token, &mut charset); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_char_parse( + message, + length, + &mut cur_token, + '?' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailmime_encoding_parse( + message, + length, + &mut cur_token, + &mut encoding, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + r = mailimf_char_parse( + message, + length, + &mut cur_token, + '?' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + lookfwd_cur_token = cur_token; + body = 0 as *mut libc::c_char; + old_body_len = 0i32 as size_t; + loop { + let mut has_base64_padding: libc::c_int = 0; + end = 0i32; + has_base64_padding = 0i32; + end_encoding = cur_token; + while !(end_encoding >= length) { + if end_encoding.wrapping_add(1i32 as libc::size_t) + < length + { + if *message.offset(end_encoding as isize) + as libc::c_int + == '?' as i32 + && *message.offset( + end_encoding + .wrapping_add(1i32 as libc::size_t) + as isize, + ) + as libc::c_int + == '=' as i32 + { + end = 1i32 + } + } + if 0 != end { + break; + } + end_encoding = end_encoding.wrapping_add(1) + } + copy_len = end_encoding.wrapping_sub(lookfwd_cur_token); + if copy_len > 0i32 as libc::size_t { + if encoding == MAILMIME_ENCODING_B as libc::c_int { + if end_encoding >= 1i32 as libc::size_t { + if *message.offset( + end_encoding + .wrapping_sub(1i32 as libc::size_t) + as isize, + ) + as libc::c_int + == '=' as i32 + { + has_base64_padding = 1i32 + } + } + } + body = realloc( + body as *mut libc::c_void, + old_body_len + .wrapping_add(copy_len) + .wrapping_add(1i32 as libc::size_t), + ) + as *mut libc::c_char; + if body.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13900684162107791171; + break; + } else { + memcpy( + body.offset(old_body_len as isize) + as *mut libc::c_void, + &*message.offset(cur_token as isize) + as *const libc::c_char + as *const libc::c_void, + copy_len, + ); + *body + .offset(old_body_len.wrapping_add(copy_len) + as isize) = '\u{0}' as i32 as libc::c_char; + old_body_len = (old_body_len as libc::size_t) + .wrapping_add(copy_len) + as size_t + as size_t + } + } + cur_token = end_encoding; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"?=\x00" as *const u8 as *const libc::c_char + as *mut libc::c_char, + strlen(b"?=\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + if 0 != has_base64_padding { + current_block = 2652804691515851435; + break; + } + lookfwd_cur_token = cur_token; + r = mailimf_fws_parse( + message, + length, + &mut lookfwd_cur_token, + ); + if r != MAILIMF_NO_ERROR as libc::c_int + && r != MAILIMF_ERROR_PARSE as libc::c_int + { + current_block = 2652804691515851435; + break; + } + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut lookfwd_cur_token, + b"=?\x00" as *const u8 as *const libc::c_char + as *mut libc::c_char, + strlen(b"=?\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + r = mailmime_charset_parse( + message, + length, + &mut lookfwd_cur_token, + &mut lookfwd_charset, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + r = mailimf_char_parse( + message, + length, + &mut lookfwd_cur_token, + '?' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + r = mailmime_encoding_parse( + message, + length, + &mut lookfwd_cur_token, + &mut lookfwd_encoding, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + r = mailimf_char_parse( + message, + length, + &mut lookfwd_cur_token, + '?' as i32 as libc::c_char, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 2652804691515851435; + break; + } + if strcasecmp(charset, lookfwd_charset) == 0i32 + && encoding == lookfwd_encoding + { + cur_token = lookfwd_cur_token; + mailmime_charset_free(lookfwd_charset); + lookfwd_charset = 0 as *mut libc::c_char + } else { + /* the next charset is not matched with the current one, + therefore exit the loop to decode the body appended so far */ + current_block = 2652804691515851435; + break; + } + } + match current_block { + 2652804691515851435 => { + if !lookfwd_charset.is_null() { + mailmime_charset_free(lookfwd_charset); + lookfwd_charset = 0 as *mut libc::c_char + } + if body.is_null() { + body = strdup( + b"\x00" as *const u8 as *const libc::c_char, + ); + if body.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13900684162107791171; + } else { + current_block = 16778110326724371720; + } + } else { + current_block = 16778110326724371720; + } + match current_block { + 13900684162107791171 => {} + _ => { + decoded_token = 0i32 as size_t; + decoded_len = 0i32 as size_t; + decoded = 0 as *mut libc::c_char; + match encoding { + 0 => { + r = mailmime_base64_body_parse( + body, + strlen(body), + &mut decoded_token, + &mut decoded, + &mut decoded_len, + ); + if r != MAILIMF_NO_ERROR as libc::c_int + { + res = r; + current_block = + 13900684162107791171; + } else { + current_block = 7337917895049117968; + } + } + 1 => { + r = + mailmime_quoted_printable_body_parse(body, + strlen(body), + &mut decoded_token, + &mut decoded, + &mut decoded_len, + 1i32); + if r != MAILIMF_NO_ERROR as libc::c_int + { + res = r; + current_block = + 13900684162107791171; + } else { + current_block = 7337917895049117968; + } + } + _ => { + current_block = 7337917895049117968; + } + } + match current_block { + 13900684162107791171 => {} + _ => { + text = + malloc(decoded_len.wrapping_add( + 1i32 as libc::size_t, + )) + as *mut libc::c_char; + if text.is_null() { + res = MAILIMF_ERROR_MEMORY + as libc::c_int + } else { + if decoded_len + > 0i32 as libc::size_t + { + memcpy( + text as *mut libc::c_void, + decoded + as *const libc::c_void, + decoded_len, + ); + } + *text + .offset(decoded_len as isize) = + '\u{0}' as i32 as libc::c_char; + if 0 != opening_quote { + r = mailimf_char_parse( + message, + length, + &mut cur_token, + '\"' as i32 as libc::c_char, + ); + if r == MAILIMF_ERROR_PARSE + as libc::c_int + { + missing_closing_quote = 1i32 + } + } + if strcasecmp( + charset, + b"utf8\x00" as *const u8 + as *const libc::c_char, + ) == 0i32 + { + free( + charset + as *mut libc::c_void, + ); + charset = strdup( + b"utf-8\x00" as *const u8 + as *const libc::c_char, + ) + } + ew = mailmime_encoded_word_new( + charset, text, + ); + if ew.is_null() { + res = MAILIMF_ERROR_MEMORY + as libc::c_int + } else { + *result = ew; + *indx = cur_token; + *p_has_fwd = has_fwd; + *p_missing_closing_quote = + missing_closing_quote; + mailmime_decoded_part_free( + decoded, + ); + free(body as *mut libc::c_void); + return MAILIMF_NO_ERROR + as libc::c_int; + } + } + mailmime_decoded_part_free(decoded); + } + } + } + } + } + _ => {} + } + free(body as *mut libc::c_void); + mailmime_encoded_text_free(text); + } + } + } + mailmime_charset_free(charset); + } + } + } + } + } + return res; +} +unsafe fn mailmime_encoding_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut libc::c_int, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut encoding: libc::c_int = 0; + cur_token = *indx; + if cur_token >= length { + return MAILIMF_ERROR_PARSE as libc::c_int; + } + match toupper(*message.offset(cur_token as isize) as libc::c_uchar as libc::c_int) + as libc::c_char as libc::c_int + { + 81 => encoding = MAILMIME_ENCODING_Q as libc::c_int, + 66 => encoding = MAILMIME_ENCODING_B as libc::c_int, + _ => return MAILIMF_ERROR_INVAL as libc::c_int, + } + cur_token = cur_token.wrapping_add(1); + *result = encoding; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; +} + +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * $Id: mailmime_decode.c,v 1.37 2010/11/16 20:52:28 hoa Exp $ + */ +/* + RFC 2047 : MIME (Multipurpose Internet Mail Extensions) Part Three: + Message Header Extensions for Non-ASCII Text +*/ +unsafe fn mailmime_charset_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut charset: *mut *mut libc::c_char, +) -> libc::c_int { + return mailmime_etoken_parse(message, length, indx, charset); +} +unsafe fn mailmime_etoken_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_custom_string_parse(message, length, indx, result, Some(is_etoken_char)); +} + +pub unsafe fn is_etoken_char(mut ch: libc::c_char) -> libc::c_int { + let mut uch: libc::c_uchar = ch as libc::c_uchar; + if (uch as libc::c_int) < 31i32 { + return 0i32; + } + match uch as libc::c_int { + 32 | 40 | 41 | 60 | 62 | 64 | 44 | 59 | 58 | 34 | 47 | 91 | 93 | 63 | 61 => return 0i32, + _ => {} + } + return 1i32; +} diff --git a/mmime/src/mailmime_disposition.rs b/mmime/src/mailmime_disposition.rs new file mode 100644 index 000000000..75458bf36 --- /dev/null +++ b/mmime/src/mailmime_disposition.rs @@ -0,0 +1,584 @@ +use libc; +use libc::toupper; + +use crate::clist::*; +use crate::mailimf::*; +use crate::mailmime::*; +use crate::mailmime_types::*; +use crate::other::*; + +pub const MAILMIME_DISPOSITION_TYPE_EXTENSION: libc::c_uint = 3; +pub const MAILMIME_DISPOSITION_TYPE_ATTACHMENT: libc::c_uint = 2; +pub const MAILMIME_DISPOSITION_TYPE_INLINE: libc::c_uint = 1; +pub const MAILMIME_DISPOSITION_TYPE_ERROR: libc::c_uint = 0; + +pub unsafe fn mailmime_disposition_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_disposition, +) -> libc::c_int { + let mut current_block: u64; + let mut final_token: size_t = 0; + let mut cur_token: size_t = 0; + let mut dsp_type: *mut mailmime_disposition_type = 0 as *mut mailmime_disposition_type; + let mut list: *mut clist = 0 as *mut clist; + let mut dsp: *mut mailmime_disposition = 0 as *mut mailmime_disposition; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailmime_disposition_type_parse(message, length, &mut cur_token, &mut dsp_type); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + list = clist_new(); + if list.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + loop { + let mut param: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + final_token = cur_token; + r = mailimf_unstrict_char_parse( + message, + length, + &mut cur_token, + ';' as i32 as libc::c_char, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + param = 0 as *mut mailmime_disposition_parm; + r = mailmime_disposition_parm_parse( + message, + length, + &mut cur_token, + &mut param, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + r = clist_insert_after(list, (*list).last, param as *mut libc::c_void); + if !(r < 0i32) { + continue; + } + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 18290070879695007868; + break; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + cur_token = final_token; + current_block = 652864300344834934; + break; + } else { + res = r; + current_block = 18290070879695007868; + break; + } + } else { + /* do nothing */ + if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 652864300344834934; + break; + } + res = r; + current_block = 18290070879695007868; + break; + } + } + match current_block { + 652864300344834934 => { + dsp = mailmime_disposition_new(dsp_type, list); + if dsp.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + *result = dsp; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => {} + } + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailmime_disposition_parm_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + mailmime_disposition_type_free(dsp_type); + } + return res; +} +/* + * libEtPan! -- a mail stuff library + * + * Copyright (C) 2001, 2005 - DINH Viet Hoa + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the libEtPan! project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +/* + * $Id: mailmime_disposition.c,v 1.17 2011/05/03 16:30:22 hoa Exp $ + */ +unsafe fn mailmime_disposition_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_disposition_parm, +) -> libc::c_int { + let mut current_block: u64; + let mut filename: *mut libc::c_char = 0 as *mut libc::c_char; + let mut creation_date: *mut libc::c_char = 0 as *mut libc::c_char; + let mut modification_date: *mut libc::c_char = 0 as *mut libc::c_char; + let mut read_date: *mut libc::c_char = 0 as *mut libc::c_char; + let mut size: size_t = 0; + let mut parameter: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + let mut cur_token: size_t = 0; + let mut dsp_parm: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + let mut type_0: libc::c_int = 0; + let mut guessed_type: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + filename = 0 as *mut libc::c_char; + creation_date = 0 as *mut libc::c_char; + modification_date = 0 as *mut libc::c_char; + read_date = 0 as *mut libc::c_char; + size = 0i32 as size_t; + parameter = 0 as *mut mailmime_parameter; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + guessed_type = mailmime_disposition_guess_type(message, length, cur_token); + type_0 = MAILMIME_DISPOSITION_PARM_PARAMETER as libc::c_int; + match guessed_type { + 0 => { + r = mailmime_filename_parm_parse(message, length, &mut cur_token, &mut filename); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 13826291924415791078; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13826291924415791078; + } else { + /* do nothing */ + res = r; + current_block = 9120900589700563584; + } + } + 1 => { + r = mailmime_creation_date_parm_parse( + message, + length, + &mut cur_token, + &mut creation_date, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 13826291924415791078; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13826291924415791078; + } else { + /* do nothing */ + res = r; + current_block = 9120900589700563584; + } + } + 2 => { + r = mailmime_modification_date_parm_parse( + message, + length, + &mut cur_token, + &mut modification_date, + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 13826291924415791078; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13826291924415791078; + } else { + /* do nothing */ + res = r; + current_block = 9120900589700563584; + } + } + 3 => { + r = mailmime_read_date_parm_parse(message, length, &mut cur_token, &mut read_date); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 13826291924415791078; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13826291924415791078; + } else { + /* do nothing */ + res = r; + current_block = 9120900589700563584; + } + } + 4 => { + r = mailmime_size_parm_parse(message, length, &mut cur_token, &mut size); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + current_block = 13826291924415791078; + } else if r == MAILIMF_ERROR_PARSE as libc::c_int { + current_block = 13826291924415791078; + } else { + /* do nothing */ + res = r; + current_block = 9120900589700563584; + } + } + _ => { + current_block = 13826291924415791078; + } + } + match current_block { + 9120900589700563584 => {} + _ => { + if type_0 == MAILMIME_DISPOSITION_PARM_PARAMETER as libc::c_int { + r = mailmime_parameter_parse(message, length, &mut cur_token, &mut parameter); + if r != MAILIMF_NO_ERROR as libc::c_int { + type_0 = guessed_type; + res = r; + current_block = 9120900589700563584; + } else { + current_block = 6721012065216013753; + } + } else { + current_block = 6721012065216013753; + } + match current_block { + 9120900589700563584 => {} + _ => { + dsp_parm = mailmime_disposition_parm_new( + type_0, + filename, + creation_date, + modification_date, + read_date, + size, + parameter, + ); + if dsp_parm.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !filename.is_null() { + mailmime_filename_parm_free(filename); + } + if !creation_date.is_null() { + mailmime_creation_date_parm_free(creation_date); + } + if !modification_date.is_null() { + mailmime_modification_date_parm_free(modification_date); + } + if !read_date.is_null() { + mailmime_read_date_parm_free(read_date); + } + if !parameter.is_null() { + mailmime_parameter_free(parameter); + } + } else { + *result = dsp_parm; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + } + } + } + return res; +} +unsafe fn mailmime_size_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut size_t, +) -> libc::c_int { + let mut value: uint32_t = 0; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"size\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"size\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '=' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailimf_number_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = value as size_t; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_read_date_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"read-date\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"read-date\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '=' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailmime_quoted_date_time_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = value; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_quoted_date_time_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + return mailimf_quoted_string_parse(message, length, indx, result); +} +unsafe fn mailmime_modification_date_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut cur_token: size_t = 0; + let mut r: libc::c_int = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"modification-date\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"modification-date\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '=' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailmime_quoted_date_time_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = value; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_creation_date_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"creation-date\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"creation-date\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '=' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailmime_quoted_date_time_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = value; + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_filename_parm_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut libc::c_char, +) -> libc::c_int { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + cur_token = *indx; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"filename\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"filename\x00" as *const u8 as *const libc::c_char), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_unstrict_char_parse(message, length, &mut cur_token, '=' as i32 as libc::c_char); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + return r; + } + r = mailmime_value_parse(message, length, &mut cur_token, &mut value); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *indx = cur_token; + *result = value; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_disposition_guess_type( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: size_t, +) -> libc::c_int { + if indx >= length { + return MAILMIME_DISPOSITION_PARM_PARAMETER as libc::c_int; + } + match toupper(*message.offset(indx as isize) as libc::c_uchar as libc::c_int) as libc::c_char + as libc::c_int + { + 70 => return MAILMIME_DISPOSITION_PARM_FILENAME as libc::c_int, + 67 => return MAILMIME_DISPOSITION_PARM_CREATION_DATE as libc::c_int, + 77 => return MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE as libc::c_int, + 82 => return MAILMIME_DISPOSITION_PARM_READ_DATE as libc::c_int, + 83 => return MAILMIME_DISPOSITION_PARM_SIZE as libc::c_int, + _ => return MAILMIME_DISPOSITION_PARM_PARAMETER as libc::c_int, + }; +} + +pub unsafe fn mailmime_disposition_type_parse( + mut message: *const libc::c_char, + mut length: size_t, + mut indx: *mut size_t, + mut result: *mut *mut mailmime_disposition_type, +) -> libc::c_int { + let mut cur_token: size_t = 0; + let mut type_0: libc::c_int = 0; + let mut extension: *mut libc::c_char = 0 as *mut libc::c_char; + let mut dsp_type: *mut mailmime_disposition_type = 0 as *mut mailmime_disposition_type; + let mut r: libc::c_int = 0; + let mut res: libc::c_int = 0; + cur_token = *indx; + r = mailimf_cfws_parse(message, length, &mut cur_token); + if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int { + res = r + } else { + type_0 = MAILMIME_DISPOSITION_TYPE_ERROR as libc::c_int; + extension = 0 as *mut libc::c_char; + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"inline\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"inline\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISPOSITION_TYPE_INLINE as libc::c_int + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailimf_token_case_insensitive_len_parse( + message, + length, + &mut cur_token, + b"attachment\x00" as *const u8 as *const libc::c_char as *mut libc::c_char, + strlen(b"attachment\x00" as *const u8 as *const libc::c_char), + ); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISPOSITION_TYPE_ATTACHMENT as libc::c_int + } + } + if r == MAILIMF_ERROR_PARSE as libc::c_int { + r = mailmime_extension_token_parse(message, length, &mut cur_token, &mut extension); + if r == MAILIMF_NO_ERROR as libc::c_int { + type_0 = MAILMIME_DISPOSITION_TYPE_EXTENSION as libc::c_int + } + } + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + dsp_type = mailmime_disposition_type_new(type_0, extension); + if dsp_type.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + if !extension.is_null() { + free(extension as *mut libc::c_void); + } + } else { + *result = dsp_type; + *indx = cur_token; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + return res; +} diff --git a/mmime/src/mailmime_types.rs b/mmime/src/mailmime_types.rs new file mode 100644 index 000000000..931799136 --- /dev/null +++ b/mmime/src/mailmime_types.rs @@ -0,0 +1,871 @@ +use libc; + +use crate::clist::*; +use crate::mailimf_types::*; +use crate::mmapstring::*; +use crate::other::*; + +pub const MAILMIME_MECHANISM_TOKEN: libc::c_uint = 6; +pub const MAILMIME_MECHANISM_BASE64: libc::c_uint = 5; +pub const MAILMIME_MECHANISM_QUOTED_PRINTABLE: libc::c_uint = 4; +pub const MAILMIME_MECHANISM_BINARY: libc::c_uint = 3; +pub const MAILMIME_MECHANISM_8BIT: libc::c_uint = 2; +pub const MAILMIME_MECHANISM_7BIT: libc::c_uint = 1; +pub const MAILMIME_MECHANISM_ERROR: libc::c_uint = 0; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_composite_type { + pub ct_type: libc::c_int, + pub ct_token: *mut libc::c_char, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_content { + pub ct_type: *mut mailmime_type, + pub ct_subtype: *mut libc::c_char, + pub ct_parameters: *mut clist, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_type { + pub tp_type: libc::c_int, + pub tp_data: unnamed, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed { + pub tp_discrete_type: *mut mailmime_discrete_type, + pub tp_composite_type: *mut mailmime_composite_type, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_discrete_type { + pub dt_type: libc::c_int, + pub dt_extension: *mut libc::c_char, +} +pub type unnamed_0 = libc::c_uint; +pub const MAILMIME_FIELD_LOCATION: unnamed_0 = 8; +pub const MAILMIME_FIELD_LANGUAGE: unnamed_0 = 7; +pub const MAILMIME_FIELD_DISPOSITION: unnamed_0 = 6; +pub const MAILMIME_FIELD_VERSION: unnamed_0 = 5; +pub const MAILMIME_FIELD_DESCRIPTION: unnamed_0 = 4; +pub const MAILMIME_FIELD_ID: unnamed_0 = 3; +pub const MAILMIME_FIELD_TRANSFER_ENCODING: unnamed_0 = 2; +pub const MAILMIME_FIELD_TYPE: unnamed_0 = 1; +pub const MAILMIME_FIELD_NONE: unnamed_0 = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_field { + pub fld_type: libc::c_int, + pub fld_data: unnamed_1, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_1 { + pub fld_content: *mut mailmime_content, + pub fld_encoding: *mut mailmime_mechanism, + pub fld_id: *mut libc::c_char, + pub fld_description: *mut libc::c_char, + pub fld_version: uint32_t, + pub fld_disposition: *mut mailmime_disposition, + pub fld_language: *mut mailmime_language, + pub fld_location: *mut libc::c_char, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_language { + pub lg_list: *mut clist, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_disposition { + pub dsp_type: *mut mailmime_disposition_type, + pub dsp_parms: *mut clist, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_disposition_type { + pub dsp_type: libc::c_int, + pub dsp_extension: *mut libc::c_char, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_mechanism { + pub enc_type: libc::c_int, + pub enc_token: *mut libc::c_char, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_fields { + pub fld_list: *mut clist, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_parameter { + pub pa_name: *mut libc::c_char, + pub pa_value: *mut libc::c_char, +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_disposition_parm { + pub pa_type: libc::c_int, + pub pa_data: unnamed_3, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_3 { + pub pa_filename: *mut libc::c_char, + pub pa_creation_date: *mut libc::c_char, + pub pa_modification_date: *mut libc::c_char, + pub pa_read_date: *mut libc::c_char, + pub pa_size: size_t, + pub pa_parameter: *mut mailmime_parameter, +} +pub const MAILMIME_DISPOSITION_PARM_PARAMETER: unnamed_11 = 5; +pub const MAILMIME_DISPOSITION_PARM_READ_DATE: unnamed_11 = 3; +pub const MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE: unnamed_11 = 2; +pub const MAILMIME_DISPOSITION_PARM_CREATION_DATE: unnamed_11 = 1; +pub const MAILMIME_DISPOSITION_PARM_FILENAME: unnamed_11 = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_multipart_body { + pub bd_list: *mut clist, +} +pub type unnamed_4 = libc::c_uint; +pub const MAILMIME_DATA_FILE: unnamed_4 = 1; +pub const MAILMIME_DATA_TEXT: unnamed_4 = 0; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_data { + pub dt_type: libc::c_int, + pub dt_encoding: libc::c_int, + pub dt_encoded: libc::c_int, + pub dt_data: unnamed_5, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_5 { + pub dt_text: unnamed_6, + pub dt_filename: *mut libc::c_char, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct unnamed_6 { + pub dt_data: *const libc::c_char, + pub dt_length: size_t, +} +pub type unnamed_7 = libc::c_uint; +pub const MAILMIME_MESSAGE: unnamed_7 = 3; +pub const MAILMIME_MULTIPLE: unnamed_7 = 2; +pub const MAILMIME_SINGLE: unnamed_7 = 1; +pub const MAILMIME_NONE: unnamed_7 = 0; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime { + pub mm_parent_type: libc::c_int, + pub mm_parent: *mut mailmime, + pub mm_multipart_pos: *mut clistiter, + pub mm_type: libc::c_int, + pub mm_mime_start: *const libc::c_char, + pub mm_length: size_t, + pub mm_mime_fields: *mut mailmime_fields, + pub mm_content_type: *mut mailmime_content, + pub mm_body: *mut mailmime_data, + pub mm_data: unnamed_8, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub union unnamed_8 { + pub mm_single: *mut mailmime_data, + pub mm_multipart: unnamed_10, + pub mm_message: unnamed_9, +} +/* message */ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct unnamed_9 { + pub mm_fields: *mut mailimf_fields, + pub mm_msg_mime: *mut mailmime, +} +/* multi-part */ +#[derive(Copy, Clone)] +#[repr(C)] +pub struct unnamed_10 { + pub mm_preamble: *mut mailmime_data, + pub mm_epilogue: *mut mailmime_data, + pub mm_mp_list: *mut clist, +} +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_encoded_word { + pub wd_charset: *mut libc::c_char, + pub wd_text: *mut libc::c_char, +} +pub type unnamed_11 = libc::c_uint; +pub const MAILMIME_DISPOSITION_PARM_SIZE: unnamed_11 = 4; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_section { + pub sec_list: *mut clist, +} + +pub unsafe fn mailmime_attribute_free(mut attribute: *mut libc::c_char) { + mailmime_token_free(attribute); +} + +pub unsafe fn mailmime_token_free(mut token: *mut libc::c_char) { + free(token as *mut libc::c_void); +} +pub unsafe fn mailmime_composite_type_new( + mut ct_type: libc::c_int, + mut ct_token: *mut libc::c_char, +) -> *mut mailmime_composite_type { + let mut ct: *mut mailmime_composite_type = 0 as *mut mailmime_composite_type; + ct = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_composite_type; + if ct.is_null() { + return 0 as *mut mailmime_composite_type; + } + (*ct).ct_type = ct_type; + (*ct).ct_token = ct_token; + return ct; +} + +pub unsafe fn mailmime_composite_type_free(mut ct: *mut mailmime_composite_type) { + if !(*ct).ct_token.is_null() { + mailmime_extension_token_free((*ct).ct_token); + } + free(ct as *mut libc::c_void); +} + +pub unsafe fn mailmime_extension_token_free(mut extension: *mut libc::c_char) { + mailmime_token_free(extension); +} + +pub unsafe fn mailmime_content_new( + mut ct_type: *mut mailmime_type, + mut ct_subtype: *mut libc::c_char, + mut ct_parameters: *mut clist, +) -> *mut mailmime_content { + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + content = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_content; + if content.is_null() { + return 0 as *mut mailmime_content; + } + (*content).ct_type = ct_type; + (*content).ct_subtype = ct_subtype; + (*content).ct_parameters = ct_parameters; + return content; +} + +pub unsafe fn mailmime_content_free(mut content: *mut mailmime_content) { + mailmime_type_free((*content).ct_type); + mailmime_subtype_free((*content).ct_subtype); + if !(*content).ct_parameters.is_null() { + clist_foreach( + (*content).ct_parameters, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailmime_parameter_free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*content).ct_parameters); + } + free(content as *mut libc::c_void); +} + +pub unsafe fn mailmime_parameter_free(mut parameter: *mut mailmime_parameter) { + mailmime_attribute_free((*parameter).pa_name); + mailmime_value_free((*parameter).pa_value); + free(parameter as *mut libc::c_void); +} + +pub unsafe fn mailmime_value_free(mut value: *mut libc::c_char) { + free(value as *mut libc::c_void); +} + +pub unsafe fn mailmime_subtype_free(mut subtype: *mut libc::c_char) { + mailmime_extension_token_free(subtype); +} + +pub unsafe fn mailmime_type_free(mut type_0: *mut mailmime_type) { + match (*type_0).tp_type { + 1 => { + mailmime_discrete_type_free((*type_0).tp_data.tp_discrete_type); + } + 2 => { + mailmime_composite_type_free((*type_0).tp_data.tp_composite_type); + } + _ => {} + } + free(type_0 as *mut libc::c_void); +} + +pub unsafe fn mailmime_discrete_type_free(mut discrete_type: *mut mailmime_discrete_type) { + if !(*discrete_type).dt_extension.is_null() { + mailmime_extension_token_free((*discrete_type).dt_extension); + } + free(discrete_type as *mut libc::c_void); +} + +pub unsafe fn mailmime_description_free(mut description: *mut libc::c_char) { + free(description as *mut libc::c_void); +} + +pub unsafe fn mailmime_location_free(mut location: *mut libc::c_char) { + free(location as *mut libc::c_void); +} + +pub unsafe fn mailmime_discrete_type_new( + mut dt_type: libc::c_int, + mut dt_extension: *mut libc::c_char, +) -> *mut mailmime_discrete_type { + let mut discrete_type: *mut mailmime_discrete_type = 0 as *mut mailmime_discrete_type; + discrete_type = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_discrete_type; + if discrete_type.is_null() { + return 0 as *mut mailmime_discrete_type; + } + (*discrete_type).dt_type = dt_type; + (*discrete_type).dt_extension = dt_extension; + return discrete_type; +} + +pub unsafe fn mailmime_encoding_free(mut encoding: *mut mailmime_mechanism) { + mailmime_mechanism_free(encoding); +} + +pub unsafe fn mailmime_mechanism_free(mut mechanism: *mut mailmime_mechanism) { + if !(*mechanism).enc_token.is_null() { + mailmime_token_free((*mechanism).enc_token); + } + free(mechanism as *mut libc::c_void); +} + +pub unsafe fn mailmime_id_free(mut id: *mut libc::c_char) { + mailimf_msg_id_free(id); +} + +pub unsafe fn mailmime_mechanism_new( + mut enc_type: libc::c_int, + mut enc_token: *mut libc::c_char, +) -> *mut mailmime_mechanism { + let mut mechanism: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + mechanism = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_mechanism; + if mechanism.is_null() { + return 0 as *mut mailmime_mechanism; + } + (*mechanism).enc_type = enc_type; + (*mechanism).enc_token = enc_token; + return mechanism; +} + +pub unsafe fn mailmime_parameter_new( + mut pa_name: *mut libc::c_char, + mut pa_value: *mut libc::c_char, +) -> *mut mailmime_parameter { + let mut parameter: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + parameter = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_parameter; + if parameter.is_null() { + return 0 as *mut mailmime_parameter; + } + (*parameter).pa_name = pa_name; + (*parameter).pa_value = pa_value; + return parameter; +} + +pub unsafe fn mailmime_type_new( + mut tp_type: libc::c_int, + mut tp_discrete_type: *mut mailmime_discrete_type, + mut tp_composite_type: *mut mailmime_composite_type, +) -> *mut mailmime_type { + let mut mime_type: *mut mailmime_type = 0 as *mut mailmime_type; + mime_type = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_type; + if mime_type.is_null() { + return 0 as *mut mailmime_type; + } + (*mime_type).tp_type = tp_type; + match tp_type { + 1 => (*mime_type).tp_data.tp_discrete_type = tp_discrete_type, + 2 => (*mime_type).tp_data.tp_composite_type = tp_composite_type, + _ => {} + } + return mime_type; +} + +pub unsafe fn mailmime_language_new(mut lg_list: *mut clist) -> *mut mailmime_language { + let mut lang: *mut mailmime_language = 0 as *mut mailmime_language; + lang = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_language; + if lang.is_null() { + return 0 as *mut mailmime_language; + } + (*lang).lg_list = lg_list; + return lang; +} + +pub unsafe fn mailmime_language_free(mut lang: *mut mailmime_language) { + clist_foreach( + (*lang).lg_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailimf_atom_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*lang).lg_list); + free(lang as *mut libc::c_void); +} +/* +void mailmime_x_token_free(gchar * x_token); +*/ +pub unsafe fn mailmime_field_new( + mut fld_type: libc::c_int, + mut fld_content: *mut mailmime_content, + mut fld_encoding: *mut mailmime_mechanism, + mut fld_id: *mut libc::c_char, + mut fld_description: *mut libc::c_char, + mut fld_version: uint32_t, + mut fld_disposition: *mut mailmime_disposition, + mut fld_language: *mut mailmime_language, + mut fld_location: *mut libc::c_char, +) -> *mut mailmime_field { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_field; + if field.is_null() { + return 0 as *mut mailmime_field; + } + (*field).fld_type = fld_type; + match fld_type { + 1 => (*field).fld_data.fld_content = fld_content, + 2 => (*field).fld_data.fld_encoding = fld_encoding, + 3 => (*field).fld_data.fld_id = fld_id, + 4 => (*field).fld_data.fld_description = fld_description, + 5 => (*field).fld_data.fld_version = fld_version, + 6 => (*field).fld_data.fld_disposition = fld_disposition, + 7 => (*field).fld_data.fld_language = fld_language, + 8 => (*field).fld_data.fld_location = fld_location, + _ => {} + } + return field; +} + +pub unsafe fn mailmime_field_free(mut field: *mut mailmime_field) { + match (*field).fld_type { + 1 => { + if !(*field).fld_data.fld_content.is_null() { + mailmime_content_free((*field).fld_data.fld_content); + } + } + 2 => { + if !(*field).fld_data.fld_encoding.is_null() { + mailmime_encoding_free((*field).fld_data.fld_encoding); + } + } + 3 => { + if !(*field).fld_data.fld_id.is_null() { + mailmime_id_free((*field).fld_data.fld_id); + } + } + 4 => { + if !(*field).fld_data.fld_description.is_null() { + mailmime_description_free((*field).fld_data.fld_description); + } + } + 6 => { + if !(*field).fld_data.fld_disposition.is_null() { + mailmime_disposition_free((*field).fld_data.fld_disposition); + } + } + 7 => { + if !(*field).fld_data.fld_language.is_null() { + mailmime_language_free((*field).fld_data.fld_language); + } + } + 8 => { + if !(*field).fld_data.fld_location.is_null() { + mailmime_location_free((*field).fld_data.fld_location); + } + } + _ => {} + } + free(field as *mut libc::c_void); +} + +pub unsafe fn mailmime_disposition_free(mut dsp: *mut mailmime_disposition) { + mailmime_disposition_type_free((*dsp).dsp_type); + clist_foreach( + (*dsp).dsp_parms, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailmime_disposition_parm_free)), + 0 as *mut libc::c_void, + ); + clist_free((*dsp).dsp_parms); + free(dsp as *mut libc::c_void); +} + +pub unsafe fn mailmime_disposition_parm_free(mut dsp_parm: *mut mailmime_disposition_parm) { + match (*dsp_parm).pa_type { + 0 => { + mailmime_filename_parm_free((*dsp_parm).pa_data.pa_filename); + } + 1 => { + mailmime_creation_date_parm_free((*dsp_parm).pa_data.pa_creation_date); + } + 2 => { + mailmime_modification_date_parm_free((*dsp_parm).pa_data.pa_modification_date); + } + 3 => { + mailmime_read_date_parm_free((*dsp_parm).pa_data.pa_read_date); + } + 5 => { + mailmime_parameter_free((*dsp_parm).pa_data.pa_parameter); + } + _ => {} + } + free(dsp_parm as *mut libc::c_void); +} + +pub unsafe fn mailmime_read_date_parm_free(mut date: *mut libc::c_char) { + mailmime_quoted_date_time_free(date); +} + +pub unsafe fn mailmime_quoted_date_time_free(mut date: *mut libc::c_char) { + mailimf_quoted_string_free(date); +} + +pub unsafe fn mailmime_modification_date_parm_free(mut date: *mut libc::c_char) { + mailmime_quoted_date_time_free(date); +} + +pub unsafe fn mailmime_creation_date_parm_free(mut date: *mut libc::c_char) { + mailmime_quoted_date_time_free(date); +} + +pub unsafe fn mailmime_filename_parm_free(mut filename: *mut libc::c_char) { + mailmime_value_free(filename); +} + +pub unsafe fn mailmime_disposition_type_free(mut dsp_type: *mut mailmime_disposition_type) { + if !(*dsp_type).dsp_extension.is_null() { + free((*dsp_type).dsp_extension as *mut libc::c_void); + } + free(dsp_type as *mut libc::c_void); +} + +pub unsafe fn mailmime_fields_new(mut fld_list: *mut clist) -> *mut mailmime_fields { + let mut fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + fields = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_fields; + if fields.is_null() { + return 0 as *mut mailmime_fields; + } + (*fields).fld_list = fld_list; + return fields; +} + +pub unsafe fn mailmime_fields_free(mut fields: *mut mailmime_fields) { + clist_foreach( + (*fields).fld_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailmime_field_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*fields).fld_list); + free(fields as *mut libc::c_void); +} + +pub unsafe fn mailmime_multipart_body_new(mut bd_list: *mut clist) -> *mut mailmime_multipart_body { + let mut mp_body: *mut mailmime_multipart_body = 0 as *mut mailmime_multipart_body; + mp_body = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_multipart_body; + if mp_body.is_null() { + return 0 as *mut mailmime_multipart_body; + } + (*mp_body).bd_list = bd_list; + return mp_body; +} + +pub unsafe fn mailmime_multipart_body_free(mut mp_body: *mut mailmime_multipart_body) { + clist_foreach( + (*mp_body).bd_list, + ::std::mem::transmute:: ()>, clist_func>(Some( + mailimf_body_free, + )), + 0 as *mut libc::c_void, + ); + clist_free((*mp_body).bd_list); + free(mp_body as *mut libc::c_void); +} + +pub unsafe fn mailmime_data_new( + mut dt_type: libc::c_int, + mut dt_encoding: libc::c_int, + mut dt_encoded: libc::c_int, + mut dt_data: *const libc::c_char, + mut dt_length: size_t, + mut dt_filename: *mut libc::c_char, +) -> *mut mailmime_data { + let mut mime_data: *mut mailmime_data = 0 as *mut mailmime_data; + mime_data = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_data; + if mime_data.is_null() { + return 0 as *mut mailmime_data; + } + (*mime_data).dt_type = dt_type; + (*mime_data).dt_encoding = dt_encoding; + (*mime_data).dt_encoded = dt_encoded; + match dt_type { + 0 => { + (*mime_data).dt_data.dt_text.dt_data = dt_data; + (*mime_data).dt_data.dt_text.dt_length = dt_length + } + 1 => (*mime_data).dt_data.dt_filename = dt_filename, + _ => {} + } + return mime_data; +} + +pub unsafe fn mailmime_data_free(mut mime_data: *mut mailmime_data) { + match (*mime_data).dt_type { + 1 => { + free((*mime_data).dt_data.dt_filename as *mut libc::c_void); + } + _ => {} + } + free(mime_data as *mut libc::c_void); +} + +pub unsafe fn mailmime_new( + mut mm_type: libc::c_int, + mut mm_mime_start: *const libc::c_char, + mut mm_length: size_t, + mut mm_mime_fields: *mut mailmime_fields, + mut mm_content_type: *mut mailmime_content, + mut mm_body: *mut mailmime_data, + mut mm_preamble: *mut mailmime_data, + mut mm_epilogue: *mut mailmime_data, + mut mm_mp_list: *mut clist, + mut mm_fields: *mut mailimf_fields, + mut mm_msg_mime: *mut mailmime, +) -> *mut mailmime { + let mut mime: *mut mailmime = 0 as *mut mailmime; + let mut cur: *mut clistiter = 0 as *mut clistiter; + mime = malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime; + if mime.is_null() { + return 0 as *mut mailmime; + } + (*mime).mm_parent = 0 as *mut mailmime; + (*mime).mm_parent_type = MAILMIME_NONE as libc::c_int; + (*mime).mm_multipart_pos = 0 as *mut clistiter; + (*mime).mm_type = mm_type; + (*mime).mm_mime_start = mm_mime_start; + (*mime).mm_length = mm_length; + (*mime).mm_mime_fields = mm_mime_fields; + (*mime).mm_content_type = mm_content_type; + (*mime).mm_body = mm_body; + match mm_type { + 1 => (*mime).mm_data.mm_single = mm_body, + 2 => { + (*mime).mm_data.mm_multipart.mm_preamble = mm_preamble; + (*mime).mm_data.mm_multipart.mm_epilogue = mm_epilogue; + (*mime).mm_data.mm_multipart.mm_mp_list = mm_mp_list; + cur = (*mm_mp_list).first; + while !cur.is_null() { + let mut submime: *mut mailmime = 0 as *mut mailmime; + submime = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime; + (*submime).mm_parent = mime; + (*submime).mm_parent_type = MAILMIME_MULTIPLE as libc::c_int; + (*submime).mm_multipart_pos = cur; + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + 3 => { + (*mime).mm_data.mm_message.mm_fields = mm_fields; + (*mime).mm_data.mm_message.mm_msg_mime = mm_msg_mime; + if !mm_msg_mime.is_null() { + (*mm_msg_mime).mm_parent = mime; + (*mm_msg_mime).mm_parent_type = MAILMIME_MESSAGE as libc::c_int + } + } + _ => {} + } + return mime; +} + +pub unsafe fn mailmime_free(mut mime: *mut mailmime) { + match (*mime).mm_type { + 1 => { + if (*mime).mm_body.is_null() && !(*mime).mm_data.mm_single.is_null() { + mailmime_data_free((*mime).mm_data.mm_single); + } + } + 2 => { + /* do nothing */ + if !(*mime).mm_data.mm_multipart.mm_preamble.is_null() { + mailmime_data_free((*mime).mm_data.mm_multipart.mm_preamble); + } + if !(*mime).mm_data.mm_multipart.mm_epilogue.is_null() { + mailmime_data_free((*mime).mm_data.mm_multipart.mm_epilogue); + } + clist_foreach( + (*mime).mm_data.mm_multipart.mm_mp_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailmime_free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*mime).mm_data.mm_multipart.mm_mp_list); + } + 3 => { + if !(*mime).mm_data.mm_message.mm_fields.is_null() { + mailimf_fields_free((*mime).mm_data.mm_message.mm_fields); + } + if !(*mime).mm_data.mm_message.mm_msg_mime.is_null() { + mailmime_free((*mime).mm_data.mm_message.mm_msg_mime); + } + } + _ => {} + } + if !(*mime).mm_body.is_null() { + mailmime_data_free((*mime).mm_body); + } + if !(*mime).mm_mime_fields.is_null() { + mailmime_fields_free((*mime).mm_mime_fields); + } + if !(*mime).mm_content_type.is_null() { + mailmime_content_free((*mime).mm_content_type); + } + free(mime as *mut libc::c_void); +} + +pub unsafe fn mailmime_encoded_word_new( + mut wd_charset: *mut libc::c_char, + mut wd_text: *mut libc::c_char, +) -> *mut mailmime_encoded_word { + let mut ew: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word; + ew = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_encoded_word; + if ew.is_null() { + return 0 as *mut mailmime_encoded_word; + } + (*ew).wd_charset = wd_charset; + (*ew).wd_text = wd_text; + return ew; +} + +pub unsafe fn mailmime_encoded_word_free(mut ew: *mut mailmime_encoded_word) { + mailmime_charset_free((*ew).wd_charset); + mailmime_encoded_text_free((*ew).wd_text); + free(ew as *mut libc::c_void); +} + +pub unsafe fn mailmime_encoded_text_free(mut text: *mut libc::c_char) { + free(text as *mut libc::c_void); +} + +pub unsafe fn mailmime_charset_free(mut charset: *mut libc::c_char) { + free(charset as *mut libc::c_void); +} + +pub unsafe fn mailmime_disposition_new( + mut dsp_type: *mut mailmime_disposition_type, + mut dsp_parms: *mut clist, +) -> *mut mailmime_disposition { + let mut dsp: *mut mailmime_disposition = 0 as *mut mailmime_disposition; + dsp = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_disposition; + if dsp.is_null() { + return 0 as *mut mailmime_disposition; + } + (*dsp).dsp_type = dsp_type; + (*dsp).dsp_parms = dsp_parms; + return dsp; +} + +pub unsafe fn mailmime_disposition_type_new( + mut dsp_type: libc::c_int, + mut dsp_extension: *mut libc::c_char, +) -> *mut mailmime_disposition_type { + let mut m_dsp_type: *mut mailmime_disposition_type = 0 as *mut mailmime_disposition_type; + m_dsp_type = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_disposition_type; + if m_dsp_type.is_null() { + return 0 as *mut mailmime_disposition_type; + } + (*m_dsp_type).dsp_type = dsp_type; + (*m_dsp_type).dsp_extension = dsp_extension; + return m_dsp_type; +} + +pub unsafe fn mailmime_disposition_parm_new( + mut pa_type: libc::c_int, + mut pa_filename: *mut libc::c_char, + mut pa_creation_date: *mut libc::c_char, + mut pa_modification_date: *mut libc::c_char, + mut pa_read_date: *mut libc::c_char, + mut pa_size: size_t, + mut pa_parameter: *mut mailmime_parameter, +) -> *mut mailmime_disposition_parm { + let mut dsp_parm: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + dsp_parm = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_disposition_parm; + if dsp_parm.is_null() { + return 0 as *mut mailmime_disposition_parm; + } + (*dsp_parm).pa_type = pa_type; + match pa_type { + 0 => (*dsp_parm).pa_data.pa_filename = pa_filename, + 1 => (*dsp_parm).pa_data.pa_creation_date = pa_creation_date, + 2 => (*dsp_parm).pa_data.pa_modification_date = pa_modification_date, + 3 => (*dsp_parm).pa_data.pa_read_date = pa_read_date, + 4 => (*dsp_parm).pa_data.pa_size = pa_size, + 5 => (*dsp_parm).pa_data.pa_parameter = pa_parameter, + _ => {} + } + return dsp_parm; +} + +pub unsafe fn mailmime_section_new(mut sec_list: *mut clist) -> *mut mailmime_section { + let mut section: *mut mailmime_section = 0 as *mut mailmime_section; + section = + malloc(::std::mem::size_of::() as libc::size_t) as *mut mailmime_section; + if section.is_null() { + return 0 as *mut mailmime_section; + } + (*section).sec_list = sec_list; + return section; +} + +pub unsafe fn mailmime_section_free(mut section: *mut mailmime_section) { + clist_foreach( + (*section).sec_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(free), + ), + 0 as *mut libc::c_void, + ); + clist_free((*section).sec_list); + free(section as *mut libc::c_void); +} + +pub unsafe fn mailmime_decoded_part_free(mut part: *mut libc::c_char) { + mmap_string_unref(part); +} diff --git a/mmime/src/mailmime_types_helper.rs b/mmime/src/mailmime_types_helper.rs new file mode 100644 index 000000000..9b7f6a188 --- /dev/null +++ b/mmime/src/mailmime_types_helper.rs @@ -0,0 +1,1446 @@ +use libc; +use rand::{thread_rng, Rng}; + +use crate::clist::*; +use crate::mailimf_types::*; +use crate::mailmime::*; +use crate::mailmime_types::*; +use crate::other::*; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct mailmime_single_fields { + pub fld_content: *mut mailmime_content, + pub fld_content_charset: *mut libc::c_char, + pub fld_content_boundary: *mut libc::c_char, + pub fld_content_name: *mut libc::c_char, + pub fld_encoding: *mut mailmime_mechanism, + pub fld_id: *mut libc::c_char, + pub fld_description: *mut libc::c_char, + pub fld_version: uint32_t, + pub fld_disposition: *mut mailmime_disposition, + pub fld_disposition_filename: *mut libc::c_char, + pub fld_disposition_creation_date: *mut libc::c_char, + pub fld_disposition_modification_date: *mut libc::c_char, + pub fld_disposition_read_date: *mut libc::c_char, + pub fld_disposition_size: size_t, + pub fld_language: *mut mailmime_language, + pub fld_location: *mut libc::c_char, +} + +pub unsafe fn mailmime_transfer_encoding_get(mut fields: *mut mailmime_fields) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_field; + if (*field).fld_type == MAILMIME_FIELD_TRANSFER_ENCODING as libc::c_int { + return (*(*field).fld_data.fld_encoding).enc_type; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILMIME_MECHANISM_8BIT as libc::c_int; +} + +pub unsafe fn mailmime_disposition_new_filename( + mut type_0: libc::c_int, + mut filename: *mut libc::c_char, +) -> *mut mailmime_disposition { + return mailmime_disposition_new_with_data( + type_0, + filename, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + -1i32 as size_t, + ); +} + +pub unsafe fn mailmime_disposition_new_with_data( + mut type_0: libc::c_int, + mut filename: *mut libc::c_char, + mut creation_date: *mut libc::c_char, + mut modification_date: *mut libc::c_char, + mut read_date: *mut libc::c_char, + mut size: size_t, +) -> *mut mailmime_disposition { + let mut current_block: u64; + let mut dsp_type: *mut mailmime_disposition_type = 0 as *mut mailmime_disposition_type; + let mut list: *mut clist = 0 as *mut clist; + let mut r: libc::c_int = 0; + let mut parm: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + let mut dsp: *mut mailmime_disposition = 0 as *mut mailmime_disposition; + dsp_type = mailmime_disposition_type_new(type_0, 0 as *mut libc::c_char); + if !dsp_type.is_null() { + list = clist_new(); + if !list.is_null() { + if !filename.is_null() { + parm = mailmime_disposition_parm_new( + MAILMIME_DISPOSITION_PARM_FILENAME as libc::c_int, + filename, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as size_t, + 0 as *mut mailmime_parameter, + ); + if parm.is_null() { + current_block = 13210718484351940574; + } else { + r = clist_insert_after(list, (*list).last, parm as *mut libc::c_void); + if r < 0i32 { + mailmime_disposition_parm_free(parm); + current_block = 13210718484351940574; + } else { + current_block = 4166486009154926805; + } + } + } else { + current_block = 4166486009154926805; + } + match current_block { + 4166486009154926805 => { + if !creation_date.is_null() { + parm = mailmime_disposition_parm_new( + MAILMIME_DISPOSITION_PARM_CREATION_DATE as libc::c_int, + 0 as *mut libc::c_char, + creation_date, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as size_t, + 0 as *mut mailmime_parameter, + ); + if parm.is_null() { + current_block = 13210718484351940574; + } else { + r = clist_insert_after(list, (*list).last, parm as *mut libc::c_void); + if r < 0i32 { + mailmime_disposition_parm_free(parm); + current_block = 13210718484351940574; + } else { + current_block = 12147880666119273379; + } + } + } else { + current_block = 12147880666119273379; + } + match current_block { + 13210718484351940574 => {} + _ => { + if !modification_date.is_null() { + parm = mailmime_disposition_parm_new( + MAILMIME_DISPOSITION_PARM_MODIFICATION_DATE as libc::c_int, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + modification_date, + 0 as *mut libc::c_char, + 0i32 as size_t, + 0 as *mut mailmime_parameter, + ); + if parm.is_null() { + current_block = 13210718484351940574; + } else { + r = clist_insert_after( + list, + (*list).last, + parm as *mut libc::c_void, + ); + if r < 0i32 { + mailmime_disposition_parm_free(parm); + current_block = 13210718484351940574; + } else { + current_block = 13550086250199790493; + } + } + } else { + current_block = 13550086250199790493; + } + match current_block { + 13210718484351940574 => {} + _ => { + if !read_date.is_null() { + parm = mailmime_disposition_parm_new( + MAILMIME_DISPOSITION_PARM_READ_DATE as libc::c_int, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + read_date, + 0i32 as size_t, + 0 as *mut mailmime_parameter, + ); + if parm.is_null() { + current_block = 13210718484351940574; + } else { + r = clist_insert_after( + list, + (*list).last, + parm as *mut libc::c_void, + ); + if r < 0i32 { + mailmime_disposition_parm_free(parm); + current_block = 13210718484351940574; + } else { + current_block = 9520865839495247062; + } + } + } else { + current_block = 9520865839495247062; + } + match current_block { + 13210718484351940574 => {} + _ => { + if size != -1i32 as size_t { + parm = mailmime_disposition_parm_new( + MAILMIME_DISPOSITION_PARM_SIZE as libc::c_int, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + size, + 0 as *mut mailmime_parameter, + ); + if parm.is_null() { + current_block = 13210718484351940574; + } else { + r = clist_insert_after( + list, + (*list).last, + parm as *mut libc::c_void, + ); + if r < 0i32 { + mailmime_disposition_parm_free(parm); + current_block = 13210718484351940574; + } else { + current_block = 12199444798915819164; + } + } + } else { + current_block = 12199444798915819164; + } + match current_block { + 13210718484351940574 => {} + _ => { + dsp = mailmime_disposition_new(dsp_type, list); + return dsp; + } + } + } + } + } + } + } + } + } + _ => {} + } + clist_foreach( + list, + ::std::mem::transmute::< + Option ()>, + clist_func, + >(Some(mailmime_disposition_parm_free)), + 0 as *mut libc::c_void, + ); + clist_free(list); + } + mailmime_disposition_type_free(dsp_type); + } + return 0 as *mut mailmime_disposition; +} + +pub unsafe fn mailmime_fields_new_empty() -> *mut mailmime_fields { + let mut list: *mut clist = 0 as *mut clist; + let mut fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + list = clist_new(); + if !list.is_null() { + fields = mailmime_fields_new(list); + if fields.is_null() { + clist_free(list); + } else { + return fields; + } + } + return 0 as *mut mailmime_fields; +} + +pub unsafe fn mailmime_fields_add( + mut fields: *mut mailmime_fields, + mut field: *mut mailmime_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = clist_insert_after( + (*fields).fld_list, + (*(*fields).fld_list).last, + field as *mut libc::c_void, + ); + if r < 0i32 { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_fields_new_with_data( + mut encoding: *mut mailmime_mechanism, + mut id: *mut libc::c_char, + mut description: *mut libc::c_char, + mut disposition: *mut mailmime_disposition, + mut language: *mut mailmime_language, +) -> *mut mailmime_fields { + let mut current_block: u64; + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + let mut fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut r: libc::c_int = 0; + fields = mailmime_fields_new_empty(); + if !fields.is_null() { + if !encoding.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_TRANSFER_ENCODING as libc::c_int, + 0 as *mut mailmime_content, + encoding, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as uint32_t, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + current_block = 5039974454013832799; + } else { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + current_block = 5039974454013832799; + } else { + current_block = 7746791466490516765; + } + } + } else { + current_block = 7746791466490516765; + } + match current_block { + 7746791466490516765 => { + if !id.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_ID as libc::c_int, + 0 as *mut mailmime_content, + 0 as *mut mailmime_mechanism, + id, + 0 as *mut libc::c_char, + 0i32 as uint32_t, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + current_block = 5039974454013832799; + } else { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + current_block = 5039974454013832799; + } else { + current_block = 13242334135786603907; + } + } + } else { + current_block = 13242334135786603907; + } + match current_block { + 5039974454013832799 => {} + _ => { + if !description.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_DESCRIPTION as libc::c_int, + 0 as *mut mailmime_content, + 0 as *mut mailmime_mechanism, + 0 as *mut libc::c_char, + description, + 0i32 as uint32_t, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + current_block = 5039974454013832799; + } else { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + current_block = 5039974454013832799; + } else { + current_block = 15125582407903384992; + } + } + } else { + current_block = 15125582407903384992; + } + match current_block { + 5039974454013832799 => {} + _ => { + if !disposition.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_DISPOSITION as libc::c_int, + 0 as *mut mailmime_content, + 0 as *mut mailmime_mechanism, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as uint32_t, + disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + current_block = 5039974454013832799; + } else { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + current_block = 5039974454013832799; + } else { + current_block = 9520865839495247062; + } + } + } else { + current_block = 9520865839495247062; + } + match current_block { + 5039974454013832799 => {} + _ => { + if !language.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_DISPOSITION as libc::c_int, + 0 as *mut mailmime_content, + 0 as *mut mailmime_mechanism, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as uint32_t, + 0 as *mut mailmime_disposition, + language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + current_block = 5039974454013832799; + } else { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + current_block = 5039974454013832799; + } else { + current_block = 15512526488502093901; + } + } + } else { + current_block = 15512526488502093901; + } + match current_block { + 5039974454013832799 => {} + _ => return fields, + } + } + } + } + } + } + } + } + _ => {} + } + clist_foreach( + (*fields).fld_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailmime_field_detach), + ), + 0 as *mut libc::c_void, + ); + mailmime_fields_free(fields); + } + return 0 as *mut mailmime_fields; +} +unsafe fn mailmime_field_detach(mut field: *mut mailmime_field) { + match (*field).fld_type { + 1 => (*field).fld_data.fld_content = 0 as *mut mailmime_content, + 2 => (*field).fld_data.fld_encoding = 0 as *mut mailmime_mechanism, + 3 => (*field).fld_data.fld_id = 0 as *mut libc::c_char, + 4 => (*field).fld_data.fld_description = 0 as *mut libc::c_char, + 6 => (*field).fld_data.fld_disposition = 0 as *mut mailmime_disposition, + 7 => (*field).fld_data.fld_language = 0 as *mut mailmime_language, + _ => {} + }; +} + +pub unsafe fn mailmime_fields_new_with_version( + mut encoding: *mut mailmime_mechanism, + mut id: *mut libc::c_char, + mut description: *mut libc::c_char, + mut disposition: *mut mailmime_disposition, + mut language: *mut mailmime_language, +) -> *mut mailmime_fields { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + let mut fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut r: libc::c_int = 0; + fields = mailmime_fields_new_with_data(encoding, id, description, disposition, language); + if !fields.is_null() { + field = mailmime_field_new( + MAILMIME_FIELD_VERSION as libc::c_int, + 0 as *mut mailmime_content, + 0 as *mut mailmime_mechanism, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + (1i32 << 16i32) as uint32_t, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if !field.is_null() { + r = mailmime_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailmime_field_detach(field); + mailmime_field_free(field); + } else { + return fields; + } + } + clist_foreach( + (*fields).fld_list, + ::std::mem::transmute:: ()>, clist_func>( + Some(mailmime_field_detach), + ), + 0 as *mut libc::c_void, + ); + mailmime_fields_free(fields); + } + return 0 as *mut mailmime_fields; +} + +pub unsafe fn mailmime_get_content_message() -> *mut mailmime_content { + let mut list: *mut clist = 0 as *mut clist; + let mut composite_type: *mut mailmime_composite_type = 0 as *mut mailmime_composite_type; + let mut mime_type: *mut mailmime_type = 0 as *mut mailmime_type; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut subtype: *mut libc::c_char = 0 as *mut libc::c_char; + composite_type = mailmime_composite_type_new( + MAILMIME_COMPOSITE_TYPE_MESSAGE as libc::c_int, + 0 as *mut libc::c_char, + ); + if !composite_type.is_null() { + mime_type = mailmime_type_new( + MAILMIME_TYPE_COMPOSITE_TYPE as libc::c_int, + 0 as *mut mailmime_discrete_type, + composite_type, + ); + if !mime_type.is_null() { + composite_type = 0 as *mut mailmime_composite_type; + list = clist_new(); + if !list.is_null() { + subtype = strdup(b"rfc822\x00" as *const u8 as *const libc::c_char); + if !subtype.is_null() { + content = mailmime_content_new(mime_type, subtype, list); + if content.is_null() { + free(subtype as *mut libc::c_void); + } else { + return content; + } + } + clist_free(list); + } + mailmime_type_free(mime_type); + } + if !composite_type.is_null() { + mailmime_composite_type_free(composite_type); + } + } + return 0 as *mut mailmime_content; +} + +pub unsafe fn mailmime_get_content_text() -> *mut mailmime_content { + let mut list: *mut clist = 0 as *mut clist; + let mut discrete_type: *mut mailmime_discrete_type = 0 as *mut mailmime_discrete_type; + let mut mime_type: *mut mailmime_type = 0 as *mut mailmime_type; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut subtype: *mut libc::c_char = 0 as *mut libc::c_char; + discrete_type = mailmime_discrete_type_new( + MAILMIME_DISCRETE_TYPE_TEXT as libc::c_int, + 0 as *mut libc::c_char, + ); + if !discrete_type.is_null() { + mime_type = mailmime_type_new( + MAILMIME_TYPE_DISCRETE_TYPE as libc::c_int, + discrete_type, + 0 as *mut mailmime_composite_type, + ); + if !mime_type.is_null() { + discrete_type = 0 as *mut mailmime_discrete_type; + list = clist_new(); + if !list.is_null() { + subtype = strdup(b"plain\x00" as *const u8 as *const libc::c_char); + if !subtype.is_null() { + content = mailmime_content_new(mime_type, subtype, list); + if content.is_null() { + free(subtype as *mut libc::c_void); + } else { + return content; + } + } + clist_free(list); + } + mailmime_type_free(mime_type); + } + if !discrete_type.is_null() { + mailmime_discrete_type_free(discrete_type); + } + } + return 0 as *mut mailmime_content; +} +/* struct mailmime_content * mailmime_get_content(char * mime_type); */ +pub unsafe fn mailmime_data_new_data( + mut encoding: libc::c_int, + mut encoded: libc::c_int, + mut data: *const libc::c_char, + mut length: size_t, +) -> *mut mailmime_data { + return mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + encoding, + encoded, + data, + length, + 0 as *mut libc::c_char, + ); +} + +pub unsafe fn mailmime_data_new_file( + mut encoding: libc::c_int, + mut encoded: libc::c_int, + mut filename: *mut libc::c_char, +) -> *mut mailmime_data { + return mailmime_data_new( + MAILMIME_DATA_FILE as libc::c_int, + encoding, + encoded, + 0 as *const libc::c_char, + 0i32 as size_t, + filename, + ); +} + +pub unsafe fn mailmime_new_message_data(mut msg_mime: *mut mailmime) -> *mut mailmime { + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut build_info: *mut mailmime = 0 as *mut mailmime; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + content = mailmime_get_content_message(); + if !content.is_null() { + mime_fields = mailmime_fields_new_with_version( + 0 as *mut mailmime_mechanism, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + ); + if !mime_fields.is_null() { + build_info = mailmime_new( + MAILMIME_MESSAGE as libc::c_int, + 0 as *const libc::c_char, + 0i32 as size_t, + mime_fields, + content, + 0 as *mut mailmime_data, + 0 as *mut mailmime_data, + 0 as *mut mailmime_data, + 0 as *mut clist, + 0 as *mut mailimf_fields, + msg_mime, + ); + if build_info.is_null() { + mailmime_fields_free(mime_fields); + } else { + return build_info; + } + } + mailmime_content_free(content); + } + return 0 as *mut mailmime; +} + +pub unsafe fn mailmime_new_empty( + mut content: *mut mailmime_content, + mut mime_fields: *mut mailmime_fields, +) -> *mut mailmime { + let mut current_block: u64; + let mut build_info: *mut mailmime = 0 as *mut mailmime; + let mut list: *mut clist = 0 as *mut clist; + let mut r: libc::c_int = 0; + let mut mime_type: libc::c_int = 0; + list = 0 as *mut clist; + match (*(*content).ct_type).tp_type { + 1 => { + mime_type = MAILMIME_SINGLE as libc::c_int; + current_block = 12349973810996921269; + } + 2 => match (*(*(*content).ct_type).tp_data.tp_composite_type).ct_type { + 2 => { + current_block = 5822726848290245908; + match current_block { + 565197971715936940 => { + if strcasecmp( + (*content).ct_subtype, + b"rfc822\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + mime_type = MAILMIME_MESSAGE as libc::c_int + } else { + mime_type = MAILMIME_SINGLE as libc::c_int + } + } + _ => mime_type = MAILMIME_MULTIPLE as libc::c_int, + } + current_block = 12349973810996921269; + } + 1 => { + current_block = 565197971715936940; + match current_block { + 565197971715936940 => { + if strcasecmp( + (*content).ct_subtype, + b"rfc822\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + mime_type = MAILMIME_MESSAGE as libc::c_int + } else { + mime_type = MAILMIME_SINGLE as libc::c_int + } + } + _ => mime_type = MAILMIME_MULTIPLE as libc::c_int, + } + current_block = 12349973810996921269; + } + _ => { + current_block = 13576996419214490990; + } + }, + _ => { + current_block = 13576996419214490990; + } + } + match current_block { + 12349973810996921269 => { + if mime_type == MAILMIME_MULTIPLE as libc::c_int { + let mut attr_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut attr_value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut param: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + let mut parameters: *mut clist = 0 as *mut clist; + let mut boundary: *mut libc::c_char = 0 as *mut libc::c_char; + list = clist_new(); + if list.is_null() { + current_block = 13576996419214490990; + } else { + attr_name = strdup(b"boundary\x00" as *const u8 as *const libc::c_char); + if attr_name.is_null() { + current_block = 13142422523813476356; + } else { + boundary = mailmime_generate_boundary(); + attr_value = boundary; + if attr_name.is_null() { + free(attr_name as *mut libc::c_void); + current_block = 13142422523813476356; + } else { + param = mailmime_parameter_new(attr_name, attr_value); + if param.is_null() { + free(attr_value as *mut libc::c_void); + free(attr_name as *mut libc::c_void); + current_block = 13142422523813476356; + } else { + if (*content).ct_parameters.is_null() { + parameters = clist_new(); + if parameters.is_null() { + mailmime_parameter_free(param); + current_block = 13142422523813476356; + } else { + current_block = 1836292691772056875; + } + } else { + parameters = (*content).ct_parameters; + current_block = 1836292691772056875; + } + match current_block { + 13142422523813476356 => {} + _ => { + r = clist_insert_after( + parameters, + (*parameters).last, + param as *mut libc::c_void, + ); + if r != 0i32 { + clist_free(parameters); + mailmime_parameter_free(param); + current_block = 13142422523813476356; + } else { + if (*content).ct_parameters.is_null() { + (*content).ct_parameters = parameters + } + current_block = 2543120759711851213; + } + } + } + } + } + } + match current_block { + 2543120759711851213 => {} + _ => { + clist_free(list); + current_block = 13576996419214490990; + } + } + } + } else { + current_block = 2543120759711851213; + } + match current_block { + 13576996419214490990 => {} + _ => { + build_info = mailmime_new( + mime_type, + 0 as *const libc::c_char, + 0i32 as size_t, + mime_fields, + content, + 0 as *mut mailmime_data, + 0 as *mut mailmime_data, + 0 as *mut mailmime_data, + list, + 0 as *mut mailimf_fields, + 0 as *mut mailmime, + ); + if build_info.is_null() { + clist_free(list); + return 0 as *mut mailmime; + } + return build_info; + } + } + } + _ => {} + } + return 0 as *mut mailmime; +} + +pub unsafe fn mailmime_generate_boundary() -> *mut libc::c_char { + let mut rng = thread_rng(); + let value: libc::c_int = rng.gen(); + let now = chrono::Utc::now().timestamp(); + let raw = format!( + "{}_{}_{}", + hex::encode(&now.to_be_bytes()[4..]), + hex::encode(value.to_be_bytes()), + hex::encode(&std::process::id().to_le_bytes()[..2]) + ); + + let c = std::ffi::CString::new(raw).unwrap(); + strdup(c.as_ptr()) +} + +pub unsafe fn mailmime_new_with_content( + mut content_type: *const libc::c_char, + mut mime_fields: *mut mailmime_fields, + mut result: *mut *mut mailmime, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut build_info: *mut mailmime = 0 as *mut mailmime; + let mut res: libc::c_int = 0; + cur_token = 0i32 as size_t; + r = mailmime_content_parse( + content_type, + strlen(content_type), + &mut cur_token, + &mut content, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r + } else { + build_info = mailmime_new_empty(content, mime_fields); + if build_info.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mailmime_content_free(content); + } else { + *result = build_info; + return MAILIMF_NO_ERROR as libc::c_int; + } + } + return res; +} + +pub unsafe fn mailmime_set_preamble_file( + mut build_info: *mut mailmime, + mut filename: *mut libc::c_char, +) -> libc::c_int { + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + data = mailmime_data_new( + MAILMIME_DATA_FILE as libc::c_int, + MAILMIME_MECHANISM_8BIT as libc::c_int, + 0i32, + 0 as *const libc::c_char, + 0i32 as size_t, + filename, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_multipart.mm_preamble = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_set_epilogue_file( + mut build_info: *mut mailmime, + mut filename: *mut libc::c_char, +) -> libc::c_int { + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + data = mailmime_data_new( + MAILMIME_DATA_FILE as libc::c_int, + MAILMIME_MECHANISM_8BIT as libc::c_int, + 0i32, + 0 as *const libc::c_char, + 0i32 as size_t, + filename, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_multipart.mm_epilogue = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_set_preamble_text( + mut build_info: *mut mailmime, + mut data_str: *mut libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + data = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + MAILMIME_MECHANISM_8BIT as libc::c_int, + 0i32, + data_str, + length, + 0 as *mut libc::c_char, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_multipart.mm_preamble = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_set_epilogue_text( + mut build_info: *mut mailmime, + mut data_str: *mut libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + data = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + MAILMIME_MECHANISM_8BIT as libc::c_int, + 0i32, + data_str, + length, + 0 as *mut libc::c_char, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_multipart.mm_epilogue = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_set_body_file( + mut build_info: *mut mailmime, + mut filename: *mut libc::c_char, +) -> libc::c_int { + let mut encoding: libc::c_int = 0; + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + encoding = mailmime_transfer_encoding_get((*build_info).mm_mime_fields); + data = mailmime_data_new( + MAILMIME_DATA_FILE as libc::c_int, + encoding, + 0i32, + 0 as *const libc::c_char, + 0i32 as size_t, + filename, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_single = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_set_body_text( + mut build_info: *mut mailmime, + mut data_str: *mut libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut encoding: libc::c_int = 0; + let mut data: *mut mailmime_data = 0 as *mut mailmime_data; + encoding = mailmime_transfer_encoding_get((*build_info).mm_mime_fields); + data = mailmime_data_new( + MAILMIME_DATA_TEXT as libc::c_int, + encoding, + 0i32, + data_str, + length, + 0 as *mut libc::c_char, + ); + if data.is_null() { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*build_info).mm_data.mm_single = data; + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_add_part( + mut build_info: *mut mailmime, + mut part: *mut mailmime, +) -> libc::c_int { + let mut r: libc::c_int = 0; + if (*build_info).mm_type == MAILMIME_MESSAGE as libc::c_int { + (*build_info).mm_data.mm_message.mm_msg_mime = part; + (*part).mm_parent_type = MAILMIME_MESSAGE as libc::c_int; + (*part).mm_parent = build_info + } else if (*build_info).mm_type == MAILMIME_MULTIPLE as libc::c_int { + r = clist_insert_after( + (*build_info).mm_data.mm_multipart.mm_mp_list, + (*(*build_info).mm_data.mm_multipart.mm_mp_list).last, + part as *mut libc::c_void, + ); + if r != 0i32 { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + (*part).mm_parent_type = MAILMIME_MULTIPLE as libc::c_int; + (*part).mm_parent = build_info; + (*part).mm_multipart_pos = (*(*build_info).mm_data.mm_multipart.mm_mp_list).last + } else { + return MAILIMF_ERROR_INVAL as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_remove_part(mut mime: *mut mailmime) { + let mut parent: *mut mailmime = 0 as *mut mailmime; + parent = (*mime).mm_parent; + if parent.is_null() { + return; + } + match (*mime).mm_parent_type { + 3 => { + (*mime).mm_parent = 0 as *mut mailmime; + (*parent).mm_data.mm_message.mm_msg_mime = 0 as *mut mailmime + } + 2 => { + (*mime).mm_parent = 0 as *mut mailmime; + clist_delete( + (*parent).mm_data.mm_multipart.mm_mp_list, + (*mime).mm_multipart_pos, + ); + } + _ => {} + }; +} + +pub unsafe fn mailmime_set_imf_fields( + mut build_info: *mut mailmime, + mut mm_fields: *mut mailimf_fields, +) { + (*build_info).mm_data.mm_message.mm_fields = mm_fields; +} + +pub unsafe fn mailmime_single_fields_init( + mut single_fields: *mut mailmime_single_fields, + mut fld_fields: *mut mailmime_fields, + mut fld_content: *mut mailmime_content, +) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + memset( + single_fields as *mut libc::c_void, + 0i32, + ::std::mem::size_of::() as libc::size_t, + ); + if !fld_content.is_null() { + mailmime_content_single_fields_init(single_fields, fld_content); + } + if fld_fields.is_null() { + return; + } + cur = (*(*fld_fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_field; + match (*field).fld_type { + 1 => { + mailmime_content_single_fields_init(single_fields, (*field).fld_data.fld_content); + } + 2 => (*single_fields).fld_encoding = (*field).fld_data.fld_encoding, + 3 => (*single_fields).fld_id = (*field).fld_data.fld_id, + 4 => (*single_fields).fld_description = (*field).fld_data.fld_description, + 5 => (*single_fields).fld_version = (*field).fld_data.fld_version, + 6 => { + mailmime_disposition_single_fields_init( + single_fields, + (*field).fld_data.fld_disposition, + ); + } + 7 => (*single_fields).fld_language = (*field).fld_data.fld_language, + 8 => (*single_fields).fld_location = (*field).fld_data.fld_location, + _ => {} + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } +} +unsafe fn mailmime_disposition_single_fields_init( + mut single_fields: *mut mailmime_single_fields, + mut fld_disposition: *mut mailmime_disposition, +) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + (*single_fields).fld_disposition = fld_disposition; + cur = (*(*fld_disposition).dsp_parms).first; + while !cur.is_null() { + let mut param: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + param = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_disposition_parm; + match (*param).pa_type { + 0 => (*single_fields).fld_disposition_filename = (*param).pa_data.pa_filename, + 1 => (*single_fields).fld_disposition_creation_date = (*param).pa_data.pa_creation_date, + 2 => { + (*single_fields).fld_disposition_modification_date = + (*param).pa_data.pa_modification_date + } + 3 => (*single_fields).fld_disposition_read_date = (*param).pa_data.pa_read_date, + 4 => (*single_fields).fld_disposition_size = (*param).pa_data.pa_size, + _ => {} + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } +} +unsafe fn mailmime_content_single_fields_init( + mut single_fields: *mut mailmime_single_fields, + mut fld_content: *mut mailmime_content, +) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + (*single_fields).fld_content = fld_content; + cur = (*(*fld_content).ct_parameters).first; + while !cur.is_null() { + let mut param: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + param = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime_parameter; + if strcasecmp( + (*param).pa_name, + b"boundary\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + (*single_fields).fld_content_boundary = (*param).pa_value + } + if strcasecmp( + (*param).pa_name, + b"charset\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + (*single_fields).fld_content_charset = (*param).pa_value + } + if strcasecmp( + (*param).pa_name, + b"name\x00" as *const u8 as *const libc::c_char, + ) == 0i32 + { + (*single_fields).fld_content_name = (*param).pa_value + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } +} + +pub unsafe fn mailmime_single_fields_new( + mut fld_fields: *mut mailmime_fields, + mut fld_content: *mut mailmime_content, +) -> *mut mailmime_single_fields { + let mut single_fields: *mut mailmime_single_fields = 0 as *mut mailmime_single_fields; + single_fields = malloc(::std::mem::size_of::() as libc::size_t) + as *mut mailmime_single_fields; + if single_fields.is_null() { + return 0 as *mut mailmime_single_fields; + } else { + mailmime_single_fields_init(single_fields, fld_fields, fld_content); + return single_fields; + }; +} + +pub unsafe fn mailmime_single_fields_free(mut single_fields: *mut mailmime_single_fields) { + free(single_fields as *mut libc::c_void); +} + +pub unsafe fn mailmime_smart_add_part( + mut mime: *mut mailmime, + mut mime_sub: *mut mailmime, +) -> libc::c_int { + let mut saved_sub: *mut mailmime = 0 as *mut mailmime; + let mut mp: *mut mailmime = 0 as *mut mailmime; + let mut res: libc::c_int = 0; + let mut r: libc::c_int = 0; + match (*mime).mm_type { + 1 => res = MAILIMF_ERROR_INVAL as libc::c_int, + 2 => { + r = mailmime_add_part(mime, mime_sub); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + return MAILIMF_NO_ERROR as libc::c_int; + } + } + _ => { + /* MAILMIME_MESSAGE */ + if (*mime).mm_data.mm_message.mm_msg_mime.is_null() { + r = mailmime_add_part(mime, mime_sub); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + return MAILIMF_NO_ERROR as libc::c_int; + } + } else { + if (*(*mime).mm_data.mm_message.mm_msg_mime).mm_type + == MAILMIME_MULTIPLE as libc::c_int + { + return mailmime_add_part((*mime).mm_data.mm_message.mm_msg_mime, mime_sub); + } + saved_sub = (*mime).mm_data.mm_message.mm_msg_mime; + mp = mailmime_multiple_new( + b"multipart/mixed\x00" as *const u8 as *const libc::c_char, + ); + if mp.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + mailmime_remove_part(saved_sub); + r = mailmime_add_part(mime, mp); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + mailmime_free(mp); + } else { + r = mailmime_add_part(mp, saved_sub); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + r = mailmime_add_part(mp, mime_sub); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = MAILIMF_ERROR_MEMORY as libc::c_int + } else { + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + mailmime_free(saved_sub); + } + } + } + } + return res; +} + +pub unsafe fn mailmime_multiple_new(mut type_0: *const libc::c_char) -> *mut mailmime { + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + let mut mp: *mut mailmime = 0 as *mut mailmime; + mime_fields = mailmime_fields_new_empty(); + if !mime_fields.is_null() { + content = mailmime_content_new_with_str(type_0); + if !content.is_null() { + mp = mailmime_new_empty(content, mime_fields); + if mp.is_null() { + mailmime_content_free(content); + } else { + return mp; + } + } + mailmime_fields_free(mime_fields); + } + return 0 as *mut mailmime; +} + +pub unsafe fn mailmime_content_new_with_str(mut str: *const libc::c_char) -> *mut mailmime_content { + let mut r: libc::c_int = 0; + let mut cur_token: size_t = 0; + let mut content: *mut mailmime_content = 0 as *mut mailmime_content; + cur_token = 0i32 as size_t; + r = mailmime_content_parse(str, strlen(str), &mut cur_token, &mut content); + if r != MAILIMF_NO_ERROR as libc::c_int { + return 0 as *mut mailmime_content; + } + return content; +} + +pub unsafe fn mailmime_smart_remove_part(mut mime: *mut mailmime) -> libc::c_int { + let mut parent: *mut mailmime = 0 as *mut mailmime; + let mut res: libc::c_int = 0; + parent = (*mime).mm_parent; + if parent.is_null() { + res = MAILIMF_ERROR_INVAL as libc::c_int + } else { + match (*mime).mm_type { + 3 => { + if !(*mime).mm_data.mm_message.mm_msg_mime.is_null() { + res = MAILIMF_ERROR_INVAL as libc::c_int + } else { + mailmime_remove_part(mime); + mailmime_free(mime); + return MAILIMF_NO_ERROR as libc::c_int; + } + } + 2 => { + if !((*(*mime).mm_data.mm_multipart.mm_mp_list).first + == (*(*mime).mm_data.mm_multipart.mm_mp_list).last + && (*(*mime).mm_data.mm_multipart.mm_mp_list).last.is_null()) + { + res = MAILIMF_ERROR_INVAL as libc::c_int + } else { + mailmime_remove_part(mime); + mailmime_free(mime); + return MAILIMF_NO_ERROR as libc::c_int; + } + } + 1 => { + mailmime_remove_part(mime); + mailmime_free(mime); + return MAILIMF_NO_ERROR as libc::c_int; + } + _ => return MAILIMF_ERROR_INVAL as libc::c_int, + } + } + return res; +} + +pub unsafe fn mailmime_fields_new_encoding(mut type_0: libc::c_int) -> *mut mailmime_fields { + let mut encoding: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + encoding = mailmime_mechanism_new(type_0, 0 as *mut libc::c_char); + if !encoding.is_null() { + mime_fields = mailmime_fields_new_with_data( + encoding, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + ); + if mime_fields.is_null() { + mailmime_mechanism_free(encoding); + } else { + return mime_fields; + } + } + return 0 as *mut mailmime_fields; +} + +pub unsafe fn mailmime_fields_new_filename( + mut dsp_type: libc::c_int, + mut filename: *mut libc::c_char, + mut encoding_type: libc::c_int, +) -> *mut mailmime_fields { + let mut dsp: *mut mailmime_disposition = 0 as *mut mailmime_disposition; + let mut encoding: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + let mut mime_fields: *mut mailmime_fields = 0 as *mut mailmime_fields; + dsp = mailmime_disposition_new_with_data( + dsp_type, + filename, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + -1i32 as size_t, + ); + if !dsp.is_null() { + encoding = mailmime_mechanism_new(encoding_type, 0 as *mut libc::c_char); + if !encoding.is_null() { + mime_fields = mailmime_fields_new_with_data( + encoding, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + dsp, + 0 as *mut mailmime_language, + ); + if mime_fields.is_null() { + mailmime_encoding_free(encoding); + } else { + return mime_fields; + } + } + mailmime_disposition_free(dsp); + } + return 0 as *mut mailmime_fields; +} + +pub unsafe fn mailmime_param_new_with_data( + mut name: *mut libc::c_char, + mut value: *mut libc::c_char, +) -> *mut mailmime_parameter { + let mut param_name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut param_value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut param: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + param_name = strdup(name); + if !param_name.is_null() { + param_value = strdup(value); + if !param_value.is_null() { + param = mailmime_parameter_new(param_name, param_value); + if param.is_null() { + free(param_value as *mut libc::c_void); + } else { + return param; + } + } + free(param_name as *mut libc::c_void); + } + return 0 as *mut mailmime_parameter; +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_generate_boundary() { + let res_c = unsafe { mailmime_generate_boundary() }; + let res = unsafe { std::ffi::CStr::from_ptr(res_c).to_str().unwrap() }; + assert_eq!(res.len(), 22); + + unsafe { free(res_c as *mut _) }; + } +} diff --git a/mmime/src/mailmime_write_generic.rs b/mmime/src/mailmime_write_generic.rs new file mode 100644 index 000000000..332db6f04 --- /dev/null +++ b/mmime/src/mailmime_write_generic.rs @@ -0,0 +1,1979 @@ +use std::ffi::CStr; + +use crate::clist::*; +use crate::mailimf_write_generic::*; +use crate::mailmime::*; +use crate::mailmime_content::*; +use crate::mailmime_types::*; +use crate::mailmime_types_helper::*; +use crate::other::*; + +pub const STATE_INIT: libc::c_uint = 0; +pub const STATE_SPACE_CR: libc::c_uint = 3; +pub const STATE_SPACE: libc::c_uint = 2; +pub const STATE_CR: libc::c_uint = 1; + +pub unsafe fn mailmime_fields_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut fields: *mut mailmime_fields, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (*cur).data as *mut mailmime_field; + r = mailmime_field_write_driver(do_write, data, col, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +unsafe fn mailmime_field_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut field: *mut mailmime_field, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*field).fld_type { + 1 => r = mailmime_content_write_driver(do_write, data, col, (*field).fld_data.fld_content), + 2 => { + r = mailmime_encoding_write_driver(do_write, data, col, (*field).fld_data.fld_encoding) + } + 3 => r = mailmime_id_write_driver(do_write, data, col, (*field).fld_data.fld_id), + 4 => { + r = mailmime_description_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_description, + ) + } + 5 => r = mailmime_version_write_driver(do_write, data, col, (*field).fld_data.fld_version), + 6 => { + r = mailmime_disposition_write_driver( + do_write, + data, + col, + (*field).fld_data.fld_disposition, + ) + } + 7 => { + r = mailmime_language_write_driver(do_write, data, col, (*field).fld_data.fld_language) + } + 8 => { + r = mailmime_location_write_driver(do_write, data, col, (*field).fld_data.fld_location) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_location_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut location: *mut libc::c_char, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut len: libc::c_int = strlen(location) as libc::c_int; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Location: \x00" as *const u8 as *const libc::c_char, + 18i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if *col > 1i32 && *col + len > 78i32 { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver(do_write, data, col, location, len as size_t); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_language_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut language: *mut mailmime_language, +) -> libc::c_int { + let mut r: libc::c_int = 0; + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut first: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Language: \x00" as *const u8 as *const libc::c_char, + 18i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + first = 1i32; + cur = (*(*language).lg_list).first; + while !cur.is_null() { + let mut lang: *mut libc::c_char = 0 as *mut libc::c_char; + let mut len: size_t = 0; + lang = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut libc::c_char; + len = strlen(lang); + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b", \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + first = 0i32 + } + if *col > 1i32 { + if (*col as libc::size_t).wrapping_add(len) > 78i32 as libc::size_t { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } + r = mailimf_string_write_driver(do_write, data, col, lang, len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_disposition_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut disposition: *mut mailmime_disposition, +) -> libc::c_int { + let mut dsp_type: *mut mailmime_disposition_type = 0 as *mut mailmime_disposition_type; + let mut r: libc::c_int = 0; + let mut cur: *mut clistiter = 0 as *mut clistiter; + dsp_type = (*disposition).dsp_type; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Disposition: \x00" as *const u8 as *const libc::c_char, + 21i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + match (*dsp_type).dsp_type { + 1 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"inline\x00" as *const u8 as *const libc::c_char, + 6i32 as size_t, + ) + } + 2 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"attachment\x00" as *const u8 as *const libc::c_char, + 10i32 as size_t, + ) + } + 3 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*dsp_type).dsp_extension, + strlen((*dsp_type).dsp_extension), + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = (*(*disposition).dsp_parms).first; + while !cur.is_null() { + let mut param: *mut mailmime_disposition_parm = 0 as *mut mailmime_disposition_parm; + param = (*cur).data as *mut mailmime_disposition_parm; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"; \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailmime_disposition_param_write_driver(do_write, data, col, param); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_disposition_param_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut param: *mut mailmime_disposition_parm, +) -> libc::c_int { + let mut len: size_t = 0; + let mut r: libc::c_int = 0; + let mut sizestr: *mut libc::c_char = std::ptr::null_mut(); + + match (*param).pa_type { + 0 => { + len = strlen(b"filename=\x00" as *const u8 as *const libc::c_char) + .wrapping_add(strlen((*param).pa_data.pa_filename)) + } + 1 => { + len = strlen(b"creation-date=\x00" as *const u8 as *const libc::c_char) + .wrapping_add(strlen((*param).pa_data.pa_creation_date)) + } + 2 => { + len = strlen(b"modification-date=\x00" as *const u8 as *const libc::c_char) + .wrapping_add(strlen((*param).pa_data.pa_modification_date)) + } + 3 => { + len = strlen(b"read-date=\x00" as *const u8 as *const libc::c_char) + .wrapping_add(strlen((*param).pa_data.pa_read_date)) + } + 4 => { + let value = (*param).pa_data.pa_size as u32; + let raw = format!("{}", value); + let raw_c = std::ffi::CString::new(raw).unwrap(); + sizestr = strdup(raw_c.as_ptr()); + len = strlen(b"size=\x00" as *const u8 as *const libc::c_char) + .wrapping_add(strlen(sizestr)) + } + 5 => { + len = strlen((*(*param).pa_data.pa_parameter).pa_name) + .wrapping_add(1i32 as libc::size_t) + .wrapping_add(strlen((*(*param).pa_data.pa_parameter).pa_value)) + } + _ => return MAILIMF_ERROR_INVAL as libc::c_int, + } + if *col > 1i32 { + if (*col as libc::size_t).wrapping_add(len) > 78i32 as libc::size_t { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } + match (*param).pa_type { + 0 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"filename=\x00" as *const u8 as *const libc::c_char, + 9i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*param).pa_data.pa_filename, + strlen((*param).pa_data.pa_filename), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 1 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"creation-date=\x00" as *const u8 as *const libc::c_char, + 14i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*param).pa_data.pa_creation_date, + strlen((*param).pa_data.pa_creation_date), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 2 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"modification-date=\x00" as *const u8 as *const libc::c_char, + 18i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*param).pa_data.pa_modification_date, + strlen((*param).pa_data.pa_modification_date), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 3 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"read-date=\x00" as *const u8 as *const libc::c_char, + 10i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*param).pa_data.pa_read_date, + strlen((*param).pa_data.pa_read_date), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 4 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"size=\x00" as *const u8 as *const libc::c_char, + 5i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver(do_write, data, col, sizestr, strlen(sizestr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + 5 => { + r = mailmime_parameter_write_driver(do_write, data, col, (*param).pa_data.pa_parameter); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + _ => {} + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_parameter_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut param: *mut mailmime_parameter, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + (*param).pa_name, + strlen((*param).pa_name), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"=\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_quoted_string_write_driver( + do_write, + data, + col, + (*param).pa_value, + strlen((*param).pa_value), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_version_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut version: uint32_t, +) -> libc::c_int { + let mut r: libc::c_int = 0; + + r = mailimf_string_write_driver( + do_write, + data, + col, + b"MIME-Version: \x00" as *const u8 as *const libc::c_char, + 14i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + + let raw = format!("{}.{}", (version >> 16) as i32, (version & 0xffff) as i32); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut versionstr = strdup(raw_c.as_ptr()); + r = mailimf_string_write_driver(do_write, data, col, versionstr, strlen(versionstr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_description_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut descr: *mut libc::c_char, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Description: \x00" as *const u8 as *const libc::c_char, + 21i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver(do_write, data, col, descr, strlen(descr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_id_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut id: *mut libc::c_char, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-ID: \x00" as *const u8 as *const libc::c_char, + 12i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"<\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver(do_write, data, col, id, strlen(id)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b">\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_encoding_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut encoding: *mut mailmime_mechanism, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Transfer-Encoding: \x00" as *const u8 as *const libc::c_char, + 27i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + match (*encoding).enc_type { + 1 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"7bit\x00" as *const u8 as *const libc::c_char, + 4i32 as size_t, + ) + } + 2 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"8bit\x00" as *const u8 as *const libc::c_char, + 4i32 as size_t, + ) + } + 3 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"binary\x00" as *const u8 as *const libc::c_char, + 6i32 as size_t, + ) + } + 4 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"quoted-printable\x00" as *const u8 as *const libc::c_char, + 16i32 as size_t, + ) + } + 5 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"base64\x00" as *const u8 as *const libc::c_char, + 6i32 as size_t, + ) + } + 6 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*encoding).enc_token, + strlen((*encoding).enc_token), + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_content_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut content: *mut mailmime_content, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"Content-Type: \x00" as *const u8 as *const libc::c_char, + 14i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailmime_content_type_write_driver(do_write, data, col, content); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_content_type_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut content: *mut mailmime_content, +) -> libc::c_int { + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut len: size_t = 0; + let mut r: libc::c_int = 0; + r = mailmime_type_write_driver(do_write, data, col, (*content).ct_type); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"/\x00" as *const u8 as *const libc::c_char, + 1i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + r = mailimf_string_write_driver( + do_write, + data, + col, + (*content).ct_subtype, + strlen((*content).ct_subtype), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + if !(*content).ct_parameters.is_null() { + cur = (*(*content).ct_parameters).first; + while !cur.is_null() { + let mut param: *mut mailmime_parameter = 0 as *mut mailmime_parameter; + param = (*cur).data as *mut mailmime_parameter; + r = mailimf_string_write_driver( + do_write, + data, + col, + b"; \x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + len = strlen((*param).pa_name) + .wrapping_add(1i32 as libc::size_t) + .wrapping_add(strlen((*param).pa_value)); + if *col > 1i32 { + if (*col as libc::size_t).wrapping_add(len) > 78i32 as libc::size_t { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n \x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } + r = mailmime_parameter_write_driver(do_write, data, col, param); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* +static int mailmime_content_write_driver(int (* do_write)(void *, const char *, size_t), void * data, int * col, + struct mailmime_content * content); +*/ +unsafe fn mailmime_type_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut type_0: *mut mailmime_type, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*type_0).tp_type { + 1 => { + r = mailmime_discrete_type_write_driver( + do_write, + data, + col, + (*type_0).tp_data.tp_discrete_type, + ) + } + 2 => { + r = mailmime_composite_type_write_driver( + do_write, + data, + col, + (*type_0).tp_data.tp_composite_type, + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_composite_type_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut composite_type: *mut mailmime_composite_type, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*composite_type).ct_type { + 1 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"message\x00" as *const u8 as *const libc::c_char, + 7i32 as size_t, + ) + } + 2 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"multipart\x00" as *const u8 as *const libc::c_char, + 9i32 as size_t, + ) + } + 3 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*composite_type).ct_token, + strlen((*composite_type).ct_token), + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +unsafe fn mailmime_discrete_type_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut discrete_type: *mut mailmime_discrete_type, +) -> libc::c_int { + let mut r: libc::c_int = 0; + match (*discrete_type).dt_type { + 1 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"text\x00" as *const u8 as *const libc::c_char, + 4i32 as size_t, + ) + } + 2 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"image\x00" as *const u8 as *const libc::c_char, + 5i32 as size_t, + ) + } + 3 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"audio\x00" as *const u8 as *const libc::c_char, + 5i32 as size_t, + ) + } + 4 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"video\x00" as *const u8 as *const libc::c_char, + 5i32 as size_t, + ) + } + 5 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"application\x00" as *const u8 as *const libc::c_char, + 11i32 as size_t, + ) + } + 6 => { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*discrete_type).dt_extension, + strlen((*discrete_type).dt_extension), + ) + } + _ => r = MAILIMF_ERROR_INVAL as libc::c_int, + } + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +pub unsafe fn mailmime_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut build_info: *mut mailmime, +) -> libc::c_int { + if !(*build_info).mm_parent.is_null() { + return mailmime_sub_write_driver(do_write, data, col, build_info); + } else { + return mailmime_part_write_driver(do_write, data, col, build_info); + }; +} +/* +static int mailmime_base64_write_driver(int (* do_write)(void *, const char *, size_t), void * data, int * col, + char * text, size_t size); + +static int mailmime_quoted_printable_write_driver(int (* do_write)(void *, const char *, size_t), void * data, int * col, int istext, + char * text, size_t size); +*/ +unsafe fn mailmime_part_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut build_info: *mut mailmime, +) -> libc::c_int { + let mut current_block: u64; + let mut cur: *mut clistiter = 0 as *mut clistiter; + let mut first: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut boundary: *mut libc::c_char = 0 as *mut libc::c_char; + let mut istext: libc::c_int = 0; + let mut res: libc::c_int = 0; + istext = 1i32; + boundary = 0 as *mut libc::c_char; + if !(*build_info).mm_content_type.is_null() { + if (*build_info).mm_type == MAILMIME_MULTIPLE as libc::c_int { + boundary = mailmime_extract_boundary((*build_info).mm_content_type); + if boundary.is_null() { + boundary = mailmime_generate_boundary(); + if boundary.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 13530634675565645571; + } else { + current_block = 13586036798005543211; + } + } else { + current_block = 13586036798005543211; + } + } else { + current_block = 13586036798005543211; + } + match current_block { + 13530634675565645571 => {} + _ => { + if (*(*(*build_info).mm_content_type).ct_type).tp_type + == MAILMIME_TYPE_DISCRETE_TYPE as libc::c_int + { + if (*(*(*(*build_info).mm_content_type).ct_type) + .tp_data + .tp_discrete_type) + .dt_type + != MAILMIME_DISCRETE_TYPE_TEXT as libc::c_int + { + istext = 0i32 + } + } + current_block = 8457315219000651999; + } + } + } else { + current_block = 8457315219000651999; + } + match current_block { + 8457315219000651999 => { + match (*build_info).mm_type { + 1 => { + /* 1-part body */ + if !(*build_info).mm_data.mm_single.is_null() { + r = mailmime_data_write_driver( + do_write, + data, + col, + (*build_info).mm_data.mm_single, + istext, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + current_block = 7639320476250304355; + } + } else { + current_block = 7639320476250304355; + } + } + 2 => { + /* multi-part */ + /* preamble */ + if !(*build_info).mm_data.mm_multipart.mm_preamble.is_null() { + r = mailmime_data_write_driver( + do_write, + data, + col, + (*build_info).mm_data.mm_multipart.mm_preamble, + 1i32, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + current_block = 17500079516916021833; + } + } + } else { + current_block = 17500079516916021833; + } + match current_block { + 16754986508692159943 => {} + _ => { + first = 1i32; + cur = (*(*build_info).mm_data.mm_multipart.mm_mp_list).first; + loop { + if cur.is_null() { + current_block = 3546145585875536353; + break; + } + let mut subpart: *mut mailmime = 0 as *mut mailmime; + subpart = (*cur).data as *mut mailmime; + if 0 == first { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + break; + } + } else { + first = 0i32 + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"--\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + break; + } else if boundary.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 16754986508692159943; + break; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + boundary, + strlen(boundary), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + break; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + break; + } else { + r = mailmime_sub_write_driver( + do_write, data, col, subpart, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + break; + } else { + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + } + } + } + match current_block { + 16754986508692159943 => {} + _ => { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"--\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else if boundary.is_null() { + res = MAILIMF_ERROR_MEMORY as libc::c_int; + current_block = 16754986508692159943; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + boundary, + strlen(boundary), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"--\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 + as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else if !(*build_info) + .mm_data + .mm_multipart + .mm_epilogue + .is_null() + { + r = mailmime_data_write_driver( + do_write, + data, + col, + (*build_info) + .mm_data + .mm_multipart + .mm_epilogue, + 1i32, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + current_block = 7639320476250304355; + } + } else { + current_block = 7639320476250304355; + } + } + } + } + } + } + } + } + } + } + 3 => { + if !(*build_info).mm_data.mm_message.mm_fields.is_null() { + r = mailimf_fields_write_driver( + do_write, + data, + col, + (*build_info).mm_data.mm_message.mm_fields, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + current_block = 2798392256336243897; + } + } else { + current_block = 2798392256336243897; + } + match current_block { + 16754986508692159943 => {} + _ => { + if !(*build_info).mm_mime_fields.is_null() { + let mut r_0: libc::c_int = 0; + let mut cur_0: *mut clistiter = 0 as *mut clistiter; + cur_0 = (*(*(*build_info).mm_mime_fields).fld_list).first; + loop { + if cur_0.is_null() { + current_block = 562309032768341766; + break; + } + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (*cur_0).data as *mut mailmime_field; + if (*field).fld_type == MAILMIME_FIELD_VERSION as libc::c_int { + r_0 = + mailmime_field_write_driver(do_write, data, col, field); + if r_0 != MAILIMF_NO_ERROR as libc::c_int { + res = r_0; + current_block = 16754986508692159943; + break; + } + } + cur_0 = if !cur_0.is_null() { + (*cur_0).next + } else { + 0 as *mut clistcell + } + } + } else { + current_block = 562309032768341766; + } + match current_block { + 16754986508692159943 => {} + _ => { + /* encapsuled message */ + if !(*build_info).mm_data.mm_message.mm_msg_mime.is_null() { + r = mailmime_sub_write_driver( + do_write, + data, + col, + (*build_info).mm_data.mm_message.mm_msg_mime, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 16754986508692159943; + } else { + current_block = 7639320476250304355; + } + } else { + current_block = 7639320476250304355; + } + } + } + } + } + } + _ => { + current_block = 7639320476250304355; + } + } + match current_block { + 16754986508692159943 => { + free(boundary as *mut libc::c_void); + } + _ => { + free(boundary as *mut libc::c_void); + return MAILIMF_NO_ERROR as libc::c_int; + } + } + } + _ => {} + } + return res; +} +unsafe fn mailmime_sub_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut build_info: *mut mailmime, +) -> libc::c_int { + let mut r: libc::c_int = 0; + if !(*build_info).mm_content_type.is_null() { + r = mailmime_content_write_driver(do_write, data, col, (*build_info).mm_content_type); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + if (*build_info).mm_type != MAILMIME_MESSAGE as libc::c_int { + if !(*build_info).mm_mime_fields.is_null() { + r = mailmime_fields_write_driver(do_write, data, col, (*build_info).mm_mime_fields); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } else if !(*build_info).mm_mime_fields.is_null() { + let mut r_0: libc::c_int = 0; + let mut cur: *mut clistiter = 0 as *mut clistiter; + cur = (*(*(*build_info).mm_mime_fields).fld_list).first; + while !cur.is_null() { + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + field = (*cur).data as *mut mailmime_field; + if (*field).fld_type != MAILMIME_FIELD_VERSION as libc::c_int { + r_0 = mailmime_field_write_driver(do_write, data, col, field); + if r_0 != MAILIMF_NO_ERROR as libc::c_int { + return r_0; + } + } + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return mailmime_part_write_driver(do_write, data, col, build_info); +} + +pub unsafe fn mailmime_data_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut mime_data: *mut mailmime_data, + mut istext: libc::c_int, +) -> libc::c_int { + let mut current_block: u64 = 0; + let mut fd: libc::c_int = 0; + let mut r: libc::c_int = 0; + let mut text: *mut libc::c_char = 0 as *mut libc::c_char; + + let mut res: libc::c_int = 0; + match (*mime_data).dt_type { + 0 => { + if 0 != (*mime_data).dt_encoded { + r = mailimf_string_write_driver( + do_write, + data, + col, + (*mime_data).dt_data.dt_text.dt_data, + (*mime_data).dt_data.dt_text.dt_length, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } else { + r = mailmime_text_content_write_driver( + do_write, + data, + col, + (*mime_data).dt_encoding, + istext, + (*mime_data).dt_data.dt_text.dt_data, + (*mime_data).dt_data.dt_text.dt_length, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + } + 1 => { + let filename = CStr::from_ptr((*mime_data).dt_data.dt_filename) + .to_str() + .unwrap(); + if let Ok(file) = std::fs::File::open(filename) { + if let Ok(mut text) = memmap::MmapOptions::new().map_copy(&file) { + if 0 != (*mime_data).dt_encoded { + r = mailimf_string_write_driver( + do_write, + data, + col, + text.as_ptr() as *const _, + text.len(), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 1055471768422549395; + } else { + current_block = 1538046216550696469; + } + } else { + r = mailmime_text_content_write_driver( + do_write, + data, + col, + (*mime_data).dt_encoding, + istext, + text.as_ptr() as *const _, + text.len(), + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + res = r; + current_block = 1055471768422549395; + } else { + current_block = 1538046216550696469; + } + } + match current_block { + 1055471768422549395 => { + current_block = 5221028069996397600; + } + _ => { + current_block = 9853141518545631134; + } + } + } else { + res = MAILIMF_ERROR_FILE as libc::c_int; + current_block = 5221028069996397600; + } + match current_block { + 5221028069996397600 => {} + _ => { + close(fd); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + current_block = 10891380440665537214; + } + } + } else { + res = MAILIMF_ERROR_FILE as libc::c_int; + current_block = 10275258781883576179; + } + match current_block { + 10891380440665537214 => {} + _ => { + current_block = 10275258781883576179; + } + } + match current_block { + 10891380440665537214 => {} + _ => return res, + } + } + _ => {} + } + return MAILIMF_NO_ERROR as libc::c_int; +} +/* ****************************************************************** */ +/* message */ +/* +static int mailmime_data_write_driver(int (* do_write)(void *, const char *, size_t), void * data, int * col, + struct mailmime_data * data, + int is_text); +*/ +unsafe fn mailmime_text_content_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut encoding: libc::c_int, + mut istext: libc::c_int, + mut text: *const libc::c_char, + mut size: size_t, +) -> libc::c_int { + match encoding { + 4 => { + return mailmime_quoted_printable_write_driver(do_write, data, col, istext, text, size) + } + 5 => return mailmime_base64_write_driver(do_write, data, col, text, size), + 1 | 2 | 3 | _ => return mailimf_string_write_driver(do_write, data, col, text, size), + }; +} + +pub unsafe fn mailmime_base64_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut text: *const libc::c_char, + mut size: size_t, +) -> libc::c_int { + let mut a: libc::c_int = 0; + let mut b: libc::c_int = 0; + let mut c: libc::c_int = 0; + let mut remains: size_t = 0; + let mut p: *const libc::c_char = 0 as *const libc::c_char; + let mut count: size_t = 0; + let mut ogroup: [libc::c_char; 4] = [0; 4]; + let mut r: libc::c_int = 0; + remains = size; + p = text; + while remains > 0i32 as libc::size_t { + match remains { + 1 => { + a = *p.offset(0isize) as libc::c_uchar as libc::c_int; + b = 0i32; + c = 0i32; + count = 1i32 as size_t + } + 2 => { + a = *p.offset(0isize) as libc::c_uchar as libc::c_int; + b = *p.offset(1isize) as libc::c_uchar as libc::c_int; + c = 0i32; + count = 2i32 as size_t + } + _ => { + a = *p.offset(0isize) as libc::c_uchar as libc::c_int; + b = *p.offset(1isize) as libc::c_uchar as libc::c_int; + c = *p.offset(2isize) as libc::c_uchar as libc::c_int; + count = 3i32 as size_t + } + } + ogroup[0usize] = base64_encoding[(a >> 2i32) as usize]; + ogroup[1usize] = base64_encoding[((a & 3i32) << 4i32 | b >> 4i32) as usize]; + ogroup[2usize] = base64_encoding[((b & 0xfi32) << 2i32 | c >> 6i32) as usize]; + ogroup[3usize] = base64_encoding[(c & 0x3fi32) as usize]; + match count { + 1 => { + ogroup[2usize] = '=' as i32 as libc::c_char; + ogroup[3usize] = '=' as i32 as libc::c_char + } + 2 => ogroup[3usize] = '=' as i32 as libc::c_char, + _ => {} + } + if *col + 4i32 > 76i32 { + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + r = mailimf_string_write_driver(do_write, data, col, ogroup.as_mut_ptr(), 4i32 as size_t); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + remains = (remains as libc::size_t).wrapping_sub(count) as size_t as size_t; + p = p.offset(count as isize) + } + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + return MAILIMF_NO_ERROR as libc::c_int; +} +static mut base64_encoding: [libc::c_char; 65] = [ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 0, +]; + +pub unsafe fn mailmime_quoted_printable_write_driver( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut istext: libc::c_int, + mut text: *const libc::c_char, + mut size: size_t, +) -> libc::c_int { + let mut i: size_t = 0; + let mut start: *const libc::c_char = 0 as *const libc::c_char; + let mut len: size_t = 0; + let mut r: libc::c_int = 0; + let mut state: libc::c_int = 0; + start = text; + len = 0i32 as size_t; + state = STATE_INIT as libc::c_int; + i = 0i32 as size_t; + while i < size { + let mut ch: libc::c_uchar = 0; + if (*col as libc::size_t).wrapping_add(len) > 72i32 as libc::size_t { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize); + r = mailimf_string_write_driver( + do_write, + data, + col, + b"=\r\n\x00" as *const u8 as *const libc::c_char, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + } + ch = *text.offset(i as isize) as libc::c_uchar; + match state { + 0 => { + let mut current_block_50: u64; + match ch as libc::c_int { + 32 | 9 => { + state = STATE_SPACE as libc::c_int; + len = len.wrapping_add(1); + i = i.wrapping_add(1); + current_block_50 = 3546145585875536353; + } + 13 => { + state = STATE_CR as libc::c_int; + i = i.wrapping_add(1); + current_block_50 = 3546145585875536353; + } + 33 | 34 | 35 | 36 | 64 | 91 | 92 | 93 | 94 | 96 | 123 | 124 | 125 | 126 + | 61 | 63 | 95 => { + /* there is no more 'From' at the beginning of a line */ + current_block_50 = 177397332496894159; + } + 70 => { + current_block_50 = 177397332496894159; + } + _ => { + if 0 != istext && ch as libc::c_int == '\n' as i32 { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + i = i.wrapping_add(1) + } else if ch as libc::c_int >= 33i32 && ch as libc::c_int <= 60i32 + || ch as libc::c_int >= 62i32 && ch as libc::c_int <= 126i32 + { + len = len.wrapping_add(1); + i = i.wrapping_add(1) + } else { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + + let raw = format!("={:02X}", (ch as libc::c_int)); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + r = mailimf_string_write_driver( + do_write, + data, + col, + hexstr, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + i = i.wrapping_add(1) + } + current_block_50 = 3546145585875536353; + } + } + match current_block_50 { + 177397332496894159 => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + let raw = format!("={:02X}", ch as libc::c_int); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + r = mailimf_string_write_driver( + do_write, + data, + col, + hexstr, + 3i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + i = i.wrapping_add(1) + } + _ => {} + } + } + 1 => match ch as libc::c_int { + 10 => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + r = mailimf_string_write_driver( + do_write, + data, + col, + b"\r\n\x00" as *const u8 as *const libc::c_char, + 2i32 as size_t, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + i = i.wrapping_add(1); + state = STATE_INIT as libc::c_int + } + _ => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize); + let raw = format!("={:02X}", b'\r' as i32); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + r = mailimf_string_write_driver(do_write, data, col, hexstr, 3i32 as size_t); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + state = STATE_INIT as libc::c_int + } + }, + 2 => match ch as libc::c_int { + 13 => { + state = STATE_SPACE_CR as libc::c_int; + i = i.wrapping_add(1) + } + 10 => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + let raw = format!( + "={:02X}\r\n", + *text.offset(i.wrapping_sub(1i32 as libc::size_t) as isize) as libc::c_int + ); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + + r = mailimf_string_write_driver(do_write, data, col, hexstr, strlen(hexstr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + state = STATE_INIT as libc::c_int; + i = i.wrapping_add(1) + } + 32 | 9 => { + len = len.wrapping_add(1); + i = i.wrapping_add(1) + } + _ => state = STATE_INIT as libc::c_int, + }, + 3 => match ch as libc::c_int { + 10 => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + let raw = format!( + "={:02X}\r\n", + *text.offset(i.wrapping_sub(2i32 as libc::size_t) as isize) as libc::c_int + ); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + + r = mailimf_string_write_driver(do_write, data, col, hexstr, strlen(hexstr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + state = STATE_INIT as libc::c_int; + i = i.wrapping_add(1) + } + _ => { + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + start = text.offset(i as isize).offset(1isize); + let raw = format!( + "{}={:02X}\r\n", + (*text.offset(i.wrapping_sub(2i32 as libc::size_t) as isize) as u8 as char), + b'\r' as i32 + ); + let raw_c = std::ffi::CString::new(raw).unwrap(); + let mut hexstr = strdup(raw_c.as_ptr()); + + r = mailimf_string_write_driver(do_write, data, col, hexstr, strlen(hexstr)); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + state = STATE_INIT as libc::c_int + } + }, + _ => {} + } + } + r = write_remaining(do_write, data, col, &mut start, &mut len); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + return MAILIMF_NO_ERROR as libc::c_int; +} +#[inline] +unsafe fn write_remaining( + mut do_write: Option< + unsafe fn(_: *mut libc::c_void, _: *const libc::c_char, _: size_t) -> libc::c_int, + >, + mut data: *mut libc::c_void, + mut col: *mut libc::c_int, + mut pstart: *mut *const libc::c_char, + mut plen: *mut size_t, +) -> libc::c_int { + let mut r: libc::c_int = 0; + if *plen > 0i32 as libc::size_t { + r = mailimf_string_write_driver(do_write, data, col, *pstart, *plen); + if r != MAILIMF_NO_ERROR as libc::c_int { + return r; + } + *plen = 0i32 as size_t + } + return MAILIMF_NO_ERROR as libc::c_int; +} diff --git a/mmime/src/mailmime_write_mem.rs b/mmime/src/mailmime_write_mem.rs new file mode 100644 index 000000000..2a4dce81d --- /dev/null +++ b/mmime/src/mailmime_write_mem.rs @@ -0,0 +1,82 @@ +use crate::mailmime_types::*; +use crate::mailmime_write_generic::*; +use crate::mmapstring::*; +use crate::other::*; + +unsafe fn do_write( + mut data: *mut libc::c_void, + mut str: *const libc::c_char, + mut length: size_t, +) -> libc::c_int { + let mut f: *mut MMAPString = 0 as *mut MMAPString; + f = data as *mut MMAPString; + if mmap_string_append_len(f, str, length).is_null() { + return 0i32; + } else { + return length as libc::c_int; + }; +} + +pub unsafe fn mailmime_content_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut content: *mut mailmime_content, +) -> libc::c_int { + return mailmime_content_write_driver(Some(do_write), f as *mut libc::c_void, col, content); +} + +pub unsafe fn mailmime_content_type_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut content: *mut mailmime_content, +) -> libc::c_int { + return mailmime_content_type_write_driver( + Some(do_write), + f as *mut libc::c_void, + col, + content, + ); +} + +pub unsafe fn mailmime_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut build_info: *mut mailmime, +) -> libc::c_int { + return mailmime_write_driver(Some(do_write), f as *mut libc::c_void, col, build_info); +} + +pub unsafe fn mailmime_quoted_printable_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut istext: libc::c_int, + mut text: *const libc::c_char, + mut size: size_t, +) -> libc::c_int { + return mailmime_quoted_printable_write_driver( + Some(do_write), + f as *mut libc::c_void, + col, + istext, + text, + size, + ); +} + +pub unsafe fn mailmime_base64_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut text: *const libc::c_char, + mut size: size_t, +) -> libc::c_int { + return mailmime_base64_write_driver(Some(do_write), f as *mut libc::c_void, col, text, size); +} + +pub unsafe fn mailmime_data_write_mem( + mut f: *mut MMAPString, + mut col: *mut libc::c_int, + mut data: *mut mailmime_data, + mut istext: libc::c_int, +) -> libc::c_int { + return mailmime_data_write_driver(Some(do_write), f as *mut libc::c_void, col, data, istext); +} diff --git a/mmime/src/mmapstring.rs b/mmime/src/mmapstring.rs new file mode 100644 index 000000000..a3749ac4c --- /dev/null +++ b/mmime/src/mmapstring.rs @@ -0,0 +1,397 @@ +use std::sync::Mutex; + +use lazy_static::lazy_static; +use libc; + +use crate::chash::*; +use crate::other::*; + +lazy_static! { + static ref mmapstring_lock: Mutex<()> = Mutex::new(()); +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct MMAPString { + pub str_0: *mut libc::c_char, + pub len: size_t, + pub allocated_len: size_t, + pub fd: libc::c_int, + pub mmapped_size: size_t, +} + +pub const TMPDIR: &'static str = "/tmp"; + +pub unsafe fn mmap_string_new(mut init: *const libc::c_char) -> *mut MMAPString { + let mut string: *mut MMAPString = 0 as *mut MMAPString; + string = mmap_string_sized_new(if !init.is_null() { + strlen(init).wrapping_add(2i32 as libc::size_t) + } else { + 2i32 as libc::size_t + }); + if string.is_null() { + return 0 as *mut MMAPString; + } + if !init.is_null() { + mmap_string_append(string, init); + } + return string; +} + +pub unsafe fn mmap_string_append( + mut string: *mut MMAPString, + mut val: *const libc::c_char, +) -> *mut MMAPString { + return mmap_string_insert_len(string, (*string).len, val, strlen(val)); +} + +pub unsafe fn mmap_string_insert_len( + mut string: *mut MMAPString, + mut pos: size_t, + mut val: *const libc::c_char, + mut len: size_t, +) -> *mut MMAPString { + if mmap_string_maybe_expand(string, len).is_null() { + return 0 as *mut MMAPString; + } + if pos < (*string).len { + memmove( + (*string).str_0.offset(pos as isize).offset(len as isize) as *mut libc::c_void, + (*string).str_0.offset(pos as isize) as *const libc::c_void, + (*string).len.wrapping_sub(pos), + ); + } + memmove( + (*string).str_0.offset(pos as isize) as *mut libc::c_void, + val as *const libc::c_void, + len, + ); + (*string).len = ((*string).len as libc::size_t).wrapping_add(len) as size_t as size_t; + *(*string).str_0.offset((*string).len as isize) = 0i32 as libc::c_char; + return string; +} +unsafe fn mmap_string_maybe_expand( + mut string: *mut MMAPString, + mut len: size_t, +) -> *mut MMAPString { + if (*string).len.wrapping_add(len) >= (*string).allocated_len { + let mut old_size: size_t = 0; + let mut newstring: *mut MMAPString = 0 as *mut MMAPString; + old_size = (*string).allocated_len; + (*string).allocated_len = nearest_power( + 1i32 as size_t, + (*string) + .len + .wrapping_add(len) + .wrapping_add(1i32 as libc::size_t), + ); + newstring = mmap_string_realloc_memory(string); + if newstring.is_null() { + (*string).allocated_len = old_size + } + return newstring; + } + return string; +} +/* Strings. + */ +/* SEB */ +unsafe fn mmap_string_realloc_memory(mut string: *mut MMAPString) -> *mut MMAPString { + let mut tmp: *mut libc::c_char = 0 as *mut libc::c_char; + tmp = realloc( + (*string).str_0 as *mut libc::c_void, + (*string).allocated_len, + ) as *mut libc::c_char; + if tmp.is_null() { + string = 0 as *mut MMAPString + } else { + (*string).str_0 = tmp + } + return string; +} +/* MMAPString */ +#[inline] +unsafe fn nearest_power(mut base: size_t, mut num: size_t) -> size_t { + if num > (-1i32 as size_t).wrapping_div(2i32 as libc::size_t) { + return -1i32 as size_t; + } else { + let mut n: size_t = base; + while n < num { + n <<= 1i32 + } + return n; + }; +} + +pub unsafe fn mmap_string_sized_new(mut dfl_size: size_t) -> *mut MMAPString { + let mut string: *mut MMAPString = 0 as *mut MMAPString; + string = malloc(::std::mem::size_of::() as libc::size_t) as *mut MMAPString; + if string.is_null() { + return 0 as *mut MMAPString; + } + (*string).allocated_len = 0i32 as size_t; + (*string).len = 0i32 as size_t; + (*string).str_0 = 0 as *mut libc::c_char; + (*string).fd = -1i32; + (*string).mmapped_size = 0i32 as size_t; + if mmap_string_maybe_expand( + string, + if dfl_size > 2i32 as libc::size_t { + dfl_size + } else { + 2i32 as libc::size_t + }, + ) + .is_null() + { + free(string as *mut libc::c_void); + return 0 as *mut MMAPString; + } + *(*string).str_0.offset(0isize) = 0i32 as libc::c_char; + return string; +} + +pub unsafe fn mmap_string_new_len( + mut init: *const libc::c_char, + mut len: size_t, +) -> *mut MMAPString { + let mut string: *mut MMAPString = 0 as *mut MMAPString; + if len <= 0i32 as libc::size_t { + return mmap_string_new(b"\x00" as *const u8 as *const libc::c_char); + } else { + string = mmap_string_sized_new(len); + if string.is_null() { + return string; + } + if !init.is_null() { + mmap_string_append_len(string, init, len); + } + return string; + }; +} + +pub unsafe fn mmap_string_append_len( + mut string: *mut MMAPString, + mut val: *const libc::c_char, + mut len: size_t, +) -> *mut MMAPString { + return mmap_string_insert_len(string, (*string).len, val, len); +} + +pub unsafe fn mmap_string_free(mut string: *mut MMAPString) { + if string.is_null() { + return; + } + free((*string).str_0 as *mut libc::c_void); + free(string as *mut libc::c_void); +} + +pub unsafe fn mmap_string_assign( + mut string: *mut MMAPString, + mut rval: *const libc::c_char, +) -> *mut MMAPString { + mmap_string_truncate(string, 0i32 as size_t); + if mmap_string_append(string, rval).is_null() { + return 0 as *mut MMAPString; + } + return string; +} + +pub unsafe fn mmap_string_truncate( + mut string: *mut MMAPString, + mut len: size_t, +) -> *mut MMAPString { + (*string).len = if len < (*string).len { + len + } else { + (*string).len + }; + *(*string).str_0.offset((*string).len as isize) = 0i32 as libc::c_char; + return string; +} + +pub unsafe fn mmap_string_set_size( + mut string: *mut MMAPString, + mut len: size_t, +) -> *mut MMAPString { + if len >= (*string).allocated_len { + if mmap_string_maybe_expand(string, len.wrapping_sub((*string).len)).is_null() { + return 0 as *mut MMAPString; + } + } + (*string).len = len; + *(*string).str_0.offset(len as isize) = 0i32 as libc::c_char; + return string; +} + +pub unsafe fn mmap_string_append_c( + mut string: *mut MMAPString, + mut c: libc::c_char, +) -> *mut MMAPString { + return mmap_string_insert_c(string, (*string).len, c); +} + +pub unsafe fn mmap_string_insert_c( + mut string: *mut MMAPString, + mut pos: size_t, + mut c: libc::c_char, +) -> *mut MMAPString { + if mmap_string_maybe_expand(string, 1i32 as size_t).is_null() { + return 0 as *mut MMAPString; + } + if pos < (*string).len { + memmove( + (*string).str_0.offset(pos as isize).offset(1isize) as *mut libc::c_void, + (*string).str_0.offset(pos as isize) as *const libc::c_void, + (*string).len.wrapping_sub(pos), + ); + } + *(*string).str_0.offset(pos as isize) = c; + (*string).len = + ((*string).len as libc::size_t).wrapping_add(1i32 as libc::size_t) as size_t as size_t; + *(*string).str_0.offset((*string).len as isize) = 0i32 as libc::c_char; + return string; +} + +pub unsafe fn mmap_string_prepend( + mut string: *mut MMAPString, + mut val: *const libc::c_char, +) -> *mut MMAPString { + return mmap_string_insert_len(string, 0i32 as size_t, val, strlen(val)); +} + +pub unsafe fn mmap_string_prepend_c( + mut string: *mut MMAPString, + mut c: libc::c_char, +) -> *mut MMAPString { + return mmap_string_insert_c(string, 0i32 as size_t, c); +} + +pub unsafe fn mmap_string_prepend_len( + mut string: *mut MMAPString, + mut val: *const libc::c_char, + mut len: size_t, +) -> *mut MMAPString { + return mmap_string_insert_len(string, 0i32 as size_t, val, len); +} + +pub unsafe fn mmap_string_insert( + mut string: *mut MMAPString, + mut pos: size_t, + mut val: *const libc::c_char, +) -> *mut MMAPString { + return mmap_string_insert_len(string, pos, val, strlen(val)); +} + +pub unsafe fn mmap_string_erase( + mut string: *mut MMAPString, + mut pos: size_t, + mut len: size_t, +) -> *mut MMAPString { + if pos.wrapping_add(len) < (*string).len { + memmove( + (*string).str_0.offset(pos as isize) as *mut libc::c_void, + (*string).str_0.offset(pos as isize).offset(len as isize) as *const libc::c_void, + (*string).len.wrapping_sub(pos.wrapping_add(len)), + ); + } + (*string).len = ((*string).len as libc::size_t).wrapping_sub(len) as size_t as size_t; + *(*string).str_0.offset((*string).len as isize) = 0i32 as libc::c_char; + return string; +} + +pub unsafe fn mmap_string_set_ceil(mut ceil: size_t) { + mmap_string_ceil = ceil; +} +static mut mmap_string_ceil: size_t = (8i32 * 1024i32 * 1024i32) as size_t; + +pub unsafe fn mmap_string_ref(mut string: *mut MMAPString) -> libc::c_int { + let mut ht: *mut chash = 0 as *mut chash; + let mut r: libc::c_int = 0; + let mut key: chashdatum = chashdatum { + data: 0 as *mut libc::c_void, + len: 0, + }; + let mut data: chashdatum = chashdatum { + data: 0 as *mut libc::c_void, + len: 0, + }; + mmapstring_lock.lock().unwrap(); + if mmapstring_hashtable.is_null() { + mmapstring_hashtable_init(); + } + ht = mmapstring_hashtable; + if ht.is_null() { + return -1i32; + } + key.data = &mut (*string).str_0 as *mut *mut libc::c_char as *mut libc::c_void; + key.len = ::std::mem::size_of::<*mut libc::c_char>() as libc::size_t as libc::c_uint; + data.data = string as *mut libc::c_void; + data.len = 0i32 as libc::c_uint; + r = chash_set( + mmapstring_hashtable, + &mut key, + &mut data, + 0 as *mut chashdatum, + ); + + if r < 0i32 { + return r; + } + return 0i32; +} + +static mut mmapstring_hashtable: *mut chash = 0 as *const chash as *mut chash; +unsafe fn mmapstring_hashtable_init() { + mmapstring_hashtable = chash_new(13i32 as libc::c_uint, 1i32); +} + +pub unsafe fn mmap_string_unref(mut str: *mut libc::c_char) -> libc::c_int { + let mut string: *mut MMAPString = 0 as *mut MMAPString; + let mut ht: *mut chash = 0 as *mut chash; + let mut key: chashdatum = chashdatum { + data: 0 as *mut libc::c_void, + len: 0, + }; + let mut data: chashdatum = chashdatum { + data: 0 as *mut libc::c_void, + len: 0, + }; + let mut r: libc::c_int = 0; + if str.is_null() { + return -1i32; + } + mmapstring_lock.lock().unwrap(); + ht = mmapstring_hashtable; + if ht.is_null() { + return -1i32; + } + key.data = &mut str as *mut *mut libc::c_char as *mut libc::c_void; + key.len = ::std::mem::size_of::<*mut libc::c_char>() as libc::size_t as libc::c_uint; + r = chash_get(ht, &mut key, &mut data); + if r < 0i32 { + string = 0 as *mut MMAPString + } else { + string = data.data as *mut MMAPString + } + if !string.is_null() { + chash_delete(ht, &mut key, 0 as *mut chashdatum); + if chash_count(ht) == 0i32 as libc::c_uint { + chash_free(ht); + mmapstring_hashtable = 0 as *mut chash + } + } + if !string.is_null() { + mmap_string_free(string); + return 0i32; + } else { + return -1i32; + }; +} +#[inline] +unsafe fn chash_count(mut hash: *mut chash) -> libc::c_uint { + return (*hash).count; +} + +pub unsafe fn mmapstring_init_lock() {} +pub unsafe fn mmapstring_uninit_lock() {} diff --git a/mmime/src/other.rs b/mmime/src/other.rs new file mode 100644 index 000000000..02d206bdb --- /dev/null +++ b/mmime/src/other.rs @@ -0,0 +1,1707 @@ +use chrono::{Datelike, Local, TimeZone, Timelike}; + +use crate::clist::*; +use crate::mailimf_types::*; +use crate::mailimf_types_helper::*; +use crate::mailmime_types::*; +use crate::mailmime_types_helper::*; + +pub(crate) use libc::{ + calloc, close, free, isalpha, isdigit, malloc, memcmp, memcpy, memmove, memset, realloc, + strcpy, strlen, strncmp, strncpy, +}; + +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 + } +} + +pub(crate) unsafe fn strdup(s: *const libc::c_char) -> *mut libc::c_char { + let slen = libc::strlen(s); + let result = libc::malloc(slen + 1); + if result.is_null() { + return std::ptr::null_mut(); + } + + libc::memcpy(result, s as *const _, slen + 1); + result as *mut _ +} + +pub(crate) type size_t = libc::size_t; +pub(crate) type uint32_t = libc::c_uint; + +pub const MAIL_ERROR_SSL: libc::c_uint = 58; +pub const MAIL_ERROR_FOLDER: libc::c_uint = 57; +pub const MAIL_ERROR_UNABLE: libc::c_uint = 56; +pub const MAIL_ERROR_SYSTEM: libc::c_uint = 55; +pub const MAIL_ERROR_COMMAND: libc::c_uint = 54; +pub const MAIL_ERROR_SEND: libc::c_uint = 53; +pub const MAIL_ERROR_CHAR_ENCODING_FAILED: libc::c_uint = 52; +pub const MAIL_ERROR_SUBJECT_NOT_FOUND: libc::c_uint = 51; +/* 50 */ +pub const MAIL_ERROR_PROGRAM_ERROR: libc::c_uint = 50; +pub const MAIL_ERROR_NO_PERMISSION: libc::c_uint = 49; +pub const MAIL_ERROR_COMMAND_NOT_SUPPORTED: libc::c_uint = 48; +pub const MAIL_ERROR_NO_APOP: libc::c_uint = 47; +pub const MAIL_ERROR_READONLY: libc::c_uint = 46; +pub const MAIL_ERROR_FATAL: libc::c_uint = 45; +pub const MAIL_ERROR_CLOSE: libc::c_uint = 44; +pub const MAIL_ERROR_CAPABILITY: libc::c_uint = 43; +pub const MAIL_ERROR_PROTOCOL: libc::c_uint = 42; +/* misc errors */ +pub const MAIL_ERROR_MISC: libc::c_uint = 41; +/* 40 */ +pub const MAIL_ERROR_EXPUNGE: libc::c_uint = 40; +pub const MAIL_ERROR_NO_TLS: libc::c_uint = 39; +pub const MAIL_ERROR_CACHE_MISS: libc::c_uint = 38; +pub const MAIL_ERROR_STARTTLS: libc::c_uint = 37; +pub const MAIL_ERROR_MOVE: libc::c_uint = 36; +pub const MAIL_ERROR_FOLDER_NOT_FOUND: libc::c_uint = 35; +pub const MAIL_ERROR_REMOVE: libc::c_uint = 34; +pub const MAIL_ERROR_PART_NOT_FOUND: libc::c_uint = 33; +pub const MAIL_ERROR_INVAL: libc::c_uint = 32; +pub const MAIL_ERROR_PARSE: libc::c_uint = 31; +/* 30 */ +pub const MAIL_ERROR_MSG_NOT_FOUND: libc::c_uint = 30; +pub const MAIL_ERROR_DISKSPACE: libc::c_uint = 29; +pub const MAIL_ERROR_SEARCH: libc::c_uint = 28; +pub const MAIL_ERROR_STORE: libc::c_uint = 27; +pub const MAIL_ERROR_FETCH: libc::c_uint = 26; +pub const MAIL_ERROR_COPY: libc::c_uint = 25; +pub const MAIL_ERROR_APPEND: libc::c_uint = 24; +pub const MAIL_ERROR_LSUB: libc::c_uint = 23; +pub const MAIL_ERROR_LIST: libc::c_uint = 22; +pub const MAIL_ERROR_UNSUBSCRIBE: libc::c_uint = 21; +/* 20 */ +pub const MAIL_ERROR_SUBSCRIBE: libc::c_uint = 20; +pub const MAIL_ERROR_STATUS: libc::c_uint = 19; +pub const MAIL_ERROR_MEMORY: libc::c_uint = 18; +pub const MAIL_ERROR_SELECT: libc::c_uint = 17; +pub const MAIL_ERROR_EXAMINE: libc::c_uint = 16; +pub const MAIL_ERROR_CHECK: libc::c_uint = 15; +pub const MAIL_ERROR_RENAME: libc::c_uint = 14; +pub const MAIL_ERROR_NOOP: libc::c_uint = 13; +pub const MAIL_ERROR_LOGOUT: libc::c_uint = 12; +pub const MAIL_ERROR_DELETE: libc::c_uint = 11; +/* 10 */ +pub const MAIL_ERROR_CREATE: libc::c_uint = 10; +pub const MAIL_ERROR_LOGIN: libc::c_uint = 9; +pub const MAIL_ERROR_STREAM: libc::c_uint = 8; +pub const MAIL_ERROR_FILE: libc::c_uint = 7; +pub const MAIL_ERROR_BAD_STATE: libc::c_uint = 6; +pub const MAIL_ERROR_CONNECT: libc::c_uint = 5; +pub const MAIL_ERROR_UNKNOWN: libc::c_uint = 4; +pub const MAIL_ERROR_NOT_IMPLEMENTED: libc::c_uint = 3; +pub const MAIL_NO_ERROR_NON_AUTHENTICATED: libc::c_uint = 2; +pub const MAIL_NO_ERROR_AUTHENTICATED: libc::c_uint = 1; +pub const MAIL_NO_ERROR: libc::c_uint = 0; + +pub const MAILIMF_ERROR_FILE: libc::c_uint = 4; +pub const MAILIMF_ERROR_INVAL: libc::c_uint = 3; +pub const MAILIMF_ERROR_MEMORY: libc::c_uint = 2; +pub const MAILIMF_ERROR_PARSE: libc::c_uint = 1; +pub const MAILIMF_NO_ERROR: libc::c_uint = 0; + +pub unsafe fn mailprivacy_prepare_mime(mut mime: *mut mailmime) { + let mut cur: *mut clistiter = 0 as *mut clistiter; + match (*mime).mm_type { + 1 => { + if !(*mime).mm_data.mm_single.is_null() { + prepare_mime_single(mime); + } + } + 2 => { + cur = (*(*mime).mm_data.mm_multipart.mm_mp_list).first; + while !cur.is_null() { + let mut child: *mut mailmime = 0 as *mut mailmime; + child = (if !cur.is_null() { + (*cur).data + } else { + 0 as *mut libc::c_void + }) as *mut mailmime; + mailprivacy_prepare_mime(child); + cur = if !cur.is_null() { + (*cur).next + } else { + 0 as *mut clistcell + } + } + } + 3 => { + if !(*mime).mm_data.mm_message.mm_msg_mime.is_null() { + mailprivacy_prepare_mime((*mime).mm_data.mm_message.mm_msg_mime); + } + } + _ => {} + }; +} + +unsafe fn prepare_mime_single(mut mime: *mut mailmime) { + let mut single_fields: mailmime_single_fields = mailmime_single_fields { + fld_content: 0 as *mut mailmime_content, + fld_content_charset: 0 as *mut libc::c_char, + fld_content_boundary: 0 as *mut libc::c_char, + fld_content_name: 0 as *mut libc::c_char, + fld_encoding: 0 as *mut mailmime_mechanism, + fld_id: 0 as *mut libc::c_char, + fld_description: 0 as *mut libc::c_char, + fld_version: 0, + fld_disposition: 0 as *mut mailmime_disposition, + fld_disposition_filename: 0 as *mut libc::c_char, + fld_disposition_creation_date: 0 as *mut libc::c_char, + fld_disposition_modification_date: 0 as *mut libc::c_char, + fld_disposition_read_date: 0 as *mut libc::c_char, + fld_disposition_size: 0, + fld_language: 0 as *mut mailmime_language, + fld_location: 0 as *mut libc::c_char, + }; + let mut encoding: libc::c_int = 0; + let mut r: libc::c_int = 0; + if !(*mime).mm_mime_fields.is_null() { + mailmime_single_fields_init( + &mut single_fields, + (*mime).mm_mime_fields, + (*mime).mm_content_type, + ); + if !single_fields.fld_encoding.is_null() { + encoding = (*single_fields.fld_encoding).enc_type; + match encoding { + 2 | 1 | 3 => { + (*single_fields.fld_encoding).enc_type = + MAILMIME_MECHANISM_QUOTED_PRINTABLE as libc::c_int + } + _ => {} + } + } else { + let mut mechanism: *mut mailmime_mechanism = 0 as *mut mailmime_mechanism; + let mut field: *mut mailmime_field = 0 as *mut mailmime_field; + mechanism = mailmime_mechanism_new( + MAILMIME_MECHANISM_QUOTED_PRINTABLE as libc::c_int, + 0 as *mut libc::c_char, + ); + if mechanism.is_null() { + return; + } + field = mailmime_field_new( + MAILMIME_FIELD_TRANSFER_ENCODING as libc::c_int, + 0 as *mut mailmime_content, + mechanism, + 0 as *mut libc::c_char, + 0 as *mut libc::c_char, + 0i32 as uint32_t, + 0 as *mut mailmime_disposition, + 0 as *mut mailmime_language, + 0 as *mut libc::c_char, + ); + if field.is_null() { + mailmime_mechanism_free(mechanism); + return; + } + r = clist_insert_after( + (*(*mime).mm_mime_fields).fld_list, + (*(*(*mime).mm_mime_fields).fld_list).last, + field as *mut libc::c_void, + ); + if r < 0i32 { + mailmime_field_free(field); + return; + } + } + } + if (*mime).mm_type == MAILMIME_SINGLE as libc::c_int { + match (*(*mime).mm_data.mm_single).dt_encoding { + 2 | 1 | 3 => { + (*(*mime).mm_data.mm_single).dt_encoding = + MAILMIME_MECHANISM_QUOTED_PRINTABLE as libc::c_int; + (*(*mime).mm_data.mm_single).dt_encoded = 0i32 + } + _ => {} + } + }; +} + +pub unsafe fn mailmime_substitute( + mut old_mime: *mut mailmime, + mut new_mime: *mut mailmime, +) -> libc::c_int { + let mut parent: *mut mailmime = 0 as *mut mailmime; + parent = (*old_mime).mm_parent; + if parent.is_null() { + return MAIL_ERROR_INVAL as libc::c_int; + } + if (*old_mime).mm_parent_type == MAILMIME_MESSAGE as libc::c_int { + (*parent).mm_data.mm_message.mm_msg_mime = new_mime + } else { + (*(*old_mime).mm_multipart_pos).data = new_mime as *mut libc::c_void + } + (*new_mime).mm_parent = parent; + (*new_mime).mm_parent_type = (*old_mime).mm_parent_type; + (*old_mime).mm_parent = 0 as *mut mailmime; + (*old_mime).mm_parent_type = MAILMIME_NONE as libc::c_int; + return MAIL_NO_ERROR as libc::c_int; +} + +/* + mailimf_address_list_new_empty creates an empty list of addresses +*/ +pub unsafe fn mailimf_address_list_new_empty() -> *mut mailimf_address_list { + let mut list: *mut clist = 0 as *mut clist; + let mut addr_list: *mut mailimf_address_list = 0 as *mut mailimf_address_list; + list = clist_new(); + if list.is_null() { + return 0 as *mut mailimf_address_list; + } + addr_list = mailimf_address_list_new(list); + if addr_list.is_null() { + return 0 as *mut mailimf_address_list; + } + return addr_list; +} + +/* + mailimf_mailbox_list_new_empty creates an empty list of mailboxes +*/ +pub unsafe fn mailimf_mailbox_list_new_empty() -> *mut mailimf_mailbox_list { + let mut list: *mut clist = 0 as *mut clist; + let mut mb_list: *mut mailimf_mailbox_list = 0 as *mut mailimf_mailbox_list; + list = clist_new(); + if list.is_null() { + return 0 as *mut mailimf_mailbox_list; + } + mb_list = mailimf_mailbox_list_new(list); + if mb_list.is_null() { + return 0 as *mut mailimf_mailbox_list; + } + return mb_list; +} + +/* + mailimf_mailbox_list_add adds a mailbox to the list of mailboxes + + @return MAILIMF_NO_ERROR will be returned on success, + other code will be returned otherwise +*/ +pub unsafe fn mailimf_mailbox_list_add( + mut mailbox_list: *mut mailimf_mailbox_list, + mut mb: *mut mailimf_mailbox, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = clist_insert_after( + (*mailbox_list).mb_list, + (*(*mailbox_list).mb_list).last, + mb as *mut libc::c_void, + ); + if r < 0i32 { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +/* + mailimf_address_list_add adds a mailbox to the list of addresses + + @return MAILIMF_NO_ERROR will be returned on success, + other code will be returned otherwise +*/ +pub unsafe fn mailimf_address_list_add( + mut address_list: *mut mailimf_address_list, + mut addr: *mut mailimf_address, +) -> libc::c_int { + let mut r: libc::c_int = 0; + r = clist_insert_after( + (*address_list).ad_list, + (*(*address_list).ad_list).last, + addr as *mut libc::c_void, + ); + if r < 0i32 { + return MAILIMF_ERROR_MEMORY as libc::c_int; + } + return MAILIMF_NO_ERROR as libc::c_int; +} + +/* + mailimf_fields_new_with_data_all creates a new mailimf_fields + structure with a set of fields + + if you don't want a given field in the set to be added in the list + of fields, you can give NULL as argument + + @param message_id sould be allocated with malloc() + @param subject should be allocated with malloc() + @param in_reply_to each elements of this list should be allocated + with malloc() + @param references each elements of this list should be allocated + with malloc() + + @return MAILIMF_NO_ERROR will be returned on success, + other code will be returned otherwise +*/ +pub unsafe fn mailimf_fields_new_with_data_all( + mut date: *mut mailimf_date_time, + mut from: *mut mailimf_mailbox_list, + mut sender: *mut mailimf_mailbox, + mut reply_to: *mut mailimf_address_list, + mut to: *mut mailimf_address_list, + mut cc: *mut mailimf_address_list, + mut bcc: *mut mailimf_address_list, + mut message_id: *mut libc::c_char, + mut in_reply_to: *mut clist, + mut references: *mut clist, + mut subject: *mut libc::c_char, +) -> *mut mailimf_fields { + let mut fields: *mut mailimf_fields = 0 as *mut mailimf_fields; + let mut r: libc::c_int = 0; + fields = mailimf_fields_new_empty(); + if !fields.is_null() { + r = mailimf_fields_add_data( + fields, + date, + from, + sender, + reply_to, + to, + cc, + bcc, + message_id, + in_reply_to, + references, + subject, + ); + if r != MAILIMF_NO_ERROR as libc::c_int { + mailimf_fields_free(fields); + } else { + return fields; + } + } + return 0 as *mut mailimf_fields; +} + +/* + mailimf_fields_add_data adds a set of fields in the + given mailimf_fields structure. + + if you don't want a given field in the set to be added in the list + of fields, you can give NULL as argument + + @param msg_id sould be allocated with malloc() + @param subject should be allocated with malloc() + @param in_reply_to each elements of this list should be allocated + with malloc() + @param references each elements of this list should be allocated + with malloc() + + @return MAILIMF_NO_ERROR will be returned on success, + other code will be returned otherwise +*/ +pub unsafe fn mailimf_fields_add_data( + mut fields: *mut mailimf_fields, + mut date: *mut mailimf_date_time, + mut from: *mut mailimf_mailbox_list, + mut sender: *mut mailimf_mailbox, + mut reply_to: *mut mailimf_address_list, + mut to: *mut mailimf_address_list, + mut cc: *mut mailimf_address_list, + mut bcc: *mut mailimf_address_list, + mut msg_id: *mut libc::c_char, + mut in_reply_to: *mut clist, + mut references: *mut clist, + mut subject: *mut libc::c_char, +) -> libc::c_int { + let mut current_block: u64; + let mut imf_date: *mut mailimf_orig_date = 0 as *mut mailimf_orig_date; + let mut imf_from: *mut mailimf_from = 0 as *mut mailimf_from; + let mut imf_sender: *mut mailimf_sender = 0 as *mut mailimf_sender; + let mut imf_reply_to: *mut mailimf_reply_to = 0 as *mut mailimf_reply_to; + let mut imf_to: *mut mailimf_to = 0 as *mut mailimf_to; + let mut imf_cc: *mut mailimf_cc = 0 as *mut mailimf_cc; + let mut imf_bcc: *mut mailimf_bcc = 0 as *mut mailimf_bcc; + let mut imf_msg_id: *mut mailimf_message_id = 0 as *mut mailimf_message_id; + let mut imf_references: *mut mailimf_references = 0 as *mut mailimf_references; + let mut imf_in_reply_to: *mut mailimf_in_reply_to = 0 as *mut mailimf_in_reply_to; + let mut imf_subject: *mut mailimf_subject = 0 as *mut mailimf_subject; + let mut field: *mut mailimf_field = 0 as *mut mailimf_field; + let mut r: libc::c_int = 0; + imf_date = 0 as *mut mailimf_orig_date; + imf_from = 0 as *mut mailimf_from; + imf_sender = 0 as *mut mailimf_sender; + imf_reply_to = 0 as *mut mailimf_reply_to; + imf_to = 0 as *mut mailimf_to; + imf_cc = 0 as *mut mailimf_cc; + imf_bcc = 0 as *mut mailimf_bcc; + imf_msg_id = 0 as *mut mailimf_message_id; + imf_references = 0 as *mut mailimf_references; + imf_in_reply_to = 0 as *mut mailimf_in_reply_to; + imf_subject = 0 as *mut mailimf_subject; + field = 0 as *mut mailimf_field; + if !date.is_null() { + imf_date = mailimf_orig_date_new(date); + if imf_date.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_ORIG_DATE as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + imf_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 2719512138335094285; + } + } + } + } else { + current_block = 2719512138335094285; + } + match current_block { + 2719512138335094285 => { + if !from.is_null() { + imf_from = mailimf_from_new(from); + if imf_from.is_null() { + current_block = 13813460800808168376; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_FROM as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + imf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 3275366147856559585; + } + } + } + } else { + current_block = 3275366147856559585; + } + match current_block { + 13813460800808168376 => {} + 16539016819803454162 => {} + _ => { + if !sender.is_null() { + imf_sender = mailimf_sender_new(sender); + if imf_sender.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_SENDER as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + imf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 15090052786889560393; + } + } + } + } else { + current_block = 15090052786889560393; + } + match current_block { + 16539016819803454162 => {} + 13813460800808168376 => {} + _ => { + if !reply_to.is_null() { + imf_reply_to = mailimf_reply_to_new(reply_to); + if imf_reply_to.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_REPLY_TO as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + imf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 10150597327160359210; + } + } + } + } else { + current_block = 10150597327160359210; + } + match current_block { + 16539016819803454162 => {} + 13813460800808168376 => {} + _ => { + if !to.is_null() { + imf_to = mailimf_to_new(to); + if imf_to.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_TO as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + imf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 17233182392562552756; + } + } + } + } else { + current_block = 17233182392562552756; + } + match current_block { + 16539016819803454162 => {} + 13813460800808168376 => {} + _ => { + if !cc.is_null() { + imf_cc = mailimf_cc_new(cc); + if imf_cc.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_CC as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + imf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = 16539016819803454162; + } else { + r = mailimf_fields_add(fields, field); + if r != MAILIMF_NO_ERROR as libc::c_int { + current_block = 13813460800808168376; + } else { + current_block = 12930649117290160518; + } + } + } + } else { + current_block = 12930649117290160518; + } + match current_block { + 16539016819803454162 => {} + 13813460800808168376 => {} + _ => { + if !bcc.is_null() { + imf_bcc = mailimf_bcc_new(bcc); + if imf_bcc.is_null() { + current_block = 16539016819803454162; + } else { + field = mailimf_field_new( + MAILIMF_FIELD_BCC as libc::c_int, + 0 as *mut mailimf_return, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + 0 as *mut mailimf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_orig_date, + 0 as *mut mailimf_from, + 0 as *mut mailimf_sender, + 0 as *mut mailimf_reply_to, + 0 as *mut mailimf_to, + 0 as *mut mailimf_cc, + imf_bcc, + 0 as *mut mailimf_message_id, + 0 as *mut mailimf_in_reply_to, + 0 as *mut mailimf_references, + 0 as *mut mailimf_subject, + 0 as *mut mailimf_comments, + 0 as *mut mailimf_keywords, + 0 as *mut mailimf_optional_field, + ); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = + 16539016819803454162; + } else { + r = mailimf_fields_add( + fields, field, + ); + if r != MAILIMF_NO_ERROR + as libc::c_int + { + current_block = + 13813460800808168376; + } else { + current_block = + 7858101417678297991; + } + } + } + } else { + current_block = 7858101417678297991; + } + match current_block { + 16539016819803454162 => {} + 13813460800808168376 => {} + _ => { + if !msg_id.is_null() { + imf_msg_id = + mailimf_message_id_new(msg_id); + if imf_msg_id.is_null() { + current_block = + 16539016819803454162; + } else { + field = + mailimf_field_new(MAILIMF_FIELD_MESSAGE_ID + as + libc::c_int, + 0 + as + *mut mailimf_return, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_reply_to, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + imf_msg_id, + 0 + as + *mut mailimf_in_reply_to, + 0 + as + *mut mailimf_references, + 0 + as + *mut mailimf_subject, + 0 + as + *mut mailimf_comments, + 0 + as + *mut mailimf_keywords, + 0 + as + *mut mailimf_optional_field); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block = + 16539016819803454162; + } else { + r = mailimf_fields_add( + fields, field, + ); + if r != MAILIMF_NO_ERROR + as libc::c_int + { + current_block + = + 13813460800808168376; + } else { + current_block + = + 15514718523126015390; + } + } + } + } else { + current_block = + 15514718523126015390; + } + match current_block { + 13813460800808168376 => {} + 16539016819803454162 => {} + _ => { + if !in_reply_to.is_null() { + imf_in_reply_to = + mailimf_in_reply_to_new( + in_reply_to, + ); + if imf_in_reply_to.is_null() + { + current_block + = + 16539016819803454162; + } else { + field + = + mailimf_field_new(MAILIMF_FIELD_IN_REPLY_TO + as + libc::c_int, + 0 + as + *mut mailimf_return, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_reply_to, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + imf_in_reply_to, + 0 + as + *mut mailimf_references, + 0 + as + *mut mailimf_subject, + 0 + as + *mut mailimf_comments, + 0 + as + *mut mailimf_keywords, + 0 + as + *mut mailimf_optional_field); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() { + current_block + = + 16539016819803454162; + } else { + r + = + mailimf_fields_add(fields, + field); + if r + != + MAILIMF_NO_ERROR + as + libc::c_int + { + current_block + = + 13813460800808168376; + } else { + current_block + = + 15587532755333643506; + } + } + } + } else { + current_block = + 15587532755333643506; + } + match current_block { + 13813460800808168376 => {} + 16539016819803454162 => {} + _ => { + if !references.is_null() + { + imf_references + = + mailimf_references_new(references); + if imf_references + .is_null() + { + current_block + = + 16539016819803454162; + } else { + field + = + mailimf_field_new(MAILIMF_FIELD_REFERENCES + as + libc::c_int, + 0 + as + *mut mailimf_return, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_reply_to, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_in_reply_to, + imf_references, + 0 + as + *mut mailimf_subject, + 0 + as + *mut mailimf_comments, + 0 + as + *mut mailimf_keywords, + 0 + as + *mut mailimf_optional_field); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field + .is_null() + { + current_block + = + 16539016819803454162; + } else { + r + = + mailimf_fields_add(fields, + field); + if r + != + MAILIMF_NO_ERROR + as + libc::c_int + { + current_block + = + 13813460800808168376; + } else { + current_block + = + 7301440000599063274; + } + } + } + } else { + current_block + = + 7301440000599063274; + } + match current_block + { + 13813460800808168376 + => + { + } + 16539016819803454162 + => + { + } + _ + => + { + if !subject.is_null() + { + imf_subject + = + mailimf_subject_new(subject); + if imf_subject.is_null() + { + current_block + = + 16539016819803454162; + } else { + field + = + mailimf_field_new(MAILIMF_FIELD_SUBJECT + as + libc::c_int, + 0 + as + *mut mailimf_return, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_orig_date, + 0 + as + *mut mailimf_from, + 0 + as + *mut mailimf_sender, + 0 + as + *mut mailimf_reply_to, + 0 + as + *mut mailimf_to, + 0 + as + *mut mailimf_cc, + 0 + as + *mut mailimf_bcc, + 0 + as + *mut mailimf_message_id, + 0 + as + *mut mailimf_in_reply_to, + 0 + as + *mut mailimf_references, + imf_subject, + 0 + as + *mut mailimf_comments, + 0 + as + *mut mailimf_keywords, + 0 + as + *mut mailimf_optional_field); + /* return-path */ + /* resent date */ + /* resent from */ + /* resent sender */ + /* resent to */ + /* resent cc */ + /* resent bcc */ + /* resent msg id */ + /* date */ + /* from */ + /* sender */ + /* reply-to */ + /* to */ + /* cc */ + /* bcc */ + /* message id */ + /* in reply to */ + /* references */ + /* subject */ + /* comments */ + /* keywords */ + /* optional field */ + if field.is_null() + { + current_block + = + 16539016819803454162; + } else { + r + = + mailimf_fields_add(fields, + field); + if r + != + MAILIMF_NO_ERROR + as + libc::c_int + { + current_block + = + 13813460800808168376; + } else { + current_block + = + 10153752038087260855; + } + } + } + } else { + current_block + = + 10153752038087260855; + } + match current_block + { + 13813460800808168376 + => + { + } + 16539016819803454162 + => + { + } + _ + => + { + return MAILIMF_NO_ERROR + as + libc::c_int + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + _ => {} + } + match current_block { + 13813460800808168376 => { + if !field.is_null() { + detach_field(field); + mailimf_field_free(field); + } + } + _ => {} + } + detach_free_fields( + imf_date, + imf_from, + imf_sender, + imf_reply_to, + imf_to, + imf_cc, + imf_bcc, + imf_msg_id, + imf_in_reply_to, + imf_references, + imf_subject, + ); + return MAILIMF_ERROR_MEMORY as libc::c_int; +} + +unsafe fn detach_free_fields( + mut date: *mut mailimf_orig_date, + mut from: *mut mailimf_from, + mut sender: *mut mailimf_sender, + mut reply_to: *mut mailimf_reply_to, + mut to: *mut mailimf_to, + mut cc: *mut mailimf_cc, + mut bcc: *mut mailimf_bcc, + mut msg_id: *mut mailimf_message_id, + mut in_reply_to: *mut mailimf_in_reply_to, + mut references: *mut mailimf_references, + mut subject: *mut mailimf_subject, +) { + detach_free_common_fields(date, from, sender, to, cc, bcc, msg_id); + if !reply_to.is_null() { + (*reply_to).rt_addr_list = 0 as *mut mailimf_address_list; + mailimf_reply_to_free(reply_to); + } + if !in_reply_to.is_null() { + (*in_reply_to).mid_list = 0 as *mut clist; + mailimf_in_reply_to_free(in_reply_to); + } + if !references.is_null() { + (*references).mid_list = 0 as *mut clist; + mailimf_references_free(references); + } + if !subject.is_null() { + (*subject).sbj_value = 0 as *mut libc::c_char; + mailimf_subject_free(subject); + }; +} + +unsafe fn detach_field(mut field: *mut mailimf_field) { + (*field).fld_type = MAILIMF_FIELD_NONE as libc::c_int; + mailimf_field_free(field); +} + +unsafe fn detach_free_common_fields( + mut imf_date: *mut mailimf_orig_date, + mut imf_from: *mut mailimf_from, + mut imf_sender: *mut mailimf_sender, + mut imf_to: *mut mailimf_to, + mut imf_cc: *mut mailimf_cc, + mut imf_bcc: *mut mailimf_bcc, + mut imf_msg_id: *mut mailimf_message_id, +) { + if !imf_date.is_null() { + (*imf_date).dt_date_time = 0 as *mut mailimf_date_time; + mailimf_orig_date_free(imf_date); + } + if !imf_from.is_null() { + (*imf_from).frm_mb_list = 0 as *mut mailimf_mailbox_list; + mailimf_from_free(imf_from); + } + if !imf_sender.is_null() { + (*imf_sender).snd_mb = 0 as *mut mailimf_mailbox; + mailimf_sender_free(imf_sender); + } + if !imf_to.is_null() { + (*imf_to).to_addr_list = 0 as *mut mailimf_address_list; + mailimf_to_free(imf_to); + } + if !imf_cc.is_null() { + (*imf_cc).cc_addr_list = 0 as *mut mailimf_address_list; + mailimf_to_free(imf_to); + } + if !imf_bcc.is_null() { + (*imf_bcc).bcc_addr_list = 0 as *mut mailimf_address_list; + mailimf_bcc_free(imf_bcc); + } + if !imf_msg_id.is_null() { + (*imf_msg_id).mid_value = 0 as *mut libc::c_char; + mailimf_message_id_free(imf_msg_id); + }; +} + +pub fn mailimf_get_date(t: i64) -> *mut mailimf_date_time { + let lt = Local.timestamp(t, 0); + + let off = (lt.offset().local_minus_utc() / (60 * 60)) * 100; + + unsafe { + mailimf_date_time_new( + lt.day() as libc::c_int, + lt.month() as libc::c_int, + lt.year() as libc::c_int, + lt.hour() as libc::c_int, + lt.minute() as libc::c_int, + lt.second() as libc::c_int, + off, + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::Utc; + use std::ffi::CString; + + #[test] + fn test_strcasecmp() { + assert_eq!(0, unsafe { + strcasecmp( + CString::new("hello").unwrap().as_ptr(), + CString::new("Hello").unwrap().as_ptr(), + ) + }); + } + + #[test] + fn test_strncasecmp() { + assert_eq!(0, unsafe { + strncasecmp( + CString::new("helloworld").unwrap().as_ptr(), + CString::new("Helloward").unwrap().as_ptr(), + 4, + ) + }); + } + + #[test] + fn test_get_date() { + let now_utc = Utc::now(); + + let now_local = Local.from_utc_datetime(&now_utc.naive_local()); + let t_local = now_local.timestamp(); + + let converted = unsafe { *mailimf_get_date(t_local as i64) }; + + assert_eq!(converted.dt_day as u32, now_local.day()); + assert_eq!(converted.dt_month as u32, now_local.month()); + assert_eq!(converted.dt_year, now_local.year()); + assert_eq!(converted.dt_hour as u32, now_local.hour()); + assert_eq!(converted.dt_min as u32, now_local.minute()); + assert_eq!(converted.dt_sec as u32, now_local.second()); + assert_eq!( + converted.dt_zone, + (now_local.offset().local_minus_utc() / (60 * 60)) * 100 + ); + } +}