test: guid.c Source File

Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# guid.c

Go to the documentation of this file.

    00001 /*++
    00002
    00003 Copyright (c) 1996  Microsoft Corporation
    00004
    00005 Module Name:
    00006
    00007     guid.c
    00008
    00009 Abstract:
    00010
    00011     This Module implements the guid manipulation functions.
    00012
    00013 Author:
    00014
    00015     George Shaw (GShaw) 9-Oct-1996
    00016
    00017 Environment:
    00018
    00019     Pure Runtime Library Routine
    00020
    00021 Revision History:
    00022
    00023 --*/
    00024
    00025 #include "nt.h"
    00026 #include "ntrtlp.h"
    00027
    00028 #if defined(ALLOC_PRAGMA) && defined(NTOS_KERNEL_RUNTIME)
    00029 static
    00030 int
    00031 __cdecl
    00032 ScanHexFormat(
    00033     IN const WCHAR* Buffer,
    00034     IN ULONG MaximumLength,
    00035     IN const WCHAR* Format,
    00036     ...);
    00037
    00038 #pragma alloc_text(PAGE, RtlStringFromGUID)
    00039 #pragma alloc_text(PAGE, ScanHexFormat)
    00040 #pragma alloc_text(PAGE, RtlGUIDFromString)
    00041 #endif // ALLOC_PRAGMA && NTOS_KERNEL_RUNTIME
    00042
    00043 extern const WCHAR GuidFormat[];
    00044
    00045 #define GUID_STRING_SIZE 38
    00046
    00047
    00048 NTSYSAPI
    00049 NTSTATUS
    00050 NTAPI
    00051 RtlStringFromGUID(
    00052     IN REFGUID Guid,
    00053     OUT PUNICODE_STRING GuidString
    00054     )
    00055 /*++
    00056
    00057 Routine Description:
    00058
    00059     Constructs the standard string version of a GUID, in the form:
    00060     "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".
    00061
    00062 Arguments:
    00063
    00064     Guid -
    00065         Contains the GUID to translate.
    00066
    00067     GuidString -
    00068         Returns a string that represents the textual format of the GUID.
    00069         Caller must call RtlFreeUnicodeString to free the buffer when done with
    00070         it.
    00071
    00072 Return Value:
    00073
    00074     NTSTATUS - Returns STATUS_SUCCESS if the user string was succesfully
    00075     initialized.
    00076
    00077 --*/
    00078 {
    00079     RTL_PAGED_CODE();
    00080     GuidString->Length = GUID_STRING_SIZE * sizeof(WCHAR);
    00081     GuidString->MaximumLength = GuidString->Length + sizeof(UNICODE_NULL);
    00082     if (!(GuidString->Buffer = RtlAllocateStringRoutine(GuidString->MaximumLength))) {
    00083         return STATUS_NO_MEMORY;
    00084     }
    00085     swprintf(GuidString->Buffer, GuidFormat, Guid->Data1, Guid->Data2, Guid->Data3, Guid->Data4[0], Guid->Data4[1], Guid->Data4[2], Guid->Data4[3], Guid->Data4[4], Guid->Data4[5], Guid->Data4[6], Guid->Data4[7]);
    00086     return STATUS_SUCCESS;
    00087 }
    00088
    00089
    00090 static
    00091 int
    00092 __cdecl
    00093 ScanHexFormat(
    00094     IN const WCHAR* Buffer,
    00095     IN ULONG MaximumLength,
    00096     IN const WCHAR* Format,
    00097     ...)
    00098 /*++
    00099
    00100 Routine Description:
    00101
    00102     Scans a source Buffer and places values from that buffer into the parameters
    00103     as specified by Format.
    00104
    00105 Arguments:
    00106
    00107     Buffer -
    00108         Contains the source buffer which is to be scanned.
    00109
    00110     MaximumLength -
    00111         Contains the maximum length in characters for which Buffer is searched.
    00112         This implies that Buffer need not be UNICODE_NULL terminated.
    00113
    00114     Format -
    00115         Contains the format string which defines both the acceptable string format
    00116         contained in Buffer, and the variable parameters which follow.
    00117
    00118 Return Value:
    00119
    00120     Returns the number of parameters filled if the end of the Buffer is reached,
    00121     else -1 on an error.
    00122
    00123 --*/
    00124 {
    00125     va_list ArgList;
    00126     int     FormatItems;
    00127
    00128     va_start(ArgList, Format);
    00129     for (FormatItems = 0;;) {
    00130         switch (*Format) {
    00131         case 0:
    00132             return (MaximumLength && *Buffer) ? -1 : FormatItems;
    00133         case '%':
    00134             Format++;
    00135             if (*Format != '%') {
    00136                 ULONG   Number;
    00137                 int     Width;
    00138                 int     Long;
    00139                 PVOID   Pointer;
    00140
    00141                 for (Long = 0, Width = 0;; Format++) {
    00142                     if ((*Format >= '0') && (*Format <= '9')) {
    00143                         Width = Width * 10 + *Format - '0';
    00144                     } else if (*Format == 'l') {
    00145                         Long++;
    00146                     } else if ((*Format == 'X') || (*Format == 'x')) {
    00147                         break;
    00148                     }
    00149                 }
    00150                 Format++;
    00151                 for (Number = 0; Width--; Buffer++, MaximumLength--) {
    00152                     if (!MaximumLength)
    00153                         return -1;
    00154                     Number *= 16;
    00155                     if ((*Buffer >= '0') && (*Buffer <= '9')) {
    00156                         Number += (*Buffer - '0');
    00157                     } else if ((*Buffer >= 'a') && (*Buffer <= 'f')) {
    00158                         Number += (*Buffer - 'a' + 10);
    00159                     } else if ((*Buffer >= 'A') && (*Buffer <= 'F')) {
    00160                         Number += (*Buffer - 'A' + 10);
    00161                     } else {
    00162                         return -1;
    00163                     }
    00164                 }
    00165                 Pointer = va_arg(ArgList, PVOID);
    00166                 if (Long) {
    00167                     *(PULONG)Pointer = Number;
    00168                 } else {
    00169                     *(PUSHORT)Pointer = (USHORT)Number;
    00170                 }
    00171                 FormatItems++;
    00172                 break;
    00173             }
    00174             /* no break */
    00175         default:
    00176             if (!MaximumLength || (*Buffer != *Format)) {
    00177                 return -1;
    00178             }
    00179             Buffer++;
    00180             MaximumLength--;
    00181             Format++;
    00182             break;
    00183         }
    00184     }
    00185 }
    00186
    00187
    00188 NTSYSAPI
    00189 NTSTATUS
    00190 NTAPI
    00191 RtlGUIDFromString(
    00192     IN PUNICODE_STRING GuidString,
    00193     OUT GUID* Guid
    00194     )
    00195 /*++
    00196
    00197 Routine Description:
    00198
    00199     Retrieves a the binary format of a textual GUID presented in the standard
    00200     string version of a GUID: "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".
    00201
    00202 Arguments:
    00203
    00204     GuidString -
    00205         Place from which to retrieve the textual form of the GUID.
    00206
    00207     Guid -
    00208         Place in which to put the binary form of the GUID.
    00209
    00210 Return Value:
    00211
    00212     Returns STATUS_SUCCESS if the buffer contained a valid GUID, else
    00213     STATUS_INVALID_PARAMETER if the string was invalid.
    00214
    00215 --*/
    00216 {
    00217     USHORT    Data4[8];
    00218     int       Count;
    00219
    00220     RTL_PAGED_CODE();
    00221     if (ScanHexFormat(GuidString->Buffer, GuidString->Length / sizeof(WCHAR), GuidFormat, &Guid->Data1, &Guid->Data2, &Guid->Data3, &Data4[0], &Data4[1], &Data4[2], &Data4[3], &Data4[4], &Data4[5], &Data4[6], &Data4[7]) == -1) {
    00222         return STATUS_INVALID_PARAMETER;
    00223     }
    00224     for (Count = 0; Count < sizeof(Data4)/sizeof(Data4[0]); Count++) {
    00225         Guid->Data4[Count] = (UCHAR)Data4[Count];
    00226     }
    00227     return STATUS_SUCCESS;
    00228 }