Skip to main content

deltachat/
tools.rs

1//! Some tools and enhancements to the used libraries, there should be
2//! no references to Context and other "larger" entities here.
3
4#![allow(missing_docs)]
5
6use std::borrow::Cow;
7use std::io::{Cursor, Write};
8use std::mem;
9use std::ops::{AddAssign, Deref};
10use std::path::{Path, PathBuf};
11use std::str::from_utf8;
12// If a time value doesn't need to be sent to another host, saved to the db or otherwise used across
13// program restarts, a monotonically nondecreasing clock (`Instant`) should be used. But as
14// `Instant` may use `libc::clock_gettime(CLOCK_MONOTONIC)`, e.g. on Android, and does not advance
15// while being in deep sleep mode, we use `SystemTime` instead, but add an alias for it to document
16// why `Instant` isn't used in those places. Also this can help to switch to another clock impl if
17// we find any. Another reason is that `Instant` may reintroduce panics in the future versions:
18// https://doc.rust-lang.org/1.87.0/std/time/struct.Instant.html#method.elapsed.
19use std::time::Duration;
20pub use std::time::SystemTime as Time;
21#[cfg(not(test))]
22pub use std::time::SystemTime;
23
24use anyhow::{Context as _, Result, bail, ensure};
25use base64::Engine as _;
26use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
27use deltachat_contact_tools::EmailAddress;
28#[cfg(test)]
29pub use deltachat_time::SystemTimeTools as SystemTime;
30use futures::TryStreamExt;
31use mailparse::MailHeaderMap;
32use mailparse::dateparse;
33use mailparse::headers::Headers;
34use num_traits::PrimInt;
35use tokio::{fs, io};
36use url::Url;
37use uuid::Uuid;
38
39use crate::chat::{add_device_msg, add_device_msg_with_importance};
40use crate::config::Config;
41use crate::constants::{self, DC_ELLIPSIS, DC_OUTDATED_WARNING_DAYS};
42use crate::context::Context;
43use crate::events::EventType;
44use crate::log::warn;
45use crate::message::{Message, Viewtype};
46use crate::stock_str;
47
48/// Shortens a string to a specified length and adds "[...]" to the
49/// end of the shortened string.
50#[expect(clippy::arithmetic_side_effects)]
51pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<'_, str> {
52    let count = buf.chars().count();
53    if count <= approx_chars + DC_ELLIPSIS.len() {
54        return Cow::Borrowed(buf);
55    }
56    let end_pos = buf
57        .char_indices()
58        .nth(approx_chars)
59        .map(|(n, _)| n)
60        .unwrap_or_default();
61
62    if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) {
63        Cow::Owned(format!(
64            "{}{}",
65            buf.get(..=index).unwrap_or_default(),
66            DC_ELLIPSIS
67        ))
68    } else {
69        Cow::Owned(format!(
70            "{}{}",
71            buf.get(..end_pos).unwrap_or_default(),
72            DC_ELLIPSIS
73        ))
74    }
75}
76
77/// Shortens a string to a specified line count and adds "[...]" to the
78/// end of the shortened string.
79///
80/// returns tuple with the String and a boolean whether is was truncated
81#[expect(clippy::arithmetic_side_effects)]
82pub(crate) fn truncate_by_lines(
83    buf: String,
84    max_lines: usize,
85    max_line_len: usize,
86) -> (String, bool) {
87    let mut lines = 0;
88    let mut line_chars = 0;
89    let mut break_point: Option<usize> = None;
90
91    for (index, char) in buf.char_indices() {
92        if char == '\n' {
93            line_chars = 0;
94            lines += 1;
95        } else {
96            line_chars += 1;
97            if line_chars > max_line_len {
98                line_chars = 1;
99                lines += 1;
100            }
101        }
102        if lines == max_lines {
103            break_point = Some(index);
104            break;
105        }
106    }
107
108    if let Some(end_pos) = break_point {
109        // Text has too many lines and needs to be truncated.
110        let text = {
111            if let Some(buffer) = buf.get(..end_pos) {
112                if let Some(index) = buffer.rfind([' ', '\n']) {
113                    buf.get(..=index)
114                } else {
115                    buf.get(..end_pos)
116                }
117            } else {
118                None
119            }
120        };
121
122        if let Some(truncated_text) = text {
123            (format!("{truncated_text}{DC_ELLIPSIS}"), true)
124        } else {
125            // In case of indexing/slicing error, we return an error
126            // message as a preview and add HTML version. This should
127            // never happen.
128            let error_text = "[Truncation of the message failed, this is a bug in the Delta Chat core. Please report it.\nYou can still open the full text to view the original message.]";
129            (error_text.to_string(), true)
130        }
131    } else {
132        // text is unchanged
133        (buf, false)
134    }
135}
136
137/// Shortens a message text if necessary according to the configuration. Adds "[...]" to the end of
138/// the shortened text.
139///
140/// Returns the resulting text and a bool telling whether a truncation was done.
141pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
142    if context.get_config_bool(Config::Bot).await? {
143        return Ok((text, false));
144    }
145    // Truncate text if it has too many lines
146    Ok(truncate_by_lines(
147        text,
148        constants::DC_DESIRED_TEXT_LINES,
149        constants::DC_DESIRED_TEXT_LINE_LEN,
150    ))
151}
152
153/* ******************************************************************************
154 * date/time tools
155 ******************************************************************************/
156
157/// Converts Unix time in seconds to a local timestamp string.
158pub fn timestamp_to_str(wanted: i64) -> String {
159    if let Some(ts) = Local.timestamp_opt(wanted, 0).single() {
160        ts.format("%Y.%m.%d %H:%M:%S").to_string()
161    } else {
162        // Out of range number of seconds.
163        "??.??.?? ??:??:??".to_string()
164    }
165}
166
167/// Converts duration to string representation suitable for logs.
168pub fn duration_to_str(duration: Duration) -> String {
169    let secs = duration.as_secs();
170    let h = secs / 3600;
171    let m = (secs % 3600) / 60;
172    let s = (secs % 3600) % 60;
173    format!("{h}h {m}m {s}s")
174}
175
176pub(crate) fn gm2local_offset() -> i64 {
177    /* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime.
178    the function may return negative values. */
179    let lt = Local::now();
180    i64::from(lt.offset().local_minus_utc())
181}
182
183/// Returns the last release timestamp as a unix timestamp compatible for comparison with time() and
184/// database times.
185pub fn get_release_timestamp() -> i64 {
186    NaiveDateTime::new(
187        *crate::release::DATE,
188        NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
189    )
190    .and_utc()
191    .timestamp_millis()
192        / 1_000
193}
194
195// if the system time is not plausible, once a day, add a device message.
196// for testing we're using time() as that is also used for message timestamps.
197// moreover, add a warning if the app is outdated.
198pub(crate) async fn maybe_add_time_based_warnings(context: &Context) {
199    if !maybe_warn_on_bad_time(context, time(), get_release_timestamp()).await {
200        maybe_warn_on_outdated(context, time(), get_release_timestamp()).await;
201    }
202}
203
204async fn maybe_warn_on_bad_time(context: &Context, now: i64, known_past_timestamp: i64) -> bool {
205    if now < known_past_timestamp {
206        let mut msg = Message::new(Viewtype::Text);
207        msg.text = stock_str::bad_time_msg_body(
208            context,
209            &Local.timestamp_opt(now, 0).single().map_or_else(
210                || "YY-MM-DD hh:mm:ss".to_string(),
211                |ts| ts.format("%Y-%m-%d %H:%M:%S").to_string(),
212            ),
213        );
214        if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
215            add_device_msg_with_importance(
216                context,
217                Some(
218                    format!(
219                        "bad-time-warning-{}",
220                        timestamp.format("%Y-%m-%d") // repeat every day
221                    )
222                    .as_str(),
223                ),
224                Some(&mut msg),
225                true,
226            )
227            .await
228            .ok();
229        } else {
230            warn!(context, "Can't convert current timestamp");
231        }
232        return true;
233    }
234    false
235}
236
237#[expect(clippy::arithmetic_side_effects)]
238async fn maybe_warn_on_outdated(context: &Context, now: i64, approx_compile_time: i64) {
239    if now > approx_compile_time + DC_OUTDATED_WARNING_DAYS * 24 * 60 * 60 {
240        let mut msg = Message::new_text(stock_str::update_reminder_msg_body(context));
241        if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
242            add_device_msg(
243                context,
244                Some(
245                    format!(
246                        "outdated-warning-{}",
247                        timestamp.format("%Y-%m") // repeat every month
248                    )
249                    .as_str(),
250                ),
251                Some(&mut msg),
252            )
253            .await
254            .ok();
255        }
256    }
257}
258
259/// Generate an unique ID.
260///
261/// The generated ID should be short but unique:
262/// - short, because it used in Chat-Group-ID headers and in QR codes
263/// - unique as two IDs generated on two devices should not be the same
264///
265/// IDs generated by this function have 144 bits of entropy
266/// and are returned as 24 Base64 characters, each containing 6 bits of entropy.
267/// 144 is chosen because it is sufficiently secure
268/// (larger than AES-128 keys used for message encryption)
269/// and divides both by 8 (byte size) and 6 (number of bits in a single Base64 character).
270pub(crate) fn create_id() -> String {
271    // Generate 144 random bits.
272    let mut arr = [0u8; 18];
273    rand::fill(&mut arr[..]);
274
275    base64::engine::general_purpose::URL_SAFE.encode(arr)
276}
277
278/// Generate a shared secret for a broadcast channel, consisting of 43 characters.
279///
280/// The string generated by this function has 258 bits of entropy
281/// and is returned as 43 Base64 characters, each containing 6 bits of entropy.
282/// 258 is chosen because we may switch to AES-256 keys in the future,
283/// and so that the shared secret definitely won't be the weak spot.
284pub(crate) fn create_broadcast_secret() -> String {
285    // ThreadRng implements CryptoRng trait and is supposed to be cryptographically secure.
286    // Generate 264 random bits.
287    let mut arr = [0u8; 33];
288    rand::fill(&mut arr[..]);
289
290    let mut res = base64::engine::general_purpose::URL_SAFE.encode(arr);
291    res.truncate(43);
292    res
293}
294
295/// Returns true if given string is a valid ID.
296///
297/// All IDs generated with `create_id()` should be considered valid.
298pub(crate) fn validate_id(s: &str) -> bool {
299    let alphabet = base64::alphabet::URL_SAFE.as_str();
300    s.chars().all(|c| alphabet.contains(c)) && s.len() > 10 && s.len() <= 32
301}
302
303pub(crate) fn validate_broadcast_secret(s: &str) -> bool {
304    let alphabet = base64::alphabet::URL_SAFE.as_str();
305    s.chars().all(|c| alphabet.contains(c)) && s.len() >= 43 && s.len() <= 100
306}
307
308/// Function generates a Message-ID that can be used for a new outgoing message.
309/// - this function is called for all outgoing messages.
310/// - the message ID should be globally unique
311/// - do not add a counter or any private data as this leaks information unnecessarily
312pub(crate) fn create_outgoing_rfc724_mid() -> String {
313    // We use UUID similarly to iCloud web mail client
314    // because it seems their spam filter does not like Message-IDs
315    // without hyphens.
316    //
317    // However, we use `localhost` instead of the real domain to avoid
318    // leaking the domain when resent by otherwise anonymizing
319    // From-rewriting mailing lists and forwarders.
320    let uuid = Uuid::new_v4();
321    format!("{uuid}@localhost")
322}
323
324// the returned suffix is lower-case
325pub fn get_filesuffix_lc(path_filename: &str) -> Option<String> {
326    Path::new(path_filename)
327        .extension()
328        .map(|p| p.to_string_lossy().to_lowercase())
329}
330
331/// Returns the `(width, height)` of the given image buffer.
332pub fn get_filemeta(buf: &[u8]) -> Result<(u32, u32)> {
333    let image = image::ImageReader::new(Cursor::new(buf)).with_guessed_format()?;
334    let dimensions = image.into_dimensions()?;
335    Ok(dimensions)
336}
337
338/// Expand paths relative to $BLOBDIR into absolute paths.
339///
340/// If `path` starts with "$BLOBDIR", replaces it with the blobdir path.
341/// Otherwise, returns path as is.
342pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf {
343    if let Ok(p) = path.strip_prefix("$BLOBDIR") {
344        context.get_blobdir().join(p)
345    } else {
346        path.into()
347    }
348}
349
350pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result<u64> {
351    let path_abs = get_abs_path(context, path);
352    let meta = fs::metadata(&path_abs).await?;
353    Ok(meta.len())
354}
355
356pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> {
357    let path_abs = get_abs_path(context, path);
358    if !path_abs.exists() {
359        bail!("path {} does not exist", path_abs.display());
360    }
361    if !path_abs.is_file() {
362        warn!(context, "refusing to delete non-file {}.", path.display());
363        bail!("not a file: \"{}\"", path.display());
364    }
365
366    let dpath = format!("{}", path.to_string_lossy());
367    fs::remove_file(path_abs)
368        .await
369        .with_context(|| format!("cannot delete {dpath:?}"))?;
370    context.emit_event(EventType::DeletedBlobFile(dpath));
371    Ok(())
372}
373
374/// Create a safe name based on a messy input string.
375///
376/// The safe name will be a valid filename on Unix and Windows and
377/// not contain any path separators.  The input can contain path
378/// segments separated by either Unix or Windows path separators,
379/// the rightmost non-empty segment will be used as name,
380/// sanitised for special characters.
381pub(crate) fn sanitize_filename(mut name: &str) -> String {
382    for part in name.rsplit('/') {
383        if !part.is_empty() {
384            name = part;
385            break;
386        }
387    }
388    for part in name.rsplit('\\') {
389        if !part.is_empty() {
390            name = part;
391            break;
392        }
393    }
394
395    let opts = sanitize_filename::Options {
396        truncate: true,
397        windows: true,
398        replacement: "",
399    };
400    let name = sanitize_filename::sanitize_with_options(name, opts);
401
402    if name.starts_with('.') || name.is_empty() {
403        format!("file{name}")
404    } else {
405        name
406    }
407}
408
409/// A guard which will remove the path when dropped.
410///
411/// It implements [`Deref`] so it can be used as a `&Path`.
412#[derive(Debug)]
413pub(crate) struct TempPathGuard {
414    path: PathBuf,
415}
416
417impl TempPathGuard {
418    pub(crate) fn new(path: PathBuf) -> Self {
419        Self { path }
420    }
421}
422
423impl Drop for TempPathGuard {
424    fn drop(&mut self) {
425        let path = self.path.clone();
426        std::fs::remove_file(path).ok();
427    }
428}
429
430impl Deref for TempPathGuard {
431    type Target = Path;
432
433    fn deref(&self) -> &Self::Target {
434        &self.path
435    }
436}
437
438impl AsRef<Path> for TempPathGuard {
439    fn as_ref(&self) -> &Path {
440        self
441    }
442}
443
444pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> {
445    let path_abs = get_abs_path(context, path);
446    if !path_abs.exists() {
447        match fs::create_dir_all(path_abs).await {
448            Ok(_) => Ok(()),
449            Err(err) => {
450                warn!(
451                    context,
452                    "Cannot create directory \"{}\": {}",
453                    path.display(),
454                    err
455                );
456                Err(err)
457            }
458        }
459    } else {
460        Ok(())
461    }
462}
463
464/// Write a the given content to provided file path.
465pub(crate) async fn write_file(
466    context: &Context,
467    path: &Path,
468    buf: &[u8],
469) -> Result<(), io::Error> {
470    let path_abs = get_abs_path(context, path);
471    fs::write(&path_abs, buf).await.map_err(|err| {
472        warn!(
473            context,
474            "Cannot write {} bytes to \"{}\": {}",
475            buf.len(),
476            path.display(),
477            err
478        );
479        err
480    })
481}
482
483/// Reads the file and returns its context as a byte vector.
484pub async fn read_file(context: &Context, path: &Path) -> Result<Vec<u8>> {
485    let path_abs = get_abs_path(context, path);
486
487    match fs::read(&path_abs).await {
488        Ok(bytes) => Ok(bytes),
489        Err(err) => {
490            warn!(
491                context,
492                "Cannot read \"{}\" or file is empty: {}",
493                path.display(),
494                err
495            );
496            Err(err.into())
497        }
498    }
499}
500
501pub async fn open_file(context: &Context, path: &Path) -> Result<fs::File> {
502    let path_abs = get_abs_path(context, path);
503
504    match fs::File::open(&path_abs).await {
505        Ok(bytes) => Ok(bytes),
506        Err(err) => {
507            warn!(
508                context,
509                "Cannot read \"{}\" or file is empty: {}",
510                path.display(),
511                err
512            );
513            Err(err.into())
514        }
515    }
516}
517
518pub fn open_file_std(context: &Context, path: impl AsRef<Path>) -> Result<std::fs::File> {
519    let path_abs = get_abs_path(context, path.as_ref());
520
521    match std::fs::File::open(path_abs) {
522        Ok(bytes) => Ok(bytes),
523        Err(err) => {
524            warn!(
525                context,
526                "Cannot read \"{}\" or file is empty: {}",
527                path.as_ref().display(),
528                err
529            );
530            Err(err.into())
531        }
532    }
533}
534
535/// Reads directory and returns a vector of directory entries.
536pub async fn read_dir(path: &Path) -> Result<Vec<fs::DirEntry>> {
537    let res = tokio_stream::wrappers::ReadDirStream::new(fs::read_dir(path).await?)
538        .try_collect()
539        .await?;
540    Ok(res)
541}
542
543pub(crate) fn time() -> i64 {
544    SystemTime::now()
545        .duration_since(SystemTime::UNIX_EPOCH)
546        .unwrap_or_default()
547        .as_secs() as i64
548}
549
550pub(crate) fn time_elapsed(time: &Time) -> Duration {
551    time.elapsed().unwrap_or_default()
552}
553
554/// Struct containing all mailto information
555#[derive(Debug, Default, Eq, PartialEq)]
556pub struct MailTo {
557    pub to: Vec<EmailAddress>,
558    pub subject: Option<String>,
559    pub body: Option<String>,
560}
561
562/// Parse mailto urls
563pub fn parse_mailto(mailto_url: &str) -> Option<MailTo> {
564    if let Ok(url) = Url::parse(mailto_url) {
565        if url.scheme() == "mailto" {
566            let mut mailto: MailTo = Default::default();
567            // Extract the email address
568            url.path().split(',').for_each(|email| {
569                if let Ok(email) = EmailAddress::new(email) {
570                    mailto.to.push(email);
571                }
572            });
573
574            // Extract query parameters
575            for (key, value) in url.query_pairs() {
576                if key == "subject" {
577                    mailto.subject = Some(value.to_string());
578                } else if key == "body" {
579                    mailto.body = Some(value.to_string());
580                }
581            }
582            Some(mailto)
583        } else {
584            None
585        }
586    } else {
587        None
588    }
589}
590
591pub(crate) trait IsNoneOrEmpty<T> {
592    /// Returns true if an Option does not contain a string
593    /// or contains an empty string.
594    fn is_none_or_empty(&self) -> bool;
595}
596impl<T> IsNoneOrEmpty<T> for Option<T>
597where
598    T: AsRef<str>,
599{
600    fn is_none_or_empty(&self) -> bool {
601        !matches!(self, Some(s) if !s.as_ref().is_empty())
602    }
603}
604
605pub(crate) trait ToOption<T> {
606    fn to_option(self) -> Option<T>;
607}
608impl<'a> ToOption<&'a str> for &'a String {
609    fn to_option(self) -> Option<&'a str> {
610        if self.is_empty() { None } else { Some(self) }
611    }
612}
613impl ToOption<String> for u16 {
614    fn to_option(self) -> Option<String> {
615        if self == 0 {
616            None
617        } else {
618            Some(self.to_string())
619        }
620    }
621}
622impl ToOption<String> for Option<i32> {
623    fn to_option(self) -> Option<String> {
624        match self {
625            None | Some(0) => None,
626            Some(v) => Some(v.to_string()),
627        }
628    }
629}
630
631#[expect(clippy::arithmetic_side_effects)]
632pub fn remove_subject_prefix(last_subject: &str) -> String {
633    let subject_start = if last_subject.starts_with("Chat:") {
634        0
635    } else {
636        // "Antw:" is the longest abbreviation in
637        // <https://en.wikipedia.org/wiki/List_of_email_subject_abbreviations#Abbreviations_in_other_languages>,
638        // so look at the first _5_ characters:
639        match last_subject.chars().take(5).position(|c| c == ':') {
640            Some(prefix_end) => prefix_end + 1,
641            None => 0,
642        }
643    };
644    last_subject
645        .chars()
646        .skip(subject_start)
647        .collect::<String>()
648        .trim()
649        .to_string()
650}
651
652// Types and methods to create hop-info for message-info
653
654#[expect(clippy::arithmetic_side_effects)]
655fn extract_address_from_receive_header<'a>(header: &'a str, start: &str) -> Option<&'a str> {
656    let header_len = header.len();
657    header.find(start).and_then(|mut begin| {
658        begin += start.len();
659        let end = header
660            .get(begin..)?
661            .find(|c: char| c.is_whitespace())
662            .unwrap_or(header_len);
663        header.get(begin..begin + end)
664    })
665}
666
667pub(crate) fn parse_receive_header(header: &str) -> String {
668    let header = header.replace(&['\r', '\n'][..], "");
669    let mut hop_info = String::from("Hop: ");
670
671    if let Some(from) = extract_address_from_receive_header(&header, "from ") {
672        hop_info += &format!("From: {}; ", from.trim());
673    }
674
675    if let Some(by) = extract_address_from_receive_header(&header, "by ") {
676        hop_info += &format!("By: {}; ", by.trim());
677    }
678
679    if let Ok(date) = dateparse(&header) {
680        // In tests, use the UTC timezone so that the test is reproducible
681        #[cfg(test)]
682        let date_obj = chrono::Utc.timestamp_opt(date, 0).single();
683        #[cfg(not(test))]
684        let date_obj = Local.timestamp_opt(date, 0).single();
685
686        hop_info += &format!(
687            "Date: {}",
688            date_obj.map_or_else(|| "?".to_string(), |x| x.to_rfc2822())
689        );
690    };
691
692    hop_info
693}
694
695/// parses "receive"-headers
696pub(crate) fn parse_receive_headers(headers: &Headers) -> String {
697    headers
698        .get_all_headers("Received")
699        .iter()
700        .rev()
701        .filter_map(|header_map_item| from_utf8(header_map_item.get_value_raw()).ok())
702        .map(parse_receive_header)
703        .collect::<Vec<_>>()
704        .join("\n")
705}
706
707/// If `collection` contains exactly one element, return this element.
708/// Otherwise, return None.
709pub(crate) fn single_value<T>(collection: impl IntoIterator<Item = T>) -> Option<T> {
710    let mut iter = collection.into_iter();
711    if let Some(value) = iter.next()
712        && iter.next().is_none()
713    {
714        return Some(value);
715    }
716    None
717}
718
719/// Compressor/decompressor buffer size.
720const BROTLI_BUFSZ: usize = 4096;
721
722/// Compresses `buf` to `Vec` using `brotli`.
723/// Note that it handles an empty `buf` as a special value that remains empty after compression,
724/// otherwise brotli would add its metadata to it which is not nice because this function is used
725/// for compression of strings stored in the db and empty strings are common there. This approach is
726/// not strictly correct because nowhere in the brotli documentation is said that an empty buffer
727/// can't be a result of compression of some input, but i think this will never break.
728pub(crate) fn buf_compress(buf: &[u8]) -> Result<Vec<u8>> {
729    if buf.is_empty() {
730        return Ok(Vec::new());
731    }
732    // level 4 is 2x faster than level 6 (and 54x faster than 10, for comparison).
733    // with the adaptiveness, we aim to not slow down processing
734    // single large files too much, esp. on low-budget devices.
735    // in tests (see #4129), this makes a difference, without compressing much worse.
736    let q: u32 = if buf.len() > 1_000_000 { 4 } else { 6 };
737    let lgwin: u32 = 22; // log2(LZ77 window size), it's the default for brotli CLI tool.
738    let mut compressor = brotli::CompressorWriter::new(Vec::new(), BROTLI_BUFSZ, q, lgwin);
739    compressor.write_all(buf)?;
740    Ok(compressor.into_inner())
741}
742
743/// Decompresses `buf` to `Vec` using `brotli`.
744/// See `buf_compress()` for why we don't pass an empty buffer to brotli decompressor.
745pub(crate) fn buf_decompress(buf: &[u8]) -> Result<Vec<u8>> {
746    if buf.is_empty() {
747        return Ok(Vec::new());
748    }
749    let mut decompressor = brotli::DecompressorWriter::new(Vec::new(), BROTLI_BUFSZ);
750    decompressor.write_all(buf)?;
751    decompressor.flush()?;
752    Ok(mem::take(decompressor.get_mut()))
753}
754
755/// Returns the given `&str` if already lowercased to avoid allocation, otherwise lowercases it.
756pub(crate) fn to_lowercase(s: &str) -> Cow<'_, str> {
757    match s.chars().all(char::is_lowercase) {
758        true => Cow::Borrowed(s),
759        false => Cow::Owned(s.to_lowercase()),
760    }
761}
762
763/// Returns text for storing in special db columns to make case-insensitive search possible for
764/// non-ASCII messages, chat and contact names.
765pub(crate) fn normalize_text(text: &str) -> Option<String> {
766    if text.is_ascii() {
767        return None;
768    };
769    Some(text.to_lowercase()).filter(|t| t != text)
770}
771
772/// Increments `*t` and checks that it equals to `expected` after that.
773#[expect(clippy::arithmetic_side_effects)]
774pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
775    t: &mut T,
776    expected: T,
777) -> Result<()> {
778    *t += T::one();
779    ensure!(*t == expected, "Incremented value != {expected:?}");
780    Ok(())
781}
782
783/// Converts usize to u64 without using `as`.
784///
785/// This is needed for example to convert in-memory buffer sizes
786/// to u64 type used for counting all the bytes written.
787///
788/// On 32-bit systems it is possible to have files
789/// larger than 4 GiB or write more than 4 GiB to network connection,
790/// in which case we need a 64-bit total counter,
791/// but use 32-bit usize for buffer sizes.
792///
793/// This can only break if usize has more than 64 bits
794/// and this is not the case as of 2025 and is
795/// unlikely to change for general purpose computers.
796/// See <https://github.com/rust-lang/rust/issues/30495>
797/// and <https://users.rust-lang.org/t/cant-convert-usize-to-u64/6243>
798/// and <https://github.com/rust-lang/rust/issues/106050>.
799pub(crate) fn usize_to_u64(v: usize) -> u64 {
800    u64::try_from(v).unwrap_or(u64::MAX)
801}
802
803/// Returns early with an error if a condition is not satisfied.
804/// In non-optimized builds, panics instead if so.
805#[macro_export]
806macro_rules! ensure_and_debug_assert {
807    ($cond:expr, $($arg:tt)*) => {
808        let cond_val = $cond;
809        debug_assert!(cond_val, $($arg)*);
810        anyhow::ensure!(cond_val, $($arg)*);
811    };
812}
813
814/// Returns early with an error on two expressions inequality.
815/// In non-optimized builds, panics instead if so.
816#[macro_export]
817macro_rules! ensure_and_debug_assert_eq {
818    ($left:expr, $right:expr, $($arg:tt)*) => {
819        match (&$left, &$right) {
820            (left_val, right_val) => {
821                debug_assert_eq!(left_val, right_val, $($arg)*);
822                anyhow::ensure!(left_val == right_val, $($arg)*);
823            }
824        }
825    };
826}
827
828/// Returns early with an error on two expressions equality.
829/// In non-optimized builds, panics instead if so.
830#[macro_export]
831macro_rules! ensure_and_debug_assert_ne {
832    ($left:expr, $right:expr, $($arg:tt)*) => {
833        match (&$left, &$right) {
834            (left_val, right_val) => {
835                debug_assert_ne!(left_val, right_val, $($arg)*);
836                anyhow::ensure!(left_val != right_val, $($arg)*);
837            }
838        }
839    };
840}
841
842/// Logs a warning if a condition is not satisfied.
843/// In non-optimized builds, panics also if so.
844#[macro_export]
845macro_rules! logged_debug_assert {
846    ($ctx:expr, $cond:expr, $($arg:tt)*) => {
847        let cond_val = $cond;
848        if !cond_val {
849            warn!($ctx, $($arg)*);
850        }
851        debug_assert!(cond_val, $($arg)*);
852    };
853}
854
855#[cfg(test)]
856mod tools_tests;