def equalFile(self, oFile): """ Compares the content of oFile with self.abContent. """ # Check the size first. try: cbFile = os.fstat(oFile.fileno()).st_size; except: return reporter.errorXcpt(); if cbFile != self.cbContent: return reporter.error('file size differs: %s, cbContent=%s' % (cbFile, self.cbContent)); # Compare the bytes next. offFile = 0; try: oFile.seek(offFile); except: return reporter.error('seek error'); while offFile < self.cbContent: cbToRead = self.cbContent - offFile; if cbToRead > 256*1024: cbToRead = 256*1024; try: abRead = oFile.read(cbToRead); except: return reporter.error('read error at offset %s' % (offFile,)); cbRead = len(abRead); if cbRead == 0: return reporter.error('premature end of file at offset %s' % (offFile,)); if not utils.areBytesEqual(abRead, self.abContent[offFile:(offFile + cbRead)]): return reporter.error('%s byte block at offset %s differs' % (cbRead, offFile,)); # Advance: offFile += cbRead; return True;
def equalMemory(self, abBuf, offFile=0): if not abBuf: return True if not self.checkRange(len(abBuf), offFile): return False if utils.areBytesEqual(abBuf, bytearray(len(abBuf))): return True cErrors = 0 offBuf = 0 while offBuf < len(abBuf): bByte = abBuf[offBuf] if not isinstance(bByte, int): bByte = ord(bByte) if bByte != 0: reporter.error('Mismatch @ %s/%s: %#x, expected 0!' % ( offFile, offBuf, bByte, )) cErrors += 1 if cErrors > 32: return False offBuf += 1 return cErrors == 0
def equalMemory(self, abBuf, offFile = 0): """ Compares the content of the given buffer with the file content at that file offset. Returns True if it matches, False + error logging if it does not match. """ if not abBuf: return True; if not self.checkRange(len(abBuf), offFile): return False; if sys.version_info[0] >= 3: if utils.areBytesEqual(abBuf, self.abContent[offFile:(offFile + len(abBuf))]): return True; else: if utils.areBytesEqual(abBuf, buffer(self.abContent, offFile, len(abBuf))): # pylint: disable=undefined-variable return True; reporter.error('mismatch with buffer @ %s LB %s (cbContent=%s)!' % (offFile, len(abBuf), self.cbContent,)); reporter.error(' type(abBuf): %s' % (type(abBuf),)); #if isinstance(abBuf, memoryview): # reporter.error(' nbytes=%s len=%s itemsize=%s type(obj)=%s' # % (abBuf.nbytes, len(abBuf), abBuf.itemsize, type(abBuf.obj),)); reporter.error('type(abContent): %s' % (type(self.abContent),)); offBuf = 0; cbLeft = len(abBuf); while cbLeft > 0: cbLine = min(16, cbLeft); abBuf1 = abBuf[offBuf:(offBuf + cbLine)]; abBuf2 = self.abContent[offFile:(offFile + cbLine)]; if not utils.areBytesEqual(abBuf1, abBuf2): try: sStr1 = self.hexFormatBytes(abBuf1); except: sStr1 = 'oops'; try: sStr2 = self.hexFormatBytes(abBuf2); except: sStr2 = 'oops'; reporter.log('%#10x: %s' % (offBuf, sStr1,)); reporter.log('%#10x: %s' % (offFile, sStr2,)); # Advance. offBuf += 16; offFile += 16; cbLeft -= 16; return False;