xref: /netbsd-src/sys/external/bsd/acpica/dist/utilities/utascii.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1cfbb7280Schristos /******************************************************************************
2cfbb7280Schristos  *
3cfbb7280Schristos  * Module Name: utascii - Utility ascii functions
4cfbb7280Schristos  *
5cfbb7280Schristos  *****************************************************************************/
6cfbb7280Schristos 
7cfbb7280Schristos /*
8*046a2985Schristos  * Copyright (C) 2000 - 2023, Intel Corp.
9cfbb7280Schristos  * All rights reserved.
10cfbb7280Schristos  *
11cfbb7280Schristos  * Redistribution and use in source and binary forms, with or without
12cfbb7280Schristos  * modification, are permitted provided that the following conditions
13cfbb7280Schristos  * are met:
14cfbb7280Schristos  * 1. Redistributions of source code must retain the above copyright
15cfbb7280Schristos  *    notice, this list of conditions, and the following disclaimer,
16cfbb7280Schristos  *    without modification.
17cfbb7280Schristos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18cfbb7280Schristos  *    substantially similar to the "NO WARRANTY" disclaimer below
19cfbb7280Schristos  *    ("Disclaimer") and any redistribution must be conditioned upon
20cfbb7280Schristos  *    including a substantially similar Disclaimer requirement for further
21cfbb7280Schristos  *    binary redistribution.
22cfbb7280Schristos  * 3. Neither the names of the above-listed copyright holders nor the names
23cfbb7280Schristos  *    of any contributors may be used to endorse or promote products derived
24cfbb7280Schristos  *    from this software without specific prior written permission.
25cfbb7280Schristos  *
26cfbb7280Schristos  * Alternatively, this software may be distributed under the terms of the
27cfbb7280Schristos  * GNU General Public License ("GPL") version 2 as published by the Free
28cfbb7280Schristos  * Software Foundation.
29cfbb7280Schristos  *
30cfbb7280Schristos  * NO WARRANTY
31cfbb7280Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32cfbb7280Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3346a330b4Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34cfbb7280Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35cfbb7280Schristos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36cfbb7280Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37cfbb7280Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38cfbb7280Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39cfbb7280Schristos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40cfbb7280Schristos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41cfbb7280Schristos  * POSSIBILITY OF SUCH DAMAGES.
42cfbb7280Schristos  */
43cfbb7280Schristos 
44cfbb7280Schristos #include "acpi.h"
45cfbb7280Schristos #include "accommon.h"
46cfbb7280Schristos 
47cfbb7280Schristos 
48cfbb7280Schristos /*******************************************************************************
49cfbb7280Schristos  *
50cfbb7280Schristos  * FUNCTION:    AcpiUtValidNameseg
51cfbb7280Schristos  *
52cfbb7280Schristos  * PARAMETERS:  Name            - The name or table signature to be examined.
53cfbb7280Schristos  *                                Four characters, does not have to be a
54cfbb7280Schristos  *                                NULL terminated string.
55cfbb7280Schristos  *
56cfbb7280Schristos  * RETURN:      TRUE if signature is has 4 valid ACPI characters
57cfbb7280Schristos  *
58cfbb7280Schristos  * DESCRIPTION: Validate an ACPI table signature.
59cfbb7280Schristos  *
60cfbb7280Schristos  ******************************************************************************/
61cfbb7280Schristos 
62cfbb7280Schristos BOOLEAN
AcpiUtValidNameseg(char * Name)63cfbb7280Schristos AcpiUtValidNameseg (
64cfbb7280Schristos     char                    *Name)
65cfbb7280Schristos {
66cfbb7280Schristos     UINT32                  i;
67cfbb7280Schristos 
68cfbb7280Schristos 
69cfbb7280Schristos     /* Validate each character in the signature */
70cfbb7280Schristos 
7194783addSchristos     for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
72cfbb7280Schristos     {
73cfbb7280Schristos         if (!AcpiUtValidNameChar (Name[i], i))
74cfbb7280Schristos         {
75cfbb7280Schristos             return (FALSE);
76cfbb7280Schristos         }
77cfbb7280Schristos     }
78cfbb7280Schristos 
79cfbb7280Schristos     return (TRUE);
80cfbb7280Schristos }
81cfbb7280Schristos 
82cfbb7280Schristos 
83cfbb7280Schristos /*******************************************************************************
84cfbb7280Schristos  *
85cfbb7280Schristos  * FUNCTION:    AcpiUtValidNameChar
86cfbb7280Schristos  *
87cfbb7280Schristos  * PARAMETERS:  Char            - The character to be examined
88cfbb7280Schristos  *              Position        - Byte position (0-3)
89cfbb7280Schristos  *
90cfbb7280Schristos  * RETURN:      TRUE if the character is valid, FALSE otherwise
91cfbb7280Schristos  *
92cfbb7280Schristos  * DESCRIPTION: Check for a valid ACPI character. Must be one of:
93cfbb7280Schristos  *              1) Upper case alpha
94cfbb7280Schristos  *              2) numeric
95cfbb7280Schristos  *              3) underscore
96cfbb7280Schristos  *
97cfbb7280Schristos  *              We allow a '!' as the last character because of the ASF! table
98cfbb7280Schristos  *
99cfbb7280Schristos  ******************************************************************************/
100cfbb7280Schristos 
101cfbb7280Schristos BOOLEAN
AcpiUtValidNameChar(char Character,UINT32 Position)102cfbb7280Schristos AcpiUtValidNameChar (
103cfbb7280Schristos     char                    Character,
104cfbb7280Schristos     UINT32                  Position)
105cfbb7280Schristos {
106cfbb7280Schristos 
107cfbb7280Schristos     if (!((Character >= 'A' && Character <= 'Z') ||
108cfbb7280Schristos           (Character >= '0' && Character <= '9') ||
109cfbb7280Schristos           (Character == '_')))
110cfbb7280Schristos     {
111cfbb7280Schristos         /* Allow a '!' in the last position */
112cfbb7280Schristos 
113cfbb7280Schristos         if (Character == '!' && Position == 3)
114cfbb7280Schristos         {
115cfbb7280Schristos             return (TRUE);
116cfbb7280Schristos         }
117cfbb7280Schristos 
118cfbb7280Schristos         return (FALSE);
119cfbb7280Schristos     }
120cfbb7280Schristos 
121cfbb7280Schristos     return (TRUE);
122cfbb7280Schristos }
123cfbb7280Schristos 
124cfbb7280Schristos 
125cfbb7280Schristos /*******************************************************************************
126cfbb7280Schristos  *
127cfbb7280Schristos  * FUNCTION:    AcpiUtCheckAndRepairAscii
128cfbb7280Schristos  *
129cfbb7280Schristos  * PARAMETERS:  Name                - Ascii string
130cfbb7280Schristos  *              Count               - Number of characters to check
131cfbb7280Schristos  *
132cfbb7280Schristos  * RETURN:      None
133cfbb7280Schristos  *
134cfbb7280Schristos  * DESCRIPTION: Ensure that the requested number of characters are printable
135cfbb7280Schristos  *              Ascii characters. Sets non-printable and null chars to <space>.
136cfbb7280Schristos  *
137cfbb7280Schristos  ******************************************************************************/
138cfbb7280Schristos 
139cfbb7280Schristos void
AcpiUtCheckAndRepairAscii(UINT8 * Name,char * RepairedName,UINT32 Count)140cfbb7280Schristos AcpiUtCheckAndRepairAscii (
141cfbb7280Schristos     UINT8                   *Name,
142cfbb7280Schristos     char                    *RepairedName,
143cfbb7280Schristos     UINT32                  Count)
144cfbb7280Schristos {
145cfbb7280Schristos     UINT32                  i;
146cfbb7280Schristos 
147cfbb7280Schristos 
148cfbb7280Schristos     for (i = 0; i < Count; i++)
149cfbb7280Schristos     {
150cfbb7280Schristos         RepairedName[i] = (char) Name[i];
151cfbb7280Schristos 
152cfbb7280Schristos         if (!Name[i])
153cfbb7280Schristos         {
154cfbb7280Schristos             return;
155cfbb7280Schristos         }
156cfbb7280Schristos         if (!isprint (Name[i]))
157cfbb7280Schristos         {
158cfbb7280Schristos             RepairedName[i] = ' ';
159cfbb7280Schristos         }
160cfbb7280Schristos     }
161cfbb7280Schristos }
162