Mercurial > projects > libpws
diff pws.c @ 0:d541e748cfd8
Initial revision
author | Guido Berhoerster <guido+libpws@berhoerster.name> |
---|---|
date | Tue, 10 Feb 2015 11:29:54 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pws.c Tue Feb 10 11:29:54 2015 +0100 @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name> + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "compat.h" + +#include <stdlib.h> +#include <string.h> + +#include "pws-internal.h" + +static void default_free(void *, size_t); + +void * (*pws_alloc)(size_t) = malloc; +void * (*pws_realloc)(void *, size_t) = realloc; +void (*pws_free)(void *, size_t) = default_free; +void * (*pws_secure_alloc)(size_t) = malloc; +void * (*pws_secure_realloc)(void *, size_t) = realloc; +void (*pws_secure_free)(void *, size_t) = default_free; + +int +pws_init(void) +{ + return (0); +} + +void +pws_finalize(void) +{ +} + +static void +default_free(void *ptr, size_t n) +{ + free(ptr); +} + +void +pws_set_alloc_functions(void *(*alloc_function)(size_t), + void *(*realloc_function)(void *, size_t), + void (*free_function)(void *, size_t), + void *(*secure_alloc_function)(size_t), + void *(*secure_realloc_function)(void *, size_t), + void (*secure_free_function)(void *, size_t)) +{ + pws_alloc = (alloc_function != NULL) ? alloc_function : malloc; + pws_realloc = (realloc_function != NULL) ? realloc_function : realloc; + pws_free = (free_function != NULL) ? free_function : default_free; + pws_secure_alloc = (secure_alloc_function != NULL) ? + secure_alloc_function : malloc; + pws_secure_realloc = (secure_realloc_function != NULL) ? + secure_realloc_function : realloc; + pws_secure_free = (secure_free_function != NULL) ? + secure_free_function : default_free; +} + +int +pws_generate_uuid(unsigned char uuid[static PWS3_UUID_SIZE]) +{ + unsigned char buf[PWS3_UUID_SIZE]; + + if (pws_random_bytes(buf, sizeof (buf)) != 0) { + return (-1); + } + + /* UUID v4 from RFC 4122, section 4.4 */ + buf[6] = (buf[6] & 0x0f) | 0x40; + buf[8] = (buf[8] & 0x3f) | 0x80; + memcpy(uuid, buf, sizeof (buf)); + + return (0); +}