NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pack.c
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 #include <string.h>
27 
28 #include "pack.h"
29 
30 uint64_t ntohq(uint64_t value)
31 {
32  const int numeric_one = 1;
33 
34  /* If we are in a Big-Endian architecture, we don't need to do anything */
35  if (*(const uint8_t *)&numeric_one != 1)
36  return value;
37 
38  /* Swap the 8 bytes of our value */
39  value = SET_BYTE((uint64_t)GET_BYTE(value, 0), 7) | SET_BYTE((uint64_t)GET_BYTE(value, 1), 6) |
40  SET_BYTE((uint64_t)GET_BYTE(value, 2), 5) | SET_BYTE((uint64_t)GET_BYTE(value, 3), 4) |
41  SET_BYTE((uint64_t)GET_BYTE(value, 4), 3) | SET_BYTE((uint64_t)GET_BYTE(value, 5), 2) |
42  SET_BYTE((uint64_t)GET_BYTE(value, 6), 1) | SET_BYTE((uint64_t)GET_BYTE(value, 7), 0);
43 
44  return value;
45 }
46 
47 void pack_uint8(uint8_t **buffer, uint8_t value)
48 {
49  uint8_t *write_head = *buffer;
50 
51  *write_head++ = value;
52  *buffer = write_head;
53 }
54 
55 void pack_uint16(uint8_t **buffer, uint16_t value)
56 {
57  uint8_t *write_head = *buffer;
58 
59  *write_head++ = GET_BYTE(value, 1);
60  *write_head++ = GET_BYTE(value, 0);
61  *buffer = write_head;
62 }
63 
64 void pack_uint32(uint8_t **buffer, uint32_t value)
65 {
66  uint8_t *write_head = *buffer;
67 
68  *write_head++ = GET_BYTE(value, 3);
69  *write_head++ = GET_BYTE(value, 2);
70  *write_head++ = GET_BYTE(value, 1);
71  *write_head++ = GET_BYTE(value, 0);
72  *buffer = write_head;
73 }
74 
75 void pack_uint64(uint8_t **buffer, uint64_t value)
76 {
77  uint8_t *write_head = *buffer;
78 
79  *write_head++ = GET_BYTE(value, 7);
80  *write_head++ = GET_BYTE(value, 6);
81  *write_head++ = GET_BYTE(value, 5);
82  *write_head++ = GET_BYTE(value, 4);
83  *write_head++ = GET_BYTE(value, 3);
84  *write_head++ = GET_BYTE(value, 2);
85  *write_head++ = GET_BYTE(value, 1);
86  *write_head++ = GET_BYTE(value, 0);
87  *buffer = write_head;
88 }
89 
90 void pack_blob(uint8_t **buffer, size_t length, uint8_t *object)
91 {
92  uint8_t *write_head = *buffer;
93 
94  memcpy(write_head, object, length);
95  write_head += length;
96  *buffer = write_head;
97 }
98 
99 uint8_t unpack_uint8(uint8_t **buffer)
100 {
101  uint8_t value = **buffer;
102 
103  *buffer += 1;
104 
105  return value;
106 }
107 
108 uint16_t unpack_uint16(uint8_t **buffer)
109 {
110  uint16_t value = 0;
111  uint8_t *read_head = *buffer;
112 
113  value |= SET_BYTE(*read_head++, 1);
114  value |= SET_BYTE(*read_head++, 0);
115  *buffer = read_head;
116 
117  return value;
118 }
119 
120 uint32_t unpack_uint32(uint8_t **buffer)
121 {
122  uint32_t value = 0;
123  uint8_t *read_head = *buffer;
124 
125  value |= SET_BYTE((uint32_t)(*read_head++), 3);
126  value |= SET_BYTE((uint32_t)(*read_head++), 2);
127  value |= SET_BYTE((uint32_t)(*read_head++), 1);
128  value |= SET_BYTE((uint32_t)(*read_head++), 0);
129  *buffer = read_head;
130 
131  return value;
132 }
133 
134 uint64_t unpack_uint64(uint8_t **buffer)
135 {
136  uint64_t value = 0;
137  uint8_t *read_head = *buffer;
138 
139  value |= SET_BYTE((uint64_t)(*read_head++), 7);
140  value |= SET_BYTE((uint64_t)(*read_head++), 6);
141  value |= SET_BYTE((uint64_t)(*read_head++), 5);
142  value |= SET_BYTE((uint64_t)(*read_head++), 4);
143  value |= SET_BYTE((uint64_t)(*read_head++), 3);
144  value |= SET_BYTE((uint64_t)(*read_head++), 2);
145  value |= SET_BYTE((uint64_t)(*read_head++), 1);
146  value |= SET_BYTE((uint64_t)(*read_head++), 0);
147  *buffer = read_head;
148 
149  return value;
150 }
151 
152 void unpack_blob(uint8_t **buffer, size_t length, uint8_t *output)
153 {
154  uint8_t *read_head = *buffer;
155 
156  memcpy(output, read_head, length);
157  read_head += length;
158  *buffer = read_head;
159 }
type value
void unpack_blob(uint8_t **buffer, size_t length, uint8_t *output)
Definition: pack.c:152
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 pack_uint16(uint8_t **buffer, uint16_t value)
Definition: pack.c:55
#define GET_BYTE(V, N)
Definition: pack.h:37
#define SET_BYTE(V, N)
Definition: pack.h:39