asluuid.c revision 245582
153913Sarchie/******************************************************************************
253913Sarchie *
353913Sarchie * Module Name: asluuid-- compiler UUID support
453913Sarchie *
553913Sarchie *****************************************************************************/
653913Sarchie
753913Sarchie/*
853913Sarchie * Copyright (C) 2000 - 2013, Intel Corp.
953913Sarchie * All rights reserved.
1053913Sarchie *
1153913Sarchie * Redistribution and use in source and binary forms, with or without
1253913Sarchie * modification, are permitted provided that the following conditions
1353913Sarchie * are met:
1453913Sarchie * 1. Redistributions of source code must retain the above copyright
1553913Sarchie *    notice, this list of conditions, and the following disclaimer,
1653913Sarchie *    without modification.
1753913Sarchie * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1853913Sarchie *    substantially similar to the "NO WARRANTY" disclaimer below
1953913Sarchie *    ("Disclaimer") and any redistribution must be conditioned upon
2053913Sarchie *    including a substantially similar Disclaimer requirement for further
2153913Sarchie *    binary redistribution.
2253913Sarchie * 3. Neither the names of the above-listed copyright holders nor the names
2353913Sarchie *    of any contributors may be used to endorse or promote products derived
2453913Sarchie *    from this software without specific prior written permission.
2553913Sarchie *
2653913Sarchie * Alternatively, this software may be distributed under the terms of the
2753913Sarchie * GNU General Public License ("GPL") version 2 as published by the Free
2853913Sarchie * Software Foundation.
2953913Sarchie *
3053913Sarchie * NO WARRANTY
3153913Sarchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3253913Sarchie * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3353913Sarchie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3453913Sarchie * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3553913Sarchie * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3653913Sarchie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3767506Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3853913Sarchie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3953913Sarchie * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4053913Sarchie * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4153913Sarchie * POSSIBILITY OF SUCH DAMAGES.
4253913Sarchie */
4353913Sarchie
4453913Sarchie
4553913Sarchie#include <contrib/dev/acpica/compiler/aslcompiler.h>
4670870Sjulian
4753913Sarchie#define _COMPONENT          ACPI_COMPILER
4853913Sarchie        ACPI_MODULE_NAME    ("asluuid")
4953913Sarchie
5053913Sarchie
5153913Sarchie/*
5253913Sarchie * UUID support functions.
5353913Sarchie *
5453913Sarchie * This table is used to convert an input UUID ascii string to a 16 byte
5553913Sarchie * buffer and the reverse. The table maps a UUID buffer index 0-15 to
5653913Sarchie * the index within the 36-byte UUID string where the associated 2-byte
5770870Sjulian * hex value can be found.
5870870Sjulian *
5970870Sjulian * 36-byte UUID strings are of the form:
6070870Sjulian *     aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
6170870Sjulian * Where aa-pp are one byte hex numbers, made up of two hex digits
6270870Sjulian *
6353913Sarchie * Note: This table is basically the inverse of the string-to-offset table
6453913Sarchie * found in the ACPI spec in the description of the ToUUID macro.
6553913Sarchie */
6653913Sarchiestatic UINT8    Gbl_MapToUuidOffset[16] =
6753913Sarchie{
6853913Sarchie    6,4,2,0,11,9,16,14,19,21,24,26,28,30,32,34
6953913Sarchie};
7053913Sarchie
7153913Sarchie#define UUID_BUFFER_LENGTH          16
7253913Sarchie#define UUID_STRING_LENGTH          36
7353913Sarchie
7453913Sarchie/* Positions for required hyphens (dashes) in UUID strings */
7553913Sarchie
7653913Sarchie#define UUID_HYPHEN1_OFFSET         8
7753913Sarchie#define UUID_HYPHEN2_OFFSET         13
7853913Sarchie#define UUID_HYPHEN3_OFFSET         18
7953913Sarchie#define UUID_HYPHEN4_OFFSET         23
8053913Sarchie
8153913Sarchie
8253913Sarchie/*******************************************************************************
8353913Sarchie *
8464505Sarchie * FUNCTION:    AuValiduateUuid
8564505Sarchie *
8664505Sarchie * PARAMETERS:  InString            - 36-byte formatted UUID string
8764505Sarchie *
8864505Sarchie * RETURN:      Status
8953913Sarchie *
9053913Sarchie * DESCRIPTION: Check all 36 characters for correct format
9153913Sarchie *
9253913Sarchie ******************************************************************************/
9353913Sarchie
9453913SarchieACPI_STATUS
9553913SarchieAuValidateUuid (
9653913Sarchie    char                    *InString)
9753913Sarchie{
9853913Sarchie    UINT32                  i;
9953913Sarchie
10053913Sarchie
10153913Sarchie    if (!InString || (ACPI_STRLEN (InString) != UUID_STRING_LENGTH))
10253913Sarchie    {
10353913Sarchie        return (AE_BAD_PARAMETER);
10453913Sarchie    }
10553913Sarchie
10653913Sarchie    /* Check all 36 characters for correct format */
10753913Sarchie
10853913Sarchie    for (i = 0; i < UUID_STRING_LENGTH; i++)
10953913Sarchie    {
11053913Sarchie        /* Must have 4 hyphens (dashes) in these positions: */
11153913Sarchie
11253913Sarchie        if ((i == UUID_HYPHEN1_OFFSET) ||
11353913Sarchie            (i == UUID_HYPHEN2_OFFSET) ||
11453913Sarchie            (i == UUID_HYPHEN3_OFFSET) ||
11553913Sarchie            (i == UUID_HYPHEN4_OFFSET))
11653913Sarchie        {
11753913Sarchie            if (InString[i] != '-')
11853913Sarchie            {
11953913Sarchie                return (AE_BAD_PARAMETER);
12053913Sarchie            }
12153913Sarchie        }
12253913Sarchie
12353913Sarchie        /* All other positions must contain hex digits */
12453913Sarchie
12553913Sarchie        else
12653913Sarchie        {
12753913Sarchie            if (!isxdigit ((int) InString[i]))
12853913Sarchie            {
12953913Sarchie                return (AE_BAD_PARAMETER);
13053913Sarchie            }
13153913Sarchie        }
13253913Sarchie    }
13353913Sarchie
13453913Sarchie    return (AE_OK);
13553913Sarchie}
13653913Sarchie
13753913Sarchie
13853913Sarchie/*******************************************************************************
13953913Sarchie *
14053913Sarchie * FUNCTION:    AuConvertStringToUuid
14153913Sarchie *
14253913Sarchie * PARAMETERS:  InString            - 36-byte formatted UUID string
14353913Sarchie *              UuidBuffer          - 16-byte UUID buffer
14453913Sarchie *
14553913Sarchie * RETURN:      Status
14653913Sarchie *
14753913Sarchie * DESCRIPTION: Convert 36-byte formatted UUID string to 16-byte UUID buffer
14853913Sarchie *
14953913Sarchie ******************************************************************************/
15053913Sarchie
15153913SarchieACPI_STATUS
15253913SarchieAuConvertStringToUuid (
15353913Sarchie    char                    *InString,
15453913Sarchie    char                    *UuidBuffer)
15553913Sarchie{
15653913Sarchie    UINT32                  i;
15753913Sarchie
15853913Sarchie
15953913Sarchie    if (!InString || !UuidBuffer)
16053913Sarchie    {
16153913Sarchie        return (AE_BAD_PARAMETER);
16253913Sarchie    }
16353913Sarchie
16453913Sarchie    for (i = 0; i < UUID_BUFFER_LENGTH; i++)
16553913Sarchie    {
16653913Sarchie        UuidBuffer[i]  = (char) (UtHexCharToValue (InString[Gbl_MapToUuidOffset[i]]) << 4);
16753913Sarchie        UuidBuffer[i] |= (char)  UtHexCharToValue (InString[Gbl_MapToUuidOffset[i] + 1]);
16853913Sarchie    }
16953913Sarchie
17053913Sarchie    return (AE_OK);
17153913Sarchie}
17253913Sarchie
17353913Sarchie
17453913Sarchie/*******************************************************************************
17553913Sarchie *
17653913Sarchie * FUNCTION:    AuConvertUuidToString
17753913Sarchie *
17853913Sarchie * PARAMETERS:  UuidBuffer          - 16-byte UUID buffer
17953913Sarchie *              OutString           - 36-byte formatted UUID string
18053913Sarchie *
18153913Sarchie * RETURN:      Status
18253913Sarchie *
18353913Sarchie * DESCRIPTION: Convert 16-byte UUID buffer to 36-byte formatted UUID string
18453913Sarchie *              OutString must be 37 bytes to include null terminator.
18553913Sarchie *
18653913Sarchie ******************************************************************************/
18753913Sarchie
18853913SarchieACPI_STATUS
18953913SarchieAuConvertUuidToString (
19053913Sarchie    char                    *UuidBuffer,
19153913Sarchie    char                    *OutString)
19253913Sarchie{
19353913Sarchie    UINT32                  i;
19453913Sarchie
19553913Sarchie
19653913Sarchie    if (!UuidBuffer || !OutString)
19753913Sarchie    {
19853913Sarchie        return (AE_BAD_PARAMETER);
19953913Sarchie    }
20053913Sarchie
20153913Sarchie    for (i = 0; i < UUID_BUFFER_LENGTH; i++)
20253913Sarchie    {
20353913Sarchie        OutString[Gbl_MapToUuidOffset[i]] =     (UINT8) AslHexLookup[(UuidBuffer[i] >> 4) & 0xF];
20453913Sarchie        OutString[Gbl_MapToUuidOffset[i] + 1] = (UINT8) AslHexLookup[UuidBuffer[i] & 0xF];
20553913Sarchie    }
20653913Sarchie
20753913Sarchie    /* Insert required hyphens (dashes) */
20853913Sarchie
20953913Sarchie    OutString[UUID_HYPHEN1_OFFSET] =
21053913Sarchie    OutString[UUID_HYPHEN2_OFFSET] =
21153913Sarchie    OutString[UUID_HYPHEN3_OFFSET] =
21253913Sarchie    OutString[UUID_HYPHEN4_OFFSET] = '-';
21353913Sarchie
21453913Sarchie    OutString[UUID_STRING_LENGTH] = 0; /* Null terminate */
21553913Sarchie    return (AE_OK);
21653913Sarchie}
21753913Sarchie