// Bastard File System
// (C) 2007 Rene Schickbauer
//
// The Bastard File System (BFS) is a small virtual FS for Applications
// that would normally use Tarballs and/or Zipfiles but have to dynamically
// change its contents
//
// WARNING: This is still ALPHA QUALITY CODE! Use with care!
//
//
#include "bfs_convert.h"
#include <string.h>

void convertTo2Char(int num, char* data) {
 	unsigned char tmp[2];

	tmp[0] = (char)(num & 0xff);
	tmp[1] = (char)((num >> 8) & 0xff);
	
	memcpy(data, tmp, 2);
}   
        
int convertFrom2Char(char* data) {
 	unsigned char tmp[2];
	unsigned int num = 0;

	memcpy(tmp, data, 2);

	num = (tmp[0] & 0xff) +
		  ((tmp[1] & 0xff) << 8);
          
    return num;       
}

void convertTo4Char(int num, char* data) {
	unsigned char tmp[4];

	tmp[0] = (char)(num & 0xff);
	tmp[1] = (char)((num >> 8) & 0xff);
	tmp[2] = (char)((num >> 16) & 0xff);
	tmp[3] = (char)((num >> 24) & 0xff);
	
	memcpy(data, tmp, 4);
}
    
int convertFrom4Char(char* data) {
 	unsigned char tmp[4];
	unsigned int num = 0;

	memcpy(tmp, data, 4);

	num = (tmp[0] & 0xff) +
		  ((tmp[1] & 0xff) << 8) +	
		  ((tmp[2] & 0xff) << 16) +	
		  ((tmp[3] & 0xff) << 24);
          
    return num;   
    
}
