Debian Codegraph @

lib/csharp/MonoExample/NDesk-dbus/MessageReader.cs

raw
1 // Copyright 2006 Alp Toker <[email protected]>
2 // This software is made available under the MIT License
3 // See COPYING for details
4
5 using System;
6 using System.Text;
7 using System.Collections.Generic;
8 using System.IO;
9 using System.Reflection;
10
11 namespace NDesk.DBus
12 {
13 class MessageReader
14 {
15 protected EndianFlag endianness;
16 protected byte[] data;
17 //TODO: this should be uint or long to handle long messages
18 protected int pos = 0;
19 protected Message message;
20
21 public MessageReader (EndianFlag endianness, byte[] data)
22 {
23 if (data == null)
24 throw new ArgumentNullException ("data");
25
26 this.endianness = endianness;
27 this.data = data;
28 }
29
30 public MessageReader (Message message) : this (message.Header.Endianness, message.Body)
31 {
32 if (message == null)
33 throw new ArgumentNullException ("message");
34
35 this.message = message;
36 }
37
38 public object ReadValue (Type type)
39 {
40 if (type == typeof (void))
41 return null;
42
43 if (type.IsArray) {
44 return ReadArray (type.GetElementType ());
45 } else if (type == typeof (ObjectPath)) {
46 return ReadObjectPath ();
47 } else if (type == typeof (Signature)) {
48 return ReadSignature ();
49 } else if (type == typeof (object)) {
50 return ReadVariant ();
51 } else if (type == typeof (string)) {
52 return ReadString ();
53 } else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (IDictionary<,>)) {
54 Type[] genArgs = type.GetGenericArguments ();
55 //Type dictType = typeof (Dictionary<,>).MakeGenericType (genArgs);
56 //workaround for Mono bug #81035 (memory leak)
57 Type dictType = Mapper.GetGenericType (typeof (Dictionary<,>), genArgs);
58 System.Collections.IDictionary idict = (System.Collections.IDictionary)Activator.CreateInstance(dictType, new object[0]);
59 GetValueToDict (genArgs[0], genArgs[1], idict);
60 return idict;
61 } else if (Mapper.IsPublic (type)) {
62 return GetObject (type);
63 } else if (!type.IsPrimitive && !type.IsEnum) {
64 return ReadStruct (type);
65 } else {
66 object val;
67 DType dtype = Signature.TypeToDType (type);
68 val = ReadValue (dtype);
69
70 if (type.IsEnum)
71 val = Enum.ToObject (type, val);
72
73 return val;
74 }
75 }
76
77 //helper method, should not be used generally
78 public object ReadValue (DType dtype)
79 {
80 switch (dtype)
81 {
82 case DType.Byte:
83 return ReadByte ();
84
85 case DType.Boolean:
86 return ReadBoolean ();
87
88 case DType.Int16:
89 return ReadInt16 ();
90
91 case DType.UInt16:
92 return ReadUInt16 ();
93
94 case DType.Int32:
95 return ReadInt32 ();
96
97 case DType.UInt32:
98 return ReadUInt32 ();
99
100 case DType.Int64:
101 return ReadInt64 ();
102
103 case DType.UInt64:
104 return ReadUInt64 ();
105
106 #if !DISABLE_SINGLE
107 case DType.Single:
108 return ReadSingle ();
109 #endif
110
111 case DType.Double:
112 return ReadDouble ();
113
114 case DType.String:
115 return ReadString ();
116
117 case DType.ObjectPath:
118 return ReadObjectPath ();
119
120 case DType.Signature:
121 return ReadSignature ();
122
123 case DType.Variant:
124 return ReadVariant ();
125
126 default:
127 throw new Exception ("Unhandled D-Bus type: " + dtype);
128 }
129 }
130
131 public object GetObject (Type type)
132 {
133 ObjectPath path = ReadObjectPath ();
134
135 return message.Connection.GetObject (type, (string)message.Header.Fields[FieldCode.Sender], path);
136 }
137
138 public byte ReadByte ()
139 {
140 return data[pos++];
141 }
142
143 public bool ReadBoolean ()
144 {
145 uint intval = ReadUInt32 ();
146
147 switch (intval) {
148 case 0:
149 return false;
150 case 1:
151 return true;
152 default:
153 throw new Exception ("Read value " + intval + " at position " + pos + " while expecting boolean (0/1)");
154 }
155 }
156
157 unsafe protected void MarshalUShort (byte *dst)
158 {
159 ReadPad (2);
160
161 if (endianness == Connection.NativeEndianness) {
162 dst[0] = data[pos + 0];
163 dst[1] = data[pos + 1];
164 } else {
165 dst[0] = data[pos + 1];
166 dst[1] = data[pos + 0];
167 }
168
169 pos += 2;
170 }
171
172 unsafe public short ReadInt16 ()
173 {
174 short val;
175
176 MarshalUShort ((byte*)&val);
177
178 return val;
179 }
180
181 unsafe public ushort ReadUInt16 ()
182 {
183 ushort val;
184
185 MarshalUShort ((byte*)&val);
186
187 return val;
188 }
189
190 unsafe protected void MarshalUInt (byte *dst)
191 {
192 ReadPad (4);
193
194 if (endianness == Connection.NativeEndianness) {
195 dst[0] = data[pos + 0];
196 dst[1] = data[pos + 1];
197 dst[2] = data[pos + 2];
198 dst[3] = data[pos + 3];
199 } else {
200 dst[0] = data[pos + 3];
201 dst[1] = data[pos + 2];
202 dst[2] = data[pos + 1];
203 dst[3] = data[pos + 0];
204 }
205
206 pos += 4;
207 }
208
209 unsafe public int ReadInt32 ()
210 {
211 int val;
212
213 MarshalUInt ((byte*)&val);
214
215 return val;
216 }
217
218 unsafe public uint ReadUInt32 ()
219 {
220 uint val;
221
222 MarshalUInt ((byte*)&val);
223
224 return val;
225 }
226
227 unsafe protected void MarshalULong (byte *dst)
228 {
229 ReadPad (8);
230
231 if (endianness == Connection.NativeEndianness) {
232 for (int i = 0; i < 8; ++i)
233 dst[i] = data[pos + i];
234 } else {
235 for (int i = 0; i < 8; ++i)
236 dst[i] = data[pos + (7 - i)];
237 }
238
239 pos += 8;
240 }
241
242 unsafe public long ReadInt64 ()
243 {
244 long val;
245
246 MarshalULong ((byte*)&val);
247
248 return val;
249 }
250
251 unsafe public ulong ReadUInt64 ()
252 {
253 ulong val;
254
255 MarshalULong ((byte*)&val);
256
257 return val;
258 }
259
260 #if !DISABLE_SINGLE
261 unsafe public float ReadSingle ()
262 {
263 float val;
264
265 MarshalUInt ((byte*)&val);
266
267 return val;
268 }
269 #endif
270
271 unsafe public double ReadDouble ()
272 {
273 double val;
274
275 MarshalULong ((byte*)&val);
276
277 return val;
278 }
279
280 public string ReadString ()
281 {
282 uint ln = ReadUInt32 ();
283
284 string val = Encoding.UTF8.GetString (data, pos, (int)ln);
285 pos += (int)ln;
286 ReadNull ();
287
288 return val;
289 }
290
291 public ObjectPath ReadObjectPath ()
292 {
293 //exactly the same as string
294 return new ObjectPath (ReadString ());
295 }
296
297 public Signature ReadSignature ()
298 {
299 byte ln = ReadByte ();
300
301 if (ln > Protocol.MaxSignatureLength)
302 throw new Exception ("Signature length " + ln + " exceeds maximum allowed " + Protocol.MaxSignatureLength + " bytes");
303
304 byte[] sigData = new byte[ln];
305 Array.Copy (data, pos, sigData, 0, (int)ln);
306 pos += (int)ln;
307 ReadNull ();
308
309 return new Signature (sigData);
310 }
311
312 public object ReadVariant ()
313 {
314 return ReadVariant (ReadSignature ());
315 }
316
317 object ReadVariant (Signature sig)
318 {
319 return ReadValue (sig.ToType ());
320 }
321
322 //not pretty or efficient but works
323 public void GetValueToDict (Type keyType, Type valType, System.Collections.IDictionary val)
324 {
325 uint ln = ReadUInt32 ();
326
327 if (ln > Protocol.MaxArrayLength)
328 throw new Exception ("Dict length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
329
330 //advance to the alignment of the element
331 //ReadPad (Protocol.GetAlignment (Signature.TypeToDType (type)));
332 ReadPad (8);
333
334 int endPos = pos + (int)ln;
335
336 //while (stream.Position != endPos)
337 while (pos < endPos)
338 {
339 ReadPad (8);
340
341 val.Add (ReadValue (keyType), ReadValue (valType));
342 }
343
344 if (pos != endPos)
345 throw new Exception ("Read pos " + pos + " != ep " + endPos);
346 }
347
348 //this could be made generic to avoid boxing
349 public Array ReadArray (Type elemType)
350 {
351 uint ln = ReadUInt32 ();
352
353 if (ln > Protocol.MaxArrayLength)
354 throw new Exception ("Array length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
355
356 //TODO: more fast paths for primitive arrays
357 if (elemType == typeof (byte)) {
358 byte[] valb = new byte[ln];
359 Array.Copy (data, pos, valb, 0, (int)ln);
360 pos += (int)ln;
361 return valb;
362 }
363
364 //advance to the alignment of the element
365 ReadPad (Protocol.GetAlignment (Signature.TypeToDType (elemType)));
366
367 int endPos = pos + (int)ln;
368
369 //List<T> vals = new List<T> ();
370 System.Collections.ArrayList vals = new System.Collections.ArrayList ();
371
372 //while (stream.Position != endPos)
373 while (pos < endPos)
374 vals.Add (ReadValue (elemType));
375
376 if (pos != endPos)
377 throw new Exception ("Read pos " + pos + " != ep " + endPos);
378
379 return vals.ToArray (elemType);
380 }
381
382 //struct
383 //probably the wrong place for this
384 //there might be more elegant solutions
385 public object ReadStruct (Type type)
386 {
387 ReadPad (8);
388
389 object val = Activator.CreateInstance (type);
390
391 /*
392 if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof (KeyValuePair<,>)) {
393 object elem;
394
395 System.Reflection.PropertyInfo key_prop = type.GetProperty ("Key");
396 GetValue (key_prop.PropertyType, out elem);
397 key_prop.SetValue (val, elem, null);
398
399 System.Reflection.PropertyInfo val_prop = type.GetProperty ("Value");
400 GetValue (val_prop.PropertyType, out elem);
401 val_prop.SetValue (val, elem, null);
402
403 return;
404 }
405 */
406
407 FieldInfo[] fis = type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
408
409 foreach (System.Reflection.FieldInfo fi in fis)
410 fi.SetValue (val, ReadValue (fi.FieldType));
411
412 return val;
413 }
414
415 public void ReadNull ()
416 {
417 if (data[pos] != 0)
418 throw new Exception ("Read non-zero byte at position " + pos + " while expecting null terminator");
419 pos++;
420 }
421
422 /*
423 public void ReadPad (int alignment)
424 {
425 pos = Protocol.Padded (pos, alignment);
426 }
427 */
428
429 public void ReadPad (int alignment)
430 {
431 for (int endPos = Protocol.Padded (pos, alignment) ; pos != endPos ; pos++)
432 if (data[pos] != 0)
433 throw new Exception ("Read non-zero byte at position " + pos + " while expecting padding");
434 }
435 }
436 }