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.
18use std::time::Duration;
19pub use std::time::SystemTime as Time;
20#[cfg(not(test))]
21pub use std::time::SystemTime;
22
23use anyhow::{Context as _, Result, bail, ensure};
24use base64::Engine as _;
25use chrono::{Local, NaiveDateTime, NaiveTime, TimeZone};
26use deltachat_contact_tools::EmailAddress;
27#[cfg(test)]
28pub use deltachat_time::SystemTimeTools as SystemTime;
29use futures::TryStreamExt;
30use mailparse::MailHeaderMap;
31use mailparse::dateparse;
32use mailparse::headers::Headers;
33use num_traits::PrimInt;
34use rand::{Rng, thread_rng};
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.
50pub(crate) fn truncate(buf: &str, approx_chars: usize) -> Cow<'_, str> {
51    let count = buf.chars().count();
52    if count <= approx_chars + DC_ELLIPSIS.len() {
53        return Cow::Borrowed(buf);
54    }
55    let end_pos = buf
56        .char_indices()
57        .nth(approx_chars)
58        .map(|(n, _)| n)
59        .unwrap_or_default();
60
61    if let Some(index) = buf.get(..end_pos).and_then(|s| s.rfind([' ', '\n'])) {
62        Cow::Owned(format!(
63            "{}{}",
64            &buf.get(..=index).unwrap_or_default(),
65            DC_ELLIPSIS
66        ))
67    } else {
68        Cow::Owned(format!(
69            "{}{}",
70            &buf.get(..end_pos).unwrap_or_default(),
71            DC_ELLIPSIS
72        ))
73    }
74}
75
76/// Shortens a string to a specified line count and adds "[...]" to the
77/// end of the shortened string.
78///
79/// returns tuple with the String and a boolean whether is was truncated
80pub(crate) fn truncate_by_lines(
81    buf: String,
82    max_lines: usize,
83    max_line_len: usize,
84) -> (String, bool) {
85    let mut lines = 0;
86    let mut line_chars = 0;
87    let mut break_point: Option<usize> = None;
88
89    for (index, char) in buf.char_indices() {
90        if char == '\n' {
91            line_chars = 0;
92            lines += 1;
93        } else {
94            line_chars += 1;
95            if line_chars > max_line_len {
96                line_chars = 1;
97                lines += 1;
98            }
99        }
100        if lines == max_lines {
101            break_point = Some(index);
102            break;
103        }
104    }
105
106    if let Some(end_pos) = break_point {
107        // Text has too many lines and needs to be truncated.
108        let text = {
109            if let Some(buffer) = buf.get(..end_pos) {
110                if let Some(index) = buffer.rfind([' ', '\n']) {
111                    buf.get(..=index)
112                } else {
113                    buf.get(..end_pos)
114                }
115            } else {
116                None
117            }
118        };
119
120        if let Some(truncated_text) = text {
121            (format!("{truncated_text}{DC_ELLIPSIS}"), true)
122        } else {
123            // In case of indexing/slicing error, we return an error
124            // message as a preview and add HTML version. This should
125            // never happen.
126            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.]";
127            (error_text.to_string(), true)
128        }
129    } else {
130        // text is unchanged
131        (buf, false)
132    }
133}
134
135/// Shortens a message text if necessary according to the configuration. Adds "[...]" to the end of
136/// the shortened text.
137///
138/// Returns the resulting text and a bool telling whether a truncation was done.
139pub(crate) async fn truncate_msg_text(context: &Context, text: String) -> Result<(String, bool)> {
140    if context.get_config_bool(Config::Bot).await? {
141        return Ok((text, false));
142    }
143    // Truncate text if it has too many lines
144    Ok(truncate_by_lines(
145        text,
146        constants::DC_DESIRED_TEXT_LINES,
147        constants::DC_DESIRED_TEXT_LINE_LEN,
148    ))
149}
150
151/* ******************************************************************************
152 * date/time tools
153 ******************************************************************************/
154
155/// Converts Unix time in seconds to a local timestamp string.
156pub fn timestamp_to_str(wanted: i64) -> String {
157    if let Some(ts) = Local.timestamp_opt(wanted, 0).single() {
158        ts.format("%Y.%m.%d %H:%M:%S").to_string()
159    } else {
160        // Out of range number of seconds.
161        "??.??.?? ??:??:??".to_string()
162    }
163}
164
165/// Converts duration to string representation suitable for logs.
166pub fn duration_to_str(duration: Duration) -> String {
167    let secs = duration.as_secs();
168    let h = secs / 3600;
169    let m = (secs % 3600) / 60;
170    let s = (secs % 3600) % 60;
171    format!("{h}h {m}m {s}s")
172}
173
174pub(crate) fn gm2local_offset() -> i64 {
175    /* returns the offset that must be _added_ to an UTC/GMT-time to create the localtime.
176    the function may return negative values. */
177    let lt = Local::now();
178    i64::from(lt.offset().local_minus_utc())
179}
180
181/// Returns the current smeared timestamp,
182///
183/// The returned timestamp MAY NOT be unique and MUST NOT go to "Date" header.
184pub(crate) fn smeared_time(context: &Context) -> i64 {
185    let now = time();
186    let ts = context.smeared_timestamp.current();
187    std::cmp::max(ts, now)
188}
189
190/// Returns a timestamp that is guaranteed to be unique.
191pub(crate) fn create_smeared_timestamp(context: &Context) -> i64 {
192    let now = time();
193    context.smeared_timestamp.create(now)
194}
195
196// creates `count` timestamps that are guaranteed to be unique.
197// the first created timestamps is returned directly,
198// get the other timestamps just by adding 1..count-1
199pub(crate) fn create_smeared_timestamps(context: &Context, count: usize) -> i64 {
200    let now = time();
201    context.smeared_timestamp.create_n(now, count as i64)
202}
203
204/// Returns the last release timestamp as a unix timestamp compatible for comparison with time() and
205/// database times.
206pub fn get_release_timestamp() -> i64 {
207    NaiveDateTime::new(
208        *crate::release::DATE,
209        NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
210    )
211    .and_utc()
212    .timestamp_millis()
213        / 1_000
214}
215
216// if the system time is not plausible, once a day, add a device message.
217// for testing we're using time() as that is also used for message timestamps.
218// moreover, add a warning if the app is outdated.
219pub(crate) async fn maybe_add_time_based_warnings(context: &Context) {
220    if !maybe_warn_on_bad_time(context, time(), get_release_timestamp()).await {
221        maybe_warn_on_outdated(context, time(), get_release_timestamp()).await;
222    }
223}
224
225async fn maybe_warn_on_bad_time(context: &Context, now: i64, known_past_timestamp: i64) -> bool {
226    if now < known_past_timestamp {
227        let mut msg = Message::new(Viewtype::Text);
228        msg.text = stock_str::bad_time_msg_body(
229            context,
230            &Local.timestamp_opt(now, 0).single().map_or_else(
231                || "YY-MM-DD hh:mm:ss".to_string(),
232                |ts| ts.format("%Y-%m-%d %H:%M:%S").to_string(),
233            ),
234        )
235        .await;
236        if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
237            add_device_msg_with_importance(
238                context,
239                Some(
240                    format!(
241                        "bad-time-warning-{}",
242                        timestamp.format("%Y-%m-%d") // repeat every day
243                    )
244                    .as_str(),
245                ),
246                Some(&mut msg),
247                true,
248            )
249            .await
250            .ok();
251        } else {
252            warn!(context, "Can't convert current timestamp");
253        }
254        return true;
255    }
256    false
257}
258
259async fn maybe_warn_on_outdated(context: &Context, now: i64, approx_compile_time: i64) {
260    if now > approx_compile_time + DC_OUTDATED_WARNING_DAYS * 24 * 60 * 60 {
261        let mut msg = Message::new_text(stock_str::update_reminder_msg_body(context).await);
262        if let Some(timestamp) = chrono::DateTime::<chrono::Utc>::from_timestamp(now, 0) {
263            add_device_msg(
264                context,
265                Some(
266                    format!(
267                        "outdated-warning-{}",
268                        timestamp.format("%Y-%m") // repeat every month
269                    )
270                    .as_str(),
271                ),
272                Some(&mut msg),
273            )
274            .await
275            .ok();
276        }
277    }
278}
279
280/// Generate an unique ID.
281///
282/// The generated ID should be short but unique:
283/// - short, because it used in Chat-Group-ID headers and in QR codes
284/// - unique as two IDs generated on two devices should not be the same
285///
286/// IDs generated by this function have 144 bits of entropy
287/// and are returned as 24 Base64 characters, each containing 6 bits of entropy.
288/// 144 is chosen because it is sufficiently secure
289/// (larger than AES-128 keys used for message encryption)
290/// and divides both by 8 (byte size) and 6 (number of bits in a single Base64 character).
291pub(crate) fn create_id() -> String {
292    // ThreadRng implements CryptoRng trait and is supposed to be cryptographically secure.
293    let mut rng = thread_rng();
294
295    // Generate 144 random bits.
296    let mut arr = [0u8; 18];
297    rng.fill(&mut arr[..]);
298
299    base64::engine::general_purpose::URL_SAFE.encode(arr)
300}
301
302/// Returns true if given string is a valid ID.
303///
304/// All IDs generated with `create_id()` should be considered valid.
305pub(crate) fn validate_id(s: &str) -> bool {
306    let alphabet = base64::alphabet::URL_SAFE.as_str();
307    s.chars().all(|c| alphabet.contains(c)) && s.len() > 10 && s.len() <= 32
308}
309
310/// Function generates a Message-ID that can be used for a new outgoing message.
311/// - this function is called for all outgoing messages.
312/// - the message ID should be globally unique
313/// - do not add a counter or any private data as this leaks information unnecessarily
314pub(crate) fn create_outgoing_rfc724_mid() -> String {
315    // We use UUID similarly to iCloud web mail client
316    // because it seems their spam filter does not like Message-IDs
317    // without hyphens.
318    //
319    // However, we use `localhost` instead of the real domain to avoid
320    // leaking the domain when resent by otherwise anonymizing
321    // From-rewriting mailing lists and forwarders.
322    let uuid = Uuid::new_v4();
323    format!("{uuid}@localhost")
324}
325
326// the returned suffix is lower-case
327pub fn get_filesuffix_lc(path_filename: &str) -> Option<String> {
328    Path::new(path_filename)
329        .extension()
330        .map(|p| p.to_string_lossy().to_lowercase())
331}
332
333/// Returns the `(width, height)` of the given image buffer.
334pub fn get_filemeta(buf: &[u8]) -> Result<(u32, u32)> {
335    let image = image::ImageReader::new(Cursor::new(buf)).with_guessed_format()?;
336    let dimensions = image.into_dimensions()?;
337    Ok(dimensions)
338}
339
340/// Expand paths relative to $BLOBDIR into absolute paths.
341///
342/// If `path` starts with "$BLOBDIR", replaces it with the blobdir path.
343/// Otherwise, returns path as is.
344pub(crate) fn get_abs_path(context: &Context, path: &Path) -> PathBuf {
345    if let Ok(p) = path.strip_prefix("$BLOBDIR") {
346        context.get_blobdir().join(p)
347    } else {
348        path.into()
349    }
350}
351
352pub(crate) async fn get_filebytes(context: &Context, path: &Path) -> Result<u64> {
353    let path_abs = get_abs_path(context, path);
354    let meta = fs::metadata(&path_abs).await?;
355    Ok(meta.len())
356}
357
358pub(crate) async fn delete_file(context: &Context, path: &Path) -> Result<()> {
359    let path_abs = get_abs_path(context, path);
360    if !path_abs.exists() {
361        bail!("path {} does not exist", path_abs.display());
362    }
363    if !path_abs.is_file() {
364        warn!(context, "refusing to delete non-file {}.", path.display());
365        bail!("not a file: \"{}\"", path.display());
366    }
367
368    let dpath = format!("{}", path.to_string_lossy());
369    fs::remove_file(path_abs)
370        .await
371        .with_context(|| format!("cannot delete {dpath:?}"))?;
372    context.emit_event(EventType::DeletedBlobFile(dpath));
373    Ok(())
374}
375
376/// Create a safe name based on a messy input string.
377///
378/// The safe name will be a valid filename on Unix and Windows and
379/// not contain any path separators.  The input can contain path
380/// segments separated by either Unix or Windows path separators,
381/// the rightmost non-empty segment will be used as name,
382/// sanitised for special characters.
383pub(crate) fn sanitize_filename(mut name: &str) -> String {
384    for part in name.rsplit('/') {
385        if !part.is_empty() {
386            name = part;
387            break;
388        }
389    }
390    for part in name.rsplit('\\') {
391        if !part.is_empty() {
392            name = part;
393            break;
394        }
395    }
396
397    let opts = sanitize_filename::Options {
398        truncate: true,
399        windows: true,
400        replacement: "",
401    };
402    let name = sanitize_filename::sanitize_with_options(name, opts);
403
404    if name.starts_with('.') || name.is_empty() {
405        format!("file{name}")
406    } else {
407        name
408    }
409}
410
411/// A guard which will remove the path when dropped.
412///
413/// It implements [`Deref`] so it can be used as a `&Path`.
414#[derive(Debug)]
415pub(crate) struct TempPathGuard {
416    path: PathBuf,
417}
418
419impl TempPathGuard {
420    pub(crate) fn new(path: PathBuf) -> Self {
421        Self { path }
422    }
423}
424
425impl Drop for TempPathGuard {
426    fn drop(&mut self) {
427        let path = self.path.clone();
428        std::fs::remove_file(path).ok();
429    }
430}
431
432impl Deref for TempPathGuard {
433    type Target = Path;
434
435    fn deref(&self) -> &Self::Target {
436        &self.path
437    }
438}
439
440impl AsRef<Path> for TempPathGuard {
441    fn as_ref(&self) -> &Path {
442        self
443    }
444}
445
446pub(crate) async fn create_folder(context: &Context, path: &Path) -> Result<(), io::Error> {
447    let path_abs = get_abs_path(context, path);
448    if !path_abs.exists() {
449        match fs::create_dir_all(path_abs).await {
450            Ok(_) => Ok(()),
451            Err(err) => {
452                warn!(
453                    context,
454                    "Cannot create directory \"{}\": {}",
455                    path.display(),
456                    err
457                );
458                Err(err)
459            }
460        }
461    } else {
462        Ok(())
463    }
464}
465
466/// Write a the given content to provided file path.
467pub(crate) async fn write_file(
468    context: &Context,
469    path: &Path,
470    buf: &[u8],
471) -> Result<(), io::Error> {
472    let path_abs = get_abs_path(context, path);
473    fs::write(&path_abs, buf).await.map_err(|err| {
474        warn!(
475            context,
476            "Cannot write {} bytes to \"{}\": {}",
477            buf.len(),
478            path.display(),
479            err
480        );
481        err
482    })
483}
484
485/// Reads the file and returns its context as a byte vector.
486pub async fn read_file(context: &Context, path: &Path) -> Result<Vec<u8>> {
487    let path_abs = get_abs_path(context, path);
488
489    match fs::read(&path_abs).await {
490        Ok(bytes) => Ok(bytes),
491        Err(err) => {
492            warn!(
493                context,
494                "Cannot read \"{}\" or file is empty: {}",
495                path.display(),
496                err
497            );
498            Err(err.into())
499        }
500    }
501}
502
503pub async fn open_file(context: &Context, path: &Path) -> Result<fs::File> {
504    let path_abs = get_abs_path(context, path);
505
506    match fs::File::open(&path_abs).await {
507        Ok(bytes) => Ok(bytes),
508        Err(err) => {
509            warn!(
510                context,
511                "Cannot read \"{}\" or file is empty: {}",
512                path.display(),
513                err
514            );
515            Err(err.into())
516        }
517    }
518}
519
520pub fn open_file_std(context: &Context, path: impl AsRef<Path>) -> Result<std::fs::File> {
521    let path_abs = get_abs_path(context, path.as_ref());
522
523    match std::fs::File::open(path_abs) {
524        Ok(bytes) => Ok(bytes),
525        Err(err) => {
526            warn!(
527                context,
528                "Cannot read \"{}\" or file is empty: {}",
529                path.as_ref().display(),
530                err
531            );
532            Err(err.into())
533        }
534    }
535}
536
537/// Reads directory and returns a vector of directory entries.
538pub async fn read_dir(path: &Path) -> Result<Vec<fs::DirEntry>> {
539    let res = tokio_stream::wrappers::ReadDirStream::new(fs::read_dir(path).await?)
540        .try_collect()
541        .await?;
542    Ok(res)
543}
544
545pub(crate) fn time() -> i64 {
546    SystemTime::now()
547        .duration_since(SystemTime::UNIX_EPOCH)
548        .unwrap_or_default()
549        .as_secs() as i64
550}
551
552pub(crate) fn time_elapsed(time: &Time) -> Duration {
553    time.elapsed().unwrap_or_default()
554}
555
556/// Struct containing all mailto information
557#[derive(Debug, Default, Eq, PartialEq)]
558pub struct MailTo {
559    pub to: Vec<EmailAddress>,
560    pub subject: Option<String>,
561    pub body: Option<String>,
562}
563
564/// Parse mailto urls
565pub fn parse_mailto(mailto_url: &str) -> Option<MailTo> {
566    if let Ok(url) = Url::parse(mailto_url) {
567        if url.scheme() == "mailto" {
568            let mut mailto: MailTo = Default::default();
569            // Extract the email address
570            url.path().split(',').for_each(|email| {
571                if let Ok(email) = EmailAddress::new(email) {
572                    mailto.to.push(email);
573                }
574            });
575
576            // Extract query parameters
577            for (key, value) in url.query_pairs() {
578                if key == "subject" {
579                    mailto.subject = Some(value.to_string());
580                } else if key == "body" {
581                    mailto.body = Some(value.to_string());
582                }
583            }
584            Some(mailto)
585        } else {
586            None
587        }
588    } else {
589        None
590    }
591}
592
593pub(crate) trait IsNoneOrEmpty<T> {
594    /// Returns true if an Option does not contain a string
595    /// or contains an empty string.
596    fn is_none_or_empty(&self) -> bool;
597}
598impl<T> IsNoneOrEmpty<T> for Option<T>
599where
600    T: AsRef<str>,
601{
602    fn is_none_or_empty(&self) -> bool {
603        !matches!(self, Some(s) if !s.as_ref().is_empty())
604    }
605}
606
607pub(crate) trait ToOption<T> {
608    fn to_option(self) -> Option<T>;
609}
610impl<'a> ToOption<&'a str> for &'a String {
611    fn to_option(self) -> Option<&'a str> {
612        if self.is_empty() { None } else { Some(self) }
613    }
614}
615impl ToOption<String> for u16 {
616    fn to_option(self) -> Option<String> {
617        if self == 0 {
618            None
619        } else {
620            Some(self.to_string())
621        }
622    }
623}
624impl ToOption<String> for Option<i32> {
625    fn to_option(self) -> Option<String> {
626        match self {
627            None | Some(0) => None,
628            Some(v) => Some(v.to_string()),
629        }
630    }
631}
632
633pub fn remove_subject_prefix(last_subject: &str) -> String {
634    let subject_start = if last_subject.starts_with("Chat:") {
635        0
636    } else {
637        // "Antw:" is the longest abbreviation in
638        // <https://en.wikipedia.org/wiki/List_of_email_subject_abbreviations#Abbreviations_in_other_languages>,
639        // so look at the first _5_ characters:
640        match last_subject.chars().take(5).position(|c| c == ':') {
641            Some(prefix_end) => prefix_end + 1,
642            None => 0,
643        }
644    };
645    last_subject
646        .chars()
647        .skip(subject_start)
648        .collect::<String>()
649        .trim()
650        .to_string()
651}
652
653// Types and methods to create hop-info for message-info
654
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        if iter.next().is_none() {
713            return Some(value);
714        }
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/// Increments `*t` and checks that it equals to `expected` after that.
756pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
757    t: &mut T,
758    expected: T,
759) -> Result<()> {
760    *t += T::one();
761    ensure!(*t == expected, "Incremented value != {expected:?}");
762    Ok(())
763}
764
765#[cfg(test)]
766mod tools_tests;