aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/chunk.h
blob: 93c4b512fce6a93c84f6b7eff1c610216922560e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
 * @file chunk.h
 *
 * @brief Pointer/lenght abstraction and its functions.
 *
 */

/*
 * Copyright (C) 2005-2006 Martin Willi
 * Copyright (C) 2005 Jan Hutter
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#ifndef CHUNK_H_
#define CHUNK_H_

#include <string.h>
#include <stdarg.h>

#include <library.h>

typedef struct chunk_t chunk_t;

/**
 * General purpose pointer/length abstraction.
 */
struct chunk_t {
	/** Pointer to start of data */
	u_char *ptr;
	/** Length of data in bytes */
	size_t len;
};

/**
 * A { NULL, 0 }-chunk handy for initialization.
 */
extern chunk_t chunk_empty;

/**
 * Initialize a chunk to point to a static(!) buffer
 */
#define chunk_from_buf(str) { str, sizeof(str) }

/**
 * Clone chunk contents in a newly allocated chunk
 */
chunk_t chunk_clone(chunk_t chunk);

/**
 * Allocate a chunk from concatenation of other chunks.
 * mode is a string 'm' and 'c, 'm' means move chunk,
 * 'c' means copy chunk.
 */
chunk_t chunk_cat(const char* mode, ...);

/**
 * Free contents of a chunk
 */
void chunk_free(chunk_t *chunk);

/**
 * Allocate a chunk
 */
chunk_t chunk_alloc(size_t bytes);

/**
 * Compare two chunks for equality,
 * NULL chunks are never equal.
 */
bool chunk_equals(chunk_t a, chunk_t b);

/**
 * Compare two chunks for equality,
 * NULL chunks are always equal.
 */
bool chunk_equals_or_null(chunk_t a, chunk_t b);

#endif /* CHUNK_H_ */