summaryrefslogtreecommitdiffstats
path: root/BeOS/ar-1.1/docs/dumpo.py
blob: 91bd8db699fb4e5be2b2907d3146b7bc7fc3267d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#! /bin/env python
""" Dump data about a Metrowerks object file.

Based on reverse-engineering the library file format, since the docs are
wrong.

Copyright (C) 1997 Chris Herborth (chrish@qnx.com)
"""

# ----------------------------------------------------------------------
# Standard modules
import sys, getopt, string, time

# ----------------------------------------------------------------------
# Extra goodies
from dumpar import mk_long, str2hex, print_offset, get_string

# ----------------------------------------------------------------------
def mk_short( str ):
	""" convert a 2-byte string into a number

	Assumes big-endian!
	"""
	if len( str ) < 2:
		raise ValueError, "str must be 2 bytes long"

	num = ord( str[1] )
	num = num + ord( str[0] ) * 0x100

	return num

# ----------------------------------------------------------------------
def usage():
	""" Display a usage message and exit.
	"""
	print "dumpo [-v] object1 [object2 ... objectn]"
	print
	print "Attempt to display some useful information about the contents"
	print "of the given Metrowerks object file(s)."
	print
	print "-v        Be verbose (displays offsets along with the data)"
	raise SystemExit

# ----------------------------------------------------------------------
def dump_o( file, verbose ):
	""" dump information about a Metrowerks object file

	Note that there is more info there, 6 more quads before the file name.
	"""
	offset = 0

	print "Dumping object:", file

	# Attempt to read the data.
	try:
		data = open( file ).read()
	except IOError, retval:
		print "*** Unable to open file %s: %s" % ( file, retval[1] )
		return

	# Check the magic number.
	if verbose:
		print_offset( offset )
	print "Magic:",
	magic = data[offset:offset + 8]
	print "'%s'" % ( magic )
	if magic != "MWOBPPC ":
		print "*** Invalid magic  number!"
		return

	offset = offset + 8

	# version
	if verbose:
		print_offset( offset )
	print "version:", mk_long( data[offset:offset + 4] )
	offset = offset + 4

	# flags
	if verbose:
		print_offset( offset )
	print "flags:", str2hex( data[offset:offset + 4] )
	offset = offset + 4

	# code size
	if verbose:
		print_offset( offset )
	print "code size:", mk_long( data[offset:offset + 4] )
	offset = offset + 4

	# data size
	if verbose:
		print_offset( offset )
	print "data size:", mk_long( data[offset:offset + 4] )
	offset = offset + 4

# ----------------------------------------------------------------------
def main():
	""" mainline
	"""

	# Set up some defaults
	be_verbose = 0

	# First, check the command-line arguments
	try:
		opt, args = getopt.getopt( sys.argv[1:], "vh?" )
	except getopt.error:
		print "*** Error parsing command-line options!"
		usage()

	for o in opt:
		if o[0] == "-h" or o[0] == "-?":
			usage()
		elif o[0] == "-v":
			be_verbose = 1
		else:
			print "*** Unknown command-line option!"
			usage()

	# Now we can attempt to dump info about the arguments.
	for obj in args:
		dump_o( obj, be_verbose )

if __name__ == "__main__":
	main()