diff --git a/I2PEndian.cpp b/I2PEndian.cpp index 1fccf47f..fa4e08cb 100644 --- a/I2PEndian.cpp +++ b/I2PEndian.cpp @@ -1,81 +1,81 @@ -#include "I2PEndian.h" - -// http://habrahabr.ru/post/121811/ -// http://codepad.org/2ycmkz2y - -#include "LittleBigEndian.h" - -uint16_t htobe16(uint16_t int16) -{ - BigEndian u16(int16); - return u16.raw_value; -} - -uint32_t htobe32(uint32_t int32) -{ - BigEndian u32(int32); - return u32.raw_value; -} - -uint64_t htobe64(uint64_t int64) -{ - BigEndian u64(int64); - return u64.raw_value; -} - -uint16_t be16toh(uint16_t big16) -{ - LittleEndian u16(big16); - return u16.raw_value; -} - -uint32_t be32toh(uint32_t big32) -{ - LittleEndian u32(big32); - return u32.raw_value; -} - -uint64_t be64toh(uint64_t big64) -{ - LittleEndian u64(big64); - return u64.raw_value; -} - -/* it can be used in Windows 8 -#include - -uint16_t htobe16(uint16_t int16) -{ - return htons(int16); -} - -uint32_t htobe32(uint32_t int32) -{ - return htonl(int32); -} - -uint64_t htobe64(uint64_t int64) -{ - // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx - //return htonll(int64); - return 0; -} - - -uint16_t be16toh(uint16_t big16) -{ - return ntohs(big16); -} - -uint32_t be32toh(uint32_t big32) -{ - return ntohl(big32); -} - -uint64_t be64toh(uint64_t big64) -{ - // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx - //return ntohll(big64); - return 0; -} -*/ \ No newline at end of file +//#include "I2PEndian.h" +// +//// http://habrahabr.ru/post/121811/ +//// http://codepad.org/2ycmkz2y +// +//#include "LittleBigEndian.h" +// +//uint16_t htobe16(uint16_t int16) +//{ +// BigEndian u16(int16); +// return u16.raw_value; +//} +// +//uint32_t htobe32(uint32_t int32) +//{ +// BigEndian u32(int32); +// return u32.raw_value; +//} +// +//uint64_t htobe64(uint64_t int64) +//{ +// BigEndian u64(int64); +// return u64.raw_value; +//} +// +//uint16_t be16toh(uint16_t big16) +//{ +// LittleEndian u16(big16); +// return u16.raw_value; +//} +// +//uint32_t be32toh(uint32_t big32) +//{ +// LittleEndian u32(big32); +// return u32.raw_value; +//} +// +//uint64_t be64toh(uint64_t big64) +//{ +// LittleEndian u64(big64); +// return u64.raw_value; +//} +// +///* it can be used in Windows 8 +//#include +// +//uint16_t htobe16(uint16_t int16) +//{ +// return htons(int16); +//} +// +//uint32_t htobe32(uint32_t int32) +//{ +// return htonl(int32); +//} +// +//uint64_t htobe64(uint64_t int64) +//{ +// // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx +// //return htonll(int64); +// return 0; +//} +// +// +//uint16_t be16toh(uint16_t big16) +//{ +// return ntohs(big16); +//} +// +//uint32_t be32toh(uint32_t big32) +//{ +// return ntohl(big32); +//} +// +//uint64_t be64toh(uint64_t big64) +//{ +// // http://msdn.microsoft.com/en-us/library/windows/desktop/jj710199%28v=vs.85%29.aspx +// //return ntohll(big64); +// return 0; +//} +//*/ \ No newline at end of file diff --git a/I2PEndian.h b/I2PEndian.h index 01ba73e8..3014f564 100644 --- a/I2PEndian.h +++ b/I2PEndian.h @@ -5,14 +5,16 @@ #include #else #include +// +//uint16_t htobe16(uint16_t int16); +//uint32_t htobe32(uint32_t int32); +//uint64_t htobe64(uint64_t int64); +// +//uint16_t be16toh(uint16_t big16); +//uint32_t be32toh(uint32_t big32); +//uint64_t be64toh(uint64_t big64); -uint16_t htobe16(uint16_t int16); -uint32_t htobe32(uint32_t int32); -uint64_t htobe64(uint64_t int64); - -uint16_t be16toh(uint16_t big16); -uint32_t be32toh(uint32_t big32); -uint64_t be64toh(uint64_t big64); +#include "portable_endian.h" #endif diff --git a/RouterContext.cpp b/RouterContext.cpp index 3e19c810..75a3eb51 100644 --- a/RouterContext.cpp +++ b/RouterContext.cpp @@ -60,7 +60,7 @@ namespace i2p bool RouterContext::Load () { - std::ifstream fk (ROUTER_KEYS); + std::ifstream fk (ROUTER_KEYS, std::ifstream::binary | std::ofstream::in); if (!fk.is_open ()) return false; fk.read ((char *)&m_Keys, sizeof (m_Keys)); @@ -74,10 +74,10 @@ namespace i2p void RouterContext::Save () { - std::ofstream fk (ROUTER_KEYS); + std::ofstream fk (ROUTER_KEYS, std::ofstream::binary | std::ofstream::out); fk.write ((char *)&m_Keys, sizeof (m_Keys)); - std::ofstream fi (ROUTER_INFO); + std::ofstream fi (ROUTER_INFO, std::ofstream::binary | std::ofstream::out); fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ()); } } diff --git a/RouterInfo.cpp b/RouterInfo.cpp index 5c2eecef..41d57117 100644 --- a/RouterInfo.cpp +++ b/RouterInfo.cpp @@ -12,6 +12,8 @@ #include "RouterInfo.h" #include "RouterContext.h" + + namespace i2p { namespace data @@ -41,7 +43,7 @@ namespace data void RouterInfo::ReadFromFile (const char * filename) { - std::ifstream s(filename); + std::ifstream s(filename, std::ifstream::binary); if (s.is_open ()) { s.seekg (0,std::ios::end); @@ -95,7 +97,7 @@ namespace data size = be16toh (size); while (r < size) { - char key[50], value[50]; + char key[500], value[500]; r += ReadString (key, s); s.seekg (1, std::ios_base::cur); r++; // = r += ReadString (value, s); @@ -126,7 +128,7 @@ namespace data size = be16toh (size); while (r < size) { - char key[50], value[50]; + char key[500], value[500]; r += ReadString (key, s); s.seekg (1, std::ios_base::cur); r++; // = r += ReadString (value, s); diff --git a/Win32/.gitignore b/Win32/.gitignore new file mode 100644 index 00000000..d62f96f6 --- /dev/null +++ b/Win32/.gitignore @@ -0,0 +1,8 @@ +* +!*/ + +!*.sln +!*.vcproj +!*.vcxproj +!*.vcxproj.filters +!.gitignore diff --git a/portable_endian.h b/portable_endian.h new file mode 100644 index 00000000..3355d2b9 --- /dev/null +++ b/portable_endian.h @@ -0,0 +1,115 @@ +#ifndef PORTABLE_ENDIAN_H__ +#define PORTABLE_ENDIAN_H__ + +#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) + +# define __WINDOWS__ + +#endif + +#if defined(__linux__) || defined(__CYGWIN__) + +# include + +#elif defined(__APPLE__) + +# include + +# define htobe16 OSSwapHostToBigInt16 +# define htole16 OSSwapHostToLittleInt16 +# define be16toh OSSwapBigToHostInt16 +# define le16toh OSSwapLittleToHostInt16 + +# define htobe32 OSSwapHostToBigInt32 +# define htole32 OSSwapHostToLittleInt32 +# define be32toh OSSwapBigToHostInt32 +# define le32toh OSSwapLittleToHostInt32 + +# define htobe64 OSSwapHostToBigInt64 +# define htole64 OSSwapHostToLittleInt64 +# define be64toh OSSwapBigToHostInt64 +# define le64toh OSSwapLittleToHostInt64 + +# define __BYTE_ORDER BYTE_ORDER +# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __PDP_ENDIAN PDP_ENDIAN + +#elif defined(__OpenBSD__) + +# include + +#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) + +# include + +# define be16toh betoh16 +# define le16toh letoh16 + +# define be32toh betoh32 +# define le32toh letoh32 + +# define be64toh betoh64 +# define le64toh letoh64 + +#elif defined(__WINDOWS__) + +#define INCL_EXTRA_HTON_FUNCTIONS +#define NOMINMAX +# include +#undef NOMINMAX +//# include + +# if BYTE_ORDER == LITTLE_ENDIAN + +# define htobe16 htons +# define htole16(x) (x) +# define be16toh ntohs +# define le16toh(x) (x) + +# define htobe32 htonl +# define htole32(x) (x) +# define be32toh ntohl +# define le32toh(x) (x) + +# define htobe64 htonll +# define htole64(x) (x) +# define be64toh ntohll +# define le64toh(x) (x) + +# elif BYTE_ORDER == BIG_ENDIAN + + /* that would be xbox 360 */ +# define htobe16(x) (x) +# define htole16(x) __builtin_bswap16(x) +# define be16toh(x) (x) +# define le16toh(x) __builtin_bswap16(x) + +# define htobe32(x) (x) +# define htole32(x) __builtin_bswap32(x) +# define be32toh(x) (x) +# define le32toh(x) __builtin_bswap32(x) + +# define htobe64(x) (x) +# define htole64(x) __builtin_bswap64(x) +# define be64toh(x) (x) +# define le64toh(x) __builtin_bswap64(x) + +# else + +# error byte order not supported + +# endif + +# define __BYTE_ORDER BYTE_ORDER +# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __PDP_ENDIAN PDP_ENDIAN + +#else + +# error platform not supported + +#endif + +#endif \ No newline at end of file