ProtoWriter.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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))
  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))
  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.position += 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.position += 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.position += 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.position += 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.position += 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(-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");
  323. }
  324. #endif
  325. writer.wireType = WireType.None;
  326. DemandSpace(32, writer); // make some space in anticipation...
  327. writer.flushLock++;
  328. writer.position++;
  329. return new SubItemToken(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(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 = token.value;
  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.position += 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 int GetPosition(ProtoWriter writer) { return writer.position; }
  468. private int position;
  469. private static void DemandSpace(int required, ProtoWriter writer)
  470. {
  471. // check for enough space
  472. if ((writer.ioBuffer.Length - writer.ioIndex) < required)
  473. {
  474. if (writer.flushLock == 0)
  475. {
  476. Flush(writer); // try emptying the buffer
  477. if ((writer.ioBuffer.Length - writer.ioIndex) >= required) return;
  478. }
  479. // either can't empty the buffer, or that didn't help; need more space
  480. BufferPool.ResizeAndFlushLeft(ref writer.ioBuffer, required + writer.ioIndex, 0, writer.ioIndex);
  481. }
  482. }
  483. /// <summary>
  484. /// Flushes data to the underlying stream, and releases any resources. The underlying stream is *not* disposed
  485. /// by this operation.
  486. /// </summary>
  487. public void Close()
  488. {
  489. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("Unable to close stream in an incomplete state");
  490. Dispose();
  491. }
  492. internal void CheckDepthFlushlock()
  493. {
  494. if (depth != 0 || flushLock != 0) throw new InvalidOperationException("The writer is in an incomplete state");
  495. }
  496. /// <summary>
  497. /// Get the TypeModel associated with this writer
  498. /// </summary>
  499. public TypeModel Model { get { return model; } }
  500. /// <summary>
  501. /// Writes any buffered data (if possible) to the underlying stream.
  502. /// </summary>
  503. /// <param name="writer">The writer to flush</param>
  504. /// <remarks>It is not always possible to fully flush, since some sequences
  505. /// may require values to be back-filled into the byte-stream.</remarks>
  506. internal static void Flush(ProtoWriter writer)
  507. {
  508. if (writer.flushLock == 0 && writer.ioIndex != 0)
  509. {
  510. writer.dest.Write(writer.ioBuffer, 0, writer.ioIndex);
  511. writer.ioIndex = 0;
  512. }
  513. }
  514. /// <summary>
  515. /// Writes an unsigned 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  516. /// </summary>
  517. private static void WriteUInt32Variant(uint value, ProtoWriter writer)
  518. {
  519. DemandSpace(5, writer);
  520. int count = 0;
  521. do {
  522. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  523. count++;
  524. } while ((value >>= 7) != 0);
  525. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  526. writer.position += count;
  527. }
  528. #if COREFX
  529. static readonly Encoding encoding = Encoding.UTF8;
  530. #else
  531. static readonly UTF8Encoding encoding = new UTF8Encoding();
  532. #endif
  533. internal static uint Zig(int value)
  534. {
  535. return (uint)((value << 1) ^ (value >> 31));
  536. }
  537. internal static ulong Zig(long value)
  538. {
  539. return (ulong)((value << 1) ^ (value >> 63));
  540. }
  541. private static void WriteUInt64Variant(ulong value, ProtoWriter writer)
  542. {
  543. DemandSpace(10, writer);
  544. int count = 0;
  545. do
  546. {
  547. writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
  548. count++;
  549. } while ((value >>= 7) != 0);
  550. writer.ioBuffer[writer.ioIndex - 1] &= 0x7F;
  551. writer.position += count;
  552. }
  553. /// <summary>
  554. /// Writes a string to the stream; supported wire-types: String
  555. /// </summary>
  556. public static void WriteString(string value, ProtoWriter writer)
  557. {
  558. if (writer == null) throw new ArgumentNullException("writer");
  559. if (writer.wireType != WireType.String) throw CreateException(writer);
  560. if (value == null) throw new ArgumentNullException("value"); // written header; now what?
  561. int len = value.Length;
  562. if (len == 0)
  563. {
  564. WriteUInt32Variant(0, writer);
  565. writer.wireType = WireType.None;
  566. return; // just a header
  567. }
  568. #if MF
  569. byte[] bytes = encoding.GetBytes(value);
  570. int actual = bytes.Length;
  571. writer.WriteUInt32Variant((uint)actual);
  572. writer.Ensure(actual);
  573. Helpers.BlockCopy(bytes, 0, writer.ioBuffer, writer.ioIndex, actual);
  574. #else
  575. int predicted = encoding.GetByteCount(value);
  576. WriteUInt32Variant((uint)predicted, writer);
  577. DemandSpace(predicted, writer);
  578. int actual = encoding.GetBytes(value, 0, value.Length, writer.ioBuffer, writer.ioIndex);
  579. Helpers.DebugAssert(predicted == actual);
  580. #endif
  581. IncrementedAndReset(actual, writer);
  582. }
  583. /// <summary>
  584. /// Writes an unsigned 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  585. /// </summary>
  586. public static void WriteUInt64(ulong value, ProtoWriter writer)
  587. {
  588. if (writer == null) throw new ArgumentNullException("writer");
  589. switch (writer.wireType)
  590. {
  591. case WireType.Fixed64:
  592. ProtoWriter.WriteInt64((long)value, writer);
  593. return;
  594. case WireType.Variant:
  595. WriteUInt64Variant(value, writer);
  596. writer.wireType = WireType.None;
  597. return;
  598. case WireType.Fixed32:
  599. checked { ProtoWriter.WriteUInt32((uint)value, writer); }
  600. return;
  601. default:
  602. throw CreateException(writer);
  603. }
  604. }
  605. /// <summary>
  606. /// Writes a signed 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  607. /// </summary>
  608. public static void WriteInt64(long value, ProtoWriter writer)
  609. {
  610. byte[] buffer;
  611. int index;
  612. if (writer == null) throw new ArgumentNullException("writer");
  613. switch (writer.wireType)
  614. {
  615. case WireType.Fixed64:
  616. DemandSpace(8, writer);
  617. buffer = writer.ioBuffer;
  618. index = writer.ioIndex;
  619. buffer[index] = (byte)value;
  620. buffer[index + 1] = (byte)(value >> 8);
  621. buffer[index + 2] = (byte)(value >> 16);
  622. buffer[index + 3] = (byte)(value >> 24);
  623. buffer[index + 4] = (byte)(value >> 32);
  624. buffer[index + 5] = (byte)(value >> 40);
  625. buffer[index + 6] = (byte)(value >> 48);
  626. buffer[index + 7] = (byte)(value >> 56);
  627. IncrementedAndReset(8, writer);
  628. return;
  629. case WireType.SignedVariant:
  630. WriteUInt64Variant(Zig(value), writer);
  631. writer.wireType = WireType.None;
  632. return;
  633. case WireType.Variant:
  634. if (value >= 0)
  635. {
  636. WriteUInt64Variant((ulong)value, writer);
  637. writer.wireType = WireType.None;
  638. }
  639. else
  640. {
  641. DemandSpace(10, writer);
  642. buffer = writer.ioBuffer;
  643. index = writer.ioIndex;
  644. buffer[index] = (byte)(value | 0x80);
  645. buffer[index + 1] = (byte)((int)(value >> 7) | 0x80);
  646. buffer[index + 2] = (byte)((int)(value >> 14) | 0x80);
  647. buffer[index + 3] = (byte)((int)(value >> 21) | 0x80);
  648. buffer[index + 4] = (byte)((int)(value >> 28) | 0x80);
  649. buffer[index + 5] = (byte)((int)(value >> 35) | 0x80);
  650. buffer[index + 6] = (byte)((int)(value >> 42) | 0x80);
  651. buffer[index + 7] = (byte)((int)(value >> 49) | 0x80);
  652. buffer[index + 8] = (byte)((int)(value >> 56) | 0x80);
  653. buffer[index + 9] = 0x01; // sign bit
  654. IncrementedAndReset(10, writer);
  655. }
  656. return;
  657. case WireType.Fixed32:
  658. checked { WriteInt32((int)value, writer); }
  659. return;
  660. default:
  661. throw CreateException(writer);
  662. }
  663. }
  664. /// <summary>
  665. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  666. /// </summary>
  667. public static void WriteUInt32(uint value, ProtoWriter writer)
  668. {
  669. if (writer == null) throw new ArgumentNullException("writer");
  670. switch (writer.wireType)
  671. {
  672. case WireType.Fixed32:
  673. ProtoWriter.WriteInt32((int)value, writer);
  674. return;
  675. case WireType.Fixed64:
  676. ProtoWriter.WriteInt64((int)value, writer);
  677. return;
  678. case WireType.Variant:
  679. WriteUInt32Variant(value, writer);
  680. writer.wireType = WireType.None;
  681. return;
  682. default:
  683. throw CreateException(writer);
  684. }
  685. }
  686. /// <summary>
  687. /// Writes a signed 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  688. /// </summary>
  689. public static void WriteInt16(short value, ProtoWriter writer)
  690. {
  691. ProtoWriter.WriteInt32(value, writer);
  692. }
  693. /// <summary>
  694. /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  695. /// </summary>
  696. public static void WriteUInt16(ushort value, ProtoWriter writer)
  697. {
  698. ProtoWriter.WriteUInt32(value, writer);
  699. }
  700. /// <summary>
  701. /// Writes an unsigned 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
  702. /// </summary>
  703. public static void WriteByte(byte value, ProtoWriter writer)
  704. {
  705. ProtoWriter.WriteUInt32(value, writer);
  706. }
  707. /// <summary>
  708. /// Writes a signed 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  709. /// </summary>
  710. public static void WriteSByte(sbyte value, ProtoWriter writer)
  711. {
  712. ProtoWriter.WriteInt32(value, writer);
  713. }
  714. private static void WriteInt32ToBuffer(int value, byte[] buffer, int index)
  715. {
  716. buffer[index] = (byte)value;
  717. buffer[index + 1] = (byte)(value >> 8);
  718. buffer[index + 2] = (byte)(value >> 16);
  719. buffer[index + 3] = (byte)(value >> 24);
  720. }
  721. /// <summary>
  722. /// Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
  723. /// </summary>
  724. public static void WriteInt32(int value, ProtoWriter writer)
  725. {
  726. byte[] buffer;
  727. int index;
  728. if (writer == null) throw new ArgumentNullException("writer");
  729. switch (writer.wireType)
  730. {
  731. case WireType.Fixed32:
  732. DemandSpace(4, writer);
  733. WriteInt32ToBuffer(value, writer.ioBuffer, writer.ioIndex);
  734. IncrementedAndReset(4, writer);
  735. return;
  736. case WireType.Fixed64:
  737. DemandSpace(8, writer);
  738. buffer = writer.ioBuffer;
  739. index = writer.ioIndex;
  740. buffer[index] = (byte)value;
  741. buffer[index + 1] = (byte)(value >> 8);
  742. buffer[index + 2] = (byte)(value >> 16);
  743. buffer[index + 3] = (byte)(value >> 24);
  744. buffer[index + 4] = buffer[index + 5] =
  745. buffer[index + 6] = buffer[index + 7] = 0;
  746. IncrementedAndReset(8, writer);
  747. return;
  748. case WireType.SignedVariant:
  749. WriteUInt32Variant(Zig(value), writer);
  750. writer.wireType = WireType.None;
  751. return;
  752. case WireType.Variant:
  753. if (value >= 0)
  754. {
  755. WriteUInt32Variant((uint)value, writer);
  756. writer.wireType = WireType.None;
  757. }
  758. else
  759. {
  760. DemandSpace(10, writer);
  761. buffer = writer.ioBuffer;
  762. index = writer.ioIndex;
  763. buffer[index] = (byte)(value | 0x80);
  764. buffer[index + 1] = (byte)((value >> 7) | 0x80);
  765. buffer[index + 2] = (byte)((value >> 14) | 0x80);
  766. buffer[index + 3] = (byte)((value >> 21) | 0x80);
  767. buffer[index + 4] = (byte)((value >> 28) | 0x80);
  768. buffer[index + 5] = buffer[index + 6] =
  769. buffer[index + 7] = buffer[index + 8] = (byte)0xFF;
  770. buffer[index + 9] = (byte)0x01;
  771. IncrementedAndReset(10, writer);
  772. }
  773. return;
  774. default:
  775. throw CreateException(writer);
  776. }
  777. }
  778. /// <summary>
  779. /// Writes a double-precision number to the stream; supported wire-types: Fixed32, Fixed64
  780. /// </summary>
  781. public
  782. #if !FEAT_SAFE
  783. unsafe
  784. #endif
  785. static void WriteDouble(double value, ProtoWriter writer)
  786. {
  787. if (writer == null) throw new ArgumentNullException("writer");
  788. switch (writer.wireType)
  789. {
  790. case WireType.Fixed32:
  791. float f = (float)value;
  792. if (Helpers.IsInfinity(f)
  793. && !Helpers.IsInfinity(value))
  794. {
  795. throw new OverflowException();
  796. }
  797. ProtoWriter.WriteSingle(f, writer);
  798. return;
  799. case WireType.Fixed64:
  800. #if FEAT_SAFE
  801. ProtoWriter.WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(value), 0), writer);
  802. #else
  803. ProtoWriter.WriteInt64(*(long*)&value, writer);
  804. #endif
  805. return;
  806. default:
  807. throw CreateException(writer);
  808. }
  809. }
  810. /// <summary>
  811. /// Writes a single-precision number to the stream; supported wire-types: Fixed32, Fixed64
  812. /// </summary>
  813. public
  814. #if !FEAT_SAFE
  815. unsafe
  816. #endif
  817. static void WriteSingle(float value, ProtoWriter writer)
  818. {
  819. if (writer == null) throw new ArgumentNullException("writer");
  820. switch (writer.wireType)
  821. {
  822. case WireType.Fixed32:
  823. #if FEAT_SAFE
  824. ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
  825. #else
  826. ProtoWriter.WriteInt32(*(int*)&value, writer);
  827. #endif
  828. return;
  829. case WireType.Fixed64:
  830. ProtoWriter.WriteDouble((double)value, writer);
  831. return;
  832. default:
  833. throw CreateException(writer);
  834. }
  835. }
  836. /// <summary>
  837. /// Throws an exception indicating that the given enum cannot be mapped to a serialized value.
  838. /// </summary>
  839. public static void ThrowEnumException(ProtoWriter writer, object enumValue)
  840. {
  841. if (writer == null) throw new ArgumentNullException("writer");
  842. string rhs = enumValue == null ? "<null>" : (enumValue.GetType().FullName + "." + enumValue.ToString());
  843. throw new ProtoException("No wire-value is mapped to the enum " + rhs + " at position " + writer.position.ToString());
  844. }
  845. // general purpose serialization exception message
  846. internal static Exception CreateException(ProtoWriter writer)
  847. {
  848. if (writer == null) throw new ArgumentNullException("writer");
  849. return new ProtoException("Invalid serialization operation with wire-type " + writer.wireType.ToString() + " at position " + writer.position.ToString());
  850. }
  851. /// <summary>
  852. /// Writes a boolean to the stream; supported wire-types: Variant, Fixed32, Fixed64
  853. /// </summary>
  854. public static void WriteBoolean(bool value, ProtoWriter writer)
  855. {
  856. ProtoWriter.WriteUInt32(value ? (uint)1 : (uint)0, writer);
  857. }
  858. /// <summary>
  859. /// Copies any extension data stored for the instance to the underlying stream
  860. /// </summary>
  861. public static void AppendExtensionData(IExtensible instance, ProtoWriter writer)
  862. {
  863. if (instance == null) throw new ArgumentNullException("instance");
  864. if (writer == null) throw new ArgumentNullException("writer");
  865. // we expect the writer to be raw here; the extension data will have the
  866. // header detail, so we'll copy it implicitly
  867. if(writer.wireType != WireType.None) throw CreateException(writer);
  868. IExtension extn = instance.GetExtensionObject(false);
  869. if (extn != null)
  870. {
  871. // unusually we *don't* want "using" here; the "finally" does that, with
  872. // the extension object being responsible for disposal etc
  873. Stream source = extn.BeginQuery();
  874. try
  875. {
  876. CopyRawFromStream(source, writer);
  877. }
  878. finally { extn.EndQuery(source); }
  879. }
  880. }
  881. private int packedFieldNumber;
  882. /// <summary>
  883. /// Used for packed encoding; indicates that the next field should be skipped rather than
  884. /// a field header written. Note that the field number must match, else an exception is thrown
  885. /// when the attempt is made to write the (incorrect) field. The wire-type is taken from the
  886. /// subsequent call to WriteFieldHeader. Only primitive types can be packed.
  887. /// </summary>
  888. public static void SetPackedField(int fieldNumber, ProtoWriter writer)
  889. {
  890. if (fieldNumber <= 0) throw new ArgumentOutOfRangeException("fieldNumber");
  891. if (writer == null) throw new ArgumentNullException("writer");
  892. writer.packedFieldNumber = fieldNumber;
  893. }
  894. internal string SerializeType(System.Type type)
  895. {
  896. return TypeModel.SerializeType(model, type);
  897. }
  898. /// <summary>
  899. /// Specifies a known root object to use during reference-tracked serialization
  900. /// </summary>
  901. public void SetRootObject(object value)
  902. {
  903. NetCache.SetKeyedObject(NetObjectCache.Root, value);
  904. }
  905. /// <summary>
  906. /// Writes a Type to the stream, using the model's DynamicTypeFormatting if appropriate; supported wire-types: String
  907. /// </summary>
  908. public static void WriteType(System.Type value, ProtoWriter writer)
  909. {
  910. if (writer == null) throw new ArgumentNullException("writer");
  911. WriteString(writer.SerializeType(value), writer);
  912. }
  913. }
  914. }