static size_t AxGetTableHeader ( FILE *InputFile, unsigned char *OutputData) { size_t BytesConverted; size_t TotalConverted = 0; int i; /* Get the full 36 byte ACPI table header, requires 3 input text lines */ for (i = 0; i < 3; i++) { if (!fgets (HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile)) { return (TotalConverted); } BytesConverted = AxConvertLine (HeaderBuffer, OutputData); TotalConverted += BytesConverted; OutputData += 16; if (BytesConverted != 16) { return (TotalConverted); } } return (TotalConverted); }
long AxProcessOneTextLine ( FILE *OutputFile, char *ThisSignature, unsigned int ThisTableBytesWritten) { size_t BytesWritten; size_t BytesConverted; /* Check for the end of this table data block */ if (AxIsEmptyLine (Gbl_LineBuffer) || (Gbl_LineBuffer[0] != ' ')) { printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten, Gbl_OutputFilename); return (0); } /* Convert one line of ascii hex data to binary */ BytesConverted = AxConvertLine (Gbl_LineBuffer, Gbl_BinaryData); /* Write the binary data */ BytesWritten = fwrite (Gbl_BinaryData, 1, BytesConverted, OutputFile); if (BytesWritten != BytesConverted) { printf ("Error while writing file %s\n", Gbl_OutputFilename); return (-1); } return (BytesWritten); }
int AxExtractTables ( char *InputPathname, char *Signature, unsigned int MinimumInstances) { FILE *InputFile; FILE *OutputFile = NULL; size_t BytesWritten; size_t TotalBytesWritten = 0; size_t BytesConverted; unsigned int State = AX_STATE_FIND_HEADER; unsigned int FoundTable = 0; unsigned int Instances = 0; unsigned int ThisInstance; char ThisSignature[4]; int Status = 0; /* Open input in text mode, output is in binary mode */ InputFile = fopen (InputPathname, "rt"); if (!InputFile) { printf ("Could not open %s\n", InputPathname); return (-1); } if (Signature) { /* Are there enough instances of the table to continue? */ AxNormalizeSignature (Signature); Instances = AxCountTableInstances (InputPathname, Signature); if (Instances < MinimumInstances) { printf ("Table %s was not found in %s\n", Signature, InputPathname); Status = -1; goto CleanupAndExit; } if (Instances == 0) { goto CleanupAndExit; } } /* Convert all instances of the table to binary */ while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile)) { switch (State) { case AX_STATE_FIND_HEADER: /* Ignore lines that are too short to be header lines */ if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH) { continue; } /* Ignore empty lines and lines that start with a space */ if ((LineBuffer[0] == ' ') || (LineBuffer[0] == '\n')) { continue; } /* * Ignore lines that are not of the form <sig> @ <addr>. * Examples of lines that must be supported: * * DSDT @ 0x737e4000 * XSDT @ 0x737f2fff * RSD PTR @ 0xf6cd0 * SSDT @ (nil) */ if (!strstr (LineBuffer, " @ ")) { continue; } AxNormalizeSignature (LineBuffer); strncpy (ThisSignature, LineBuffer, 4); if (Signature) { /* Ignore signatures that don't match */ if (strncmp (ThisSignature, Signature, 4)) { continue; } } /* * Get the instance number for this signature. Only the * SSDT and PSDT tables can have multiple instances. */ ThisInstance = AxGetNextInstance (InputPathname, ThisSignature); /* Build an output filename and create/open the output file */ if (ThisInstance > 0) { sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance); } else { sprintf (Filename, "%4.4s.dat", ThisSignature); } AxStrlwr (Filename); OutputFile = fopen (Filename, "w+b"); if (!OutputFile) { printf ("Could not open %s\n", Filename); Status = -1; goto CleanupAndExit; } State = AX_STATE_EXTRACT_DATA; TotalBytesWritten = 0; FoundTable = 1; continue; case AX_STATE_EXTRACT_DATA: /* Empty line or non-data line terminates the data */ if ((LineBuffer[0] == '\n') || (LineBuffer[0] != ' ')) { fclose (OutputFile); OutputFile = NULL; State = AX_STATE_FIND_HEADER; printf ("Acpi table [%4.4s] - %u bytes written to %s\n", ThisSignature, (unsigned int) TotalBytesWritten, Filename); continue; } /* Convert the ascii data (one line of text) to binary */ BytesConverted = AxConvertLine (LineBuffer, Data); /* Write the binary data */ BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile); if (BytesWritten != BytesConverted) { printf ("Write error on %s\n", Filename); fclose (OutputFile); OutputFile = NULL; Status = -1; goto CleanupAndExit; } TotalBytesWritten += BytesConverted; continue; default: Status = -1; goto CleanupAndExit; } } if (!FoundTable) { printf ("Table %s was not found in %s\n", Signature, InputPathname); } CleanupAndExit: if (OutputFile) { fclose (OutputFile); if (State == AX_STATE_EXTRACT_DATA) { /* Received an EOF while extracting data */ printf ("Acpi table [%4.4s] - %u bytes written to %s\n", ThisSignature, (unsigned int) TotalBytesWritten, Filename); } } fclose (InputFile); return (Status); }