Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

corpus.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. #
  3. # Common corpus functions
  4. #
  5. # SPDX-License-Identifier: curl
  6. # License-Filename: COPYING
  7. #
  8. import logging
  9. import struct
  10. log = logging.getLogger(__name__)
  11. class BaseType(object):
  12. TYPE_URL = 1
  13. TYPE_RSP1 = 2
  14. TYPE_USERNAME = 3
  15. TYPE_PASSWORD = 4
  16. TYPE_POSTFIELDS = 5
  17. TYPE_HEADER = 6
  18. TYPE_COOKIE = 7
  19. TYPE_UPLOAD1 = 8
  20. TYPE_RANGE = 9
  21. TYPE_CUSTOMREQUEST = 10
  22. TYPE_MAIL_RECIPIENT = 11
  23. TYPE_MAIL_FROM = 12
  24. class TLVEncoder(BaseType):
  25. def __init__(self, output):
  26. self.output = output
  27. def write_string(self, tlv_type, wstring):
  28. data = wstring.encode("utf-8")
  29. self.write_tlv(tlv_type, len(data), data)
  30. def write_bytes(self, tlv_type, bytedata):
  31. self.write_tlv(tlv_type, len(bytedata), bytedata)
  32. def maybe_write_string(self, tlv_type, wstring):
  33. if wstring is not None:
  34. self.write_string(tlv_type, wstring)
  35. def write_tlv(self, tlv_type, tlv_length, tlv_data=None):
  36. log.debug("Writing TLV %d, length %d, data %r",
  37. tlv_type,
  38. tlv_length,
  39. tlv_data)
  40. data = struct.pack("!H", tlv_type)
  41. self.output.write(data)
  42. data = struct.pack("!L", tlv_length)
  43. self.output.write(data)
  44. if tlv_data:
  45. self.output.write(tlv_data)
  46. class TLVDecoder(BaseType):
  47. def __init__(self, inputdata):
  48. self.inputdata = inputdata
  49. self.pos = 0
  50. self.tlv = None
  51. def __iter__(self):
  52. self.pos = 0
  53. self.tlv = None
  54. return self
  55. def __next__(self):
  56. if self.tlv:
  57. self.pos += self.tlv.total_length()
  58. if (self.pos + TLVHeader.TLV_DECODE_FMT_LEN) > len(self.inputdata):
  59. raise StopIteration
  60. # Get the next TLV
  61. self.tlv = TLVHeader(self.inputdata[self.pos:])
  62. return self.tlv
  63. next = __next__
  64. class TLVHeader(BaseType):
  65. TLV_DECODE_FMT = "!HL"
  66. TLV_DECODE_FMT_LEN = struct.calcsize(TLV_DECODE_FMT)
  67. def __init__(self, data):
  68. # Parse the data to populate the TLV fields
  69. (self.type, self.length) = struct.unpack(self.TLV_DECODE_FMT, data[0:self.TLV_DECODE_FMT_LEN])
  70. # Get the remaining data and store it.
  71. self.data = data[self.TLV_DECODE_FMT_LEN:self.TLV_DECODE_FMT_LEN + self.length]
  72. def __repr__(self):
  73. return ("{self.__class__.__name__}(type={self.type!r}, length={self.length!r}, data={self.data!r})"
  74. .format(self=self))
  75. def total_length(self):
  76. return self.TLV_DECODE_FMT_LEN + self.length