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
use core::fmt;
use super::HEADER_LEN;
use crate::{index::INDEX_LEN, record_field::RecordFields};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default)]
pub struct Schema {
pub sub_type: SchemaSubType,
pub num_record_fields: u8,
pub date: (u8, u8, u8),
pub v4_records_count: u32,
pub v4_records_position_start: u32,
pub v6_records_count: u32,
pub v6_records_position_start: u32,
pub v4_index_position_start: u32,
pub v6_index_position_start: u32,
pub r#type: SchemaType,
pub license_code: u8,
pub total_size: u32,
}
impl Schema {
pub fn record_fields(&self) -> Option<RecordFields> {
RecordFields::try_from((self.r#type, self.sub_type)).ok()
}
pub fn has_v6(&self) -> bool {
self.v6_records_count > 0
}
pub fn v4_index_seek_from_start(&self) -> u64 {
self.v4_index_position_start as u64 - 1
}
pub fn v6_index_seek_from_start(&self) -> Option<u64> {
if self.has_v6() {
Some(self.v6_index_position_start as u64 - 1)
} else {
None
}
}
pub fn v4_records_seek_from_start(&self) -> u64 {
self.v4_records_position_start as u64 - 1
}
pub fn v6_records_seek_from_start(&self) -> Option<u64> {
if self.has_v6() {
Some(self.v6_records_position_start as u64 - 1)
} else {
None
}
}
}
impl Schema {
pub fn verify(&self) -> Result<(), VerifyError> {
let record_fields = self
.record_fields()
.ok_or(VerifyError::SubTypeInvalid(self.sub_type))?;
if record_fields.len() != self.num_record_fields as usize {
return Err(VerifyError::NumRecordFieldsMismatch(self.num_record_fields));
}
if !self.has_v6() {
if self.v6_index_position_start != 1 {
return Err(VerifyError::Other(
"v6_index_position_start should eq 1 when v6_records_count is 0".into(),
));
}
if self.v6_records_position_start != 1 {
return Err(VerifyError::Other(
"v6_records_position_start should eq 1 when v6_records_count is 0".into(),
));
}
}
let mut cur_position: u32 = 0;
cur_position += HEADER_LEN + 1;
if self.v4_index_position_start != cur_position {
return Err(VerifyError::XPositionStartInvalid(
format!(
"v4_index_position_start mismatch {} {}",
self.v4_index_position_start, cur_position
)
.into(),
));
}
cur_position += INDEX_LEN;
if self.has_v6() {
if self.v6_index_position_start != cur_position {
return Err(VerifyError::XPositionStartInvalid(
format!(
"v6_index_position_start mismatch {} {}",
self.v6_index_position_start, cur_position
)
.into(),
));
}
cur_position += INDEX_LEN;
}
if self.v4_records_position_start != cur_position {
return Err(VerifyError::XPositionStartInvalid(
format!(
"v4_records_position_start mismatch {} {}",
self.v4_records_position_start, cur_position
)
.into(),
));
}
cur_position += record_fields.records_bytes_len_for_ipv4(self.v4_records_count);
if self.has_v6() {
if self.v6_records_position_start != cur_position {
return Err(VerifyError::XPositionStartInvalid(
format!(
"v6_records_position_start mismatch {} {}",
self.v6_records_position_start, cur_position
)
.into(),
));
}
cur_position += record_fields.records_bytes_len_for_ipv6(self.v6_records_count);
}
if cur_position >= self.total_size {
return Err(VerifyError::TotalSizeTooSmall(self.total_size));
}
Ok(())
}
}
#[derive(Debug)]
pub enum VerifyError {
SubTypeInvalid(SchemaSubType),
NumRecordFieldsMismatch(u8),
XPositionStartInvalid(Box<str>),
TotalSizeTooSmall(u32),
Other(Box<str>),
}
impl fmt::Display for VerifyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for VerifyError {}
#[derive(Debug, Clone, Copy, Default)]
pub struct SchemaSubType(pub u8);
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaType {
None,
IP2Location,
IP2Proxy,
}
impl Default for SchemaType {
fn default() -> Self {
Self::None
}
}
impl TryFrom<u8> for SchemaType {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::IP2Location),
2 => Ok(Self::IP2Proxy),
_ => Err(()),
}
}
}
impl SchemaType {
pub fn is_ip2location(&self) -> bool {
matches!(self, Self::IP2Location | Self::None)
}
pub fn is_ip2proxy(&self) -> bool {
matches!(self, Self::IP2Proxy)
}
}