1 /******************************************************************************
3 * Module Name: aslascii - ASCII detection and support routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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/actables.h>
46 #include <contrib/dev/acpica/include/acapps.h>
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslascii")
52 /* Local prototypes */
55 FlConsumeAnsiComment (
57 ASL_FILE_STATUS *Status);
62 ASL_FILE_STATUS *Status);
65 /*******************************************************************************
67 * FUNCTION: FlIsFileAsciiSource
69 * PARAMETERS: Filename - Full input filename
70 * DisplayErrors - TRUE if error messages desired
74 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
75 * within comments. Note: does not handle nested comments and does
76 * not handle comment delimiters within string literals. However,
77 * on the rare chance this happens and an invalid character is
78 * missed, the parser will catch the error by failing in some
79 * spectactular manner.
81 ******************************************************************************/
86 BOOLEAN DisplayErrors)
89 ACPI_SIZE BadBytes = 0;
90 BOOLEAN OpeningComment = FALSE;
91 ASL_FILE_STATUS Status;
95 /* Open file in text mode so file offset is always accurate */
97 Handle = fopen (Filename, "rb");
100 perror ("Could not open input file");
107 /* Read the entire file */
109 while (fread (&Byte, 1, 1, Handle) == 1)
111 /* Ignore comment fields (allow non-ascii within) */
115 /* Check for second comment open delimiter */
119 FlConsumeAnsiComment (Handle, &Status);
124 FlConsumeNewComment (Handle, &Status);
129 OpeningComment = FALSE;
131 else if (Byte == '/')
133 OpeningComment = TRUE;
136 /* Check for an ASCII character */
138 if (!ACPI_IS_ASCII (Byte))
140 if ((BadBytes < 10) && (DisplayErrors))
143 "Found non-ASCII character in source text: "
144 "0x%2.2X in line %u, file offset 0x%2.2X\n",
145 Byte, Status.Line, Status.Offset);
150 /* Ensure character is either printable or a "space" char */
152 else if (!isprint (Byte) && !isspace (Byte))
154 if ((BadBytes < 10) && (DisplayErrors))
157 "Found invalid character in source text: "
158 "0x%2.2X in line %u, file offset 0x%2.2X\n",
159 Byte, Status.Line, Status.Offset);
164 /* Update line counter as necessary */
176 /* Were there any non-ASCII characters in the file? */
183 "Total %u invalid characters found in input source text, "
184 "could be a binary file\n", BadBytes);
185 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
188 return (AE_BAD_CHARACTER);
191 /* File is OK (100% ASCII) */
197 /*******************************************************************************
199 * FUNCTION: FlConsumeAnsiComment
201 * PARAMETERS: Handle - Open input file
202 * Status - File current status struct
204 * RETURN: Number of lines consumed
206 * DESCRIPTION: Step over a normal slash-star type comment
208 ******************************************************************************/
211 FlConsumeAnsiComment (
213 ASL_FILE_STATUS *Status)
216 BOOLEAN ClosingComment = FALSE;
219 while (fread (&Byte, 1, 1, Handle) == 1)
221 /* Scan until comment close is found */
235 ClosingComment = FALSE;
238 else if (Byte == '*')
240 ClosingComment = TRUE;
243 /* Maintain line count */
255 /*******************************************************************************
257 * FUNCTION: FlConsumeNewComment
259 * PARAMETERS: Handle - Open input file
260 * Status - File current status struct
262 * RETURN: Number of lines consumed
264 * DESCRIPTION: Step over a slash-slash type of comment
266 ******************************************************************************/
269 FlConsumeNewComment (
271 ASL_FILE_STATUS *Status)
276 while (fread (&Byte, 1, 1, Handle) == 1)
280 /* Comment ends at newline */