UserAttributeSubpacket.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  7. {
  8. /**
  9. * Basic type for a user attribute sub-packet.
  10. */
  11. public class UserAttributeSubpacket
  12. {
  13. internal readonly UserAttributeSubpacketTag type;
  14. private readonly bool longLength; // we preserve this as not everyone encodes length properly.
  15. protected readonly byte[] data;
  16. protected internal UserAttributeSubpacket(UserAttributeSubpacketTag type, byte[] data)
  17. : this(type, false, data)
  18. {
  19. }
  20. protected internal UserAttributeSubpacket(UserAttributeSubpacketTag type, bool forceLongLength, byte[] data)
  21. {
  22. this.type = type;
  23. this.longLength = forceLongLength;
  24. this.data = data;
  25. }
  26. public virtual UserAttributeSubpacketTag SubpacketType
  27. {
  28. get { return type; }
  29. }
  30. /**
  31. * return the generic data making up the packet.
  32. */
  33. public virtual byte[] GetData()
  34. {
  35. return data;
  36. }
  37. public virtual void Encode(Stream os)
  38. {
  39. int bodyLen = data.Length + 1;
  40. if (bodyLen < 192 && !longLength)
  41. {
  42. os.WriteByte((byte)bodyLen);
  43. }
  44. else if (bodyLen <= 8383 && !longLength)
  45. {
  46. bodyLen -= 192;
  47. os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
  48. os.WriteByte((byte)bodyLen);
  49. }
  50. else
  51. {
  52. os.WriteByte(0xff);
  53. os.WriteByte((byte)(bodyLen >> 24));
  54. os.WriteByte((byte)(bodyLen >> 16));
  55. os.WriteByte((byte)(bodyLen >> 8));
  56. os.WriteByte((byte)bodyLen);
  57. }
  58. os.WriteByte((byte) type);
  59. os.Write(data, 0, data.Length);
  60. }
  61. public override bool Equals(
  62. object obj)
  63. {
  64. if (obj == this)
  65. return true;
  66. UserAttributeSubpacket other = obj as UserAttributeSubpacket;
  67. if (other == null)
  68. return false;
  69. return type == other.type
  70. && Arrays.AreEqual(data, other.data);
  71. }
  72. public override int GetHashCode()
  73. {
  74. return type.GetHashCode() ^ Arrays.GetHashCode(data);
  75. }
  76. }
  77. }
  78. #pragma warning restore
  79. #endif