Commit f5b8155e authored by Philipp Schafft's avatar Philipp Schafft 🦁
Browse files

Merge branch 'feature-uuid' into devel-phschafft

parents 8bddb6d3 051292dc
Pipeline #2759 passed with stages
in 4 minutes and 1 second
......@@ -16,6 +16,7 @@ libigloo_la_SOURCES = \
src/sp.c \
src/ro.c \
src/tap.c \
src/uuid.c \
src/private.h
AM_CPPFLAGS = -I$(top_srcdir)/include
......@@ -39,7 +40,8 @@ IGLOO_BUILT_TESTS = \
tests/error \
tests/prng \
tests/cs \
tests/time
tests/time \
tests/uuid
# List all tests, if a test does not need to be compile
# (i.e. a script) add it here but not in IGLOO_BUILT_TESTS
......@@ -67,7 +69,8 @@ pkginclude_HEADERS = \
include/igloo/prng.h \
include/igloo/cs.h \
include/igloo/sp.h \
include/igloo/tap.h
include/igloo/tap.h \
include/igloo/uuid.h
# pkg-config .pc file
......
......@@ -10,6 +10,7 @@ libigloo 0.9.1 (2022-??-??)
* Portable time API: simple API for most common time related functions
* Digest and HMAC: support for digest and HMAC calculation (limited to SHA3)
* PRNG: support for pseudo random number generation
* UUID: Added code for random (v4) UUID generation
libigloo 0.9.0 (2020-12-14)
* Initial release.
/* Copyright (C) 2022 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _LIBIGLOO__UUID_H_
#define _LIBIGLOO__UUID_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <igloo/types.h>
/* Creates a new random UUID (v4).
*
* This function generates a new random UUID providing a string pool reference.
* This is the preferred way of working with string based UUIDs as it allows the reference to travel.
* The reference must be freed with igloo_sp_unref().
*
* Parameters:
* ref
* instance
* Reference and instance as used by igloo_sp_replace().
*
* See also:
* * igloo_sp_replace().
*/
igloo_error_t igloo_uuid_new_random_sp(const char **ref, igloo_ro_t instance) igloo_ATTR_F_WARN_UNUSED_RESULT;
/* Creates a new random UUID (v4).
*
* This function generates a new random UUID providing a bare C string.
* The string must later be freed with free(3).
*
* Parameters:
* ref
* A pointer to a C string. The C string MUST be NULL when this function is called. It will be updated
* to a newly created C string containing the UUID. It must be freed with free(3).
* instance
* The instance to use for the generation process.
* The instance may be un-referenced before the provided C string is free(3)ed.
*
* See also:
* * igloo_uuid_new_random_sp().
*/
igloo_error_t igloo_uuid_new_random_cstr(char **str, igloo_ro_t instance) igloo_ATTR_F_WARN_UNUSED_RESULT;
#ifdef __cplusplus
}
#endif
#endif /* ! _LIBIGLOO__UUID_H_ */
/* Copyright (C) 2022 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <igloo/uuid.h>
#include <igloo/sp.h>
#include <igloo/error.h>
#include <igloo/prng.h>
#define UUID_LENGTH_BINARY 16
#define UUID_LENGTH_STRING 36
static igloo_error_t igloo_uuid_new_random_r(char *buffer, igloo_ro_t instance)
{
unsigned char id[UUID_LENGTH_BINARY];
if (!buffer)
return igloo_ERROR_FAULT;
if (igloo_prng_read(instance, id, sizeof(id), igloo_PRNG_FLAG_NONE) != sizeof(id))
return igloo_ERROR_IO;
id[6] = (id[6] & 0x0F) | 0x40;
id[8] = (id[8] & 0x3F) | 0x80;
// 0 1 2 3 4 5 6 7 8 9 101112131415
// 551a9547-fe63-45cf-a153-2801bac47ff8
snprintf(buffer, UUID_LENGTH_STRING+1, "%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x",
(unsigned int)id[0], (unsigned int)id[1], (unsigned int)id[2], (unsigned int)id[3],
(unsigned int)id[4], (unsigned int)id[5],
(unsigned int)id[6], (unsigned int)id[7],
(unsigned int)id[8], (unsigned int)id[9],
(unsigned int)id[10], (unsigned int)id[11], (unsigned int)id[12],
(unsigned int)id[13], (unsigned int)id[14], (unsigned int)id[15]
);
return igloo_ERROR_NONE;
}
igloo_error_t igloo_uuid_new_random_sp(const char **ref, igloo_ro_t instance)
{
char buffer[UUID_LENGTH_STRING+1];
igloo_error_t error = igloo_uuid_new_random_r(buffer, instance);
if (error != igloo_ERROR_NONE)
return error;
return igloo_sp_replace(buffer, ref, instance);
}
igloo_error_t igloo_uuid_new_random_cstr(char **str, igloo_ro_t instance)
{
char buffer[UUID_LENGTH_STRING+1];
igloo_error_t error = igloo_uuid_new_random_r(buffer, instance);
char *res;
if (!str || *str)
return igloo_ERROR_FAULT;
if (error != igloo_ERROR_NONE)
return error;
res = strdup(buffer);
if (res == NULL)
return igloo_ERROR_NOMEM;
*str = res;
return igloo_ERROR_NONE;
}
/* Copyright (C) 2022 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <igloo/tap.h>
#include <igloo/igloo.h>
#include <igloo/uuid.h>
#include <igloo/sp.h>
static igloo_ro_t g_instance;
static void check_uuid(const char *ref)
{
size_t len = strlen(ref);
igloo_tap_test("strlen(ref) == 36", len == 36);
igloo_tap_diagnostic("Ref is:");
igloo_tap_diagnostic(ref);
if (len == 36) {
igloo_tap_test("ref[8] == '-' && ref[13] == '-' && ref[18] == '-' && ref[23] == '-'", ref[8] == '-' && ref[13] == '-' && ref[18] == '-' && ref[23] == '-');
}
}
static void group_sp(void)
{
const char *ref = NULL;
igloo_tap_test_success("igloo_uuid_new_random_sp", igloo_uuid_new_random_sp(&ref, g_instance));
igloo_tap_test("ref != NULL", ref != NULL);
if (ref) {
check_uuid(ref);
igloo_tap_test_success("igloo_sp_unref", igloo_sp_unref(&ref, g_instance));
igloo_tap_test("ref == NULL", ref == NULL);
}
}
static void group_cstr(void)
{
char *ref = NULL;
igloo_tap_test_success("igloo_uuid_new_random_cstr", igloo_uuid_new_random_cstr(&ref, g_instance));
igloo_tap_test("ref != NULL", ref != NULL);
if (ref) {
check_uuid(ref);
free(ref);
}
}
int main (void)
{
igloo_tap_init();
igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
igloo_tap_test_success("igloo_initialize", igloo_initialize(&g_instance));
igloo_tap_group_run("sp", group_sp);
igloo_tap_group_run("cstr", group_cstr);
igloo_tap_test_success("unref instance", igloo_ro_unref(&g_instance));
igloo_tap_fin();
return EXIT_FAILURE; // return failure as we should never reach this point!
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment