9880ae20f875f4f0d4bdfc0056d85ec1496d1a8a
4 * @brief Pointer/length abstraction and its functions.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
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>.
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
32 typedef struct chunk_t chunk_t
;
35 * General purpose pointer/length abstraction.
38 /** Pointer to start of data */
40 /** Length of data in bytes */
45 * A { NULL, 0 }-chunk handy for initialization.
47 extern chunk_t chunk_empty
;
50 * Create a new chunk pointing to "ptr" with length "len"
52 chunk_t
chunk_create(u_char
*ptr
, size_t len
);
55 * Create a clone of a chunk pointing to "ptr"
57 chunk_t
chunk_create_clone(u_char
*ptr
, chunk_t chunk
);
60 * Calculate length of multiple chunks
62 size_t chunk_length(const char *mode
, ...);
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 "..."
69 chunk_t
chunk_create_cat(u_char
*ptr
, const char* mode
, ...);
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);
79 void chunk_split(chunk_t chunk
, const char *mode
, ...);
82 * Free contents of a chunk
84 void chunk_free(chunk_t
*chunk
);
87 * Initialize a chunk to point to buffer inspectable by sizeof()
89 #define chunk_from_buf(str) { str, sizeof(str) }
92 * Allocate a chunk on the heap
94 #define chunk_alloc(bytes) chunk_create(malloc(bytes), bytes)
97 * Allocate a chunk on the stack
99 #define chunk_alloca(bytes) chunk_create(alloca(bytes), bytes)
102 * Clone a chunk on heap
104 #define chunk_clone(chunk) chunk_create_clone(malloc(chunk.len), chunk)
107 * Clone a chunk on stack
109 #define chunk_clonea(chunk) chunk_create_clone(alloca(chunk.len), chunk)
112 * Concatenate chunks into a chunk on heap
114 #define chunk_cat(mode, ...) chunk_create_cat(malloc(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
117 * Concatenate chunks into a chunk on stack
119 #define chunk_cata(mode, ...) chunk_create_cat(alloca(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
122 * Skip n bytes in chunk (forward pointer, shorten length)
124 chunk_t
chunk_skip(chunk_t chunk
, size_t bytes
);
127 * Compare two chunks, returns zero if a equals b
128 * or negative/positive if a is small/greater than b
130 int chunk_compare(chunk_t a
, chunk_t b
);
133 * Compare two chunks for equality,
134 * NULL chunks are never equal.
136 bool chunk_equals(chunk_t a
, chunk_t b
);
139 * Compare two chunks for equality,
140 * NULL chunks are always equal.
142 bool chunk_equals_or_null(chunk_t a
, chunk_t b
);
144 #endif /* CHUNK_H_ */