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