dump_records_to_html.c

Dump STDF files to a nice html page for comparing the raw binary with human readable ASCII

00001 
00004 /*
00005  * Copyright (C) 2004-2006 Mike Frysinger <vapier@gmail.com>
00006  * Released under the BSD license.  For more information,
00007  * please see: http://opensource.org/licenses/bsd-license.php
00008  *
00009  * $Header: /cvsroot/freestdf/libstdf/examples/dump_records_to_html.c,v 1.13 2005/12/08 04:07:43 vapier Exp $
00010  */
00011 
00012 #include <libstdf.h>
00013 
00014 #if defined(HAVE_GETOPT_H)
00015 # include <getopt.h>
00016 #endif
00017 
00018 #define MAX_REC_STYLES  4
00019 int max_width, width, rec_rot;
00020 rec_unknown *raw_rec;
00021 
00022 #define OUT_HEX         1
00023 #define OUT_ASCII       2
00024 
00025 void write_rec(FILE *f, rec_header *h, int type)
00026 {
00027     int i;
00028     int towrite, written;
00029     byte_t *rec;
00030     int tagged;
00031 
00032     rec = raw_rec->data;
00033     written = 0;
00034     tagged = 0;
00035     h->REC_LEN += 4;
00036 
00037     do {
00038         towrite = max_width - width;
00039         if (h->REC_LEN < written + towrite)
00040             towrite = h->REC_LEN - written;
00041         for (i=0; i<towrite; ++i) {
00042             if (tagged > 3) {                       /* raw data */
00043                 fprintf(f, "<td class=r%i>", rec_rot);
00044                 if (type == OUT_HEX)
00045                     fprintf(f, "%.2X", rec[i]);
00046                 else {
00047                     if (rec[i] < 0x20 || rec[i] > 0x7F)
00048                         fprintf(f, "%.2X", rec[i]);
00049                     else
00050                         fprintf(f, "%c", rec[i]);
00051                 }
00052                 fprintf(f, "</td>");
00053             } else {                                /* record header */
00054                 if (type == OUT_HEX) {
00055                     fprintf(f, "<td class=r%i><span class='head", rec_rot);
00056                     fprintf(f, (tagged<2)?"len":"type");
00057                     fprintf(f, "'>%.2X</span></td>", rec[i]);
00058                 } else {
00059                     if (tagged == 0)        /* rec len */
00060                         fprintf(f, "<td class=r%i colspan=2><span class=headlen>%i</span></td>",
00061                                 rec_rot, h->REC_LEN - 4);
00062                     else if (tagged == 2)   /* rec type */
00063                         fprintf(f, "<td class=r%i colspan=2><span class=headtype>%s</span></td>",
00064                                 rec_rot, stdf_get_rec_name_from_head((*h)));
00065                     else if (width == 0 && i == 0)
00066                         fprintf(f, "<td class=r%i></td>", rec_rot);
00067                 }
00068                 tagged++;
00069             }
00070         }
00071         width += towrite;
00072         rec += towrite;
00073         written += towrite;
00074         if (width == max_width) {
00075             fprintf(f, "</tr>\n<tr>");
00076             width = 0;
00077         }
00078     } while (written < h->REC_LEN);
00079 }
00080 
00081 void usage(char *prog)
00082 {
00083     printf("Usage: %s [options] <stdf file> <html file>\n"
00084 #if defined(HAVE_GETOPT_H)
00085            "Options:\n"
00086            "\t-h\tthis screen\n"
00087            "\t-c\t# of records to output (default is 25; 0 to show all)\n"
00088            "\t-w\twidth of output (default is 25)\n"
00089 #else
00090            "\nin the excellent words of netcat:\n"
00091            "/* If your shitbox doesn't have getopt, step into the nineties already. */\n\n"
00092 #endif
00093            , prog);
00094 }
00095 
00096 int main(int argc, char *argv[])
00097 {
00098     stdf_file *f;
00099     char cpu_name[256];
00100     FILE *out;
00101     int x, rec_count, max_recs, type;
00102     dtc_U4 byte_order, stdf_ver;
00103 
00104     max_recs = 25;
00105     max_width = 25;
00106 #if defined(HAVE_GETOPT_H)
00107     while ((x=getopt(argc, argv, "c:w:h")) != EOF) {
00108         switch (x) {
00109             case 'c':
00110                 max_recs = atoi(optarg);
00111                 break;
00112             case 'w':
00113                 max_width = atoi(optarg);
00114                 break;
00115             case 'h':
00116                 usage(argv[0]);
00117                 return EXIT_SUCCESS;
00118             default:
00119                 usage(argv[0]);
00120                 return EXIT_FAILURE;
00121         }
00122     }
00123     x = argc - optind;
00124 #else
00125     x = argc - 1;
00126 #endif
00127 
00128     if (x != 2) {
00129         if (x == 0)
00130             fprintf(stderr, "Missing source/destination files!\n");
00131         else if (x == 1)
00132             fprintf(stderr, "Missing destination file!\n");
00133         else
00134             fprintf(stderr, "Too many arguements!\n");
00135         usage(argv[0]);
00136         return EXIT_FAILURE;
00137     }
00138 #if defined(HAVE_GETOPT_H)
00139     x = optind;
00140 #else
00141     x = 1;
00142 #endif
00143 
00144     f = stdf_open(argv[x]);
00145     if (!f) {
00146         perror("Could not stdf_open file");
00147         return EXIT_FAILURE;
00148     }
00149     stdf_get_setting(f, STDF_SETTING_VERSION, &stdf_ver);
00150     stdf_get_setting(f, STDF_SETTING_BYTE_ORDER, &byte_order);
00151     if (byte_order == LITTLE_ENDIAN)
00152         sprintf(cpu_name, "Little Endian [intel/x86]");
00153     else if (byte_order == BIG_ENDIAN)
00154         sprintf(cpu_name, "Big Endian [sun/sparc]");
00155     else
00156         sprintf(cpu_name, "Unknown Endian [???]");
00157 
00158     if ((out=fopen(argv[x+1], "w")) == NULL) {
00159         perror("Could not open html file");
00160         return EXIT_FAILURE;
00161     }
00162 
00163     fprintf(out,
00164             "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>\n"
00165             "<html>\n"
00166             "<head>\n"
00167             " <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'\n>"
00168             " <title>%s</title>\n"
00169             " <style type='text/css'>\n"
00170             "  table { border-collapse:collapse; font-family:monospace; }\n"
00171             "  td { border: 1px solid #C0C0C0; text-align:center; }\n"
00172             "  th { border: 1px solid black; text-align:center; }\n"
00173             "  td.r1 { background-color: #DDDAEC; }\n"
00174             "  td.r2 { background-color: #D4FFA9; }\n"
00175             "  td.r3 { background-color: #FED0D4; }\n"
00176             "  td.r4 { background-color: #FEFFC5; }\n"
00177             "  span.headlen { font-weight:bolder; }\n"
00178             "  span.headtype { font-style:italic; font-weight:bolder; }\n"
00179             " </style>\n"
00180             "</head>\n"
00181             "<body>\n"
00182             "<h1>File: %s<br>STDF v%i<br>CPU Type: %i (%s)</h1>\n"
00183             "<table><tr>\n",
00184             argv[x], argv[x], stdf_ver, byte_order, cpu_name);
00185 
00186     for (type=1; type<3; type++) {
00187         stdf_close(f);
00188         f = stdf_open(argv[x]);
00189 
00190         width = 0;
00191         rec_count = max_recs;
00192         rec_rot = 1;
00193 
00194         fprintf(out, "<td><table>\n<tr>");
00195         for (width=0; width<max_width; ++width)
00196             if (type == OUT_HEX)
00197                 fprintf(out, "<th>%.2X</th>", width);
00198             else
00199                 fprintf(out, "<th>%i</th>", width);
00200         fprintf(out, "</tr>\n<tr>");
00201 
00202         while ((raw_rec=stdf_read_record_raw(f)) != NULL) {
00203             write_rec(out, &(raw_rec->header), type);
00204             stdf_free_record(raw_rec);
00205             if (--rec_count == 0)
00206                 break;
00207             if (++rec_rot > MAX_REC_STYLES)
00208                 rec_rot = 1;
00209         }
00210         if (width != 0)
00211             fprintf(out, "</tr>\n");
00212         fprintf(out, "</table></td>\n");
00213     }
00214 
00215     fprintf(out,
00216             "</tr></table>\n"
00217             "</body>\n"
00218             "</html>");
00219 
00220     fclose(out);
00221     stdf_close(f);
00222 
00223     return EXIT_SUCCESS;
00224 }

Generated on Thu Jun 8 14:05:35 2006 for libstdf by  doxygen 1.4.6