+ $this->finalPackageChunks['is_complete'] = TRUE;
+ }
+
+ /**
+ * Sorts the chunks array by using the serial number as a sorting key. In
+ * most situations a call of ksort() is enough to accomblish this. So this
+ * method may only call ksort() on the chunks array.
+ *
+ * This method sorts 'content' and 'hashes' so both must have used the
+ * serial numbers as array indexes.
+ *
+ * @return void
+ */
+ private function sortChunksArray () {
+ // Sort 'content' first
+ ksort($this->finalPackageChunks['content']);
+
+ // ... then 'hashes'
+ ksort($this->finalPackageChunks['hashes']);
+ }
+
+ /**
+ * Prepares the package assemble by removing last chunks (last shall be
+ * hash chunk, pre-last shall be EOP chunk) and verify that all serial
+ * numbers are valid (same as PackageFragmenter class would generate).
+ *
+ * @return void
+ */
+ private function preparePackageAssmble () {
+ // Make sure both arrays have same count (this however should always be TRUE)
+ assert(count($this->finalPackageChunks['hashes']) == count($this->finalPackageChunks['content']));
+ //* DIE: */ exit(__METHOD__ . ':finalPackageChunks='.print_r($this->finalPackageChunks['content'], TRUE));
+
+ /*
+ * Remove last element (hash chunk) from 'hashes'. This hash will never
+ * be needed, so ignore it.
+ */
+ array_pop($this->finalPackageChunks['hashes']);
+
+ // ... and from 'content' as well but save it for later use
+ $this->chunkHashes = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::HASH_CHUNK_IDENTIFIER)));
+
+ // Remove EOP chunk and keep a copy of it
+ array_pop($this->finalPackageChunks['hashes']);
+ $this->eopChunk = explode(PackageFragmenter::CHUNK_HASH_SEPARATOR, substr(array_pop($this->finalPackageChunks['content']), strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)));
+
+ // Verify all serial numbers
+ $this->verifyChunkSerialNumbers();
+ }
+
+ /**
+ * Verifies all chunk serial numbers by using a freshly initialized
+ * fragmenter instance. Do ALWAYS sort the array and array_pop() the hash
+ * chunk before calling this method to avoid re-requests of many chunks.
+ *
+ * @return void
+ */
+ private function verifyChunkSerialNumbers () {
+ // Reset the serial number generator
+ $this->getFragmenterInstance()->resetSerialNumber();
+
+ // "Walk" through all (content) chunks
+ foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
+ // Get next serial number
+ $nextSerial = $this->getFragmenterInstance()->getNextHexSerialNumber();
+
+ // Debug output
+ //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ',nextSerial=' . $nextSerial);
+
+ // Is it not the same? Then re-request it
+ if ($serialNumber != $nextSerial) {
+ // This is invalid, so remove it
+ unset($this->finalPackageChunks['content'][$serialNumber]);
+ unset($this->finalPackageChunks['hashes'][$serialNumber]);
+
+ // And re-request it with valid serial number (and hash chunk)
+ $this->rerequestChunkBySerialNumber($nextSerial);
+ } // END - if
+ } // END - foreach
+ }
+
+ /**
+ * Assembles and verifies ("final check") chunks back together to the
+ * original package (raw data for the start). This method should only be
+ * called AFTER the EOP and final-chunk chunk have been removed.
+ *
+ * @return void
+ */
+ private function assembleAllChunksToPackage () {
+ // If chunkHashes is not filled, don't continue
+ assert(count($this->chunkHashes) > 0);
+
+ // Init raw package data string
+ $this->rawPackageData = '';
+
+ // That went well, so start assembling all chunks
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Handling ' . count($this->finalPackageChunks['content']) . ' entries ...');
+ foreach ($this->finalPackageChunks['content'] as $serialNumber => $content) {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $serialNumber . ' - validating ...');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('finalPackageChunks=' . print_r($this->finalPackageChunks, TRUE) . 'chunkHashes=' . print_r($this->chunkHashes, TRUE));
+
+ // Is this chunk valid? This should be the case
+ assert($this->isChunkHashValid(array(
+ self::CHUNK_SPLITS_INDEX_HASH => $this->finalPackageChunks['hashes'][$serialNumber],
+ self::CHUNK_SPLITS_INDEX_RAW_DATA => $content
+ )));
+
+ // ... and is also in the hash chunk?
+ assert(in_array($this->finalPackageChunks['hashes'][$serialNumber], $this->chunkHashes));
+
+ // Verification okay, add it to the raw data
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Adding ' . strlen($content) . ' bytes as raw package data ...');
+ $this->rawPackageData .= $content;
+ } // END - foreach
+
+ // Debug output
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',index=' . (count($this->chunkHashes) - 2) . ',chunkHashes='.print_r($this->chunkHashes, TRUE));
+
+ // The last chunk hash must match with the one from eopChunk[1]
+ assert($this->eopChunk[1] == $this->chunkHashes[count($this->chunkHashes) - 2]);
+ }
+
+ /**
+ * Verifies the finally assembled raw package data by comparing it against
+ * the final hash.
+ *
+ * @return void
+ */
+ private function verifyRawPackageData () {
+ // Hash the raw package data for final verification
+ $finalHash = $this->getCryptoInstance()->hashString($this->rawPackageData, $this->eopChunk[0], FALSE);
+
+ // Is it the same?
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: eopChunk[1]=' . $this->eopChunk[1] . ',finalHash=' . $finalHash);
+ assert($finalHash == $this->eopChunk[0]);