OpenASIP 2.2
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
TPEF::BinaryStream Class Reference

#include <BinaryStream.hh>

Collaboration diagram for TPEF::BinaryStream:
Collaboration graph

Public Member Functions

 BinaryStream (std::ostream &stream, bool littleEndian=false)
 
 BinaryStream (std::string name, bool littleEndian=false)
 
virtual ~BinaryStream ()
 
Byte readByte ()
 
HalfWord readHalfWord ()
 
Word readWord ()
 
void readByteBlock (Byte *buffer, unsigned int howmany)
 
void readHalfWordBlock (HalfWord *buffer, unsigned int howmany)
 
void readWordBlock (Word *buffer, unsigned int howmany)
 
void writeByte (Byte byte)
 
void writeHalfWord (HalfWord halfword)
 
void writeWord (Word word)
 
void writeByteBlock (Byte *bytes, unsigned int howmany)
 
void writeHalfWordBlock (HalfWord *hwords, unsigned int howmany)
 
void writeWordBlock (Word *words, unsigned int howmany)
 
unsigned int readPosition ()
 
unsigned int writePosition ()
 
void setReadPosition (unsigned int position)
 
void setWritePosition (unsigned int position)
 
bool endOfFile ()
 
unsigned int sizeOfFile ()
 
void setTPEFVersion (TPEFHeaders::TPEFVersion version)
 
TPEFHeaders::TPEFVersion TPEFVersion () const
 

Private Member Functions

BinaryStreamoperator= (BinaryStream &old)
 Assignment not allowed.
 
 BinaryStream (BinaryStream &old)
 Copying not allowed.
 
void openInput (std::string name)
 
void openOutput (std::string name)
 
void close ()
 
Byte getByte ()
 
void putByte (Byte byte)
 
bool needsSwap () const
 

Private Attributes

std::ifstream iStream_
 The input stream.
 
std::ofstream oStream_
 The output stream.
 
std::string fileName_
 The name of the stream.
 
std::ostream * extOStream_
 Externally given output stream.
 
bool littleEndianStorage_
 In case we want to store the words in little endian order, big endian otherwise.
 
TPEFHeaders::TPEFVersion tpefVersion_
 Indicates TPEF format version used.
 

Detailed Description

Abstracts the input/output binary stream used to read and write TTA programs.

It takes care of opening and closing the streams automatically, and hides possible byte order mismatch.

The bits read from the stream are converted to the byte order of the host machine. Conversely, the bits written into the stream will be converted to the standard byte order of TTA Program Exchange Format files which adheres to the byte order of the ADF the TPEF is associated with (by default big endian).

Definition at line 59 of file BinaryStream.hh.

Constructor & Destructor Documentation

◆ BinaryStream() [1/3]

TPEF::BinaryStream::BinaryStream ( std::ostream &  stream,
bool  littleEndian = false 
)

Definition at line 50 of file BinaryStream.cc.

50 :
51 fileName_(""), extOStream_(&stream), littleEndianStorage_(littleEndian),
53}
std::string fileName_
The name of the stream.
std::ostream * extOStream_
Externally given output stream.
TPEFHeaders::TPEFVersion tpefVersion_
Indicates TPEF format version used.
bool littleEndianStorage_
In case we want to store the words in little endian order, big endian otherwise.
@ TPEF_V2
Support for over 255 Buses, FUs, RFs.

◆ BinaryStream() [2/3]

TPEF::BinaryStream::BinaryStream ( std::string  name,
bool  littleEndian = false 
)

Prepares the binary stream so that it can be read from / written to.

The input string is used as the name of the file to open (if existing) or to create (if not existing).

Parameters
nameis the name of the input file.
Note
The initial read and write positions of the stream are 0.

Definition at line 64 of file BinaryStream.cc.

64 :
65 fileName_(name), extOStream_(NULL), littleEndianStorage_(littleEndian),
67}
@ TPEF_V1
Initial TPEF version.

◆ ~BinaryStream()

TPEF::BinaryStream::~BinaryStream ( )
virtual

Closes the stream.

No other cleanup or deallocation activity is required.

Note
Only way of closing and flushing the actual output stream can be done be calling the destructor (through deletion of the object).

Definition at line 107 of file BinaryStream.cc.

107 {
108 close();
109}

References close().

Here is the call graph for this function:

◆ BinaryStream() [3/3]

TPEF::BinaryStream::BinaryStream ( BinaryStream old)
private

Copying not allowed.

Member Function Documentation

◆ close()

void TPEF::BinaryStream::close ( )
private

Closes the stream.

Definition at line 544 of file BinaryStream.cc.

544 {
545 if (iStream_.is_open()) {
546 iStream_.close();
547 }
548 if (oStream_.is_open()) {
549 oStream_.close();
550 }
551}
std::ifstream iStream_
The input stream.
std::ofstream oStream_
The output stream.

References iStream_, and oStream_.

Referenced by ~BinaryStream().

◆ endOfFile()

bool TPEF::BinaryStream::endOfFile ( )

Returns true if read position is at the end of file.

Exceptions
UnreachableStreamIf stream is bad or otherwise unreachable.

Definition at line 722 of file BinaryStream.cc.

722 {
723 if (extOStream_ != NULL) {
724 throw UnreachableStream(
725 __FILE__, __LINE__, __func__, "External stream is write-only.");
726 }
727
728 if (!iStream_.is_open()) {
729 try {
731
732 } catch (const UnreachableStream& error) {
733 UnreachableStream newException =
734 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
735 newException.setCause(error);
736 throw newException;
737 }
738 }
739
740 if (iStream_.bad()) {
741 throw UnreachableStream(
742 __FILE__, __LINE__, __func__, fileName_);
743 }
744
745 return iStream_.eof();
746}
#define __func__
void setCause(const Exception &cause)
Definition Exception.cc:75
void openInput(std::string name)

References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().

Here is the call graph for this function:

◆ getByte()

Byte TPEF::BinaryStream::getByte ( )
private

Referenced by readByte(), and readByteBlock().

◆ needsSwap()

bool TPEF::BinaryStream::needsSwap ( ) const
private

Returns true in case words should be swapped due to mismatch between the host and current target (TTA) endianness.

Definition at line 74 of file BinaryStream.cc.

74 {
75 return (littleEndianStorage_ && (HOST_BIGENDIAN == 1)) ||
77}
#define HOST_BIGENDIAN
Definition BaseType.hh:123

References HOST_BIGENDIAN, and littleEndianStorage_.

Referenced by readHalfWord(), readWord(), writeHalfWord(), and writeWord().

◆ openInput()

void TPEF::BinaryStream::openInput ( std::string  name)
private

Opens the binary file for input.

Parameters
nameName of the input file.
Exceptions
UnreachableStreamIf file is not found or is unreadable.
Note
The initial read position is 0.

Definition at line 484 of file BinaryStream.cc.

484 {
485 if (extOStream_ != NULL) {
486 throw UnreachableStream(
487 __FILE__, __LINE__, __func__, "External stream is write-only.");
488 }
489
490 iStream_.open(name.c_str());
491
492 if (!iStream_.is_open()) {
493 const std::string error = (boost::format(
494 "File '%s' could not be opened for input.") % name).str();
495 throw UnreachableStream(
496 __FILE__, __LINE__, __func__, error);
497 }
498 if (oStream_.is_open()) {
499 oStream_.flush();
500 }
501 iStream_.tie(&oStream_);
502}

References __func__, extOStream_, iStream_, and oStream_.

Referenced by endOfFile(), readPosition(), setReadPosition(), and sizeOfFile().

◆ openOutput()

void TPEF::BinaryStream::openOutput ( std::string  name)
private

Opens the binary file for output.

If a file doesn't exist, a new empty file will be created.

Parameters
nameName of the output file.
Exceptions
UnreachableStreamIf file cannot be opened.
Note
The initial write position is 0.

Definition at line 514 of file BinaryStream.cc.

514 {
515 if (extOStream_ != NULL) {
516 throw UnreachableStream(
517 __FILE__, __LINE__, __func__,
518 "External stream should be always open.");
519 }
520
521 oStream_.open(name.c_str(), fstream::out);
522
523 // With some versions of STL a non-existing file is not
524 // automatically created when using only 'out'-flag. In that case,
525 // we have to open the stream in truncate mode to force the creating
526 // of the empty file.
527 if (!oStream_.is_open()) {
528 oStream_.clear();
529 oStream_.open(name.c_str(), fstream::out | fstream::trunc);
530 }
531
532 if (!oStream_.is_open()) {
533 const std::string error = (boost::format(
534 "File '%s' could not be opened for output.") % fileName_).str();
535 throw UnreachableStream(
536 __FILE__, __LINE__, __func__, error);
537 }
538}

References __func__, extOStream_, fileName_, and oStream_.

Referenced by setWritePosition(), and writePosition().

◆ operator=()

BinaryStream & TPEF::BinaryStream::operator= ( BinaryStream old)
private

Assignment not allowed.

◆ putByte()

void TPEF::BinaryStream::putByte ( Byte  byte)
private

Referenced by writeByte(), and writeByteBlock().

◆ readByte()

Byte TPEF::BinaryStream::readByte ( )

Reads one Byte from the binary stream.

Returns
The next 8-bit byte from the stream.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 120 of file BinaryStream.cc.

120 {
121 Byte value;
122
123 try {
124 value = getByte();
125
126 } catch (const EndOfFile& error) {
127 EndOfFile newException =
128 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
129 newException.setCause(error);
130 throw newException;
131
132 } catch (const UnreachableStream& error) {
133 UnreachableStream newException =
134 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
135 newException.setCause(error);
136 throw newException;
137 }
138
139 return value;
140}
unsigned char Byte
Definition BaseType.hh:116

References __func__, fileName_, getByte(), and Exception::setCause().

Referenced by TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::TPEFReader::isMyStreamType(), TPEF::TPEFCodeSectionReader::readAnnotations(), Assembler::readAssemblerCode(), TPEF::TPEFReader::readData(), TPEF::AOutDataSectionReader::readData(), TPEF::AOutStringSectionReader::readData(), TPEF::AOutTextSectionReader::readData(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFDataSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFStringSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::TPEFCodeSectionReader::readId(), and TPEF::TPEFCodeSectionReader::readInfo().

Here is the call graph for this function:

◆ readByteBlock()

void TPEF::BinaryStream::readByteBlock ( Byte buffer,
unsigned int  howmany 
)

Reads a block of Bytes from the binary stream.

Parameters
bufferis where the bytes are returned.
howmanyis how many bytes are supposed to be read.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 225 of file BinaryStream.cc.

225 {
226 try {
227 for (unsigned int i = 0; i < howmany; i++) {
228 buffer[i] = getByte();
229 }
230
231 } catch (const EndOfFile& error) {
232 EndOfFile newException =
233 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
234 newException.setCause(error);
235 throw newException;
236
237 } catch (const UnreachableStream& error) {
238 UnreachableStream newException =
239 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
240 newException.setCause(error);
241 throw newException;
242 }
243}

References __func__, fileName_, getByte(), and Exception::setCause().

Referenced by readHalfWord(), and readWord().

Here is the call graph for this function:

◆ readHalfWord()

HalfWord TPEF::BinaryStream::readHalfWord ( )

Reads one HalfWord from the binary stream.

Returns
The next 16-bit word (half-word) from the stream.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 150 of file BinaryStream.cc.

150 {
151 union {
152 Byte buffer[sizeof(HalfWord)];
153 HalfWord result;
154 } U;
155
156 try {
157 readByteBlock(U.buffer, sizeof(HalfWord));
158
159 } catch (const EndOfFile& error) {
160 EndOfFile newException =
161 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
162 newException.setCause(error);
163 throw newException;
164
165 } catch (const UnreachableStream& error) {
166 UnreachableStream newException =
167 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
168 newException.setCause(error);
169 throw newException;
170 }
171
172 // convert half-word to host endianess
173 if (needsSwap())
174 Swapper::swap(U.buffer, (Byte*)&U.result, sizeof(U.result));
175
176 return U.result;
177}
bool needsSwap() const
void readByteBlock(Byte *buffer, unsigned int howmany)
static void swap(const Byte *src, Byte *dst, int size)

References __func__, fileName_, needsSwap(), readByteBlock(), Exception::setCause(), and TPEF::Swapper::swap().

Referenced by TPEF::AOutTextSectionReader::initializeImmediateMove(), TPEF::AOutTextSectionReader::initializeMove(), TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::AOutReader::isMyStreamType(), TPEF::TPEFReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), readHalfWordBlock(), TPEF::AOutReader::readHeader(), TPEF::TPEFCodeSectionReader::readId(), TPEF::TPEFCodeSectionReader::readInfo(), TPEF::TPEFLineNumSectionReader::readInfo(), and TPEF::TPEFRelocSectionReader::readInfo().

Here is the call graph for this function:

◆ readHalfWordBlock()

void TPEF::BinaryStream::readHalfWordBlock ( HalfWord *  buffer,
unsigned int  howmany 
)

Reads a block of HalfWords from the binary stream.

Parameters
bufferis where the halfwords are returned.
howmanyis how many halfwords are supposed to be read.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 254 of file BinaryStream.cc.

254 {
255 try {
256 for (unsigned int i = 0; i < howmany; i++) {
257 buffer[i] = readHalfWord();
258 }
259
260 } catch (const EndOfFile& error) {
261 EndOfFile newException =
262 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
263 newException.setCause(error);
264 throw newException;
265
266 } catch (const UnreachableStream& error) {
267 UnreachableStream newException =
268 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
269 newException.setCause(error);
270 throw newException;
271 }
272}
HalfWord readHalfWord()

References __func__, fileName_, readHalfWord(), and Exception::setCause().

Here is the call graph for this function:

◆ readPosition()

unsigned int TPEF::BinaryStream::readPosition ( )

Returns the current position of the read cursor.

Exceptions
UnreachableStreamIf stream is bad or otherwise unreachable.

Definition at line 561 of file BinaryStream.cc.

561 {
562 if (extOStream_ != NULL) {
563 throw UnreachableStream(
564 __FILE__, __LINE__, __func__, "External stream is write-only.");
565 }
566
567 if (!iStream_.is_open()) {
568 try {
570 } catch (const UnreachableStream& error) {
571 UnreachableStream newException =
572 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
573 newException.setCause(error);
574 throw newException;
575 }
576 }
577
578 if (iStream_.bad()) {
579 throw UnreachableStream(__FILE__, __LINE__,
580 "BinaryStream::readPosition", fileName_);
581 }
582 return iStream_.tellg();
583}

References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().

Referenced by TPEF::AOutReader::isMyStreamType(), TPEF::TPEFReader::isMyStreamType(), Assembler::readAssemblerCode(), TPEF::BinaryReader::readBinary(), TPEF::AOutReader::readData(), TPEF::TPEFReader::readData(), TPEF::AOutRelocationSectionReader::readData(), TPEF::AOutSymbolSectionReader::readData(), TPEF::AOutTextSectionReader::readData(), TPEF::TPEFCodeSectionReader::readData(), TPEF::TPEFDataSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFStringSectionReader::readData(), TPEF::AOutReader::readHeader(), and sizeOfFile().

Here is the call graph for this function:

◆ readWord()

Word TPEF::BinaryStream::readWord ( )

Reads one Word from the binary stream.

Returns
The next 32-bit word from the stream.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 187 of file BinaryStream.cc.

187 {
188 union {
189 Byte buffer[sizeof(Word)];
190 Word result;
191 } U;
192
193 try {
194 readByteBlock(U.buffer, sizeof(Word));
195
196 } catch (const EndOfFile& error) {
197 EndOfFile newException =
198 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
199 newException.setCause(error);
200 throw newException;
201
202 } catch (const UnreachableStream& error) {
203 UnreachableStream newException =
204 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
205 newException.setCause(error);
206 throw newException;
207 }
208
209 // convert word to host endianess
210 if (needsSwap())
211 Swapper::swap(U.buffer, (Byte*)&U.result, sizeof(U.result));
212
213 return U.result;
214}

References __func__, fileName_, needsSwap(), readByteBlock(), Exception::setCause(), and TPEF::Swapper::swap().

Referenced by TPEF::AOutTextSectionReader::initializeImmediateMove(), TPEF::AOutTextSectionReader::initializeMove(), TPEF::AOutRelocationSectionReader::initializeRelocElement(), TPEF::AOutSymbolSectionReader::initializeSymbol(), TPEF::TPEFReader::readData(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), TPEF::AOutReader::readHeader(), TPEF::TPEFSectionReader::readInfo(), and readWordBlock().

Here is the call graph for this function:

◆ readWordBlock()

void TPEF::BinaryStream::readWordBlock ( Word *  buffer,
unsigned int  howmany 
)

Reads a block of Words from the binary stream.

Parameters
bufferis where the words are returned.
howmanyis how many words are supposed to be read.
Exceptions
UnreachableStreamIf reading from the stream fails.
EndOfFileIf end of file were reached.

Definition at line 283 of file BinaryStream.cc.

283 {
284 try {
285 for (unsigned int i = 0; i < howmany; i++) {
286 buffer[i] = readWord();
287 }
288 } catch (const EndOfFile& error) {
289 EndOfFile newException =
290 EndOfFile(__FILE__, __LINE__, __func__, fileName_);
291 newException.setCause(error);
292 throw newException;
293
294 } catch (const UnreachableStream& error) {
295 UnreachableStream newException =
296 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
297 newException.setCause(error);
298 throw newException;
299 }
300}

References __func__, fileName_, readWord(), and Exception::setCause().

Here is the call graph for this function:

◆ setReadPosition()

void TPEF::BinaryStream::setReadPosition ( unsigned int  position)

Sets the read cursor position in the stream.

If the stream has reached end-of-file, and read position is then set before eof, eof-status is automatically cleared and reading is again possible. Setting position beyond eof doesn't immediately set eof-status, instead, it is only set after trying to read past eof.

Parameters
positionNew read cursor position.
Exceptions
UnreachableStreamIf stream is bad or otherwise unreachable.

Definition at line 629 of file BinaryStream.cc.

629 {
630 if (extOStream_ != NULL) {
631 throw UnreachableStream(
632 __FILE__, __LINE__, __func__, "External stream is write-only.");
633 }
634
635 if (!iStream_.is_open()) {
636 try {
638
639 } catch (const UnreachableStream& error) {
640 UnreachableStream newException =
641 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
642 newException.setCause(error);
643 throw newException;
644 }
645 }
646 if (iStream_.bad()) {
647 throw UnreachableStream(
648 __FILE__, __LINE__, __func__, fileName_);
649 }
650
651 // if eof is already reached, the stream must be cleared to make it
652 // accessible again
653 bool eof = false;
654 if (iStream_.eof()) {
655 eof = true;
656 iStream_.clear();
657 }
658
659 // possible eof-status is cleared if the position is set before eof
660 iStream_.seekg(0, ios::end);
661 unsigned int fileSize = iStream_.tellg();
662 if (position <= fileSize) {
663 iStream_.clear();
664 eof = false;
665 }
666
667 iStream_.seekg(position);
668
669 // return the eof status if it has been cleared earlier to access the
670 // stream and read position is not set before eof
671 if (eof) {
672 iStream_.setstate(ios::eofbit);
673 }
674}

References __func__, extOStream_, fileName_, iStream_, openInput(), and Exception::setCause().

Referenced by TPEF::AOutReader::isMyStreamType(), TPEF::TPEFReader::isMyStreamType(), Assembler::readAssemblerCode(), TPEF::TPEFReader::readData(), TPEF::TPEFASpaceSectionReader::readData(), TPEF::TPEFDebugSectionReader::readData(), TPEF::TPEFLineNumSectionReader::readData(), TPEF::TPEFRelocSectionReader::readData(), TPEF::TPEFResourceSectionReader::readData(), TPEF::TPEFSectionReader::readData(), TPEF::TPEFSymbolSectionReader::readData(), and sizeOfFile().

Here is the call graph for this function:

◆ setTPEFVersion()

void TPEF::BinaryStream::setTPEFVersion ( TPEFHeaders::TPEFVersion  version)

Sets TPEF version to be used.

Parameters
versionTPEF format version.

Definition at line 86 of file BinaryStream.cc.

86 {
87 tpefVersion_ = version;
88}

References tpefVersion_.

Referenced by TPEF::TPEFWriter::actualWriteBinary(), and TPEF::TPEFReader::readData().

◆ setWritePosition()

void TPEF::BinaryStream::setWritePosition ( unsigned int  position)

Sets the write cursor position in the stream.

If write position is located past the end of the file and writing to the stream is attempted, WritePastEOF exception will be thrown.

See also
putByte
Parameters
positionNew write cursor position.
Exceptions
UnreachableStreamIf stream is bad or otherwise unreachable.

Definition at line 689 of file BinaryStream.cc.

689 {
690 if (extOStream_ != NULL) {
691 extOStream_->seekp(position);
692 return;
693 }
694
695 if (!oStream_.is_open()) {
696 try {
698
699 } catch (const UnreachableStream& error) {
700 UnreachableStream newException =
701 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
702 newException.setCause(error);
703 throw newException;
704 }
705 }
706
707 if (oStream_.bad()) {
708 throw UnreachableStream(
709 __FILE__, __LINE__, __func__, fileName_);
710 }
711
712 oStream_.seekp(position);
713}
void openOutput(std::string name)

References __func__, extOStream_, fileName_, openOutput(), oStream_, and Exception::setCause().

Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFSectionWriter::actualWriteHeader(), sizeOfFile(), TPEF::FileOffsetReplacer::tryToReplace(), TPEF::SectionIdReplacer::tryToReplace(), TPEF::SectionIndexReplacer::tryToReplace(), TPEF::SectionOffsetReplacer::tryToReplace(), and TPEF::SectionSizeReplacer::tryToReplace().

Here is the call graph for this function:

◆ sizeOfFile()

unsigned int TPEF::BinaryStream::sizeOfFile ( )

Checks the size of the file being handled.

First tries to check the size from output stream, if it's not opened, tries to check from input stream.

Exceptions
UnreachableStreamIf stream is bad or file does not exist.
Note
UnreachableStream is also generated if file is existing with only write rights and it is not yet opened for writing. This is because of restricted file handling abilities of C++ standard library.
Returns
The size of the file.

Definition at line 764 of file BinaryStream.cc.

764 {
765 if (extOStream_ != NULL) {
766 unsigned int currentPos = extOStream_->tellp();
767 extOStream_->seekp(0, ios::end);
768 unsigned int fileSize = extOStream_->tellp();
769 extOStream_->seekp(currentPos, ios::end);
770 return fileSize;
771 }
772
773 if (oStream_.is_open()) {
774 unsigned int currentPos = writePosition();
775 oStream_.seekp(0, ios::end);
776 unsigned int fileSize = oStream_.tellp();
777 setWritePosition(currentPos);
778 return fileSize;
779
780 } else if (!iStream_.is_open()) {
781 try {
783
784 } catch (const UnreachableStream& error) {
785 UnreachableStream newException =
786 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
787 newException.setCause(error);
788 throw newException;
789 }
790 }
791
792 if (iStream_.bad()) {
793 throw UnreachableStream(
794 __FILE__, __LINE__, __func__, fileName_);
795 }
796
797 unsigned int currentPos = readPosition();
798 iStream_.seekg(0, ios::end);
799 unsigned int fileSize = iStream_.tellg();
800 setReadPosition(currentPos);
801 return fileSize;
802}
void setReadPosition(unsigned int position)
unsigned int readPosition()
unsigned int writePosition()
void setWritePosition(unsigned int position)

References __func__, extOStream_, fileName_, iStream_, openInput(), oStream_, readPosition(), Exception::setCause(), setReadPosition(), setWritePosition(), and writePosition().

Referenced by TPEF::AOutReader::isMyStreamType(), Assembler::readAssemblerCode(), and TPEF::AOutReader::readHeader().

Here is the call graph for this function:

◆ TPEFVersion()

TPEFHeaders::TPEFVersion TPEF::BinaryStream::TPEFVersion ( ) const

Returns TPEF version being used.

Definition at line 95 of file BinaryStream.cc.

95 {
96 return tpefVersion_;
97}

References tpefVersion_.

Referenced by TPEF::TPEFCodeSectionReader::readId(), and TPEF::TPEFCodeSectionWriter::writeId().

◆ writeByte()

void TPEF::BinaryStream::writeByte ( Byte  byte)

Writes one Byte to the binary stream.

Parameters
byteThe Byte to write to the stream.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 310 of file BinaryStream.cc.

310 {
311 try {
312 putByte(byte);
313
314 } catch (const WritePastEOF& error) {
315 WritePastEOF newException =
316 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
317 newException.setCause(error);
318 throw newException;
319
320 } catch (const UnreachableStream& error) {
321 UnreachableStream newException =
322 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
323 newException.setCause(error);
324 throw newException;
325 }
326}
void putByte(Byte byte)

References __func__, fileName_, putByte(), and Exception::setCause().

Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFDataSectionWriter::actualWriteData(), TPEF::TPEFStringSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFRelocSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::TPEFCodeSectionWriter::writeAnnotations(), TPEF::TPEFCodeSectionWriter::writeAttributeField(), TPEF::TPEFCodeSectionWriter::writeDataField(), TPEF::TPEFASpaceSectionWriter::writeElement(), TPEF::TPEFCodeSectionWriter::writeId(), TPEF::TPEFCodeSectionWriter::writeInfo(), and TPEF::SectionIndexReplacer::writeReplacement().

Here is the call graph for this function:

◆ writeByteBlock()

void TPEF::BinaryStream::writeByteBlock ( Byte bytes,
unsigned int  howmany 
)

Writes a block of Bytes to the binary stream.

Parameters
bytesThe block of Bytes to be written.
howmanyHow many Bytes to write.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 401 of file BinaryStream.cc.

401 {
402 try {
403 for (unsigned int i = 0; i < howmany; i++) {
404 putByte(bytes[i]);
405 }
406 } catch (const WritePastEOF& error) {
407 WritePastEOF newException =
408 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
409 newException.setCause(error);
410 throw newException;
411
412 } catch (const UnreachableStream& error) {
413 UnreachableStream newException =
414 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
415 newException.setCause(error);
416 throw newException;
417 }
418}

References __func__, fileName_, putByte(), and Exception::setCause().

Referenced by writeHalfWord(), and writeWord().

Here is the call graph for this function:

◆ writeHalfWord()

void TPEF::BinaryStream::writeHalfWord ( HalfWord  halfword)

Writes one HalfWord to the binary stream.

Parameters
halfwordThe HalfWord to write to the stream.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 336 of file BinaryStream.cc.

336 {
337 Byte buffer[sizeof(HalfWord)];
338
339 // convert HalfWord to the target byte order.
340 if (needsSwap())
341 Swapper::swap((Byte*)&halfword, buffer, sizeof(HalfWord));
342
343 try {
344 writeByteBlock(buffer, sizeof(HalfWord));
345
346 } catch (const WritePastEOF& error) {
347 WritePastEOF newException =
348 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
349 newException.setCause(error);
350 throw newException;
351
352 } catch (const UnreachableStream& error) {
353 UnreachableStream newException =
354 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
355 newException.setCause(error);
356 throw newException;
357 }
358}
void writeByteBlock(Byte *bytes, unsigned int howmany)

References __func__, fileName_, needsSwap(), Exception::setCause(), TPEF::Swapper::swap(), and writeByteBlock().

Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFLineNumSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::SectionIdReplacer::tryToReplace(), TPEF::TPEFCodeSectionWriter::writeDataField(), writeHalfWordBlock(), TPEF::TPEFCodeSectionWriter::writeId(), TPEF::TPEFCodeSectionWriter::writeInfo(), TPEF::TPEFLineNumSectionWriter::writeInfo(), TPEF::TPEFRelocSectionWriter::writeInfo(), and TPEF::SectionIndexReplacer::writeReplacement().

Here is the call graph for this function:

◆ writeHalfWordBlock()

void TPEF::BinaryStream::writeHalfWordBlock ( HalfWord *  halfwords,
unsigned int  howmany 
)

Writes a block of HalfWords to the binary stream.

Parameters
halfwordsThe block of HalfWords to be written.
howmanyHow many HalfWords to write.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 429 of file BinaryStream.cc.

429 {
430 try {
431 for (unsigned int i = 0; i < howmany; i++) {
432 writeHalfWord(halfwords[i]);
433 }
434 } catch (const WritePastEOF& error) {
435 WritePastEOF newException =
436 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
437 newException.setCause(error);
438 throw newException;
439
440 } catch (const UnreachableStream& error) {
441 UnreachableStream newException =
442 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
443 newException.setCause(error);
444 throw newException;
445 }
446}
void writeHalfWord(HalfWord halfword)

References __func__, fileName_, Exception::setCause(), and writeHalfWord().

Here is the call graph for this function:

◆ writePosition()

unsigned int TPEF::BinaryStream::writePosition ( )

Returns the current position of the write cursor.

Exceptions
UnreachableStreamIf stream is bad or otherwise unreachable.

Definition at line 592 of file BinaryStream.cc.

592 {
593 if (extOStream_ != NULL) {
594 return extOStream_->tellp();
595 }
596
597 if (!oStream_.is_open()) {
598 try {
600
601 } catch (const UnreachableStream& error) {
602 UnreachableStream newException =
603 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
604 newException.setCause(error);
605 throw newException;
606 }
607 }
608
609 if (oStream_.bad()) {
610 throw UnreachableStream(
611 __FILE__, __LINE__, __func__, fileName_);
612 }
613 return oStream_.tellp();
614}

References __func__, extOStream_, fileName_, openOutput(), oStream_, and Exception::setCause().

Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFCodeSectionWriter::actualWriteData(), TPEF::TPEFDataSectionWriter::actualWriteData(), TPEF::TPEFStringSectionWriter::actualWriteData(), TPEF::TPEFASpaceSectionWriter::actualWriteData(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFLineNumSectionWriter::actualWriteData(), TPEF::TPEFRelocSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFSymbolSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), sizeOfFile(), and TPEF::ValueReplacer::ValueReplacer().

Here is the call graph for this function:

◆ writeWord()

void TPEF::BinaryStream::writeWord ( Word  word)

Writes one Word to the binary stream.

Parameters
wordThe Word to write to the stream.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 368 of file BinaryStream.cc.

368 {
369 Byte buffer[sizeof(Word)];
370
371 // convert Word to the target byte order.
372 if (needsSwap())
373 Swapper::swap((Byte*)&word, buffer, sizeof(Word));
374
375 try {
376 writeByteBlock(buffer, sizeof(Word));
377
378 } catch (const WritePastEOF& error) {
379 WritePastEOF newException =
380 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
381 newException.setCause(error);
382 throw newException;
383
384 } catch (const UnreachableStream& error) {
385 UnreachableStream newException =
386 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
387 newException.setCause(error);
388 throw newException;
389 }
390}

References __func__, fileName_, needsSwap(), Exception::setCause(), TPEF::Swapper::swap(), and writeByteBlock().

Referenced by TPEF::TPEFWriter::actualWriteBinary(), TPEF::TPEFDebugSectionWriter::actualWriteData(), TPEF::TPEFResourceSectionWriter::actualWriteData(), TPEF::TPEFSectionWriter::actualWriteHeader(), TPEF::FileOffsetReplacer::tryToReplace(), TPEF::SectionOffsetReplacer::tryToReplace(), TPEF::SectionSizeReplacer::tryToReplace(), TPEF::TPEFSectionWriter::writeBodyStartOffset(), TPEF::TPEFSectionWriter::writeInfo(), TPEF::SectionIndexReplacer::writeReplacement(), TPEF::TPEFNullSectionWriter::writeSize(), TPEF::TPEFUDataSectionWriter::writeSize(), TPEF::TPEFSymbolSectionWriter::writeValueAndSize(), and writeWordBlock().

Here is the call graph for this function:

◆ writeWordBlock()

void TPEF::BinaryStream::writeWordBlock ( Word *  words,
unsigned int  howmany 
)

Writes a block of Words to the binary stream.

Parameters
wordsThe block of Words to be written.
howmanyHow many Words to write.
Exceptions
UnreachableStreamIf writing to stream fails.
WritePastEOFIf tried to write past end of file.

Definition at line 457 of file BinaryStream.cc.

457 {
458 try {
459 for (unsigned int i = 0; i < howmany; i++) {
460 writeWord(words[i]);
461 }
462 } catch (const WritePastEOF& error) {
463 WritePastEOF newException =
464 WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
465 newException.setCause(error);
466 throw newException;
467
468 } catch (const UnreachableStream& error) {
469 UnreachableStream newException =
470 UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
471 newException.setCause(error);
472 throw newException;
473 }
474}
void writeWord(Word word)

References __func__, fileName_, Exception::setCause(), and writeWord().

Here is the call graph for this function:

Member Data Documentation

◆ extOStream_

std::ostream* TPEF::BinaryStream::extOStream_
private

Externally given output stream.

Definition at line 100 of file BinaryStream.hh.

Referenced by endOfFile(), openInput(), openOutput(), readPosition(), setReadPosition(), setWritePosition(), sizeOfFile(), and writePosition().

◆ fileName_

std::string TPEF::BinaryStream::fileName_
private

◆ iStream_

std::ifstream TPEF::BinaryStream::iStream_
private

The input stream.

Definition at line 93 of file BinaryStream.hh.

Referenced by close(), endOfFile(), openInput(), readPosition(), setReadPosition(), and sizeOfFile().

◆ littleEndianStorage_

bool TPEF::BinaryStream::littleEndianStorage_
private

In case we want to store the words in little endian order, big endian otherwise.

Definition at line 104 of file BinaryStream.hh.

Referenced by needsSwap().

◆ oStream_

std::ofstream TPEF::BinaryStream::oStream_
private

The output stream.

Definition at line 95 of file BinaryStream.hh.

Referenced by close(), openInput(), openOutput(), setWritePosition(), sizeOfFile(), and writePosition().

◆ tpefVersion_

TPEFHeaders::TPEFVersion TPEF::BinaryStream::tpefVersion_
private

Indicates TPEF format version used.

Definition at line 107 of file BinaryStream.hh.

Referenced by setTPEFVersion(), and TPEFVersion().


The documentation for this class was generated from the following files: