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
use std::{io::Cursor, string::ToString}; use byteorder::{LittleEndian, ReadBytesExt}; use failure::{bail, ensure, Error}; use crate::model::{owned::ConfigurationBuf, Configuration}; #[derive(Debug)] pub struct ConfigurationWrapper<'a> { slice: &'a [u8], } impl<'a> ConfigurationWrapper<'a> { pub fn new(slice: &'a [u8]) -> Self { Self { slice } } pub fn to_buffer(&self) -> Result<ConfigurationBuf, Error> { ConfigurationBuf::from_cursor(self.slice.into()) } } impl<'a> Configuration for ConfigurationWrapper<'a> { fn get_size(&self) -> Result<u32, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(0); Ok(cursor.read_u32::<LittleEndian>()?) } fn get_mcc(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(4); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_mnc(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(6); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_language(&self) -> Result<String, Error> { let lang_low = self.slice[8]; let lang_high = self.slice[9]; let region = Region::from((lang_low, lang_high)); Ok(region.to_string()) } fn get_region(&self) -> Result<String, Error> { let lang_low = self.slice[10]; let lang_high = self.slice[11]; let region = Region::from((lang_low, lang_high)); Ok(region.to_string()) } fn get_orientation(&self) -> Result<u8, Error> { Ok(self.slice[12]) } fn get_touchscreen(&self) -> Result<u8, Error> { Ok(self.slice[13]) } fn get_density(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(14); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_keyboard(&self) -> Result<u8, Error> { Ok(self.slice[16]) } fn get_navigation(&self) -> Result<u8, Error> { Ok(self.slice[17]) } fn get_input_flags(&self) -> Result<u8, Error> { Ok(self.slice[18]) } fn get_width(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(20); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_height(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(22); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_sdk_version(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(24); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_min_sdk_version(&self) -> Result<u16, Error> { let mut cursor = Cursor::new(self.slice); cursor.set_position(26); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_screen_layout(&self) -> Result<u8, Error> { let size = self.get_size()?; ensure!(size >= 28, "not enough bytes to retrieve the field"); Ok(self.slice[28]) } fn get_ui_mode(&self) -> Result<u8, Error> { let size = self.get_size()?; ensure!(size >= 29, "not enough bytes to retrieve the field"); Ok(self.slice[29]) } fn get_smallest_screen(&self) -> Result<u16, Error> { let size = self.get_size()?; ensure!(size >= 30, "not enough bytes to retrieve the field"); let mut cursor = Cursor::new(self.slice); cursor.set_position(30); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_screen_width(&self) -> Result<u16, Error> { let size = self.get_size()?; ensure!(size >= 32, "not enough bytes to retrieve the field"); let mut cursor = Cursor::new(self.slice); cursor.set_position(32); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_screen_height(&self) -> Result<u16, Error> { let size = self.get_size()?; ensure!(size >= 34, "not enough bytes to retrieve the field"); let mut cursor = Cursor::new(self.slice); cursor.set_position(34); Ok(cursor.read_u16::<LittleEndian>()?) } fn get_locale_script(&self) -> Result<Option<String>, Error> { bail!("not enough bytes to retrieve the field") } fn get_locale_variant(&self) -> Result<Option<String>, Error> { bail!("not enough bytes to retrieve the field") } fn get_secondary_layout(&self) -> Result<Option<u8>, Error> { bail!("not enough bytes to retrieve the field") } } #[derive(Default, Debug, Copy, Clone)] pub struct Region { low: u8, high: u8, } impl Into<(u8, u8)> for Region { fn into(self) -> (u8, u8) { (self.low, self.high) } } impl<'a> From<&'a [u8]> for Region { fn from(input: &'a [u8]) -> Self { if let [low, high] = *input { Self { low, high } } else { Self::default() } } } impl From<(u8, u8)> for Region { fn from(input: (u8, u8)) -> Self { Self { low: input.0, high: input.1, } } } impl ToString for Region { fn to_string(&self) -> String { let mut chrs = Vec::new(); if self.low == 0 && self.high == 0 { return "any".to_owned(); } chrs.push(self.low); chrs.push(self.high); String::from_utf8(chrs).unwrap_or_else(|_| String::new()) } } #[cfg(test)] mod tests { use super::{Configuration, ConfigurationWrapper, Region, ToString}; use crate::raw_chunks::EXAMPLE_CONFIGURATION; #[test] fn it_can_encode_bytes_region_to_string() { let region = Region::from((99, 97)); assert_eq!("ca", region.to_string()); } #[test] fn it_can_encode_bytes_region_to_string_any() { let region = Region::from((0, 0)); assert_eq!("any", region.to_string()); } #[test] fn it_can_encode_bytes_region_from_string() { let region = Region::from("ps".as_ref()); let (low, high) = region.into(); assert_eq!(112, low); assert_eq!(115, high); } #[test] fn it_can_encode_bytes_region_from_string_any() { let region = Region::from("any".as_ref()); let (low, high) = region.into(); assert_eq!(0, low); assert_eq!(0, high); } #[test] fn it_can_decode_a_full_configuration_slice() { let wrapper = ConfigurationWrapper::new(EXAMPLE_CONFIGURATION); assert_eq!(56, wrapper.get_size().unwrap()); assert_eq!(310, wrapper.get_mcc().unwrap()); assert_eq!(800, wrapper.get_mnc().unwrap()); assert_eq!("bs", wrapper.get_language().unwrap()); assert_eq!("BA", wrapper.get_region().unwrap()); assert_eq!(0, wrapper.get_orientation().unwrap()); assert_eq!(0, wrapper.get_touchscreen().unwrap()); assert_eq!(0, wrapper.get_density().unwrap()); assert_eq!(0, wrapper.get_keyboard().unwrap()); assert_eq!(0, wrapper.get_keyboard().unwrap()); assert_eq!(0, wrapper.get_width().unwrap()); assert_eq!(0, wrapper.get_height().unwrap()); } }