2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2015 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libjaylink-internal.h"
31 * Buffer helper functions.
35 * Write a 16-bit unsigned integer value to a buffer.
37 * The value is stored in the buffer in device byte order.
39 * @param[out] buffer Buffer to write the value into.
40 * @param[in] value Value to write into the buffer in host byte order.
41 * @param[in] offset Offset of the value within the buffer in bytes.
43 JAYLINK_PRIV
void buffer_set_u16(uint8_t *buffer
, uint16_t value
,
47 * Store the value in the buffer and swap byte order depending on the
50 #ifdef WORDS_BIGENDIAN
51 buffer
[offset
+ 0] = value
;
52 buffer
[offset
+ 1] = value
>> 8;
54 memcpy(buffer
+ offset
, &value
, sizeof(value
));
59 * Read a 16-bit unsigned integer value from a buffer.
61 * The value in the buffer is expected to be stored in device byte order.
63 * @param[in] buffer Buffer to read the value from.
64 * @param[in] offset Offset of the value within the buffer in bytes.
66 * @return The value read from the buffer in host byte order.
68 JAYLINK_PRIV
uint16_t buffer_get_u16(const uint8_t *buffer
, size_t offset
)
73 * Read the value from the buffer and swap byte order depending on the
76 #ifdef WORDS_BIGENDIAN
77 value
= (((uint16_t)buffer
[offset
+ 1])) | \
78 (((uint16_t)buffer
[offset
+ 0]) << 8);
80 memcpy(&value
, buffer
+ offset
, sizeof(value
));
87 * Write a 32-bit unsigned integer value to a buffer.
89 * The value is stored in the buffer in device byte order.
91 * @param[out] buffer Buffer to write the value into.
92 * @param[in] value Value to write into the buffer in host byte order.
93 * @param[in] offset Offset of the value within the buffer in bytes.
95 JAYLINK_PRIV
void buffer_set_u32(uint8_t *buffer
, uint32_t value
,
99 * Store the value in the buffer and swap byte order depending on the
102 #ifdef WORDS_BIGENDIAN
103 buffer
[offset
+ 0] = value
;
104 buffer
[offset
+ 1] = value
>> 8;
105 buffer
[offset
+ 2] = value
>> 16;
106 buffer
[offset
+ 3] = value
>> 24;
108 memcpy(buffer
+ offset
, &value
, sizeof(value
));
113 * Read a 32-bit unsigned integer value from a buffer.
115 * The value in the buffer is expected to be stored in device byte order.
117 * @param[in] buffer Buffer to read the value from.
118 * @param[in] offset Offset of the value within the buffer in bytes.
120 * @return The value read from the buffer in host byte order.
122 JAYLINK_PRIV
uint32_t buffer_get_u32(const uint8_t *buffer
, size_t offset
)
127 * Read the value from the buffer and swap byte order depending on the
130 #ifdef WORDS_BIGENDIAN
131 value
= (((uint32_t)buffer
[offset
+ 3])) | \
132 (((uint32_t)buffer
[offset
+ 2]) << 8) | \
133 (((uint32_t)buffer
[offset
+ 1]) << 16) | \
134 (((uint32_t)buffer
[offset
+ 0]) << 24);
136 memcpy(&value
, buffer
+ offset
, sizeof(value
));