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
use scroll::{ctx, Pread, Uleb128};
use crate::cache::Ref;
use crate::class::ClassId;
use crate::encoded_item::EncodedItem;
use crate::encoded_item::EncodedItemArray;
use crate::error::Error;
use crate::jtype::Type;
use crate::string::JString;
use crate::string::StringId;
use crate::uint;
use crate::ulong;
use crate::ushort;
use getset::{CopyGetters, Getters};
bitflags! {
pub struct AccessFlags: ulong {
const PUBLIC = 0x1;
const PRIVATE = 0x2;
const PROTECTED = 0x4;
const STATIC = 0x8;
const FINAL = 0x10;
const VOLATILE = 0x40;
const TRANSIENT = 0x80;
const SYNTHETIC = 0x1000;
const ANNOTATION = 0x2000;
const ENUM = 0x4000;
}
}
#[derive(Debug, Getters, CopyGetters)]
pub struct Field {
#[get = "pub"]
name: Ref<JString>,
#[get = "pub"]
jtype: Type,
#[get_copy = "pub"]
class: ClassId,
#[get_copy = "pub"]
access_flags: AccessFlags,
}
impl Field {
pub(crate) fn try_from_dex<S: AsRef<[u8]>>(
dex: &super::Dex<S>,
encoded_field: &EncodedField,
) -> super::Result<Self> {
debug!(target: "field", "encoded field: {:?}", encoded_field);
let field_item = dex.get_field_item(encoded_field.field_id)?;
debug!(target: "field", "field id item: {:?}", field_item);
Ok(Self {
name: dex.get_string(field_item.name_idx)?,
jtype: dex.get_type(uint::from(field_item.type_idx))?,
class: uint::from(field_item.class_idx),
access_flags: AccessFlags::from_bits(encoded_field.access_flags).ok_or_else(|| {
Error::InvalidId(format!(
"Invalid access flags when loading field {}",
field_item.name_idx
))
})?,
})
}
}
pub(crate) type EncodedFieldArray = EncodedItemArray<EncodedField>;
#[derive(Pread, Debug, Getters)]
#[get = "pub"]
pub struct FieldIdItem {
class_idx: ushort,
type_idx: ushort,
name_idx: StringId,
}
impl FieldIdItem {
pub(crate) fn try_from_dex<T: AsRef<[u8]>>(
dex: &super::Dex<T>,
offset: ulong,
) -> super::Result<Self> {
let source = &dex.source;
Ok(source.pread_with(offset as usize, dex.get_endian())?)
}
}
pub type FieldId = ulong;
#[derive(Debug, CopyGetters)]
#[get_copy = "pub"]
pub struct EncodedField {
pub(crate) field_id: FieldId,
access_flags: ulong,
}
impl EncodedItem for EncodedField {
fn id(&self) -> ulong {
self.field_id
}
}
impl<'a> ctx::TryFromCtx<'a, ulong> for EncodedField {
type Error = Error;
type Size = usize;
fn try_from_ctx(source: &'a [u8], prev_id: ulong) -> super::Result<(Self, Self::Size)> {
let offset = &mut 0;
let id = Uleb128::read(source, offset)?;
let access_flags = Uleb128::read(source, offset)?;
Ok((
Self {
field_id: prev_id + id,
access_flags,
},
*offset,
))
}
}