X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=libbcachefs%2Fvarint.c;h=cb4f33ed9ab374fbd50bbf74419335564f55df9a;hb=3798bbae98cb82e13df18ddf095488b98afe0ddd;hp=a2d6bb7136c7d412d95469ac09b2c063fd0dd86d;hpb=dafd26afd98f92d8f66346aca7b6361d15f4dd3c;p=bcachefs-tools-debian diff --git a/libbcachefs/varint.c b/libbcachefs/varint.c index a2d6bb7..cb4f33e 100644 --- a/libbcachefs/varint.c +++ b/libbcachefs/varint.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include @@ -12,21 +13,21 @@ /** * bch2_varint_encode - encode a variable length integer - * @out - destination to encode to - * @v - unsigned integer to encode - * - * Returns the size in bytes of the encoded integer - at most 9 bytes + * @out: destination to encode to + * @v: unsigned integer to encode + * Returns: size in bytes of the encoded integer - at most 9 bytes */ int bch2_varint_encode(u8 *out, u64 v) { unsigned bits = fls64(v|1); unsigned bytes = DIV_ROUND_UP(bits, 7); + __le64 v_le; if (likely(bytes < 9)) { v <<= bytes; v |= ~(~0 << (bytes - 1)); - v = cpu_to_le64(v); - memcpy(out, &v, bytes); + v_le = cpu_to_le64(v); + memcpy(out, &v_le, bytes); } else { *out++ = 255; bytes = 9; @@ -38,11 +39,10 @@ int bch2_varint_encode(u8 *out, u64 v) /** * bch2_varint_decode - encode a variable length integer - * @in - varint to decode - * @end - end of buffer to decode from - * @out - on success, decoded integer - * - * Returns the size in bytes of the decoded integer - or -1 on failure (would + * @in: varint to decode + * @end: end of buffer to decode from + * @out: on success, decoded integer + * Returns: size in bytes of the decoded integer - or -1 on failure (would * have read past the end of the buffer) */ int bch2_varint_decode(const u8 *in, const u8 *end, u64 *out) @@ -56,9 +56,10 @@ int bch2_varint_decode(const u8 *in, const u8 *end, u64 *out) return -1; if (likely(bytes < 9)) { - v = 0; - memcpy(&v, in, bytes); - v = le64_to_cpu(v); + __le64 v_le = 0; + + memcpy(&v_le, in, bytes); + v = le64_to_cpu(v_le); v >>= bytes; } else { v = get_unaligned_le64(++in); @@ -70,6 +71,9 @@ int bch2_varint_decode(const u8 *in, const u8 *end, u64 *out) /** * bch2_varint_encode_fast - fast version of bch2_varint_encode + * @out: destination to encode to + * @v: unsigned integer to encode + * Returns: size in bytes of the encoded integer - at most 9 bytes * * This version assumes it's always safe to write 8 bytes to @out, even if the * encoded integer would be smaller. @@ -93,6 +97,11 @@ int bch2_varint_encode_fast(u8 *out, u64 v) /** * bch2_varint_decode_fast - fast version of bch2_varint_decode + * @in: varint to decode + * @end: end of buffer to decode from + * @out: on success, decoded integer + * Returns: size in bytes of the decoded integer - or -1 on failure (would + * have read past the end of the buffer) * * This version assumes that it is safe to read at most 8 bytes past the end of * @end (we still return an error if the varint extends past @end).