Commit 3a20274ddff20d9c3ea4895353b23ff252954b87

Christoph Reichenbach 2022-06-04T20:16:28

Refactoring: move GUID operations out of Joystick - SDL_JoystickGUID -> SDL_GUID (though we retain a type alias) - Operations for GUID <-> String ops are now in src/SDL_guid.c and include/SDL_guid.h - The corresponding Joystick operations delegate to SDL_guid.c - Added test/testguid.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
diff --git a/Makefile.in b/Makefile.in
index 7a4b1ac..5b06356 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -80,6 +80,7 @@ HDRS = \
 	SDL_filesystem.h \
 	SDL_gamecontroller.h \
 	SDL_gesture.h \
+	SDL_guid.h \
 	SDL_haptic.h \
 	SDL_hidapi.h \
 	SDL_hints.h \
diff --git a/Makefile.os2 b/Makefile.os2
index 5d19272..14b4953 100644
--- a/Makefile.os2
+++ b/Makefile.os2
@@ -67,7 +67,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MAJOR_VERSION=$(MAJOR_VERSION)
 CFLAGS_DLL+= -DSDL_BUILD_MINOR_VERSION=$(MINOR_VERSION)
 CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
 
-SRCS = SDL.c SDL_assert.c SDL_error.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
+SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
 SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
 SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
 SRCS+= SDL_rwops.c SDL_power.c
diff --git a/Makefile.w32 b/Makefile.w32
index e9d7264..60775f1 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -43,7 +43,7 @@ CFLAGS_DLL+= -DSDL_BUILD_MICRO_VERSION=$(MICRO_VERSION)
 
 RCFLAGS = -q -r -bt=nt $(INCPATH)
 
-SRCS = SDL.c SDL_assert.c SDL_error.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
+SRCS = SDL.c SDL_assert.c SDL_error.c SDL_guid.c SDL_log.c SDL_dataqueue.c SDL_hints.c SDL_list.c
 SRCS+= SDL_getenv.c SDL_iconv.c SDL_malloc.c SDL_qsort.c SDL_stdlib.c SDL_string.c SDL_strtokr.c SDL_crc32.c
 SRCS+= SDL_cpuinfo.c SDL_atomic.c SDL_spinlock.c SDL_thread.c SDL_timer.c
 SRCS+= SDL_rwops.c SDL_power.c
diff --git a/VisualC-WinRT/SDL-UWP.vcxproj b/VisualC-WinRT/SDL-UWP.vcxproj
index 5ac747d..80c8379 100644
--- a/VisualC-WinRT/SDL-UWP.vcxproj
+++ b/VisualC-WinRT/SDL-UWP.vcxproj
@@ -45,6 +45,7 @@
     <ClInclude Include="..\include\SDL_error.h" />
     <ClInclude Include="..\include\SDL_events.h" />
     <ClInclude Include="..\include\SDL_filesystem.h" />
+    <ClInclude Include="..\include\SDL_guid.h" />
     <ClInclude Include="..\include\SDL_haptic.h" />
     <ClInclude Include="..\include\SDL_hints.h" />
     <ClInclude Include="..\include\SDL_hidapi.h" />
@@ -301,6 +302,7 @@
     <ClCompile Include="..\src\SDL_dataqueue.c" />
     <ClCompile Include="..\src\SDL_list.c" />
     <ClCompile Include="..\src\SDL_error.c" />
+    <ClCompile Include="..\src\SDL_guid.c" />
     <ClCompile Include="..\src\SDL_hints.c" />
     <ClCompile Include="..\src\SDL_log.c" />
     <ClCompile Include="..\src\sensor\dummy\SDL_dummysensor.c" />
diff --git a/VisualC-WinRT/SDL-UWP.vcxproj.filters b/VisualC-WinRT/SDL-UWP.vcxproj.filters
index fbb03f2..af1553b 100644
--- a/VisualC-WinRT/SDL-UWP.vcxproj.filters
+++ b/VisualC-WinRT/SDL-UWP.vcxproj.filters
@@ -63,6 +63,9 @@
     <ClInclude Include="..\include\SDL_filesystem.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\include\SDL_guid.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
     <ClInclude Include="..\include\SDL_haptic.h">
       <Filter>Header Files</Filter>
     </ClInclude>
@@ -633,6 +636,9 @@
     <ClCompile Include="..\src\SDL_error.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\src\SDL_guid.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="..\src\SDL_hints.c">
       <Filter>Source Files</Filter>
     </ClCompile>
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index 679b678..aaeda87 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -234,6 +234,7 @@
     <ClInclude Include="..\..\include\SDL_filesystem.h" />
     <ClInclude Include="..\..\include\SDL_gamecontroller.h" />
     <ClInclude Include="..\..\include\SDL_gesture.h" />
+    <ClInclude Include="..\..\include\SDL_guid.h" />
     <ClInclude Include="..\..\include\SDL_haptic.h" />
     <ClInclude Include="..\..\include\SDL_hints.h" />
     <ClInclude Include="..\..\include\SDL_hidapi.h" />
@@ -367,6 +368,7 @@
     <ClInclude Include="..\..\src\SDL_assert_c.h" />
     <ClInclude Include="..\..\src\SDL_dataqueue.h" />
     <ClInclude Include="..\..\src\SDL_error_c.h" />
+    <ClCompile Include="..\..\src\SDL_guid.c" />
     <ClInclude Include="..\..\src\SDL_hints_c.h" />
     <ClInclude Include="..\..\src\SDL_internal.h" />
     <ClInclude Include="..\..\src\SDL_list.h" />
diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters
index 582bba4..a3d9d5c 100644
--- a/VisualC/SDL/SDL.vcxproj.filters
+++ b/VisualC/SDL/SDL.vcxproj.filters
@@ -231,6 +231,9 @@
     <ClInclude Include="..\..\include\SDL_gesture.h">
       <Filter>API Headers</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\include\SDL_guid.h">
+      <Filter>API Headers</Filter>
+    </ClInclude>
     <ClInclude Include="..\..\include\SDL_haptic.h">
       <Filter>API Headers</Filter>
     </ClInclude>
@@ -833,6 +836,7 @@
     <ClCompile Include="..\..\src\SDL_assert.c" />
     <ClCompile Include="..\..\src\SDL_dataqueue.c" />
     <ClCompile Include="..\..\src\SDL_error.c" />
+    <ClCompile Include="..\..\src\SDL_guid.c" />
     <ClCompile Include="..\..\src\SDL_hints.c" />
     <ClCompile Include="..\..\src\SDL_list.c" />
     <ClCompile Include="..\..\src\audio\SDL_audio.c">
diff --git a/include/SDL_guid.h b/include/SDL_guid.h
new file mode 100644
index 0000000..ba99f3c
--- /dev/null
+++ b/include/SDL_guid.h
@@ -0,0 +1,100 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+/**
+ *  \file SDL_guid.h
+ *
+ *  Include file for handling ::SDL_GUID values.
+ */
+
+#ifndef SDL_guid_h_
+#define SDL_guid_h_
+
+#include "SDL_stdinc.h"
+#include "SDL_error.h"
+
+#include "begin_code.h"
+/* Set up for C function definitions, even when using C++ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * An SDL_GUID is a 128-bit identifier for an input device that
+ *   identifies that device across runs of SDL programs on the same
+ *   platform.  If the device is detached and then re-attached to a
+ *   different port, or if the base system is rebooted, the device
+ *   should still report the same GUID.
+ *
+ * GUIDs are as precise as possible but are not guaranteed to
+ *   distinguish physically distinct but equivalent devices.  For
+ *   example, two game controllers from the same vendor with the same
+ *   product ID and revision may have the same GUID.
+ *
+ * GUIDs may be platform-dependent (i.e., the same device may report
+ *   different GUIDs on different operating systems).
+ */
+typedef struct {
+    Uint8 data[16];
+} SDL_GUID;
+
+/* Function prototypes */
+
+/**
+ * Get an ASCII string representation for a given ::SDL_GUID.
+ *
+ * You should supply at least 33 bytes for pszGUID.
+ *
+ * \param guid the ::SDL_GUID you wish to convert to string
+ * \param pszGUID buffer in which to write the ASCII string
+ * \param cbGUID the size of pszGUID
+ *
+ * \since This function is available since SDL 2.tbd.
+ *
+ * \sa SDL_GUIDFromString
+ */
+extern DECLSPEC void SDLCALL SDL_GUIDToString(SDL_GUID guid, char *pszGUID, int cbGUID);
+
+/**
+ * Convert a GUID string into a ::SDL_GUID structure.
+ *
+ * Performs no error checking. If this function is given a string containing
+ * an invalid GUID, the function will silently succeed, but the GUID generated
+ * will not be useful.
+ *
+ * \param pchGUID string containing an ASCII representation of a GUID
+ * \returns a ::SDL_GUID structure.
+ *
+ * \since This function is available since SDL 2.tbd.
+ *
+ * \sa SDL_GUIDToString
+ */
+extern DECLSPEC SDL_GUID SDLCALL SDL_GUIDFromString(const char *pchGUID);
+
+/* Ends C function definitions when using C++ */
+#ifdef __cplusplus
+}
+#endif
+#include "close_code.h"
+
+#endif /* SDL_guid_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/include/SDL_joystick.h b/include/SDL_joystick.h
index 0c7562b..60bd470 100644
--- a/include/SDL_joystick.h
+++ b/include/SDL_joystick.h
@@ -43,6 +43,7 @@
 
 #include "SDL_stdinc.h"
 #include "SDL_error.h"
+#include "SDL_guid.h"
 
 #include "begin_code.h"
 /* Set up for C function definitions, even when using C++ */
@@ -69,9 +70,7 @@ struct _SDL_Joystick;
 typedef struct _SDL_Joystick SDL_Joystick;
 
 /* A structure that encodes the stable unique id for a joystick device */
-typedef struct {
-    Uint8 data[16];
-} SDL_JoystickGUID;
+typedef SDL_GUID SDL_JoystickGUID;
 
 /**
  * This is a unique ID for a joystick for the time it is connected to the system,
diff --git a/src/SDL_guid.c b/src/SDL_guid.c
new file mode 100644
index 0000000..d3529b7
--- /dev/null
+++ b/src/SDL_guid.c
@@ -0,0 +1,91 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_internal.h"
+
+
+#include "SDL_guid.h"
+
+/* convert the guid to a printable string */
+void SDL_GUIDToString(SDL_GUID guid, char *pszGUID, int cbGUID)
+{
+    static const char k_rgchHexToASCII[] = "0123456789abcdef";
+    int i;
+
+    if ((pszGUID == NULL) || (cbGUID <= 0)) {
+        return;
+    }
+
+    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
+        /* each input byte writes 2 ascii chars, and might write a null byte. */
+        /* If we don't have room for next input byte, stop */
+        unsigned char c = guid.data[i];
+
+        *pszGUID++ = k_rgchHexToASCII[c >> 4];
+        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
+    }
+    *pszGUID = '\0';
+}
+
+/*-----------------------------------------------------------------------------
+ * Purpose: Returns the 4 bit nibble for a hex character
+ * Input  : c -
+ * Output : unsigned char
+ *-----------------------------------------------------------------------------*/
+static unsigned char nibble(unsigned char c)
+{
+    if ((c >= '0') && (c <= '9')) {
+        return (c - '0');
+    }
+
+    if ((c >= 'A') && (c <= 'F')) {
+        return (c - 'A' + 0x0a);
+    }
+
+    if ((c >= 'a') && (c <= 'f')) {
+        return (c - 'a' + 0x0a);
+    }
+
+    /* received an invalid character, and no real way to return an error */
+    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
+    return 0;
+}
+
+/* convert the string version of a guid to the struct */
+SDL_GUID SDL_GUIDFromString(const char *pchGUID)
+{
+    SDL_GUID guid;
+    int maxoutputbytes= sizeof(guid);
+    size_t len = SDL_strlen(pchGUID);
+    Uint8 *p;
+    size_t i;
+
+    /* Make sure it's even */
+    len = (len) & ~0x1;
+
+    SDL_memset(&guid, 0x00, sizeof(guid));
+
+    p = (Uint8 *)&guid;
+    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
+        *p = (nibble((unsigned char)pchGUID[i]) << 4) | nibble((unsigned char)pchGUID[i+1]);
+    }
+
+    return guid;
+}
diff --git a/src/dynapi/SDL2.exports b/src/dynapi/SDL2.exports
index 9c6dac4..8cd2a4b 100644
--- a/src/dynapi/SDL2.exports
+++ b/src/dynapi/SDL2.exports
@@ -843,3 +843,5 @@
 ++'_SDL_JoystickAttachVirtualEx'.'SDL2.dll'.'SDL_JoystickAttachVirtualEx'
 ++'_SDL_GameControllerGetFirmwareVersion'.'SDL2.dll'.'SDL_GameControllerGetFirmwareVersion'
 ++'_SDL_JoystickGetFirmwareVersion'.'SDL2.dll'.'SDL_JoystickGetFirmwareVersion'
+++'_SDL_GUIDToString'.'SDL2.dll'.'SDL_GUIDToString'
+++'_SDL_GUIDFromString'.'SDL2.dll'.'SDL_GUIDFromString'
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 3e4c258..85ee397 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -869,3 +869,5 @@
 #define SDL_JoystickAttachVirtualEx SDL_JoystickAttachVirtualEx_REAL
 #define SDL_GameControllerGetFirmwareVersion SDL_GameControllerGetFirmwareVersion_REAL
 #define SDL_JoystickGetFirmwareVersion SDL_JoystickGetFirmwareVersion_REAL
+#define SDL_GUIDToString SDL_GUIDToString_REAL
+#define SDL_GUIDFromString SDL_GUIDFromString_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index f316476..db3f864 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -944,3 +944,5 @@ SDL_DYNAPI_PROC(const char*,SDL_JoystickPath,(SDL_Joystick *a),(a),return)
 SDL_DYNAPI_PROC(int,SDL_JoystickAttachVirtualEx,(const SDL_VirtualJoystickDesc *a),(a),return)
 SDL_DYNAPI_PROC(Uint16,SDL_GameControllerGetFirmwareVersion,(SDL_GameController *a),(a),return)
 SDL_DYNAPI_PROC(Uint16,SDL_JoystickGetFirmwareVersion,(SDL_Joystick *a),(a),return)
+SDL_DYNAPI_PROC(void,SDL_GUIDToString,(SDL_GUID a, char *b, int c),(a,b,c),)
+SDL_DYNAPI_PROC(SDL_GUID,SDL_GUIDFromString,(const char *a),(a),return)
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index ae4f5dc..2d31578 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -2735,68 +2735,13 @@ SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick)
 /* convert the guid to a printable string */
 void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
 {
-    static const char k_rgchHexToASCII[] = "0123456789abcdef";
-    int i;
-
-    if ((pszGUID == NULL) || (cbGUID <= 0)) {
-        return;
-    }
-
-    for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
-        /* each input byte writes 2 ascii chars, and might write a null byte. */
-        /* If we don't have room for next input byte, stop */
-        unsigned char c = guid.data[i];
-
-        *pszGUID++ = k_rgchHexToASCII[c >> 4];
-        *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
-    }
-    *pszGUID = '\0';
-}
-
-/*-----------------------------------------------------------------------------
- * Purpose: Returns the 4 bit nibble for a hex character
- * Input  : c -
- * Output : unsigned char
- *-----------------------------------------------------------------------------*/
-static unsigned char nibble(unsigned char c)
-{
-    if ((c >= '0') && (c <= '9')) {
-        return (c - '0');
-    }
-
-    if ((c >= 'A') && (c <= 'F')) {
-        return (c - 'A' + 0x0a);
-    }
-
-    if ((c >= 'a') && (c <= 'f')) {
-        return (c - 'a' + 0x0a);
-    }
-
-    /* received an invalid character, and no real way to return an error */
-    /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
-    return 0;
+    SDL_GUIDToString(guid, pszGUID, cbGUID);
 }
 
 /* convert the string version of a joystick guid to the struct */
 SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
 {
-    SDL_JoystickGUID guid;
-    int maxoutputbytes= sizeof(guid);
-    size_t len = SDL_strlen(pchGUID);
-    Uint8 *p;
-    size_t i;
-
-    /* Make sure it's even */
-    len = (len) & ~0x1;
-
-    SDL_memset(&guid, 0x00, sizeof(guid));
-
-    p = (Uint8 *)&guid;
-    for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
-        *p = (nibble((unsigned char)pchGUID[i]) << 4) | nibble((unsigned char)pchGUID[i+1]);
-    }
-
-    return guid;
+    return SDL_GUIDFromString(pchGUID);
 }
 
 /* update the power level for this joystick */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index da3482b..3273935 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -85,6 +85,7 @@ add_executable(testgesture testgesture.c)
 add_executable(testgl2 testgl2.c)
 add_executable(testgles testgles.c)
 add_executable(testgles2 testgles2.c)
+add_executable(testguid testguid.c)
 add_executable(testhaptic testhaptic.c)
 add_executable(testhotplug testhotplug.c)
 add_executable(testrumble testrumble.c)
@@ -166,6 +167,7 @@ SET(ALL_TESTS
     testgl2
     testgles
     testgles2
+    testguid
     testhaptic
     testhittesting
     testhotplug
@@ -214,6 +216,7 @@ set(NONINTERACTIVE
     testatomic
     testerror
     testfilesystem
+    testguid
     testlocale
     testplatform
     testpower
@@ -313,35 +316,36 @@ if(PSP)
     # Build EBOOT files if building for PSP
     set(BUILD_EBOOT
         ${NEEDS_RESOURCES}
+        testatomic
+        testaudiocapture
+        testaudioinfo
         testbounds
-        testgl2
-        testsem
         testdisplayinfo
-        teststreaming
-        testgeometry
-        testfile
         testdraw2
-        testviewport
-        testhittesting
-        testoverlay2
-        testver
         testdrawchessboard
-        testsurround
-        testintersections
-        testmessage
-        testaudiocapture
         testerror
-        testatomic
-        testjoystick
-        testiconv
+        testfile
         testfilesystem
+        testgeometry
+        testgl2
+        testguid
+        testhittesting
+        testiconv
+        testintersections
+        testjoystick
+        testlock
+        testmessage
+        testoverlay2
         testplatform
-        testthread
+        testpower
         testqsort
-        testaudioinfo
-        testlock
+        testsem
+        teststreaming
+        testsurround
+        testthread
         testtimer
-        testpower
+        testver
+        testviewport
         testwm2
         torturethread
     )
diff --git a/test/Makefile.in b/test/Makefile.in
index e3e6db9..9fc38f2 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -40,6 +40,7 @@ TARGETS = \
 	testgamecontroller$(EXE) \
 	testgeometry$(EXE) \
 	testgesture$(EXE) \
+	testguid$(EXE) \
 	testhaptic$(EXE) \
 	testhittesting$(EXE) \
 	testhotplug$(EXE) \
@@ -201,7 +202,7 @@ testgeometry$(EXE): $(srcdir)/testgeometry.c $(srcdir)/testutils.c
 
 testgesture$(EXE): $(srcdir)/testgesture.c
 	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
- 
+
 testgl2$(EXE): $(srcdir)/testgl2.c
 	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
 
@@ -214,6 +215,9 @@ testgles2$(EXE): $(srcdir)/testgles2.c
 testgles2_sdf$(EXE): $(srcdir)/testgles2_sdf.c $(srcdir)/testutils.c
 	$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
 
+testguid$(EXE): $(srcdir)/testguid.c
+	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
+
 testhaptic$(EXE): $(srcdir)/testhaptic.c
 	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
 
diff --git a/test/testguid.c b/test/testguid.c
new file mode 100644
index 0000000..3b2b90f
--- /dev/null
+++ b/test/testguid.c
@@ -0,0 +1,169 @@
+/*
+  Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely.
+*/
+
+/**
+ * Automated tests for GUID processing
+ */
+
+#include <stdio.h>
+
+#include "SDL.h"
+
+/* Helpers */
+
+static int _error_count = 0;
+
+static int
+_require_eq(Uint64 expected, Uint64 actual, int line, char *msg)
+{
+    if (expected != actual) {
+        _error_count += 1;
+        SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[%s, L%d] %s: Actual %ld (0x%lx) != expected %ld (0x%lx)",
+                     __FILE__, line, msg, actual, actual, expected, expected);
+        return 0;
+    }
+    return 1;
+}
+
+#define ASSERT_EQ(MSG, EXPECTED, ACTUAL) _require_eq((EXPECTED), (ACTUAL), __LINE__, (MSG))
+
+/* Helpers */
+
+#define NUM_TEST_GUIDS 5
+
+static struct {
+    char *str;
+    Uint64 upper, lower;
+} test_guids[NUM_TEST_GUIDS] = {
+    { "0000000000000000"    "ffffffffffffffff",
+     0x0000000000000000,   0xfffffffffffffffflu },
+    { "0011223344556677"    "8091a2b3c4d5e6f0",
+     0x0011223344556677lu, 0x8091a2b3c4d5e6f0lu },
+    { "a011223344556677"    "8091a2b3c4d5e6f0",
+     0xa011223344556677lu, 0x8091a2b3c4d5e6f0lu },
+    { "a011223344556677"    "8091a2b3c4d5e6f1",
+     0xa011223344556677lu, 0x8091a2b3c4d5e6f1lu },
+    { "a011223344556677"    "8191a2b3c4d5e6f0",
+     0xa011223344556677lu, 0x8191a2b3c4d5e6f0lu },
+};
+
+static void
+upper_lower_to_bytestring(Uint8* out, Uint64 upper, Uint64 lower)
+{
+    Uint64 values[2];
+    int i, k;
+
+    values[0] = upper;
+    values [1] = lower;
+
+    for (i = 0; i < 2; ++i) {
+        Uint64 v = values[i];
+
+        for (k = 0; k < 8; ++k) {
+            *out++ = v >> 56;
+            v <<= 8;
+        }
+    }
+}
+
+/* ================= Test Case Implementation ================== */
+
+/**
+ * @brief Check String-to-GUID conversion
+ *
+ * @sa SDL_GUIDFromString
+ */
+static void
+TestGuidFromString(void)
+{
+    int i;
+
+    for (i = 0; i < NUM_TEST_GUIDS; ++i) {
+        Uint8 expected[16];
+        SDL_GUID guid;
+
+        upper_lower_to_bytestring(expected,
+                                  test_guids[i].upper, test_guids[i].lower);
+
+        guid = SDL_GUIDFromString(test_guids[i].str);
+        if (!ASSERT_EQ("GUID from string", 0, SDL_memcmp(expected, guid.data, 16))) {
+            SDL_Log("  GUID was: '%s'", test_guids[i].str);
+        }
+    }
+}
+
+/**
+ * @brief Check GUID-to-String conversion
+ *
+ * @sa SDL_GUIDToString
+ */
+static void
+TestGuidToString(void)
+{
+    int i;
+
+    for (i = 0; i < NUM_TEST_GUIDS; ++i) {
+        const int guid_str_offset = 4;
+        char guid_str_buf[64];
+        char *guid_str = guid_str_buf + guid_str_offset;
+        SDL_GUID guid;
+        int size;
+
+        upper_lower_to_bytestring(guid.data,
+                                  test_guids[i].upper, test_guids[i].lower);
+
+        /* Serialise to limited-length buffers */
+        for (size = 0; size <= 36; ++size) {
+            const Uint8 fill_char = size + 0xa0;
+            Uint32 expected_prefix;
+            Uint32 actual_prefix;
+            int written_size;
+
+            SDL_memset(guid_str_buf, fill_char, sizeof(guid_str_buf));
+            SDL_GUIDToString(guid, guid_str, size);
+
+            /* Check bytes before guid_str_buf */
+            expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (fill_char << 24);
+            SDL_memcpy(&actual_prefix, guid_str_buf, 4);
+            if (!ASSERT_EQ("String buffer memory before output untouched: ", expected_prefix, actual_prefix)) {
+                SDL_Log("  at size=%d", size);
+            }
+
+            /* Check that we did not overwrite too much */
+            written_size = 0;
+            while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
+                ++written_size;
+            }
+            if (!ASSERT_EQ("Output length is within expected bounds", 1, written_size <= size)) {
+                SDL_Log("  with lenght %d: wrote %d of %d permitted bytes",
+                        size, written_size, size);
+            }
+            if (size >= 33) {
+                if (!ASSERT_EQ("GUID string equality", 0, SDL_strcmp(guid_str, test_guids[i].str))) {
+                    SDL_Log("  from string: %s", test_guids[i].str);
+                }
+            }
+        }
+    }
+}
+
+int
+main(int argc, char *argv[])
+{
+    /* Enable standard application logging */
+    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+    TestGuidFromString();
+    TestGuidToString();
+
+    return _error_count > 0;
+}
diff --git a/test/watcom.mif b/test/watcom.mif
index dbeec5d..ad759a8 100644
--- a/test/watcom.mif
+++ b/test/watcom.mif
@@ -11,7 +11,7 @@ TARGETS = testatomic.exe testdisplayinfo.exe testbounds.exe testdraw2.exe &
           testdrawchessboard.exe testdropfile.exe testerror.exe testfile.exe &
           testfilesystem.exe testgamecontroller.exe testgeometry.exe testgesture.exe &
           testhittesting.exe testhotplug.exe testiconv.exe testime.exe testlocale.exe &
-          testintersections.exe testjoystick.exe testkeys.exe testloadso.exe &
+          testguid.exe testintersections.exe testjoystick.exe testkeys.exe testloadso.exe &
           testlock.exe testmessage.exe testoverlay2.exe testplatform.exe &
           testpower.exe testsensor.exe testrelative.exe testrendercopyex.exe &
           testrendertarget.exe testrumble.exe testscale.exe testsem.exe &