DtlsReassembler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  7. {
  8. internal sealed class DtlsReassembler
  9. {
  10. private readonly short m_msg_type;
  11. private readonly byte[] m_body;
  12. private readonly IList m_missing = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList();
  13. internal DtlsReassembler(short msg_type, int length)
  14. {
  15. this.m_msg_type = msg_type;
  16. this.m_body = new byte[length];
  17. this.m_missing.Add(new Range(0, length));
  18. }
  19. internal short MsgType
  20. {
  21. get { return m_msg_type; }
  22. }
  23. internal byte[] GetBodyIfComplete()
  24. {
  25. return m_missing.Count > 0 ? null : m_body;
  26. }
  27. internal void ContributeFragment(short msg_type, int length, byte[] buf, int off, int fragment_offset,
  28. int fragment_length)
  29. {
  30. int fragment_end = fragment_offset + fragment_length;
  31. if (m_msg_type != msg_type || m_body.Length != length || fragment_end > length)
  32. return;
  33. if (fragment_length == 0)
  34. {
  35. // NOTE: Empty messages still require an empty fragment to complete it
  36. if (fragment_offset == 0 && m_missing.Count > 0)
  37. {
  38. Range firstRange = (Range)m_missing[0];
  39. if (firstRange.End == 0)
  40. {
  41. m_missing.RemoveAt(0);
  42. }
  43. }
  44. return;
  45. }
  46. for (int i = 0; i < m_missing.Count; ++i)
  47. {
  48. Range range = (Range)m_missing[i];
  49. if (range.Start >= fragment_end)
  50. break;
  51. if (range.End > fragment_offset)
  52. {
  53. int copyStart = System.Math.Max(range.Start, fragment_offset);
  54. int copyEnd = System.Math.Min(range.End, fragment_end);
  55. int copyLength = copyEnd - copyStart;
  56. Array.Copy(buf, off + copyStart - fragment_offset, m_body, copyStart, copyLength);
  57. if (copyStart == range.Start)
  58. {
  59. if (copyEnd == range.End)
  60. {
  61. m_missing.RemoveAt(i--);
  62. }
  63. else
  64. {
  65. range.Start = copyEnd;
  66. }
  67. }
  68. else
  69. {
  70. if (copyEnd != range.End)
  71. {
  72. m_missing.Insert(++i, new Range(copyEnd, range.End));
  73. }
  74. range.End = copyStart;
  75. }
  76. }
  77. }
  78. }
  79. internal void Reset()
  80. {
  81. m_missing.Clear();
  82. m_missing.Add(new Range(0, m_body.Length));
  83. }
  84. private sealed class Range
  85. {
  86. private int m_start, m_end;
  87. internal Range(int start, int end)
  88. {
  89. this.m_start = start;
  90. this.m_end = end;
  91. }
  92. public int Start
  93. {
  94. get { return m_start; }
  95. set { this.m_start = value; }
  96. }
  97. public int End
  98. {
  99. get { return m_end; }
  100. set { this.m_end = value; }
  101. }
  102. }
  103. }
  104. }
  105. #pragma warning restore
  106. #endif