/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "common/binary_utils.hpp"

static bool is_little_endian(){
	unsigned x=0x76543210;
	char* c=(char*)&x;
	return *c==0x10;
}

uint16_t polygun::utils::bytes_to_uint16(const char* data){
	uint16_t output;
	if(is_little_endian()){
		((char*)&output)[0]=data[0];
		((char*)&output)[1]=data[1];
	}
	else{
		((char*)&output)[1]=data[0];
		((char*)&output)[0]=data[1];
	}
	return output;
}

void polygun::utils::uint16_to_bytes(uint16_t data,char* output){
	if(is_little_endian()){
		output[0]=((char*)&data)[0];
		output[1]=((char*)&data)[1];
	}
	else{
		output[1]=((char*)&data)[0];
		output[0]=((char*)&data)[1];
	}
}

uint32_t polygun::utils::bytes_to_uint32(const char* data){
	uint32_t output;
	if(is_little_endian()){
		((char*)&output)[0]=data[0];
		((char*)&output)[1]=data[1];
		((char*)&output)[2]=data[2];
		((char*)&output)[3]=data[3];
	}
	else{
		((char*)&output)[3]=data[0];
		((char*)&output)[2]=data[1];
		((char*)&output)[1]=data[2];
		((char*)&output)[0]=data[3];
	}
	return output;
}

void polygun::utils::uint32_to_bytes(uint32_t data,char* output){
	if(is_little_endian()){
		output[0]=((char*)&data)[0];
		output[1]=((char*)&data)[1];
		output[2]=((char*)&data)[2];
		output[3]=((char*)&data)[3];
	}
	else{
		output[3]=((char*)&data)[0];
		output[2]=((char*)&data)[1];
		output[1]=((char*)&data)[2];
		output[0]=((char*)&data)[3];
	}
}

float polygun::utils::bytes_to_float(const char* data) {
	float output;
	if(is_little_endian()){
		((char*)&output)[0]=data[0];
		((char*)&output)[1]=data[1];
		((char*)&output)[2]=data[2];
		((char*)&output)[3]=data[3];
	}
	else{
		((char*)&output)[3]=data[0];
		((char*)&output)[2]=data[1];
		((char*)&output)[1]=data[2];
		((char*)&output)[0]=data[3];
	}
	return output;
}

void polygun::utils::float_to_bytes(float data, char* output) {
	if(is_little_endian()){
		output[0]=((char*)&data)[0];
		output[1]=((char*)&data)[1];
		output[2]=((char*)&data)[2];
		output[3]=((char*)&data)[3];
	}
	else{
		output[3]=((char*)&data)[0];
		output[2]=((char*)&data)[1];
		output[1]=((char*)&data)[2];
		output[0]=((char*)&data)[3];
	}
}
