assert(strlen($finalHash) > 0);
// Is the pointer already initialized?
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash=' . $finalHash);
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
assert(isset($this->chunkPointers[$finalHash]));
// Return it
assert(isset($this->chunkPointers[$finalHash]));
// Count one up
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
$this->chunkPointers[$finalHash]++;
}
$finalHash . self::CHUNK_HASH_SEPARATOR .
$this->generateHashFromRawData($lastChunk);
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': Adding EOP chunk with size of ' . strlen($chunkData) . ',finalHash=' . $finalHash . ' ...');
+
// Add it as regular chunk
$this->addChunkData($finalHash, $chunkData);
}
// Calculate real (data) chunk size
$dataChunkSize = $this->getDataChunkSizeFromHash($finalHash);
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: dataChunkSize=' . $dataChunkSize);
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': dataChunkSize=' . $dataChunkSize);
// Init variables
$chunkHash = '';
} // END - for
// Debug output
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Raw data of ' . strlen($rawData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).');
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': Raw data of ' . strlen($rawData) . ' bytes has been fragmented into ' . count($this->chunks[$finalHash]) . ' chunk(s).');
// Add end-of-package chunk
$this->appendEndOfPackageChunk($chunkData, $finalHash);
*
* @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) {
+ private function addChunkData ($finalHash, $chunkData, $prepend = false) {
// Hash it
$rawDataHash = $this->getCryptoInstance()->hashString($chunkData, '', false);
assert(strlen($rawData) <= NetworkPackage::TCP_PACKAGE_SIZE);
// Add it to the array
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($rawData) . ' bytes of a chunk ...');
- $this->chunks[$finalHash][] = $rawData;
- $this->chunkHashes[$finalHash][] = $rawDataHash;
+ 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;
+ }
}
/**
* Prepends a chunk (or more) with all hashes from all chunks + final chunk.
*
- * @param $rawData Raw data string
* @param $finalHash Final hash from the raw data
* @return void
*/
- private function prependHashChunk ($rawData, $finalHash) {
+ private function prependHashChunk ($finalHash) {
// "Implode" the whole array of hashes into one string
$rawData = self::HASH_CHUNK_IDENTIFIER . implode(self::CHUNK_HASH_SEPARATOR, $this->chunkHashes[$finalHash]);
- // Also get a hash from it
- $chunkHash = $this->generateHashFromRawData($rawData);
-
- // Calulcate chunk size
- $dataChunkSize = $this->getDataChunkSizeFromHash($chunkHash);
-
- // Now array_unshift() it to the two chunk arrays
- for ($idx = 0; $idx < strlen($rawData); $idx += $dataChunkSize) {
- // Get the next chunk
- $chunk = substr($rawData, $idx, $dataChunkSize);
-
- // Hash it and remember it in seperate array
- $chunkHash = $this->getCryptoInstance()->hashString($chunk, '', false);
- array_unshift($this->chunkHashes[$finalHash], $chunkHash);
-
- // Prepend the hash to the chunk
- $chunk =
- $chunkHash . self::CHUNK_DATA_HASH_SEPARATOR .
- $this->getNextHexSerialNumber() . self::CHUNK_DATA_HASH_SEPARATOR .
- $chunk . self::CHUNK_SEPARATOR
- ;
-
- // 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
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($chunk) . ' bytes of a chunk.');
- array_unshift($this->chunks[$finalHash], $chunk);
- } // END - for
+ // Prepend chunk
+ $this->addChunkData($finalHash, $rawData, true);
}
/**
$this->splitEncodedDataIntoChunks($rawData, $finalHash);
// Prepend a chunk with all hashes together
- $this->prependHashChunk($rawData, $finalHash);
+ $this->prependHashChunk($finalHash);
// Mark the package as fragmented
$this->markPackageDataProcessed($packageData);
}
// Return final hash
- //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: finalHash[' . gettype($finalHash) . ']=' . $finalHash);
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
return $finalHash;
}
* @throws AssertionException If $finalHash was not 'true'
*/
public function getNextRawDataChunk ($finalHash) {
+ // Debug message
+ //* NOISY-DEBUG: */ $this->debugOutput(__METHOD__ . ': finalHash[' . gettype($finalHash) . ']=' . $finalHash);
+
try {
// Get current chunk index
$current = $this->getCurrentChunkPointer($finalHash);
// If there is no entry left, return an empty array
if ((!isset($this->chunkHashes[$finalHash][$current])) || (!isset($this->chunks[$finalHash][$current]))) {
// No more entries found
+ $this->debugOutput(__METHOD__. ': finalHash=' . $finalHash . ',current=' . $current . ' - No more entries found!');
return array();
} // END - if
+ // Debug message
+ $this->debugOutput(__METHOD__. ': finalHash=' . $finalHash . ',current=' . $current . ',chunkHashes()=' . count($this->chunkHashes[$finalHash]) .' - Entry choosen ...');
+
// Generate the array
$rawDataChunk = array(
$this->chunkHashes[$finalHash][$current] => $this->chunks[$finalHash][$current]