1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2023 Red Hat
9 #include <linux/unaligned.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
14 * These utilities encode or decode a number from an offset in a larger data buffer and then
15 * advance the offset pointer to the next field in the buffer.
18 static inline void decode_s64_le(const u8
*buffer
, size_t *offset
, s64
*decoded
)
20 *decoded
= get_unaligned_le64(buffer
+ *offset
);
21 *offset
+= sizeof(s64
);
24 static inline void encode_s64_le(u8
*data
, size_t *offset
, s64 to_encode
)
26 put_unaligned_le64(to_encode
, data
+ *offset
);
27 *offset
+= sizeof(s64
);
30 static inline void decode_u64_le(const u8
*buffer
, size_t *offset
, u64
*decoded
)
32 *decoded
= get_unaligned_le64(buffer
+ *offset
);
33 *offset
+= sizeof(u64
);
36 static inline void encode_u64_le(u8
*data
, size_t *offset
, u64 to_encode
)
38 put_unaligned_le64(to_encode
, data
+ *offset
);
39 *offset
+= sizeof(u64
);
42 static inline void decode_s32_le(const u8
*buffer
, size_t *offset
, s32
*decoded
)
44 *decoded
= get_unaligned_le32(buffer
+ *offset
);
45 *offset
+= sizeof(s32
);
48 static inline void encode_s32_le(u8
*data
, size_t *offset
, s32 to_encode
)
50 put_unaligned_le32(to_encode
, data
+ *offset
);
51 *offset
+= sizeof(s32
);
54 static inline void decode_u32_le(const u8
*buffer
, size_t *offset
, u32
*decoded
)
56 *decoded
= get_unaligned_le32(buffer
+ *offset
);
57 *offset
+= sizeof(u32
);
60 static inline void encode_u32_le(u8
*data
, size_t *offset
, u32 to_encode
)
62 put_unaligned_le32(to_encode
, data
+ *offset
);
63 *offset
+= sizeof(u32
);
66 static inline void decode_u16_le(const u8
*buffer
, size_t *offset
, u16
*decoded
)
68 *decoded
= get_unaligned_le16(buffer
+ *offset
);
69 *offset
+= sizeof(u16
);
72 static inline void encode_u16_le(u8
*data
, size_t *offset
, u16 to_encode
)
74 put_unaligned_le16(to_encode
, data
+ *offset
);
75 *offset
+= sizeof(u16
);
78 #endif /* UDS_NUMERIC_H */