File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / uuid.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Mon Nov 17 20:02:48 2025 UTC (4 weeks, 2 days ago) by misho
Branches: MAIN
CVS tags: elwix6_13, HEAD, ELWIX6_12
Version 6.12

    1: /*************************************************************************
    2: * (C) 2025 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: uuid.c,v 1.2 2025/11/17 20:02:48 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2025
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * e_random() - ELWIX Random routine
   51:  *
   52:  * return: random number
   53:  */
   54: long
   55: e_random()
   56: {
   57: 	int fd;
   58: 	long ret = -1L;
   59: 
   60: 	fd = open("/dev/urandom", O_RDONLY);
   61: 	if (fd == -1) {
   62: 		srandom(time(NULL) ^ getpid());
   63: 		return random();
   64: 	}
   65: 	if (read(fd, &ret, sizeof ret) == -1) {
   66: 		srandom(time(NULL) ^ getpid());
   67: 		ret = random();
   68: 	}
   69: 	close(fd);
   70: 
   71: 	return ret;
   72: }
   73: 
   74: /*
   75:  * e_randbuf() - Fill buffer with random numbers
   76:  *
   77:  * @rnd = Buffer
   78:  * @rndlen = Length of buffer
   79:  *
   80:  * return: -1 error or 0 ok
   81:  */
   82: int
   83: e_randbuf(u_char *rnd, int rndlen)
   84: {
   85: 	int fd, ret = 0;
   86: 
   87: 	if (!rnd || rndlen < 1)
   88: 		return -1;
   89: 
   90: 	fd = open("/dev/urandom", O_RDONLY);
   91: 	if (fd == -1) {
   92: 		elwix_SetErr(errno, "%s", strerror(errno));
   93: 		return -1;
   94: 	}
   95: 	if (read(fd, rnd, rndlen) == -1) {
   96: 		elwix_SetErr(errno, "%s", strerror(errno));
   97: 		ret = -1;
   98: 	}
   99: 	close(fd);
  100: 
  101: 	return ret;
  102: }
  103: 
  104: /*
  105:  * uuid_gen() - Generate UUID
  106:  *
  107:  * @uuid = UUID
  108:  *
  109:  * return: -1 error or 0 ok
  110:  */
  111: int
  112: uuid_gen(uuid_t uuid)
  113: {
  114: 	uuid_t id;
  115: 
  116: 	if (e_randbuf(id, sizeof id) == -1)
  117: 		return -1;
  118: 
  119: 	/* UUID v4 */
  120: 	id[6] = (id[6] & 0xf) | 0x40;
  121: 	/* RFC4122 */
  122: 	id[8] = (id[8] & 0x3f) | 0x80;
  123: 
  124: 	if (uuid)
  125: 		memcpy(uuid, id, sizeof(uuid_t));
  126: 	return 0;
  127: }
  128: 
  129: /*
  130:  * uuid_str() - Convert UUID to string
  131:  *
  132:  * @uuid = UUID
  133:  * @str = String
  134:  * @len = Length
  135:  *
  136:  * return: -1 error or processed bytes
  137:  */
  138: int
  139: uuid_str(uuid_t uuid, char *str, int len)
  140: {
  141: 	if (!uuid || !str || len < 37)
  142: 		return -1;
  143: 
  144: 	return snprintf(str, len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  145: 			uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
  146: 			uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
  147: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>