
/*
 * Copyright 2011-2022 Cuberite Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#undef  ntohll
#define ntohll(x) (((static_cast<UInt64>(ntohl(static_cast<UInt32>(x)))) << 32) + ntohl(x >> 32))





// Changes endianness
inline UInt64 HostToNetwork8(const void * a_Value)
{
	UInt64 buf;
	memcpy( &buf, a_Value, sizeof( buf));
	buf = (( ( static_cast<UInt64>(htonl(static_cast<UInt32>(buf)))) << 32) + htonl(buf >> 32));
	return buf;
}





inline UInt32 HostToNetwork4(const void * a_Value)
{
	UInt32 buf;
	memcpy( &buf, a_Value, sizeof( buf));
	buf = ntohl( buf);
	return buf;
}





inline double NetworkToHostDouble8(const void * a_Value)
{
	UInt64 buf = 0;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	double x;
	memcpy(&x, &buf, sizeof(double));
	return x;
}





inline Int64 NetworkToHostLong8(const void * a_Value)
{
	UInt64 buf;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	return *reinterpret_cast<Int64 *>(&buf);
}





inline UInt64 NetworkToHostULong8(const void * a_Value)
{
	UInt64 buf;
	memcpy(&buf, a_Value, 8);
	buf = ntohll(buf);
	return buf;
}





inline float NetworkToHostFloat4(const void * a_Value)
{
	UInt32 buf;
	float x;
	memcpy(&buf, a_Value, 4);
	buf = ntohl(buf);
	memcpy(&x, &buf, sizeof(float));
	return x;
}




