BclHelpers.cs 22 KB

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