+ $this->appendEndOfPackageChunk($chunkData, $finalHash);
+ }
+
+ /**
+ * Adds the given chunk (raw data) to the proper array and hashes it for
+ * later verfication.
+ *
+ * @param $finalHash Final hash for faster processing
+ * @param $chunkData Raw chunk data
+ * @param $prepend Whether append (default) or prepend the chunk
+ * @return void
+ */
+ private function addChunkData ($finalHash, $chunkData, $prepend = false) {
+ // Hash it
+ $rawDataHash = $this->getCryptoInstance()->hashString($chunkData, '', false);
+
+ // Prepend the hash to the chunk
+ $rawData = (
+ $rawDataHash . self::CHUNK_DATA_HASH_SEPARATOR .
+ $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR .
+ $chunkData . self::CHUNK_SEPARATOR
+ );
+
+ // Make sure the chunk is not larger than a TCP package can hold
+ assert(strlen($rawData) <= NetworkPackage::TCP_PACKAGE_SIZE);
+
+ // Add it to the array
+ if ($prepend === true) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': Prepending ' . strlen($rawData) . ' bytes of a chunk, finalHash=' . $finalHash . ' ...');
+ array_unshift($this->chunkHashes[$finalHash], $rawDataHash);
+ array_unshift($this->chunks[$finalHash] , $rawData);
+ } else {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': Appending ' . strlen($rawData) . ' bytes of a chunk, finalHash=' . $finalHash . ' ...');
+ $this->chunks[$finalHash][] = $rawData;
+ $this->chunkHashes[$finalHash][] = $rawDataHash;
+ }