// Default is all fine
$isValid = true;
+ // Split the (possible) EOP chunk
+ $chunkSplits = explode(PackageFragmenter::CHUNK_DATA_HASH_SEPARATOR, $chunks[count($chunks) - 1]);
+
+ // Make sure chunks with only 3 elements are parsed (for details see ChunkHandler)
+ assert(count($chunkSplits) == 3);
+
// Validate final chunk
- if (substr($chunks[count($chunks) - 1], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) {
+ if (substr($chunkSplits[2], 0, strlen(PackageFragmenter::END_OF_PACKAGE_IDENTIFIER)) != PackageFragmenter::END_OF_PACKAGE_IDENTIFIER) {
// Not fine
$isValid = false;
- } elseif (substr_count($chunks[count($chunks) - 1], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) {
+ } elseif (substr_count($chunkSplits[2], PackageFragmenter::CHUNK_HASH_SEPARATOR) != 1) {
// CHUNK_HASH_SEPARATOR shall only be found once
$isValid = false;
}
/**
* Appends an end-of-package chunk to the chunk list for given chunk and
- * final hash.
+ * final hash. As of 23-March-2012 the format of this chunk will be as any
+ * regular one to keep things easy (KISS) in ChunkHandler class.
*
* @param $chunkHash Last chunk's hash
* @param $finalHash Final hash for raw (unencoded) data
*/
private function appendEndOfPackageChunk ($chunkHash, $finalHash) {
// Generate end-of-package marker
- $rawData =
+ $chunkData =
self::END_OF_PACKAGE_IDENTIFIER .
$finalHash . self::CHUNK_HASH_SEPARATOR .
$chunkHash . self::CHUNK_SEPARATOR;
- // Also get a hash from it
- $eopHash = $this->generateHashFromRawData($rawData);
-
- // Append it to the eop's data and hash array
- $this->chunkHashes[$finalHash][] = $eopHash;
- $this->chunks[$finalHash][] = $rawData;
+ // Add it as regular chunk
+ $this->addChunkData($chunkData);
}
/**
// Now split it up
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);
- $this->chunkHashes[$finalHash][] = $chunkHash;
+ $chunkData = substr($rawData, $idx, $dataChunkSize);
- // 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.');
- $this->chunks[$finalHash][] = $chunk;
+ // Add the chunk to the propper array and do all the stuff there
+ $this->addChunkData($chunkData);
} // END - for
// Debug output
$this->appendEndOfPackageChunk($chunkHash, $finalHash);
}
+ /**
+ * Adds the given chunk (raw data) to the proper array and hashes it for
+ * later verfication.
+ *
+ * @param $chunkData Raw chunk data
+ * @return void
+ */
+ private function addChunkData ($chunkData) {
+ // 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 .
+ $rawData . 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
+ //* NOISY-DEBUG: */ $this->debugOutput('FRAGMENTER: Adding ' . strlen($rawData) . ' bytes of a chunk.');
+ $this->chunks[$finalHash][] = $rawData;
+ $this->chunkHashes[$finalHash][] = $rawDataHash;
+ }
+
/**
* Prepends a chunk (or more) with all hashes from all chunks + final chunk.
*