#include <../arch/arch.h>

V_LOCAL char
hex_char(u8_t bits1_4)
{
	bits1_4 &= 0x0F;
	if(bits1_4 <= 9)
		return (char)('0' + bits1_4);
	else
		return (char)('A'+(bits1_4 - 0x0A));
}

#define	INBUF_LEN	256

void	V_CALL
archUniqueString(char *buf,vsize_t buffer_size)
{
	int count, fd;
	buf[--buffer_size] = 0;
	fd = open("/dev/random",S_IRUSR,O_RDONLY);
	if(fd != -1)
	{
		u8_t inbuf[INBUF_LEN];
		do
		{
			read(fd,inbuf,INBUF_LEN);
			for(count = 0;count < INBUF_LEN;count++)
			{
				*(buf++) = hex_char(inbuf[count]);
				if(!(--buffer_size))
					break;
				*(buf++) = hex_char(inbuf[count] >> 4);
				if(!(--buffer_size))
					break;
			}
		} while(buffer_size);
		close(fd);
	}
	else
	{
		vhandle_t rgen;
		pid_t pid = getpid();
		timeDelay(1);
		rgen = ranCreateGenerator(timeTickCount());
		pid = (pid + (pid >> 4) + (pid >> 8) + (pid >> 16)) & 0xF;
		for(;buffer_size;buffer_size--,buf++)
			*buf = hex_char((char)(ranNextLong(rgen,15) ^ pid));
		ranDestroyGenerator(rgen);
	}
}
