comparison pws-internal.h @ 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 #ifndef PWS_INTERNAL_H
25 #define PWS_INTERNAL_H
26
27 #include <signal.h>
28 #ifdef HAVE_SYS_TREE_H
29 #include <sys/tree.h>
30 #endif /* HAVE_SYS_TREE_H */
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include "include/pws.h"
35
36 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
37 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
38 #define CLAMP(x, min, max) (((x) > (max)) ? (max) : (((x) < (min)) ?\
39 (min) : (x)))
40
41 #ifdef NDEBUG
42 #include <signal.h>
43 #include <unistd.h>
44 #define PWS_ASSERT(cond) while (cond) {\
45 write(STDERR_FIELNO, "assertion failed: " #cond "\n", \
46 sizeof ("assertion failed: " #cond "\n"));\
47 raise(SIGKILL);\
48 }
49 #else
50 #include <assert.h>
51 #define PWS_ASSERT(cond) assert(cond)
52 #endif /* NDEBUG */
53
54 struct pws3_field {
55 int is_header;
56 uint8_t field_type;
57 size_t size;
58 union {
59 unsigned char *bytes;
60 unsigned char uuid[PWS3_UUID_SIZE];
61 char *text;
62 uint8_t uint8;
63 uint16_t uint16;
64 uint32_t uint32;
65 } value;
66 RB_ENTRY(pws3_field) tree_entry;
67 };
68
69 struct pws3_record {
70 struct pws3_field *fields[256];
71 RB_ENTRY(pws3_record) tree_entry;
72 };
73
74 extern void * (*pws_alloc)(size_t);
75 extern void * (*pws_realloc)(void *, size_t);
76 extern void (*pws_free)(void *, size_t);
77 extern void * (*pws_secure_alloc)(size_t);
78 extern void * (*pws_secure_realloc)(void *, size_t);
79 extern void (*pws_secure_free)(void *, size_t);
80
81 int pws_random_bytes(void *, size_t);
82
83 #endif /* PWS_INTERNAL_H */