The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / chunk.h
1 /*
2 * Copyright (C) 2005-2008 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 /**
20 * @defgroup chunk chunk
21 * @{ @ingroup libstrongswan
22 */
23
24 #ifndef CHUNK_H_
25 #define CHUNK_H_
26
27 #include <string.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30
31 typedef struct chunk_t chunk_t;
32
33 /**
34 * General purpose pointer/length abstraction.
35 */
36 struct chunk_t {
37 /** Pointer to start of data */
38 u_char *ptr;
39 /** Length of data in bytes */
40 size_t len;
41 };
42
43 #include <library.h>
44
45 /**
46 * A { NULL, 0 }-chunk handy for initialization.
47 */
48 extern chunk_t chunk_empty;
49
50 /**
51 * Create a new chunk pointing to "ptr" with length "len"
52 */
53 chunk_t chunk_create(u_char *ptr, size_t len);
54
55 /**
56 * Create a clone of a chunk pointing to "ptr"
57 */
58 chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk);
59
60 /**
61 * Calculate length of multiple chunks
62 */
63 size_t chunk_length(const char *mode, ...);
64
65 /**
66 * Concatenate chunks into a chunk pointing to "ptr",
67 * "mode" is a string of "c" (copy) and "m" (move), which says
68 * how to handle to chunks in "..."
69 */
70 chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...);
71
72 /**
73 * Split up a chunk into parts, "mode" is a string of "a" (alloc),
74 * "c" (copy) and "m" (move). Each letter say for the corresponding chunk if
75 * it should get allocated on heap, copied into existing chunk, or the chunk
76 * should point into "chunk". The length of each part is an argument before
77 * each target chunk. E.g.:
78 * chunk_split(chunk, "mcac", 3, &a, 7, &b, 5, &c, d.len, &d);
79 */
80 void chunk_split(chunk_t chunk, const char *mode, ...);
81
82 /**
83 * Write the binary contents of a chunk_t to a file
84 */
85 bool chunk_write(chunk_t chunk, const char *path, const char *label, mode_t mask, bool force);
86
87 /**
88 * convert a chunk to an allocated hex string
89 */
90 char *chunk_to_hex(chunk_t chunk, bool uppercase);
91
92 /**
93 * Free contents of a chunk
94 */
95 void chunk_free(chunk_t *chunk);
96
97 /**
98 * Overwrite the contents of a chunk with pseudo-random bytes and free them
99 */
100 void chunk_free_randomized(chunk_t *chunk);
101
102 /**
103 * Initialize a chunk to point to buffer inspectable by sizeof()
104 */
105 #define chunk_from_buf(str) { str, sizeof(str) }
106
107 /**
108 * Initialize a chunk to point to a thing
109 */
110 #define chunk_from_thing(thing) chunk_create((char*)&(thing), sizeof(thing))
111
112 /**
113 * Allocate a chunk on the heap
114 */
115 #define chunk_alloc(bytes) chunk_create(malloc(bytes), bytes)
116
117 /**
118 * Allocate a chunk on the stack
119 */
120 #define chunk_alloca(bytes) chunk_create(alloca(bytes), bytes)
121
122 /**
123 * Clone a chunk on heap
124 */
125 #define chunk_clone(chunk) chunk_create_clone(malloc(chunk.len), chunk)
126
127 /**
128 * Clone a chunk on stack
129 */
130 #define chunk_clonea(chunk) chunk_create_clone(alloca(chunk.len), chunk)
131
132 /**
133 * Concatenate chunks into a chunk on heap
134 */
135 #define chunk_cat(mode, ...) chunk_create_cat(malloc(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
136
137 /**
138 * Concatenate chunks into a chunk on stack
139 */
140 #define chunk_cata(mode, ...) chunk_create_cat(alloca(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
141
142 /**
143 * Skip n bytes in chunk (forward pointer, shorten length)
144 */
145 chunk_t chunk_skip(chunk_t chunk, size_t bytes);
146
147 /**
148 * Compare two chunks, returns zero if a equals b
149 * or negative/positive if a is small/greater than b
150 */
151 int chunk_compare(chunk_t a, chunk_t b);
152
153 /**
154 * Compare two chunks for equality,
155 * NULL chunks are never equal.
156 */
157 bool chunk_equals(chunk_t a, chunk_t b);
158
159 /**
160 * Compare two chunks for equality,
161 * NULL chunks are always equal.
162 */
163 bool chunk_equals_or_null(chunk_t a, chunk_t b);
164
165 /**
166 * Get printf hooks for a chunk.
167 *
168 * Arguments are:
169 * chunk_t *chunk
170 */
171 printf_hook_functions_t chunk_get_printf_hooks();
172
173 #endif /* CHUNK_H_ @}*/