Skip to content

Commit

Permalink
Add UUID little endian utility functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nkarstens committed Nov 24, 2023
1 parent 67cde2e commit 033f44e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pbio/include/pbio/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ void pbio_set_uint32_be(uint8_t *buf, uint32_t value) {
buf[3] = value;
}

bool pbio_uuid128_le_compare(const uint8_t *le, const uint8_t *uuid);
void pbio_uuid128_le_copy(uint8_t *dst, const uint8_t *src);
bool pbio_uuid128_reverse_compare(const uint8_t *uuid1, const uint8_t *uuid2);
void pbio_uuid128_reverse_copy(uint8_t *dst, const uint8_t *src);

Expand Down
52 changes: 52 additions & 0 deletions lib/pbio/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

/**
* Compares a 128-bit UUID from @p le in little endian format with
* the given uuid @p uuid.
*
* According to RFC 4122, the UUID is grouped into the following:
* 1) One 32-bit
* 2) Two 16-bit
* 3) Eight 8-bit
*
* @param [in] le The little endian UUID.
* @param [in] uuid The UUID to compare against.
*/
bool pbio_uuid128_le_compare(const uint8_t *le, const uint8_t *uuid) {
return (le[0] == uuid[3]) &&
(le[1] == uuid[2]) &&
(le[2] == uuid[1]) &&
(le[3] == uuid[0]) &&
(le[4] == uuid[5]) &&
(le[5] == uuid[4]) &&
(le[6] == uuid[7]) &&
(le[7] == uuid[6]) &&
(memcmp(&le[8], &uuid[8], 8) == 0);
}

/**
* Copies a 128-bit UUID from @p src to a buffer @p dst,
* which is a buffer used by a little endian medium.
*
* According to RFC 4122, the UUID is grouped into the following:
* 1) One 32-bit
* 2) Two 16-bit
* 3) Eight 8-bit
*
* @param [in] dst The destination array.
* @param [in] src The UUID to reverse and copy.
*/
void pbio_uuid128_le_copy(uint8_t *dst, const uint8_t *src) {
dst[0] = src[3];
dst[1] = src[2];
dst[2] = src[1];
dst[3] = src[0];

dst[4] = src[5];
dst[5] = src[4];

dst[6] = src[7];
dst[7] = src[6];

memcpy(&dst[8], &src[8], 8);
}

/**
* Compares two 128-bit UUIDs with opposite byte ordering for equality.
Expand Down

0 comments on commit 033f44e

Please sign in to comment.