1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_BYTESTRING_H_
3 #define CCAN_BYTESTRING_H_
9 #include <ccan/array_size/array_size.h>
17 * bytestring - construct a new bytestring
18 * @p: pointer to the content of the bytestring
19 * @l: length of the bytestring
21 * Builds a new bytestring starting at p, of length l.
24 * char x[5] = "abcde";
25 * struct bytestring bs = bytestring(x, 5);
26 * assert(bs.len == 5);
28 static inline struct bytestring bytestring(const char *p, size_t l)
30 struct bytestring bs = {
38 #define bytestring_NULL bytestring(NULL, 0)
41 * BYTESTRING - construct a bytestring from a string literal
44 * Builds a new bytestring containing the given literal string, not
45 * including the terminating \0 (but including any internal \0s).
48 * assert(BYTESTRING("abc\0def").len == 7);
50 #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1))
54 * bytestring_from_string - construct a bytestring from a NUL terminated string
55 * @s: NUL-terminated string pointer
57 * Builds a new bytestring containing the given NUL-terminated string,
58 * up to, but not including, the terminating \0.
61 * assert(bytestring_from_string("abc\0def").len == 3);
63 static inline struct bytestring bytestring_from_string(const char *s)
66 return bytestring_NULL;
67 return bytestring(s, strlen(s));
71 * bytestring_eq - test if bytestrings have identical content
74 * Returns 1 if the given bytestrings have identical length and
75 * content, 0 otherwise.
77 static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
79 return (a.len == b.len)
80 && (memcmp(a.ptr, b.ptr, a.len) == 0);
83 #endif /* CCAN_BYTESTRING_H_ */