+ /**
+ * Initializes this fragmenter
+ *
+ * @return void
+ */
+ private function initFragmenter () {
+ // Load some configuration entries and "cache" them:
+ // - Chunk size in bits
+ $this->chunkSize = $this->getConfigInstance()->getConfigEntry('package_chunk_size');
+
+ // - Maximum serial number
+ $this->maxSerialNumber = $this->hex2dec(str_repeat('f', $this->maxSerialLength));
+ }
+
+ /**
+ * Initializes the pointer for given final hash
+ *
+ * @param $finalHash Final hash to initialize pointer for
+ * @return void
+ */
+ private function initPointer ($finalHash) {
+ $this->chunkPointers[$finalHash] = 0;
+ }
+
+ /**
+ * "Getter" for processedPackages array index
+ *
+ * @param $packageData Raw package data array
+ * @return $index Array index for processedPackages
+ */
+ private function getProcessedPackagesIndex (array $packageData) {
+ return (
+ $packageData['sender'] . NetworkPackage::PACKAGE_DATA_SEPERATOR .
+ $packageData['recipient'] . NetworkPackage::PACKAGE_DATA_SEPERATOR .
+ $packageData['content'] . NetworkPackage::PACKAGE_DATA_SEPERATOR
+ );
+ }
+
+ /**
+ * Checks wether the given package data is already processed by this fragmenter
+ *
+ * @param $packageData Raw package data array
+ * @return $isProcessed Wether the package has been fragmented
+ */
+ private function isPackageProcessed (array $packageData) {
+ // Get array index
+ $index = $this->getProcessedPackagesIndex($packageData);
+
+ // Is the array index there?
+ $isProcessed = (
+ (isset($this->processedPackages[$index])) &&
+ ($this->processedPackages[$index] === true)
+ );
+
+ // Return it
+ return $isProcessed;
+ }
+
+ /**
+ * Marks the given package data as processed by this fragmenter
+ *
+ * @param $packageData Raw package data array
+ * @return void
+ */
+ private function markPackageDataProcessed (array $packageData) {
+ // Remember it (until we may remove it)
+ $this->processedPackages[$this->getProcessedPackagesIndex($packageData)] = true;
+ }
+
+ /**
+ * Getter for final hash from given package data
+ *
+ * @param $packageData Raw package data array
+ * @return $finalHash Final hash for package data
+ */
+ private function getFinalHashFromPackageData (array $packageData) {
+ // Make sure it is there
+ assert(isset($this->processedPackages[$this->getProcessedPackagesIndex($packageData)]));
+
+ // Return it
+ return $this->processedPackages[$this->getProcessedPackagesIndex($packageData)];
+ }
+
+ /**
+ * Get next chunk pointer for given final hash
+ *
+ * @param $finalHash Final hash to get current pointer for
+ */
+ private function getCurrentChunkPointer ($finalHash) {
+ // Is the final hash valid?
+ assert(strlen($finalHash) > 0);
+
+ // Is the pointer already initialized?
+ //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash=' . $finalHash);
+ assert(isset($this->chunkPointers[$finalHash]));
+
+ // Return it
+ return $this->chunkPointers[$finalHash];
+ }
+
+ /**
+ * Advance the chunk pointer for given final hash
+ *
+ * @param $finalHash Final hash to advance the pointer for
+ */
+ private function nextChunkPointer ($finalHash) {
+ // Is the pointer already initialized?
+ assert(isset($this->chunkPointers[$finalHash]));
+
+ // Count one up
+ $this->chunkPointers[$finalHash]++;
+ }
+
+ /**
+ * "Getter" for data chunk size of given hash.
+ *
+ * @param $hash Hash to substract it's length
+ * @return $dataChunkSize The chunk size
+ */
+ private function getDataChunkSizeFromHash ($hash) {
+ // Calculate real (data) chunk size
+ $dataChunkSize = (
+ // Real chunk size
+ ($this->chunkSize / 8) -
+ // Hash size
+ strlen($hash) -
+ // Length of sperators
+ (strlen(self::CHUNK_DATA_HASH_SEPERATOR) * 2) -
+ // Length of max serial number
+ $this->maxSerialLength
+ );
+
+ // This should be larger than zero bytes
+ assert($dataChunkSize > 0);
+
+ // Return it
+ return $dataChunkSize;
+ }
+
+ /**
+ * Generates a hash from raw data
+ *
+ * @param $rawData Raw data bytes to hash
+ * @return $hash Hash from the raw data
+ */
+ private function generateHashFromRawData ($rawData) {
+ // Get the crypto instance and hash the data
+ $hash = $this->getCryptoInstance()->hashString($rawData);
+
+ // Return it
+ return $hash;
+ }
+
+ /**
+ * "Getter" for the next hexadecimal-encoded serial number
+ *
+ * @return $encodedSerialNumber The next hexadecimal-encoded serial number
+ */
+ private function getNextHexSerialNumber () {
+ // Assert on maximum serial number length
+ assert($this->serialNumber <= $this->maxSerialNumber);
+
+ // Encode the current serial number
+ $encodedSerialNumber = $this->dec2Hex($this->serialNumber, $this->maxSerialLength);
+
+ // Count one up
+ $this->serialNumber++;
+
+ // Return the encoded serial number
+ return $encodedSerialNumber;
+ }
+
+ /**
+ * Splits the given encoded data into smaller chunks, the size of the final
+ * and the seperator is being subtracted from chunk size to fit it into a
+ * TCP package (512 bytes).
+ *
+ * @param $encodedData Encoded data string
+ * @param $finalHash Final hash from the raw data
+ * @return void
+ */
+ private function splitEncodedDataIntoChunks ($encodedData, $finalHash) {
+ // Make sure final hashes with at least 32 bytes can pass
+ assert(strlen($finalHash) >= 32);
+
+ // Calculate real (data) chunk size
+ $dataChunkSize = $this->getDataChunkSizeFromHash($finalHash);
+
+ // Now split it up
+ for ($idx = 0; $idx < strlen($encodedData); $idx += $dataChunkSize) {
+ // Get the next chunk
+ $chunk = substr($encodedData, $idx, $dataChunkSize);
+
+ // Hash it and remember it in seperate array
+ $chunkHash = $this->getCryptoInstance()->hashString($chunk);
+ $this->chunkHashes[$finalHash][] = $chunkHash;
+
+ // Prepend the hash to the chunk
+ $chunk = $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . $chunk;
+
+ // Make sure the chunk is not larger than a TCP package can hold
+ assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE);
+
+ // Add it to the array
+ $this->chunks[$finalHash][] = $chunk;
+ } // END - for
+
+ // Debug output
+ $this->debugOutput('FRAGMENTER: Encoded data of ' . strlen($encodedData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).');
+ }
+
+ /**
+ * Prepends a chunk (or more) with all hashes from all chunks + final chunk.
+ *
+ * @param $encodedData Encoded data string
+ * @param $finalHash Final hash from the raw data
+ * @return void
+ */
+ private function prependHashChunk ($encodedData, $finalHash) {
+ // "Implode" the whole array of hashes into one string
+ $rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPERATOR, $this->chunkHashes[$finalHash]);
+
+ // Also get a hash from it
+ $chunkHash = $this->generateHashFromRawData($rawData);
+
+ // Also encode this one
+ $encodedData = $this->getOutputStreamInstance()->streamData($rawData);
+
+ // Calulcate chunk size
+ $dataChunkSize = $this->getDataChunkSizeFromHash($chunkHash);
+
+ // Now array_unshift() it to the two chunk arrays
+ for ($idx = 0; $idx < strlen($encodedData); $idx += $dataChunkSize) {
+ // Get the next chunk
+ $chunk = substr($encodedData, $idx, $dataChunkSize);
+
+ // Hash it and remember it in seperate array
+ $chunkHash = $this->getCryptoInstance()->hashString($chunk);
+ array_unshift($this->chunkHashes[$finalHash], $chunkHash);
+
+ // Prepend the hash to the chunk
+ $chunk = $chunkHash . self::CHUNK_DATA_HASH_SEPERATOR . $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPERATOR . $chunk;
+
+ // Make sure the chunk is not larger than a TCP package can hold
+ assert(strlen($chunk) <= NetworkPackage::TCP_PACKAGE_SIZE);
+
+ // Add it to the array
+ array_unshift($this->chunks[$finalHash], $chunk);
+ } // END - for
+ }
+