crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/Utilities/RawByteToTextConverter.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 using System.Collections;
       
    20 using System.Collections.Generic;
       
    21 
       
    22 namespace SymbianUtils.Utilities
       
    23 {
       
    24 	public class RawByteUtility
       
    25 	{
       
    26 		public static void ConvertToCharacter( byte aByte, StringBuilder aBuilder )
       
    27 		{
       
    28 			if	( aByte <= 32 || aByte > 126 )
       
    29 			{
       
    30 				aBuilder.Append( '.' );
       
    31 			}
       
    32 			else
       
    33 			{
       
    34 				aBuilder.Append( System.Convert.ToChar( aByte ) );
       
    35 			}
       
    36 		}
       
    37 
       
    38         public static string ConvertDataToText( IEnumerable<byte> aBytes, bool aFlushEntireList, ref uint aStartingAddress )
       
    39         {
       
    40             Queue<byte> queue = new Queue<byte>();
       
    41             foreach ( byte b in aBytes )
       
    42             {
       
    43                 queue.Enqueue( b );
       
    44             }
       
    45             return ConvertDataToText( queue, aFlushEntireList, ref aStartingAddress );
       
    46         }
       
    47 
       
    48         public static string ConvertDataToText( Queue<byte> aByteQueue, bool aFlushEntireQueue, ref uint aStartingAddress )
       
    49         {
       
    50             RawByteConverter converter = new RawByteConverter();
       
    51             string ret = converter.Convert( aByteQueue, aFlushEntireQueue, ref aStartingAddress );
       
    52             return ret;
       
    53         }
       
    54 
       
    55 		public static uint CombineBytes( uint aByte0, uint aByte1, uint aByte2, uint aByte3 )
       
    56 		{
       
    57 			aByte0 = ( aByte0 << 24 );
       
    58 			aByte1 = ( aByte1 << 16 );
       
    59 			aByte2 = ( aByte2 <<  8 );
       
    60 			//
       
    61 			uint ret = ( aByte0 + aByte1 + aByte2 + aByte3 );
       
    62 			//
       
    63 			return ret;
       
    64 		}
       
    65 
       
    66         public static string CreateHexData( byte aByte0, byte aByte1, byte aByte2, byte aByte3 )
       
    67 		{
       
    68 			StringBuilder charData = new StringBuilder();
       
    69 			//
       
    70 			charData.Append( aByte0.ToString("x2") + " " );
       
    71 			charData.Append( aByte1.ToString("x2") + " " );
       
    72 			charData.Append( aByte2.ToString("x2") + " " );
       
    73 			charData.Append( aByte3.ToString("x2") + " " );
       
    74 			//
       
    75 			string charDataString = charData.ToString();
       
    76 			return charDataString;
       
    77 		}
       
    78 
       
    79         public static string CreateCharacterisedData( uint aDWord )
       
    80         {
       
    81             byte b3 = (byte) (   aDWord & 0x000000FF );
       
    82             byte b2 = (byte) ( ( aDWord & 0x0000FF00 ) >>  8 );
       
    83             byte b1 = (byte) ( ( aDWord & 0x00FF0000 ) >> 16 );
       
    84             byte b0 = (byte) ( ( aDWord & 0xFF000000 ) >> 24 );
       
    85             //
       
    86             return CreateCharacterisedData( b0, b1, b2, b3 );
       
    87         }
       
    88 
       
    89         public static string CreateCharacterisedData( byte[] aBytes )
       
    90         {
       
    91             if ( aBytes.Length != 4 )
       
    92             {
       
    93                 throw new ArgumentException( "Expected 4 byte array" );
       
    94             }
       
    95 
       
    96             return CreateCharacterisedData( aBytes[ 0 ], aBytes[ 1 ], aBytes[ 2 ], aBytes[ 3 ] );
       
    97         }
       
    98 
       
    99 		public static string CreateCharacterisedData( byte aByte0, byte aByte1, byte aByte2, byte aByte3 )
       
   100 		{
       
   101 			StringBuilder charData = new StringBuilder();
       
   102 			//
       
   103 			ConvertToCharacter( aByte3, charData );
       
   104 			ConvertToCharacter( aByte2, charData );
       
   105 			ConvertToCharacter( aByte1, charData );
       
   106 			ConvertToCharacter( aByte0, charData );
       
   107 			//
       
   108 			string charDataString = charData.ToString();
       
   109 			return charDataString;
       
   110 		}
       
   111 	}
       
   112 
       
   113     public class RawByteConverter
       
   114     {
       
   115         #region Delegates & events
       
   116         public delegate void HandleLine( string aLine );
       
   117         public event HandleLine iLineHandler;
       
   118         #endregion
       
   119 
       
   120         #region Constructors
       
   121         public RawByteConverter()
       
   122         {
       
   123         }
       
   124         #endregion
       
   125 
       
   126         #region API
       
   127         public string Convert( Queue<byte> aByteQueue, bool aFlushEntireQueue, ref uint aStartingAddress )
       
   128         {
       
   129             const int KNumberOfBytesPerLine = 16;
       
   130 
       
   131             StringBuilder ret = new StringBuilder();
       
   132 
       
   133             // First try to build entire lines of 16 bytes
       
   134             while ( aByteQueue.Count >= KNumberOfBytesPerLine )
       
   135             {
       
   136                 StringBuilder byteVals = new StringBuilder();
       
   137                 byteVals.Append( aStartingAddress.ToString( "x8" ) + ": " );
       
   138                 int bytesProcessedForThisLine = 0;
       
   139                 //
       
   140                 StringBuilder byteChars = new StringBuilder();
       
   141                 while ( bytesProcessedForThisLine != KNumberOfBytesPerLine )
       
   142                 {
       
   143                     // Extract at most 4 bytes of data to process
       
   144                     byte b0 = aByteQueue.Dequeue();
       
   145                     byte b1 = aByteQueue.Dequeue();
       
   146                     byte b2 = aByteQueue.Dequeue();
       
   147                     byte b3 = aByteQueue.Dequeue();
       
   148 
       
   149                     // Create double-char hex representation of each character
       
   150                     byteVals.Append( RawByteUtility.CreateHexData( b0, b1, b2, b3 ) );
       
   151 
       
   152                     // Character representation of data...
       
   153                     byteChars.Append( RawByteUtility.CreateCharacterisedData( b0, b1, b2, b3 ) );
       
   154 
       
   155                     // Handle new line scenario
       
   156                     bytesProcessedForThisLine += 4;
       
   157                 }
       
   158 
       
   159                 byteVals.Append( byteChars.ToString() );
       
   160                 byteVals.Append( System.Environment.NewLine );
       
   161                 //
       
   162                 if ( iLineHandler != null )
       
   163                 {
       
   164                     iLineHandler( byteVals.ToString() );
       
   165                 }
       
   166                 //
       
   167                 ret.Append( byteVals.ToString() );
       
   168                 aStartingAddress += KNumberOfBytesPerLine;
       
   169             }
       
   170 
       
   171             // Extract remaining data only if the client specified that the entire queue should
       
   172             // be emptied
       
   173             int numberLeft = aByteQueue.Count;
       
   174             if ( aFlushEntireQueue && numberLeft > 0 && numberLeft < 16 )
       
   175             {
       
   176                 StringBuilder byteVals = new StringBuilder();
       
   177                 byteVals.Append( aStartingAddress.ToString( "x8" ) + ": " );
       
   178                 //
       
   179                 StringBuilder byteChars = new StringBuilder();
       
   180                 while ( aByteQueue.Count > 0 )
       
   181                 {
       
   182                     byte b0 = aByteQueue.Dequeue();
       
   183 
       
   184                     // Create double-char hex representation of each character
       
   185                     byteVals.Append( b0.ToString( "x2" ) + " " );
       
   186 
       
   187                     // Character representation of data...
       
   188                     RawByteUtility.ConvertToCharacter( b0, byteChars );
       
   189                 }
       
   190 
       
   191                 // We may need to pad some bytes
       
   192                 int padCount = KNumberOfBytesPerLine - numberLeft;
       
   193                 while ( padCount > 0 )
       
   194                 {
       
   195                     byteVals.Append( "  " + " " );
       
   196                     byteChars.Append( "." );
       
   197                     --padCount;
       
   198                 }
       
   199 
       
   200                 // Combine data
       
   201                 byteVals.Append( byteChars.ToString() );
       
   202                 byteVals.Append( System.Environment.NewLine );
       
   203                 //
       
   204                 if ( iLineHandler != null )
       
   205                 {
       
   206                     iLineHandler( byteVals.ToString() );
       
   207                 }
       
   208                 //
       
   209                 ret.Append( byteVals.ToString() );
       
   210                 aStartingAddress += (uint) numberLeft;
       
   211             }
       
   212 
       
   213             return ret.ToString();
       
   214         }
       
   215         #endregion
       
   216 
       
   217         #region Data members
       
   218         #endregion
       
   219     }
       
   220 }