1 // Copyright 2017, Igor Shaula
2 // Licensed under the MIT License <LICENSE or
3 // http://opensource.org/licenses/MIT>. This file
4 // may not be copied, modified, or distributed
5 // except according to those terms.
6 use super::super::FromRegValue;
7 use super::{
8     DecodeResult, Decoder, DecoderEnumerationState, DecoderError, DecoderReadingState, DECODER_SAM,
9 };
10 use serde::de::*;
11 use std::fmt;
12 use std::mem;
13 
14 impl Error for DecoderError {
custom<T: fmt::Display>(msg: T) -> Self15     fn custom<T: fmt::Display>(msg: T) -> Self {
16         DecoderError::DeserializerError(format!("{}", msg))
17     }
18 }
19 
20 impl<'de, 'a> Deserializer<'de> for &'a mut Decoder {
21     type Error = DecoderError;
deserialize_any<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,22     fn deserialize_any<V>(self, visitor: V) -> DecodeResult<V::Value>
23     where
24         V: Visitor<'de>,
25     {
26         use self::DecoderEnumerationState::*;
27         match self.enumeration_state {
28             EnumeratingKeys(..) => no_impl!("deserialize_any for keys"),
29             EnumeratingValues(..) => {
30                 let s = self.f_name.as_ref().ok_or(DecoderError::NoFieldName)?;
31                 let v = self.key.get_raw_value(s)?;
32                 use RegType::*;
33                 match v.vtype {
34                     REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => {
35                         visitor.visit_string(String::from_reg_value(&v)?)
36                     }
37                     REG_DWORD => visitor.visit_u32(u32::from_reg_value(&v)?),
38                     REG_QWORD => visitor.visit_u64(u64::from_reg_value(&v)?),
39                     _ => Err(DecoderError::DecodeNotImplemented(
40                         "value type deserialization not implemented".to_owned(),
41                     )),
42                 }
43             }
44         }
45     }
46 
deserialize_bool<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,47     fn deserialize_bool<V>(self, visitor: V) -> DecodeResult<V::Value>
48     where
49         V: Visitor<'de>,
50     {
51         visitor.visit_bool(read_value!(self).map(|v: u32| v > 0)?)
52     }
53 
deserialize_u8<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,54     fn deserialize_u8<V>(self, visitor: V) -> DecodeResult<V::Value>
55     where
56         V: Visitor<'de>,
57     {
58         self.deserialize_u32(visitor)
59     }
60 
deserialize_u16<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,61     fn deserialize_u16<V>(self, visitor: V) -> DecodeResult<V::Value>
62     where
63         V: Visitor<'de>,
64     {
65         self.deserialize_u32(visitor)
66     }
67 
deserialize_u32<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,68     fn deserialize_u32<V>(self, visitor: V) -> DecodeResult<V::Value>
69     where
70         V: Visitor<'de>,
71     {
72         visitor.visit_u32(read_value!(self)?)
73     }
74 
deserialize_u64<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,75     fn deserialize_u64<V>(self, visitor: V) -> DecodeResult<V::Value>
76     where
77         V: Visitor<'de>,
78     {
79         visitor.visit_u64(read_value!(self)?)
80     }
81 
deserialize_i8<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,82     fn deserialize_i8<V>(self, visitor: V) -> DecodeResult<V::Value>
83     where
84         V: Visitor<'de>,
85     {
86         visitor.visit_i8(parse_string!(self)?)
87     }
88 
deserialize_i16<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,89     fn deserialize_i16<V>(self, visitor: V) -> DecodeResult<V::Value>
90     where
91         V: Visitor<'de>,
92     {
93         visitor.visit_i16(parse_string!(self)?)
94     }
95 
deserialize_i32<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,96     fn deserialize_i32<V>(self, visitor: V) -> DecodeResult<V::Value>
97     where
98         V: Visitor<'de>,
99     {
100         visitor.visit_i32(parse_string!(self)?)
101     }
102 
deserialize_i64<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,103     fn deserialize_i64<V>(self, visitor: V) -> DecodeResult<V::Value>
104     where
105         V: Visitor<'de>,
106     {
107         visitor.visit_i64(parse_string!(self)?)
108     }
109 
deserialize_f32<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,110     fn deserialize_f32<V>(self, visitor: V) -> DecodeResult<V::Value>
111     where
112         V: Visitor<'de>,
113     {
114         visitor.visit_f32(parse_string!(self)?)
115     }
116 
deserialize_f64<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,117     fn deserialize_f64<V>(self, visitor: V) -> DecodeResult<V::Value>
118     where
119         V: Visitor<'de>,
120     {
121         visitor.visit_f64(parse_string!(self)?)
122     }
123 
deserialize_char<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,124     fn deserialize_char<V>(self, _visitor: V) -> DecodeResult<V::Value>
125     where
126         V: Visitor<'de>,
127     {
128         no_impl!("deserialize_char")
129     }
130 
deserialize_str<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,131     fn deserialize_str<V>(self, _visitor: V) -> DecodeResult<V::Value>
132     where
133         V: Visitor<'de>,
134     {
135         no_impl!("deserialize_str")
136     }
137 
deserialize_string<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,138     fn deserialize_string<V>(self, visitor: V) -> DecodeResult<V::Value>
139     where
140         V: Visitor<'de>,
141     {
142         use self::DecoderReadingState::*;
143         match self.reading_state {
144             WaitingForKey => {
145                 let s = self.f_name.as_ref().ok_or(DecoderError::NoFieldName)?;
146                 visitor.visit_string(s.clone())
147             }
148             WaitingForValue => visitor.visit_string(read_value!(self)?),
149         }
150     }
151 
deserialize_bytes<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,152     fn deserialize_bytes<V>(self, _visitor: V) -> DecodeResult<V::Value>
153     where
154         V: Visitor<'de>,
155     {
156         no_impl!("deserialize_bytes")
157     }
158 
deserialize_byte_buf<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,159     fn deserialize_byte_buf<V>(self, _visitor: V) -> DecodeResult<V::Value>
160     where
161         V: Visitor<'de>,
162     {
163         no_impl!("deserialize_byte_buf")
164     }
165 
deserialize_option<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,166     fn deserialize_option<V>(self, visitor: V) -> DecodeResult<V::Value>
167     where
168         V: Visitor<'de>,
169     {
170         let v = {
171             let s = self.f_name.as_ref().ok_or(DecoderError::NoFieldName)?;
172             self.key.get_raw_value(s)
173         };
174         match v {
175             Ok(..) => visitor.visit_some(&mut *self),
176             Err(..) => visitor.visit_none(),
177         }
178     }
179 
deserialize_unit<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,180     fn deserialize_unit<V>(self, _visitor: V) -> DecodeResult<V::Value>
181     where
182         V: Visitor<'de>,
183     {
184         no_impl!("deserialize_unit")
185     }
186 
deserialize_unit_struct<V>(self, _name: &'static str, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,187     fn deserialize_unit_struct<V>(self, _name: &'static str, _visitor: V) -> DecodeResult<V::Value>
188     where
189         V: Visitor<'de>,
190     {
191         no_impl!("deserialize_unit_struct")
192     }
193 
deserialize_newtype_struct<V>( self, _name: &'static str, _visitor: V, ) -> DecodeResult<V::Value> where V: Visitor<'de>,194     fn deserialize_newtype_struct<V>(
195         self,
196         _name: &'static str,
197         _visitor: V,
198     ) -> DecodeResult<V::Value>
199     where
200         V: Visitor<'de>,
201     {
202         no_impl!("deserialize_newtype_struct")
203     }
204 
deserialize_seq<V>(self, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,205     fn deserialize_seq<V>(self, _visitor: V) -> DecodeResult<V::Value>
206     where
207         V: Visitor<'de>,
208     {
209         no_impl!("deserialize_seq")
210     }
211 
deserialize_tuple<V>(self, _len: usize, _visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,212     fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> DecodeResult<V::Value>
213     where
214         V: Visitor<'de>,
215     {
216         no_impl!("deserialize_tuple")
217     }
218 
deserialize_tuple_struct<V>( self, _name: &'static str, _len: usize, _visitor: V, ) -> DecodeResult<V::Value> where V: Visitor<'de>,219     fn deserialize_tuple_struct<V>(
220         self,
221         _name: &'static str,
222         _len: usize,
223         _visitor: V,
224     ) -> DecodeResult<V::Value>
225     where
226         V: Visitor<'de>,
227     {
228         no_impl!("deserialize_tuple_struct")
229     }
230 
deserialize_map<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,231     fn deserialize_map<V>(self, visitor: V) -> DecodeResult<V::Value>
232     where
233         V: Visitor<'de>,
234     {
235         visitor.visit_map(self)
236     }
237 
deserialize_struct<V>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> DecodeResult<V::Value> where V: Visitor<'de>,238     fn deserialize_struct<V>(
239         self,
240         _name: &'static str,
241         _fields: &'static [&'static str],
242         visitor: V,
243     ) -> DecodeResult<V::Value>
244     where
245         V: Visitor<'de>,
246     {
247         visitor.visit_map(self)
248     }
249 
deserialize_identifier<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,250     fn deserialize_identifier<V>(self, visitor: V) -> DecodeResult<V::Value>
251     where
252         V: Visitor<'de>,
253     {
254         self.deserialize_string(visitor)
255     }
256 
deserialize_enum<V>( self, _name: &'static str, _variants: &'static [&'static str], _visitor: V, ) -> DecodeResult<V::Value> where V: Visitor<'de>,257     fn deserialize_enum<V>(
258         self,
259         _name: &'static str,
260         _variants: &'static [&'static str],
261         _visitor: V,
262     ) -> DecodeResult<V::Value>
263     where
264         V: Visitor<'de>,
265     {
266         no_impl!("deserialize_enum")
267     }
268 
deserialize_ignored_any<V>(self, visitor: V) -> DecodeResult<V::Value> where V: Visitor<'de>,269     fn deserialize_ignored_any<V>(self, visitor: V) -> DecodeResult<V::Value>
270     where
271         V: Visitor<'de>,
272     {
273         self.deserialize_any(visitor)
274     }
275 }
276 
277 impl<'de, 'a> MapAccess<'de> for Decoder {
278     type Error = DecoderError;
next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> where K: DeserializeSeed<'de>,279     fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
280     where
281         K: DeserializeSeed<'de>,
282     {
283         self.reading_state = DecoderReadingState::WaitingForKey;
284         use self::DecoderEnumerationState::*;
285         match self.enumeration_state {
286             EnumeratingKeys(index) => match self.key.enum_key(index) {
287                 Some(res) => {
288                     self.f_name = Some(res?);
289                     self.enumeration_state = EnumeratingKeys(index + 1);
290                     seed.deserialize(&mut *self).map(Some)
291                 }
292                 None => {
293                     self.enumeration_state = EnumeratingValues(0);
294                     self.next_key_seed(seed)
295                 }
296             },
297             EnumeratingValues(index) => {
298                 let next_value = self.key.enum_value(index);
299                 match next_value {
300                     Some(res) => {
301                         self.f_name = Some(res?.0);
302                         self.enumeration_state = EnumeratingValues(index + 1);
303                         seed.deserialize(&mut *self).map(Some)
304                     }
305                     None => Ok(None),
306                 }
307             }
308         }
309     }
310 
next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> where V: DeserializeSeed<'de>,311     fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
312     where
313         V: DeserializeSeed<'de>,
314     {
315         self.reading_state = DecoderReadingState::WaitingForValue;
316         use self::DecoderEnumerationState::*;
317         match self.enumeration_state {
318             EnumeratingKeys(..) => {
319                 let f_name = self.f_name.as_ref().ok_or(DecoderError::NoFieldName)?;
320                 match self.key.open_subkey_with_flags(f_name, DECODER_SAM) {
321                     Ok(subkey) => {
322                         let mut nested = Decoder::new(subkey);
323                         seed.deserialize(&mut nested)
324                     }
325                     Err(err) => Err(DecoderError::IoError(err)),
326                 }
327             }
328             EnumeratingValues(..) => seed.deserialize(&mut *self),
329         }
330     }
331 }
332