1use crate::sha256::sha256_hex;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum FileOrganization {
17 Sequential,
18 LineSequential,
19 Indexed,
20 Relative,
21 Vsam,
22 Unknown,
23}
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[non_exhaustive]
27pub enum ExtractMethod {
28 UnloadedFixedRecord,
29 ProgramExport,
30 VendorTool,
31 Unknown,
32}
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35#[non_exhaustive]
36pub enum RecordLengthSource {
37 Copybook,
38 Operator,
39 Trailer,
40 Unknown,
41}
42
43fn org(o: FileOrganization) -> &'static str {
44 match o {
45 FileOrganization::Sequential => "sequential",
46 FileOrganization::LineSequential => "line_sequential",
47 FileOrganization::Indexed => "indexed",
48 FileOrganization::Relative => "relative",
49 FileOrganization::Vsam => "vsam",
50 FileOrganization::Unknown => "unknown",
51 }
52}
53fn meth(m: ExtractMethod) -> &'static str {
54 match m {
55 ExtractMethod::UnloadedFixedRecord => "unloaded_fixed_record",
56 ExtractMethod::ProgramExport => "program_export",
57 ExtractMethod::VendorTool => "vendor_tool",
58 ExtractMethod::Unknown => "unknown",
59 }
60}
61fn rls(r: RecordLengthSource) -> &'static str {
62 match r {
63 RecordLengthSource::Copybook => "copybook",
64 RecordLengthSource::Operator => "operator",
65 RecordLengthSource::Trailer => "trailer",
66 RecordLengthSource::Unknown => "unknown",
67 }
68}
69
70pub struct ExtractProfile<'a> {
72 pub source_file_organization: FileOrganization,
73 pub extract_method: ExtractMethod,
74 pub record_length_source: RecordLengthSource,
75 pub copybook_source: &'a str,
76 pub code_set_conversion_before_kobold: Option<&'a str>,
78 pub source_system_cutoff: Option<&'a str>,
79 pub business_date: Option<&'a str>,
80 pub operator_declared_assumptions: &'a [&'a str],
81}
82
83pub struct ExtractManifest {
85 pub manifest_json: String,
86 pub casefile_json: String,
87}
88
89fn jstr(s: &str) -> String {
90 let mut o = String::from("\"");
91 for c in s.chars() {
92 match c {
93 '"' => o.push_str("\\\""),
94 '\\' => o.push_str("\\\\"),
95 c if (c as u32) < 0x20 => o.push_str(&format!("\\u{:04x}", c as u32)),
96 c => o.push(c),
97 }
98 }
99 o.push('"');
100 o
101}
102fn opt(s: Option<&str>) -> String {
103 s.map(jstr).unwrap_or_else(|| "null".to_string())
104}
105
106pub fn extract_manifest(copybook: &str, data: &[u8], profile: &ExtractProfile) -> ExtractManifest {
109 let assumptions = profile
110 .operator_declared_assumptions
111 .iter()
112 .map(|a| jstr(a))
113 .collect::<Vec<_>>()
114 .join(",");
115 let manifest_json = format!(
116 concat!(
117 "{{\"schema\":\"kobold-extract-profile-v1\",\"file_sha256\":{},\"copybook_sha256\":{},",
118 "\"source_file_organization\":{},\"extract_method\":{},\"record_length_source\":{},",
119 "\"copybook_source\":{},\"code_set_conversion_before_kobold\":{},\"source_system_cutoff\":{},",
120 "\"business_date\":{},\"operator_declared_assumptions\":[{}]}}"
121 ),
122 jstr(&sha256_hex(data)),
123 jstr(&sha256_hex(copybook.as_bytes())),
124 jstr(org(profile.source_file_organization)),
125 jstr(meth(profile.extract_method)),
126 jstr(rls(profile.record_length_source)),
127 jstr(profile.copybook_source),
128 opt(profile.code_set_conversion_before_kobold),
129 opt(profile.source_system_cutoff),
130 opt(profile.business_date),
131 assumptions,
132 );
133 let casefile_json = format!(
134 concat!(
135 "{{\"schema\":\"kobold-extract-forensic-casefile-v1\",\"court\":\"KOBOLD.EXTRACT.PROFILE.1\",",
136 "\"extraction_provenance\":{},",
137 "\"copybook_freshness\":{{\"claimed\":false,\"copybook_sha256\":{},\"copybook_source\":{},",
138 "\"evidence\":\"hash + declared provenance only\",\"risk\":\"a stale copybook may decode bytes plausibly wrong\"}},",
139 "\"extraction_truth\":{{\"claimed\":false}},\"currentness\":{{\"claimed\":false}},",
140 "\"negative_capabilities\":[\"NEG.EXTRACT.EXTRACTION_TRUTH\",\"NEG.EXTRACT.VSAM_NOT_CLAIMED\",",
141 "\"NEG.EXTRACT.INDEXED_BACKEND_NOT_CLAIMED\",\"NEG.EXTRACT.FILE_STATUS_NOT_INTERPRETED\",",
142 "\"NEG.CODESET.FILE_IO_CONVERSION\",\"NEG.COPYBOOK.STALE\",\"NEG.CURRENTNESS\"]}}\n"
143 ),
144 manifest_json,
145 jstr(&sha256_hex(copybook.as_bytes())),
146 jstr(profile.copybook_source),
147 );
148 ExtractManifest {
149 manifest_json,
150 casefile_json,
151 }
152}