NBTConnectionReceiveBuffer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2014-2020 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 System.IO;
  10. using Utilities;
  11. namespace SMBLibrary.NetBios
  12. {
  13. public class NBTConnectionReceiveBuffer
  14. {
  15. private byte[] m_buffer;
  16. private int m_readOffset = 0;
  17. private int m_bytesInBuffer = 0;
  18. private int? m_packetLength;
  19. public NBTConnectionReceiveBuffer() : this(SessionPacket.MaxSessionPacketLength)
  20. {
  21. }
  22. /// <param name="bufferLength">Must be large enough to hold the largest possible NBT packet</param>
  23. public NBTConnectionReceiveBuffer(int bufferLength)
  24. {
  25. if (bufferLength < SessionPacket.MaxSessionPacketLength)
  26. {
  27. throw new ArgumentException("bufferLength must be large enough to hold the largest possible NBT packet");
  28. }
  29. m_buffer = new byte[bufferLength];
  30. }
  31. public void IncreaseBufferSize(int bufferLength)
  32. {
  33. byte[] buffer = new byte[bufferLength];
  34. if (m_bytesInBuffer > 0)
  35. {
  36. Array.Copy(m_buffer, m_readOffset, buffer, 0, m_bytesInBuffer);
  37. m_readOffset = 0;
  38. }
  39. m_buffer = buffer;
  40. }
  41. public void SetNumberOfBytesReceived(int numberOfBytesReceived)
  42. {
  43. m_bytesInBuffer += numberOfBytesReceived;
  44. }
  45. public bool HasCompletePacket()
  46. {
  47. if (m_bytesInBuffer >= 4)
  48. {
  49. if (!m_packetLength.HasValue)
  50. {
  51. m_packetLength = SessionPacket.GetSessionPacketLength(m_buffer, m_readOffset);
  52. }
  53. return m_bytesInBuffer >= m_packetLength.Value;
  54. }
  55. return false;
  56. }
  57. /// <summary>
  58. /// HasCompletePacket must be called and return true before calling DequeuePacket
  59. /// </summary>
  60. /// <exception cref="System.IO.InvalidDataException"></exception>
  61. public SessionPacket DequeuePacket()
  62. {
  63. SessionPacket packet;
  64. try
  65. {
  66. packet = SessionPacket.GetSessionPacket(m_buffer, m_readOffset);
  67. }
  68. catch (IndexOutOfRangeException ex)
  69. {
  70. throw new InvalidDataException("Invalid NetBIOS session packet", ex);
  71. }
  72. RemovePacketBytes();
  73. return packet;
  74. }
  75. /// <summary>
  76. /// HasCompletePDU must be called and return true before calling DequeuePDUBytes
  77. /// </summary>
  78. public byte[] DequeuePacketBytes()
  79. {
  80. byte[] packetBytes = ByteReader.ReadBytes(m_buffer, m_readOffset, m_packetLength.Value);
  81. RemovePacketBytes();
  82. return packetBytes;
  83. }
  84. private void RemovePacketBytes()
  85. {
  86. m_bytesInBuffer -= m_packetLength.Value;
  87. if (m_bytesInBuffer == 0)
  88. {
  89. m_readOffset = 0;
  90. m_packetLength = null;
  91. }
  92. else
  93. {
  94. m_readOffset += m_packetLength.Value;
  95. m_packetLength = null;
  96. if (!HasCompletePacket())
  97. {
  98. Array.Copy(m_buffer, m_readOffset, m_buffer, 0, m_bytesInBuffer);
  99. m_readOffset = 0;
  100. }
  101. }
  102. }
  103. public byte[] Buffer
  104. {
  105. get
  106. {
  107. return m_buffer;
  108. }
  109. }
  110. public int WriteOffset
  111. {
  112. get
  113. {
  114. return m_readOffset + m_bytesInBuffer;
  115. }
  116. }
  117. public int BytesInBuffer
  118. {
  119. get
  120. {
  121. return m_bytesInBuffer;
  122. }
  123. }
  124. public int AvailableLength
  125. {
  126. get
  127. {
  128. return m_buffer.Length - (m_readOffset + m_bytesInBuffer);
  129. }
  130. }
  131. }
  132. }