forked from steemit/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed_string endian swap steemit#951
- Loading branch information
Michael Vandeberg
committed
May 2, 2017
1 parent
d6297b0
commit 51eab68
Showing
15 changed files
with
179 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
libraries/plugins/auth_util/include/steemit/plugins/auth_util/auth_util_api.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <steemit/protocol/fixed_string.hpp> | ||
|
||
#include <boost/endian/conversion.hpp> | ||
|
||
namespace fc { | ||
void to_variant( const steemit::protocol::fixed_string& s, variant& v ) { v = std::string( s ); } | ||
void from_variant( const variant& v, steemit::protocol::fixed_string& s ) { s = v.as_string(); } | ||
|
||
uint128 endian_reverse( const uint128& u ) | ||
{ | ||
return uint128( boost::endian::endian_reverse( u.hi ), boost::endian::endian_reverse( u.lo ) ); | ||
} | ||
} // fc | ||
|
||
namespace steemit { namespace protocol { | ||
|
||
fixed_string::fixed_string( const std::string& str ) | ||
{ | ||
Storage d; | ||
if( str.size() <= sizeof(d) ) | ||
memcpy( (char*)&d, str.c_str(), str.size() ); | ||
else | ||
memcpy( (char*)&d, str.c_str(), sizeof(d) ); | ||
|
||
data = boost::endian::big_to_native( d ); | ||
} | ||
|
||
fixed_string::operator std::string()const | ||
{ | ||
Storage d = boost::endian::native_to_big( data ); | ||
size_t s; | ||
|
||
if( *(((const char*)&d) + sizeof(d) - 1) ) | ||
s = sizeof(d); | ||
else | ||
s = strnlen( (const char*)&d, sizeof(d) ); | ||
|
||
const char* self = (const char*)&d; | ||
|
||
return std::string( self, self + s ); | ||
} | ||
|
||
uint32_t fixed_string::size()const | ||
{ | ||
Storage d = boost::endian::native_to_big( data ); | ||
if( *(((const char*)&d) + sizeof(d) - 1) ) | ||
return sizeof(d); | ||
return strnlen( (const char*)&d, sizeof(d) ); | ||
} | ||
|
||
} } // steemit::protocol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
libraries/protocol/include/steemit/protocol/fixed_string.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#pragma once | ||
|
||
#include <fc/uint128.hpp> | ||
#include <fc/io/raw_fwd.hpp> | ||
|
||
namespace steemit { namespace protocol { | ||
|
||
/** | ||
* This class is an in-place memory allocation of a 16 character string. | ||
* | ||
* The string will serialize the same way as std::string for variant and raw formats, | ||
* but will be in memory as a 128-bit integer so that we can exploit efficient | ||
* integer comparisons for sorting. | ||
*/ | ||
class fixed_string | ||
{ | ||
public: | ||
typedef fc::uint128_t Storage; | ||
|
||
fixed_string(){} | ||
fixed_string( const fixed_string& c ) : data( c.data ){} | ||
fixed_string( const char* str ) : fixed_string( std::string( str ) ) {} | ||
fixed_string( const std::string& str ); | ||
|
||
operator std::string()const; | ||
|
||
uint32_t size()const; | ||
|
||
uint32_t length()const { return size(); } | ||
|
||
fixed_string& operator = ( const fixed_string& str ) | ||
{ | ||
data = str.data; | ||
return *this; | ||
} | ||
|
||
fixed_string& operator = ( const char* str ) | ||
{ | ||
*this = fixed_string( str ); | ||
return *this; | ||
} | ||
|
||
fixed_string& operator = ( const std::string& str ) | ||
{ | ||
*this = fixed_string( str ); | ||
return *this; | ||
} | ||
|
||
friend std::string operator + ( const fixed_string& a, const std::string& b ) { return std::string( a ) + b; } | ||
friend std::string operator + ( const std::string& a, const fixed_string& b ){ return a + std::string( b ); } | ||
friend bool operator < ( const fixed_string& a, const fixed_string& b ) { return a.data < b.data; } | ||
friend bool operator <= ( const fixed_string& a, const fixed_string& b ) { return a.data <= b.data; } | ||
friend bool operator > ( const fixed_string& a, const fixed_string& b ) { return a.data > b.data; } | ||
friend bool operator >= ( const fixed_string& a, const fixed_string& b ) { return a.data >= b.data; } | ||
friend bool operator == ( const fixed_string& a, const fixed_string& b ) { return a.data == b.data; } | ||
friend bool operator != ( const fixed_string& a, const fixed_string& b ) { return a.data != b.data; } | ||
|
||
Storage data; | ||
}; | ||
|
||
} } // steemit::protocol | ||
|
||
namespace fc { namespace raw { | ||
|
||
template< typename Stream > | ||
inline void pack( Stream& s, const steemit::protocol::fixed_string& u ) | ||
{ | ||
pack( s, std::string( u ) ); | ||
} | ||
|
||
template< typename Stream > | ||
inline void unpack( Stream& s, steemit::protocol::fixed_string& u ) | ||
{ | ||
std::string str; | ||
unpack( s, str ); | ||
u = str; | ||
} | ||
|
||
} // raw | ||
|
||
void to_variant( const steemit::protocol::fixed_string& s, variant& v ); | ||
void from_variant( const variant& v, steemit::protocol::fixed_string& s ); | ||
} // fc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters