Browse Source

StructReader improvements

* added read() to get raw bytes
* renamed read_format() to unpack()
* added parameter in read_cstring() to specify terminator
pull/55/head
Rossen Georgiev 9 years ago
parent
commit
86e4047582
  1. 16
      docs/api/steam.util.rst
  2. 12
      steam/core/msg/structs.py
  3. 23
      steam/util/binary.py

16
docs/api/steam.util.rst

@ -6,6 +6,14 @@ util
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
util.binary
-----------
.. automodule:: steam.util.binary
:members:
:undoc-members:
:show-inheritance:
util.throttle util.throttle
------------- -------------
@ -21,11 +29,3 @@ util.web
:members: :members:
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
util.binary
-----------
.. automodule:: steam.util.binary
:members:
:undoc-members:
:show-inheritance:

12
steam/core/msg/structs.py

@ -139,13 +139,13 @@ class ClientVACBanStatus(StructMessage):
def load(self, data): def load(self, data):
buf = StructReader(data) buf = StructReader(data)
numBans, = buf.read_format("<I") numBans, = buf.unpack("<I")
for _ in range(numBans): for _ in range(numBans):
m = self.VACBanRange() m = self.VACBanRange()
self.ranges.append(m) self.ranges.append(m)
m.start, m.end, _ = buf.read_format("<III") m.start, m.end, _ = buf.unpack("<III")
if m.start > m.end: if m.start > m.end:
m.start, m.end = m.end, m.start m.start, m.end = m.end, m.start
@ -181,7 +181,7 @@ class ClientChatMsg(StructMessage):
def load(self, data): def load(self, data):
buf = StructReader(data) buf = StructReader(data)
self.steamIdChatter, self.steamIdChatRoom, self.ChatMsgType = buf.read_format("<QQI") self.steamIdChatter, self.steamIdChatRoom, self.ChatMsgType = buf.unpack("<QQI")
self.text = buf.read_cstring().decode('utf-8') self.text = buf.read_cstring().decode('utf-8')
def __str__(self): def __str__(self):
@ -269,15 +269,15 @@ class ClientMarketingMessageUpdate2(StructMessage):
def load(self, data): def load(self, data):
buf = StructReader(data) buf = StructReader(data)
self.time, count = buf.read_format("<II") self.time, count = buf.unpack("<II")
for _ in range(count): for _ in range(count):
m = self.MarketingMessage() m = self.MarketingMessage()
self.messages.append(m) self.messages.append(m)
length, m.id = buf.read_format("<IQ") length, m.id = buf.unpack("<IQ")
m.url = buf.read_cstring().decode('utf-8') m.url = buf.read_cstring().decode('utf-8')
m.flags = buf.read_format("<I") m.flags = buf.unpack("<I")
def __str__(self): def __str__(self):
text = ["time: %s" % self.time, text = ["time: %s" % self.time,

23
steam/util/binary.py

@ -12,18 +12,29 @@ class StructReader(object):
self.data = data self.data = data
self.offset = 0 self.offset = 0
def read_cstring(self): def read(self, n=1):
"""Return n bytes
:param n: number of bytes to return
:type n: :class:`int`
:return: bytes
:rtype: :class:`bytes`
"""
self.offset += n
return self.data[self.offset - n:self.offset]
def read_cstring(self, terminator=b'\x00'):
"""Reads a single null termianted string """Reads a single null termianted string
:return: string without bytes :return: string without bytes
:rtype: :class:`bytes` :rtype: :class:`bytes`
""" """
null_index = self.data.find(b'\x00', self.offset) null_index = self.data.find(terminator, self.offset)
text = self.data[self.offset:null_index] # bytes without the null result = self.data[self.offset:null_index] # bytes without the terminator
self.offset = null_index + 1 # advanced past null self.offset = null_index + len(terminator) # advance offset past terminator
return text return result
def read_format(self, format_text): def unpack(self, format_text):
"""Unpack bytes using struct modules format """Unpack bytes using struct modules format
:param format_text: struct's module format :param format_text: struct's module format

Loading…
Cancel
Save