Skip to content

Commit

Permalink
common: add function to convert from Microsoft's GUID to string
Browse files Browse the repository at this point in the history
  • Loading branch information
metalefty committed Nov 1, 2022
1 parent e396afb commit f9d929c
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
18 changes: 18 additions & 0 deletions common/guid.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,21 @@ const char *guid_to_str(const struct guid *guid, char *str)
g_bytes_to_hexstr(guid->g, GUID_SIZE, str, GUID_STR_SIZE);
return str;
}

const char *ms_guid_to_str(const char *src, char *dest)
{
const unsigned char *guid = (const unsigned char *)src;

/*
* Flipping integers into little-endian
* See also: https://devblogs.microsoft.com/oldnewthing/20220928-00/?p=107221
*/
g_sprintf(dest, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
guid[3], guid[2], guid[1], guid[0],
guid[5], guid[4],
guid[7], guid[6],
guid[8], guid[9],
guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);

return dest;
}
13 changes: 12 additions & 1 deletion common/guid.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#include "arch.h"

#define GUID_SIZE 16 /* bytes */
#define GUID_STR_SIZE (GUID_SIZE * 2 + 1) /* Size for string representation */
#define GUID_STR_SIZE (GUID_SIZE * 2 + 4 + 1) /* w/ 4 hyphens + null terminator */


/**
* Use a struct for the guid so we can easily copy by assignment
Expand Down Expand Up @@ -72,4 +73,14 @@ guid_is_set(const struct guid *guid);
*/
const char *guid_to_str(const struct guid *guid, char *str);


/**
* Converts a Microsoft's COM GUID to a string representation
*
* @param src GUID to represent
* @param dest pointer to at least GUID_STR_SIZE bytes to store the
* representation
*/
const char *ms_guid_to_str(const char *src, char *dest);
#endif

3 changes: 2 additions & 1 deletion tests/common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ test_common_SOURCES = \
test_string_calls.c \
test_os_calls.c \
test_ssl_calls.c \
test_base64.c
test_base64.c \
test_guid.c

test_common_CFLAGS = \
@CHECK_CFLAGS@ \
Expand Down
1 change: 1 addition & 0 deletions tests/common/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Suite *make_suite_test_string(void);
Suite *make_suite_test_os_calls(void);
Suite *make_suite_test_ssl_calls(void);
Suite *make_suite_test_base64(void);
Suite *make_suite_test_guid(void);

#endif /* TEST_COMMON_H */
1 change: 1 addition & 0 deletions tests/common/test_common_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int main (void)
srunner_add_suite(sr, make_suite_test_os_calls());
srunner_add_suite(sr, make_suite_test_ssl_calls());
srunner_add_suite(sr, make_suite_test_base64());
srunner_add_suite(sr, make_suite_test_guid());
// srunner_add_suite(sr, make_list_suite());

srunner_set_tap(sr, "-");
Expand Down
71 changes: 71 additions & 0 deletions tests/common/test_guid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#if defined(HAVE_CONFIG_H)
#include "config_ac.h"
#endif

#include "guid.h"
#include "string_calls.h"
#include "ms-rdpbcgr.h"

#include "test_common.h"

/******************************************************************************/

START_TEST(test_ms_guid_to_str_remotefx)
{
/* setup */
char dest[GUID_STR_SIZE];

/* test */
ms_guid_to_str(XR_CODEC_GUID_REMOTEFX, dest);

/* verify */
ck_assert_str_eq(dest, "76772F12-BD72-4463-AFB3-B73C9C6F7886");
}
END_TEST

START_TEST(test_ms_guid_to_str_nscodec)
{

/* setup */
char dest[GUID_STR_SIZE];

/* test */
ms_guid_to_str(XR_CODEC_GUID_NSCODEC, dest);

/* verify */
ck_assert_str_eq(dest, "CA8D1BB9-000F-154F-589F-AE2D1A87E2D6");
}
END_TEST

START_TEST(test_ms_guid_to_str_ignore)
{
/* setup */
char dest[GUID_STR_SIZE];

/* test */
ms_guid_to_str(XR_CODEC_GUID_IGNORE, dest);

/* verify */
ck_assert_str_eq(dest, "0C4351A6-3535-42AE-910C-CDFCE5760B58");
}
END_TEST

/******************************************************************************/

Suite *
make_suite_test_guid(void)
{
Suite *s;
TCase *tc_guid;

s = suite_create("GUID");

tc_guid = tcase_create("guid_to_str");
suite_add_tcase(s, tc_guid);
tcase_add_test(tc_guid, test_ms_guid_to_str_remotefx);
tcase_add_test(tc_guid, test_ms_guid_to_str_nscodec);
tcase_add_test(tc_guid, test_ms_guid_to_str_ignore);

return s;
}
42 changes: 42 additions & 0 deletions tests/common/test_string_calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#endif

#include "string_calls.h"
#include "ms-rdpbcgr.h"

#include "test_common.h"

Expand Down Expand Up @@ -1035,6 +1036,46 @@ END_TEST

/******************************************************************************/

START_TEST(test_guid_to_string_remotefx)
{
/* setup */
char dest[37] = { 0 };

/* test */
g_guid_to_string(dest, XR_CODEC_GUID_REMOTEFX);

/* verify */
ck_assert_str_eq(dest, "76772F12-BD72-4463-AFB3B73C9C6F7886");
}
END_TEST

START_TEST(test_guid_to_string_nscodec)
{
/* setup */
char dest[37] = { 0 };

/* test */
g_guid_to_string(dest, XR_CODEC_GUID_NSCODEC);

/* verify */
ck_assert_str_eq(dest, "CA8D1BB9-000F-154F-589FAE2D1A87E2D6");
}
END_TEST

START_TEST(test_guid_to_string_ignore)
{
/* setup */
char dest[37] = { 0 };

/* test */
g_guid_to_string(dest, XR_CODEC_GUID_IGNORE);

/* verify */
ck_assert_str_eq(dest, "0C4351A6-3535-42AE-910CCDFCE5760B58");
}
END_TEST
/******************************************************************************/

Suite *
make_suite_test_string(void)
{
Expand All @@ -1045,6 +1086,7 @@ make_suite_test_string(void)
TCase *tc_bm2char;
TCase *tc_char2bm;
TCase *tc_strtrim;
TCase *tc_guid;

s = suite_create("String");

Expand Down

0 comments on commit f9d929c

Please sign in to comment.