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