+ /**
+ * Initializes the handler
+ *
+ * @return void
+ */
+ private function initHandler () {
+ // Noisy debug line:
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Initializing handler ...');
+
+ // Init finalPackageChunks
+ $this->finalPackageChunks = array(
+ // Array for package content
+ 'content' => array(),
+ // ... and for the hashes
+ 'hashes' => array(),
+ // ... marker for that the final array is complete for assembling all chunks
+ 'is_complete' => FALSE,
+ // ... steps done to assemble all chunks
+ 'assemble_steps' => 0,
+ );
+
+ // ... chunkHashes:
+ $this->chunkHashes = array();
+
+ // ... eopChunk:
+ $this->eopChunk = array(
+ 0 => 'INVALID',
+ 1 => 'INVALID',
+ );
+ }
+
+ /**
+ * Checks whether the hash generated from package content is the same ("valid") as given
+ *
+ * @param $chunkSplits An array from a splitted chunk
+ * @return $isValid Whether the hash is "valid"
+ */
+ private function isChunkHashValid (array $chunkSplits) {
+ // Noisy debug line:
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkSplits=' . print_r($chunkSplits, TRUE));
+
+ // Assert on some elements
+ assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA]));
+ assert(isset($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]));
+
+ // Now hash the raw data again
+ $chunkHash = $this->getCryptoInstance()->hashString($chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA], $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH], FALSE);
+
+ // Check it
+ $isValid = ($chunkSplits[self::CHUNK_SPLITS_INDEX_HASH] === $chunkHash);
+
+ // Debug output
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: chunkHash=' . $chunkHash . ',isValid=' . intval($isValid));
+
+ // ... and return it
+ return $isValid;
+ }
+
+ /**
+ * Checks whether the given serial number is valid
+ *
+ * @param $serialNumber A serial number from a chunk
+ * @return $isValid Whether the serial number is valid
+ */
+ private function isSerialNumberValid ($serialNumber) {
+ // Check it
+ $isValid = ((strlen($serialNumber) == PackageFragmenter::MAX_SERIAL_LENGTH) && ($this->hexval($serialNumber, FALSE) === $serialNumber));
+
+ // Return result
+ return $isValid;
+ }
+
+ /**
+ * Adds the chunk to the final array which will be used for the final step
+ * which will be to assemble all chunks back to the original package content
+ * and for the final hash check.
+ *
+ * This method may throw an exception if a chunk with the same serial number
+ * has already been added to avoid mixing chunks from different packages.
+ *
+ * @param $chunkSplits An array from a splitted chunk
+ * @return void
+ */
+ private function addChunkToFinalArray (array $chunkSplits) {
+ // Is the serial number (index 1) already been added?
+ if (isset($this->finalPackageChunks[$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]])) {
+ // Then throw an exception
+ throw new ChunkAlreadyAssembledException(array($this, $chunkSplits), self::EXCEPTION_CHUNK_ALREADY_ASSEMBLED);
+ } // END - if
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHUNK-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: serialNumber=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL] . ',hash=' . $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH]);
+
+ // Add the chunk data (index 2) to the final array and use the serial number as index
+ $this->finalPackageChunks['content'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_RAW_DATA];
+
+ // ... and the hash as well
+ $this->finalPackageChunks['hashes'][$chunkSplits[self::CHUNK_SPLITS_INDEX_SERIAL]] = $chunkSplits[self::CHUNK_SPLITS_INDEX_HASH];
+ }
+
+ /**
+ * Marks the final array as completed, do only this if you really have all
+ * chunks together including EOP and "hash chunk".
+ *
+ * @return void
+ */
+ private function markFinalArrayAsCompleted () {
+ /*
+ * As for now, just set the array element. If any further steps are
+ * being added, this should always be the last step.
+ */
+ $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]);
+ }
+