From: Roland Häder Date: Fri, 23 Mar 2012 21:08:20 +0000 (+0000) Subject: Refactured EOP chunk, now it is formatted as any regular chunk. This makes the code... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=92f400c279b7b1f44387b4faf3ec7ebd89161b2a;p=hub.git Refactured EOP chunk, now it is formatted as any regular chunk. This makes the code in ChunkHandler class not so complicated just for the EOP chunk --- diff --git a/application/hub/main/class_BaseHubSystem.php b/application/hub/main/class_BaseHubSystem.php index 4de321119..845daef6b 100644 --- a/application/hub/main/class_BaseHubSystem.php +++ b/application/hub/main/class_BaseHubSystem.php @@ -286,11 +286,17 @@ class BaseHubSystem extends BaseFrameworkSystem { // 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; } diff --git a/application/hub/main/package/fragmenter/class_PackageFragmenter.php b/application/hub/main/package/fragmenter/class_PackageFragmenter.php index 8799f131c..09da5b728 100644 --- a/application/hub/main/package/fragmenter/class_PackageFragmenter.php +++ b/application/hub/main/package/fragmenter/class_PackageFragmenter.php @@ -305,7 +305,8 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg /** * 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 @@ -313,17 +314,13 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg */ 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); } /** @@ -349,25 +346,10 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg // 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 @@ -377,6 +359,33 @@ class PackageFragmenter extends BaseFrameworkSystem implements Fragmentable, Reg $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. *