ProtoWriter.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using ProtoBuf.Meta;
  5. #if MF
  6. using OverflowException = System.ApplicationException;
  7. #endif
  8. #if FEAT_IKVM
  9. using Type = IKVM.Reflection.Type;
  10. #endif
  11. namespace ProtoBuf
  12. {
  13. /// <summary>
  14. /// Represents an output stream for writing protobuf data.
  15. ///
  16. /// Why is the API backwards (static methods with writer arguments)?
  17. /// See: http://marcgravell.blogspot.com/2010/03/last-will-be-first-and-first-will-be.html
  18. /// </summary>
  19. public sealed class ProtoWriter : IDisposable
  20. {
  21. private Stream dest;
  22. TypeModel model;
  23. /// <summary>
  24. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type).
  25. /// </summary>
  26. /// <param name="value">The object to write.</param>
  27. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  28. /// <param name="writer">The destination.</param>
  29. public static void WriteObject(object value, int key, ProtoWriter writer)
  30. {
  31. #if FEAT_IKVM
  32. throw new NotSupportedException();
  33. #else
  34. if (writer == null) throw new ArgumentNullException("writer");
  35. if (writer.model == null)
  36. {
  37. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  38. }
  39. SubItemToken token = StartSubItem(value, writer);
  40. if (key >= 0)
  41. {
  42. writer.model.Serialize(key, value, writer);
  43. }
  44. else if (writer.model != null && writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false, null))
  45. {
  46. // all ok
  47. }
  48. else
  49. {
  50. TypeModel.ThrowUnexpectedType(value.GetType());
  51. }
  52. EndSubItem(token, writer);
  53. #endif
  54. }
  55. /// <summary>
  56. /// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type) - but the
  57. /// caller is asserting that this relationship is non-recursive; no recursion check will be
  58. /// performed.
  59. /// </summary>
  60. /// <param name="value">The object to write.</param>
  61. /// <param name="key">The key that uniquely identifies the type within the model.</param>
  62. /// <param name="writer">The destination.</param>
  63. public static void WriteRecursionSafeObject(object value, int key, ProtoWriter writer)
  64. {
  65. if (writer == null) throw new ArgumentNullException("writer");
  66. if (writer.model == null)
  67. {
  68. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  69. }
  70. SubItemToken token = StartSubItem(null, writer);
  71. writer.model.Serialize(key, value, writer);
  72. EndSubItem(token, writer);
  73. }
  74. internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
  75. {
  76. #if FEAT_IKVM
  77. throw new NotSupportedException();
  78. #else
  79. if (writer.model == null)
  80. {
  81. throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
  82. }
  83. if (writer.wireType != WireType.None) throw ProtoWriter.CreateException(writer);
  84. switch (style)
  85. {
  86. case PrefixStyle.Base128:
  87. writer.wireType = WireType.String;
  88. writer.fieldNumber = fieldNumber;
  89. if (fieldNumber > 0) WriteHeaderCore(fieldNumber, WireType.String, writer);
  90. break;
  91. case PrefixStyle.Fixed32:
  92. case PrefixStyle.Fixed32BigEndian:
  93. writer.fieldNumber = 0;
  94. writer.wireType = WireType.Fixed32;
  95. break;
  96. default:
  97. throw new ArgumentOutOfRangeException("style");
  98. }
  99. SubItemToken token = StartSubItem(value, writer, true);
  100. if (key < 0)
  101. {
  102. if (!writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, Serializer.ListItemTag, value, false, null))
  103. {
  104. TypeModel.ThrowUnexpectedType(value.GetType());
  105. }
  106. }
  107. else
  108. {
  109. writer.model.Serialize(key, value, writer);
  110. }
  111. EndSubItem(token, writer, style);
  112. #endif
  113. }
  114. internal int GetTypeKey(ref Type type)
  115. {
  116. return model.GetKey(ref type);
  117. }
  118. private readonly NetObjectCache netCache = new NetObjectCache();
  119. internal NetObjectCache NetCache
  120. {
  121. get { return netCache;}
  122. }
  123. private int fieldNumber, flushLock;
  124. WireType wireType;
  125. internal WireType WireType { get { return wireType; } }
  126. /// <summary>
  127. /// Writes a field-header, indicating the format of the next data we plan to write.
  128. /// </summary>
  129. public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer) {
  130. if (writer == null) throw new ArgumentNullException("writer");
  131. if (writer.wireType != WireType.None) throw new InvalidOperationException("Cannot write a " + wireType.ToString()
  132. + " header until the " + writer.wireType.ToString() + " data has been written");
  133. if(fieldNumber < 0) throw new ArgumentOutOfRangeException("fieldNumber");
  134. #if DEBUG
  135. switch (wireType)
  136. { // validate requested header-type
  137. case WireType.Fixed32:
  138. case WireType.Fixed64:
  139. case WireType.String:
  140. case WireType.StartGroup:
  141. case WireType.SignedVariant:
  142. case WireType.Variant:
  143. break; // fine
  144. case WireType.None:
  145. case WireType.EndGroup:
  146. default:
  147. throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType");
  148. }
  149. #endif
  150. if (writer.packedFieldNumber == 0) {
  151. writer.fieldNumber = fieldNumber;
  152. writer.wireType = wireType;
  153. WriteHeaderCore(fieldNumber, wireType, writer);
  154. }
  155. else if (writer.packedFieldNumber == fieldNumber)
  156. { // we'll set things up, but note we *don't* actually write the header here
  157. switch (wireType)
  158. {
  159. case WireType.Fixed32:
  160. case WireType.Fixed64:
  161. case WireType.Variant:
  162. case WireType.SignedVariant:
  163. break; // fine
  164. default:
  165. throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString());
  166. }
  167. writer.fieldNumber = fieldNumber;
  168. writer.wireType = wireType;
  169. }
  170. else
  171. {
  172. throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
  173. }
  174. }
  175. internal static void WriteHeaderCore(int fieldNumber, WireType wireType, ProtoWriter writer)
  176. {
  177. uint header = (((uint)fieldNumber) << 3)
  178. | (((uint)wireType) & 7);
  179. WriteUInt32Variant(header, writer);
  180. }
  181. /// <summary>
  182. /// Writes a byte-array to the stream; supported wire-types: String
  183. /// </summary>
  184. public static void WriteBytes(byte[] data, ProtoWriter writer)
  185. {
  186. if (data == null) throw new ArgumentNullException("data");
  187. ProtoWriter.WriteBytes(data, 0, data.Length, writer);
  188. }
  189. /// <summary>
  190. /// Writes a byte-array to the stream; supported wire-types: String
  191. /// </summary>
  192. public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer)
  193. {
  194. if (data == null) throw new ArgumentNullException("data");
  195. if (writer == null) throw new ArgumentNullException("writer");
  196. switch (writer.wireType)
  197. {
  198. case WireType.Fixed32:
  199. if (length != 4) throw new ArgumentException("length");
  200. goto CopyFixedLength; // ugly but effective
  201. case WireType.Fixed64:
  202. if (length != 8) throw new ArgumentException("length");
  203. goto CopyFixedLength; // ugly but effective
  204. case WireType.String:
  205. WriteUInt32Variant((uint)length, writer);
  206. writer.wireType = WireType.None;
  207. if (length == 0) return;
  208. if (writer.flushLock != 0 || length <= writer.ioBuffer.Length) // write to the buffer
  209. {
  210. goto CopyFixedLength; // ugly but effective
  211. }
  212. // writing data that is bigger than the buffer (and the buffer
  213. // isn't currently locked due to a sub-object needing the size backfilled)
  214. Flush(writer); // commit any existing data from the buffer
  215. // now just write directly to the underlying stream
  216. writer.dest.Write(data, offset, length);
  217. writer.position64 += length; // since we've flushed offset etc is 0, and remains
  218. // zero since we're writing directly to the stream
  219. return;
  220. }
  221. throw CreateException(writer);
  222. CopyFixedLength: // no point duplicating this lots of times, and don't really want another stackframe
  223. DemandSpace(length, writer);
  224. Helpers.BlockCopy(data, offset, writer.ioBuffer, writer.ioIndex, length);
  225. IncrementedAndReset(length, writer);
  226. }
  227. private static void CopyRawFromStream(Stream source, ProtoWriter writer)
  228. {
  229. byte[] buffer = writer.ioBuffer;
  230. int space = buffer.Length - writer.ioIndex, bytesRead = 1; // 1 here to spoof case where already full
  231. // try filling the buffer first
  232. while (space > 0 && (bytesRead = source.Read(buffer, writer.ioIndex, space)) > 0)
  233. {
  234. writer.ioIndex += bytesRead;
  235. writer.position64 += bytesRead;
  236. space -= bytesRead;
  237. }
  238. if (bytesRead <= 0) return; // all done using just the buffer; stream exhausted
  239. // at this point the stream still has data, but buffer is full;
  240. if (writer.flushLock == 0)
  241. {
  242. // flush the buffer and write to the underlying stream instead
  243. Flush(writer);
  244. while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
  245. {
  246. writer.dest.Write(buffer, 0, bytesRead);
  247. writer.position64 += bytesRead;
  248. }
  249. }
  250. else
  251. {
  252. do
  253. {
  254. // need more space; resize (double) as necessary,
  255. // requesting a reasonable minimum chunk each time
  256. // (128 is the minimum; there may actually be much
  257. // more space than this in the buffer)
  258. DemandSpace(128, writer);
  259. if((bytesRead = source.Read(writer.ioBuffer, writer.ioIndex,
  260. writer.ioBuffer.Length - writer.ioIndex)) <= 0) break;
  261. writer.position64 += bytesRead;
  262. writer.ioIndex += bytesRead;
  263. } while (true);
  264. }
  265. }
  266. private static void IncrementedAndReset(int length, ProtoWriter writer)
  267. {
  268. Helpers.DebugAssert(length >= 0);
  269. writer.ioIndex += length;
  270. writer.position64 += length;
  271. writer.wireType = WireType.None;
  272. }
  273. int depth = 0;
  274. const int RecursionCheckDepth = 25;
  275. /// <summary>
  276. /// Indicates the start of a nested record.
  277. /// </summary>
  278. /// <param name="instance">The instance to write.</param>
  279. /// <param name="writer">The destination.</param>
  280. /// <returns>A token representing the state of the stream; this token is given to EndSubItem.</returns>
  281. public static SubItemToken StartSubItem(object instance, ProtoWriter writer)
  282. {
  283. return StartSubItem(instance, writer, false);
  284. }
  285. MutableList recursionStack;
  286. private void CheckRecursionStackAndPush(object instance)
  287. {
  288. int hitLevel;
  289. if (recursionStack == null) { recursionStack = new MutableList(); }
  290. else if (instance != null && (hitLevel = recursionStack.IndexOfReference(instance)) >= 0)
  291. {
  292. #if DEBUG
  293. Helpers.DebugWriteLine("Stack:");
  294. foreach(object obj in recursionStack)
  295. {
  296. Helpers.DebugWriteLine(obj == null ? "<null>" : obj.ToString());
  297. }
  298. Helpers.DebugWriteLine(instance == null ? "<null>" : instance.ToString());
  299. #endif
  300. throw new ProtoException("Possible recursion detected (offset: " + (recursionStack.Count - hitLevel).ToString() + " level(s)): " + instance.ToString());
  301. }
  302. recursionStack.Add(instance);
  303. }
  304. private void PopRecursionStack() { recursionStack.RemoveLast(); }
  305. private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
  306. {
  307. if (writer == null) throw new ArgumentNullException("writer");
  308. if (++writer.depth > RecursionCheckDepth)
  309. {
  310. writer.CheckRecursionStackAndPush(instance);
  311. }
  312. if(writer.packedFieldNumber != 0) throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
  313. switch (writer.wireType)
  314. {
  315. case WireType.StartGroup:
  316. writer.wireType = WireType.None;
  317. return new SubItemToken((long)(-writer.fieldNumber));
  318. case WireType.String:
  319. #if DEBUG
  320. if(writer.model != null && writer.model.ForwardsOnly)
  321. {
  322. throw new ProtoException("Should not be buffering data: " + instance ?? "(null)");
  323. }
  324. #endif
  325. writer.wireType = WireType.None;
  326. DemandSpace(32, writer); // make some space in anticipation...
  327. writer.flushLock++;
  328. writer.position64++;
  329. return new SubItemToken((long)(writer.ioIndex++)); // leave 1 space (optimistic) for length
  330. case WireType.Fixed32:
  331. {
  332. if (!allowFixed) throw CreateException(writer);
  333. DemandSpace(32, writer); // make some space in anticipation...
  334. writer.flushLock++;
  335. SubItemToken token = new SubItemToken((long)writer.ioIndex);
  336. ProtoWriter.IncrementedAndReset(4, writer); // leave 4 space (rigid) for length
  337. return token;
  338. }
  339. default:
  340. throw CreateException(writer);
  341. }
  342. }
  343. /// <summary>
  344. /// Indicates the end of a nested record.
  345. /// </summary>
  346. /// <param name="token">The token obtained from StartubItem.</param>
  347. /// <param name="writer">The destination.</param>
  348. public static void EndSubItem(SubItemToken token, ProtoWriter writer)
  349. {
  350. EndSubItem(token, writer, PrefixStyle.Base128);
  351. }
  352. private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
  353. {
  354. if (writer == null) throw new ArgumentNullException("writer");
  355. if (writer.wireType != WireType.None) { throw CreateException(writer); }
  356. int value = (int)token.value64;
  357. if (writer.depth <= 0) throw CreateException(writer);
  358. if (writer.depth-- > RecursionCheckDepth)
  359. {
  360. writer.PopRecursionStack();
  361. }
  362. writer.packedFieldNumber = 0; // ending the sub-item always wipes packed encoding
  363. if (value < 0)
  364. { // group - very simple append
  365. WriteHeaderCore(-value, WireType.EndGroup, writer);
  366. writer.wireType = WireType.None;
  367. return;
  368. }
  369. // so we're backfilling the length into an existing sequence
  370. int len;
  371. switch(style)
  372. {
  373. case PrefixStyle.Fixed32:
  374. len = (int)((writer.ioIndex - value) - 4);
  375. ProtoWriter.WriteInt32ToBuffer(len, writer.ioBuffer, value);
  376. break;
  377. case PrefixStyle.Fixed32BigEndian:
  378. len = (int)((writer.ioIndex - value) - 4);
  379. byte[] buffer = writer.ioBuffer;
  380. ProtoWriter.WriteInt32ToBuffer(len, buffer, value);
  381. // and swap the byte order
  382. byte b = buffer[value];
  383. buffer[value] = buffer[value + 3];
  384. buffer[value + 3] = b;
  385. b = buffer[value + 1];
  386. buffer[value + 1] = buffer[value + 2];
  387. buffer[value + 2] = b;
  388. break;
  389. case PrefixStyle.Base128:
  390. // string - complicated because we only reserved one byte;
  391. // if the prefix turns out to need more than this then
  392. // we need to shuffle the existing data
  393. len = (int)((writer.ioIndex - value) - 1);
  394. int offset = 0;
  395. uint tmp = (uint)len;
  396. while ((tmp >>= 7) != 0) offset++;
  397. if (offset == 0)
  398. {
  399. writer.ioBuffer[value] = (byte)(len & 0x7F);
  400. }
  401. else
  402. {
  403. DemandSpace(offset, writer);
  404. byte[] blob = writer.ioBuffer;
  405. Helpers.BlockCopy(blob, value + 1, blob, value + 1 + offset, len);
  406. tmp = (uint)len;
  407. do
  408. {
  409. blob[value++] = (byte)((tmp & 0x7F) | 0x80);
  410. } while ((tmp >>= 7) != 0);
  411. blob[value - 1] = (byte)(blob[value - 1] & ~0x80);
  412. writer.position64 += offset;
  413. writer.ioIndex += offset;
  414. }
  415. break;
  416. default:
  417. throw new ArgumentOutOfRangeException("style");
  418. }
  419. // and this object is no longer a blockage - also flush if sensible
  420. const int ADVISORY_FLUSH_SIZE = 1024;
  421. if (--writer.flushLock == 0 && writer.ioIndex >= ADVISORY_FLUSH_SIZE)
  422. {
  423. ProtoWriter.Flush(writer);
  424. }
  425. }
  426. /// <summary>
  427. /// Creates a new writer against a stream
  428. /// </summary>
  429. /// <param name="dest">The destination stream</param>
  430. /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to serialize sub-objects</param>
  431. /// <param name="context">Additional context about this serialization operation</param>
  432. public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
  433. {
  434. if (dest == null) throw new ArgumentNullException("dest");
  435. if (!dest.CanWrite) throw new ArgumentException("Cannot write to stream", "dest");
  436. //if (model == null) throw new ArgumentNullException("model");
  437. this.dest = dest;
  438. this.ioBuffer = BufferPool.GetBuffer();
  439. this.model = model;
  440. this.wireType = WireType.None;
  441. if (context == null) { context = SerializationContext.Default; }
  442. else { context.Freeze(); }
  443. this.context = context;
  444. }
  445. private readonly SerializationContext context;
  446. /// <summary>
  447. /// Addition information about this serialization operation.
  448. /// </summary>
  449. public SerializationContext Context { get { return context; } }
  450. void IDisposable.Dispose()
  451. {
  452. Dispose();
  453. }
  454. private void Dispose()
  455. { // importantly, this does **not** own the stream, and does not dispose it
  456. if (dest != null)
  457. {
  458. Flush(this);
  459. dest = null;
  460. }
  461. model = null;
  462. BufferPool.ReleaseBufferToPool(ref ioBuffer);
  463. }
  464. private byte[] ioBuffer;
  465. private int ioIndex;
  466. // note that this is used by some of the unit tests and should not be removed
  467. internal static long GetLongPosition(ProtoWriter writer) { return writer.position64; }
  468. internal static int GetPosition(ProtoWriter writer) { return checked((int) writer.position64); }
  469. private long position64;
  470. private static void DemandSpace(int required, ProtoWriter writer)
  471. {
  472. // check for enough space
  473. if ((writer.ioBuffer.Length - writer.ioIndex) < required)
  474. {
  475. if (writer.flushLock == 0)
  476. {
  477. Flush(writer); // try emptying the buffer
  478. if ((writer.ioBuffer.Length - writer.ioIndex) >= required) return;
  479. }
  480. // either can't empty the buffer, or that didn't help; need more space
  481. BufferPool.ResizeAndFlushLeft(ref writer.ioBuffer, required + writer.ioIndex, 0, writer.ioIndex);
  482. }
  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 { get { return 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. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  524. count++;
  525. } while ((value >>= 7) != 0);
  526. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  527. writer.position64 += count;
  528. }
  529. #if COREFX
  530. static readonly Encoding encoding = Encoding.UTF8;
  531. #else
  532. static readonly UTF8Encoding encoding = new UTF8Encoding();
  533. #endif
  534. internal static uint Zig(int value)
  535. {
  536. return (uint)((value << 1) ^ (value >> 31));
  537. }
  538. internal static ulong Zig(long value)
  539. {
  540. return (ulong)((value << 1) ^ (value >> 63));
  541. }
  542. private static void WriteUInt64Variant(ulong value, ProtoWriter writer)
  543. {
  544. DemandSpace(10, writer);
  545. int count = 0;
  546. do
  547. {
  548. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  549. count++;
  550. } while ((value >>= 7) != 0);
  551. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  552. writer.position64 += count;
  553. }
  554. /// <summary>
  555. /// Writes a string to the stream; supported wire-types: String
  556. /// </summary>
  557. public static void WriteString(string value, ProtoWriter writer)
  558. {
  559. if (writer == null) throw new ArgumentNullException("writer");
  560. if (writer.wireType != WireType.String) throw CreateException(writer);
  561. if (value == null) throw new ArgumentNullException("value"); // written header; now what?
  562. int len = value.Length;
  563. if (len == 0)
  564. {
  565. WriteUInt32Variant(0, writer);
  566. writer.wireType = WireType.None;
  567. return; // just a header
  568. }
  569. #if MF
  570. byte[] bytes = encoding.GetBytes(value);
  571. int actual = bytes.Length;
  572. writer.WriteUInt32Variant((uint)actual);
  573. writer.Ensure(actual);
  574. Helpers.BlockCopy(bytes, 0, writer.ioBuffer, writer.ioIndex, actual);
  575. #else
  576. int predicted = encoding.GetByteCount(value);
  577. WriteUInt32Variant((uint)predicted, writer);
  578. DemandSpace(predicted, writer);
  579. int actual = encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
  580. Helpers.DebugAssert(predicted == actual);
  581. #endif
  582. IncrementedAndReset(actual, writer);
  583. }
  584. /// <summary>
  585. /// Writes an unsigned 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  586. /// </summary>
  587. public static void WriteUInt64(ulong value, ProtoWriter writer)
  588. {
  589. if (writer == null) throw new ArgumentNullException("writer");
  590. switch (writer.wireType)
  591. {
  592. case WireType.Fixed64:
  593. ProtoWriter.WriteInt64((long)value, writer);
  594. return;
  595. case WireType.Variant:
  596. WriteUInt64Variant(value, writer);
  597. writer.wireType = WireType.None;
  598. return;
  599. case WireType.Fixed32:
  600. checked { ProtoWriter.WriteUInt32((uint)value, writer); }
  601. return;
  602. default:
  603. throw CreateException(writer);
  604. }
  605. }
  606. /// <summary>
  607. /// Writes a signed 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  608. /// </summary>
  609. public static void WriteInt64(long value, ProtoWriter writer)
  610. {
  611. byte[] buffer;
  612. int index;
  613. if (writer == null) throw new ArgumentNullException("writer");
  614. switch (writer.wireType)
  615. {
  616. case WireType.Fixed64:
  617. DemandSpace(8, writer);
  618. buffer = writer.ioBuffer;
  619. index = writer.ioIndex;
  620. buffer[index] = (byte)value;
  621. buffer[index + 1] = (byte)(value >> 8);
  622. buffer[index + 2] = (byte)(value >> 16);
  623. buffer[index + 3] = (byte)(value >> 24);
  624. buffer[index + 4] = (byte)(value >> 32);
  625. buffer[index + 5] = (byte)(value >> 40);
  626. buffer[index + 6] = (byte)(value >> 48);
  627. buffer[index + 7] = (byte)(value >> 56);
  628. IncrementedAndReset(8, writer);
  629. return;
  630. case WireType.SignedVariant:
  631. WriteUInt64Variant(Zig(value), writer);
  632. writer.wireType = WireType.None;
  633. return;
  634. case WireType.Variant:
  635. if (value >= 0)
  636. {
  637. WriteUInt64Variant((ulong)value, writer);
  638. writer.wireType = WireType.None;
  639. }
  640. else
  641. {
  642. DemandSpace(10, writer);
  643. buffer = writer.ioBuffer;
  644. index = writer.ioIndex;
  645. buffer[index] = (byte)(value | 0x80);
  646. buffer[index + 1] = (byte)((int)(value >> 7) | 0x80);
  647. buffer[index + 2] = (byte)((int)(value >> 14) | 0x80);
  648. buffer[index + 3] = (byte)((int)(value >> 21) | 0x80);
  649. buffer[index + 4] = (byte)((int)(value >> 28) | 0x80);
  650. buffer[index + 5] = (byte)((int)(value >> 35) | 0x80);
  651. buffer[index + 6] = (byte)((int)(value >> 42) | 0x80);
  652. buffer[index + 7] = (byte)((int)(value >> 49) | 0x80);
  653. buffer[index + 8] = (byte)((int)(value >> 56) | 0x80);
  654. buffer[index + 9] = 0x01; // sign bit
  655. IncrementedAndReset(10, writer);
  656. }
  657. return;
  658. case WireType.Fixed32:
  659. checked { WriteInt32((int)value, writer); }
  660. return;
  661. default:
  662. throw CreateException(writer);
  663. }
  664. }
  665. /// <summary>
  666. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  667. /// </summary>
  668. public static void WriteUInt32(uint value, ProtoWriter writer)
  669. {
  670. if (writer == null) throw new ArgumentNullException("writer");
  671. switch (writer.wireType)
  672. {
  673. case WireType.Fixed32:
  674. ProtoWriter.WriteInt32((int)value, writer);
  675. return;
  676. case WireType.Fixed64:
  677. ProtoWriter.WriteInt64((int)value, writer);
  678. return;
  679. case WireType.Variant:
  680. WriteUInt32Variant(value, writer);
  681. writer.wireType = WireType.None;
  682. return;
  683. default:
  684. throw CreateException(writer);
  685. }
  686. }
  687. /// <summary>
  688. /// Writes a signed 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  689. /// </summary>
  690. public static void WriteInt16(short value, ProtoWriter writer)
  691. {
  692. ProtoWriter.WriteInt32(value, writer);
  693. }
  694. /// <summary>
  695. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  696. /// </summary>
  697. public static void WriteUInt16(ushort value, ProtoWriter writer)
  698. {
  699. ProtoWriter.WriteUInt32(value, writer);
  700. }
  701. /// <summary>
  702. /// Writes an unsigned 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  703. /// </summary>
  704. public static void WriteByte(byte value, ProtoWriter writer)
  705. {
  706. ProtoWriter.WriteUInt32(value, writer);
  707. }
  708. /// <summary>
  709. /// Writes a signed 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  710. /// </summary>
  711. public static void WriteSByte(sbyte value, ProtoWriter writer)
  712. {
  713. ProtoWriter.WriteInt32(value, writer);
  714. }
  715. private static void WriteInt32ToBuffer(int value, byte[] buffer, int index)
  716. {
  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. }
  722. /// <summary>
  723. /// Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  724. /// </summary>
  725. public static void WriteInt32(int value, ProtoWriter writer)
  726. {
  727. byte[] buffer;
  728. int index;
  729. if (writer == null) throw new ArgumentNullException("writer");
  730. switch (writer.wireType)
  731. {
  732. case WireType.Fixed32:
  733. DemandSpace(4, writer);
  734. WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
  735. IncrementedAndReset(4, writer);
  736. return;
  737. case WireType.Fixed64:
  738. DemandSpace(8, writer);
  739. buffer = writer.ioBuffer;
  740. index = writer.ioIndex;
  741. buffer[index] = (byte)value;
  742. buffer[index + 1] = (byte)(value >> 8);
  743. buffer[index + 2] = (byte)(value >> 16);
  744. buffer[index + 3] = (byte)(value >> 24);
  745. buffer[index + 4] = buffer[index + 5] =
  746. buffer[index + 6] = buffer[index + 7] = 0;
  747. IncrementedAndReset(8, writer);
  748. return;
  749. case WireType.SignedVariant:
  750. WriteUInt32Variant(Zig(value), writer);
  751. writer.wireType = WireType.None;
  752. return;
  753. case WireType.Variant:
  754. if (value >= 0)
  755. {
  756. WriteUInt32Variant((uint)value, writer);
  757. writer.wireType = WireType.None;
  758. }
  759. else
  760. {
  761. DemandSpace(10, writer);
  762. buffer = writer.ioBuffer;
  763. index = writer.ioIndex;
  764. buffer[index] = (byte)(value | 0x80);
  765. buffer[index + 1] = (byte)((value >> 7) | 0x80);
  766. buffer[index + 2] = (byte)((value >> 14) | 0x80);
  767. buffer[index + 3] = (byte)((value >> 21) | 0x80);
  768. buffer[index + 4] = (byte)((value >> 28) | 0x80);
  769. buffer[index + 5] = buffer[index + 6] =
  770. buffer[index + 7] = buffer[index + 8] = (byte)0xFF;
  771. buffer[index + 9] = (byte)0x01;
  772. IncrementedAndReset(10, writer);
  773. }
  774. return;
  775. default:
  776. throw CreateException(writer);
  777. }
  778. }
  779. /// <summary>
  780. /// Writes a double-precision number to the stream; supported wire-types: Fixed32, Fixed64
  781. /// </summary>
  782. public
  783. #if !FEAT_SAFE
  784. unsafe
  785. #endif
  786. static void WriteDouble(double value, ProtoWriter writer)
  787. {
  788. if (writer == null) throw new ArgumentNullException("writer");
  789. switch (writer.wireType)
  790. {
  791. case WireType.Fixed32:
  792. float f = (float)value;
  793. if (Helpers.IsInfinity(f)
  794. && !Helpers.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("instance");
  865. if (writer == null) throw new ArgumentNullException("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("fieldNumber");
  892. if (writer == null) throw new ArgumentNullException("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(System.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(System.Type value, ProtoWriter writer)
  940. {
  941. if (writer == null) throw new ArgumentNullException("writer");
  942. WriteString(writer.SerializeType(value), writer);
  943. }
  944. }
  945. }