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

#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++) = nibble2HexChar(inbuf[count]);
				if(!(--buffer_size))
					break;
				*(buf++) = nibble2HexChar(inbuf[count] >> 4);
				if(!(--buffer_size))
					break;
			}
		} while(buffer_size);
		close(fd);
	}
	else
	{
		u32_t mask;
		static u32_t salt = 1;
		ran_state_t rs;
		pid_t pid = getpid();
		ranInitState(&rs,++salt);
		mask = (pid + (pid >> 4) + (pid >> 8) + (pid >> 16)) & 0xF;
		for(;buffer_size;buffer_size--,buf++)
			*buf = nibble2HexChar((u8_t)(ranGenerateNext(&rs) ^ mask));
	}
}
