comparison pws.c @ 0:d541e748cfd8

Initial revision
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Tue, 10 Feb 2015 11:29:54 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d541e748cfd8
1 /*
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "compat.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "pws-internal.h"
30
31 static void default_free(void *, size_t);
32
33 void * (*pws_alloc)(size_t) = malloc;
34 void * (*pws_realloc)(void *, size_t) = realloc;
35 void (*pws_free)(void *, size_t) = default_free;
36 void * (*pws_secure_alloc)(size_t) = malloc;
37 void * (*pws_secure_realloc)(void *, size_t) = realloc;
38 void (*pws_secure_free)(void *, size_t) = default_free;
39
40 int
41 pws_init(void)
42 {
43 return (0);
44 }
45
46 void
47 pws_finalize(void)
48 {
49 }
50
51 static void
52 default_free(void *ptr, size_t n)
53 {
54 free(ptr);
55 }
56
57 void
58 pws_set_alloc_functions(void *(*alloc_function)(size_t),
59 void *(*realloc_function)(void *, size_t),
60 void (*free_function)(void *, size_t),
61 void *(*secure_alloc_function)(size_t),
62 void *(*secure_realloc_function)(void *, size_t),
63 void (*secure_free_function)(void *, size_t))
64 {
65 pws_alloc = (alloc_function != NULL) ? alloc_function : malloc;
66 pws_realloc = (realloc_function != NULL) ? realloc_function : realloc;
67 pws_free = (free_function != NULL) ? free_function : default_free;
68 pws_secure_alloc = (secure_alloc_function != NULL) ?
69 secure_alloc_function : malloc;
70 pws_secure_realloc = (secure_realloc_function != NULL) ?
71 secure_realloc_function : realloc;
72 pws_secure_free = (secure_free_function != NULL) ?
73 secure_free_function : default_free;
74 }
75
76 int
77 pws_generate_uuid(unsigned char uuid[static PWS3_UUID_SIZE])
78 {
79 unsigned char buf[PWS3_UUID_SIZE];
80
81 if (pws_random_bytes(buf, sizeof (buf)) != 0) {
82 return (-1);
83 }
84
85 /* UUID v4 from RFC 4122, section 4.4 */
86 buf[6] = (buf[6] & 0x0f) | 0x40;
87 buf[8] = (buf[8] & 0x3f) | 0x80;
88 memcpy(uuid, buf, sizeof (buf));
89
90 return (0);
91 }