dexd/
header.rs

1// Shane Isbell licenses this file to you under the Apache License, Version 2.0
2// (the "License"); you may not use this file except in compliance with the License.
3//
4// You may obtain a copy of the License at
5//
6//       http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License. See the NOTICE file distributed with this work for
13// additional information regarding copyright ownership.
14use byteorder::{LittleEndian, ReadBytesExt};
15use prelude::get_bytes;
16use std::fs::File;
17
18#[derive(Debug, Clone)]
19pub struct DexFileHeader {
20    pub magic: Vec<u8>,
21
22    pub checksum: u32,
23
24    pub signature: Vec<u8>,
25
26    pub file_size: u32,
27
28    pub header_size: u32,
29
30    pub endian_tag: u32,
31
32    pub link_size: u32,
33
34    pub link_off: u32,
35
36    pub map_off: u32,
37
38    pub string_ids_size: u32,
39
40    pub string_ids_off: u32,
41
42    pub type_ids_size: u32,
43
44    pub type_ids_off: u32,
45
46    pub proto_ids_size: u32,
47
48    pub proto_ids_off: u32,
49
50    pub field_ids_size: u32,
51
52    pub field_ids_off: u32,
53
54    pub method_ids_size: u32,
55
56    pub method_ids_off: u32,
57
58    pub class_defs_size: u32,
59
60    pub class_defs_off: u32,
61
62    pub data_size: u32,
63
64    pub data_off: u32,
65}
66
67pub trait HeaderDexer {
68    fn dex_header(&mut self) -> DexFileHeader;
69
70    fn u32(&mut self) -> u32;
71}
72
73impl HeaderDexer for File {
74    fn u32(&mut self) -> u32 {
75        self.read_u32::<LittleEndian>().unwrap()
76    }
77
78    fn dex_header(&mut self) -> DexFileHeader {
79        DexFileHeader {
80            magic: get_bytes(&self, 8),
81            checksum: self.u32(),
82            signature: get_bytes(&self, 20),
83            file_size: self.u32(),
84            header_size: self.u32(),
85            endian_tag: self.u32(),
86            link_size: self.u32(),
87            link_off: self.u32(),
88            map_off: self.u32(),
89            string_ids_size: self.u32(),
90            string_ids_off: self.u32(),
91            type_ids_size: self.u32(),
92            type_ids_off: self.u32(),
93            proto_ids_size: self.u32(),
94            proto_ids_off: self.u32(),
95            field_ids_size: self.u32(),
96            field_ids_off: self.u32(),
97            method_ids_size: self.u32(),
98            method_ids_off: self.u32(),
99            class_defs_size: self.u32(),
100            class_defs_off: self.u32(),
101            data_size: self.u32(),
102            data_off: self.u32(),
103        }
104    }
105}