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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_MEM_H
#define CCAN_MEM_H
#include "config.h"
#include <ccan/compiler/compiler.h>
#include <string.h>
#include <stdbool.h>
#if !HAVE_MEMMEM
PURE_FUNCTION
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen);
#endif
#if !HAVE_MEMRCHR
PURE_FUNCTION
void *memrchr(const void *s, int c, size_t n);
#endif
/**
* mempbrkm - locates the first occurrence in @data of any bytes in @accept
* @data: where we search
* @len: length of data in bytes
* @accept: array of bytes we search for
* @accept_len: # of bytes in accept
*
* Returns a pointer to the byte in @data that matches one of the bytes in
* @accept, or NULL if no such byte is found.
*
* Example:
* char otherbytes[] = "Hello \0world";
* size_t otherbytes_len = sizeof(otherbytes) - 1;
* char *r = mempbrkm(otherbytes, otherbytes_len, "\0b", 2);
* if (r) {
* printf("Found %c\n", *r);
* } else {
* printf("Nada\n");
* }
*
*/
PURE_FUNCTION
void *mempbrkm(const void *data, size_t len, const void *accept, size_t accept_len);
/**
* mempbrk - locates the first occurrence in @data of any bytes in @accept
* @data: where we search
* @len: length of data in bytes
* @accept: NUL terminated string containing the bytes we search for
*
* Returns a pointer to the byte in @data that matches one of the bytes in
* @accept, or NULL if no such byte is found.
*
* Example:
*
* r = mempbrk(otherbytes, otherbytes_len, "abcde");
* if (r) {
* printf("Found %c\n", *r);
* } else {
* printf("Nada\n");
* }
*/
PURE_FUNCTION
static inline char *mempbrk(const void *data, size_t len, const char *accept)
{
return mempbrkm(data, len, accept, strlen(accept));
}
/**
* memcchr - scan memory until a character does _not_ match @c
* @data: pointer to memory to scan
* @data_len: length of data
* @c: character to scan for
*
* The complement of memchr().
*
* Returns a pointer to the first character which is _not_ @c. If all memory in
* @data is @c, returns NULL.
*
* Example:
* char somebytes[] = "HI By\0e";
* size_t bytes_len = sizeof(somebytes) - 1;
* r = memcchr(somebytes, ' ', bytes_len);
* if (r) {
* printf("Found %c after trimming spaces\n", *r);
* }
*/
PURE_FUNCTION
void *memcchr(void const *data, int c, size_t data_len);
/**
* memeq - Are two byte arrays equal?
* @a: first array
* @al: bytes in first array
* @b: second array
* @bl: bytes in second array
*
* Example:
* if (memeq(somebytes, bytes_len, otherbytes, otherbytes_len)) {
* printf("memory blocks are the same!\n");
* }
*/
PURE_FUNCTION
static inline bool memeq(const void *a, size_t al, const void *b, size_t bl)
{
return al == bl && !memcmp(a, b, bl);
}
/**
* memstarts - determine if @data starts with @prefix
* @data: does this begin with @prefix?
* @data_len: bytes in @data
* @prefix: does @data begin with these bytes?
* @prefix_len: bytes in @prefix
*
* Returns true if @data starts with @prefix, otherwise return false.
*
* Example:
* if (memstarts(somebytes, bytes_len, otherbytes, otherbytes_len)) {
* printf("somebytes starts with otherbytes!\n");
* }
*/
PURE_FUNCTION
static inline bool memstarts(void const *data, size_t data_len,
void const *prefix, size_t prefix_len)
{
if (prefix_len > data_len)
return false;
return memeq(data, prefix_len, prefix, prefix_len);
}
/**
* memeqstr - Is a byte array equal to a NUL terminated string?
* @data: byte array
* @length: length of @data in bytes
* @string: NUL terminated string
*
* The '\0' byte is ignored when checking if @bytes == @string.
*
* Example:
* if (memeqstr(somebytes, bytes_len, "foo")) {
* printf("somebytes == 'foo'!\n");
* }
*/
PURE_FUNCTION
static inline bool memeqstr(const void *data, size_t length, const char *string)
{
return memeq(data, length, string, strlen(string));
}
/**
* memstarts_str - Does this byte array start with a string prefix?
* @a: byte array
* @al: length in bytes
* @s: string prefix
*
* Example:
* if (memstarts_str(somebytes, bytes_len, "It")) {
* printf("somebytes starts with 'It'\n");
* }
*/
PURE_FUNCTION
static inline bool memstarts_str(const void *a, size_t al, const char *s)
{
return memstarts(a, al, s, strlen(s));
}
/**
* memends - Does this byte array end with a given byte-array suffix?
* @s: byte array
* @s_len: length in bytes
* @suffix: byte array suffix
* @suffix_len: length of suffix in bytes
*
* Returns true if @suffix appears as a substring at the end of @s,
* false otherwise.
*/
PURE_FUNCTION
static inline bool memends(const void *s, size_t s_len, const void *suffix, size_t suffix_len)
{
return (s_len >= suffix_len) && (memcmp((const char *)s + s_len - suffix_len,
suffix, suffix_len) == 0);
}
/**
* memends_str - Does this byte array end with a string suffix?
* @a: byte array
* @al: length in bytes
* @s: string suffix
*
* Example:
* if (memends_str(somebytes, bytes_len, "It")) {
* printf("somebytes ends with with 'It'\n");
* }
*/
PURE_FUNCTION
static inline bool memends_str(const void *a, size_t al, const char *s)
{
return memends(a, al, s, strlen(s));
}
/**
* memoverlaps - Do two memory ranges overlap?
* @a: pointer to first memory range
* @al: length of first memory range
* @b: pointer to second memory range
* @al: length of second memory range
*/
CONST_FUNCTION
static inline bool memoverlaps(const void *a_, size_t al,
const void *b_, size_t bl)
{
const char *a = a_;
const char *b = b_;
return (a < (b + bl)) && (b < (a + al));
}
/*
* memswap - Exchange two memory regions
* @a: first region
* @b: second region
* @n: length of the regions
*
* Undefined results if the two memory regions overlap.
*/
void memswap(void *a, void *b, size_t n);
#endif /* CCAN_MEM_H */
|