From 6150d728a547121c26dea55698092f8cc5973baf Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Fri, 25 Apr 2014 15:17:52 -0700 Subject: [PATCH] * Changed swap* functions to both return, and take a reference * Fixed writeInt64 in FileWriter * Fixed ec.cpp on big endian systems --- include/Athena/Utility.hpp | 12 ++++++------ src/Athena/FileWriter.cpp | 2 +- src/Athena/Utility.cpp | 24 ++++++++++++------------ src/ec.cpp | 13 ++++++++++--- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/Athena/Utility.hpp b/include/Athena/Utility.hpp index fb25fa3..b128862 100644 --- a/include/Athena/Utility.hpp +++ b/include/Athena/Utility.hpp @@ -27,12 +27,12 @@ namespace utility { bool isEmpty(Int8*, Uint32); -Uint16 swapU16(Uint16 val ); -Int16 swap16 (Int16 val ); -Uint32 swapU32(Uint32 val); -Int32 swap32 (Int32 val ); -Uint64 swapU64(Uint64 val); -Int64 swap64 (Int64 val); +Uint16 swapU16(Uint16& val ); +Int16 swap16 (Int16& val ); +Uint32 swapU32(Uint32& val); +Int32 swap32 (Int32& val ); +Uint64 swapU64(Uint64& val); +Int64 swap64 (Int64& val); float swapFloat(float val); double swapDouble(double val); diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index ca73c43..0ab0764 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -219,7 +219,7 @@ void FileWriter::writeUint64(Uint64 val) m_bitValid = false; 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)) THROW_IO_EXCEPTION("Unable to write to stream"); diff --git a/src/Athena/Utility.cpp b/src/Athena/Utility.cpp index fe061ba..1c80212 100644 --- a/src/Athena/Utility.cpp +++ b/src/Athena/Utility.cpp @@ -32,43 +32,43 @@ bool isEmpty(Int8* buf, Uint32 size) 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 & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8; 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) & 0x0000FF0000000000ULL) >> 24) | (((Int64)(val) & 0x000000FF00000000ULL) >> 8) | (((Int64)(val) & 0x00000000FF000000ULL) << 8) | (((Int64)(val) & 0x0000000000FF0000ULL) << 24) | (((Int64)(val) & 0x000000000000FF00ULL) << 40) | - (((Int64)(val) & 0x00000000000000FFULL) << 56))); + (((Int64)(val) & 0x00000000000000FFULL) << 56)))); } bool isSystemBigEndian() diff --git a/src/ec.cpp b/src/ec.cpp index d73faed..582ce61 100644 --- a/src/ec.cpp +++ b/src/ec.cpp @@ -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 ) { 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); 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); - *(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); }