def mask(self, data): """ Performs the masking or unmasking operation on data using the simple masking algorithm: .. j = i MOD 4 transformed-octet-i = original-octet-i XOR masking-key-octet-j """ masked = bytearray(data) if py3k: key = self.masking_key else: key = map(ord, self.masking_key) for i in range(len(data)): masked[i] = masked[i] ^ key[i%4] return masked
def validate(self, ba): """ Incrementally validate a chunk of bytes provided as bytearray. Will return a quad (valid?, endsOnCodePoint?, currentIndex, totalIndex). As soon as an octet is encountered which renders the octet sequence invalid, a quad with valid? == False is returned. currentIndex returns the index within the currently consumed chunk, and totalIndex the index within the total consumed sequence that was the point of bail out. When valid? == True, currentIndex will be len(ba) and totalIndex the total amount of consumed bytes. """ l = len(ba) for i in range(0, l): ## optimized version of decode(), since we are not interested in actual code points self.state = Utf8Validator.UTF8VALIDATOR_DFA[256 + ( self.state << 4) + Utf8Validator.UTF8VALIDATOR_DFA[ba[i]]] if self.state == Utf8Validator.UTF8_REJECT: self.i += i return False, False, i, self.i self.i += l return True, self.state == Utf8Validator.UTF8_ACCEPT, l, self.i
def validate(self, ba): """ Incrementally validate a chunk of bytes provided as bytearray. Will return a quad (valid?, endsOnCodePoint?, currentIndex, totalIndex). As soon as an octet is encountered which renders the octet sequence invalid, a quad with valid? == False is returned. currentIndex returns the index within the currently consumed chunk, and totalIndex the index within the total consumed sequence that was the point of bail out. When valid? == True, currentIndex will be len(ba) and totalIndex the total amount of consumed bytes. """ l = len(ba) for i in range(0, l): ## optimized version of decode(), since we are not interested in actual code points self.state = Utf8Validator.UTF8VALIDATOR_DFA[ 256 + (self.state << 4) + Utf8Validator.UTF8VALIDATOR_DFA[ba[i]] ] if self.state == Utf8Validator.UTF8_REJECT: self.i += i return False, False, i, self.i self.i += l return True, self.state == Utf8Validator.UTF8_ACCEPT, l, self.i