ctb_formats_dceutils/
dce3_01a.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use anyhow::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_01a_to_dc(data: &[u8], log: &mut FormatLog) -> Result<String> {
39 let hex = bin2hex(data);
40 if !hex.starts_with("444345650201") {
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 if hex.len() < 14 || hex.get(12..14) != Some("02") {
45 log.error(
46 "This document is not stored using the specified version of DCE.",
47 );
48 bail!(
49 "This document is not stored using the specified version of DCE."
50 );
51 }
52
53 let tables = get_tables();
54 let mut txt = String::new();
55 let mut state = "Core".to_string();
56 let mut counter = 14_usize;
57 let mut append = String::new();
58
59 while counter < hex.len() {
60 let end_two = counter.saturating_add(2);
61 if end_two <= hex.len() {
62 if let Some(slice) = hex.get(counter..end_two) {
63 let byte_hex = slice.to_uppercase();
64 match state.as_str() {
65 "Core" => {
66 if let Some(val) =
67 tables.dc_map_dce3_01a_core.get(&byte_hex)
68 {
69 if let Some(stripped) = val.strip_prefix('>') {
70 state = stripped.to_string();
71 append.clear();
72 } else {
73 append = format!("{val},");
74 }
75 } else {
76 append.clear();
77 }
78 }
79 "Variant_Selectors" => {
80 if byte_hex == "FD" || byte_hex == "FE" {
81 state = "Core".to_string();
82 append.clear();
83 } else if let Some(val) = tables
84 .dc_map_dce3_01a_variant_selectors
85 .get(&byte_hex)
86 {
87 append = format!("{val},");
88 } else {
89 append.clear();
90 }
91 }
92 "Semantic_Records" => {
93 if byte_hex == "FD" || byte_hex == "FE" {
94 state = "Core".to_string();
95 append.clear();
96 } else if let Some(val) = tables
97 .dc_map_dce3_01a_semantic_records
98 .get(&byte_hex)
99 {
100 append = format!("{val},");
101 } else {
102 append.clear();
103 }
104 }
105 "Mathematics" => {
106 if byte_hex == "FD" || byte_hex == "FE" {
107 state = "Core".to_string();
108 append.clear();
109 } else if let Some(val) =
110 tables.dc_map_dce3_01a_mathematics.get(&byte_hex)
111 {
112 append = format!("{val},");
113 } else {
114 append.clear();
115 }
116 }
117 "Punctuation_and_Whitespace"
118 | "Whitespace_and_Punctuation" => {
119 if byte_hex == "FD" || byte_hex == "FE" {
125 state = "Core".to_string();
126 append.clear();
127 } else if let Some(val) = tables
128 .dc_map_dce3_01a_punctuation_and_whitespace
129 .get(&byte_hex)
130 {
131 append = format!("{val},");
132 } else {
133 append.clear();
134 }
135 }
136 _ => {}
137 }
138 }
139 }
140
141 let end_four = counter.saturating_add(4);
142 if end_four <= hex.len() {
143 if let Some(four_hex) = hex.get(counter..end_four) {
144 if four_hex == "fd03" {
145 break;
146 }
147 }
148 }
149
150 txt.push_str(&append);
151 counter = counter.saturating_add(2);
152 }
153
154 if txt.len() >= 4 {
155 if let Some(sub) = txt.get(3..txt.len().saturating_sub(1)) {
156 txt = sub.to_string();
157 } else {
158 txt.clear();
159 }
160 } else {
161 txt.clear();
162 }
163
164 Ok(txt)
165}
166
167pub fn convert_3_01a_raw_to_dc(
168 data: &[u8],
169 log: &mut FormatLog,
170) -> Result<String> {
171 let hex = bin2hex(data);
172 if !hex.starts_with("80") {
173 log.error("This document is not stored using the specified format.");
174 bail!("This document is not stored using the specified format.");
175 }
176
177 let mut wrapped = Vec::with_capacity(data.len().saturating_add(10));
178 wrapped
179 .extend_from_slice(&[0x44, 0x43, 0x45, 0x65, 0x02, 0x01, 0x02, 0xFD]);
180 wrapped.extend_from_slice(data);
181 wrapped.extend_from_slice(&[0xFD, 0x03]);
182
183 dce_convert_internal(&wrapped, "3_01a", "dc", log)
184 .map(|b| String::from_utf8_lossy(&b).into_owned())
185}
186
187pub fn convert_dc_to_3_01a_output(
192 data: &str,
193 log: &mut FormatLog,
194) -> Result<Vec<u8>> {
195 let encoded = dc_map_send_simple(data);
196 let hex_str = format!("44434565020102fd{encoded}fd03");
197 let bytes = hex2bin(&hex_str).map_err(|e| {
198 log.error(&format!("invalid hex string generated: {e}"));
199 anyhow!("invalid hex string generated: {e}")
200 })?;
201 Ok(bytes)
202}
203
204pub fn dc_map_send_simple(dc_list: &str) -> String {
205 if dc_list.is_empty() {
206 return String::new();
207 }
208 let dc_array = explode_escaped(',', dc_list);
209 let tables = get_tables();
210 let mut mapped = String::new();
211 let mut state = "Core".to_string();
212 let mut state_switch = String::new();
213
214 for dc_id in dc_array {
215 if let Some(record) = tables.dc_map_send_dce3_01a_all.get(&dc_id) {
216 if let Some((state_needed, append)) = record.split_once(':') {
217 if state_needed == state {
218 mapped.push_str(append);
219 } else {
220 for val in tables.dc_map_send_dce3_01a_all.values() {
222 let switch_query = format!(">{state_needed}");
223 if val.contains(&switch_query) {
224 if let Some((_, switch_hex)) = val.split_once(':') {
225 state_switch = switch_hex.to_string();
226 break;
227 }
228 }
229 }
230 mapped.push_str(&state_switch);
231 mapped.push_str(append);
232 state = state_needed.to_string();
233 }
234 }
235 }
236 }
237
238 mapped
239}