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: Filename - Full input filename
152 * DisplayErrors - TRUE if error messages desired
156 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
157 * within comments. Note: does not handle nested comments and does
158 * not handle comment delimiters within string literals. However,
159 * on the rare chance this happens and an invalid character is
160 * missed, the parser will catch the error by failing in some
161 * spectactular manner.
163 ******************************************************************************/
168 BOOLEAN DisplayErrors)
171 ACPI_SIZE BadBytes = 0;
172 BOOLEAN OpeningComment = FALSE;
173 ASL_FILE_STATUS Status;
177 /* Open file in text mode so file offset is always accurate */
179 Handle = fopen (Filename, "rb");
184 /* Read the entire file */
186 while (fread (&Byte, 1, 1, Handle) == 1)
188 /* Ignore comment fields (allow non-ascii within) */
192 /* Check for second comment open delimiter */
196 FlConsumeAnsiComment (Handle, &Status);
201 FlConsumeNewComment (Handle, &Status);
206 OpeningComment = FALSE;
208 else if (Byte == '/')
210 OpeningComment = TRUE;
213 /* Check for an ASCII character */
215 if (!ACPI_IS_ASCII (Byte))
217 if ((BadBytes < 10) && (DisplayErrors))
220 "Found non-ASCII character in source text: "
221 "0x%2.2X in line %u, file offset 0x%2.2X\n",
222 Byte, Status.Line, Status.Offset);
227 /* Ensure character is either printable or a "space" char */
229 else if (!ACPI_IS_PRINT (Byte) && !ACPI_IS_SPACE (Byte))
231 if ((BadBytes < 10) && (DisplayErrors))
234 "Found invalid character in source text: "
235 "0x%2.2X in line %u, file offset 0x%2.2X\n",
236 Byte, Status.Line, Status.Offset);
241 /* Update line counter as necessary */
253 /* Were there any non-ASCII characters in the file? */
260 "Total %u invalid characters found in input source text, "
261 "could be a binary file\n", BadBytes);
262 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
265 return (AE_BAD_CHARACTER);
268 /* File is OK (100% ASCII) */
274 /*******************************************************************************
276 * FUNCTION: FlConsumeAnsiComment
278 * PARAMETERS: Handle - Open input file
279 * Status - File current status struct
281 * RETURN: Number of lines consumed
283 * DESCRIPTION: Step over a normal slash-star type comment
285 ******************************************************************************/
288 FlConsumeAnsiComment (
290 ASL_FILE_STATUS *Status)
293 BOOLEAN ClosingComment = FALSE;
296 while (fread (&Byte, 1, 1, Handle) == 1)
298 /* Scan until comment close is found */
312 ClosingComment = FALSE;
315 else if (Byte == '*')
317 ClosingComment = TRUE;
320 /* Maintain line count */
332 /*******************************************************************************
334 * FUNCTION: FlConsumeNewComment
336 * PARAMETERS: Handle - Open input file
337 * Status - File current status struct
339 * RETURN: Number of lines consumed
341 * DESCRIPTION: Step over a slash-slash type of comment
343 ******************************************************************************/
346 FlConsumeNewComment (
348 ASL_FILE_STATUS *Status)
353 while (fread (&Byte, 1, 1, Handle) == 1)
357 /* Comment ends at newline */