Skip to content

Commit 487b05d

Browse files
committed
Add UUID little endian utility functions
Adds functions to copy and compare UUIDs in little endian format. USB is a little endian protocol, so this will be used to encode and decode UUIDs properly on this medium.
1 parent 2f86e9d commit 487b05d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/pbio/include/pbio/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ void pbio_set_uint32_be(uint8_t *buf, uint32_t value) {
118118
buf[3] = value;
119119
}
120120

121+
bool pbio_uuid128_le_compare(const uint8_t *le, const uint8_t *uuid);
122+
void pbio_uuid128_le_copy(uint8_t *dst, const uint8_t *src);
121123
bool pbio_uuid128_reverse_compare(const uint8_t *uuid1, const uint8_t *uuid2);
122124
void pbio_uuid128_reverse_copy(uint8_t *dst, const uint8_t *src);
123125

lib/pbio/src/util.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,58 @@
33

44
#include <stdbool.h>
55
#include <stdint.h>
6+
#include <string.h>
7+
8+
/**
9+
* Compares a 128-bit UUID from @p le in little endian format with
10+
* the given uuid @p uuid.
11+
*
12+
* According to RFC 4122, the UUID is grouped into the following:
13+
* 1) One 32-bit
14+
* 2) Two 16-bit
15+
* 3) Eight 8-bit
16+
*
17+
* @param [in] le The little endian UUID.
18+
* @param [in] uuid The UUID to compare against.
19+
*/
20+
bool pbio_uuid128_le_compare(const uint8_t *le, const uint8_t *uuid) {
21+
return ((le[0] == uuid[3]) &&
22+
(le[1] == uuid[2]) &&
23+
(le[2] == uuid[1]) &&
24+
(le[3] == uuid[0]) &&
25+
(le[4] == uuid[5]) &&
26+
(le[5] == uuid[4]) &&
27+
(le[6] == uuid[7]) &&
28+
(le[7] == uuid[6]) &&
29+
(memcmp(&le[8], &uuid[8], 8) == 0));
30+
}
31+
32+
/**
33+
* Copies a 128-bit UUID from @p src to a buffer @p dst,
34+
* which is a buffer used by a little endian medium.
35+
*
36+
* According to RFC 4122, the UUID is grouped into the following:
37+
* 1) One 32-bit
38+
* 2) Two 16-bit
39+
* 3) Eight 8-bit
40+
*
41+
* @param [in] dst The destination array.
42+
* @param [in] src The UUID to reverse and copy.
43+
*/
44+
void pbio_uuid128_le_copy(uint8_t *dst, const uint8_t *src) {
45+
dst[0] = src[3];
46+
dst[1] = src[2];
47+
dst[2] = src[1];
48+
dst[3] = src[0];
49+
50+
dst[4] = src[5];
51+
dst[5] = src[4];
52+
53+
dst[6] = src[7];
54+
dst[7] = src[6];
55+
56+
memcpy(&dst[8], &src[8], 8);
57+
}
658

759
/**
860
* Compares two 128-bit UUIDs with opposite byte ordering for equality.

0 commit comments

Comments
 (0)