NBTConnectionReceiveBuffer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Utilities;
  10. namespace SMBLibrary.NetBios
  11. {
  12. public class NBTConnectionReceiveBuffer
  13. {
  14. private byte[] m_buffer;
  15. private int m_readOffset = 0;
  16. private int m_bytesInBuffer = 0;
  17. private int? m_packetLength;
  18. public NBTConnectionReceiveBuffer() : this(SessionPacket.MaxSessionPacketLength)
  19. {
  20. }
  21. /// <param name="bufferLength">Must be large enough to hold the largest possible NBT packet</param>
  22. public NBTConnectionReceiveBuffer(int bufferLength)
  23. {
  24. if (bufferLength < SessionPacket.MaxSessionPacketLength)
  25. {
  26. throw new ArgumentException("bufferLength must be large enough to hold the largest possible NBT packet");
  27. }
  28. m_buffer = new byte[bufferLength];
  29. }
  30. public void SetNumberOfBytesReceived(int numberOfBytesReceived)
  31. {
  32. m_bytesInBuffer += numberOfBytesReceived;
  33. }
  34. public bool HasCompletePacket()
  35. {
  36. if (m_bytesInBuffer >= 4)
  37. {
  38. if (!m_packetLength.HasValue)
  39. {
  40. byte flags = ByteReader.ReadByte(m_buffer, m_readOffset + 1);
  41. int trailerLength = (flags & 0x01) << 16 | BigEndianConverter.ToUInt16(m_buffer, m_readOffset + 2);
  42. m_packetLength = 4 + trailerLength;
  43. }
  44. return m_bytesInBuffer >= m_packetLength.Value;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// HasCompletePacket must be called and return true before calling DequeuePacket
  50. /// </summary>
  51. /// <exception cref="System.IO.InvalidDataException"></exception>
  52. public SessionPacket DequeuePacket()
  53. {
  54. SessionPacket packet;
  55. try
  56. {
  57. packet = SessionPacket.GetSessionPacket(m_buffer, m_readOffset);
  58. }
  59. catch (IndexOutOfRangeException ex)
  60. {
  61. throw new System.IO.InvalidDataException("Invalid Packet", ex);
  62. }
  63. RemovePacketBytes();
  64. return packet;
  65. }
  66. /// <summary>
  67. /// HasCompletePDU must be called and return true before calling DequeuePDUBytes
  68. /// </summary>
  69. public byte[] DequeuePacketBytes()
  70. {
  71. byte[] packetBytes = ByteReader.ReadBytes(m_buffer, m_readOffset, m_packetLength.Value);
  72. RemovePacketBytes();
  73. return packetBytes;
  74. }
  75. private void RemovePacketBytes()
  76. {
  77. m_bytesInBuffer -= m_packetLength.Value;
  78. if (m_bytesInBuffer == 0)
  79. {
  80. m_readOffset = 0;
  81. m_packetLength = null;
  82. }
  83. else
  84. {
  85. m_readOffset += m_packetLength.Value;
  86. m_packetLength = null;
  87. if (!HasCompletePacket())
  88. {
  89. Array.Copy(m_buffer, m_readOffset, m_buffer, 0, m_bytesInBuffer);
  90. m_readOffset = 0;
  91. }
  92. }
  93. }
  94. public byte[] Buffer
  95. {
  96. get
  97. {
  98. return m_buffer;
  99. }
  100. }
  101. public int WriteOffset
  102. {
  103. get
  104. {
  105. return m_readOffset + m_bytesInBuffer;
  106. }
  107. }
  108. public int BytesInBuffer
  109. {
  110. get
  111. {
  112. return m_bytesInBuffer;
  113. }
  114. }
  115. public int AvailableLength
  116. {
  117. get
  118. {
  119. return m_buffer.Length - (m_readOffset + m_bytesInBuffer);
  120. }
  121. }
  122. }
  123. }