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
use crate::main_header::HostOS;

#[repr(u8)]
#[derive(Debug)]
pub enum FileType {
    Binary = 0,
    Text7Bit = 1,
    CommentHeader = 2,
    Directory = 3,
    VolumeLabel = 4,
    ChapterLabel = 5,
    Unknown(u8),
}

impl From<u8> for FileType {
    fn from(value: u8) -> Self {
        match value {
            0 => FileType::Binary,
            1 => FileType::Text7Bit,
            2 => FileType::CommentHeader,
            3 => FileType::Directory,
            4 => FileType::VolumeLabel,
            5 => FileType::ChapterLabel,
            _ => FileType::Unknown(value),
        }
    }
}

#[repr(u8)]
#[derive(Debug)]
pub enum CompressionMethod {
    Stored = 0,
    CompressedMost = 1,
    Compressed = 2,
    CompressedFaster = 3,
    CompressedFastest = 4,
    NoDataNoCrc = 8,
    NoData = 9,
    Unknown(u8),
}

impl From<u8> for CompressionMethod {
    fn from(value: u8) -> Self {
        match value {
            0 => CompressionMethod::Stored,
            1 => CompressionMethod::CompressedMost,
            2 => CompressionMethod::Compressed,
            3 => CompressionMethod::CompressedFaster,
            4 => CompressionMethod::CompressedFastest,
            8 => CompressionMethod::NoDataNoCrc,
            9 => CompressionMethod::NoData,
            _ => CompressionMethod::Unknown(value),
        }
    }
}

#[derive(Debug)]
pub struct LocalFileHeader {
    pub archiver_version_number: u8,
    pub min_version_to_extract: u8,
    pub host_os: HostOS,
    pub arj_flags: u8,
    pub compression_method: CompressionMethod,
    pub file_type: FileType,

    pub date_time_modified: u32,
    pub compressed_size: u32,
    pub original_size: u32,
    pub original_crc32: u32,
    
    pub file_spec_position: u16,
    pub file_access_mode: u16,
    
    pub first_chapter: u8,
    pub last_chapter: u8,

    pub extended_file_position: u32,

    pub date_time_accessed: u32,
    pub date_time_created: u32,

    pub original_size_even_for_volumes: u32,

    pub name: String,
    pub comment: String,
}

impl LocalFileHeader {

    pub fn load_from(mut header_bytes: &[u8]) -> Option<Self> {
        convert_u8!(header_size, header_bytes);

        convert_u8!(archiver_version_number, header_bytes);
        convert_u8!(min_version_to_extract, header_bytes);
        convert_u8!(host_os, header_bytes);
        convert_u8!(arj_flags, header_bytes);
        convert_u8!(compression_method, header_bytes);
        convert_u8!(file_type, header_bytes);
        skip!(header_bytes, 1);
        convert_u32!(date_time_modified, header_bytes);
        convert_u32!(compressed_size, header_bytes);
        convert_u32!(original_size, header_bytes);
        convert_u32!(original_crc32, header_bytes);
        convert_u16!(file_spec_position, header_bytes);
        convert_u16!(file_access_mode, header_bytes);
        convert_u8!(first_chapter, header_bytes);
        convert_u8!(last_chapter, header_bytes);

        let mut extended_file_position = 0;
        let mut date_time_accessed = 0;
        let mut date_time_created = 0;
        let mut original_size_even_for_volumes = 0;
        
        if header_size >= 33 {
            convert_u32!(extended_file_position2, header_bytes);
            extended_file_position = extended_file_position2;

            if header_size >= 45 {
                convert_u32!(date_time_accessed2, header_bytes);
                convert_u32!(date_time_created2, header_bytes);
                convert_u32!(original_size_even2_for_volumes, header_bytes);
                date_time_accessed = date_time_accessed2;
                date_time_created = date_time_created2;
                original_size_even_for_volumes = original_size_even2_for_volumes;
                skip!(header_bytes, 12);
            }
            skip!(header_bytes, 4);
        }

        convert_string!(name, header_bytes);
        convert_string!(comment, header_bytes);

        Some(LocalFileHeader {
            archiver_version_number,
            min_version_to_extract,
            host_os: host_os.into(),
            arj_flags,
            compression_method: compression_method.into(),
            file_type: file_type.into(),
            date_time_modified,
            compressed_size,
            original_size,
            original_crc32,
            file_spec_position,
            file_access_mode,
            first_chapter,
            last_chapter,
            extended_file_position,
            date_time_accessed,
            date_time_created,
            original_size_even_for_volumes,
            name,
            comment,
        })
    }

    pub fn is_garbled(&self) -> bool {
        self.arj_flags & 0x01 != 0
    }

    pub fn is_volume(&self) -> bool {
        self.arj_flags & 0x04 != 0
    }

    pub fn is_ext_file(&self) -> bool {
        self.arj_flags & 0x08 != 0
    }

    pub fn is_path_sym(&self) -> bool {
        self.arj_flags & 0x10 != 0
    }

    pub fn is_backup(&self) -> bool {
        self.arj_flags & 0x20 != 0
    }
}