MemLibGuid.c 3.8 KB

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