cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

id.c (2861B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21
     22#include "guacamole/mem.h"
     23#include "guacamole/error.h"
     24#include "id.h"
     25
     26#if defined(HAVE_LIBUUID)
     27#include <uuid/uuid.h>
     28#elif defined(HAVE_OSSP_UUID_H)
     29#include <ossp/uuid.h>
     30#else
     31#include <uuid.h>
     32#endif
     33
     34#include <stdlib.h>
     35
     36/**
     37 * The length of a UUID in bytes. All UUIDs are guaranteed to be 36 1-byte
     38 * characters long.
     39 */
     40#define GUAC_UUID_LEN 36
     41
     42char* guac_generate_id(char prefix) {
     43
     44    char* buffer;
     45    char* identifier;
     46
     47    /* Prepare object to receive generated UUID */
     48#ifdef HAVE_LIBUUID
     49    uuid_t uuid;
     50#else
     51    uuid_t* uuid;
     52    if (uuid_create(&uuid) != UUID_RC_OK) {
     53        guac_error = GUAC_STATUS_NO_MEMORY;
     54        guac_error_message = "Could not allocate memory for UUID";
     55        return NULL;
     56    }
     57#endif
     58
     59    /* Generate unique identifier */
     60#ifdef HAVE_LIBUUID
     61    uuid_generate(uuid);
     62#else
     63    if (uuid_make(uuid, UUID_MAKE_V4) != UUID_RC_OK) {
     64        uuid_destroy(uuid);
     65        guac_error = GUAC_STATUS_NO_MEMORY;
     66        guac_error_message = "UUID generation failed";
     67        return NULL;
     68    }
     69#endif
     70
     71    /* Allocate buffer for future formatted ID */
     72    buffer = guac_mem_alloc(GUAC_UUID_LEN + 2);
     73    if (buffer == NULL) {
     74#ifndef HAVE_LIBUUID
     75        uuid_destroy(uuid);
     76#endif
     77        guac_error = GUAC_STATUS_NO_MEMORY;
     78        guac_error_message = "Could not allocate memory for unique ID";
     79        return NULL;
     80    }
     81
     82    identifier = &(buffer[1]);
     83
     84    /* Convert UUID to string to produce unique identifier */
     85#ifdef HAVE_LIBUUID
     86    uuid_unparse_lower(uuid, identifier);
     87#else
     88    size_t identifier_length = GUAC_UUID_LEN + 1;
     89    if (uuid_export(uuid, UUID_FMT_STR, &identifier, &identifier_length) != UUID_RC_OK) {
     90        guac_mem_free(buffer);
     91        uuid_destroy(uuid);
     92        guac_error = GUAC_STATUS_INTERNAL_ERROR;
     93        guac_error_message = "Conversion of UUID to unique ID failed";
     94        return NULL;
     95    }
     96
     97    /* Clean up generated UUID */
     98    uuid_destroy(uuid);
     99#endif
    100
    101    buffer[0] = prefix;
    102    buffer[GUAC_UUID_LEN + 1] = '\0';
    103    return buffer;
    104
    105}
    106
    107