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
//! Representations of logical structures found on android binary files

use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
use model::owned::Entry;
use errors::*;

mod element;
mod value;
pub mod owned;
pub mod builder;

pub use self::element::Element;
pub use self::element::Tag;
pub use self::element::ElementContainer;
pub use self::value::Value;

use visitor::Origin;

pub type Namespaces = BTreeMap<String, String>;
pub type Entries = HashMap<u32, Entry>;

pub trait Identifier {
    fn get_package(&self) -> u8;
    fn get_spec(&self) -> u8;
    fn get_id(&self) -> u16;
}

impl Identifier for u32 {
    fn get_package(&self) -> u8 {
        let mut package_id = (self >> 24) as u8;

        if package_id == 0 {
            package_id = 1;
            info!("Resource with package id 0 found. Recreate id with current package id");
        }

        package_id
    }

    fn get_spec(&self) -> u8 {
        ((self & 0x00FF0000) >> 16) as u8
    }

    fn get_id(&self) -> u16 {
        (self & 0xFFFF) as u16
    }
}

// Traits
pub trait StringTable {
    fn get_strings_len(&self) -> u32;
    fn get_styles_len(&self) -> u32;
    fn get_string(&self, idx: u32) -> Result<Rc<String>>;
}

// TODO: Decide if the trait should return Results or not
pub trait Package {
    fn get_id(&self) -> Result<u32>;
    fn get_name(&self) -> Result<String>;
}

pub trait Library {
    fn get_name(&self) -> Option<String>;
    fn format_reference(&self,
                        id: u32,
                        key: u32,
                        namespace: Option<String>,
                        prefix: &str)
                        -> Result<String>;
    // fn get_entries(&self) -> &Entries;
    fn get_entry(&self, id: u32) -> Result<&Entry>;
    fn get_entries_string(&self, str_id: u32) -> Result<Rc<String>>;
    fn get_spec_string(&self, str_id: u32) -> Result<Rc<String>>;
}

pub trait LibraryBuilder<'a> {
    type StringTable: StringTable;
    type TypeSpec: TypeSpec;

    fn set_string_table(&mut self, string_table: Self::StringTable, origin: Origin);
    fn add_entries(&mut self, entries: Entries);
    fn add_type_spec(&mut self, type_spec: Self::TypeSpec);
}

pub trait Resources<'a> {
    type Library: Library + LibraryBuilder<'a>;

    fn get_package(&self, package_id: u8) -> Option<&Self::Library>;
    fn get_mut_package(&mut self, package_id: u8) -> Option<&mut Self::Library>;
    fn get_main_package(&self) -> Option<&Self::Library>;
    fn is_main_package(&self, package_id: u8) -> bool;
}

/// Trait that represents a XML tag start
pub trait TagStart {
    /// Type of the attributes
    type Attribute: AttributeTrait;

    /// Return the ¿line in which the tag appear?
    fn get_line(&self) -> Result<u32>;
    /// Return the content of the unknown field1
    fn get_field1(&self) -> Result<u32>;
    /// Return the namespace index. If there is no namespace, it will return 0xFFFFFFFF
    fn get_namespace_index(&self) -> Result<u32>;
    /// Returns the index of the tag name on the string table
    fn get_element_name_index(&self) -> Result<u32>;
    /// Return the content of the unknown field1
    fn get_field2(&self) -> Result<u32>;
    /// Return the amount of attributes this tag contains
    fn get_attributes_amount(&self) -> Result<u32>;
    /// Returns the ¿class?
    fn get_class(&self) -> Result<u32>;
    /// Returns the attribute on the `index` position or error if it is greater than
    /// `get_attributes_amount`
    fn get_attribute(&self, index: u32) -> Result<Self::Attribute>;
}

pub trait AttributeTrait {
    /// Return the namespace index. If there is no namespace, it will return 0xFFFFFFFF
    fn get_namespace(&self) -> Result<u32>;
    /// Returns the index of the attribute on the string table
    fn get_name(&self) -> Result<u32>;
    /// Returns the ¿class?
    fn get_class(&self) -> Result<u32>;
    /// Returns the data type (see `Values`)
    fn get_resource_value(&self) -> Result<u32>;
    /// Returns the value (see `Values`)
    fn get_data(&self) -> Result<u32>;

    /// Creates a `Value` depending on the data type and data value
    fn get_value(&self) -> Result<Value> {
        let data_type = ((self.get_resource_value()? >> 24) & 0xFF) as u8;
        let data_value = self.get_data()?;
        let class = self.get_class()?;

        let value = if class == 0xFFFFFFFF {
            Value::new(data_type, data_value)?
        } else {
            Value::StringReference(class)
        };

        Ok(value)
    }
}

pub trait TagEnd {
    fn get_id(&self) -> Result<u32>;
}

pub trait NamespaceStart {
    fn get_line(&self) -> Result<u32>;
    fn get_prefix<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>>;
    fn get_namespace<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>>;
}

pub trait NamespaceEnd {
    fn get_line(&self) -> Result<u32>;
    fn get_prefix<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>>;
    fn get_namespace<S: StringTable>(&self, string_table: &S) -> Result<Rc<String>>;
}

pub trait TypeSpec {
    fn get_id(&self) -> Result<u16>;
    fn get_amount(&self) -> Result<u32>;
    fn get_flag(&self, index: u32) -> Result<u32>;
}

pub trait TableType {
    type Configuration: Configuration;

    fn get_id(&self) -> Result<u8>;
    fn get_amount(&self) -> Result<u32>;
    fn get_configuration(&self) -> Result<Self::Configuration>;
    fn get_entry(&self, index: u32) -> Result<Entry>;
}

pub trait Configuration {
    fn get_size(&self) -> Result<u32>;
    fn get_mcc(&self) -> Result<u16>;
    fn get_mnc(&self) -> Result<u16>;
    fn get_language(&self) -> Result<String>;
    fn get_region(&self) -> Result<String>;
    fn get_orientation(&self) -> Result<u8>;
    fn get_touchscreen(&self) -> Result<u8>;
    fn get_density(&self) -> Result<u16>;
    fn get_keyboard(&self) -> Result<u8>;
    fn get_navigation(&self) -> Result<u8>;
    fn get_input_flags(&self) -> Result<u8>;
    fn get_width(&self) -> Result<u16>;
    fn get_height(&self) -> Result<u16>;
    fn get_sdk_version(&self) -> Result<u16>;
    fn get_min_sdk_version(&self) -> Result<u16>;
    fn get_screen_layout(&self) -> Result<u8>;
    fn get_ui_mode(&self) -> Result<u8>;
    fn get_smallest_screen(&self) -> Result<u16>;
    fn get_screen_width(&self) -> Result<u16>;
    fn get_screen_height(&self) -> Result<u16>;
    fn get_locale_script(&self) -> Result<Option<String>>;
    fn get_locale_variant(&self) -> Result<Option<String>>;
    fn get_secondary_layout(&self) -> Result<Option<u8>>;
}

#[cfg(test)]
mod tests {
    use model::Identifier;

    #[test]
    fn it_extracts_package_id() {
        assert_eq!(2130837685.get_package(), 127)
    }

    #[test]
    fn it_converts_package_id_if_is_0() {
        assert_eq!(131253.get_package(), 1)
    }

    #[test]
    fn it_extracts_spec_id() {
        assert_eq!(2130837685.get_spec(), 2)
    }

    #[test]
    fn it_extracts_id() {
        assert_eq!(2130837685.get_id(), 181)
    }
}