Example #1
0
        def loadChunks(self):
                """
                Loads the headers of the chunks to prepare for listing|extract
                """

                # are the chunks out of order in regards to image order?
                disorder = 0
                last = -1
                dev = -1

                while True:

                        # Read each segment's header
                        chunk = UNDZChunk(self, self.dzfile)
                        self.chunks.append(chunk)

                        # check ordering
                        if dev > chunk.getDev():
                                if chunk.getChunkName()[-4:] != ".img":
                                        disorder += 1
                                        dev = chunk.getDev()
                        elif dev < chunk.getDev():
                                last = -1
                                dev = chunk.getDev()

                        if last > chunk.getTargetStart():
                                disorder += 1
                        last = chunk.getTargetStart()

                        # Would seeking the file to the end of the compressed
                        # data bring us to the end of the file, or beyond it?
                        next = chunk.getNext()
                        if next >= int(self.length):
                                break

                        # Seek to next DZ header
                        self.dzfile.seek(next, io.SEEK_SET)

                # If I'm perverse enough to think of this...
                if disorder > 0:
                        print("[ ] Warning: Found {:d} out of order chunks (please report)".format(disorder), file=sys.stderr)

                # They're in the order to write, not block order though
                self.chunks.sort(key=lambda c: (c.getTargetStart() + (c.getDev()<<48)))

                try:
                        emptycount = 0
                        g = gpt.GPT(self.chunks[0].extract())
                        ordered = range(len(g.slices)) if g.ordered else range(len(g.slices)).sort(key=lambda s: g.slices[s].startLBA)

                        self.shiftLBA = g.shiftLBA

                        next = g.dataStartLBA
                        slice = UNDZSlice(self, 0, self.chunks[0].getSliceName(), 0, next<<self.shiftLBA)
                        self.slices.append(slice)
                        self.sliceIdx[self.chunks[0].getSliceName()] = slice

                        index = 0
                        while index < len(g.slices):
                                if g.slices[index].type == UUID(int=0):
                                        del g.slices[index]
                                else:
                                        index += 1

                        index = 1
                        for slice in g.slices:
                                new = UNDZSlice(self, index, slice.name, slice.startLBA<<self.shiftLBA, (slice.endLBA+1)<<self.shiftLBA)
                                self.slices.append(new)
                                self.sliceIdx[slice.name] = new
                                index += 1

                        for i in range(len(g.slices)):
                                if next != g.slices[i].startLBA:
                                        print("[!] Unallocated space found. Slice, Start, Size, End: " + str(next) + " " + str((g.slices[i].startLBA - next)<<self.shiftLBA), next<<self.shiftLBA, (g.slices[i].startLBA-1)<<self.shiftLBA)
                                        emptycount += 1
                                next = g.slices[i].endLBA+1

                        if next != g.dataEndLBA+1:
                                new = UNDZSlice(self, None, "_unallocated_" + str(emptycount), next<<self.shiftLBA, g.dataEndLBA<<self.shiftLBA)
                                self.slices.append(new)
                                emptycount += 1
                                next = g.dataEndLBA+1

                        slice = UNDZSlice(self, index, self.chunks[-1].getSliceName(), g.dataEndLBA<<self.shiftLBA, (g.altLBA+1)<<self.shiftLBA)
                        self.slices.append(slice)
                        self.sliceIdx[self.chunks[-1].getSliceName()] = slice

                except gpt.NoGPT as err:
                        print("[!] Unable to find GPT in DZ file: {:s}".format(err))
                        pass

                for chunk in self.chunks:
                        self.addChunk(chunk)
Example #2
0
	def loadChunks(self):
		"""
		Loads the headers of the chunks to prepare for listing|extract
		"""

		# are the chunks out of order in regards to image order?
		disorder = 0
		last = -1
		order = -1

		while True:

			# Read each segment's header
			chunk = UNDZChunk(self, self.dzfile)
			self.chunks.append(chunk)

			# check ordering
			if order > chunk.getOrder():
				if chunk.getChunkName()[-4:] != ".img":
					disorder += 1
					order = chunk.getOrder()
			elif order < chunk.getOrder():
				last = -1
				order = chunk.getOrder()

			if last > chunk.getTargetStart():
				disorder += 1
			last = chunk.getTargetStart()

			# Would seeking the file to the end of the compressed
			# data bring us to the end of the file, or beyond it?
			next = chunk.getNext()
			if next >= int(self.length):
				break

			# Seek to next DZ header
			self.dzfile.seek(next, io.SEEK_SET)

		# If I'm perverse enough to think of this...
		if disorder > 0:
			print("[ ] Warning: Found {:d} out of order chunks (please report)".format(disorder), file=sys.stderr)

		# They're in the order to write, not block order though
		self.chunks.sort(key=lambda c: c.getTargetStart())

		try:
			emptycount = 0
			g = gpt.GPT(self.chunks[0].extract())

			self.shiftLBA = g.shiftLBA

			next = g.dataStartLBA
			slice = UNDZSlice(self, self.chunks[0].getSliceName(), 0, next<<g.shiftLBA)
			self.slices.append(slice)
			self.sliceIdx[self.chunks[0].getSliceName()] = slice

			for slice in g.slices:
				if next != slice.startLBA<<g.shiftLBA:
					new = UNDZSlice(self, "_unallocated_" + str(emptycount), next<<g.shiftLBA, (slice.startLBA-1)<<g.shiftLBA)
					self.slices.append(new)
					emptycount += 1
				next = (slice.endLBA+1)<<g.shiftLBA
				new = UNDZSlice(self, slice.name, slice.startLBA<<g.shiftLBA, next)
				self.slices.append(new)
				self.sliceIdx[slice.name] = new

			if next != (g.dataEndLBA+1)<<g.shiftLBA:
				new = UNDZSlice(self, "_unallocated_" + str(emptycount), next, g.dataEndLBA<<g.shiftLBA)
				self.slices.append(new)
				emptycount += 1
				next = (g.dataEndLBA+1)<<g.shiftLBA

			slice = UNDZSlice(self, self.chunks[-1].getSliceName(), g.dataEndLBA<<g.shiftLBA, (g.altLBA+1)<<g.shiftLBA)
			self.slices.append(slice)
			self.sliceIdx[self.chunks[-1].getSliceName()] = slice

		except NoGPT:
			pass

		for chunk in self.chunks:
			self.addChunk(chunk)