/*
* Naamari-lautta - a wrapper of console cloud clients for Naamari
* Copyright (c) 2015-2017 Sunkware
* Key FP = 6B6D C8E9 3438 6E9C 3D97  56E5 2CE9 A476 99EF 28F6
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* [ WWW: sunkware.org ]                         [ E-MAIL: sunkware@gmail.com ]
 */

package main

// perhaps it may be done with pointers or []interface{}...
// Functions dealing with int16/32/64 rely on little-endian byte order in memory

func SliceToBool(slice []byte) (value bool) {
	if slice[0] != 0 {
		value = true
	} else {
		value = false
	}

	return
}

func BoolToSlice(value bool) (slice []byte) {
	slice = make([]byte, 1)

	if value == true {
		slice[0] = 1
	} else {
		slice[0] = 0
	}

	return
}

func SliceToUIntX(slice []byte) (value uint64) {
	value = 0

	for i := 0; i < len(slice); i++ {
		value += (uint64(slice[i]) << (uint(i) << 3))
	}

	return
}

func UIntXToSlice(value uint64, numBits int) (slice []byte) {
	slice = make([]byte, numBits>>3)

	for i := 0; i < (numBits >> 3); i++ {
		slice[i] = byte((value >> (uint(i) << 3)) & 0xFF)
	}

	return
}

func SliceToUInt8(slice []byte) (value uint8) {
	value = uint8(SliceToUIntX(slice))
	return
}

func SliceToUInt16(slice []byte) (value uint16) {
	value = uint16(SliceToUIntX(slice))
	return
}

func SliceToUInt32(slice []byte) (value uint32) {
	value = uint32(SliceToUIntX(slice))
	return
}

func SliceToUInt64(slice []byte) (value uint64) {
	value = uint64(SliceToUIntX(slice))
	return
}

func UInt8ToSlice(value uint8) (slice []byte) {
	slice = UIntXToSlice(uint64(value), 8)
	return
}

func UInt16ToSlice(value uint16) (slice []byte) {
	slice = UIntXToSlice(uint64(value), 16)
	return
}

func UInt32oSlice(value uint32) (slice []byte) {
	slice = UIntXToSlice(uint64(value), 32)
	return
}

func UInt64ToSlice(value uint64) (slice []byte) {
	slice = UIntXToSlice(uint64(value), 64)
	return
}
