bintools/petools/pefile/pe_utl.cpp
changeset 600 6d08f4a05d93
equal deleted inserted replaced
599:fa7a3cc6effd 600:6d08f4a05d93
       
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <e32std_private.h>
       
    18 #include "pe_file.h"
       
    19 #include <string.h>
       
    20 #include "e32image.h"
       
    21 #include "h_utl.h"
       
    22 
       
    23 TInt PEFile::CmpSectionName(PIMAGE_SECTION_HEADER apSectionHeader, char *aName)
       
    24 //
       
    25 // Returns true if the name of the pe section is the same as aName
       
    26 //
       
    27 	{
       
    28 
       
    29 	return (strnicmp((const char *)apSectionHeader->Name, aName, IMAGE_SIZEOF_SHORT_NAME)==0);
       
    30 	}
       
    31 
       
    32 TInt PEFile::VirtualAddressInSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
       
    33 //
       
    34 // Returns true if the virtual address is in the section
       
    35 //
       
    36 	{
       
    37 
       
    38 	TUint start = iLinkedBase + aHeader->VirtualAddress;
       
    39 	TUint finish = start + aHeader->Misc.VirtualSize;
       
    40 	return (aVA>=start) && (aVA<finish);
       
    41 	}
       
    42 
       
    43 TUint PEFile::DistanceFromSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
       
    44 //
       
    45 // Returns the minimum distance from aVA to any address in the specified section
       
    46 //
       
    47 	{
       
    48 
       
    49 	TUint start = iLinkedBase + aHeader->VirtualAddress;
       
    50 	TUint finish = start + aHeader->Misc.VirtualSize;
       
    51 	if (aVA>=start)
       
    52 		{
       
    53 		if (aVA<finish)
       
    54 			return 0;
       
    55 		return aVA - finish + 1;
       
    56 		}
       
    57 	return start - aVA;
       
    58 	}
       
    59 
       
    60 TInt PEFile::FindSectionByVa(TUint aVA, TUint aTryToBeClever)
       
    61 	{
       
    62 	TInt i;
       
    63 	TInt s = -1;
       
    64 	if (aTryToBeClever == 0)
       
    65 		{
       
    66 		for (i=0; i<KNumberOfSections; i++)
       
    67 			if (iSectionHeader[i])
       
    68 				if (VirtualAddressInSection(aVA, iSectionHeader[i]))
       
    69 					s=i;
       
    70 		}
       
    71 	if (aTryToBeClever == 1)
       
    72 		{
       
    73 		// Find the minimum distance from the specified address to any section
       
    74 		TUint dist = KMaxTUint;
       
    75 		for (i=0; i<KNumberOfSections; i++)
       
    76 			{
       
    77 			if (!iSectionHeader[i])
       
    78 				continue;
       
    79 			TUint d = DistanceFromSection(aVA, iSectionHeader[i]);
       
    80 			if (d < dist)
       
    81 				{
       
    82 				dist = d;
       
    83 				s = i;
       
    84 				}
       
    85 			else if (d == dist)
       
    86 				{
       
    87 				s = -1;	// Ambiguous :-(
       
    88 				}
       
    89 			}
       
    90 		if (dist >= 0x1000u)
       
    91 			s = -1;		// too far for comfort
       
    92 		}
       
    93 	return s;
       
    94 	}