def findSynchronizeBits(parser, start, max_size): """ Find synchronisation bits (11 bits set to 1) Returns None on error, or number of bytes before the synchronization. """ address0 = parser.absolute_address end = start + max_size size = 0 while start < end: # Fast search: search 0xFF (first byte of sync frame field) length = parser.stream.searchBytesLength("\xff", False, start, end) if length is None: return None size += length start += length * 8 # Strong validation of frame: create the frame # and call method isValid() try: frame = createOrphanField(parser, start - address0, Frame, "frame") valid = frame.isValid() except HACHOIR_ERRORS: valid = False if valid: return size # Invalid frame: continue start += 8 size += 1 return None
def findSynchronizeBits(parser, start, max_size): """ Find synchronisation bits (11 bits set to 1) Returns None on error, or number of bytes before the synchronization. """ address0 = parser.absolute_address end = start + max_size size = 0 while start < end: # Fast search: search 0xFF (first byte of sync frame field) length = parser.stream.searchBytesLength("\xff", False, start, end) if length is None: return None size += length start += length * 8 # Strong validation of frame: create the frame # and call method isValid() try: frame = createOrphanField(parser, start-address0, Frame, "frame") valid = frame.isValid() except HACHOIR_ERRORS: valid = False if valid: return size # Invalid frame: continue start += 8 size += 1 return None
def createLastPage(self): start = self[0].size end = MAX_FILESIZE * 8 if True: # FIXME: This doesn't work on all files (eg. some Ogg/Theora) offset = self.stream.searchBytes("OggS\0\5", start, end) if offset is None: offset = self.stream.searchBytes("OggS\0\4", start, end) if offset is None: return None return createOrphanField(self, offset, OggPage, "page") else: # Very slow version page = None while True: offset = self.stream.searchBytes("OggS\0", start, end) if offset is None: break page = createOrphanField(self, offset, OggPage, "page") start += page.size return page
def createContentSize(self): # Get "frames" field field = self[0] if field.name != "frames": try: field = self[1] except MissingField: # File only contains ID3v1 or ID3v2 return field.size # Error: second field are not the frames"? if field.name != "frames": return None # Go to last frame frames = field frame = frames["frame[0]"] address0 = field.absolute_address size = address0 + frame.size while True: try: # Parse one MPEG audio frame frame = createOrphanField(frames, size - address0, Frame, "frame") # Check frame 32 bits header if not frame.isValid(): break except HACHOIR_ERRORS: break if MAX_FILESIZE < (size + frame.size): break size += frame.size # ID3v1 at the end? try: if self.stream.readBytes(size, 3) == "TAG": size += ID3v1.static_size except InputStreamError: pass return size