You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.2 KiB

7 years ago
  1. # Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
  2. #
  3. # This file is part of paramiko.
  4. #
  5. # Paramiko is free software; you can redistribute it and/or modify it under the
  6. # terms of the GNU Lesser General Public License as published by the Free
  7. # Software Foundation; either version 2.1 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
  11. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  13. # details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
  17. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  18. from paramiko.common import max_byte, zero_byte
  19. from paramiko.py3compat import b, byte_ord, byte_chr, long
  20. import paramiko.util as util
  21. class BERException (Exception):
  22. pass
  23. class BER(object):
  24. """
  25. Robey's tiny little attempt at a BER decoder.
  26. """
  27. def __init__(self, content=bytes()):
  28. self.content = b(content)
  29. self.idx = 0
  30. def asbytes(self):
  31. return self.content
  32. def __str__(self):
  33. return self.asbytes()
  34. def __repr__(self):
  35. return 'BER(\'' + repr(self.content) + '\')'
  36. def decode(self):
  37. return self.decode_next()
  38. def decode_next(self):
  39. if self.idx >= len(self.content):
  40. return None
  41. ident = byte_ord(self.content[self.idx])
  42. self.idx += 1
  43. if (ident & 31) == 31:
  44. # identifier > 30
  45. ident = 0
  46. while self.idx < len(self.content):
  47. t = byte_ord(self.content[self.idx])
  48. self.idx += 1
  49. ident = (ident << 7) | (t & 0x7f)
  50. if not (t & 0x80):
  51. break
  52. if self.idx >= len(self.content):
  53. return None
  54. # now fetch length
  55. size = byte_ord(self.content[self.idx])
  56. self.idx += 1
  57. if size & 0x80:
  58. # more complimicated...
  59. # FIXME: theoretically should handle indefinite-length (0x80)
  60. t = size & 0x7f
  61. if self.idx + t > len(self.content):
  62. return None
  63. size = util.inflate_long(
  64. self.content[self.idx: self.idx + t], True)
  65. self.idx += t
  66. if self.idx + size > len(self.content):
  67. # can't fit
  68. return None
  69. data = self.content[self.idx: self.idx + size]
  70. self.idx += size
  71. # now switch on id
  72. if ident == 0x30:
  73. # sequence
  74. return self.decode_sequence(data)
  75. elif ident == 2:
  76. # int
  77. return util.inflate_long(data)
  78. else:
  79. # 1: boolean (00 false, otherwise true)
  80. msg = 'Unknown ber encoding type {:d} (robey is lazy)'
  81. raise BERException(msg.format(ident))
  82. @staticmethod
  83. def decode_sequence(data):
  84. out = []
  85. ber = BER(data)
  86. while True:
  87. x = ber.decode_next()
  88. if x is None:
  89. break
  90. out.append(x)
  91. return out
  92. def encode_tlv(self, ident, val):
  93. # no need to support ident > 31 here
  94. self.content += byte_chr(ident)
  95. if len(val) > 0x7f:
  96. lenstr = util.deflate_long(len(val))
  97. self.content += byte_chr(0x80 + len(lenstr)) + lenstr
  98. else:
  99. self.content += byte_chr(len(val))
  100. self.content += val
  101. def encode(self, x):
  102. if type(x) is bool:
  103. if x:
  104. self.encode_tlv(1, max_byte)
  105. else:
  106. self.encode_tlv(1, zero_byte)
  107. elif (type(x) is int) or (type(x) is long):
  108. self.encode_tlv(2, util.deflate_long(x))
  109. elif type(x) is str:
  110. self.encode_tlv(4, x)
  111. elif (type(x) is list) or (type(x) is tuple):
  112. self.encode_tlv(0x30, self.encode_sequence(x))
  113. else:
  114. raise BERException(
  115. 'Unknown type for encoding: {!r}'.format(type(x))
  116. )
  117. @staticmethod
  118. def encode_sequence(data):
  119. ber = BER()
  120. for item in data:
  121. ber.encode(item)
  122. return ber.asbytes()

Powered by TurnKey Linux.