ctb_formats_dceutils/
dce3_0a.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! DCE 3.0a specification character set and codepage conversion.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use anyhow::{Context, anyhow};
30use ctb_formats_hexdump::hex2bin;
31
32use ctb_formats_utilities::FormatLog;
33
34use crate::dce_convert_internal;
35use crate::tables::get_tables;
36use crate::tools::explode_escaped;
37
38pub fn convert_3_0a_to_dc(data: &[u8], log: &mut FormatLog) -> Result<String> {
39    let hex = bin2hex(data);
40    if hex.len() < 14 || hex.get(12..14) != Some("01") {
41        log.error("This document is not stored using the specified format.");
42        bail!("This document is not stored using the specified format.");
43    }
44
45    let tables = get_tables();
46    let mut txt = String::new();
47    let mut counter = 14_usize;
48
49    while counter < hex.len() {
50        let next_two = counter.saturating_add(2);
51        if next_two <= hex.len() {
52            if let Some(slice) = hex.get(counter..next_two) {
53                let byte_hex = slice.to_uppercase();
54                if let Some(val) = tables.dc_map_dce3_0a_core.get(&byte_hex) {
55                    if !val.is_empty() {
56                        txt.push_str(val);
57                        txt.push(',');
58                    }
59                }
60            }
61        }
62        let next_four = counter.saturating_add(4);
63        if next_four <= hex.len() {
64            if let Some(four_slice) = hex.get(counter..next_four) {
65                if four_slice == "fd03" {
66                    break;
67                }
68            }
69        }
70        counter = counter.saturating_add(2);
71    }
72
73    if txt.len() >= 6 {
74        let sub = txt
75            .get(3..txt.len().saturating_sub(3))
76            .context("substring slice")?;
77        txt = sub.to_string();
78    } else {
79        txt.clear();
80    }
81
82    Ok(txt)
83}
84
85pub fn convert_3_0a_raw_to_dc(
86    data: &[u8],
87    log: &mut FormatLog,
88) -> Result<String> {
89    let hex = bin2hex(data);
90    if hex.len() < 2 || hex.get(0..2) != Some("80") {
91        log.error("This document is not stored using the specified format.");
92        bail!("This document is not stored using the specified format.");
93    }
94
95    let mut wrapped = Vec::with_capacity(data.len().saturating_add(10));
96    wrapped
97        .extend_from_slice(&[0x44, 0x43, 0x45, 0x65, 0x02, 0x01, 0x01, 0xFD]);
98    wrapped.extend_from_slice(data);
99    wrapped.extend_from_slice(&[0xFD, 0x03]);
100
101    dce_convert_internal(&wrapped, "3_0a", "dc", log)
102        .map(|b| String::from_utf8_lossy(&b).into_owned())
103}
104
105pub fn convert_dc_to_3_0a_output(
106    data: &str,
107    log: &mut FormatLog,
108) -> Result<Vec<u8>> {
109    let dc_array = explode_escaped(',', data);
110    let tables = get_tables();
111    let mut hex_str = "44434565020101fd80".to_string();
112
113    for dc_id in dc_array {
114        if let Some(mapped) = tables.dc_map_send_dce3_0a.get(&dc_id) {
115            hex_str.push_str(&mapped.to_lowercase());
116        }
117    }
118    hex_str.push_str("81fd03");
119
120    let bytes = hex2bin(&hex_str).map_err(|e| {
121        log.error(&format!("Failed to encode hex in 3_0a serialization: {e}"));
122        anyhow!("Failed to encode hex in 3_0a serialization: {e}")
123    })?;
124    Ok(bytes)
125}
126
127pub fn convert_dc_to_3_0a_raw_output(
128    data: &str,
129    log: &mut FormatLog,
130) -> Result<Vec<u8>> {
131    let full = dce_convert_internal(data.as_bytes(), "dc", "3_0a", log)?;
132    if full.len() >= 12 {
133        let sub = full
134            .get(9..full.len().saturating_sub(3))
135            .context("substring slice")?;
136        Ok(sub.to_vec())
137    } else {
138        bail!("Output too short")
139    }
140}