* Changed swap* functions to both return, and take a reference

* Fixed writeInt64 in FileWriter
* Fixed ec.cpp on big endian systems
This commit is contained in:
Phillip Stephens 2014-04-25 15:17:52 -07:00
parent b7c359ea40
commit 6150d728a5
4 changed files with 29 additions and 22 deletions

View File

@ -27,12 +27,12 @@ namespace utility
{ {
bool isEmpty(Int8*, Uint32); bool isEmpty(Int8*, Uint32);
Uint16 swapU16(Uint16 val ); Uint16 swapU16(Uint16& val );
Int16 swap16 (Int16 val ); Int16 swap16 (Int16& val );
Uint32 swapU32(Uint32 val); Uint32 swapU32(Uint32& val);
Int32 swap32 (Int32 val ); Int32 swap32 (Int32& val );
Uint64 swapU64(Uint64 val); Uint64 swapU64(Uint64& val);
Int64 swap64 (Int64 val); Int64 swap64 (Int64& val);
float swapFloat(float val); float swapFloat(float val);
double swapDouble(double val); double swapDouble(double val);

View File

@ -219,7 +219,7 @@ void FileWriter::writeUint64(Uint64 val)
m_bitValid = false; m_bitValid = false;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian())) if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
val = utility::swapU32(val); val = utility::swapU64(val);
if (fwrite(&val, 1, sizeof(Uint64), m_fileHandle) != sizeof(Uint64)) if (fwrite(&val, 1, sizeof(Uint64), m_fileHandle) != sizeof(Uint64))
THROW_IO_EXCEPTION("Unable to write to stream"); THROW_IO_EXCEPTION("Unable to write to stream");

View File

@ -32,43 +32,43 @@ bool isEmpty(Int8* buf, Uint32 size)
return buf[0] == 0 && !memcmp(buf, buf + 1, size - 1); return buf[0] == 0 && !memcmp(buf, buf + 1, size - 1);
} }
Uint16 swapU16(Uint16 val ) Uint16 swapU16(Uint16& val )
{ {
return (Uint16)swap16(val); return (Uint16)swap16((Int16&)val);
} }
Int16 swap16(Int16 val ) Int16 swap16(Int16& val )
{ {
return (val << 8) | ((val >> 8) & 0xFF); return (val = (val << 8) | ((val >> 8) & 0xFF));
} }
Uint32 swapU32(Uint32 val) Uint32 swapU32(Uint32& val)
{ {
return (Uint32)swap32(val); return (Uint32)swap32((Int32&)val);
} }
int swap32(Int32 val ) int swap32(Int32& val )
{ {
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16; val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8; val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val; return val;
} }
Uint64 swapU64(Uint64 val) Uint64 swapU64(Uint64& val)
{ {
return (Uint64)swap64(val); return (Uint64)swap64((Int64&)val);
} }
Int64 swap64(Int64 val) Int64 swap64(Int64& val)
{ {
return ((Int64)((((Int64)(val) & 0xFF00000000000000ULL) >> 56) | return (val = ((Int64)((((Int64)(val) & 0xFF00000000000000ULL) >> 56) |
(((Int64)(val) & 0x00FF000000000000ULL) >> 40) | (((Int64)(val) & 0x00FF000000000000ULL) >> 40) |
(((Int64)(val) & 0x0000FF0000000000ULL) >> 24) | (((Int64)(val) & 0x0000FF0000000000ULL) >> 24) |
(((Int64)(val) & 0x000000FF00000000ULL) >> 8) | (((Int64)(val) & 0x000000FF00000000ULL) >> 8) |
(((Int64)(val) & 0x00000000FF000000ULL) << 8) | (((Int64)(val) & 0x00000000FF000000ULL) << 8) |
(((Int64)(val) & 0x0000000000FF0000ULL) << 24) | (((Int64)(val) & 0x0000000000FF0000ULL) << 24) |
(((Int64)(val) & 0x000000000000FF00ULL) << 40) | (((Int64)(val) & 0x000000000000FF00ULL) << 40) |
(((Int64)(val) & 0x00000000000000FFULL) << 56))); (((Int64)(val) & 0x00000000000000FFULL) << 56))));
} }
bool isSystemBigEndian() bool isSystemBigEndian()

View File

@ -405,11 +405,18 @@ bool check_ec(Uint8 *ng, Uint8 *ap, Uint8 *sig, Uint8 *sig_hash)
void make_ec_cert(Uint8 *cert, Uint8 *sig, char *signer, char *name, Uint8 *priv, Uint32 key_id ) void make_ec_cert(Uint8 *cert, Uint8 *sig, char *signer, char *name, Uint8 *priv, Uint32 key_id )
{ {
memset(cert, 0, 0x180); memset(cert, 0, 0x180);
*(Uint32*)(cert) = Athena::utility::swapU32(0x10002); *(Uint32*)(cert) = 0x10002;
if (!Athena::utility::isSystemBigEndian())
Athena::utility::swapU32(*(Uint32*)(cert));
memcpy((char*)cert + 4, sig, 60); memcpy((char*)cert + 4, sig, 60);
strcpy((char*)cert + 0x80, signer); strcpy((char*)cert + 0x80, signer);
*(Uint32*)(cert + 0xc0) = Athena::utility::swapU32(2); *(Uint32*)(cert + 0xc0) = 2;
if (!Athena::utility::isSystemBigEndian())
Athena::utility::swapU32(*(Uint32*)(cert + 0xc0));
strcpy((char*)cert + 0xc4, name); strcpy((char*)cert + 0xc4, name);
*(Uint32*)(cert + 0x104) = Athena::utility::swapU32(key_id); *(Uint32*)(cert + 0x104) = key_id;
if (!Athena::utility::isSystemBigEndian())
Athena::utility::swapU32(*(Uint32*)(cert + 0x104));
ec_priv_to_pub(priv, cert + 0x108); ec_priv_to_pub(priv, cert + 0x108);
} }