home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / hd_pjr / part01 / hexprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-25  |  4.4 KB  |  172 lines

  1. /******************************************************************************
  2. *
  3. *   Copyright P.J.Ruczynski 1990
  4. *   This software is free for redistribution and re-use as long as this
  5. *   copyright is included in all source files. This software is supplied
  6. *   as is and no responsibilty is taken by the author for any problems
  7. *   arising from this code.
  8. *
  9. * File name        -  hexprint.c
  10. *
  11. * Module name        -  HEXPRINT
  12. *
  13. * Author        -  P.J.Ruczynski    <pjr@pyra.co.uk>
  14. *
  15. * First Release        -  16 Feb 1990
  16. *
  17. * Version number    -  1.4
  18. *
  19. * Description        -  A hexadecimal printing routine, will print the given
  20. *               buffer with the hex on the left and the ascii on the
  21. *               right.
  22. *
  23. *            Revision List
  24. *
  25. * pjr    08.05.89    Added offset numbering. This can be compiled out for
  26. *            optimum performance.
  27. *
  28. * pjr    11.05.89    Added compressed output format. This can be compiled
  29. *            out for optimum performance.
  30. *
  31. * pjr    02.08.89    Changed the bcopy and bcmp routines to use defines defined
  32. *            in hexprint.h and controlled by compilation flag 'ATT'. With this
  33. *            defined att routines are used, default means bsd routines are used.
  34. *            Note that the compression stuff uses BCMP and BCOPY.
  35. *
  36. * pjr    04.08.89    Added output to file capability. This can be compiled in
  37. *            by defining the H_FILEIO flag. Note that you also have to have
  38. *            a file open with an fd of lfp, although this can be changed by
  39. *            changing the decn below.
  40. *
  41. ******************************************************************************/
  42. #include <stdio.h>
  43. #include "hexprint.h"
  44.  
  45. #ifdef H_FILEIO
  46. extern FILE *lfp;
  47. #else
  48. #define lfp stdout
  49. #endif /* H_FILEIO */
  50.  
  51. #ifdef H_COMPRESS
  52. int hex_compression = FALSE;    /* compression is off as a default */
  53. #endif /* H_COMPRESS */
  54.  
  55. #ifdef H_OFFSETS
  56. int offset_print = FALSE;    /* only print offsets on request */
  57. long offset = 0L;        /* default start offset for printing */
  58. #endif /* H_OFFSET */
  59.  
  60. /*
  61.  * hexprint
  62.  *
  63.  * routine to print a buffer out in hex form with ascii form on
  64.  * the right hand side
  65.  */
  66. /*****************************************************************************/
  67. void hexprint(buf, buflen)
  68. /*****************************************************************************/
  69. char *buf;    /* ptr to data part of msg req, protocol format, to print     */
  70. int buflen;
  71. {
  72. int i,j;
  73. char string[H_SLEN];
  74.  
  75. #ifdef H_COMPRESS
  76. int done_compression = FALSE;    /* have we done any compression ? */
  77. char o_string[H_SLEN];            /* string prior to the current one */
  78. #endif /* H_COMPRESS */
  79.  
  80. #ifdef H_OFFSETS
  81. long o;
  82.     if (offset_print)
  83.     {
  84.         o = offset;
  85.         fprintf(lfp,"%5x  ",o);
  86.     }
  87. #endif /* H_OFFSETS */
  88.  
  89.     for (i=0; i<buflen; i++) {
  90.         string[i%H_SLEN] = *buf++;
  91.         if (i%H_SLEN == (H_SLEN-1)) {
  92. #ifdef H_OFFSETS
  93.             o += H_SLEN;
  94. #endif /* H_OFFSETS */
  95. #ifdef H_COMPRESS
  96.             if (hex_compression)
  97.                 if (i == (H_SLEN-1))  /* first time around ? */
  98.                     BCOPY(string, o_string, H_SLEN);
  99.                 else
  100.                 {
  101.  
  102.                 if (BCMP(o_string, string, H_SLEN) == 0)
  103.                     {
  104.                         fprintf(lfp,"* ");
  105.                         done_compression = TRUE;
  106.                         BCOPY(string, o_string, H_SLEN);
  107.                         continue;
  108.                     }
  109.                     else if (done_compression)
  110.                     {
  111.                         fprintf(lfp,"\n");
  112. #ifdef H_OFFSETS
  113.                         if (offset_print)
  114.                             fprintf(lfp, "%5x  ",o - H_SLEN);
  115. #endif /* H_OFFSETS */
  116.                         done_compression = FALSE;
  117.                     }
  118.                     BCOPY(string, o_string, H_SLEN);
  119.                 }
  120. #endif /* H_COMPRESS */
  121.             for (j=0; j<H_SLEN; j++)
  122.                 fprintf(lfp, "%2x ",(unsigned char)string[j]);
  123.  
  124.             for (j=0; j<H_SLEN; j++)
  125.                 if (string[j] >= 0x20 && string[j] <= 0x7e)
  126.                     fprintf(lfp, "%c",string[j]);
  127.                 else
  128.                     fprintf(lfp, ".");
  129.             fprintf(lfp, "\n");
  130. #ifdef H_OFFSETS
  131.             if ((i != (buflen -1)) && offset_print)
  132.                 fprintf(lfp, "%5x  ",o);
  133. #endif /* H_OFFSETS */
  134.         } else
  135.             if ((i%H_SLEN < (H_SLEN-1)) && (i == buflen-1)) {
  136. #ifdef H_COMPRESS
  137.                 if (done_compression)
  138.                 {
  139.                     fprintf(lfp, "\n");
  140. #ifdef H_OFFSETS
  141.                     if (offset_print)
  142.                         fprintf(lfp, "%5x  ",o);
  143. #endif /* H_OFFSETS */
  144.                     done_compression = FALSE;
  145.                 }
  146.                 BCOPY(string, o_string, H_SLEN);
  147. #endif /* H_COMPRESS */
  148.                 for (j=0; j<=(i%H_SLEN); j++)
  149.                     fprintf(lfp, "%2x ",(unsigned char)string[j]);
  150.  
  151.                 for (j=0; j<((H_SLEN-1)-(i%H_SLEN)); j++)
  152.                     fprintf(lfp, "   ");
  153.  
  154.                 for (j=0; j<=(i%H_SLEN); j++)
  155.                     if (string[j] >= 0x20 &&
  156.                         string[j] <= 0x7e)
  157.                         fprintf(lfp, "%c",string[j]);
  158.                     else
  159.                         fprintf(lfp, ".");
  160.  
  161.                 fprintf(lfp, "\n");
  162.             }
  163.     } /* end of for */
  164.  
  165. #ifdef H_COMPRESS
  166.     if (done_compression)
  167.         fprintf(lfp, "\n");
  168. #endif /* H_COMPRESS */
  169.  
  170. } /* end of hexprint */
  171.  
  172.