Skip to content

Instantly share code, notes, and snippets.

@blubberdiblub
Last active August 29, 2015 14:05
Show Gist options
  • Save blubberdiblub/3c745c7fcfa626b0c9ea to your computer and use it in GitHub Desktop.
Save blubberdiblub/3c745c7fcfa626b0c9ea to your computer and use it in GitHub Desktop.
minecraft packet dissector
@handler('read', channel=channel_upstream)
def read(self, data):
offset = 0
while True:
shift = 0
size = 0
while True
if offset >= len(data):
data = yield SPECIAL_VALUE
offset = 0
byte = indexbytes(data, offset)
offset += 1
size |= (byte & 0x7F) << shift
if not (byte & 0x80):
break
shift += 7
buffer = bytearray()
while True
end = offset + size - len(buffer)
buffer.extend(data[offset:end])
if len(buffer) >= size:
offset = end
break
data = yield SPECIAL_VALUE
offset = 0
self.fire(read(buffer), self.channel_downstream)
yield # normal yield without waiting for a new read event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment