xp3/
lib.rs

1/*
2 * Created on Tue Dec 15 2020
3 *
4 * Copyright (c) storycraft. Licensed under the Apache Licence 2.0.
5 */
6
7//! A XP3(krkr) archive library for rust.
8//! ## Examples
9//! See `examples` directory for various code examples.
10
11pub mod archive;
12pub mod reader;
13pub mod writer;
14
15pub mod header;
16pub mod index;
17pub mod index_set;
18
19pub use reader::XP3Reader;
20pub use writer::XP3Writer;
21
22use std::{error::Error, io};
23
24use self::{header::XP3Header, index_set::XP3IndexSet};
25
26pub const XP3_MAGIC: [u8; 10] = [ 0x58_u8, 0x50, 0x33, 0x0D, 0x0A, 0x20, 0x0A, 0x1A, 0x8B, 0x67 ];
27
28pub const XP3_CURRENT_VER_IDENTIFIER: u64 = 0x17;
29
30pub const XP3_VERSION_IDENTIFIER: u8 = 128;
31
32pub const XP3_INDEX_CONTINUE: u8 = 0x80;
33
34pub const XP3_INDEX_FILE_IDENTIFIER: u32 = 1701603654; // File
35
36pub const XP3_INDEX_INFO_IDENTIFIER: u32 = 1868983913; // info
37pub const XP3_INDEX_SEGM_IDENTIFIER: u32 = 1835492723; // segm
38pub const XP3_INDEX_ADLR_IDENTIFIER: u32 = 1919706209; // adlr
39pub const XP3_INDEX_TIME_IDENTIFIER: u32 = 1701669236; // time
40
41#[derive(Debug)]
42pub struct XP3Error {
43
44    kind: XP3ErrorKind,
45    error: Option<Box<dyn Error>>
46
47}
48
49impl XP3Error {
50
51    pub fn new(kind: XP3ErrorKind, error: Option<Box<dyn Error>>) -> Self {
52        Self {
53            kind, error
54        }
55    }
56
57    pub fn kind(&self) -> &XP3ErrorKind {
58        &self.kind
59    }
60
61    pub fn error(&self) -> &Option<Box<dyn Error>> {
62        &self.error
63    }
64
65}
66
67impl From<io::Error> for XP3Error {
68
69    fn from(err: io::Error) -> Self {
70        XP3Error::new(XP3ErrorKind::Io(err), None)
71    }
72
73}
74
75#[derive(Debug)]
76pub enum XP3ErrorKind {
77
78    Io(io::Error),
79    InvalidFile,
80    InvalidHeader,
81    InvalidFileIndexHeader,
82    InvalidFileIndex,
83    InvalidFileIndexFlag,
84
85    FileNotFound
86
87}
88
89/// Virtual XP3 container containing XP3 file information.
90#[derive(Debug)]
91pub struct VirtualXP3 {
92
93    header: XP3Header,
94    index_set: XP3IndexSet
95
96}
97
98impl VirtualXP3 {
99
100    pub fn new(
101        header: XP3Header,
102        index_set: XP3IndexSet
103    ) -> Self {
104        Self {
105            header,
106            index_set
107        }
108    }
109
110    pub fn header(&self) -> XP3Header {
111        self.header
112    }
113
114    pub fn set_header(&mut self, header: XP3Header) {
115        self.header = header;
116    }
117
118    pub fn index_set(&self) -> &XP3IndexSet {
119        &self.index_set
120    }
121
122    pub fn set_index_set(&mut self, index_set: XP3IndexSet) {
123        self.index_set = index_set;
124    }
125
126}