BclHelpers.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using System;
  2. using System.Reflection;
  3. namespace ProtoBuf
  4. {
  5. internal enum TimeSpanScale
  6. {
  7. Days = 0,
  8. Hours = 1,
  9. Minutes = 2,
  10. Seconds = 3,
  11. Milliseconds = 4,
  12. Ticks = 5,
  13. MinMax = 15
  14. }
  15. /// <summary>
  16. /// Provides support for common .NET types that do not have a direct representation
  17. /// in protobuf, using the definitions from bcl.proto
  18. /// </summary>
  19. public
  20. #if FX11
  21. sealed
  22. #else
  23. static
  24. #endif
  25. class BclHelpers
  26. {
  27. /// <summary>
  28. /// Creates a new instance of the specified type, bypassing the constructor.
  29. /// </summary>
  30. /// <param name="type">The type to create</param>
  31. /// <returns>The new instance</returns>
  32. /// <exception cref="NotSupportedException">If the platform does not support constructor-skipping</exception>
  33. public static object GetUninitializedObject(Type type)
  34. {
  35. #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8)
  36. return System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type);
  37. #else
  38. throw new NotSupportedException("Constructor-skipping is not supported on this platform");
  39. #endif
  40. }
  41. #if FX11
  42. private BclHelpers() { } // not a static class for C# 1.2 reasons
  43. #endif
  44. const int FieldTimeSpanValue = 0x01, FieldTimeSpanScale = 0x02, FieldTimeSpanKind = 0x03;
  45. internal static readonly DateTime[] EpochOrigin = {
  46. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
  47. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
  48. new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Local)
  49. };
  50. /// <summary>
  51. /// Writes a TimeSpan to a protobuf stream
  52. /// </summary>
  53. public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
  54. {
  55. WriteTimeSpanImpl(timeSpan, dest, DateTimeKind.Unspecified);
  56. }
  57. private static void WriteTimeSpanImpl(TimeSpan timeSpan, ProtoWriter dest, DateTimeKind kind)
  58. {
  59. if (dest == null) throw new ArgumentNullException("dest");
  60. long value;
  61. switch(dest.WireType)
  62. {
  63. case WireType.String:
  64. case WireType.StartGroup:
  65. TimeSpanScale scale;
  66. value = timeSpan.Ticks;
  67. if (timeSpan == TimeSpan.MaxValue)
  68. {
  69. value = 1;
  70. scale = TimeSpanScale.MinMax;
  71. }
  72. else if (timeSpan == TimeSpan.MinValue)
  73. {
  74. value = -1;
  75. scale = TimeSpanScale.MinMax;
  76. }
  77. else if (value % TimeSpan.TicksPerDay == 0)
  78. {
  79. scale = TimeSpanScale.Days;
  80. value /= TimeSpan.TicksPerDay;
  81. }
  82. else if (value % TimeSpan.TicksPerHour == 0)
  83. {
  84. scale = TimeSpanScale.Hours;
  85. value /= TimeSpan.TicksPerHour;
  86. }
  87. else if (value % TimeSpan.TicksPerMinute == 0)
  88. {
  89. scale = TimeSpanScale.Minutes;
  90. value /= TimeSpan.TicksPerMinute;
  91. }
  92. else if (value % TimeSpan.TicksPerSecond == 0)
  93. {
  94. scale = TimeSpanScale.Seconds;
  95. value /= TimeSpan.TicksPerSecond;
  96. }
  97. else if (value % TimeSpan.TicksPerMillisecond == 0)
  98. {
  99. scale = TimeSpanScale.Milliseconds;
  100. value /= TimeSpan.TicksPerMillisecond;
  101. }
  102. else
  103. {
  104. scale = TimeSpanScale.Ticks;
  105. }
  106. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  107. if(value != 0) {
  108. ProtoWriter.WriteFieldHeader(FieldTimeSpanValue, WireType.SignedVariant, dest);
  109. ProtoWriter.WriteInt64(value, dest);
  110. }
  111. if(scale != TimeSpanScale.Days) {
  112. ProtoWriter.WriteFieldHeader(FieldTimeSpanScale, WireType.Variant, dest);
  113. ProtoWriter.WriteInt32((int)scale, dest);
  114. }
  115. if(kind != DateTimeKind.Unspecified)
  116. {
  117. ProtoWriter.WriteFieldHeader(FieldTimeSpanKind, WireType.Variant, dest);
  118. ProtoWriter.WriteInt32((int)kind, dest);
  119. }
  120. ProtoWriter.EndSubItem(token, dest);
  121. break;
  122. case WireType.Fixed64:
  123. ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
  124. break;
  125. default:
  126. throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
  127. }
  128. }
  129. /// <summary>
  130. /// Parses a TimeSpan from a protobuf stream
  131. /// </summary>
  132. public static TimeSpan ReadTimeSpan(ProtoReader source)
  133. {
  134. DateTimeKind kind;
  135. long ticks = ReadTimeSpanTicks(source, out kind);
  136. if (ticks == long.MinValue) return TimeSpan.MinValue;
  137. if (ticks == long.MaxValue) return TimeSpan.MaxValue;
  138. return TimeSpan.FromTicks(ticks);
  139. }
  140. /// <summary>
  141. /// Parses a DateTime from a protobuf stream
  142. /// </summary>
  143. public static DateTime ReadDateTime(ProtoReader source)
  144. {
  145. DateTimeKind kind;
  146. long ticks = ReadTimeSpanTicks(source, out kind);
  147. if (ticks == long.MinValue) return DateTime.MinValue;
  148. if (ticks == long.MaxValue) return DateTime.MaxValue;
  149. return EpochOrigin[(int)kind].AddTicks(ticks);
  150. }
  151. /// <summary>
  152. /// Writes a DateTime to a protobuf stream, excluding the <c>Kind</c>
  153. /// </summary>
  154. public static void WriteDateTime(DateTime value, ProtoWriter dest)
  155. {
  156. WriteDateTimeImpl(value, dest, false);
  157. }
  158. /// <summary>
  159. /// Writes a DateTime to a protobuf stream, including the <c>Kind</c>
  160. /// </summary>
  161. public static void WriteDateTimeWithKind(DateTime value, ProtoWriter dest)
  162. {
  163. WriteDateTimeImpl(value, dest, true);
  164. }
  165. private static void WriteDateTimeImpl(DateTime value, ProtoWriter dest, bool includeKind)
  166. {
  167. if (dest == null) throw new ArgumentNullException("dest");
  168. TimeSpan delta;
  169. switch (dest.WireType)
  170. {
  171. case WireType.StartGroup:
  172. case WireType.String:
  173. if (value == DateTime.MaxValue)
  174. {
  175. delta = TimeSpan.MaxValue;
  176. includeKind = false;
  177. }
  178. else if (value == DateTime.MinValue)
  179. {
  180. delta = TimeSpan.MinValue;
  181. includeKind = false;
  182. }
  183. else
  184. {
  185. delta = value - EpochOrigin[0];
  186. }
  187. break;
  188. default:
  189. delta = value - EpochOrigin[0];
  190. break;
  191. }
  192. WriteTimeSpanImpl(delta, dest, includeKind ? value.Kind : DateTimeKind.Unspecified);
  193. }
  194. private static long ReadTimeSpanTicks(ProtoReader source, out DateTimeKind kind) {
  195. kind = DateTimeKind.Unspecified;
  196. switch (source.WireType)
  197. {
  198. case WireType.String:
  199. case WireType.StartGroup:
  200. SubItemToken token = ProtoReader.StartSubItem(source);
  201. int fieldNumber;
  202. TimeSpanScale scale = TimeSpanScale.Days;
  203. long value = 0;
  204. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  205. {
  206. switch (fieldNumber)
  207. {
  208. case FieldTimeSpanScale:
  209. scale = (TimeSpanScale)source.ReadInt32();
  210. break;
  211. case FieldTimeSpanValue:
  212. source.Assert(WireType.SignedVariant);
  213. value = source.ReadInt64();
  214. break;
  215. case FieldTimeSpanKind:
  216. kind = (DateTimeKind)source.ReadInt32();
  217. switch(kind)
  218. {
  219. case DateTimeKind.Unspecified:
  220. case DateTimeKind.Utc:
  221. case DateTimeKind.Local:
  222. break; // fine
  223. default:
  224. throw new ProtoException("Invalid date/time kind: " + kind.ToString());
  225. }
  226. break;
  227. default:
  228. source.SkipField();
  229. break;
  230. }
  231. }
  232. ProtoReader.EndSubItem(token, source);
  233. switch (scale)
  234. {
  235. case TimeSpanScale.Days:
  236. return value * TimeSpan.TicksPerDay;
  237. case TimeSpanScale.Hours:
  238. return value * TimeSpan.TicksPerHour;
  239. case TimeSpanScale.Minutes:
  240. return value * TimeSpan.TicksPerMinute;
  241. case TimeSpanScale.Seconds:
  242. return value * TimeSpan.TicksPerSecond;
  243. case TimeSpanScale.Milliseconds:
  244. return value * TimeSpan.TicksPerMillisecond;
  245. case TimeSpanScale.Ticks:
  246. return value;
  247. case TimeSpanScale.MinMax:
  248. switch (value)
  249. {
  250. case 1: return long.MaxValue;
  251. case -1: return long.MinValue;
  252. default: throw new ProtoException("Unknown min/max value: " + value.ToString());
  253. }
  254. default:
  255. throw new ProtoException("Unknown timescale: " + scale.ToString());
  256. }
  257. case WireType.Fixed64:
  258. return source.ReadInt64();
  259. default:
  260. throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString());
  261. }
  262. }
  263. const int FieldDecimalLow = 0x01, FieldDecimalHigh = 0x02, FieldDecimalSignScale = 0x03;
  264. /// <summary>
  265. /// Parses a decimal from a protobuf stream
  266. /// </summary>
  267. public static decimal ReadDecimal(ProtoReader reader)
  268. {
  269. ulong low = 0;
  270. uint high = 0;
  271. uint signScale = 0;
  272. int fieldNumber;
  273. SubItemToken token = ProtoReader.StartSubItem(reader);
  274. while ((fieldNumber = reader.ReadFieldHeader()) > 0)
  275. {
  276. switch (fieldNumber)
  277. {
  278. case FieldDecimalLow: low = reader.ReadUInt64(); break;
  279. case FieldDecimalHigh: high = reader.ReadUInt32(); break;
  280. case FieldDecimalSignScale: signScale = reader.ReadUInt32(); break;
  281. default: reader.SkipField(); break;
  282. }
  283. }
  284. ProtoReader.EndSubItem(token, reader);
  285. if (low == 0 && high == 0) return decimal.Zero;
  286. int lo = (int)(low & 0xFFFFFFFFL),
  287. mid = (int)((low >> 32) & 0xFFFFFFFFL),
  288. hi = (int)high;
  289. bool isNeg = (signScale & 0x0001) == 0x0001;
  290. byte scale = (byte)((signScale & 0x01FE) >> 1);
  291. return new decimal(lo, mid, hi, isNeg, scale);
  292. }
  293. /// <summary>
  294. /// Writes a decimal to a protobuf stream
  295. /// </summary>
  296. public static void WriteDecimal(decimal value, ProtoWriter writer)
  297. {
  298. int[] bits = decimal.GetBits(value);
  299. ulong a = ((ulong)bits[1]) << 32, b = ((ulong)bits[0]) & 0xFFFFFFFFL;
  300. ulong low = a | b;
  301. uint high = (uint)bits[2];
  302. uint signScale = (uint)(((bits[3] >> 15) & 0x01FE) | ((bits[3] >> 31) & 0x0001));
  303. SubItemToken token = ProtoWriter.StartSubItem(null, writer);
  304. if (low != 0) {
  305. ProtoWriter.WriteFieldHeader(FieldDecimalLow, WireType.Variant, writer);
  306. ProtoWriter.WriteUInt64(low, writer);
  307. }
  308. if (high != 0)
  309. {
  310. ProtoWriter.WriteFieldHeader(FieldDecimalHigh, WireType.Variant, writer);
  311. ProtoWriter.WriteUInt32(high, writer);
  312. }
  313. if (signScale != 0)
  314. {
  315. ProtoWriter.WriteFieldHeader(FieldDecimalSignScale, WireType.Variant, writer);
  316. ProtoWriter.WriteUInt32(signScale, writer);
  317. }
  318. ProtoWriter.EndSubItem(token, writer);
  319. }
  320. const int FieldGuidLow = 1, FieldGuidHigh = 2;
  321. /// <summary>
  322. /// Writes a Guid to a protobuf stream
  323. /// </summary>
  324. public static void WriteGuid(Guid value, ProtoWriter dest)
  325. {
  326. byte[] blob = value.ToByteArray();
  327. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  328. if (value != Guid.Empty)
  329. {
  330. ProtoWriter.WriteFieldHeader(FieldGuidLow, WireType.Fixed64, dest);
  331. ProtoWriter.WriteBytes(blob, 0, 8, dest);
  332. ProtoWriter.WriteFieldHeader(FieldGuidHigh, WireType.Fixed64, dest);
  333. ProtoWriter.WriteBytes(blob, 8, 8, dest);
  334. }
  335. ProtoWriter.EndSubItem(token, dest);
  336. }
  337. /// <summary>
  338. /// Parses a Guid from a protobuf stream
  339. /// </summary>
  340. public static Guid ReadGuid(ProtoReader source)
  341. {
  342. ulong low = 0, high = 0;
  343. int fieldNumber;
  344. SubItemToken token = ProtoReader.StartSubItem(source);
  345. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  346. {
  347. switch (fieldNumber)
  348. {
  349. case FieldGuidLow: low = source.ReadUInt64(); break;
  350. case FieldGuidHigh: high = source.ReadUInt64(); break;
  351. default: source.SkipField(); break;
  352. }
  353. }
  354. ProtoReader.EndSubItem(token, source);
  355. if(low == 0 && high == 0) return Guid.Empty;
  356. uint a = (uint)(low >> 32), b = (uint)low, c = (uint)(high >> 32), d= (uint)high;
  357. return new Guid((int)b, (short)a, (short)(a >> 16),
  358. (byte)d, (byte)(d >> 8), (byte)(d >> 16), (byte)(d >> 24),
  359. (byte)c, (byte)(c >> 8), (byte)(c >> 16), (byte)(c >> 24));
  360. }
  361. private const int
  362. FieldExistingObjectKey = 1,
  363. FieldNewObjectKey = 2,
  364. FieldExistingTypeKey = 3,
  365. FieldNewTypeKey = 4,
  366. FieldTypeName = 8,
  367. FieldObject = 10;
  368. /// <summary>
  369. /// Optional behaviours that introduce .NET-specific functionality
  370. /// </summary>
  371. [Flags]
  372. public enum NetObjectOptions : byte
  373. {
  374. /// <summary>
  375. /// No special behaviour
  376. /// </summary>
  377. None = 0,
  378. /// <summary>
  379. /// Enables full object-tracking/full-graph support.
  380. /// </summary>
  381. AsReference = 1,
  382. /// <summary>
  383. /// Embeds the type information into the stream, allowing usage with types not known in advance.
  384. /// </summary>
  385. DynamicType = 2,
  386. /// <summary>
  387. /// If false, the constructor for the type is bypassed during deserialization, meaning any field initializers
  388. /// or other initialization code is skipped.
  389. /// </summary>
  390. UseConstructor = 4,
  391. /// <summary>
  392. /// Should the object index be reserved, rather than creating an object promptly
  393. /// </summary>
  394. LateSet = 8
  395. }
  396. /// <summary>
  397. /// Reads an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
  398. /// </summary>
  399. public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
  400. {
  401. #if FEAT_IKVM
  402. throw new NotSupportedException();
  403. #else
  404. SubItemToken token = ProtoReader.StartSubItem(source);
  405. int fieldNumber;
  406. int newObjectKey = -1, newTypeKey = -1, tmp;
  407. while ((fieldNumber = source.ReadFieldHeader()) > 0)
  408. {
  409. switch (fieldNumber)
  410. {
  411. case FieldExistingObjectKey:
  412. tmp = source.ReadInt32();
  413. value = source.NetCache.GetKeyedObject(tmp);
  414. break;
  415. case FieldNewObjectKey:
  416. newObjectKey = source.ReadInt32();
  417. break;
  418. case FieldExistingTypeKey:
  419. tmp = source.ReadInt32();
  420. type = (Type)source.NetCache.GetKeyedObject(tmp);
  421. key = source.GetTypeKey(ref type);
  422. break;
  423. case FieldNewTypeKey:
  424. newTypeKey = source.ReadInt32();
  425. break;
  426. case FieldTypeName:
  427. string typeName = source.ReadString();
  428. type = source.DeserializeType(typeName);
  429. if(type == null)
  430. {
  431. throw new ProtoException("Unable to resolve type: " + typeName + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
  432. }
  433. if (type == typeof(string))
  434. {
  435. key = -1;
  436. }
  437. else
  438. {
  439. key = source.GetTypeKey(ref type);
  440. if (key < 0)
  441. throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
  442. }
  443. break;
  444. case FieldObject:
  445. bool isString = type == typeof(string);
  446. bool wasNull = value == null;
  447. bool lateSet = wasNull && (isString || ((options & NetObjectOptions.LateSet) != 0));
  448. if (newObjectKey >= 0 && !lateSet)
  449. {
  450. if (value == null)
  451. {
  452. source.TrapNextObject(newObjectKey);
  453. }
  454. else
  455. {
  456. source.NetCache.SetKeyedObject(newObjectKey, value);
  457. }
  458. if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type);
  459. }
  460. object oldValue = value;
  461. if (isString)
  462. {
  463. value = source.ReadString();
  464. }
  465. else
  466. {
  467. value = ProtoReader.ReadTypedObject(oldValue, key, source, type);
  468. }
  469. if (newObjectKey >= 0)
  470. {
  471. if(wasNull && !lateSet)
  472. { // this both ensures (via exception) that it *was* set, and makes sure we don't shout
  473. // about changed references
  474. oldValue = source.NetCache.GetKeyedObject(newObjectKey);
  475. }
  476. if (lateSet)
  477. {
  478. source.NetCache.SetKeyedObject(newObjectKey, value);
  479. if (newTypeKey >= 0) source.NetCache.SetKeyedObject(newTypeKey, type);
  480. }
  481. }
  482. if (newObjectKey >= 0 && !lateSet && !ReferenceEquals(oldValue, value))
  483. {
  484. throw new ProtoException("A reference-tracked object changed reference during deserialization");
  485. }
  486. if (newObjectKey < 0 && newTypeKey >= 0)
  487. { // have a new type, but not a new object
  488. source.NetCache.SetKeyedObject(newTypeKey, type);
  489. }
  490. break;
  491. default:
  492. source.SkipField();
  493. break;
  494. }
  495. }
  496. if(newObjectKey >= 0 && (options & NetObjectOptions.AsReference) == 0)
  497. {
  498. throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
  499. }
  500. ProtoReader.EndSubItem(token, source);
  501. return value;
  502. #endif
  503. }
  504. /// <summary>
  505. /// Writes an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
  506. /// </summary>
  507. public static void WriteNetObject(object value, ProtoWriter dest, int key, NetObjectOptions options)
  508. {
  509. #if FEAT_IKVM
  510. throw new NotSupportedException();
  511. #else
  512. if (dest == null) throw new ArgumentNullException("dest");
  513. bool dynamicType = (options & NetObjectOptions.DynamicType) != 0,
  514. asReference = (options & NetObjectOptions.AsReference) != 0;
  515. WireType wireType = dest.WireType;
  516. SubItemToken token = ProtoWriter.StartSubItem(null, dest);
  517. bool writeObject = true;
  518. if (asReference)
  519. {
  520. bool existing;
  521. int objectKey = dest.NetCache.AddObjectKey(value, out existing);
  522. ProtoWriter.WriteFieldHeader(existing ? FieldExistingObjectKey : FieldNewObjectKey, WireType.Variant, dest);
  523. ProtoWriter.WriteInt32(objectKey, dest);
  524. if (existing)
  525. {
  526. writeObject = false;
  527. }
  528. }
  529. if (writeObject)
  530. {
  531. if (dynamicType)
  532. {
  533. bool existing;
  534. Type type = value.GetType();
  535. if (!(value is string))
  536. {
  537. key = dest.GetTypeKey(ref type);
  538. if (key < 0) throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
  539. }
  540. int typeKey = dest.NetCache.AddObjectKey(type, out existing);
  541. ProtoWriter.WriteFieldHeader(existing ? FieldExistingTypeKey : FieldNewTypeKey, WireType.Variant, dest);
  542. ProtoWriter.WriteInt32(typeKey, dest);
  543. if (!existing)
  544. {
  545. ProtoWriter.WriteFieldHeader(FieldTypeName, WireType.String, dest);
  546. ProtoWriter.WriteString(dest.SerializeType(type), dest);
  547. }
  548. }
  549. ProtoWriter.WriteFieldHeader(FieldObject, wireType, dest);
  550. if (value is string)
  551. {
  552. ProtoWriter.WriteString((string)value, dest);
  553. }
  554. else {
  555. ProtoWriter.WriteObject(value, key, dest);
  556. }
  557. }
  558. ProtoWriter.EndSubItem(token, dest);
  559. #endif
  560. }
  561. }
  562. }