1 /** @file
2   Implementation of GUID functions.
3 
4   The following BaseMemoryLib instances contain the same copy of this file:
5 
6     BaseMemoryLib
7     BaseMemoryLibMmx
8     BaseMemoryLibSse2
9     BaseMemoryLibRepStr
10     BaseMemoryLibOptDxe
11     BaseMemoryLibOptPei
12     PeiMemoryLib
13     UefiMemoryLib
14 
15   Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
16   This program and the accompanying materials
17   are licensed and made available under the terms and conditions of the BSD License
18   which accompanies this distribution.  The full text of the license may be found at
19   http://opensource.org/licenses/bsd-license.php
20 
21   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 
24 **/
25 
26 #include "MemLibInternals.h"
27 
28 /**
29   Copies a source GUID to a destination GUID.
30 
31   This function copies the contents of the 128-bit GUID specified by SourceGuid to
32   DestinationGuid, and returns DestinationGuid.
33 
34   If DestinationGuid is NULL, then ASSERT().
35   If SourceGuid is NULL, then ASSERT().
36 
37   @param  DestinationGuid   Pointer to the destination GUID.
38   @param  SourceGuid        Pointer to the source GUID.
39 
40   @return DestinationGuid.
41 
42 **/
43 GUID *
44 EFIAPI
CopyGuid(OUT GUID * DestinationGuid,IN CONST GUID * SourceGuid)45 CopyGuid (
46   OUT GUID       *DestinationGuid,
47   IN CONST GUID  *SourceGuid
48   )
49 {
50   WriteUnaligned64 (
51     (UINT64*)DestinationGuid,
52     ReadUnaligned64 ((CONST UINT64*)SourceGuid)
53     );
54   WriteUnaligned64 (
55     (UINT64*)DestinationGuid + 1,
56     ReadUnaligned64 ((CONST UINT64*)SourceGuid + 1)
57     );
58   return DestinationGuid;
59 }
60 
61 /**
62   Compares two GUIDs.
63 
64   This function compares Guid1 to Guid2.  If the GUIDs are identical then TRUE is returned.
65   If there are any bit differences in the two GUIDs, then FALSE is returned.
66 
67   If Guid1 is NULL, then ASSERT().
68   If Guid2 is NULL, then ASSERT().
69 
70   @param  Guid1       A pointer to a 128 bit GUID.
71   @param  Guid2       A pointer to a 128 bit GUID.
72 
73   @retval TRUE        Guid1 and Guid2 are identical.
74   @retval FALSE       Guid1 and Guid2 are not identical.
75 
76 **/
77 BOOLEAN
78 EFIAPI
CompareGuid(IN CONST GUID * Guid1,IN CONST GUID * Guid2)79 CompareGuid (
80   IN CONST GUID  *Guid1,
81   IN CONST GUID  *Guid2
82   )
83 {
84   return (CompareMem(Guid1, Guid2, sizeof(GUID) == 0)) ? TRUE : FALSE;
85 }
86 
87 /**
88   Scans a target buffer for a GUID, and returns a pointer to the matching GUID
89   in the target buffer.
90 
91   This function searches the target buffer specified by Buffer and Length from
92   the lowest address to the highest address at 128-bit increments for the 128-bit
93   GUID value that matches Guid.  If a match is found, then a pointer to the matching
94   GUID in the target buffer is returned.  If no match is found, then NULL is returned.
95   If Length is 0, then NULL is returned.
96 
97   If Length > 0 and Buffer is NULL, then ASSERT().
98   If Buffer is not aligned on a 32-bit boundary, then ASSERT().
99   If Length is not aligned on a 128-bit boundary, then ASSERT().
100   If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
101 
102   @param  Buffer  Pointer to the target buffer to scan.
103   @param  Length  Number of bytes in Buffer to scan.
104   @param  Guid    Value to search for in the target buffer.
105 
106   @return A pointer to the matching Guid in the target buffer or NULL otherwise.
107 
108 **/
109 VOID *
110 EFIAPI
ScanGuid(IN CONST VOID * Buffer,IN UINTN Length,IN CONST GUID * Guid)111 ScanGuid (
112   IN CONST VOID  *Buffer,
113   IN UINTN       Length,
114   IN CONST GUID  *Guid
115   )
116 {
117   CONST GUID                        *GuidPtr;
118 
119   ASSERT (((UINTN)Buffer & (sizeof (Guid->Data1) - 1)) == 0);
120   ASSERT (Length <= (MAX_ADDRESS - (UINTN)Buffer + 1));
121   ASSERT ((Length & (sizeof (*GuidPtr) - 1)) == 0);
122 
123   GuidPtr = (GUID*)Buffer;
124   Buffer  = GuidPtr + Length / sizeof (*GuidPtr);
125   while (GuidPtr < (CONST GUID*)Buffer) {
126     if (CompareGuid (GuidPtr, Guid)) {
127       return (VOID*)GuidPtr;
128     }
129     GuidPtr++;
130   }
131   return NULL;
132 }
133