Search Code
About crypto

Published 29 Dec 2009.

Last change message: N/A

Import this program

crypto

Published 29 Dec 2009, by   user Anders Rundgren   tag No tags
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HexDump.cpp Source File

HexDump.cpp

00001 #include <stdio.h>
00002 
00003 static void hex (int i)
00004   {
00005     if (i < 10)
00006       {
00007         putchar (i + 48);
00008       }
00009     else
00010       {
00011         putchar (i + 55);
00012       }
00013   }
00014 
00015 static void twohex (int i)
00016   {
00017     i &= 0xFF;
00018     hex (i / 16);
00019     hex (i % 16);
00020   }
00021 
00022 static  void addrhex (int i)
00023   {
00024     if (i > 65535)
00025       {
00026         twohex (i / 65536);
00027         i %= 65536;
00028       }
00029     twohex (i / 256);
00030     twohex (i % 256);
00031   }
00032 
00033 #include "hexdump.h"
00034 
00035 namespace webpki
00036 {
00037 
00038 void HexDump::printDebugData (const unsigned char *indata, const int length, int bytes_per_line)
00039   {
00040     int index = 0;
00041     int i = 0;
00042     if (length == 0)
00043       {
00044         printf ("No data");
00045         return;
00046       }
00047     bool only_data = false;
00048     if (bytes_per_line < 0)
00049       {
00050         bytes_per_line = -bytes_per_line;
00051         only_data = true;
00052       }
00053     while (index < length)
00054       {
00055         if (index > 0)
00056           {
00057             putchar ('\n');
00058           }
00059         addrhex (index);
00060         putchar (':');
00061         int q = length - index;
00062         if (q > bytes_per_line)
00063           {
00064             q = bytes_per_line;
00065           }
00066         for(i = 0; i < q; i++)
00067           {
00068             putchar (' ');
00069             twohex (indata[index + i]);
00070           }
00071         if (only_data)
00072           {
00073             index += q;
00074             continue;
00075           }
00076         while (i++ <= bytes_per_line)
00077           {
00078             putchar (' ');
00079             putchar (' ');
00080             putchar (' ');
00081           }
00082         putchar ('\'');
00083         for(i = 0; i < q; i++)
00084           {
00085             int c = indata[index++];
00086             if (c < 32 || c >= 127)
00087               {
00088                 putchar ('.');
00089               }
00090             else
00091               {
00092                 putchar (c);
00093               }
00094           }
00095         putchar ('\'');
00096         while (i++ < bytes_per_line)
00097           {
00098             putchar (' ');
00099           }
00100       }
00101   }
00102 
00103 
00104 void HexDump::printHexString (const unsigned char *indata, int length)
00105   {
00106     int i = 0;
00107     while (i < length)
00108       {
00109         twohex (indata[i++]);
00110       }
00111   }
00112 
00113 }
00114 
00115