@@ -29,42 +29,35 @@ def __setitem__(self,key,value):
2929 super (PlayerDict ,self ).__setitem__ (value .pid ,value )
3030
3131
32-
32+ from StringIO import StringIO
33+ from os import SEEK_CUR ,SEEK_END
34+
3335class ByteStream (object ):
3436 """Track and return the bytes for investigative and debugging purposes"""
3537 """Most functions will return the byte_code as well when requested"""
3638
3739 def __init__ (self , stream ):
3840 self .__cbyte = 0
3941 self .cursor = - 1 #First element is 0
40- self .stream = stream . encode ( "hex" ). upper ( )
42+ self .stream = StringIO ( stream )
4143
4244 def get_big (self , number , byte_code = False ):
43- #Do a sanity check, if streams are parsed right this won't ever happen
44- if len (self .stream )- self .__cbyte < number * 2 :
45- msg = "Stream only has %s bytes left; %s bytes requested"
46- raise ValueError (msg % (self .remaining , number ) )
47-
48- #For big endian, the byte_string is the result
49- result = self .stream [self .__cbyte :self .__cbyte + number * 2 ]
50-
51- #Move the ByteStream forward
52- self .__cbyte = self .__cbyte + number * 2
53- self .cursor = self .cursor + number
54-
45+ result = self .stream .read (number )
5546 if byte_code :
5647 return result , result
5748 return result
5849
5950 def skip (self , number , byte_code = False ):
60- #This is just an alias for get_big really, still return the byte_string
51+ #This is just an alias for get_big, return the byte_string if asked
6152 #so that it can be recorded for event analysis
6253 if byte_code :
6354 return self .get_big (number )
6455 self .get_big (number )
6556
6657 def peek (self , number ):
67- return self .stream [self .__cbyte :self .__cbyte + number * 2 ]
58+ ret = self .stream .read (number )
59+ self .stream .seek (- number ,SEEK_CUR )
60+ return ret
6861
6962 def get_little (self , number , byte_code = False ):
7063 #Get a list of the next 'number' of bytes from the stream
@@ -83,8 +76,9 @@ def get_little(self, number, byte_code=False):
8376 def get_string (self , length , byte_code = False ):
8477 string , bytes = self .get_big (length , byte_code = True )
8578 if byte_code :
86- return string .decode ("hex" ), bytes
87- return string .decode ("hex" )
79+ #This would be easier if I knew what the original encoding was
80+ return string .endcode ("hex" ).decode ("hex" ), bytes
81+ return string .encode ("hex" ).decode ("hex" )
8882
8983 def get_big_int (self , number , byte_code = False ):
9084 result , byte_string = self .get_big (number , byte_code = True )
0 commit comments