NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pack.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef COMMON_PACK_H_
27 #define COMMON_PACK_H_
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /* Get LSB at position N from logical value V */
37 #define GET_BYTE(V, N) ((uint8_t)((V) >> ((N)*8) & 0xFF))
38 /* Set byte value V at the LSB position N */
39 #define SET_BYTE(V, N) (((V)&0xFF) << ((N)*8))
40 
41 /*
42  * 64-bit extensions to regular host-to-network/network-to-host functions
43  *
44  * @value [in]: value to convert
45  * @return: host byte order/network byte order
46  */
47 uint64_t ntohq(uint64_t value);
48 #define htonq ntohq
49 
50 /*
51  * Pack an 8-bit numeric value into a work buffer, and advance the write head.
52  *
53  * @buffer [in]: pointer to a write-head to write into, and to increment
54  * @value [in]: value to pack
55  */
56 void pack_uint8(uint8_t **buffer, uint8_t value);
57 
58 /*
59  * Pack a 16-bit numeric value into a work buffer in Big Endian, and advance the write head.
60  *
61  * @buffer [in]: pointer to a write-head to write into, and to increment
62  * @value [in]: value to pack
63  */
64 void pack_uint16(uint8_t **buffer, uint16_t value);
65 
66 /*
67  * Pack a 32-bit numeric value into a work buffer in Big Endian, and advance the write head.
68  *
69  * @buffer [in]: pointer to a write-head to write into, and to increment
70  * @value [in]: value to pack
71  */
72 void pack_uint32(uint8_t **buffer, uint32_t value);
73 
74 /*
75  * Pack a 64-bit numeric value into a work buffer in Big Endian, and advance the write head.
76  *
77  * @buffer [in]: pointer to a write-head to write into, and to increment
78  * @value [in]: value to pack
79  */
80 void pack_uint64(uint8_t **buffer, uint64_t value);
81 
82 /*
83  * Pack a binary large object into a work buffer, and advance the write head.
84  *
85  * @buffer [in]: pointer to a write-head to write into, and to increment
86  * @length [in]: object size to pack
87  * @object [in]: pointer to byte array to be packed
88  */
89 void pack_blob(uint8_t **buffer, size_t length, uint8_t *object);
90 
91 /*
92  * Unpack an 8-bit numeric value from a work buffer, and advance the read head.
93  *
94  * @buffer [in]: pointer to a read-head to read from, and to increment
95  * @return: unpacked numerical value
96  */
97 uint8_t unpack_uint8(uint8_t **buffer);
98 
99 /*
100  * Unpack a 16-bit Big Endian numeric value from a work buffer, and advance the read head.
101  *
102  * @buffer [in]: pointer to a read-head to read from, and to increment
103  * @return: unpacked numerical value
104  */
105 uint16_t unpack_uint16(uint8_t **buffer);
106 
107 /*
108  * Unpack a 32-bit Big Endian numeric value from a work buffer, and advance the read head.
109  *
110  * @buffer [in]: pointer to a read-head to read from, and to increment
111  * @return: unpacked numerical value
112  */
113 uint32_t unpack_uint32(uint8_t **buffer);
114 
115 /*
116  * Unpack a 64-bit Big Endian numeric value from a work buffer, and advance the read head.
117  *
118  * @buffer [in]: pointer to a read-head to read from, and to increment
119  * @return: unpacked numerical value
120  */
121 uint64_t unpack_uint64(uint8_t **buffer);
122 
123 /*
124  * Unpack a binary large object from a work buffer, and advance the read head.
125  *
126  * @buffer [in]: pointer to a read-head to read from, and to increment
127  * @length [in]: object size to unpack
128  * @object [out]: pointer to hold received byte array
129  */
130 void unpack_blob(uint8_t **buffer, size_t length, uint8_t *object);
131 
132 #ifdef __cplusplus
133 } /* extern "C" */
134 #endif
135 
136 #endif /* COMMON_PACK_H_ */
type value
uint64_t ntohq(uint64_t value)
Definition: pack.c:30
uint16_t unpack_uint16(uint8_t **buffer)
Definition: pack.c:108
void pack_uint32(uint8_t **buffer, uint32_t value)
Definition: pack.c:64
uint8_t unpack_uint8(uint8_t **buffer)
Definition: pack.c:99
void pack_blob(uint8_t **buffer, size_t length, uint8_t *object)
Definition: pack.c:90
uint64_t unpack_uint64(uint8_t **buffer)
Definition: pack.c:134
uint32_t unpack_uint32(uint8_t **buffer)
Definition: pack.c:120
void pack_uint64(uint8_t **buffer, uint64_t value)
Definition: pack.c:75
void pack_uint8(uint8_t **buffer, uint8_t value)
Definition: pack.c:47
void unpack_blob(uint8_t **buffer, size_t length, uint8_t *object)
Definition: pack.c:152
void pack_uint16(uint8_t **buffer, uint16_t value)
Definition: pack.c:55