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