1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
/******************************************************************************
*
* Module Name: aslascii - ASCII detection and support routines
*
*****************************************************************************/
/*
* Copyright (C) 2000 - 2014, Intel Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*/
#include "aslcompiler.h"
#include <acapps.h>
#define _COMPONENT ACPI_COMPILER
ACPI_MODULE_NAME ("aslascii")
/* Local prototypes */
static void
FlConsumeAnsiComment (
FILE *Handle,
ASL_FILE_STATUS *Status);
static void
FlConsumeNewComment (
FILE *Handle,
ASL_FILE_STATUS *Status);
/*******************************************************************************
*
* FUNCTION: FlCheckForAcpiTable
*
* PARAMETERS: Handle - Open input file
*
* RETURN: Status
*
* DESCRIPTION: Determine if a file seems to be a binary ACPI table, via the
* following checks on what would be the table header:
* 0) File must be at least as long as an ACPI_TABLE_HEADER
* 1) The header length field must match the file size
* 2) Signature, OemId, OemTableId, AslCompilerId must be ASCII
*
******************************************************************************/
ACPI_STATUS
FlCheckForAcpiTable (
FILE *Handle)
{
ACPI_TABLE_HEADER Table;
UINT32 FileSize;
size_t Actual;
UINT32 i;
/* Read a potential table header */
Actual = fread (&Table, 1, sizeof (ACPI_TABLE_HEADER), Handle);
fseek (Handle, 0, SEEK_SET);
if (Actual < sizeof (ACPI_TABLE_HEADER))
{
return (AE_ERROR);
}
/* Header length field must match the file size */
FileSize = CmGetFileSize (Handle);
if (Table.Length != FileSize)
{
return (AE_ERROR);
}
/*
* These fields must be ASCII:
* Signature, OemId, OemTableId, AslCompilerId.
* We allow a NULL terminator in OemId and OemTableId.
*/
for (i = 0; i < ACPI_NAME_SIZE; i++)
{
if (!ACPI_IS_ASCII ((UINT8) Table.Signature[i]))
{
return (AE_ERROR);
}
if (!ACPI_IS_ASCII ((UINT8) Table.AslCompilerId[i]))
{
return (AE_ERROR);
}
}
for (i = 0; (i < ACPI_OEM_ID_SIZE) && (Table.OemId[i]); i++)
{
if (!ACPI_IS_ASCII ((UINT8) Table.OemId[i]))
{
return (AE_ERROR);
}
}
for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (Table.OemTableId[i]); i++)
{
if (!ACPI_IS_ASCII ((UINT8) Table.OemTableId[i]))
{
return (AE_ERROR);
}
}
printf ("Binary file appears to be a valid ACPI table, disassembling\n");
return (AE_OK);
}
/*******************************************************************************
*
* FUNCTION: FlCheckForAscii
*
* PARAMETERS: Handle - Open input file
* Filename - Input filename
* DisplayErrors - TRUE if error messages desired
*
* RETURN: Status
*
* DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
* within comments. Note: does not handle nested comments and does
* not handle comment delimiters within string literals. However,
* on the rare chance this happens and an invalid character is
* missed, the parser will catch the error by failing in some
* spectactular manner.
*
******************************************************************************/
ACPI_STATUS
FlCheckForAscii (
FILE *Handle,
char *Filename,
BOOLEAN DisplayErrors)
{
UINT8 Byte;
ACPI_SIZE BadBytes = 0;
BOOLEAN OpeningComment = FALSE;
ASL_FILE_STATUS Status;
Status.Line = 1;
Status.Offset = 0;
/* Read the entire file */
while (fread (&Byte, 1, 1, Handle) == 1)
{
/* Ignore comment fields (allow non-ascii within) */
if (OpeningComment)
{
/* Check for second comment open delimiter */
if (Byte == '*')
{
FlConsumeAnsiComment (Handle, &Status);
}
if (Byte == '/')
{
FlConsumeNewComment (Handle, &Status);
}
/* Reset */
OpeningComment = FALSE;
}
else if (Byte == '/')
{
OpeningComment = TRUE;
}
/* Check for an ASCII character */
if (!ACPI_IS_ASCII (Byte))
{
if ((BadBytes < 10) && (DisplayErrors))
{
AcpiOsPrintf (
"Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
Byte, Status.Line, Status.Offset);
}
BadBytes++;
}
/* Update line counter */
else if (Byte == 0x0A)
{
Status.Line++;
}
Status.Offset++;
}
/* Seek back to the beginning of the source file */
fseek (Handle, 0, SEEK_SET);
/* Were there any non-ASCII characters in the file? */
if (BadBytes)
{
if (DisplayErrors)
{
AcpiOsPrintf (
"%u non-ASCII characters found in input source text, could be a binary file\n",
BadBytes);
AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
}
return (AE_BAD_CHARACTER);
}
/* File is OK (100% ASCII) */
return (AE_OK);
}
/*******************************************************************************
*
* FUNCTION: FlConsumeAnsiComment
*
* PARAMETERS: Handle - Open input file
* Status - File current status struct
*
* RETURN: Number of lines consumed
*
* DESCRIPTION: Step over a normal slash-star type comment
*
******************************************************************************/
static void
FlConsumeAnsiComment (
FILE *Handle,
ASL_FILE_STATUS *Status)
{
UINT8 Byte;
BOOLEAN ClosingComment = FALSE;
while (fread (&Byte, 1, 1, Handle) == 1)
{
/* Scan until comment close is found */
if (ClosingComment)
{
if (Byte == '/')
{
return;
}
if (Byte != '*')
{
/* Reset */
ClosingComment = FALSE;
}
}
else if (Byte == '*')
{
ClosingComment = TRUE;
}
/* Maintain line count */
if (Byte == 0x0A)
{
Status->Line++;
}
Status->Offset++;
}
}
/*******************************************************************************
*
* FUNCTION: FlConsumeNewComment
*
* PARAMETERS: Handle - Open input file
* Status - File current status struct
*
* RETURN: Number of lines consumed
*
* DESCRIPTION: Step over a slash-slash type of comment
*
******************************************************************************/
static void
FlConsumeNewComment (
FILE *Handle,
ASL_FILE_STATUS *Status)
{
UINT8 Byte;
while (fread (&Byte, 1, 1, Handle) == 1)
{
Status->Offset++;
/* Comment ends at newline */
if (Byte == 0x0A)
{
Status->Line++;
return;
}
}
}
|