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
#![allow(non_snake_case, non_upper_case_globals)]

use std::rc::Rc;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Field {
    pub size: u32,
    pub ty: Option<TypeSpec>,
    pub name: Option<String>
}

impl Field {
    pub fn size(size: u32) -> Field {
        Field { size, ty: None, name: None }
    }
    pub fn with_ty(mut self, ty: TypeSpec) -> Self {
        self.ty = Some(ty);
        self
    }
    pub fn with_name<T: Into<String>>(mut self, name: T) -> Self {
        self.name = Some(name.into());
        self
    }
    pub fn type_of(&self) -> TypeSpec {
        self.ty.to_owned().unwrap_or(TypeSpec::Unknown)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeLayout {
    pub name: String,
    fields: Vec<Field>,
    size: u32
}

impl TypeLayout {
    pub fn new(name: String, fields: Vec<Field>) -> Self {
        let size = fields.iter().map(|f| f.size).sum();

        TypeLayout { name, fields, size }
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn field_for(&self, mut offset: u32) -> Option<&Field> {
        for f in self.fields.iter() {
            if offset == 0 {
                return Some(f);
            } else {
                if f.size > offset {
                    return None;
                } else {
                    offset -= f.size;
                }
            }
        }

        None
    }
}

pub struct TypeAtlas {
    types: Vec<TypeLayout>
}

pub const KPCR: usize = 0;
pub const U64: usize = 1;
pub const I64: usize = 2;
pub const U32: usize = 3;
pub const I32: usize = 4;
pub const U16: usize = 5;
pub const I16: usize = 6;
pub const KTSS64: usize = 7;
pub const KTHREAD: usize = 8;
pub const KPCRB: usize = 9;
pub const AVFrame: usize = 10;

impl TypeAtlas {
    pub fn new() -> TypeAtlas {
        let KPCR_Layout = TypeLayout::new(
            "KPCR".to_string(),
            vec![
                // 0x0000
                Field::size(8).with_ty(TypeSpec::Unknown.pointer_to()).with_name("GdtBase"),
                // 0x0008
                Field::size(8).with_ty(TypeSpec::struct_pointer(KTSS64)).with_name("TssBase"),
                // 0x0010
                Field::size(8).with_ty(TypeSpec::LayoutId(U64)).with_name("UserRsp"),
                // 0x0018
                Field::size(8).with_ty(TypeSpec::struct_pointer(KPCR)).with_name("Self"),
                // 0x0020
                Field::size(8).with_ty(TypeSpec::struct_pointer(KPCRB)).with_name("CurrentPcrb"),
                // 0x0028
                Field::size(0x20).with_name("TODO:KPCRB_data"),
                // 0x0048
                Field::size(8).with_ty(TypeSpec::Unknown.pointer_to()).with_name("unknown_kpcr_field_0x48"),
                // 0x0050
                Field::size(0x10).with_name("TODO:KPCR_data"),
                // 0x0060
                Field::size(2).with_name("MajorVersion"),
                Field::size(2).with_name("MinorVersion"),
                // 0x0064
                Field::size(4).with_name("TODO:KPCR_data"),
                // 0x0068
                Field::size(0x118).with_name("TODO:KPCR_data"),
                // 0x0180
                Field::size(8).with_name("Unknown_KPCRB_field"),
                // 0x0188
                Field::size(8).with_ty(TypeSpec::struct_pointer(KTHREAD)).with_name("CurrentThread"),
                // 0x0190
                Field::size(8).with_ty(TypeSpec::struct_pointer(KTHREAD)).with_name("NextThread"),
                // 0x0198
                Field::size(8).with_ty(TypeSpec::struct_pointer(KTHREAD)).with_name("IdleThread"),
                // 0x01a0
                Field::size(8).with_ty(TypeSpec::LayoutId(U64)).with_name("UserRsp"),
                // 0x01a8
                Field::size(8).with_ty(TypeSpec::LayoutId(U64)).with_name("RspBase"),
                // 0x01b0
                Field::size(0x5e50).with_name("TODO:KPCRB_data"),
                // 0x6000
                Field::size(8).with_name("Unknown_KPCRB_field"),
                // 0x6008
                Field::size(8).with_ty(TypeSpec::Unknown.pointer_to()).with_name("Unknown_KPCRB_field"),
            ]
        );

        let U64_Layout = TypeLayout::new(
            "U64".to_string(),
            vec![Field::size(8)]
        );

        let I64_Layout = TypeLayout::new(
            "I64".to_string(),
            vec![Field::size(8)]
        );

        let U32_Layout = TypeLayout::new(
            "U32".to_string(),
            vec![Field::size(4)]
        );

        let I32_Layout = TypeLayout::new(
            "I32".to_string(),
            vec![Field::size(4)]
        );

        let U16_Layout = TypeLayout::new(
            "U16".to_string(),
            vec![Field::size(2)]
        );

        let I16_Layout = TypeLayout::new(
            "I16".to_string(),
            vec![Field::size(2)]
        );

        let AVFrame_Layout = TypeLayout::new(
            "AVFrame".to_string(),
            vec![
                Field::size(64).with_name("data"),
                Field::size(4 * 8).with_name("linesize"),
                // 0x60
                Field::size(8).with_name("extended_data"),
                Field::size(4).with_name("width"),
                Field::size(4).with_name("height"),
                // 0x70
                Field::size(4).with_name("nb_samples"),
                Field::size(4).with_name("format"),
                Field::size(4).with_name("key_frame"),
                Field::size(4).with_name("pict_type"),
                // 0x80
                Field::size(4).with_name("sample_aspect_ratio.num"),
                Field::size(4).with_name("sample_aspect_ratio.den"),
                Field::size(8).with_name("pts"),
                Field::size(8).with_name("pkt_pts"),
                Field::size(8).with_name("pkt_dts"),
                // 0x98
                Field::size(4).with_name("coded_picture_number"),
                Field::size(4).with_name("display_picture_number"),
                // 0xa0
                Field::size(4).with_name("quality"),
                Field::size(4).with_name("pad_0"),
                Field::size(8).with_name("opaque"),
                Field::size(8 * 8).with_name("error(deprecated)"),
                Field::size(4).with_name("repeat_pict"),
                // 0xf0
                Field::size(4).with_name("interlaced_frame"),
                Field::size(4).with_name("top_field_first"),
                Field::size(4).with_name("palette_has_changed"),
                Field::size(8).with_name("reordered_opaque"),
                Field::size(4).with_name("sample_rate"),
                Field::size(4).with_name("pad_1"),
                Field::size(8).with_name("channel_layout"),
                // 0x110
                Field::size(8 * 8).with_name("buf"),
                // 0x150
                Field::size(8).with_name("extended_buf"),
                Field::size(4).with_name("nb_extended_buf"),
                Field::size(4).with_name("pad_2"),
                Field::size(8).with_name("side_data"),
                Field::size(4).with_name("nb_side_data"),
                // 0x168
                Field::size(4).with_name("flags"),
                Field::size(4).with_name("color_range"),
                // 0x170
                Field::size(4).with_name("color_primaries"),
                Field::size(4).with_name("color_trc"),
                Field::size(4).with_name("colorspace"),
                Field::size(4).with_name("chroma_location"),
                // 0x180
                Field::size(4).with_name("pad_3"),
                Field::size(8).with_name("best_effort_timestamp"),
                Field::size(8).with_name("pkt_pos"),
                Field::size(8).with_name("pkt_duration"),
                Field::size(8).with_name("metadata"),
                // 0x1a0
                Field::size(4).with_name("decode_error_flags"),
                Field::size(4).with_name("channels"),
                Field::size(4).with_name("pkt_size"),
                Field::size(4).with_name("pad_4"),
                Field::size(8).with_name("hw_frames_ctx"),
                Field::size(8).with_name("opaque_ref"),
                Field::size(8).with_name("crop_top"),
                Field::size(8).with_name("crop_bottom"),
                Field::size(8).with_name("crop_left"),
                Field::size(8).with_name("crop_right"),
            ]
        );

        // https://github.com/ntdiff/headers/blob/master/Win10_1507_TS1/x64/System32/hal.dll/Standalone/_KTSS64.h
        let KTSS64_Layout = TypeLayout::new(
            "KTSS64".to_string(),
            vec![
                // 0x0000
                Field { size: 4, ty: Some(TypeSpec::LayoutId(U32)), name: Some("Reserved0".to_string()) },
                // 0x0004
                Field { size: 8, ty: Some(TypeSpec::LayoutId(I64)), name: Some("Rsp0".to_string()) },
                // 0x000c
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Rsp1".to_string()) },
                // 0x0014
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Rsp2".to_string()) },
                // 0x001c
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[0]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[1]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[2]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[3]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[4]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[5]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[6]".to_string()) },
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Ist[7]".to_string()) },
                // 0x005c
                Field { size: 8, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I64)))), name: Some("Reserved1".to_string()) },
                // 0x0064
                Field { size: 2, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I16)))), name: Some("Reserved2".to_string()) },
                // 0x0066
                Field { size: 2, ty: Some(TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(I16)))), name: Some("IoMapBase".to_string()) },

            ]
        );

        let KTHREAD_Layout = TypeLayout::new(
            "KTHREAD".to_string(),
            vec![
            ]
        );

        let types = vec![KPCR_Layout.clone(), U64_Layout, I64_Layout, U32_Layout, I32_Layout, U16_Layout, I16_Layout, KTSS64_Layout, KTHREAD_Layout, KPCR_Layout, AVFrame_Layout];

        TypeAtlas { types }
    }

    pub fn name_of(&self, type_spec: &TypeSpec) -> String {
        match type_spec {
            TypeSpec::Top => "[any]".to_string(),
            TypeSpec::LayoutId(id) => { self.types[*id].name.to_string() },
            TypeSpec::PointerTo(spec) => { format!("{}*", self.name_of(spec)) },
            TypeSpec::Unknown => "[unknown]".to_string(),
            TypeSpec::Bottom => "[!]".to_string(),
        }
    }

    pub fn layout_of(&self, type_id: usize) -> &TypeLayout {
        &self.types[type_id]
    }

    pub fn get_field(&self, ty: &TypeSpec, offset: u32) -> Option<&Field> {
        if let TypeSpec::PointerTo(inner) = ty {
            if let TypeSpec::LayoutId(id) = **inner {
                let inner_layout = self.layout_of(id);
                return inner_layout.field_for(offset);
            }
        }

        None
    }
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum TypeSpec {
    Top,
    LayoutId(usize),
    PointerTo(Rc<TypeSpec>),
    Unknown,
    Bottom,
}

impl TypeSpec {
    pub fn struct_pointer(id: usize) -> Self {
        TypeSpec::PointerTo(Rc::new(TypeSpec::LayoutId(id)))
    }
    pub fn pointer_to(self) -> Self {
        TypeSpec::PointerTo(Rc::new(self))
    }
}

pub trait Typed {
    fn type_of(&self, type_atlas: &TypeAtlas) -> TypeSpec;
}