ProtoWriter.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using ProtoBuf.Meta;
  5. namespace ProtoBuf
  6. {
  7. /// <summary>
  8. /// Represents an output stream for writing protobuf data.
  9. ///
  10. /// Why is the API backwards (static methods with writer arguments)?
  11. /// See: http://marcgravell.blogspot.com/2010/03/last-will-be-first-and-first-will-be.html
  12. /// </summary>
  13. public sealed class ProtoWriter : IDisposable
  14. {
  15. private Stream dest;
  16. TypeModel model;
  17. /// <summary>
  18. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type).
  19. /// </summary>
  20. /// <param name="value">The object to write.</param>
  21. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  22. /// <param name="writer">The destination.</param>
  23. public static void WriteObject(object value, int key, ProtoWriter writer)
  24. {
  25. if (writer == null) throw new ArgumentNullException("writer");
  26. if (writer.model == null)
  27. {
  28. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  29. }
  30. SubItemToken token = StartSubItem(value, writer);
  31. if (key >= 0)
  32. {
  33. writer.model.Serialize(key, value, writer);
  34. }
  35. else if (writer.model != null && writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false, null))
  36. {
  37. // all ok
  38. }
  39. else
  40. {
  41. TypeModel.ThrowUnexpectedType(value.GetType());
  42. }
  43. EndSubItem(token, writer);
  44. }
  45. /// <summary>
  46. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type) - but the
  47. /// caller is asserting that this relationship is non-recursive; no recursion check will be
  48. /// performed.
  49. /// </summary>
  50. /// <param name="value">The object to write.</param>
  51. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  52. /// <param name="writer">The destination.</param>
  53. public static void WriteRecursionSafeObject(object value, int key, ProtoWriter writer)
  54. {
  55. if (writer == null) throw new ArgumentNullException(nameof(writer));
  56. if (writer.model == null)
  57. {
  58. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  59. }
  60. SubItemToken token = StartSubItem(null, writer);
  61. writer.model.Serialize(key, value, writer);
  62. EndSubItem(token, writer);
  63. }
  64. internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
  65. {
  66. if (writer.model == null)
  67. {
  68. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  69. }
  70. if (writer.wireType != WireType.None) throw ProtoWriter.CreateException(writer);
  71. switch (style)
  72. {
  73. case PrefixStyle.Base128:
  74. writer.wireType = WireType.String;
  75. writer.fieldNumber = fieldNumber;
  76. if (fieldNumber > 0) WriteHeaderCore(fieldNumber, WireType.String, writer);
  77. break;
  78. case PrefixStyle.Fixed32:
  79. case PrefixStyle.Fixed32BigEndian:
  80. writer.fieldNumber = 0;
  81. writer.wireType = WireType.Fixed32;
  82. break;
  83. default:
  84. throw new ArgumentOutOfRangeException("style");
  85. }
  86. SubItemToken token = StartSubItem(value, writer, true);
  87. if (key < 0)
  88. {
  89. if (!writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false, null))
  90. {
  91. TypeModel.ThrowUnexpectedType(value.GetType());
  92. }
  93. }
  94. else
  95. {
  96. writer.model.Serialize(key, value, writer);
  97. }
  98. EndSubItem(token, writer, style);
  99. }
  100. internal int GetTypeKey(ref Type type)
  101. {
  102. return model.GetKey(ref type);
  103. }
  104. private readonly NetObjectCache netCache = new NetObjectCache();
  105. internal NetObjectCache NetCache => netCache;
  106. private int fieldNumber, flushLock;
  107. WireType wireType;
  108. internal WireType WireType { get { return wireType; } }
  109. /// <summary>
  110. /// Writes a field-header, indicating the format of the next data we plan to write.
  111. /// </summary>
  112. public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer)
  113. {
  114. if (writer == null) throw new ArgumentNullException("writer");
  115. if (writer.wireType != WireType.None) throw new InvalidOperationException("Cannot write a " + wireType.ToString()
  116. + " header until the " + writer.wireType.ToString() + " data has been written");
  117. if (fieldNumber < 0) throw new ArgumentOutOfRangeException("fieldNumber");
  118. #if DEBUG
  119. switch (wireType)
  120. { // validate requested header-type
  121. case WireType.Fixed32:
  122. case WireType.Fixed64:
  123. case WireType.String:
  124. case WireType.StartGroup:
  125. case WireType.SignedVariant:
  126. case WireType.Variant:
  127. break; // fine
  128. case WireType.None:
  129. case WireType.EndGroup:
  130. default:
  131. throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType");
  132. }
  133. #endif
  134. if (writer.packedFieldNumber == 0)
  135. {
  136. writer.fieldNumber = fieldNumber;
  137. writer.wireType = wireType;
  138. WriteHeaderCore(fieldNumber, wireType, writer);
  139. }
  140. else if (writer.packedFieldNumber == fieldNumber)
  141. { // we'll set things up, but note we *don't* actually write the header here
  142. switch (wireType)
  143. {
  144. case WireType.Fixed32:
  145. case WireType.Fixed64:
  146. case WireType.Variant:
  147. case WireType.SignedVariant:
  148. break; // fine
  149. default:
  150. throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString());
  151. }
  152. writer.fieldNumber = fieldNumber;
  153. writer.wireType = wireType;
  154. }
  155. else
  156. {
  157. throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
  158. }
  159. }
  160. internal static void WriteHeaderCore(int fieldNumber, WireType wireType, ProtoWriter writer)
  161. {
  162. uint header = (((uint)fieldNumber) << 3)
  163. | (((uint)wireType) & 7);
  164. WriteUInt32Variant(header, writer);
  165. }
  166. /// <summary>
  167. /// Writes a byte-array to the stream; supported wire-types: String
  168. /// </summary>
  169. public static void WriteBytes(byte[] data, ProtoWriter writer)
  170. {
  171. if (data == null) throw new ArgumentNullException(nameof(data));
  172. ProtoWriter.WriteBytes(data, 0, data.Length, writer);
  173. }
  174. /// <summary>
  175. /// Writes a byte-array to the stream; supported wire-types: String
  176. /// </summary>
  177. public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
  178. {
  179. if (data == null) throw new ArgumentNullException(nameof(data));
  180. if (writer == null) throw new ArgumentNullException(nameof(writer));
  181. switch (writer.wireType)
  182. {
  183. case WireType.Fixed32:
  184. if (length != 4) throw new ArgumentException(nameof(length));
  185. goto CopyFixedLength; // ugly but effective
  186. case WireType.Fixed64:
  187. if (length != 8) throw new ArgumentException(nameof(length));
  188. goto CopyFixedLength; // ugly but effective
  189. case WireType.String:
  190. WriteUInt32Variant((uint)length, writer);
  191. writer.wireType = WireType.None;
  192. if (length == 0) return;
  193. if (writer.flushLock != 0 || length <= writer.ioBuffer.Length) // write to the buffer
  194. {
  195. goto CopyFixedLength; // ugly but effective
  196. }
  197. // writing data that is bigger than the buffer (and the buffer
  198. // isn't currently locked due to a sub-object needing the size backfilled)
  199. Flush(writer); // commit any existing data from the buffer
  200. // now just write directly to the underlying stream
  201. writer.dest.Write(data, offset, length);
  202. writer.position64 += length; // since we've flushed offset etc is 0, and remains
  203. // zero since we're writing directly to the stream
  204. return;
  205. }
  206. throw CreateException(writer);
  207. CopyFixedLength: // no point duplicating this lots of times, and don't really want another stackframe
  208. DemandSpace(length, writer);
  209. Buffer.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
  210. IncrementedAndReset(length, writer);
  211. }
  212. private static void CopyRawFromStream(Stream source, ProtoWriter writer)
  213. {
  214. byte[] buffer = writer.ioBuffer;
  215. int space = buffer.Length - writer.ioIndex, bytesRead = 1; // 1 here to spoof case where already full
  216. // try filling the buffer first
  217. while (space > 0 && (bytesRead = source.Read(buffer, writer.ioIndex, space)) > 0)
  218. {
  219. writer.ioIndex += bytesRead;
  220. writer.position64 += bytesRead;
  221. space -= bytesRead;
  222. }
  223. if (bytesRead <= 0) return; // all done using just the buffer; stream exhausted
  224. // at this point the stream still has data, but buffer is full;
  225. if (writer.flushLock == 0)
  226. {
  227. // flush the buffer and write to the underlying stream instead
  228. Flush(writer);
  229. while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
  230. {
  231. writer.dest.Write(buffer, 0, bytesRead);
  232. writer.position64 += bytesRead;
  233. }
  234. }
  235. else
  236. {
  237. do
  238. {
  239. // need more space; resize (double) as necessary,
  240. // requesting a reasonable minimum chunk each time
  241. // (128 is the minimum; there may actually be much
  242. // more space than this in the buffer)
  243. DemandSpace(128, writer);
  244. if ((bytesRead = source.Read(writer.ioBuffer, writer.ioIndex,
  245. writer.ioBuffer.Length - writer.ioIndex)) <= 0) break;
  246. writer.position64 += bytesRead;
  247. writer.ioIndex += bytesRead;
  248. } while (true);
  249. }
  250. }
  251. private static void IncrementedAndReset(int length, ProtoWriter writer)
  252. {
  253. Helpers.DebugAssert(length >= 0);
  254. writer.ioIndex += length;
  255. writer.position64 += length;
  256. writer.wireType = WireType.None;
  257. }
  258. int depth = 0;
  259. const int RecursionCheckDepth = 25;
  260. /// <summary>
  261. /// Indicates the start of a nested record.
  262. /// </summary>
  263. /// <param name="instance">The instance to write.</param>
  264. /// <param name="writer">The destination.</param>
  265. /// <returns>A token representing the state of the stream; this token is given to EndSubItem.</returns>
  266. public static SubItemToken StartSubItem(object instance, ProtoWriter writer)
  267. {
  268. return StartSubItem(instance, writer, false);
  269. }
  270. MutableList recursionStack;
  271. private void CheckRecursionStackAndPush(object instance)
  272. {
  273. int hitLevel;
  274. if (recursionStack == null) { recursionStack = new MutableList(); }
  275. else if (instance != null && (hitLevel = recursionStack.IndexOfReference(instance)) >= 0)
  276. {
  277. #if DEBUG
  278. Helpers.DebugWriteLine("Stack:");
  279. foreach (object obj in recursionStack)
  280. {
  281. Helpers.DebugWriteLine(obj == null ? "<null>" : obj.ToString());
  282. }
  283. Helpers.DebugWriteLine(instance == null ? "<null>" : instance.ToString());
  284. #endif
  285. throw new ProtoException("Possible recursion detected (offset: " + (recursionStack.Count - hitLevel).ToString() + " level(s)): " + instance.ToString());
  286. }
  287. recursionStack.Add(instance);
  288. }
  289. private void PopRecursionStack() { recursionStack.RemoveLast(); }
  290. private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
  291. {
  292. if (writer == null) throw new ArgumentNullException("writer");
  293. if (++writer.depth > RecursionCheckDepth)
  294. {
  295. writer.CheckRecursionStackAndPush(instance);
  296. }
  297. if (writer.packedFieldNumber != 0) throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
  298. switch (writer.wireType)
  299. {
  300. case WireType.StartGroup:
  301. writer.wireType = WireType.None;
  302. return new SubItemToken((long)(-writer.fieldNumber));
  303. case WireType.String:
  304. #if DEBUG
  305. if (writer.model != null && writer.model.ForwardsOnly)
  306. {
  307. throw new ProtoException("Should not be buffering data: " + instance ?? "(null)");
  308. }
  309. #endif
  310. writer.wireType = WireType.None;
  311. DemandSpace(32, writer); // make some space in anticipation...
  312. writer.flushLock++;
  313. writer.position64++;
  314. return new SubItemToken((long)(writer.ioIndex++)); // leave 1 space (optimistic) for length
  315. case WireType.Fixed32:
  316. {
  317. if (!allowFixed) throw CreateException(writer);
  318. DemandSpace(32, writer); // make some space in anticipation...
  319. writer.flushLock++;
  320. SubItemToken token = new SubItemToken((long)writer.ioIndex);
  321. ProtoWriter.IncrementedAndReset(4, writer); // leave 4 space (rigid) for length
  322. return token;
  323. }
  324. default:
  325. throw CreateException(writer);
  326. }
  327. }
  328. /// <summary>
  329. /// Indicates the end of a nested record.
  330. /// </summary>
  331. /// <param name="token">The token obtained from StartubItem.</param>
  332. /// <param name="writer">The destination.</param>
  333. public static void EndSubItem(SubItemToken token, ProtoWriter writer)
  334. {
  335. EndSubItem(token, writer, PrefixStyle.Base128);
  336. }
  337. private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
  338. {
  339. if (writer == null) throw new ArgumentNullException("writer");
  340. if (writer.wireType != WireType.None) { throw CreateException(writer); }
  341. int value = (int)token.value64;
  342. if (writer.depth <= 0) throw CreateException(writer);
  343. if (writer.depth-- > RecursionCheckDepth)
  344. {
  345. writer.PopRecursionStack();
  346. }
  347. writer.packedFieldNumber = 0; // ending the sub-item always wipes packed encoding
  348. if (value < 0)
  349. { // group - very simple append
  350. WriteHeaderCore(-value, WireType.EndGroup, writer);
  351. writer.wireType = WireType.None;
  352. return;
  353. }
  354. // so we're backfilling the length into an existing sequence
  355. int len;
  356. switch (style)
  357. {
  358. case PrefixStyle.Fixed32:
  359. len = (int)((writer.ioIndex - value) - 4);
  360. ProtoWriter.WriteInt32ToBuffer(len, writer.ioBuffer, value);
  361. break;
  362. case PrefixStyle.Fixed32BigEndian:
  363. len = (int)((writer.ioIndex - value) - 4);
  364. byte[] buffer = writer.ioBuffer;
  365. ProtoWriter.WriteInt32ToBuffer(len, buffer, value);
  366. // and swap the byte order
  367. byte b = buffer[value];
  368. buffer[value] = buffer[value + 3];
  369. buffer[value + 3] = b;
  370. b = buffer[value + 1];
  371. buffer[value + 1] = buffer[value + 2];
  372. buffer[value + 2] = b;
  373. break;
  374. case PrefixStyle.Base128:
  375. // string - complicated because we only reserved one byte;
  376. // if the prefix turns out to need more than this then
  377. // we need to shuffle the existing data
  378. len = (int)((writer.ioIndex - value) - 1);
  379. int offset = 0;
  380. uint tmp = (uint)len;
  381. while ((tmp >>= 7) != 0) offset++;
  382. if (offset == 0)
  383. {
  384. writer.ioBuffer[value] = (byte)(len & 0x7F);
  385. }
  386. else
  387. {
  388. DemandSpace(offset, writer);
  389. byte[] blob = writer.ioBuffer;
  390. Buffer.BlockCopy(blob, value + 1, blob, value + 1 + offset, len);
  391. tmp = (uint)len;
  392. do
  393. {
  394. blob[value++] = (byte)((tmp & 0x7F) | 0x80);
  395. } while ((tmp >>= 7) != 0);
  396. blob[value - 1] = (byte)(blob[value - 1] & ~0x80);
  397. writer.position64 += offset;
  398. writer.ioIndex += offset;
  399. }
  400. break;
  401. default:
  402. throw new ArgumentOutOfRangeException("style");
  403. }
  404. // and this object is no longer a blockage - also flush if sensible
  405. const int ADVISORY_FLUSH_SIZE = 1024;
  406. if (--writer.flushLock == 0 && writer.ioIndex >= ADVISORY_FLUSH_SIZE)
  407. {
  408. ProtoWriter.Flush(writer);
  409. }
  410. }
  411. /// <summary>
  412. /// Creates a new writer against a stream
  413. /// </summary>
  414. /// <param name="dest">The destination stream</param>
  415. /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to serialize sub-objects</param>
  416. /// <param name="context">Additional context about this serialization operation</param>
  417. public static ProtoWriter Create(Stream dest, TypeModel model, SerializationContext context = null)
  418. #pragma warning disable CS0618
  419. => new ProtoWriter(dest, model, context);
  420. #pragma warning restore CS0618
  421. /// <summary>
  422. /// Creates a new writer against a stream
  423. /// </summary>
  424. /// <param name="dest">The destination stream</param>
  425. /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to serialize sub-objects</param>
  426. /// <param name="context">Additional context about this serialization operation</param>
  427. [Obsolete("Please use ProtoWriter.Create; this API may be removed in a future version", error: false)]
  428. public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
  429. {
  430. if (dest == null) throw new ArgumentNullException("dest");
  431. if (!dest.CanWrite) throw new ArgumentException("Cannot write to stream", "dest");
  432. //if (model == null) throw new ArgumentNullException("model");
  433. this.dest = dest;
  434. this.ioBuffer = BufferPool.GetBuffer();
  435. this.model = model;
  436. this.wireType = WireType.None;
  437. if (context == null) { context = SerializationContext.Default; }
  438. else { context.Freeze(); }
  439. this.context = context;
  440. }
  441. private readonly SerializationContext context;
  442. /// <summary>
  443. /// Addition information about this serialization operation.
  444. /// </summary>
  445. public SerializationContext Context => context;
  446. void IDisposable.Dispose()
  447. {
  448. Dispose();
  449. }
  450. private void Dispose()
  451. { // importantly, this does **not** own the stream, and does not dispose it
  452. if (dest != null)
  453. {
  454. Flush(this);
  455. dest = null;
  456. }
  457. model = null;
  458. BufferPool.ReleaseBufferToPool(ref ioBuffer);
  459. }
  460. private byte[] ioBuffer;
  461. private int ioIndex;
  462. // note that this is used by some of the unit tests and should not be removed
  463. internal static long GetLongPosition(ProtoWriter writer) { return writer.position64; }
  464. internal static int GetPosition(ProtoWriter writer) { return checked((int)writer.position64); }
  465. private long position64;
  466. private static void DemandSpace(int required, ProtoWriter writer)
  467. {
  468. // check for enough space
  469. if ((writer.ioBuffer.Length - writer.ioIndex) < required)
  470. {
  471. TryFlushOrResize(required, writer);
  472. }
  473. }
  474. private static void TryFlushOrResize(int required, ProtoWriter writer)
  475. {
  476. if (writer.flushLock == 0)
  477. {
  478. Flush(writer); // try emptying the buffer
  479. if ((writer.ioBuffer.Length - writer.ioIndex) >= required) return;
  480. }
  481. // either can't empty the buffer, or that didn't help; need more space
  482. BufferPool.ResizeAndFlushLeft(ref writer.ioBuffer, required + writer.ioIndex, 0, writer.ioIndex);
  483. }
  484. /// <summary>
  485. /// Flushes data to the underlying stream, and releases any resources. The underlying stream is *not* disposed
  486. /// by this operation.
  487. /// </summary>
  488. public void Close()
  489. {
  490. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("Unable to close stream in an incomplete state");
  491. Dispose();
  492. }
  493. internal void CheckDepthFlushlock()
  494. {
  495. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("The writer is in an incomplete state");
  496. }
  497. /// <summary>
  498. /// Get the TypeModel associated with this writer
  499. /// </summary>
  500. public TypeModel Model => model;
  501. /// <summary>
  502. /// Writes any buffered data (if possible) to the underlying stream.
  503. /// </summary>
  504. /// <param name="writer">The writer to flush</param>
  505. /// <remarks>It is not always possible to fully flush, since some sequences
  506. /// may require values to be back-filled into the byte-stream.</remarks>
  507. internal static void Flush(ProtoWriter writer)
  508. {
  509. if (writer.flushLock == 0 && writer.ioIndex != 0)
  510. {
  511. writer.dest.Write(writer.ioBuffer, 0, writer.ioIndex);
  512. writer.ioIndex = 0;
  513. }
  514. }
  515. /// <summary>
  516. /// Writes an unsigned 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  517. /// </summary>
  518. private static void WriteUInt32Variant(uint value, ProtoWriter writer)
  519. {
  520. DemandSpace(5, writer);
  521. int count = 0;
  522. do
  523. {
  524. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  525. count++;
  526. } while ((value >>= 7) != 0);
  527. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  528. writer.position64 += count;
  529. }
  530. #if COREFX
  531. static readonly Encoding encoding = Encoding.UTF8;
  532. #else
  533. static readonly UTF8Encoding encoding = new UTF8Encoding();
  534. #endif
  535. internal static uint Zig(int value)
  536. {
  537. return (uint)((value << 1) ^ (value >> 31));
  538. }
  539. internal static ulong Zig(long value)
  540. {
  541. return (ulong)((value << 1) ^ (value >> 63));
  542. }
  543. private static void WriteUInt64Variant(ulong value, ProtoWriter writer)
  544. {
  545. DemandSpace(10, writer);
  546. int count = 0;
  547. do
  548. {
  549. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  550. count++;
  551. } while ((value >>= 7) != 0);
  552. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  553. writer.position64 += count;
  554. }
  555. /// <summary>
  556. /// Writes a string to the stream; supported wire-types: String
  557. /// </summary>
  558. public static void WriteString(string value, ProtoWriter writer)
  559. {
  560. if (writer == null) throw new ArgumentNullException("writer");
  561. if (writer.wireType != WireType.String) throw CreateException(writer);
  562. if (value == null) throw new ArgumentNullException("value"); // written header; now what?
  563. int len = value.Length;
  564. if (len == 0)
  565. {
  566. WriteUInt32Variant(0, writer);
  567. writer.wireType = WireType.None;
  568. return; // just a header
  569. }
  570. int predicted = encoding.GetByteCount(value);
  571. WriteUInt32Variant((uint)predicted, writer);
  572. DemandSpace(predicted, writer);
  573. int actual = encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
  574. Helpers.DebugAssert(predicted == actual);
  575. IncrementedAndReset(actual, writer);
  576. }
  577. /// <summary>
  578. /// Writes an unsigned 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  579. /// </summary>
  580. public static void WriteUInt64(ulong value, ProtoWriter writer)
  581. {
  582. if (writer == null) throw new ArgumentNullException(nameof(writer));
  583. switch (writer.wireType)
  584. {
  585. case WireType.Fixed64:
  586. ProtoWriter.WriteInt64((long)value, writer);
  587. return;
  588. case WireType.Variant:
  589. WriteUInt64Variant(value, writer);
  590. writer.wireType = WireType.None;
  591. return;
  592. case WireType.Fixed32:
  593. checked { ProtoWriter.WriteUInt32((uint)value, writer); }
  594. return;
  595. default:
  596. throw CreateException(writer);
  597. }
  598. }
  599. /// <summary>
  600. /// Writes a signed 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  601. /// </summary>
  602. public static void WriteInt64(long value, ProtoWriter writer)
  603. {
  604. byte[] buffer;
  605. int index;
  606. if (writer == null) throw new ArgumentNullException(nameof(writer));
  607. switch (writer.wireType)
  608. {
  609. case WireType.Fixed64:
  610. DemandSpace(8, writer);
  611. buffer = writer.ioBuffer;
  612. index = writer.ioIndex;
  613. #if NETCOREAPP2_1
  614. System.Buffers.Binary.BinaryPrimitives.WriteInt64LittleEndian(buffer.AsSpan(index, 8), value);
  615. #else
  616. buffer[index] = (byte)value;
  617. buffer[index + 1] = (byte)(value >> 8);
  618. buffer[index + 2] = (byte)(value >> 16);
  619. buffer[index + 3] = (byte)(value >> 24);
  620. buffer[index + 4] = (byte)(value >> 32);
  621. buffer[index + 5] = (byte)(value >> 40);
  622. buffer[index + 6] = (byte)(value >> 48);
  623. buffer[index + 7] = (byte)(value >> 56);
  624. #endif
  625. IncrementedAndReset(8, writer);
  626. return;
  627. case WireType.SignedVariant:
  628. WriteUInt64Variant(Zig(value), writer);
  629. writer.wireType = WireType.None;
  630. return;
  631. case WireType.Variant:
  632. if (value >= 0)
  633. {
  634. WriteUInt64Variant((ulong)value, writer);
  635. writer.wireType = WireType.None;
  636. }
  637. else
  638. {
  639. DemandSpace(10, writer);
  640. buffer = writer.ioBuffer;
  641. index = writer.ioIndex;
  642. buffer[index] = (byte)(value | 0x80);
  643. buffer[index + 1] = (byte)((int)(value >> 7) | 0x80);
  644. buffer[index + 2] = (byte)((int)(value >> 14) | 0x80);
  645. buffer[index + 3] = (byte)((int)(value >> 21) | 0x80);
  646. buffer[index + 4] = (byte)((int)(value >> 28) | 0x80);
  647. buffer[index + 5] = (byte)((int)(value >> 35) | 0x80);
  648. buffer[index + 6] = (byte)((int)(value >> 42) | 0x80);
  649. buffer[index + 7] = (byte)((int)(value >> 49) | 0x80);
  650. buffer[index + 8] = (byte)((int)(value >> 56) | 0x80);
  651. buffer[index + 9] = 0x01; // sign bit
  652. IncrementedAndReset(10, writer);
  653. }
  654. return;
  655. case WireType.Fixed32:
  656. checked { WriteInt32((int)value, writer); }
  657. return;
  658. default:
  659. throw CreateException(writer);
  660. }
  661. }
  662. /// <summary>
  663. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  664. /// </summary>
  665. public static void WriteUInt32(uint value, ProtoWriter writer)
  666. {
  667. if (writer == null) throw new ArgumentNullException("writer");
  668. switch (writer.wireType)
  669. {
  670. case WireType.Fixed32:
  671. ProtoWriter.WriteInt32((int)value, writer);
  672. return;
  673. case WireType.Fixed64:
  674. ProtoWriter.WriteInt64((int)value, writer);
  675. return;
  676. case WireType.Variant:
  677. WriteUInt32Variant(value, writer);
  678. writer.wireType = WireType.None;
  679. return;
  680. default:
  681. throw CreateException(writer);
  682. }
  683. }
  684. /// <summary>
  685. /// Writes a signed 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  686. /// </summary>
  687. public static void WriteInt16(short value, ProtoWriter writer)
  688. {
  689. ProtoWriter.WriteInt32(value, writer);
  690. }
  691. /// <summary>
  692. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  693. /// </summary>
  694. public static void WriteUInt16(ushort value, ProtoWriter writer)
  695. {
  696. ProtoWriter.WriteUInt32(value, writer);
  697. }
  698. /// <summary>
  699. /// Writes an unsigned 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  700. /// </summary>
  701. public static void WriteByte(byte value, ProtoWriter writer)
  702. {
  703. ProtoWriter.WriteUInt32(value, writer);
  704. }
  705. /// <summary>
  706. /// Writes a signed 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  707. /// </summary>
  708. public static void WriteSByte(sbyte value, ProtoWriter writer)
  709. {
  710. ProtoWriter.WriteInt32(value, writer);
  711. }
  712. private static void WriteInt32ToBuffer(int value, byte[] buffer, int index)
  713. {
  714. #if NETCOREAPP2_1
  715. System.Buffers.Binary.BinaryPrimitives.WriteInt32LittleEndian(buffer.AsSpan(index, 4), value);
  716. #else
  717. buffer[index] = (byte)value;
  718. buffer[index + 1] = (byte)(value >> 8);
  719. buffer[index + 2] = (byte)(value >> 16);
  720. buffer[index + 3] = (byte)(value >> 24);
  721. #endif
  722. }
  723. /// <summary>
  724. /// Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  725. /// </summary>
  726. public static void WriteInt32(int value, ProtoWriter writer)
  727. {
  728. byte[] buffer;
  729. int index;
  730. if (writer == null) throw new ArgumentNullException(nameof(writer));
  731. switch (writer.wireType)
  732. {
  733. case WireType.Fixed32:
  734. DemandSpace(4, writer);
  735. WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
  736. IncrementedAndReset(4, writer);
  737. return;
  738. case WireType.Fixed64:
  739. DemandSpace(8, writer);
  740. buffer = writer.ioBuffer;
  741. index = writer.ioIndex;
  742. buffer[index] = (byte)value;
  743. buffer[index + 1] = (byte)(value >> 8);
  744. buffer[index + 2] = (byte)(value >> 16);
  745. buffer[index + 3] = (byte)(value >> 24);
  746. buffer[index + 4] = buffer[index + 5] =
  747. buffer[index + 6] = buffer[index + 7] = 0;
  748. IncrementedAndReset(8, writer);
  749. return;
  750. case WireType.SignedVariant:
  751. WriteUInt32Variant(Zig(value), writer);
  752. writer.wireType = WireType.None;
  753. return;
  754. case WireType.Variant:
  755. if (value >= 0)
  756. {
  757. WriteUInt32Variant((uint)value, writer);
  758. writer.wireType = WireType.None;
  759. }
  760. else
  761. {
  762. DemandSpace(10, writer);
  763. buffer = writer.ioBuffer;
  764. index = writer.ioIndex;
  765. buffer[index] = (byte)(value | 0x80);
  766. buffer[index + 1] = (byte)((value >> 7) | 0x80);
  767. buffer[index + 2] = (byte)((value >> 14) | 0x80);
  768. buffer[index + 3] = (byte)((value >> 21) | 0x80);
  769. buffer[index + 4] = (byte)((value >> 28) | 0x80);
  770. buffer[index + 5] = buffer[index + 6] =
  771. buffer[index + 7] = buffer[index + 8] = (byte)0xFF;
  772. buffer[index + 9] = (byte)0x01;
  773. IncrementedAndReset(10, writer);
  774. }
  775. return;
  776. default:
  777. throw CreateException(writer);
  778. }
  779. }
  780. /// <summary>
  781. /// Writes a double-precision number to the stream; supported wire-types: Fixed32, Fixed64
  782. /// </summary>
  783. public
  784. #if !FEAT_SAFE
  785. unsafe
  786. #endif
  787. static void WriteDouble(double value, ProtoWriter writer)
  788. {
  789. if (writer == null) throw new ArgumentNullException("writer");
  790. switch (writer.wireType)
  791. {
  792. case WireType.Fixed32:
  793. float f = (float)value;
  794. if (float.IsInfinity(f) && !double.IsInfinity(value))
  795. {
  796. throw new OverflowException();
  797. }
  798. ProtoWriter.WriteSingle(f, writer);
  799. return;
  800. case WireType.Fixed64:
  801. #if FEAT_SAFE
  802. ProtoWriter.WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(value), 0), writer);
  803. #else
  804. ProtoWriter.WriteInt64(*(long*)&value, writer);
  805. #endif
  806. return;
  807. default:
  808. throw CreateException(writer);
  809. }
  810. }
  811. /// <summary>
  812. /// Writes a single-precision number to the stream; supported wire-types: Fixed32, Fixed64
  813. /// </summary>
  814. public
  815. #if !FEAT_SAFE
  816. unsafe
  817. #endif
  818. static void WriteSingle(float value, ProtoWriter writer)
  819. {
  820. if (writer == null) throw new ArgumentNullException("writer");
  821. switch (writer.wireType)
  822. {
  823. case WireType.Fixed32:
  824. #if FEAT_SAFE
  825. ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
  826. #else
  827. ProtoWriter.WriteInt32(*(int*)&value, writer);
  828. #endif
  829. return;
  830. case WireType.Fixed64:
  831. ProtoWriter.WriteDouble((double)value, writer);
  832. return;
  833. default:
  834. throw CreateException(writer);
  835. }
  836. }
  837. /// <summary>
  838. /// Throws an exception indicating that the given enum cannot be mapped to a serialized value.
  839. /// </summary>
  840. public static void ThrowEnumException(ProtoWriter writer, object enumValue)
  841. {
  842. if (writer == null) throw new ArgumentNullException("writer");
  843. string rhs = enumValue == null ? "<null>" : (enumValue.GetType().FullName + "." + enumValue.ToString());
  844. throw new ProtoException("No wire-value is mapped to the enum " + rhs + " at position " + writer.position64.ToString());
  845. }
  846. // general purpose serialization exception message
  847. internal static Exception CreateException(ProtoWriter writer)
  848. {
  849. if (writer == null) throw new ArgumentNullException("writer");
  850. return new ProtoException("Invalid serialization operation with wire-type " + writer.wireType.ToString() + " at position " + writer.position64.ToString());
  851. }
  852. /// <summary>
  853. /// Writes a boolean to the stream; supported wire-types: Variant, Fixed32, Fixed64
  854. /// </summary>
  855. public static void WriteBoolean(bool value, ProtoWriter writer)
  856. {
  857. ProtoWriter.WriteUInt32(value ? (uint)1 : (uint)0, writer);
  858. }
  859. /// <summary>
  860. /// Copies any extension data stored for the instance to the underlying stream
  861. /// </summary>
  862. public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
  863. {
  864. if (instance == null) throw new ArgumentNullException(nameof(instance));
  865. if (writer == null) throw new ArgumentNullException(nameof(writer));
  866. // we expect the writer to be raw here; the extension data will have the
  867. // header detail, so we'll copy it implicitly
  868. if (writer.wireType != WireType.None) throw CreateException(writer);
  869. IExtension extn = instance.GetExtensionObject(false);
  870. if (extn != null)
  871. {
  872. // unusually we *don't* want "using" here; the "finally" does that, with
  873. // the extension object being responsible for disposal etc
  874. Stream source = extn.BeginQuery();
  875. try
  876. {
  877. CopyRawFromStream(source, writer);
  878. }
  879. finally { extn.EndQuery(source); }
  880. }
  881. }
  882. private int packedFieldNumber;
  883. /// <summary>
  884. /// Used for packed encoding; indicates that the next field should be skipped rather than
  885. /// a field header written. Note that the field number must match, else an exception is thrown
  886. /// when the attempt is made to write the (incorrect) field. The wire-type is taken from the
  887. /// subsequent call to WriteFieldHeader. Only primitive types can be packed.
  888. /// </summary>
  889. public static void SetPackedField(int fieldNumber, ProtoWriter writer)
  890. {
  891. if (fieldNumber <= 0) throw new ArgumentOutOfRangeException(nameof(fieldNumber));
  892. if (writer == null) throw new ArgumentNullException(nameof(writer));
  893. writer.packedFieldNumber = fieldNumber;
  894. }
  895. /// <summary>
  896. /// Used for packed encoding; explicitly reset the packed field marker; this is not required
  897. /// if using StartSubItem/EndSubItem
  898. /// </summary>
  899. public static void ClearPackedField(int fieldNumber, ProtoWriter writer)
  900. {
  901. if (fieldNumber != writer.packedFieldNumber)
  902. throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
  903. writer.packedFieldNumber = 0;
  904. }
  905. /// <summary>
  906. /// Used for packed encoding; writes the length prefix using fixed sizes rather than using
  907. /// buffering. Only valid for fixed-32 and fixed-64 encoding.
  908. /// </summary>
  909. public static void WritePackedPrefix(int elementCount, WireType wireType, ProtoWriter writer)
  910. {
  911. if (writer.WireType != WireType.String) throw new InvalidOperationException("Invalid wire-type: " + writer.WireType);
  912. if (elementCount < 0) throw new ArgumentOutOfRangeException(nameof(elementCount));
  913. ulong bytes;
  914. switch (wireType)
  915. {
  916. // use long in case very large arrays are enabled
  917. case WireType.Fixed32: bytes = ((ulong)elementCount) << 2; break; // x4
  918. case WireType.Fixed64: bytes = ((ulong)elementCount) << 3; break; // x8
  919. default:
  920. throw new ArgumentOutOfRangeException(nameof(wireType), "Invalid wire-type: " + wireType);
  921. }
  922. WriteUInt64Variant(bytes, writer);
  923. writer.wireType = WireType.None;
  924. }
  925. internal string SerializeType(Type type)
  926. {
  927. return TypeModel.SerializeType(model, type);
  928. }
  929. /// <summary>
  930. /// Specifies a known root object to use during reference-tracked serialization
  931. /// </summary>
  932. public void SetRootObject(object value)
  933. {
  934. NetCache.SetKeyedObject(NetObjectCache.Root, value);
  935. }
  936. /// <summary>
  937. /// Writes a Type to the stream, using the model's DynamicTypeFormatting if appropriate; supported wire-types: String
  938. /// </summary>
  939. public static void WriteType(Type value, ProtoWriter writer)
  940. {
  941. if (writer == null) throw new ArgumentNullException(nameof(writer));
  942. WriteString(writer.SerializeType(value), writer);
  943. }
  944. }
  945. }