toolsandutils/e32tools/pefile/pe_utl.cpp
changeset 0 83f4b4db085c
child 10 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     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 
       
    22 TInt PEFile::CmpSectionName(PIMAGE_SECTION_HEADER apSectionHeader, char *aName)
       
    23 //
       
    24 // Returns true if the name of the pe section is the same as aName
       
    25 //
       
    26 	{
       
    27 
       
    28 	return (strnicmp((const char *)apSectionHeader->Name, aName, IMAGE_SIZEOF_SHORT_NAME)==0);
       
    29 	}
       
    30 
       
    31 TInt PEFile::VirtualAddressInSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
       
    32 //
       
    33 // Returns true if the virtual address is in the section
       
    34 //
       
    35 	{
       
    36 
       
    37 	TUint start = iLinkedBase + aHeader->VirtualAddress;
       
    38 	TUint finish = start + aHeader->Misc.VirtualSize;
       
    39 	return (aVA>=start) && (aVA<finish);
       
    40 	}
       
    41 
       
    42 TUint PEFile::DistanceFromSection(TUint aVA, PIMAGE_SECTION_HEADER aHeader)
       
    43 //
       
    44 // Returns the minimum distance from aVA to any address in the specified section
       
    45 //
       
    46 	{
       
    47 
       
    48 	TUint start = iLinkedBase + aHeader->VirtualAddress;
       
    49 	TUint finish = start + aHeader->Misc.VirtualSize;
       
    50 	if (aVA>=start)
       
    51 		{
       
    52 		if (aVA<finish)
       
    53 			return 0;
       
    54 		return aVA - finish + 1;
       
    55 		}
       
    56 	return start - aVA;
       
    57 	}
       
    58 
       
    59 TInt PEFile::FindSectionByVa(TUint aVA, TUint aTryToBeClever)
       
    60 	{
       
    61 	TInt i;
       
    62 	TInt s = -1;
       
    63 	if (aTryToBeClever == 0)
       
    64 		{
       
    65 		for (i=0; i<KNumberOfSections; i++)
       
    66 			if (iSectionHeader[i])
       
    67 				if (VirtualAddressInSection(aVA, iSectionHeader[i]))
       
    68 					s=i;
       
    69 		}
       
    70 	if (aTryToBeClever == 1)
       
    71 		{
       
    72 		// Find the minimum distance from the specified address to any section
       
    73 		TUint dist = KMaxTUint;
       
    74 		for (i=0; i<KNumberOfSections; i++)
       
    75 			{
       
    76 			if (!iSectionHeader[i])
       
    77 				continue;
       
    78 			TUint d = DistanceFromSection(aVA, iSectionHeader[i]);
       
    79 			if (d < dist)
       
    80 				{
       
    81 				dist = d;
       
    82 				s = i;
       
    83 				}
       
    84 			else if (d == dist)
       
    85 				{
       
    86 				s = -1;	// Ambiguous :-(
       
    87 				}
       
    88 			}
       
    89 		if (dist >= 0x1000u)
       
    90 			s = -1;		// too far for comfort
       
    91 		}
       
    92 	return s;
       
    93 	}