1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*
* file viewer (hex dump)
* quick & dirty for now, handling xxl files doesn't work very well ...
*
* (c) 2001 Gerd Hoffmann <gerd@kraxel.org>
*
*/
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/ScrolledW.h>
#include <Xm/SelectioB.h>
#include "RegEdit.h"
#include "hex.h"
extern Display *dpy;
extern Widget app_shell;
/*----------------------------------------------------------------------*/
static Widget hex_dlg, hex_label;
static void
hex_destroy(Widget widget, XtPointer clientdata, XtPointer call_data)
{
XtDestroyWidget(hex_dlg);
hex_dlg = NULL;
}
static XmString
hexify(int addr,unsigned char *data, int bytes)
{
XmString xmpage,xmchunk;
char line[120];
int len,y,i;
xmpage = XmStringGenerate("", NULL, XmMULTIBYTE_TEXT, NULL);
for (y = 0; y < bytes; y += 16) {
/* hexify line */
len = sprintf(line,"%04x ",addr+y);
for (i = 0; i < 16; i++) {
if (y+i < bytes)
len += sprintf(line+len,"%02x ",data[y+i]);
else
len += sprintf(line+len," ");
if (3 == i%4)
len += sprintf(line+len," ");
}
for (i = 0; i < 16; i++) {
if (y+i < bytes)
if (isprint(data[y+i]))
len += sprintf(line+len,"%c",data[y+i]);
else
len += sprintf(line+len,".");
else
len += sprintf(line+len," ");
}
len += sprintf(line+len,"\n");
/* add last chunk for line */
xmchunk = XmStringGenerate(line, NULL, XmMULTIBYTE_TEXT, NULL);
xmpage = XmStringConcatAndFree(xmpage, xmchunk);
}
return xmpage;
}
/* guess whenever we have ascii or binary data */
static int
is_binary(unsigned char *data, int bytes)
{
int i, bin;
bin = 0;
for (i = 0; i < 64 && i < bytes; i++) {
if (!isprint(data[i]) &&
data[i] != '\t' &&
data[i] != '\n')
bin = 1;
}
return bin;
}
void
hex_display(char *filename)
{
Widget view;
XmString xmpage,chunk;
unsigned char data[32768+1];
int chars;
FILE *fp;
/* build dialog */
if (!hex_dlg) {
hex_dlg = XmCreatePromptDialog(app_shell,"hex",NULL,0);
XmdRegisterEditres(XtParent(hex_dlg));
XtUnmanageChild(XmSelectionBoxGetChild(hex_dlg,
XmDIALOG_SELECTION_LABEL));
XtUnmanageChild(XmSelectionBoxGetChild(hex_dlg,XmDIALOG_HELP_BUTTON));
XtUnmanageChild(XmSelectionBoxGetChild(hex_dlg,
XmDIALOG_CANCEL_BUTTON));
XtUnmanageChild(XmSelectionBoxGetChild(hex_dlg,XmDIALOG_TEXT));
XtAddCallback(hex_dlg,XmNokCallback,hex_destroy,NULL);
view = XmCreateScrolledWindow(hex_dlg,"view",NULL,0);
XtManageChild(view);
hex_label = XtVaCreateManagedWidget("label", xmLabelWidgetClass,
view, NULL);
}
XtManageChild(hex_dlg);
/* read the file (32k max) */
fp = fopen(filename,"r");
memset(data,0,sizeof(data));
chars = fread(data, 1, sizeof(data)-1, fp);
if (is_binary(data,chars)) {
xmpage = hexify(0, data, chars);
if (sizeof(data)-1 == chars) {
chunk = XmStringGenerate("[ ... ]\n",NULL,
XmMULTIBYTE_TEXT, NULL);
xmpage = XmStringConcatAndFree(xmpage,chunk);
}
} else {
xmpage = XmStringGenerate(data, NULL, XmMULTIBYTE_TEXT, NULL);
if (sizeof(data)-1 == chars) {
chunk = XmStringGenerate("\n[ ... ]\n",NULL,
XmMULTIBYTE_TEXT, NULL);
xmpage = XmStringConcatAndFree(xmpage,chunk);
}
}
XtVaSetValues(hex_label,XmNlabelString,xmpage,NULL);
XmStringFree(xmpage);
XtVaSetValues(XtParent(hex_dlg),XmNtitle,filename,NULL);
fclose(fp);
}
|