crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/RawItems/RawItem.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Text;
       
    19 
       
    20 namespace SymbianUtils.RawItems
       
    21 {
       
    22 	public class RawItem
       
    23 	{
       
    24 		#region Constructors
       
    25         public RawItem()
       
    26             : this( 0, 0, 0, "...." )
       
    27         {
       
    28         }
       
    29 
       
    30         public RawItem( uint aAddress, uint aReversedData, uint aOriginalData, string aCharacterisedData )
       
    31 		{
       
    32 			iAddress = aAddress;
       
    33 			iData = aReversedData;
       
    34 			iOriginalData = aOriginalData;
       
    35 			iCharacterisedData = aCharacterisedData;
       
    36 		}
       
    37 
       
    38         public RawItem( uint aAddress, uint aData )
       
    39         {
       
    40             iAddress = aAddress;
       
    41             iData = aData;
       
    42             
       
    43             // Reverse bytes to get the original data
       
    44             uint[] bytes = AsBytes( aData );
       
    45 			iOriginalData = SwapEndianness( bytes );
       
    46 
       
    47             // Get chars
       
    48             iCharacterisedData = ConvertToCharacters( bytes );
       
    49         }
       
    50 		#endregion
       
    51 
       
    52         #region API
       
    53         public int PrintableCharacterCount( bool aUnicode, ref int aDotCount )
       
    54         {
       
    55             int count = 0;
       
    56             //
       
    57             int start = ( aUnicode ) ? 1 : 0;
       
    58             int delta = ( aUnicode ) ? 2 : 1;
       
    59             int length = iCharacterisedData.Length;
       
    60             //
       
    61             for ( int i = start; i < length; i += delta )
       
    62             {
       
    63                 char c = iCharacterisedData[ i ];
       
    64                 int charAsInt = System.Convert.ToInt32( c );
       
    65 
       
    66                 if ( c >= 32 && c < 128 || c == 0x0D || c == 0x0A || c == 0x09 )
       
    67                 {
       
    68                     ++count;
       
    69                 }
       
    70                 if ( c == '.' )
       
    71                 {
       
    72                     ++aDotCount;
       
    73                 }
       
    74             }
       
    75             return count;
       
    76         }
       
    77         #endregion
       
    78 
       
    79         #region Properties
       
    80         public uint Address
       
    81 		{
       
    82 			get { return iAddress; }
       
    83 			set { iAddress = value; }
       
    84 		}
       
    85 
       
    86 		public uint Data
       
    87 		{
       
    88 			get { return iData; }
       
    89 			set { iData = value; }
       
    90 		}
       
    91 
       
    92         public byte[] DataArray
       
    93         {
       
    94             get
       
    95             {
       
    96                 byte[] ret = new byte[ 4 ];
       
    97                 //
       
    98                 ret[ 0 ] = (byte) ( ( iData >> 0 ) & 0xFF );
       
    99                 ret[ 1 ] = (byte) ( ( iData >> 8 ) & 0xFF );
       
   100                 ret[ 2 ] = (byte) ( ( iData >> 16 ) & 0xFF );
       
   101                 ret[ 3 ] = (byte) ( ( iData >> 24 ) & 0xFF );
       
   102                 //
       
   103                 return ret;
       
   104             }
       
   105         }
       
   106 
       
   107         public ushort[] DataArrayWords
       
   108         {
       
   109             get
       
   110             {
       
   111                 ushort[] ret = new ushort[ 2 ];
       
   112                 //
       
   113                 ret[ 0 ] = (ushort) ( ( iData )       & 0x0000FFFF );
       
   114                 ret[ 1 ] = (ushort) ( ( iData >> 16 ) & 0x0000FFFF );
       
   115                 //
       
   116                 return ret;
       
   117             }
       
   118         }
       
   119 
       
   120 		public uint OriginalData
       
   121 		{
       
   122 			get { return iOriginalData; }
       
   123 			set { iOriginalData = value; }
       
   124 		}
       
   125 
       
   126 		public string CharacterisedData
       
   127 		{
       
   128 			get { return iCharacterisedData; }
       
   129 			set { iCharacterisedData = value; }
       
   130 		}
       
   131 
       
   132 		public string OriginalCharacterisedData
       
   133 		{
       
   134 			get 
       
   135 			{
       
   136 				char[] reversedCharacterisedData = new char[ iCharacterisedData.Length ];
       
   137 				for (int i = 0; i < iCharacterisedData.Length; i+=4)
       
   138 				{
       
   139 					string bytes = iCharacterisedData.Substring(i, 4);
       
   140 					reversedCharacterisedData[i]   = bytes[3];
       
   141 					reversedCharacterisedData[i+1] = bytes[2];
       
   142 					reversedCharacterisedData[i+2] = bytes[1];
       
   143 					reversedCharacterisedData[i+3] = bytes[0];
       
   144 				}
       
   145 				//
       
   146 				string characterisedData = new string(reversedCharacterisedData);
       
   147 				return characterisedData;
       
   148 			}
       
   149 		}
       
   150 
       
   151 		public string OriginalCharacterisedDataAsUnicode
       
   152 		{
       
   153 			get 
       
   154 			{
       
   155                 StringBuilder text = new StringBuilder( OriginalCharacterisedData );
       
   156 				for( int i = text.Length-1; i >= 0; i-=2 )
       
   157 				{
       
   158                     text.Remove( i, 1 );
       
   159 				}
       
   160                 string ret = text.ToString();
       
   161                 return ret;
       
   162 			}
       
   163 		}
       
   164 
       
   165         public object Tag
       
   166 		{
       
   167 			get { return iTag; }
       
   168 			set { iTag = value; }
       
   169 		}
       
   170 
       
   171         public static uint RoundToNearestDWord( uint aValue )
       
   172         {
       
   173             uint dwords = aValue / KSizeOfOneRawItemInBytes;
       
   174             uint ret = dwords * KSizeOfOneRawItemInBytes;
       
   175             uint remainder = aValue % KSizeOfOneRawItemInBytes;
       
   176             //
       
   177             if ( remainder > 0 )
       
   178             {
       
   179                 ret += KSizeOfOneRawItemInBytes;
       
   180             }
       
   181             //
       
   182             return ret;
       
   183         }
       
   184 		#endregion
       
   185 
       
   186         #region Constants
       
   187         public const uint KSizeOfOneRawItemInBytes = 4;
       
   188         #endregion
       
   189 
       
   190         #region Internal methods
       
   191         private static uint[] AsBytes( uint aValue )
       
   192         {
       
   193             uint[] ret = new uint[4];
       
   194             //
       
   195             ret[ 0 ] = ( aValue ) & 0xFF;
       
   196             ret[ 1 ] = ( aValue >> 8 ) & 0xFF;
       
   197             ret[ 2 ] = ( aValue >> 16 ) & 0xFF;
       
   198             ret[ 3 ] = ( aValue >> 24 ) & 0xFF;
       
   199             //
       
   200             return ret;
       
   201         }
       
   202 
       
   203         private static uint SwapEndianness( uint aValue )
       
   204         {
       
   205             uint[] bytes = AsBytes( aValue );
       
   206             return SwapEndianness( bytes );
       
   207         }
       
   208 
       
   209         private static uint SwapEndianness( uint[] aBytes )
       
   210         {
       
   211             System.Diagnostics.Debug.Assert( aBytes.Length == KSizeOfOneRawItemInBytes );
       
   212             uint ret = 0;
       
   213             //
       
   214             ret += ( aBytes[ 3 ] <<  0 );
       
   215             ret += ( aBytes[ 2 ] <<  8 );
       
   216             ret += ( aBytes[ 1 ] << 16 );
       
   217             ret += ( aBytes[ 0 ] << 24 );
       
   218             //
       
   219             return ret;
       
   220         }
       
   221 
       
   222         private static string ConvertToCharacters( uint[] aBytes )
       
   223         {
       
   224             StringBuilder ret = new StringBuilder();
       
   225             //
       
   226             foreach ( uint val in aBytes )
       
   227             {
       
   228                 char c = System.Convert.ToChar( val );
       
   229                 //
       
   230     			if ( c <= 0x20 || c >= 0x7f || c == '%' )
       
   231                 {
       
   232                     c = '.';
       
   233                 }
       
   234                 //
       
   235                 ret.Insert( 0, c );
       
   236             }
       
   237             //
       
   238             return ret.ToString();
       
   239         }
       
   240         #endregion
       
   241 
       
   242         #region Data members
       
   243         private object iTag = null;
       
   244 		private uint iAddress;
       
   245 		private uint iData;
       
   246 		private uint iOriginalData;
       
   247 		private string iCharacterisedData = string.Empty;
       
   248 		#endregion
       
   249 	}
       
   250 }