2016-08-26 13:58:53 +03:00
|
|
|
from io import BytesIO, BufferedWriter
|
|
|
|
from struct import pack
|
|
|
|
|
|
|
|
|
|
|
|
class BinaryWriter:
|
|
|
|
"""
|
|
|
|
Small utility class to write binary data.
|
|
|
|
Also creates a "Memory Stream" if necessary
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, stream=None):
|
|
|
|
if not stream:
|
|
|
|
stream = BytesIO()
|
|
|
|
|
|
|
|
self.stream = stream
|
|
|
|
self.writer = BufferedWriter(self.stream)
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count = 0
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# region Writing
|
|
|
|
|
2016-09-03 11:54:58 +03:00
|
|
|
# "All numbers are written as little endian." |> Source: https://core.telegram.org/mtproto
|
2016-08-27 12:59:23 +03:00
|
|
|
def write_byte(self, value):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes a single byte value"""
|
2016-08-27 12:59:23 +03:00
|
|
|
self.writer.write(pack('B', value))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += 1
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-27 12:59:23 +03:00
|
|
|
def write_int(self, value, signed=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes an integer value (4 bytes), which can or cannot be signed"""
|
2016-09-03 11:54:58 +03:00
|
|
|
self.writer.write(int.to_bytes(value, length=4, byteorder='little', signed=signed))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += 4
|
2016-08-27 12:59:23 +03:00
|
|
|
|
|
|
|
def write_long(self, value, signed=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes a long integer value (8 bytes), which can or cannot be signed"""
|
2016-09-03 11:54:58 +03:00
|
|
|
self.writer.write(int.to_bytes(value, length=8, byteorder='little', signed=signed))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += 8
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-08-27 12:59:23 +03:00
|
|
|
def write_float(self, value):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes a floating point value (4 bytes)"""
|
2016-09-03 11:54:58 +03:00
|
|
|
self.writer.write(pack('<f', value))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += 4
|
2016-08-27 12:59:23 +03:00
|
|
|
|
|
|
|
def write_double(self, value):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes a floating point value (8 bytes)"""
|
2016-09-03 11:54:58 +03:00
|
|
|
self.writer.write(pack('<d', value))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += 8
|
2016-08-27 12:59:23 +03:00
|
|
|
|
2016-08-30 14:11:19 +03:00
|
|
|
def write_large_int(self, value, bits, signed=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes a n-bits long integer value"""
|
2016-09-03 11:54:58 +03:00
|
|
|
self.writer.write(int.to_bytes(value, length=bits // 8, byteorder='little', signed=signed))
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += bits // 8
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def write(self, data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes the given bytes array"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self.writer.write(data)
|
2016-09-07 20:48:49 +03:00
|
|
|
self.written_count += len(data)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Telegram custom writing
|
|
|
|
|
|
|
|
def tgwrite_bytes(self, data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Write bytes by using Telegram guidelines"""
|
2016-08-26 13:58:53 +03:00
|
|
|
if len(data) < 254:
|
|
|
|
padding = (len(data) + 1) % 4
|
|
|
|
if padding != 0:
|
|
|
|
padding = 4 - padding
|
|
|
|
|
|
|
|
self.write(bytes([len(data)]))
|
|
|
|
self.write(data)
|
|
|
|
|
|
|
|
else:
|
|
|
|
padding = len(data) % 4
|
|
|
|
if padding != 0:
|
|
|
|
padding = 4 - padding
|
|
|
|
|
|
|
|
self.write(bytes([254]))
|
|
|
|
self.write(bytes([len(data) % 256]))
|
|
|
|
self.write(bytes([(len(data) >> 8) % 256]))
|
|
|
|
self.write(bytes([(len(data) >> 16) % 256]))
|
|
|
|
self.write(data)
|
|
|
|
|
|
|
|
self.write(bytes(padding))
|
|
|
|
|
|
|
|
def tgwrite_string(self, string):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Write a string by using Telegram guidelines"""
|
2016-08-26 13:58:53 +03:00
|
|
|
return self.tgwrite_bytes(string.encode('utf-8'))
|
|
|
|
|
2016-09-04 12:07:18 +03:00
|
|
|
def tgwrite_bool(self, boolean):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Write a boolean value by using Telegram guidelines"""
|
2016-08-27 12:59:23 +03:00
|
|
|
# boolTrue boolFalse
|
2016-09-04 12:07:18 +03:00
|
|
|
return self.write_int(0x997275b5 if boolean else 0xbc799737, signed=False)
|
2016-08-27 12:59:23 +03:00
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
def flush(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Flush the current stream to "update" changes"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self.writer.flush()
|
|
|
|
|
|
|
|
def close(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Close the current stream"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self.writer.close()
|
|
|
|
|
|
|
|
def get_bytes(self, flush=True):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Get the current bytes array content from the buffer, optionally flushing first"""
|
2016-08-26 13:58:53 +03:00
|
|
|
if flush:
|
|
|
|
self.writer.flush()
|
2016-08-30 14:11:19 +03:00
|
|
|
return self.stream.getvalue()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2016-09-07 20:48:49 +03:00
|
|
|
def get_written_bytes_count(self):
|
|
|
|
"""Gets the count of bytes written in the buffer.
|
|
|
|
This may NOT be equal to the stream length if one was provided when initializing the writer"""
|
|
|
|
return self.written_count
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
# with block
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
self.close()
|