static BOOLEAN AxIsFileAscii ( FILE *Handle) { UINT8 Byte; /* Read the entire file */ while (fread (&Byte, 1, 1, Handle) == 1) { /* Check for an ASCII character */ if (!ACPI_IS_ASCII (Byte)) { goto ErrorExit; } /* Ensure character is either printable or a "space" char */ else if (!isprint (Byte) && !isspace (Byte)) { goto ErrorExit; } } /* File is OK (100% ASCII) */ fseek (Handle, 0, SEEK_SET); return (TRUE); ErrorExit: printf ("File is binary (contains non-text or non-ascii characters)\n"); fseek (Handle, 0, SEEK_SET); return (FALSE); }
ACPI_STATUS AcValidateTableHeader ( FILE *File, long TableOffset) { ACPI_TABLE_HEADER TableHeader; ACPI_SIZE Actual; long OriginalOffset; UINT32 FileSize; UINT32 i; ACPI_FUNCTION_TRACE (AcValidateTableHeader); /* Read a potential table header */ OriginalOffset = ftell (File); fseek (File, TableOffset, SEEK_SET); Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File); fseek (File, OriginalOffset, SEEK_SET); if (Actual < sizeof (ACPI_TABLE_HEADER)) { return (AE_ERROR); } /* Validate the signature (limited ASCII chars) */ if (!AcpiUtValidNameseg (TableHeader.Signature)) { fprintf (stderr, "Invalid table signature: 0x%8.8X\n", *ACPI_CAST_PTR (UINT32, TableHeader.Signature)); return (AE_BAD_SIGNATURE); } /* Validate table length against bytes remaining in the file */ FileSize = CmGetFileSize (File); if (TableHeader.Length > (UINT32) (FileSize - TableOffset)) { fprintf (stderr, "Table [%4.4s] is too long for file - " "needs: 0x%.2X, remaining in file: 0x%.2X\n", TableHeader.Signature, TableHeader.Length, (UINT32) (FileSize - TableOffset)); return (AE_BAD_HEADER); } /* * These fields must be ASCII: 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) TableHeader.AslCompilerId[i])) { goto BadCharacters; } } for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++) { if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i])) { goto BadCharacters; } } for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++) { if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i])) { goto BadCharacters; } } return (AE_OK); BadCharacters: ACPI_WARNING ((AE_INFO, "Table header for [%4.4s] has invalid ASCII character(s)", TableHeader.Signature)); return (AE_OK); }
ACPI_STATUS FlIsFileAsciiSource ( char *Filename, BOOLEAN DisplayErrors) { UINT8 Byte; ACPI_SIZE BadBytes = 0; BOOLEAN OpeningComment = FALSE; ASL_FILE_STATUS Status; FILE *Handle; /* Open file in text mode so file offset is always accurate */ Handle = fopen (Filename, "rb"); if (!Handle) { perror ("Could not open input file"); return (AE_ERROR); } 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 ( "Found non-ASCII character in source text: " "0x%2.2X in line %u, file offset 0x%2.2X\n", Byte, Status.Line, Status.Offset); } BadBytes++; } /* Ensure character is either printable or a "space" char */ else if (!isprint (Byte) && !isspace (Byte)) { if ((BadBytes < 10) && (DisplayErrors)) { AcpiOsPrintf ( "Found invalid character in source text: " "0x%2.2X in line %u, file offset 0x%2.2X\n", Byte, Status.Line, Status.Offset); } BadBytes++; } /* Update line counter as necessary */ if (Byte == 0x0A) { Status.Line++; } Status.Offset++; } fclose (Handle); /* Were there any non-ASCII characters in the file? */ if (BadBytes) { if (DisplayErrors) { AcpiOsPrintf ( "Total %u invalid 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); }
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); }
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); }