Support stringstream by filling gaps in output stream

This commit is contained in:
Serge Lamikhov-Center 2021-03-27 21:14:56 +03:00
parent 57e614a486
commit becd79d05c
3 changed files with 19 additions and 8 deletions

View File

@ -237,7 +237,7 @@ template <class T> class section_impl : public section
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void save_header( std::ostream& stream, std::streampos header_offset ) const void save_header( std::ostream& stream, std::streampos header_offset ) const
{ {
stream.seekp( header_offset ); adjust_stream_size( stream, header_offset );
stream.write( reinterpret_cast<const char*>( &header ), stream.write( reinterpret_cast<const char*>( &header ),
sizeof( header ) ); sizeof( header ) );
} }
@ -245,7 +245,7 @@ template <class T> class section_impl : public section
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void save_data( std::ostream& stream, std::streampos data_offset ) const void save_data( std::ostream& stream, std::streampos data_offset ) const
{ {
stream.seekp( data_offset ); adjust_stream_size( stream, data_offset );
stream.write( get_data(), get_size() ); stream.write( get_data(), get_size() );
} }

View File

@ -168,7 +168,7 @@ template <class T> class segment_impl : public segment
data = 0; data = 0;
} }
else { else {
data = new (std::nothrow) char[size + 1]; data = new ( std::nothrow ) char[size + 1];
if ( 0 != data ) { if ( 0 != data ) {
stream.read( data, size ); stream.read( data, size );
@ -185,7 +185,7 @@ template <class T> class segment_impl : public segment
{ {
ph.p_offset = data_offset; ph.p_offset = data_offset;
ph.p_offset = ( *convertor )( ph.p_offset ); ph.p_offset = ( *convertor )( ph.p_offset );
stream.seekp( header_offset ); adjust_stream_size( stream, header_offset );
stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) ); stream.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) );
} }

View File

@ -23,6 +23,8 @@ THE SOFTWARE.
#ifndef ELFIO_UTILS_HPP #ifndef ELFIO_UTILS_HPP
#define ELFIO_UTILS_HPP #define ELFIO_UTILS_HPP
#include <iostream>
#define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \ #define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \
TYPE get_##NAME() const { return ( *convertor )( FIELD ); } TYPE get_##NAME() const { return ( *convertor )( FIELD ); }
@ -175,17 +177,26 @@ inline uint32_t elf_hash( const unsigned char* name )
inline std::string to_hex_string( Elf64_Addr t ) inline std::string to_hex_string( Elf64_Addr t )
{ {
std::string s; std::string s;
while( t ) { while ( t ) {
auto d = t & 0xf; auto d = t & 0xf;
if ( d < 0xa ) if ( d < 0xa )
s = char('0' + d) + s; s = char( '0' + d ) + s;
else else
s = char('A' + d - 0xa) + s; s = char( 'A' + d - 0xA ) + s;
t >>= 4; t >>= 4;
} }
return "0x" + s; return "0x" + s;
} }
inline void adjust_stream_size( std::ostream& stream, std::streamsize offset )
{
stream.seekp( 0, stream.end );
if ( stream.tellp() < offset ) {
std::streamsize size = offset - stream.tellp();
stream.write( std::string( size, '\0' ).c_str(), size );
}
stream.seekp( offset );
}
} // namespace ELFIO } // namespace ELFIO