| 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 |
|
| 8 |
using System.Collections.Generic; |
| 9 |
//TODO: Reflection should be done at a higher level than this class |
| 10 |
using System.Reflection; |
| 11 |
|
| 12 |
namespace NDesk.DBus |
| 13 |
{ |
| 14 |
//maybe this should be nullable? |
| 15 |
struct Signature |
| 16 |
{ |
| 17 |
//TODO: this class needs some work |
| 18 |
//Data should probably include the null terminator |
| 19 |
|
| 20 |
public static readonly Signature Empty = new Signature (String.Empty); |
| 21 |
|
| 22 |
public static bool operator == (Signature a, Signature b) |
| 23 |
{ |
| 24 |
/* |
| 25 |
//TODO: remove this hack to handle bad case when Data is null |
| 26 |
if (a.data == null || b.data == null) |
| 27 |
throw new Exception ("Encountered Signature with null buffer"); |
| 28 |
*/ |
| 29 |
|
| 30 |
/* |
| 31 |
if (a.data == null && b.data == null) |
| 32 |
return true; |
| 33 |
|
| 34 |
if (a.data == null || b.data == null) |
| 35 |
return false; |
| 36 |
*/ |
| 37 |
|
| 38 |
if (a.data.Length != b.data.Length) |
| 39 |
return false; |
| 40 |
|
| 41 |
for (int i = 0 ; i != a.data.Length ; i++) |
| 42 |
if (a.data[i] != b.data[i]) |
| 43 |
return false; |
| 44 |
|
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
public static bool operator != (Signature a, Signature b) |
| 49 |
{ |
| 50 |
return !(a == b); |
| 51 |
} |
| 52 |
|
| 53 |
public override bool Equals (object o) |
| 54 |
{ |
| 55 |
if (o == null) |
| 56 |
return false; |
| 57 |
|
| 58 |
if (!(o is Signature)) |
| 59 |
return false; |
| 60 |
|
| 61 |
return this == (Signature)o; |
| 62 |
} |
| 63 |
|
| 64 |
public override int GetHashCode () |
| 65 |
{ |
| 66 |
return data.GetHashCode (); |
| 67 |
} |
| 68 |
|
| 69 |
public static Signature operator + (Signature s1, Signature s2) |
| 70 |
{ |
| 71 |
return Concat (s1, s2); |
| 72 |
} |
| 73 |
|
| 74 |
//these need to be optimized |
| 75 |
public static Signature Concat (Signature s1, Signature s2) |
| 76 |
{ |
| 77 |
return new Signature (s1.Value + s2.Value); |
| 78 |
} |
| 79 |
|
| 80 |
public static Signature Copy (Signature sig) |
| 81 |
{ |
| 82 |
return new Signature (sig.data); |
| 83 |
} |
| 84 |
|
| 85 |
public Signature (string value) |
| 86 |
{ |
| 87 |
this.data = Encoding.ASCII.GetBytes (value); |
| 88 |
} |
| 89 |
|
| 90 |
public Signature (byte[] value) |
| 91 |
{ |
| 92 |
this.data = (byte[])value.Clone (); |
| 93 |
} |
| 94 |
|
| 95 |
//this will become obsolete soon |
| 96 |
internal Signature (DType value) |
| 97 |
{ |
| 98 |
this.data = new byte[] {(byte)value}; |
| 99 |
} |
| 100 |
|
| 101 |
internal Signature (DType[] value) |
| 102 |
{ |
| 103 |
this.data = new byte[value.Length]; |
| 104 |
|
| 105 |
/* |
| 106 |
MemoryStream ms = new MemoryStream (this.data); |
| 107 |
|
| 108 |
foreach (DType t in value) |
| 109 |
ms.WriteByte ((byte)t); |
| 110 |
*/ |
| 111 |
|
| 112 |
for (int i = 0 ; i != value.Length ; i++) |
| 113 |
this.data[i] = (byte)value[i]; |
| 114 |
} |
| 115 |
|
| 116 |
byte[] data; |
| 117 |
|
| 118 |
//TODO: this should be private, but MessageWriter and Monitor still use it |
| 119 |
//[Obsolete] |
| 120 |
public byte[] GetBuffer () |
| 121 |
{ |
| 122 |
return data; |
| 123 |
} |
| 124 |
|
| 125 |
internal DType this[int index] |
| 126 |
{ |
| 127 |
get { |
| 128 |
return (DType)data[index]; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public int Length |
| 133 |
{ |
| 134 |
get { |
| 135 |
return data.Length; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
//[Obsolete] |
| 140 |
public string Value |
| 141 |
{ |
| 142 |
get { |
| 143 |
/* |
| 144 |
//FIXME: hack to handle bad case when Data is null |
| 145 |
if (data == null) |
| 146 |
return String.Empty; |
| 147 |
*/ |
| 148 |
|
| 149 |
return Encoding.ASCII.GetString (data); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public override string ToString () |
| 154 |
{ |
| 155 |
return Value; |
| 156 |
|
| 157 |
/* |
| 158 |
StringBuilder sb = new StringBuilder (); |
| 159 |
|
| 160 |
foreach (DType t in data) { |
| 161 |
//we shouldn't rely on object mapping here, but it's an easy way to get string representations for now |
| 162 |
Type type = DTypeToType (t); |
| 163 |
if (type != null) { |
| 164 |
sb.Append (type.Name); |
| 165 |
} else { |
| 166 |
char c = (char)t; |
| 167 |
if (!Char.IsControl (c)) |
| 168 |
sb.Append (c); |
| 169 |
else |
| 170 |
sb.Append (@"\" + (int)c); |
| 171 |
} |
| 172 |
sb.Append (" "); |
| 173 |
} |
| 174 |
|
| 175 |
return sb.ToString (); |
| 176 |
*/ |
| 177 |
} |
| 178 |
|
| 179 |
public Signature MakeArraySignature () |
| 180 |
{ |
| 181 |
return new Signature (DType.Array) + this; |
| 182 |
} |
| 183 |
|
| 184 |
public static Signature MakeStruct (params Signature[] elems) |
| 185 |
{ |
| 186 |
Signature sig = Signature.Empty; |
| 187 |
|
| 188 |
sig += new Signature (DType.StructBegin); |
| 189 |
|
| 190 |
foreach (Signature elem in elems) |
| 191 |
sig += elem; |
| 192 |
|
| 193 |
sig += new Signature (DType.StructEnd); |
| 194 |
|
| 195 |
return sig; |
| 196 |
} |
| 197 |
|
| 198 |
public static Signature MakeDictEntry (Signature keyType, Signature valueType) |
| 199 |
{ |
| 200 |
Signature sig = Signature.Empty; |
| 201 |
|
| 202 |
sig += new Signature (DType.DictEntryBegin); |
| 203 |
|
| 204 |
sig += keyType; |
| 205 |
sig += valueType; |
| 206 |
|
| 207 |
sig += new Signature (DType.DictEntryEnd); |
| 208 |
|
| 209 |
return sig; |
| 210 |
} |
| 211 |
|
| 212 |
public static Signature MakeDict (Signature keyType, Signature valueType) |
| 213 |
{ |
| 214 |
return MakeDictEntry (keyType, valueType).MakeArraySignature (); |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
//TODO: complete this |
| 219 |
public bool IsPrimitive |
| 220 |
{ |
| 221 |
get { |
| 222 |
if (this == Signature.Empty) |
| 223 |
return true; |
| 224 |
|
| 225 |
return false; |
| 226 |
} |
| 227 |
} |
| 228 |
*/ |
| 229 |
|
| 230 |
public bool IsDict |
| 231 |
{ |
| 232 |
get { |
| 233 |
if (Length < 3) |
| 234 |
return false; |
| 235 |
|
| 236 |
if (!IsArray) |
| 237 |
return false; |
| 238 |
|
| 239 |
if (this[2] != DType.DictEntryBegin) |
| 240 |
return false; |
| 241 |
|
| 242 |
return true; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public bool IsArray |
| 247 |
{ |
| 248 |
get { |
| 249 |
if (Length < 2) |
| 250 |
return false; |
| 251 |
|
| 252 |
if (this[0] != DType.Array) |
| 253 |
return false; |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
public Signature GetElementSignature () |
| 260 |
{ |
| 261 |
if (!IsArray) |
| 262 |
throw new Exception ("Cannot get the element signature of a non-array (signature was '" + this + "')"); |
| 263 |
|
| 264 |
//TODO: improve this |
| 265 |
if (Length != 2) |
| 266 |
throw new NotSupportedException ("Parsing signatures with more than one primitive value is not supported (signature was '" + this + "')"); |
| 267 |
|
| 268 |
return new Signature (this[1]); |
| 269 |
} |
| 270 |
|
| 271 |
public Type[] ToTypes () |
| 272 |
{ |
| 273 |
List<Type> types = new List<Type> (); |
| 274 |
for (int i = 0 ; i != data.Length ; types.Add (ToType (ref i))); |
| 275 |
return types.ToArray (); |
| 276 |
} |
| 277 |
|
| 278 |
public Type ToType () |
| 279 |
{ |
| 280 |
int pos = 0; |
| 281 |
Type ret = ToType (ref pos); |
| 282 |
if (pos != data.Length) |
| 283 |
throw new Exception ("Signature '" + Value + "' is not a single complete type"); |
| 284 |
return ret; |
| 285 |
} |
| 286 |
|
| 287 |
internal static DType TypeCodeToDType (TypeCode typeCode) |
| 288 |
{ |
| 289 |
switch (typeCode) |
| 290 |
{ |
| 291 |
case TypeCode.Empty: |
| 292 |
return DType.Invalid; |
| 293 |
case TypeCode.Object: |
| 294 |
return DType.Invalid; |
| 295 |
case TypeCode.DBNull: |
| 296 |
return DType.Invalid; |
| 297 |
case TypeCode.Boolean: |
| 298 |
return DType.Boolean; |
| 299 |
case TypeCode.Char: |
| 300 |
return DType.UInt16; |
| 301 |
case TypeCode.SByte: |
| 302 |
return DType.Byte; |
| 303 |
case TypeCode.Byte: |
| 304 |
return DType.Byte; |
| 305 |
case TypeCode.Int16: |
| 306 |
return DType.Int16; |
| 307 |
case TypeCode.UInt16: |
| 308 |
return DType.UInt16; |
| 309 |
case TypeCode.Int32: |
| 310 |
return DType.Int32; |
| 311 |
case TypeCode.UInt32: |
| 312 |
return DType.UInt32; |
| 313 |
case TypeCode.Int64: |
| 314 |
return DType.Int64; |
| 315 |
case TypeCode.UInt64: |
| 316 |
return DType.UInt64; |
| 317 |
case TypeCode.Single: |
| 318 |
return DType.Single; |
| 319 |
case TypeCode.Double: |
| 320 |
return DType.Double; |
| 321 |
case TypeCode.Decimal: |
| 322 |
return DType.Invalid; |
| 323 |
case TypeCode.DateTime: |
| 324 |
return DType.Invalid; |
| 325 |
case TypeCode.String: |
| 326 |
return DType.String; |
| 327 |
default: |
| 328 |
return DType.Invalid; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
//FIXME: this method is bad, get rid of it |
| 333 |
internal static DType TypeToDType (Type type) |
| 334 |
{ |
| 335 |
if (type == typeof (void)) |
| 336 |
return DType.Invalid; |
| 337 |
|
| 338 |
if (type == typeof (string)) |
| 339 |
return DType.String; |
| 340 |
|
| 341 |
if (type == typeof (ObjectPath)) |
| 342 |
return DType.ObjectPath; |
| 343 |
|
| 344 |
if (type == typeof (Signature)) |
| 345 |
return DType.Signature; |
| 346 |
|
| 347 |
if (type == typeof (object)) |
| 348 |
return DType.Variant; |
| 349 |
|
| 350 |
if (type.IsPrimitive) |
| 351 |
return TypeCodeToDType (Type.GetTypeCode (type)); |
| 352 |
|
| 353 |
if (type.IsEnum) |
| 354 |
return TypeToDType (Enum.GetUnderlyingType (type)); |
| 355 |
|
| 356 |
//needs work |
| 357 |
if (type.IsArray) |
| 358 |
return DType.Array; |
| 359 |
|
| 360 |
//if (type.UnderlyingSystemType != null) |
| 361 |
// return TypeToDType (type.UnderlyingSystemType); |
| 362 |
if (Mapper.IsPublic (type)) |
| 363 |
return DType.ObjectPath; |
| 364 |
|
| 365 |
if (!type.IsPrimitive && !type.IsEnum) |
| 366 |
return DType.Struct; |
| 367 |
|
| 368 |
//TODO: maybe throw an exception here |
| 369 |
return DType.Invalid; |
| 370 |
} |
| 371 |
|
| 372 |
/* |
| 373 |
public static DType TypeToDType (Type type) |
| 374 |
{ |
| 375 |
if (type == null) |
| 376 |
return DType.Invalid; |
| 377 |
else if (type == typeof (byte)) |
| 378 |
return DType.Byte; |
| 379 |
else if (type == typeof (bool)) |
| 380 |
return DType.Boolean; |
| 381 |
else if (type == typeof (short)) |
| 382 |
return DType.Int16; |
| 383 |
else if (type == typeof (ushort)) |
| 384 |
return DType.UInt16; |
| 385 |
else if (type == typeof (int)) |
| 386 |
return DType.Int32; |
| 387 |
else if (type == typeof (uint)) |
| 388 |
return DType.UInt32; |
| 389 |
else if (type == typeof (long)) |
| 390 |
return DType.Int64; |
| 391 |
else if (type == typeof (ulong)) |
| 392 |
return DType.UInt64; |
| 393 |
else if (type == typeof (float)) //not supported by libdbus at time of writing |
| 394 |
return DType.Single; |
| 395 |
else if (type == typeof (double)) |
| 396 |
return DType.Double; |
| 397 |
else if (type == typeof (string)) |
| 398 |
return DType.String; |
| 399 |
else if (type == typeof (ObjectPath)) |
| 400 |
return DType.ObjectPath; |
| 401 |
else if (type == typeof (Signature)) |
| 402 |
return DType.Signature; |
| 403 |
else |
| 404 |
return DType.Invalid; |
| 405 |
} |
| 406 |
*/ |
| 407 |
|
| 408 |
public Type ToType (ref int pos) |
| 409 |
{ |
| 410 |
DType dtype = (DType)data[pos++]; |
| 411 |
|
| 412 |
switch (dtype) { |
| 413 |
case DType.Invalid: |
| 414 |
return typeof (void); |
| 415 |
case DType.Byte: |
| 416 |
return typeof (byte); |
| 417 |
case DType.Boolean: |
| 418 |
return typeof (bool); |
| 419 |
case DType.Int16: |
| 420 |
return typeof (short); |
| 421 |
case DType.UInt16: |
| 422 |
return typeof (ushort); |
| 423 |
case DType.Int32: |
| 424 |
return typeof (int); |
| 425 |
case DType.UInt32: |
| 426 |
return typeof (uint); |
| 427 |
case DType.Int64: |
| 428 |
return typeof (long); |
| 429 |
case DType.UInt64: |
| 430 |
return typeof (ulong); |
| 431 |
case DType.Single: ////not supported by libdbus at time of writing |
| 432 |
return typeof (float); |
| 433 |
case DType.Double: |
| 434 |
return typeof (double); |
| 435 |
case DType.String: |
| 436 |
return typeof (string); |
| 437 |
case DType.ObjectPath: |
| 438 |
return typeof (ObjectPath); |
| 439 |
case DType.Signature: |
| 440 |
return typeof (Signature); |
| 441 |
case DType.Array: |
| 442 |
//peek to see if this is in fact a dictionary |
| 443 |
if ((DType)data[pos] == DType.DictEntryBegin) { |
| 444 |
//skip over the { |
| 445 |
pos++; |
| 446 |
Type keyType = ToType (ref pos); |
| 447 |
Type valueType = ToType (ref pos); |
| 448 |
//skip over the } |
| 449 |
pos++; |
| 450 |
//return typeof (IDictionary<,>).MakeGenericType (new Type[] {keyType, valueType}); |
| 451 |
//workaround for Mono bug #81035 (memory leak) |
| 452 |
return Mapper.GetGenericType (typeof (IDictionary<,>), new Type[] {keyType, valueType}); |
| 453 |
} else { |
| 454 |
return ToType (ref pos).MakeArrayType (); |
| 455 |
} |
| 456 |
case DType.Struct: |
| 457 |
return typeof (ValueType); |
| 458 |
case DType.DictEntry: |
| 459 |
return typeof (System.Collections.Generic.KeyValuePair<,>); |
| 460 |
case DType.Variant: |
| 461 |
return typeof (object); |
| 462 |
default: |
| 463 |
throw new NotSupportedException ("Parsing or converting this signature is not yet supported (signature was '" + this + "'), at DType." + dtype); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
public static Signature GetSig (object[] objs) |
| 468 |
{ |
| 469 |
return GetSig (Type.GetTypeArray (objs)); |
| 470 |
} |
| 471 |
|
| 472 |
public static Signature GetSig (Type[] types) |
| 473 |
{ |
| 474 |
if (types == null) |
| 475 |
throw new ArgumentNullException ("types"); |
| 476 |
|
| 477 |
Signature sig = Signature.Empty; |
| 478 |
|
| 479 |
foreach (Type type in types) |
| 480 |
sig += GetSig (type); |
| 481 |
|
| 482 |
return sig; |
| 483 |
} |
| 484 |
|
| 485 |
public static Signature GetSig (Type type) |
| 486 |
{ |
| 487 |
if (type == null) |
| 488 |
throw new ArgumentNullException ("type"); |
| 489 |
|
| 490 |
//this is inelegant, but works for now |
| 491 |
if (type == typeof (Signature)) |
| 492 |
return new Signature (DType.Signature); |
| 493 |
|
| 494 |
if (type == typeof (ObjectPath)) |
| 495 |
return new Signature (DType.ObjectPath); |
| 496 |
|
| 497 |
if (type == typeof (void)) |
| 498 |
return Signature.Empty; |
| 499 |
|
| 500 |
if (type == typeof (string)) |
| 501 |
return new Signature (DType.String); |
| 502 |
|
| 503 |
if (type == typeof (object)) |
| 504 |
return new Signature (DType.Variant); |
| 505 |
|
| 506 |
if (type.IsArray) |
| 507 |
return GetSig (type.GetElementType ()).MakeArraySignature (); |
| 508 |
|
| 509 |
if (type.IsGenericType && (type.GetGenericTypeDefinition () == typeof (IDictionary<,>) || type.GetGenericTypeDefinition () == typeof (Dictionary<,>))) { |
| 510 |
|
| 511 |
Type[] genArgs = type.GetGenericArguments (); |
| 512 |
return Signature.MakeDict (GetSig (genArgs[0]), GetSig (genArgs[1])); |
| 513 |
} |
| 514 |
|
| 515 |
if (Mapper.IsPublic (type)) { |
| 516 |
return new Signature (DType.ObjectPath); |
| 517 |
} |
| 518 |
|
| 519 |
if (!type.IsPrimitive && !type.IsEnum) { |
| 520 |
Signature sig = Signature.Empty; |
| 521 |
|
| 522 |
foreach (FieldInfo fi in type.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) |
| 523 |
sig += GetSig (fi.FieldType); |
| 524 |
|
| 525 |
return Signature.MakeStruct (sig); |
| 526 |
} |
| 527 |
|
| 528 |
DType dtype = Signature.TypeToDType (type); |
| 529 |
return new Signature (dtype); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
enum ArgDirection |
| 534 |
{ |
| 535 |
In, |
| 536 |
Out, |
| 537 |
} |
| 538 |
|
| 539 |
enum DType : byte |
| 540 |
{ |
| 541 |
Invalid = (byte)'\0', |
| 542 |
|
| 543 |
Byte = (byte)'y', |
| 544 |
Boolean = (byte)'b', |
| 545 |
Int16 = (byte)'n', |
| 546 |
UInt16 = (byte)'q', |
| 547 |
Int32 = (byte)'i', |
| 548 |
UInt32 = (byte)'u', |
| 549 |
Int64 = (byte)'x', |
| 550 |
UInt64 = (byte)'t', |
| 551 |
Single = (byte)'f', //This is not yet supported! |
| 552 |
Double = (byte)'d', |
| 553 |
String = (byte)'s', |
| 554 |
ObjectPath = (byte)'o', |
| 555 |
Signature = (byte)'g', |
| 556 |
|
| 557 |
Array = (byte)'a', |
| 558 |
//TODO: remove Struct and DictEntry -- they are not relevant to wire protocol |
| 559 |
Struct = (byte)'r', |
| 560 |
DictEntry = (byte)'e', |
| 561 |
Variant = (byte)'v', |
| 562 |
|
| 563 |
StructBegin = (byte)'(', |
| 564 |
StructEnd = (byte)')', |
| 565 |
DictEntryBegin = (byte)'{', |
| 566 |
DictEntryEnd = (byte)'}', |
| 567 |
} |
| 568 |
} |