1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! Common types use throughout
use std::fmt::{Debug, Display};
use std::str::FromStr;
use std::io::{Read, Seek};
use std::fs::File;

use crate::error::NitfError;

/// Lowest level object for file parsing
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct NitfField<V: FromStr + Debug> {
    /// Byte representation
    pub bytes: Vec<u8>,
    /// String representation of field
    pub string: String,
    /// Parsed representation of value
    pub val: V,
    /// Number of bytes used to store value in file
    length: u64,
    /// Byte offset in file
    offset: u64,
}

/// Standard security metadata
#[derive(Default, Clone, Debug, Eq, PartialEq)]
pub struct Security {
    /// File Security Classification
    pub clas: NitfField<Classification>,
    /// File Classification Security System
    pub clsy: NitfField<String>, // TODO: Check value registry
    /// File Codewords
    pub code: NitfField<String>,
    /// File Control and Handling
    pub ctlh: NitfField<String>,
    /// File Releasing Instructions
    pub rel: NitfField<String>,
    /// File Declassification Type
    pub dctp: NitfField<DeclassificationType>,
    /// File Declassification Date
    pub dcdt: NitfField<String>,
    /// File Declassification Exemption
    pub dcxm: NitfField<DeclassificationExemption>,
    /// File Downgrade
    pub dg: NitfField<Downgrade>,
    /// File Downgrade Date
    pub dgdt: NitfField<String>,
    /// File Classification Text
    pub cltx: NitfField<String>,
    /// File Classification Authority Type
    pub catp: NitfField<ClassificationAuthorityType>,
    /// File Classification Authority
    pub caut: NitfField<String>,
    /// File Classification Reason
    pub crsn: NitfField<ClassificationReason>, // TODO: Check value registry
    /// File Security Source Date
    pub srdt: NitfField<String>,
    /// File Security Control Number
    pub ctln: NitfField<String>,
}

/// Classification codes
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Classification {
    #[default]
    /// Unclassified
    U,
    /// Top Secret
    T,
    /// Secret
    S,
    /// Confidential
    C,
    /// Restricted
    R,
}

/// Declassification codes
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum DeclassificationType {
    #[default]
    /// Default value, two spaces
    DEFAULT,
    /// Declassify on specific date
    DD,
    /// Declassify on occurrence of event
    DE,
    /// Downgrade to specified level on specific date
    GD,
    /// Downgrade to specified level on occurrence of event
    GE,
    /// OADR
    O,
    /// Exempt from automatic declassification
    X,
}

///  Declassification exemption
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum DeclassificationExemption {
    #[default]
    /// Default value, four spaces
    DEFAULT,
    /// Valid value, see NitfField.string for value
    VALID,
}

/// Downgrade classification
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum Downgrade {
    #[default]
    /// Default value, two spaces
    DEFAULT,
    /// Secret
    S,
    /// Confidential
    C,
    /// Restricted
    R,
}

/// Classification authority
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum ClassificationAuthorityType {
    #[default]
    /// Default, one space
    DEFAULT,
    /// Original classification authority
    O,
    /// Derivative from a single source
    D,
    /// Derivative from multiple sources
    M,
}

/// Reason for classification
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum ClassificationReason {
    #[default]
    /// Default value, one space
    DEFAULT,
    /// Valid value, see NitfField.string for value
    VALID,
}

/// Use Default implementation
impl<V> NitfField<V>
where
    V: FromStr + Debug,
    <V as FromStr>::Err: Debug,
{
    /// Read the specified number of bytes and parse the value of a given field
    pub fn read<T: Sized + Into<u64>>(&mut self, reader: &mut File, n_bytes: T) {
        self.length = n_bytes.into();
        self.bytes = vec![0; self.length as usize];
        self.offset = reader.stream_position().unwrap();
        reader.read_exact(&mut self.bytes).unwrap();
        let result = String::from_utf8(self.bytes.to_vec());
        match result {
            Ok(str) => {
                self.string = str.trim().to_string();
                self.val = self.string.parse().unwrap();
            }
            Err(err) => {
                self.string = String::from("Error parsing string");
                println!("{}", err)
            }
        }
    }
}
impl<V: FromStr + Debug> Display for NitfField<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", &self.string)
    }
}
impl Security {
    pub fn read(&mut self, reader: &mut File) {
        self.clas.read(reader, 1u8);
        self.clsy.read(reader, 2u8);
        self.code.read(reader, 11u8);
        self.ctlh.read(reader, 2u8);
        self.rel.read(reader, 20u8);
        self.dctp.read(reader, 2u8);
        self.dcdt.read(reader, 8u8);
        self.dcxm.read(reader, 4u8);
        self.dg.read(reader, 1u8);
        self.dgdt.read(reader, 8u8);
        self.cltx.read(reader, 43u8);
        self.catp.read(reader, 1u8);
        self.caut.read(reader, 40u8);
        self.crsn.read(reader, 1u8);
        self.srdt.read(reader, 8u8);
        self.ctln.read(reader, 15u8);
    }
}
impl Display for Security {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut out_str = String::default();
        out_str += format!("CLAS: {}, ", self.clas).as_ref();
        out_str += format!("CLSY: {}, ", self.clsy).as_ref();
        out_str += format!("CODE: {}, ", self.code).as_ref();
        out_str += format!("CTLH: {}, ", self.ctlh).as_ref();
        out_str += format!("REL: {}, ", self.rel).as_ref();
        out_str += format!("DCTP: {}, ", self.dctp).as_ref();
        out_str += format!("DCDT: {}, ", self.dcdt).as_ref();
        out_str += format!("DCXM: {}, ", self.dcxm).as_ref();
        out_str += format!("DG: {}, ", self.dg).as_ref();
        out_str += format!("DGDT: {}, ", self.dgdt).as_ref();
        out_str += format!("CLTX: {}, ", self.cltx).as_ref();
        out_str += format!("CATP: {}, ", self.catp).as_ref();
        out_str += format!("CAUT: {}, ", self.caut).as_ref();
        out_str += format!("CRSN: {}, ", self.crsn).as_ref();
        out_str += format!("SRDT: {}, ", self.srdt).as_ref();
        out_str += format!("CTLN: {}", self.ctln).as_ref();
        write!(f, "{}", out_str)
    }
}
impl FromStr for Classification {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "U" => Ok(Self::U),
            "T" => Ok(Self::T),
            "S" => Ok(Self::S),
            "C" => Ok(Self::C),
            "R" => Ok(Self::R),
            _ => Err(NitfError::FieldError),
        }
    }
}
impl FromStr for DeclassificationType {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::DEFAULT),
            "DD" => Ok(Self::DD),
            "DE" => Ok(Self::DE),
            "GD" => Ok(Self::GD),
            "GE" => Ok(Self::GE),
            "O" => Ok(Self::O),
            "X" => Ok(Self::X),
            _ => Err(NitfError::FieldError),
        }
    }
}
impl FromStr for DeclassificationExemption {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::DEFAULT),
            "X1" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(1)
            "X2" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(2)
            "X3" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(3)
            "X4" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(4)
            "X5" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(5)
            "X6" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(6)
            "X7" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(7)
            "X8" => Ok(Self::VALID),   // DOD 5200.01-V1, 4-201b(8)
            "25X1" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(1)
            "25X2" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(2)
            "25X3" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(3)
            "25X4" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(4)
            "25X5" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(5)
            "25X6" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(6)
            "25X7" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(7)
            "25X8" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(8)
            "25X9" => Ok(Self::VALID), // DOD 5200.01-V1, 4-301b(9)
            _ => Err(NitfError::FieldError),
        }
    }
}
impl FromStr for Downgrade {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::DEFAULT),
            "S" => Ok(Self::S),
            "C" => Ok(Self::C),
            "R" => Ok(Self::R),
            _ => Err(NitfError::FieldError),
        }
    }
}
impl FromStr for ClassificationAuthorityType {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::DEFAULT),
            "O" => Ok(Self::O),
            "D" => Ok(Self::D),
            "M" => Ok(Self::M),
            _ => Err(NitfError::FieldError),
        }
    }
}
impl FromStr for ClassificationReason {
    type Err = NitfError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(Self::DEFAULT),
            "A" => Ok(Self::VALID),
            "B" => Ok(Self::VALID),
            "C" => Ok(Self::VALID),
            "D" => Ok(Self::VALID),
            "E" => Ok(Self::VALID),
            "F" => Ok(Self::VALID),
            "G" => Ok(Self::VALID),
            "H" => Ok(Self::VALID),
            _ => Err(NitfError::FieldError),
        }
    }
}

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct ExtendedSubheader {
    /// User defined tagged record entries (TREs)
    pub tre: Vec<u8>,
    /// Length of subheader
    pub size: usize,

}
impl ExtendedSubheader {
    pub fn read(&mut self, reader: &mut File, n_bytes: usize) {
        self.size = n_bytes;
        self.tre = vec![0; n_bytes];
        reader.read_exact(self.tre.as_mut_slice()).unwrap();

    }
}
impl Display for ExtendedSubheader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Can't copy vector directly, convert to slice, then clone into new vector
        let out_str = String::from_utf8(self.tre.to_vec()).unwrap();
        write!(f, "[{out_str}]")
    }
}