1 /******************************************************************************
3 * Module Name: aslascii - ASCII detection and support routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/acapps.h>
47 #define _COMPONENT ACPI_COMPILER
48 ACPI_MODULE_NAME ("aslascii")
51 /* Local prototypes */
54 FlConsumeAnsiComment (
56 ASL_FILE_STATUS *Status);
61 ASL_FILE_STATUS *Status);
64 /*******************************************************************************
66 * FUNCTION: FlCheckForAcpiTable
68 * PARAMETERS: Handle - Open input file
72 * DESCRIPTION: Determine if a file seems to be a binary ACPI table, via the
73 * following checks on what would be the table header:
74 * 0) File must be at least as long as an ACPI_TABLE_HEADER
75 * 1) The header length field must match the file size
76 * 2) Signature, OemId, OemTableId, AslCompilerId must be ASCII
78 ******************************************************************************/
84 ACPI_TABLE_HEADER Table;
90 /* Read a potential table header */
92 Actual = fread (&Table, 1, sizeof (ACPI_TABLE_HEADER), Handle);
93 fseek (Handle, 0, SEEK_SET);
95 if (Actual < sizeof (ACPI_TABLE_HEADER))
100 /* Header length field must match the file size */
102 FileSize = CmGetFileSize (Handle);
103 if (Table.Length != FileSize)
109 * These fields must be ASCII:
110 * Signature, OemId, OemTableId, AslCompilerId.
111 * We allow a NULL terminator in OemId and OemTableId.
113 for (i = 0; i < ACPI_NAME_SIZE; i++)
115 if (!ACPI_IS_ASCII ((UINT8) Table.Signature[i]))
120 if (!ACPI_IS_ASCII ((UINT8) Table.AslCompilerId[i]))
126 for (i = 0; (i < ACPI_OEM_ID_SIZE) && (Table.OemId[i]); i++)
128 if (!ACPI_IS_ASCII ((UINT8) Table.OemId[i]))
134 for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (Table.OemTableId[i]); i++)
136 if (!ACPI_IS_ASCII ((UINT8) Table.OemTableId[i]))
142 printf ("Binary file appears to be a valid ACPI table, disassembling\n");
147 /*******************************************************************************
149 * FUNCTION: FlCheckForAscii
151 * PARAMETERS: Handle - Open input file
152 * Filename - Input filename
153 * DisplayErrors - TRUE if error messages desired
157 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
158 * within comments. Note: does not handle nested comments and does
159 * not handle comment delimiters within string literals. However,
160 * on the rare chance this happens and an invalid character is
161 * missed, the parser will catch the error by failing in some
162 * spectactular manner.
164 ******************************************************************************/
170 BOOLEAN DisplayErrors)
173 ACPI_SIZE BadBytes = 0;
174 BOOLEAN OpeningComment = FALSE;
175 ASL_FILE_STATUS Status;
181 /* Read the entire file */
183 while (fread (&Byte, 1, 1, Handle) == 1)
185 /* Ignore comment fields (allow non-ascii within) */
189 /* Check for second comment open delimiter */
193 FlConsumeAnsiComment (Handle, &Status);
198 FlConsumeNewComment (Handle, &Status);
203 OpeningComment = FALSE;
205 else if (Byte == '/')
207 OpeningComment = TRUE;
210 /* Check for an ASCII character */
212 if (!ACPI_IS_ASCII (Byte))
214 if ((BadBytes < 10) && (DisplayErrors))
217 "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
218 Byte, Status.Line, Status.Offset);
224 /* Update line counter */
226 else if (Byte == 0x0A)
234 /* Seek back to the beginning of the source file */
236 fseek (Handle, 0, SEEK_SET);
238 /* Were there any non-ASCII characters in the file? */
245 "%u non-ASCII characters found in input source text, could be a binary file\n",
247 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
250 return (AE_BAD_CHARACTER);
253 /* File is OK (100% ASCII) */
259 /*******************************************************************************
261 * FUNCTION: FlConsumeAnsiComment
263 * PARAMETERS: Handle - Open input file
264 * Status - File current status struct
266 * RETURN: Number of lines consumed
268 * DESCRIPTION: Step over a normal slash-star type comment
270 ******************************************************************************/
273 FlConsumeAnsiComment (
275 ASL_FILE_STATUS *Status)
278 BOOLEAN ClosingComment = FALSE;
281 while (fread (&Byte, 1, 1, Handle) == 1)
283 /* Scan until comment close is found */
296 ClosingComment = FALSE;
299 else if (Byte == '*')
301 ClosingComment = TRUE;
304 /* Maintain line count */
316 /*******************************************************************************
318 * FUNCTION: FlConsumeNewComment
320 * PARAMETERS: Handle - Open input file
321 * Status - File current status struct
323 * RETURN: Number of lines consumed
325 * DESCRIPTION: Step over a slash-slash type of comment
327 ******************************************************************************/
330 FlConsumeNewComment (
332 ASL_FILE_STATUS *Status)
337 while (fread (&Byte, 1, 1, Handle) == 1)
341 /* Comment ends at newline */