File: numeric.h

package info (click to toggle)
linux 6.18.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,742,096 kB
  • sloc: ansic: 26,781,576; asm: 272,087; sh: 148,750; python: 79,244; makefile: 57,741; perl: 36,527; xml: 19,542; cpp: 5,911; yacc: 4,939; lex: 2,950; awk: 1,607; sed: 30; ruby: 25
file content (78 lines) | stat: -rw-r--r-- 1,960 bytes parent folder | download | duplicates (17)
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
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright 2023 Red Hat
 */

#ifndef UDS_NUMERIC_H
#define UDS_NUMERIC_H

#include <linux/unaligned.h>
#include <linux/kernel.h>
#include <linux/types.h>

/*
 * These utilities encode or decode a number from an offset in a larger data buffer and then
 * advance the offset pointer to the next field in the buffer.
 */

static inline void decode_s64_le(const u8 *buffer, size_t *offset, s64 *decoded)
{
	*decoded = get_unaligned_le64(buffer + *offset);
	*offset += sizeof(s64);
}

static inline void encode_s64_le(u8 *data, size_t *offset, s64 to_encode)
{
	put_unaligned_le64(to_encode, data + *offset);
	*offset += sizeof(s64);
}

static inline void decode_u64_le(const u8 *buffer, size_t *offset, u64 *decoded)
{
	*decoded = get_unaligned_le64(buffer + *offset);
	*offset += sizeof(u64);
}

static inline void encode_u64_le(u8 *data, size_t *offset, u64 to_encode)
{
	put_unaligned_le64(to_encode, data + *offset);
	*offset += sizeof(u64);
}

static inline void decode_s32_le(const u8 *buffer, size_t *offset, s32 *decoded)
{
	*decoded = get_unaligned_le32(buffer + *offset);
	*offset += sizeof(s32);
}

static inline void encode_s32_le(u8 *data, size_t *offset, s32 to_encode)
{
	put_unaligned_le32(to_encode, data + *offset);
	*offset += sizeof(s32);
}

static inline void decode_u32_le(const u8 *buffer, size_t *offset, u32 *decoded)
{
	*decoded = get_unaligned_le32(buffer + *offset);
	*offset += sizeof(u32);
}

static inline void encode_u32_le(u8 *data, size_t *offset, u32 to_encode)
{
	put_unaligned_le32(to_encode, data + *offset);
	*offset += sizeof(u32);
}

static inline void decode_u16_le(const u8 *buffer, size_t *offset, u16 *decoded)
{
	*decoded = get_unaligned_le16(buffer + *offset);
	*offset += sizeof(u16);
}

static inline void encode_u16_le(u8 *data, size_t *offset, u16 to_encode)
{
	put_unaligned_le16(to_encode, data + *offset);
	*offset += sizeof(u16);
}

#endif /* UDS_NUMERIC_H */