/*
 * SecBus - This file is part of SecBus
 * Copyright (C) 2012 - Telecom ParisTech
 * 
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at    
 * http://www.cecill.info/licences/Licence_CeCILL_V1.1-US.txt
 *
 */

#ifndef SECBUSLIB__H_
#define SECBUSLIB__H_

#include "global.h"
#include "types.h"
//DES function
void DES(int *in, int *out, int *key);
//test if a 8-byte block is complete
bool fill_64(int be, addr_t address);
//Count number of zero in "n"
template<typename T>
unsigned int _ctz(T n, unsigned int p);
//Inclusion function
bool inclusion(addr_t address, addr_t low_address, addr_t high_address);
//Inferior function
bool inferior(addr_t address, addr_t up_address);
//Superior function
bool superior(addr_t address, addr_t low_address);
//Return a block_size_in_bytes aligned address of "address"
addr_t aligned(addr_t address, int block_size_in_bytes);
//Return index of "address" in a "block_size_in_bytes" block
unsigned int index(addr_t address, int block_size_in_bytes);
//Get the 4K page address containing "address"
addr_t get4kPageAddress(addr_t address);
//Get the 4M page address containing "address"
addr_t get4mPageAddress(addr_t address);
//Get the first level PSPE address
addr_t getFirstLevelAddress(addr_t base_address, addr_t address);
//Get the second level PSPE address
addr_t getSecondLevelAddress(addr_t base_address, addr_t address);
//Get SP address from "SP_index"
addr_t getSPAddress(int SP_index);
//Get Validity bit from "PSPE"
bool getValidBit(int* PSPE);
//Set Validity bit in "PSPE"
void setValidBit(int* PSPE, bool V);
//Get Type bit from "PSPE"
PSPEtype_e getTypeBit(int* PSPE);
//Set Type bit in "PSPE"
void setTypeBit(int* PSPE, PSPEtype_e T);
//Get Size bit from "PSPE"
PageSize_e getSizeBit(int* PSPE);
//Set Size bit in "PSPE"
void setSizeBit(int* PSPE, PageSize_e PS);
//get SP index from "PSPE"
unsigned int getSPindex(int* PSPE);
//Set SP index in "PSPE"
void setSPindex(int* PSPE, int SP_index);
//Get Root Hash Value from "PSPE"
void getRHV(int* PSPE, int * RHV);
//Set Root Hash Value in "PSPE"
void setRHV(int* PSPE, int *RHV);
//Get base address from "PSPE"
addr_t getBaseAddress(int* PSPE);
//Set base address in "PSPE"
void setBaseAddress(int* PSPE, addr_t base_address);
//Get Confidentiality mode from "SP"
confidentiality_mode_e getPM(int* SP);
//Get Integrity mode from "SP"
integrity_mode_e getIM(int* SP);
//Set Confidentiality mode in "SP"
void setPM(int* SP, confidentiality_mode_e PM);
//Set Integrity mode in "SP"
void setIM(int* SP, integrity_mode_e IM);
//Get key from "SP"
void getSK(int* SP, int* Key);
//Set key in "SP"
void setSK(int* SP, int *PSPE, int * Key);

//Return base address of page containing address
addr_t getPageBaseAddress(addr_t address, PageSize_e page_size);

//Return parent address from a node address
addr_t getParentAddress(addr_t node_address, PageSize_e ht_page_size);

//Return parent address from a leaf address
addr_t getParentLeavesAddress(addr_t leaf_address, addr_t  ht_start_address, PageSize_e ht_page_size);

//Return address of the hash set containing  node address
addr_t getSetAddress(addr_t node_address, PageSize_e ht_page_size);

//Return the index of a hash block in a hash set
unsigned int getBlockIndex(addr_t address, PageSize_e ht_page_size);

//Test if address belongs to last level of tree
bool isLastLevel(addr_t address, PageSize_e ht_page_size);

//Get address of parent MAC block
addr_t getMacAddress(addr_t address, addr_t page_address, PageSize_e page_size);

//Test if address belongs to Master tree
bool isMasterTree(addr_t address);

//Get brother address of a node
addr_t getBrotherAddress(addr_t node_address, PageSize_e ht_page_size);

//Compare computed value and reference value
bool compare(int *value, int *reference, int size_in_bytes);

//Test if address belongs to Master Block 
bool isMasterBlock(addr_t address);

#endif

