Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Windows support #6

Merged
merged 2 commits into from
Jan 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Garlic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <inttypes.h>
#include <endian.h>
#include "I2PEndian.h"
#include <map>
#include <string>
#include "ElGamal.h"
Expand Down
2 changes: 1 addition & 1 deletion I2NPProtocol.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <string.h>
#include <endian.h>
#include "I2PEndian.h"
#include <cryptopp/sha.h>
#include <cryptopp/modes.h>
#include <cryptopp/aes.h>
Expand Down
81 changes: 81 additions & 0 deletions I2PEndian.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "I2PEndian.h"

// http://habrahabr.ru/post/121811/
// http://codepad.org/2ycmkz2y

#include "LittleBigEndian.h"

uint16_t htobe16(uint16_t int16)
{
BigEndian<uint16_t> u16(int16);
return u16.raw_value;
}

uint32_t htobe32(uint32_t int32)
{
BigEndian<uint32_t> u32(int32);
return u32.raw_value;
}

uint64_t htobe64(uint64_t int64)
{
BigEndian<uint64_t> u64(int64);
return u64.raw_value;
}

uint16_t be16toh(uint16_t big16)
{
LittleEndian<uint16_t> u16(big16);
return u16.raw_value;
}

uint32_t be32toh(uint32_t big32)
{
LittleEndian<uint32_t> u32(big32);
return u32.raw_value;
}

uint64_t be64toh(uint64_t big64)
{
LittleEndian<uint64_t> u64(big64);
return u64.raw_value;
}

/* it can be used in Windows 8
#include <Winsock2.h>

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;
}
*/
19 changes: 19 additions & 0 deletions I2PEndian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef I2PENDIAN_H__
#define I2PENDIAN_H__

#ifndef _WIN32
#include <endian.h>
#else
#include <cstdint>

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);

#endif

#endif // I2PENDIAN_H__
4 changes: 4 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ namespace data

IdentHash (const uint8_t * hash) { memcpy (m_Hash, hash, 32); };
IdentHash (const IdentHash& ) = default;
#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
IdentHash (IdentHash&& ) = default;
#endif
IdentHash () = default;

IdentHash& operator= (const IdentHash& ) = default;
#ifndef _WIN32
IdentHash& operator= (IdentHash&& ) = default;
#endif

uint8_t * operator()() { return m_Hash; };
const uint8_t * operator()() const { return m_Hash; };
Expand Down
242 changes: 242 additions & 0 deletions LittleBigEndian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// LittleBigEndian.h fixed for 64-bits added union
//

#ifndef LITTLEBIGENDIAN_H
#define LITTLEBIGENDIAN_H

// Determine Little-Endian or Big-Endian

#define CURRENT_BYTE_ORDER (*(int *)"\x01\x02\x03\x04")
#define LITTLE_ENDIAN_BYTE_ORDER 0x04030201
#define BIG_ENDIAN_BYTE_ORDER 0x01020304
#define PDP_ENDIAN_BYTE_ORDER 0x02010403

#define IS_LITTLE_ENDIAN (CURRENT_BYTE_ORDER == LITTLE_ENDIAN_BYTE_ORDER)
#define IS_BIG_ENDIAN (CURRENT_BYTE_ORDER == BIG_ENDIAN_BYTE_ORDER)
#define IS_PDP_ENDIAN (CURRENT_BYTE_ORDER == PDP_ENDIAN_BYTE_ORDER)

// Forward declaration

template<typename T>
struct LittleEndian;

template<typename T>
struct BigEndian;

// Little-Endian template

#pragma pack(push,1)
template<typename T>
struct LittleEndian
{
union
{
unsigned char bytes[sizeof(T)];
T raw_value;
};

LittleEndian(T t = T())
{
operator =(t);
}

LittleEndian(const LittleEndian<T> & t)
{
raw_value = t.raw_value;
}

LittleEndian(const BigEndian<T> & t)
{
for (unsigned i = 0; i < sizeof(T); i++)
bytes[i] = t.bytes[sizeof(T)-1-i];
}

operator const T() const
{
T t = T();
for (unsigned i = 0; i < sizeof(T); i++)
t |= T(bytes[i]) << (i << 3);
return t;
}

const T operator = (const T t)
{
for (unsigned i = 0; i < sizeof(T); i++)
bytes[i] = t >> (i << 3);
return t;
}

// operators

const T operator += (const T t)
{
return (*this = *this + t);
}

const T operator -= (const T t)
{
return (*this = *this - t);
}

const T operator *= (const T t)
{
return (*this = *this * t);
}

const T operator /= (const T t)
{
return (*this = *this / t);
}

const T operator %= (const T t)
{
return (*this = *this % t);
}

LittleEndian<T> operator ++ (int)
{
LittleEndian<T> tmp(*this);
operator ++ ();
return tmp;
}

LittleEndian<T> & operator ++ ()
{
for (unsigned i = 0; i < sizeof(T); i++)
{
++bytes[i];
if (bytes[i] != 0)
break;
}
return (*this);
}

LittleEndian<T> operator -- (int)
{
LittleEndian<T> tmp(*this);
operator -- ();
return tmp;
}

LittleEndian<T> & operator -- ()
{
for (unsigned i = 0; i < sizeof(T); i++)
{
--bytes[i];
if (bytes[i] != (T)(-1))
break;
}
return (*this);
}
};
#pragma pack(pop)

// Big-Endian template

#pragma pack(push,1)
template<typename T>
struct BigEndian
{
union
{
unsigned char bytes[sizeof(T)];
T raw_value;
};

BigEndian(T t = T())
{
operator =(t);
}

BigEndian(const BigEndian<T> & t)
{
raw_value = t.raw_value;
}

BigEndian(const LittleEndian<T> & t)
{
for (unsigned i = 0; i < sizeof(T); i++)
bytes[i] = t.bytes[sizeof(T)-1-i];
}

operator const T() const
{
T t = T();
for (unsigned i = 0; i < sizeof(T); i++)
t |= T(bytes[sizeof(T) - 1 - i]) << (i << 3);
return t;
}

const T operator = (const T t)
{
for (unsigned i = 0; i < sizeof(T); i++)
bytes[sizeof(T) - 1 - i] = t >> (i << 3);
return t;
}

// operators

const T operator += (const T t)
{
return (*this = *this + t);
}

const T operator -= (const T t)
{
return (*this = *this - t);
}

const T operator *= (const T t)
{
return (*this = *this * t);
}

const T operator /= (const T t)
{
return (*this = *this / t);
}

const T operator %= (const T t)
{
return (*this = *this % t);
}

BigEndian<T> operator ++ (int)
{
BigEndian<T> tmp(*this);
operator ++ ();
return tmp;
}

BigEndian<T> & operator ++ ()
{
for (unsigned i = 0; i < sizeof(T); i++)
{
++bytes[sizeof(T) - 1 - i];
if (bytes[sizeof(T) - 1 - i] != 0)
break;
}
return (*this);
}

BigEndian<T> operator -- (int)
{
BigEndian<T> tmp(*this);
operator -- ();
return tmp;
}

BigEndian<T> & operator -- ()
{
for (unsigned i = 0; i < sizeof(T); i++)
{
--bytes[sizeof(T) - 1 - i];
if (bytes[sizeof(T) - 1 - i] != (T)(-1))
break;
}
return (*this);
}
};
#pragma pack(pop)

#endif // LITTLEBIGENDIAN_H
2 changes: 1 addition & 1 deletion NTCPSession.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <string.h>
#include <stdlib.h>
#include <endian.h>
#include "I2PEndian.h"
#include <time.h>
#include <boost/bind.hpp>
#include <cryptopp/dh.h>
Expand Down