#include <share.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
FILE *f;
char buffer[4096+16];
int r,a;
int bigedian=0;
int uoff=0;
int get_aqw(unsigned char c,int aqw) {//0-ascii|1-q|2-w
switch (aqw) {
case 0:if (c> 0x80) aqw=1; else aqw=0; return aqw;
case 1:if (c>=0x40) aqw=2; else aqw=0; return aqw;
case 2:if (c> 0x80) aqw=1; else aqw=0; return aqw;
}
return aqw;
}
void HexDump(char *buf,int len,int addr) {
int i,j,k,r,kk,dk,aqw,aqw0,m;
char t;
char binstr[113+1];
char ln[16+2+1];
char bb[16];
WCHAR W[16+2+1];
BOOL b=TRUE;
char Wdf[2]={'\xa1','\xf5'};
unsigned char c0,c1;
//000000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................. 哈哈哈哈哈哈哈哈 .................
// 9 54 59 7678 96 113
ln[17]=0;
kk=0;
aqw=0;
aqw0=0;
for (i=0;i<len;i++) {
aqw=get_aqw((unsigned char)buf[i],aqw);
if (0==(i%16)) {
aqw0=aqw;
sprintf(binstr,"%08x ",i+addr);
sprintf(binstr+9," %02x",(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr+54," %02x ",(unsigned char)buf[i]);
memcpy(ln,buf+i-15,17);
for (j=0;j<17;j++) if ((unsigned)ln[j]<' ') ln[j]='.';
if (aqw!=1) ln[16]=' ';
if (aqw0==2) ln[0]=' ';
sprintf(binstr+59,"%s",ln);
memset(binstr+76,' ',37);
memcpy(bb,buf+(i-15)+uoff,16);
if (bigedian) for (m=0;m<15;m+=2) {t=bb[m];bb[m]=bb[m+1];bb[m+1]=t;}
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)bb,8,(LPSTR)(binstr+78),16,(LPCSTR)Wdf,&b);
for (j=78;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
c0=(unsigned)buf[i];
c1=(unsigned)buf[i-1];
if (c0>=0xE0 && i<len-2) dk=2;
else if (c1>=0xE0 && i<len-1) dk=1;
else dk=0;
r=MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-15+kk],16+dk-kk,(LPWSTR)W,0);
MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-15+kk],16+dk-kk,(LPWSTR)W,r);
r=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,r,(LPSTR)(binstr+96+kk),0,(LPCSTR)Wdf,&b);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,r,(LPSTR)(binstr+96+kk),r,(LPCSTR)Wdf,&b);
for (j=96+kk+r;j<113;j++) binstr[j]=' ';
for (j=96+kk ;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
binstr[113]=0;
printf("%s\n",binstr);
kk=dk;
} else if (8==(i%16)) {
sprintf(binstr+9+(i%16)*3,"-%02x",(unsigned char)buf[i]);
} else {
sprintf(binstr+9+(i%16)*3," %02x",(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
k=16-k;
memset(ln,' ',17);
memcpy(ln,buf+i-k,k);
if (aqw0==2) ln[0]=' ';
for (j=0;j<k;j++) if ((unsigned)ln[j]<' ') ln[j]='.';
sprintf(binstr,"%s %s",binstr,ln);
memset(binstr+76,' ',37);
memcpy(bb,buf+(i-k)+uoff,k);
if (bigedian) for (m=0;m<k;m+=2) {t=bb[m];bb[m]=bb[m+1];bb[m+1]=t;}
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)&bb,k/2,(LPSTR)(binstr+78),16,(LPCSTR)Wdf,&b);
for (j=78;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
r=MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-k+kk],k-kk,(LPWSTR)W,0);
MultiByteToWideChar(65001,0,(LPCSTR)&buf[i-k+kk],k-kk,(LPWSTR)W,r);
r=WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,r,(LPSTR)(binstr+96+kk),0,(LPCSTR)Wdf,&b);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK|WC_DEFAULTCHAR,(LPCWSTR)W,r,(LPSTR)(binstr+96+kk),r,(LPCSTR)Wdf,&b);
for (j=96+kk+r;j<113;j++) binstr[j]=' ';
for (j=96+kk ;j<113;j++) if ((unsigned)binstr[j]<' ') binstr[j]='.';
binstr[113]=0;
printf("%s\n",binstr);
}
}
int main(int argc,char **argv) {
if (argc<2) {
fprintf(stderr,"Usage: %s filename.ext [b|1]\n",argv[0]);
return 2;
}
f=_fsopen(argv[1],"rb",_SH_DENYNO);
if (NULL==f) {
fprintf(stderr,"Can not open file [%s]!\n",argv[1]);
return 1;
}
if (argc>=3) {
if (strchr(argv[2],'b')) bigedian=1;
if (strchr(argv[2],'1')) uoff=1;
}
printf("=Offset= ===================HexBytes==================== ======ANSI======= =====Unicode==== ======UTF8=======\n");
//000000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................. 哈哈哈哈哈哈哈哈 .................
a=0;
memset(buffer+4096,' ',16);
while (1) {
r=fread(buffer,1,4096,f);
HexDump(buffer,r,a);
a+=r;
if (r<4096) break;
}
fclose(f);
return 0;
}