Annotation of mstools/samples/sdktools/image/imagehlp/imagedir.c, revision 1.1

1.1     ! root        1: /*++
        !             2: 
        !             3: Copyright (c) 1991  Microsoft Corporation
        !             4: 
        !             5: Module Name:
        !             6: 
        !             7:     imagedir.c
        !             8: 
        !             9: Abstract:
        !            10: 
        !            11:     The module contains the code to translate an image directory type to
        !            12:     the address of the data for that entry.
        !            13: 
        !            14: Author:
        !            15: 
        !            16:     Steve Wood 18-Aug-1989
        !            17: 
        !            18: Environment:
        !            19: 
        !            20:     User Mode or Kernel Mode
        !            21: 
        !            22: Revision History:
        !            23: 
        !            24: --*/
        !            25: 
        !            26: #include <windows.h>
        !            27: 
        !            28: 
        !            29: // Helper routines
        !            30: 
        !            31: PIMAGE_NT_HEADERS
        !            32: ImageNtHeader (
        !            33:     IN PVOID Base
        !            34:     );
        !            35: 
        !            36: PVOID
        !            37: ImageDirectoryEntryToData (
        !            38:     IN PVOID Base,
        !            39:     IN BOOLEAN MappedAsImage,
        !            40:     IN USHORT DirectoryEntry,
        !            41:     OUT PULONG Size
        !            42:     );
        !            43: 
        !            44: PIMAGE_NT_HEADERS
        !            45: ImageNtHeader (
        !            46:     IN PVOID Base
        !            47:     )
        !            48: 
        !            49: /*++
        !            50: 
        !            51: Routine Description:
        !            52: 
        !            53:     This function returns the address of the NT Header.
        !            54: 
        !            55: Arguments:
        !            56: 
        !            57:     Base - Supplies the base of the image.
        !            58: 
        !            59: Return Value:
        !            60: 
        !            61:     Returns the address of the NT Header.
        !            62: 
        !            63: --*/
        !            64: 
        !            65: {
        !            66: 
        !            67:     PIMAGE_NT_HEADERS NtHeaders;
        !            68: 
        !            69:     if (Base != NULL &&
        !            70:         Base != (PVOID)-1
        !            71:        ) {
        !            72:         try {
        !            73:             if (((PIMAGE_DOS_HEADER)Base)->e_magic == IMAGE_DOS_SIGNATURE) {
        !            74:                 NtHeaders = (PIMAGE_NT_HEADERS)((PCHAR)Base + ((PIMAGE_DOS_HEADER)Base)->e_lfanew);
        !            75:                 if (NtHeaders->Signature == IMAGE_NT_SIGNATURE) {
        !            76:                     return NtHeaders;
        !            77:                 }
        !            78:             }
        !            79: 
        !            80:         } except(EXCEPTION_EXECUTE_HANDLER) {
        !            81:             return NULL;
        !            82:         }
        !            83: 
        !            84:     }
        !            85: 
        !            86:     return NULL;
        !            87: }
        !            88: 
        !            89: 
        !            90: PVOID
        !            91: ImageDirectoryEntryToData (
        !            92:     IN PVOID Base,
        !            93:     IN BOOLEAN MappedAsImage,
        !            94:     IN USHORT DirectoryEntry,
        !            95:     OUT PULONG Size
        !            96:     )
        !            97: 
        !            98: /*++
        !            99: 
        !           100: Routine Description:
        !           101: 
        !           102:     This function locates a Directory Entry within the image header
        !           103:     and returns either the virtual address or seek address of the
        !           104:     data the Directory describes.
        !           105: 
        !           106: Arguments:
        !           107: 
        !           108:     Base - Supplies the base of the image or data file.
        !           109: 
        !           110:     MappedAsImage - FALSE if the file is mapped as a data file.
        !           111:                   - TRUE if the file is mapped as an image.
        !           112: 
        !           113:     DirectoryEntry - Supplies the directory entry to locate.
        !           114: 
        !           115:     Size - Return the size of the directory.
        !           116: 
        !           117: Return Value:
        !           118: 
        !           119:     NULL - The file does not contain data for the specified directory entry.
        !           120: 
        !           121:     NON-NULL - Returns the address of the raw data the directory describes.
        !           122: 
        !           123: --*/
        !           124: 
        !           125: {
        !           126:     ULONG i, DirectoryAddress;
        !           127:     PIMAGE_NT_HEADERS NtHeaders;
        !           128:     PIMAGE_SECTION_HEADER NtSection;
        !           129: 
        !           130:     NtHeaders = ImageNtHeader(Base);
        !           131: 
        !           132:     if (DirectoryEntry >= NtHeaders->OptionalHeader.NumberOfRvaAndSizes) {
        !           133:         return( NULL );
        !           134:     }
        !           135: 
        !           136:     if (!(DirectoryAddress = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].VirtualAddress)) {
        !           137:         return( NULL );
        !           138:     }
        !           139:     *Size = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].Size;
        !           140:     if (MappedAsImage || DirectoryAddress < NtHeaders->OptionalHeader.SizeOfHeaders) {
        !           141:         return( (PVOID)((ULONG)Base + DirectoryAddress) );
        !           142:     }
        !           143: 
        !           144:     NtSection = (PIMAGE_SECTION_HEADER)((ULONG)NtHeaders +
        !           145:                         sizeof(ULONG) +
        !           146:                         sizeof(IMAGE_FILE_HEADER) +
        !           147:                         NtHeaders->FileHeader.SizeOfOptionalHeader
        !           148:                         );
        !           149: 
        !           150:     for (i=0; i<NtHeaders->FileHeader.NumberOfSections; i++) {
        !           151:         if (DirectoryAddress >= NtSection->VirtualAddress &&
        !           152:            DirectoryAddress < NtSection->VirtualAddress + NtSection->SizeOfRawData) {
        !           153:             return( (PVOID)((ULONG)Base + (DirectoryAddress - NtSection->VirtualAddress) + NtSection->PointerToRawData) );
        !           154:         }
        !           155:         ++NtSection;
        !           156:     }
        !           157:     return( NULL );
        !           158: }
        !           159: 
        !           160: 
        !           161: USHORT
        !           162: ChkSum(
        !           163:     ULONG PartialSum,
        !           164:     PUSHORT Source,
        !           165:     ULONG Length
        !           166:     )
        !           167: 
        !           168: /*++
        !           169: 
        !           170: Routine Description:
        !           171: 
        !           172:     Compute a partial checksum on a portion of an imagefile.
        !           173: 
        !           174: Arguments:
        !           175: 
        !           176:     PartialSum - Supplies the initial checksum value.
        !           177: 
        !           178:     Sources - Supplies a pointer to the array of words for which the
        !           179:         checksum is computed.
        !           180: 
        !           181:     Length - Supplies the length of the array in words.
        !           182: 
        !           183: Return Value:
        !           184: 
        !           185:     The computed checksum value is returned as the function value.
        !           186: 
        !           187: --*/
        !           188: 
        !           189: {
        !           190: 
        !           191:     //
        !           192:     // Compute the word wise checksum allowing carries to occur into the
        !           193:     // high order half of the checksum longword.
        !           194:     //
        !           195: 
        !           196:     while (Length--) {
        !           197:         PartialSum += *Source++;
        !           198:         PartialSum = (PartialSum >> 16) + (PartialSum & 0xffff);
        !           199:     }
        !           200: 
        !           201:     //
        !           202:     // Fold final carry into a single word result and return the resultant
        !           203:     // value.
        !           204:     //
        !           205: 
        !           206:     return (USHORT)(((PartialSum >> 16) + PartialSum) & 0xffff);
        !           207: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.