#!/usr/bin/python

# Copyright (c) 2010-2022 Belledonne Communications SARL.
#
# This file is part of mediastreamer2 
# (see https://gitlab.linphone.org/BC/public/mediastreamer2).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import argparse
import string
import sys


def print_include_hexdump_line(buf):
    hexbuf = [('0x%02x' % ord(i)) for i in buf]
    finalcomma = ''
    if len(hexbuf) == 12:
        finalcomma = ','
    print("  {0}{1}".format(", ".join(hexbuf[i] for i in range(0, len(hexbuf))), finalcomma))

def include_hexdump(infilename):
    varname = infilename.replace('.', '_').replace('-', '_')
    infile = open(infilename, 'r')
    print("const char {0}[] = {{".format(varname))
    while True:
        buf = infile.read(12)
        if not buf:
            break
        print_include_hexdump_line(buf)
    print(",0x00};")

def print_hexdump_line(counter, buf):
    hexbuf = [('%02x' % ord(i)) for i in buf]
    print("{0}: {1:<39}  {2}".format(("%08x" % (counter * 16)),
        ' '.join([''.join(hexbuf[i : i + 2]) for i in range(0, len(hexbuf), 2)]),
        ''.join([c if c in string.printable[:-5] else '.' for c in buf])))

def hexdump(infilename):
    infile = open(infilename, 'r')
    counter = 0
    while True:
        buf = infile.read(16)
        if not buf:
            break
        print_hexdump_line(counter, buf)
        counter += 1


def main(argv = None):
    if argv is None:
        argv = sys.argv
    argparser = argparse.ArgumentParser(description="Make a hexdump.")
    argparser.add_argument('-i', '--include', help="Output in C include file style. A complete static array definition is written (named after the input file).", action='store_true')
    argparser.add_argument('infile', help="File to dump.")
    args = argparser.parse_args()
    if args.include:
        include_hexdump(args.infile)
    else:
        hexdump(args.infile)



if __name__ == "__main__":
    sys.exit(main())
