📄 a_dumphex.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2006 Dump file in hexidecimal value Each line dumped is like: Offset Hexidecimal value ASCII +-------+ +---------------------------------------------+ +--------------+ 0000:0000 30 31 32 33 34 35 36 37-38 39 61 62 63 64 65 66 0123456789abcdef Build: make a_dumphex (g++ a_dumphex.cpp -pthread -lwy)*/#include "../src/wy_uty.h"#include "../src/wyregfile.h"#include <cctype> static const size_t Radix=16; // radix used in the dumpstatic const size_t DumpLineLength=16; // max. bytes dump'd in one line// [Syn] Convert bytes pointed by ldata to text string suitable for the dump and// save into output. off_cnt is the file offset of the data pointed by// ldata.//static void to_dump_line(WyStr& output, off_t off_cnt, const WyCSeg& ldata){ WyRet r; size_t skip_bgn(off_cnt&0x0f); size_t line_bytes=DumpLineLength-skip_bgn; // bytes that will be actually // converted if(line_bytes>ldata.size()) { line_bytes=ldata.size(); } output.reset(); // write off_cnt as "xxxx:xxxx" into output // { const size_t OffsetDigits=sizeof(off_t)<<1; output=Wy::wrd(off_cnt-skip_bgn,Radix); if(output.size()<OffsetDigits) { if((r=output.insert(0,OffsetDigits-output.size(),'0'))!=Ok) { WY_THROW(r); } if((r=output.insert(OffsetDigits>>1,1,':'))!=Ok) { WY_THROW(r); } } } if(line_bytes==0) { return; } // convert ldata[i] and append to output as // " hh hh hh hh hh hh hh hh-hh hh hh hh hh hh hh hh" // for(size_t i=0; i<skip_bgn; ++i) { output+=" . "; } for(size_t i=0; i<line_bytes; ++i) { WyStr tmp( Wy::wrd((unsigned char)ldata[i],Radix) ); output+=' '; if(tmp.size()<2) { // radix has to be 16 output+='0'; } output+=tmp; } size_t skip_end=DumpLineLength-skip_bgn-line_bytes; for(size_t i=0; i<skip_end; ++i) { output+=" . "; } output+=" "; output[33]='-'; // mark the separator // append ldata[i] to output. // If ldata[i] is not printable, '.' is used instead. // if((r=output.append(skip_bgn,' '))!=Ok) { WY_THROW(r); } for(size_t i=0; i<line_bytes; ++i) { output+= std::isprint(ldata[i])? ldata[i] : '.'; }};// Dump file fpath constents in hexidecimal value, the contents is from offset// off_bgn, of length dump_len.//static void dumphex(const char* fpath, off_t off_bgn, size_t dump_len){ WyRet r; WyRegFile infile(fpath,O_RDONLY); WyStr rbuf; size_t rlen=DumpLineLength-(off_bgn&0xf); infile.set_pos(off_bgn); // loop for each read for(size_t off_cnt=off_bgn; dump_len ; off_cnt+=rbuf.size()) { if(rlen>dump_len) { rlen=dump_len; } // read data from infile // if((r=Wy::full_read(infile,rbuf,rlen))!=Ok) { WY_THROW(r); } if(rbuf.size()==0) { break; } // rlen initialize to DumpLineLength for the reset loop rlen=DumpLineLength; dump_len-=rbuf.size(); // write the converted data to standard output WyStr obuf; to_dump_line(obuf,off_cnt,rbuf.cseg()); Wy::cout << obuf << '\n'; }};int main(int argc, char* argv[])try { static const char *cmd_syntax= "\n" " Dump file in hexidecimal value from offset, of length to standard output\n" " [Usage]$a_dumphex file offset length\n" ; if(argc!=4) { Wy::cerr << cmd_syntax; return(-1); } WyRet r; size_t foff_beg; size_t flen; // read argv[2] and convert to foff_beg size_t nidx=0; if((r=Wy::_scanum(foff_beg,nidx,WyCSeg(argv[2])))!=Ok) { Wy::cerr << "Error in 2nd argument, " << Wy::wrd(r) << '\n'; return(-1); } // read argv[3] and convert to flen nidx=0; if((r=Wy::_scanum(flen,nidx,WyCSeg(argv[3])))!=Ok) { Wy::cerr << "Error in 3rd argument, " << Wy::wrd(r) << '\n'; return(-1); } // dump file contents from offset/length of file argv[1] dumphex(argv[1], foff_beg, flen); return(0);}catch(const WyRet& e) { Wy::cerr << Wy::wrd(e) << '\n'; return(e->c_repcode());}catch(...) { Wy::cerr << "main caught(...)\n"; return(-1);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -