#! /usr/local/bin/ruby -Kn # hd.rb # /home/tetsu/src/ruby/toolbox/hd.rb # Created: February 22,1998 Sunday 18:39:06 # Author: tetsu(WATANABE Tetsuya) # Revised by: Hideo Konami, 2006/03/12 RCS_ID = %q$Id: hd.rb,v 2.3 2000/09/05 06:54:01 tetsu Exp $ # usage: hd.rb -sE -0100 /vmlinuz | less require 'nkf' def usage STDERR.print < 0 begin f.pos = offset rescue offset = 0 end end l_unpackformat = 'N*' unpackformat = 'N' l_packformat = 'N*' packformat = 'N' dumpformat = '%08x %08x %08x %08x %08x ' dumplastformat = '%8.8x' back = '' p_flag = true while $_ = f.read(16) break if $_.length != 16 if back == $_ print "*\n" if p_flag p_flag = false offset += 16 next elsif p_flag == false p_flag = true end back = $_ bin = $_.unpack(l_unpackformat) printf(dumpformat, offset, *bin) print $_.tr("\000-\037\177", '.').bin_to_str, "\n" offset += 16 end unless $_.nil? n = $_.length / 4 left_byte = $_.length % 4 bin = $_.unpack(unpackformat + n.to_s + 'a' + left_byte.to_s) n.times {|i| bin[i] = format(dumplastformat, bin[i])} if left_byte > 0 w = bin[-1].unpack('C*') foo = '' w.each_index {|i| foo.concat(format('%2.2x', w[i]))} bin[-1] = foo + ' ' * (4 - left_byte) end (16 / 4 - bin.size).times {bin.push(' ')} printf(dumpformat.gsub('x', 's'), format('%08x', offset), *bin) print $_.tr("\000-\037\177", '.').bin_to_str, "\n" end end $kanji = false $kanji_chip = '' $nkf_opt_in = '-E' $nkf_opt_out = '-e' offset = 0 while $_ = ARGV[0] and /^-/ =~ $_ # Changed by H.K. case ARGV.shift when /^-0([0-7]+)$/ offset = $1.oct when /^-0x([\da-fA-F]+)$/ offset = $1.hex when /^-(\d+)$/ offset = $1.to_i when /^-[se]+$/i $kanji = true $nkf_opt_in = '-' + $1 if /([SE])/ =~ $_ # Changed by H.K. $nkf_opt_out = '-' + $1 if /([se])/ =~ $_ # Changed by H.K. else usage end end if $nkf_opt_in == '-E' # - f4ff (less) $kanji_re = /((?:[\241-\364][\241-\376])+)/n $kana_re = /((?:\216[\241-\337])+)/n $kanji_chip_re = /^[\216\241-\376]$/n else # 0x81-0x9f 0xe0-0xfc/0x40-0x7e 0x80-0xfc # 0x40-0x7e 0x80-0xa0 0xe0-0xfc # - eafc (less) $kanji_re = /((?:[\201-\237\340-\352][\100-\176\200-\374])+)/n # kconv/nkf 0xa0-0xdf $kana_re = /([\241-\337]+)/n $kanji_chip_re = /^[\201-\237\340-\357]$/n end if ARGV.length == 0 hd(STDIN, offset) else while file = ARGV.shift f = File.open(file) hd(f, offset) f.close end end exit