Home | History | Annotate | Line # | Download | only in acpisrc
asfile.c revision 1.1.1.12
      1 /******************************************************************************
      2  *
      3  * Module Name: asfile - Main module for the acpi source processor utility
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2017, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     25  *
     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.
     29  *
     30  * NO WARRANTY
     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.
     42  */
     43 
     44 #include "acpisrc.h"
     45 
     46 /* Local prototypes */
     47 
     48 void
     49 AsDoWildcard (
     50     ACPI_CONVERSION_TABLE   *ConversionTable,
     51     char                    *SourcePath,
     52     char                    *TargetPath,
     53     int                     MaxPathLength,
     54     int                     FileType,
     55     char                    *WildcardSpec);
     56 
     57 BOOLEAN
     58 AsDetectLoneLineFeeds (
     59     char                    *Filename,
     60     char                    *Buffer);
     61 
     62 static BOOLEAN
     63 AsCheckForNonPrintableChars (
     64     char                    *FileBuffer,
     65     UINT32                  FileSize);
     66 
     67 static ACPI_INLINE int
     68 AsMaxInt (int a, int b)
     69 {
     70     return (a > b ? a : b);
     71 }
     72 
     73 
     74 /******************************************************************************
     75  *
     76  * FUNCTION:    AsDoWildcard
     77  *
     78  * DESCRIPTION: Process files via wildcards
     79  *
     80  ******************************************************************************/
     81 
     82 void
     83 AsDoWildcard (
     84     ACPI_CONVERSION_TABLE   *ConversionTable,
     85     char                    *SourcePath,
     86     char                    *TargetPath,
     87     int                     MaxPathLength,
     88     int                     FileType,
     89     char                    *WildcardSpec)
     90 {
     91     void                    *DirInfo;
     92     char                    *Filename;
     93     char                    *SourceDirPath;
     94     char                    *TargetDirPath;
     95     char                    RequestedFileType;
     96 
     97 
     98     if (FileType == FILE_TYPE_DIRECTORY)
     99     {
    100         RequestedFileType = REQUEST_DIR_ONLY;
    101     }
    102     else
    103     {
    104         RequestedFileType = REQUEST_FILE_ONLY;
    105     }
    106 
    107     VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
    108             WildcardSpec, SourcePath));
    109 
    110     /* Open the directory for wildcard search */
    111 
    112     DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
    113     if (DirInfo)
    114     {
    115         /*
    116          * Get all of the files that match both the
    117          * wildcard and the requested file type
    118          */
    119         while ((Filename = AcpiOsGetNextFilename (DirInfo)))
    120         {
    121             /* Looking for directory files, must check file type */
    122 
    123             switch (RequestedFileType)
    124             {
    125             case REQUEST_DIR_ONLY:
    126 
    127                 /* If we actually have a dir, process the subtree */
    128 
    129                 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
    130                     &SourceDirPath, &TargetDirPath))
    131                 {
    132                     VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
    133 
    134                     AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
    135                     free (SourceDirPath);
    136                     free (TargetDirPath);
    137                 }
    138                 break;
    139 
    140             case REQUEST_FILE_ONLY:
    141 
    142                 /* Otherwise, this is a file, not a directory */
    143 
    144                 VERBOSE_PRINT (("File: %s\n", Filename));
    145 
    146                 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
    147                     MaxPathLength, Filename, FileType);
    148                 break;
    149 
    150             default:
    151 
    152                 break;
    153             }
    154         }
    155 
    156         /* Cleanup */
    157 
    158         AcpiOsCloseDirectory (DirInfo);
    159     }
    160 }
    161 
    162 
    163 /******************************************************************************
    164  *
    165  * FUNCTION:    AsProcessTree
    166  *
    167  * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
    168  *              ".H" are processed as the tree is traversed.
    169  *
    170  ******************************************************************************/
    171 
    172 ACPI_NATIVE_INT
    173 AsProcessTree (
    174     ACPI_CONVERSION_TABLE   *ConversionTable,
    175     char                    *SourcePath,
    176     char                    *TargetPath)
    177 {
    178     int                     MaxPathLength;
    179 
    180 
    181     MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
    182 
    183     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    184     {
    185         if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
    186         {
    187             AcpiUtStrlwr (TargetPath);
    188         }
    189 
    190         VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
    191         if (mkdir (TargetPath))
    192         {
    193             if (errno != EEXIST)
    194             {
    195                 printf ("Could not create target directory\n");
    196                 return (-1);
    197             }
    198         }
    199     }
    200 
    201     /* Do the C source files */
    202 
    203     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    204         FILE_TYPE_SOURCE, "*.c");
    205 
    206     /* Do the C header files */
    207 
    208     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    209         FILE_TYPE_HEADER, "*.h");
    210 
    211     /* Do the Lex file(s) */
    212 
    213     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    214         FILE_TYPE_SOURCE, "*.l");
    215 
    216     /* Do the yacc file(s) */
    217 
    218     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    219         FILE_TYPE_SOURCE, "*.y");
    220 
    221     /* Do any ASL files */
    222 
    223     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    224         FILE_TYPE_HEADER, "*.asl");
    225 
    226     /* Do any subdirectories */
    227 
    228     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    229         FILE_TYPE_DIRECTORY, "*");
    230 
    231     return (0);
    232 }
    233 
    234 
    235 /******************************************************************************
    236  *
    237  * FUNCTION:    AsDetectLoneLineFeeds
    238  *
    239  * DESCRIPTION: Find LF without CR.
    240  *
    241  ******************************************************************************/
    242 
    243 BOOLEAN
    244 AsDetectLoneLineFeeds (
    245     char                    *Filename,
    246     char                    *Buffer)
    247 {
    248     UINT32                  i = 1;
    249     UINT32                  LfCount = 0;
    250     UINT32                  LineCount = 0;
    251 
    252 
    253     if (!Buffer[0])
    254     {
    255         return (FALSE);
    256     }
    257 
    258     while (Buffer[i])
    259     {
    260         if (Buffer[i] == 0x0A)
    261         {
    262             if (Buffer[i-1] != 0x0D)
    263             {
    264                 LfCount++;
    265             }
    266 
    267             LineCount++;
    268         }
    269         i++;
    270     }
    271 
    272     if (LfCount)
    273     {
    274         if (LineCount == LfCount)
    275         {
    276             if (!Gbl_IgnoreLoneLineFeeds)
    277             {
    278                 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
    279                     Filename, LfCount);
    280             }
    281         }
    282         else
    283         {
    284             printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
    285         }
    286 
    287         return (TRUE);
    288     }
    289 
    290     return (FALSE);
    291 }
    292 
    293 
    294 /******************************************************************************
    295  *
    296  * FUNCTION:    AsConvertFile
    297  *
    298  * DESCRIPTION: Perform the requested transforms on the file buffer (as
    299  *              determined by the ConversionTable and the FileType).
    300  *
    301  ******************************************************************************/
    302 
    303 void
    304 AsConvertFile (
    305     ACPI_CONVERSION_TABLE   *ConversionTable,
    306     char                    *FileBuffer,
    307     char                    *Filename,
    308     ACPI_NATIVE_INT         FileType)
    309 {
    310     UINT32                  i;
    311     UINT32                  Functions;
    312     ACPI_STRING_TABLE       *StringTable;
    313     ACPI_IDENTIFIER_TABLE   *ConditionalTable;
    314     ACPI_IDENTIFIER_TABLE   *LineTable;
    315     ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
    316     ACPI_IDENTIFIER_TABLE   *SpecialMacroTable;
    317 
    318 
    319     switch (FileType)
    320     {
    321     case FILE_TYPE_SOURCE:
    322 
    323         Functions           = ConversionTable->SourceFunctions;
    324         StringTable         = ConversionTable->SourceStringTable;
    325         LineTable           = ConversionTable->SourceLineTable;
    326         ConditionalTable    = ConversionTable->SourceConditionalTable;
    327         StructTable         = ConversionTable->SourceStructTable;
    328         SpecialMacroTable   = ConversionTable->SourceSpecialMacroTable;
    329         break;
    330 
    331     case FILE_TYPE_HEADER:
    332 
    333         Functions           = ConversionTable->HeaderFunctions;
    334         StringTable         = ConversionTable->HeaderStringTable;
    335         LineTable           = ConversionTable->HeaderLineTable;
    336         ConditionalTable    = ConversionTable->HeaderConditionalTable;
    337         StructTable         = ConversionTable->HeaderStructTable;
    338         SpecialMacroTable   = ConversionTable->HeaderSpecialMacroTable;
    339         break;
    340 
    341     case FILE_TYPE_PATCH:
    342 
    343         Functions           = ConversionTable->PatchFunctions;
    344         StringTable         = ConversionTable->PatchStringTable;
    345         LineTable           = ConversionTable->PatchLineTable;
    346         ConditionalTable    = ConversionTable->PatchConditionalTable;
    347         StructTable         = ConversionTable->PatchStructTable;
    348         SpecialMacroTable   = ConversionTable->PatchSpecialMacroTable;
    349         break;
    350 
    351     default:
    352 
    353         printf ("Unknown file type, cannot process\n");
    354         return;
    355     }
    356 
    357 
    358     Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
    359     Gbl_Files++;
    360     VERBOSE_PRINT (("Processing %u bytes\n",
    361         (unsigned int) strlen (FileBuffer)));
    362 
    363     if (Gbl_Cleanup)
    364     {
    365         AsRemoveExtraLines (FileBuffer, Filename);
    366         AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
    367     }
    368 
    369     if (ConversionTable->LowerCaseTable)
    370     {
    371         for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
    372         {
    373             AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
    374                 FileBuffer);
    375         }
    376     }
    377 
    378     /* Process all the string replacements */
    379 
    380     if (StringTable)
    381     {
    382         for (i = 0; StringTable[i].Target; i++)
    383         {
    384             AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
    385                 StringTable[i].Type, FileBuffer);
    386         }
    387     }
    388 
    389     if (LineTable)
    390     {
    391         for (i = 0; LineTable[i].Identifier; i++)
    392         {
    393             AsRemoveLine (FileBuffer, LineTable[i].Identifier);
    394         }
    395     }
    396 
    397     if (ConditionalTable)
    398     {
    399         for (i = 0; ConditionalTable[i].Identifier; i++)
    400         {
    401             AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
    402         }
    403     }
    404 
    405 #ifdef _OBSOLETE_FUNCTIONS
    406     if (MacroTable)
    407     {
    408         for (i = 0; MacroTable[i].Identifier; i++)
    409         {
    410             AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
    411         }
    412     }
    413 #endif
    414 
    415     if (StructTable)
    416     {
    417         for (i = 0; StructTable[i].Identifier; i++)
    418         {
    419             AsInsertPrefix (FileBuffer, StructTable[i].Identifier,
    420                 StructTable[i].Type);
    421         }
    422     }
    423 
    424     if (SpecialMacroTable)
    425     {
    426         for (i = 0; SpecialMacroTable[i].Identifier; i++)
    427         {
    428             AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
    429         }
    430     }
    431 
    432     /* Process the function table */
    433 
    434     for (i = 0; i < 32; i++)
    435     {
    436         /* Decode the function bitmap */
    437 
    438         switch ((1 << i) & Functions)
    439         {
    440         case 0:
    441 
    442             /* This function not configured */
    443             break;
    444 
    445         case CVT_COUNT_TABS:
    446 
    447             AsCountTabs (FileBuffer, Filename);
    448             break;
    449 
    450         case CVT_COUNT_NON_ANSI_COMMENTS:
    451 
    452             AsCountNonAnsiComments (FileBuffer, Filename);
    453             break;
    454 
    455         case CVT_CHECK_BRACES:
    456 
    457             AsCheckForBraces (FileBuffer, Filename);
    458             break;
    459 
    460         case CVT_TRIM_LINES:
    461 
    462             AsTrimLines (FileBuffer, Filename);
    463             break;
    464 
    465         case CVT_COUNT_LINES:
    466 
    467             AsCountSourceLines (FileBuffer, Filename);
    468             break;
    469 
    470         case CVT_BRACES_ON_SAME_LINE:
    471 
    472             AsBracesOnSameLine (FileBuffer);
    473             break;
    474 
    475         case CVT_MIXED_CASE_TO_UNDERSCORES:
    476 
    477             AsMixedCaseToUnderscores (FileBuffer, Filename);
    478             break;
    479 
    480         case CVT_LOWER_CASE_IDENTIFIERS:
    481 
    482             AsLowerCaseIdentifiers (FileBuffer);
    483             break;
    484 
    485         case CVT_REMOVE_DEBUG_MACROS:
    486 
    487             AsRemoveDebugMacros (FileBuffer);
    488             break;
    489 
    490         case CVT_TRIM_WHITESPACE:
    491 
    492             AsTrimWhitespace (FileBuffer);
    493             break;
    494 
    495         case CVT_REMOVE_EMPTY_BLOCKS:
    496 
    497             AsRemoveEmptyBlocks (FileBuffer, Filename);
    498             break;
    499 
    500         case CVT_REDUCE_TYPEDEFS:
    501 
    502             AsReduceTypedefs (FileBuffer, "typedef union");
    503             AsReduceTypedefs (FileBuffer, "typedef struct");
    504             break;
    505 
    506         case CVT_SPACES_TO_TABS4:
    507 
    508             AsTabify4 (FileBuffer);
    509             break;
    510 
    511         case CVT_SPACES_TO_TABS8:
    512 
    513             AsTabify8 (FileBuffer);
    514             break;
    515 
    516         case CVT_COUNT_SHORTMULTILINE_COMMENTS:
    517 
    518 #ifdef ACPI_FUTURE_IMPLEMENTATION
    519             AsTrimComments (FileBuffer, Filename);
    520 #endif
    521             break;
    522 
    523         default:
    524 
    525             printf ("Unknown conversion subfunction opcode\n");
    526             break;
    527         }
    528     }
    529 
    530     if (ConversionTable->NewHeader)
    531     {
    532         AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
    533     }
    534 }
    535 
    536 /*******************************************************************************
    537  *
    538  * FUNCTION:    AsCheckForNonPrintableChars
    539  *
    540  * PARAMETERS:  FileBuffer              - Buffer with contents of entire file
    541  *              FileSize                - Size of the file and buffer
    542  *
    543  * RETURN:      TRUE if there are no non-printable characters
    544  *
    545  * DESCRIPTION: Scan a file for any non-printable ASCII bytes.
    546  *
    547  ******************************************************************************/
    548 
    549 static BOOLEAN
    550 AsCheckForNonPrintableChars (
    551     char                    *FileBuffer,
    552     UINT32                  FileSize)
    553 {
    554     BOOLEAN                 Found = TRUE;
    555     UINT8                   Byte;
    556     UINT32                  i;
    557 
    558 
    559     /* Scan entire file for any non-printable characters */
    560 
    561     for (i = 0; i < FileSize; i++)
    562     {
    563         Byte = FileBuffer[i];
    564         if (!isprint (Byte) && !isspace (Byte))
    565         {
    566             printf ( "Non-printable character (0x%2.2X) "
    567                 "at file offset: %8u (0x%X)\n", Byte, i, i);
    568             Found = FALSE;
    569         }
    570     }
    571 
    572     return (Found);
    573 }
    574 
    575 
    576 /******************************************************************************
    577  *
    578  * FUNCTION:    AsProcessOneFile
    579  *
    580  * DESCRIPTION: Process one source file. The file is opened, read entirely
    581  *              into a buffer, converted, then written to a new file.
    582  *
    583  ******************************************************************************/
    584 
    585 ACPI_NATIVE_INT
    586 AsProcessOneFile (
    587     ACPI_CONVERSION_TABLE   *ConversionTable,
    588     char                    *SourcePath,
    589     char                    *TargetPath,
    590     int                     MaxPathLength,
    591     char                    *Filename,
    592     ACPI_NATIVE_INT         FileType)
    593 {
    594     char                    *Pathname;
    595     char                    *OutPathname;
    596     int                     Status = 0;
    597 
    598 
    599     /* Allocate a file pathname buffer for both source and target */
    600 
    601     Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
    602     if (!Pathname)
    603     {
    604         printf ("Could not allocate buffer for file pathnames\n");
    605         return (-1);
    606     }
    607 
    608     Gbl_FileType = FileType;
    609 
    610     /* Generate the source pathname and read the file */
    611 
    612     if (SourcePath)
    613     {
    614         strcpy (Pathname, SourcePath);
    615         strcat (Pathname, "/");
    616     }
    617 
    618     strcat (Pathname, Filename);
    619     if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
    620     {
    621         Status = -1;
    622         goto Exit1;
    623     }
    624 
    625     /* Exit now if simply checking the file for printable ascii chars */
    626 
    627     if (Gbl_CheckAscii)
    628     {
    629         Status = 0;
    630         goto Exit2;
    631     }
    632 
    633     Gbl_HeaderSize = 0;
    634     if (strstr (Filename, ".asl"))
    635     {
    636         Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
    637     }
    638     else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
    639     {
    640         Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
    641     }
    642     else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
    643     {
    644         Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
    645     }
    646 
    647     /* Process the file in the buffer */
    648 
    649     Gbl_MadeChanges = FALSE;
    650     if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
    651     {
    652         /*
    653          * All lone LFs will be converted to CR/LF
    654          * (when file is written, Windows version only)
    655          */
    656         printf ("Converting lone linefeeds\n");
    657         Gbl_MadeChanges = TRUE;
    658     }
    659 
    660     AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
    661 
    662     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    663     {
    664         if (!(Gbl_Overwrite && !Gbl_MadeChanges))
    665         {
    666             /* Generate the target pathname and write the file */
    667 
    668             OutPathname = calloc (MaxPathLength +
    669                 strlen (Filename) + 2 + strlen (TargetPath), 1);
    670             if (!OutPathname)
    671             {
    672                 printf ("Could not allocate buffer for file pathnames\n");
    673                 Status = -1;
    674                 goto Exit2;
    675             }
    676 
    677             strcpy (OutPathname, TargetPath);
    678             if (SourcePath)
    679             {
    680                 strcat (OutPathname, "/");
    681                 strcat (OutPathname, Filename);
    682             }
    683 
    684             AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
    685             free (OutPathname);
    686         }
    687     }
    688 
    689 Exit2:
    690     free (Gbl_FileBuffer);
    691 
    692 Exit1:
    693     free (Pathname);
    694     return (Status);
    695 }
    696 
    697 
    698 /******************************************************************************
    699  *
    700  * FUNCTION:    AsCheckForDirectory
    701  *
    702  * DESCRIPTION: Check if the current file is a valid directory. If not,
    703  *              construct the full pathname for the source and target paths.
    704  *              Checks for the dot and dot-dot files (they are ignored)
    705  *
    706  ******************************************************************************/
    707 
    708 ACPI_NATIVE_INT
    709 AsCheckForDirectory (
    710     char                    *SourceDirPath,
    711     char                    *TargetDirPath,
    712     char                    *Filename,
    713     char                    **SourcePath,
    714     char                    **TargetPath)
    715 {
    716     char                    *SrcPath;
    717     char                    *TgtPath;
    718 
    719 
    720     if (!(strcmp (Filename, ".")) ||
    721         !(strcmp (Filename, "..")))
    722     {
    723         return (-1);
    724     }
    725 
    726     SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
    727     if (!SrcPath)
    728     {
    729         printf ("Could not allocate buffer for directory source pathname\n");
    730         return (-1);
    731     }
    732 
    733     TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
    734     if (!TgtPath)
    735     {
    736         printf ("Could not allocate buffer for directory target pathname\n");
    737         free (SrcPath);
    738         return (-1);
    739     }
    740 
    741     strcpy (SrcPath, SourceDirPath);
    742     strcat (SrcPath, "/");
    743     strcat (SrcPath, Filename);
    744 
    745     strcpy (TgtPath, TargetDirPath);
    746     strcat (TgtPath, "/");
    747     strcat (TgtPath, Filename);
    748 
    749     *SourcePath = SrcPath;
    750     *TargetPath = TgtPath;
    751     return (0);
    752 }
    753 
    754 
    755 /******************************************************************************
    756  *
    757  * FUNCTION:    AsGetFile
    758  *
    759  * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
    760  *
    761  ******************************************************************************/
    762 
    763 int
    764 AsGetFile (
    765     char                    *Filename,
    766     char                    **FileBuffer,
    767     UINT32                  *FileSize)
    768 {
    769     FILE                    *File;
    770     UINT32                  Size;
    771     char                    *Buffer;
    772     size_t                  Actual;
    773 
    774 
    775     /* Binary mode leaves CR/LF pairs */
    776 
    777     File = fopen (Filename, "rb");
    778     if (!File)
    779     {
    780         printf ("Could not open file %s\n", Filename);
    781         return (-1);
    782     }
    783 
    784     /* Need file size to allocate a buffer */
    785 
    786     Size = CmGetFileSize (File);
    787     if (Size == ACPI_UINT32_MAX)
    788     {
    789         printf ("Could not get file size for %s\n", Filename);
    790         goto ErrorExit;
    791     }
    792 
    793     /*
    794      * Create a buffer for the entire file
    795      * Add plenty extra buffer to accommodate string replacements
    796      */
    797     Gbl_TotalSize += Size;
    798 
    799     Buffer = calloc (Size * 2, 1);
    800     if (!Buffer)
    801     {
    802         printf ("Could not allocate buffer of size %u\n", Size * 2);
    803         goto ErrorExit;
    804     }
    805 
    806     /* Read the entire file */
    807 
    808     Actual = fread (Buffer, 1, Size, File);
    809     if (Actual != Size)
    810     {
    811         printf ("Could not read the input file %s (%u bytes)\n",
    812             Filename, Size);
    813         goto ErrorFree;
    814     }
    815 
    816     Buffer [Size] = 0;         /* Null terminate the buffer */
    817     fclose (File);
    818 
    819     /* This option checks the entire file for non-printable chars */
    820 
    821     if (Gbl_CheckAscii)
    822     {
    823         if (AsCheckForNonPrintableChars (Buffer, Size))
    824         {
    825             printf ("File contains only printable ASCII characters\n");
    826         }
    827 
    828         free (Buffer);
    829         return (0);
    830     }
    831 
    832     /* Check for unix contamination */
    833 
    834     Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
    835 
    836     /*
    837      * Convert all CR/LF pairs to LF only. We do this locally so that
    838      * this code is portable across operating systems.
    839      */
    840     AsConvertToLineFeeds (Buffer);
    841 
    842     *FileBuffer = Buffer;
    843     *FileSize = Size;
    844     return (0);
    845 
    846 ErrorFree:
    847     free (Buffer);
    848 
    849 ErrorExit:
    850     fclose (File);
    851     return (-1);
    852 }
    853 
    854 
    855 /******************************************************************************
    856  *
    857  * FUNCTION:    AsPutFile
    858  *
    859  * DESCRIPTION: Create a new output file and write the entire contents of the
    860  *              buffer to the new file. Buffer must be a zero terminated string
    861  *
    862  ******************************************************************************/
    863 
    864 int
    865 AsPutFile (
    866     char                    *Pathname,
    867     char                    *FileBuffer,
    868     UINT32                  SystemFlags)
    869 {
    870     FILE                    *File;
    871     UINT32                  FileSize;
    872     size_t                  Actual;
    873     int                     Status = 0;
    874 
    875 
    876     /* Create the target file */
    877 
    878     if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
    879     {
    880         /* Put back the CR before each LF */
    881 
    882         AsInsertCarriageReturns (FileBuffer);
    883     }
    884 
    885     File = fopen (Pathname, "w+b");
    886     if (!File)
    887     {
    888         perror ("Could not create destination file");
    889         printf ("Could not create destination file \"%s\"\n", Pathname);
    890         return (-1);
    891     }
    892 
    893     /* Write the buffer to the file */
    894 
    895     FileSize = strlen (FileBuffer);
    896     Actual = fwrite (FileBuffer, 1, FileSize, File);
    897     if (Actual != FileSize)
    898     {
    899         printf ("Error writing output file \"%s\"\n", Pathname);
    900         Status = -1;
    901     }
    902 
    903     fclose (File);
    904     return (Status);
    905 }
    906