UserAttributeSubpacketsReader.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.Bcpg.Attr;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  8. {
  9. /**
  10. * reader for user attribute sub-packets
  11. */
  12. public class UserAttributeSubpacketsParser
  13. {
  14. private readonly Stream input;
  15. public UserAttributeSubpacketsParser(
  16. Stream input)
  17. {
  18. this.input = input;
  19. }
  20. public virtual UserAttributeSubpacket ReadPacket()
  21. {
  22. int l = input.ReadByte();
  23. if (l < 0)
  24. return null;
  25. int bodyLen = 0;
  26. bool longLength = false;
  27. if (l < 192)
  28. {
  29. bodyLen = l;
  30. }
  31. else if (l <= 223)
  32. {
  33. bodyLen = ((l - 192) << 8) + (input.ReadByte()) + 192;
  34. }
  35. else if (l == 255)
  36. {
  37. bodyLen = (input.ReadByte() << 24) | (input.ReadByte() << 16)
  38. | (input.ReadByte() << 8) | input.ReadByte();
  39. longLength = true;
  40. }
  41. else
  42. {
  43. throw new IOException("unrecognised length reading user attribute sub packet");
  44. }
  45. int tag = input.ReadByte();
  46. if (tag < 0)
  47. throw new EndOfStreamException("unexpected EOF reading user attribute sub packet");
  48. byte[] data = new byte[bodyLen - 1];
  49. if (Streams.ReadFully(input, data) < data.Length)
  50. throw new EndOfStreamException();
  51. UserAttributeSubpacketTag type = (UserAttributeSubpacketTag) tag;
  52. switch (type)
  53. {
  54. case UserAttributeSubpacketTag.ImageAttribute:
  55. return new ImageAttrib(longLength, data);
  56. }
  57. return new UserAttributeSubpacket(type, longLength, data);
  58. }
  59. }
  60. }
  61. #pragma warning restore
  62. #endif