"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/libstrophe/uuid.c" (23 Feb 2021, 1822 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "uuid.c" see the Fossies "Dox" file reference documentation.

    1 /* uuid.c
    2  * strophe XMPP client library -- UUID generation
    3  *
    4  * Copyright (C) 2015 Dmitry Podgorny <pasis.ua@gmail.com>
    5  *
    6  *  This software is provided AS-IS with no warranty, either express
    7  *  or implied.
    8  *
    9  *  This program is dual licensed under the MIT and GPLv3 licenses.
   10  */
   11 
   12 /** @file
   13  *  Generation of UUID version 4 according to RFC4122.
   14  */
   15 
   16 #include <nms_common.h>
   17 #include <strophe.h>
   18 
   19 #include "common.h"
   20 #include "rand.h"
   21 
   22 /** @def XMPP_UUID_LEN
   23  *  UUID length in string representation excluding '\0'.
   24  */
   25 #define XMPP_UUID_LEN 36
   26 
   27 /** Generate UUID version 4 in pre-allocated buffer.
   28  *
   29  *  @param ctx a Strophe context object
   30  *  @param uuid pre-allocated buffer of size (XMPP_UUID_LEN + 1)
   31  */
   32 static void crypto_uuid_gen(xmpp_ctx_t *ctx, char *uuid)
   33 {
   34     unsigned char buf[16];
   35     int i = 0; /* uuid iterator */
   36     int j = 0; /* buf iterator */
   37 
   38     static const char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
   39                                '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
   40 
   41     xmpp_rand_bytes(ctx->rand, buf, sizeof(buf));
   42     buf[8] &= 0x3f;
   43     buf[8] |= 0x80;
   44     buf[6] &= 0x0f;
   45     buf[6] |= 0x40;
   46     while (i < XMPP_UUID_LEN) {
   47         if (i == 8 || i == 13 || i == 18 || i == 23)
   48             uuid[i++] = '-';
   49         else {
   50             uuid[i++] = hex[buf[j] >> 4];
   51             uuid[i++] = hex[buf[j] & 0x0f];
   52             ++j;
   53         }
   54     }
   55     uuid[XMPP_UUID_LEN] = '\0';
   56 }
   57 
   58 /** Generate UUID version 4.
   59  *  This function allocates memory for the resulting string and must be freed
   60  *  with xmpp_free().
   61  *
   62  *  @param ctx a Strophe context object
   63  *
   64  *  @return ASCIIZ string
   65  */
   66 char *xmpp_uuid_gen(xmpp_ctx_t *ctx)
   67 {
   68     char *uuid;
   69 
   70     uuid = xmpp_alloc(ctx, XMPP_UUID_LEN + 1);
   71     if (uuid != NULL) {
   72         crypto_uuid_gen(ctx, uuid);
   73     }
   74     return uuid;
   75 }