X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fbytestring%2Fbytestring.h;fp=ccan%2Fbytestring%2Fbytestring.h;h=ee4087d44cb4c9dc864e632973e917cafb768ea9;hp=0000000000000000000000000000000000000000;hb=8253b9dd5d279793e267ed5dfcc4c0f2cfbaeb83;hpb=0a8d854a29499156e99b8695208b1fde8d45c881 diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h new file mode 100644 index 00000000..ee4087d4 --- /dev/null +++ b/ccan/bytestring/bytestring.h @@ -0,0 +1,81 @@ +/* Licensed under LGPLv2+ - see LICENSE file for details */ +#ifndef CCAN_BYTESTRING_H_ +#define CCAN_BYTESTRING_H_ + +#include +#include +#include + +#include + +struct bytestring { + const char *ptr; + size_t len; +}; + +/** + * bytestring - construct a new bytestring + * @p: pointer to the content of the bytestring + * @l: length of the bytestring + * + * Builds a new bytestring starting at p, of length l. + * + * Example: + * char x[5] = "abcde"; + * struct bytestring bs = bytestring(x, 5); + * assert(bs.len == 5); + */ +static inline struct bytestring bytestring(const char *p, size_t l) +{ + struct bytestring bs = { + .ptr = p, + .len = l, + }; + + return bs; +} + +#define bytestring_NULL bytestring(NULL, 0) + +/** + * BYTESTRING - construct a bytestring from a string literal + * @s: string literal + * + * Builds a new bytestring containing the given literal string, not + * including the terminating \0 (but including any internal \0s). + * + * Example: + * assert(BYTESTRING("abc\0def").len == 7); + */ +#define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1)) + + +/** + * bytestring_from_string - construct a bytestring from a NUL terminated string + * @s: NUL-terminated string pointer + * + * Builds a new bytestring containing the given NUL-terminated string, + * up to, but not including, the terminating \0. + * + * Example: + * assert(bytestring_from_string("abc\0def").len == 3); + */ +static inline struct bytestring bytestring_from_string(const char *s) +{ + return bytestring(s, strlen(s)); +} + +/** + * bytestring_eq - test if bytestrings have identical content + * @a, @b: bytestrings + * + * Returns 1 if the given bytestrings have identical length and + * content, 0 otherwise. + */ +static inline bool bytestring_eq(struct bytestring a, struct bytestring b) +{ + return (a.len == b.len) + && (memcmp(a.ptr, b.ptr, a.len) == 0); +} + +#endif /* CCAN_BYTESTRING_H_ */