3 * A general binary file class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class BaseBinaryFile extends BaseAbstractFile {
26 * Separator for header data
28 const SEPARATOR_HEADER_DATA = 0x01;
31 * Separator header->entries
33 const SEPARATOR_HEADER_ENTRIES = 0x02;
36 * Separator group->hash
38 const SEPARATOR_GROUP_HASH = 0x03;
41 * Separator hash->value
43 const SEPARATOR_HASH_VALUE = 0x04;
46 * Separator entry->entry
48 const SEPARATOR_ENTRIES = 0x05;
51 * Separator type->position
53 const SEPARATOR_TYPE_POSITION = 0x06;
58 const LENGTH_COUNT = 20;
63 const LENGTH_POSITION = 20;
68 const LENGTH_GROUP = 10;
71 * Maximum length of entry type
73 const LENGTH_TYPE = 20;
75 //***** Array elements for 'gaps' array *****
80 const GAPS_INDEX_START = 'start';
85 const GAPS_INDEX_END = 'end';
88 * Current seek position
90 private $seekPosition = 0;
95 private $headerSize = 0;
100 private $header = array();
103 * Seek positions for gaps ("fragmentation")
105 private $gaps = array();
108 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
110 private $damagedEntries = array();
115 private $backBuffer = '';
118 * Currently loaded block (will be returned by current())
120 private $currentBlock = '';
123 * Protected constructor
125 * @param $className Name of the class
128 protected function __construct ($className) {
129 // Call parent constructor
130 parent::__construct($className);
132 // Init counters and gaps array
133 $this->initCountersGapsArray();
137 * Checks whether the abstracted file only contains gaps by counting all
138 * gaps' bytes together and compare it to total length.
140 * @return $isGapsOnly Whether the abstracted file only contains gaps
142 private function isFileOnlyGaps () {
143 // First/last gap found?
144 /* Only for debugging
145 if (isset($this->gaps[0])) {
146 // Output first and last gap
147 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', __METHOD__, __LINE__, print_r($this->gaps[0], TRUE), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], TRUE)));
151 // Now count every gap
153 foreach ($this->gaps as $gap) {
154 // Calculate size of found gap: end-start including both
155 $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
159 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
161 // Total gap size + header size must be same as file size
162 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
169 * Initializes counter for valid entries, arrays for damaged entries and
170 * an array for gap seek positions. If you call this method on your own,
171 * please re-analyze the file structure. So you are better to call
172 * analyzeFile() instead of this method.
176 public function initCountersGapsArray () {
177 // Init counter and seek position
178 $this->setCounter(0);
179 $this->setSeekPosition(0);
182 $this->gaps = array();
183 $this->damagedEntries = array();
187 * Getter for header size
189 * @return $totalEntries Size of file header
191 public final function getHeaderSize () {
193 return $this->headerSize;
197 * Setter for header size
199 * @param $headerSize Size of file header
202 public final function setHeaderSize ($headerSize) {
204 $this->headerSize = $headerSize;
208 * Getter for header array
210 * @return $totalEntries Size of file header
212 public final function getHeader () {
214 return $this->header;
220 * @param $header Array for a file header
223 public final function setHeader (array $header) {
225 $this->header = $header;
229 * Getter for seek position
231 * @return $seekPosition Current seek position (stored here in object)
233 public final function getSeekPosition () {
235 return $this->seekPosition;
239 * Setter for seek position
241 * @param $seekPosition Current seek position (stored here in object)
244 protected final function setSeekPosition ($seekPosition) {
246 $this->seekPosition = $seekPosition;
250 * Updates seekPosition attribute from file to avoid to much access on file.
254 public function updateSeekPosition () {
255 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
257 // Get key (= seek position)
258 $seekPosition = $this->key();
259 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
262 $this->setSeekPosition($seekPosition);
264 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
268 * Seeks to beginning of file, updates seek position in this object and
269 * flushes the header.
273 protected function rewindUpdateSeekPosition () {
274 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
276 // flushFileHeader must be callable
277 assert(is_callable(array($this, 'flushFileHeader')));
279 // Seek to beginning of file
282 // And update seek position ...
283 $this->updateSeekPosition();
285 // ... to write it back into the file
286 $this->flushFileHeader();
288 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
292 * Seeks to old position
296 protected function seekToOldPosition () {
297 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
299 // Seek to currently ("old") saved position
300 $this->seek($this->getSeekPosition());
302 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
306 * Checks whether the block separator has been found
308 * @param $str String to look in
309 * @return $isFound Whether the block separator has been found
311 public static function isBlockSeparatorFound ($str) {
313 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
320 * Initializes the back-buffer by setting it to an empty string.
324 private function initBackBuffer () {
325 // Simply call the setter
326 $this->setBackBuffer('');
330 * Setter for backBuffer field
332 * @param $backBuffer Characters to "store" in back-buffer
335 private function setBackBuffer ($backBuffer) {
336 // Cast to string (so no arrays or objects)
337 $backBuffer = (string) $backBuffer;
340 $this->backBuffer = $backBuffer;
344 * Getter for backBuffer field
346 * @return $backBuffer Characters "stored" in back-buffer
348 private function getBackBuffer () {
349 return $this->backBuffer;
353 * Setter for currentBlock field
355 * @param $currentBlock Characters to set a currently loaded block
358 private function setCurrentBlock ($currentBlock) {
359 // Cast to string (so no arrays or objects)
360 $currentBlock = (string) $currentBlock;
363 $this->currentBlock = $currentBlock;
367 * Gets currently read data
369 * @return $current Currently read data
371 public function getCurrentBlock () {
373 return $this->currentBlock;
377 * Initializes this file class
379 * @param $fileName Name of this abstract file
382 protected function initFile ($fileName) {
383 // Get a file i/o pointer instance
384 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
386 // ... and set it here
387 $this->setPointerInstance($pointerInstance);
391 * Writes data at given position
393 * @param $seekPosition Seek position
394 * @param $data Data to be written
395 * @param $flushHeader Whether to flush the header (default: flush)
398 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
399 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
401 // Write data at given position
402 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
405 $this->incrementCounter();
407 // Update seek position
408 $this->updateSeekPosition();
411 if ($flushHeader === TRUE) {
413 $this->flushFileHeader();
415 // Seek to old position
416 $this->seekToOldPosition();
419 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
423 * Marks the currently loaded block as empty (with length of the block)
425 * @param $length Length of the block
428 protected function markCurrentBlockAsEmpty ($length) {
429 // Get current seek position
430 $currentPosition = $this->key();
432 // Now add it as gap entry
433 array_push($this->gaps, array(
434 self::GAPS_INDEX_START => ($currentPosition - $length),
435 self::GAPS_INDEX_END => $currentPosition,
440 * Checks whether the file header is initialized
442 * @return $isInitialized Whether the file header is initialized
444 public function isFileHeaderInitialized () {
445 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
447 // Default is not initialized
448 $isInitialized = FALSE;
450 // Is the file initialized?
451 if ($this->isFileInitialized()) {
452 // Some bytes has been written, so rewind to start of it.
453 $rewindStatus = $this->rewind();
454 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
456 // Is the rewind() call successfull?
457 if ($rewindStatus != 1) {
458 // Something bad happened
459 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
463 $this->readFileHeader();
465 // The above method does already check the header
466 $isInitialized = TRUE;
470 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
471 return $isInitialized;
475 * Checks whether the assigned file has been initialized
477 * @return $isInitialized Whether the file's size is zero
479 public function isFileInitialized () {
480 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
482 // Get it from iterator which holds the pointer instance. If FALSE is returned
483 $fileSize = $this->size();
484 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
487 * The returned file size should not be FALSE or NULL as this means
488 * that the pointer class does not work correctly.
490 assert(is_int($fileSize));
492 // Is more than 0 returned?
493 $isInitialized = ($fileSize > 0);
496 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
497 return $isInitialized;
501 * Creates the assigned file
505 public function createFileHeader () {
506 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
508 // The file's header should not be initialized here
509 assert(!$this->isFileHeaderInitialized());
511 // Simple flush file header which will create it.
512 $this->flushFileHeader();
514 // Rewind seek position (to beginning of file) and update/flush file header
515 $this->rewindUpdateSeekPosition();
517 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
521 * Pre-allocates file (if enabled) with some space for later faster write access.
523 * @param $type Type of the file
526 public function preAllocateFile ($type) {
527 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
530 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
532 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
534 // Don't continue here.
539 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
541 // Calculate minimum length for one entry
542 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
543 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
545 // Calulcate seek position
546 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
547 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
549 // Now simply write a NUL there. This will pre-allocate the file.
550 $this->writeData($seekPosition, chr(0));
552 // Rewind seek position (to beginning of file) and update/flush file header
553 $this->rewindUpdateSeekPosition();
555 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
559 * Determines seek position
561 * @return $seekPosition Current seek position
563 public function determineSeekPosition () {
564 // Call pointer instance
565 return $this->getPointerInstance()->determineSeekPosition();
569 * Seek to given offset (default) or other possibilities as fseek() gives.
571 * @param $offset Offset to seek to (or used as "base" for other seeks)
572 * @param $whence Added to offset (default: only use offset to seek to)
573 * @return $status Status of file seek: 0 = success, -1 = failed
575 public function seek ($offset, $whence = SEEK_SET) {
576 // Call pointer instance
577 return $this->getPointerInstance()->seek($offset, $whence);
581 * Reads given amount of bytes from file.
583 * @param $bytes Amount of bytes to read
584 * @return $data Data read from file
586 public function read ($bytes = NULL) {
587 // $bytes shall be integer
588 assert(is_int($bytes));
590 // Call pointer instance
591 return $this->getPointerInstance()->read($bytes);
595 * Rewinds to the beginning of the file
597 * @return $status Status of this operation
599 public function rewind () {
600 // Call pointer instance
601 return $this->getPointerInstance()->rewind();
605 * Analyzes entries in index file. This will count all found (and valid)
606 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
607 * only gaps are found, the file is considered as "virgin" (no entries).
611 public function analyzeFile () {
612 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
614 // Make sure the file is initialized
615 assert($this->isFileInitialized());
617 // Init counters and gaps array
618 $this->initCountersGapsArray();
620 // Output message (as this may take some time)
621 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
623 // First rewind to the begining
626 // Then try to load all entries
627 while ($this->valid()) {
632 $current = $this->getCurrentBlock();
635 * If the block is empty, maybe the whole file is? This could mean
636 * that the file has been pre-allocated.
638 if (empty($current)) {
639 // Then skip this part
644 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
647 // If the last read block is empty, check gaps
648 if (empty($current)) {
650 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
652 // Check gaps, if the whole file is empty.
653 if ($this->isFileOnlyGaps()) {
654 // Only gaps, so don't continue here.
659 * The above call has calculated a total size of all gaps. If the
660 * percentage of gaps passes a "soft" limit and last
661 * defragmentation is to far in the past, or if a "hard" limit has
662 * reached, run defragmentation.
664 if ($this->isDefragmentationNeeded()) {
665 // Run "defragmentation"
666 $this->doRunDefragmentation();
669 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
673 * Advances to next "block" of bytes
677 public function next () {
678 // Is there nothing to read?
679 if (!$this->valid()) {
685 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
687 // Make sure the block instance is set
688 assert($this->getBlockInstance() instanceof CalculatableBlock);
690 // First calculate minimum block length
691 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
692 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
694 // Short be more than zero!
697 // Read possibly back-buffered bytes from previous call of next().
698 $data = $this->getBackBuffer();
701 * Read until a entry/block separator has been found. The next read
702 * "block" may not fit, so this loop will continue until the EOB or EOF
703 * has been reached whatever comes first.
705 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
706 // Then read the next possible block
707 $block = $this->read($length);
710 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
713 if (strlen(trim($block)) == 0) {
714 // Mark this block as empty
715 $this->markCurrentBlockAsEmpty(strlen($block));
717 // Skip to next block
721 // At this block then
725 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
729 if ($this->isEndOfFileReached()) {
730 // Set whole data as current read block
731 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
732 $this->setCurrentBlock($data);
734 // Then abort here silently
735 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
740 * Init back-buffer which is the data that has been found beyond the
743 $this->initBackBuffer();
746 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
748 // This array must contain two elements
749 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
750 assert(count($dataArray) == 2);
752 // Left part is the actual block, right one the back-buffer data
753 $this->setCurrentBlock($dataArray[0]);
754 $this->setBackBuffer($dataArray[1]);
758 * Checks wether the current entry is valid (not at the end of the file).
759 * This method will return TRUE if an emptied (nulled) entry has been found.
761 * @return $isValid Whether the next entry is valid
763 public function valid () {
764 // Make sure the block instance is set
765 assert($this->getBlockInstance() instanceof Block);
767 // First calculate minimum block length
768 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
770 // Short be more than zero!
773 // Get current seek position
774 $seekPosition = $this->key();
776 // Then try to read it
777 $data = $this->read($length);
779 // If some bytes could be read, all is fine
780 $isValid = ((is_string($data)) && (strlen($data) > 0));
783 $headerSize = $this->getHeaderSize();
785 // Is the seek position at or beyond the header?
786 if ($seekPosition >= $headerSize) {
787 // Seek back to old position
788 $this->seek($seekPosition);
790 // Seek directly behind the header
791 $this->seek($headerSize);
799 * Gets current seek position ("key").
801 * @return $key Current key in iteration
803 public function key () {
804 // Call pointer instance
805 return $this->getPointerInstance()->determineSeekPosition();
809 * Reads the file header
813 public function readFileHeader () {
814 // Make sure the block instance is set
815 assert($this->getBlockInstance() instanceof Block);
817 // Call block instance
818 $this->getBlockInstance()->readFileHeader();
822 * Flushes the file header
826 public function flushFileHeader () {
827 // Make sure the block instance is set
828 assert($this->getBlockInstance() instanceof Block);
830 // Call block instance
831 $this->getBlockInstance()->flushFileHeader();
835 * Searches for next suitable gap the given length of data can fit in
836 * including padding bytes.
838 * @param $length Length of raw data
839 * @return $seekPosition Found next gap's seek position
841 public function searchNextGap ($length) {
842 // If the file is only gaps, no need to seek
843 if ($this->isFileOnlyGaps()) {
844 // The first empty block is the first one right after the header
845 return ($this->getHeaderSize() + 1);
849 $this->partialStub('length=' . $length);