def read_value(self, istream): if self.length is not self.LENGTH: raise errors.ReadValueError(Integer.__name__, 'length', self.LENGTH, self.length) self.value = unpack(self.pack_string, istream.read(self.length))[0] pad = unpack(self.pack_string, istream.read(self.padding_length))[0] if pad is not 0: raise errors.ReadValueError(Integer.__name__, 'pad', 0, pad) self.validate()
def read_type(self, istream): # Read in the bytes for the type tts = istream.read(self.TYPE_SIZE) num_bytes = len(tts) if num_bytes != self.TYPE_SIZE: min_bytes = 'a minimum of {0} bytes'.format(self.TYPE_SIZE) raise errors.ReadValueError(Base.__name__, 'type', min_bytes, '{0} bytes'.format(num_bytes)) typ = unpack('!B', tts)[0] enum_typ = enums.Types(typ) if enum_typ is not self.type: raise errors.ReadValueError(Base.__name__, 'type', self.type.value, typ)
def read_length(self, istream): # Read in the bytes for the length lst = istream.read(self.LENGTH_SIZE) num_bytes = len(lst) if num_bytes != self.LENGTH_SIZE: min_bytes = 'a minimum of {0} bytes'.format(self.LENGTH_SIZE) raise errors.ReadValueError(Base.__name__, 'length', min_bytes, '{0} bytes'.format(num_bytes)) self.length = unpack('!I', lst)[0]
def read_tag(self, istream): # Read in the bytes for the tag tts = istream.read(self.TAG_SIZE) tag = unpack('!I', b'\x00' + tts[0:self.TAG_SIZE])[0] enum_tag = enums.Tags(tag) # Verify that the tag matches for the current object if enum_tag is not self.tag: raise errors.ReadValueError(Base.__name__, 'tag', hex(self.tag.value), hex(tag))
def read_value(self, istream): # Read string text self.value = '' for _ in range(self.length): c = unpack(self.BYTE_FORMAT, istream.read(1))[0] if sys.version >= '3': c = c.decode() self.value += c # Read padding and check content self.padding_length = self.PADDING_SIZE - (self.length % self.PADDING_SIZE) if self.padding_length < self.PADDING_SIZE: for _ in range(self.padding_length): pad = unpack('!B', istream.read(1))[0] if pad is not 0: raise errors.ReadValueError(TextString.__name__, 'pad', 0, pad)
def read_value(self, istream): # Read bytes into bytearray data = bytearray() for _ in range(self.length): data.append(istream.read(1)[0]) self.value = bytes(data) # Read padding and check content self.padding_length = self.PADDING_SIZE - (self.length % self.PADDING_SIZE) if self.padding_length == self.PADDING_SIZE: self.padding_length = 0 if self.padding_length < self.PADDING_SIZE: for _ in range(self.padding_length): pad = unpack('!B', istream.read(1))[0] if pad is not 0: raise errors.ReadValueError(TextString.__name__, 'pad', 0, pad)