Sha256: 8100b12fac1e9bb41df8f3a9eefb1a636284be7680c08fe5ac5d36c59018183d

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

=begin

= File
	hexdump.rb

= Info
	This file is part of Origami, PDF manipulation framework for Ruby
	Copyright (C) 2010	Guillaume Delugré <guillaume@security-labs.org>
	All right reserved.
	
  Origami is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Origami is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with Origami.  If not, see <http://www.gnu.org/licenses/>.

=end

class String #:nodoc:
  
  def hexdump(bytesperline = 16, upcase = true, offsets = true)
    
    dump = ""
    counter = 0
    
    while counter < length
      
      offset = sprintf("%010u", counter)
      
      linelen = (counter < length - bytesperline) ? bytesperline : (length - counter)
      bytes = ""
      linelen.times do |i|
        
        byte = self[counter + i].ord.to_s(16)
        if byte.size < 2 then byte.insert(0, "0") end
        bytes << byte
        bytes << " " unless i == bytesperline - 1
        
      end

      ascii = self[counter, linelen].ascii_print
      
      if upcase
        offset.upcase!
        bytes.upcase!
      end
      
      dump << "#{offset if offsets}  #{bytes.to_s.ljust(bytesperline * 3 - 1)}  #{ascii}\n"
      
      counter += bytesperline
      
    end

    dump
  end
  
  def ascii_print

    printable = ""
    self.each_byte do |c|
      if c >= ' '[0].ord && c <= '~'[0].ord then printable << c else printable << '.' end
    end

    printable
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
origami-1.2.3 bin/gui/hexdump.rb
origami-1.2.2 bin/gui/hexdump.rb
origami-1.2.1 bin/gui/hexdump.rb
origami-1.2.0 bin/gui/hexdump.rb
origami-1.1.2 bin/gui/hexdump.rb
origami-1.1.1 bin/gui/hexdump.rb
origami-1.0.4 bin/gui/hexdump.rb
origami-1.0.3 bin/gui/hexdump.rb
origami-1.0.2 bin/gui/hexdump.rb