-
Notifications
You must be signed in to change notification settings - Fork 5
/
readbin.py
231 lines (227 loc) · 9.83 KB
/
readbin.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# PROGRAM-NAME: readbin.py BACKUP-REQUIRED: Yes
# AUTHOR: Tony Dycks REVISED BY: Tony Dycks
# DATE-WRITTEN: June 13, 2001 DATE-REVISED: June 20, 2001
# LANGUAGE: Active Python/Linux Python VERSION. NO.: 1.5.2 to 2.1
# PLATFORM: Win32/Linux (Text Console) VERSION. NO.: Any
#
# DESCRIPTION:
# Display Hex And Character Contents Of File Entered On The Command Line.
# Display 16 Characters Of Hex And Character Data Each Screen Line. Pause
# Display And Prompt For Continuation Every 20 Lines. A Keyboard Entry
# Of "X" <Enter> or "x" <Enter> Terminates The Program. Any Other Key
# Entry Continues The Display For Another 20 Lines.
#
# LIBRARY IMPORTS:
# sys Library Module For Screen I/O Functions And Exit To System
# os Library Module For Platform Clear Console Screen Function
#
# USAGE:
# PYTHON readbin.py </Filepath/Filename.Ext> <Enter> {Linux or Unix Implementations}
# PYTHON readbin.py <D:\Filepath\Filename.Ext> <Enter> {Windows/Ms-DOS Implementations}
#
# USAGE EXAMPLE:
# PYTHON readbin readbin.py <Enter>
# {Displays This Source Program In ASCII and Hex Dump Format}
#
# REFERENCES: Adaptation of REXX Command Line Utility Program For Reading Binary
# Files Using Python on a Win32 or Linux Terminal Console.
#
# TESTING SPECIFICS:
# Program Has Been Tested On The Following Platforms And Versions Of Python:
#
# ---------------------------- ----------------------------------------------
# Platform/Version Python Version
# ---------------------------- ----------------------------------------------
# Red Hat Linux Version 6.1 Stichting Mathmatisch Centrum Amsterdam V1.5.2
# Red Hat Linux Version 7.1 Stichting Mathmatisch Centrum Amsterdam V1.5.2
# SuSE Linux Version 7.0 Stichting Mathmatisch Centrum Amsterdam V1.5.2
# Microsoft Windows 98 SE Active State Active Python Version 2.1
# Microsoft Windows NT 4.0 Active State Active Python Version 2.1
# Microsoft MS-DOS Version 7.0 Rational Systems, Inc. Version 1.97
#
# LIMITATIONS AND ASSUMPTIONS:
# Execution From A Terminal Console OS Prompt Assumed. Execution from within the
# Python Interpreter Shell Requires Modification of argv Command Line
# String Edits. Program Has Been Tested On Terminal Console Windows For
# The Following Platforms: 1) Red Hat Linux(tm) 6.1 Using Python Version 1.5.2,
# 2) SuSE Linux(tm) 7.0 Using Python Version 1.5.2, 3) Microsoft Windows 98 SE(tm)
# Using Active State Active Python 2.1(tm).
#
# Modification to the 'sys.platform' testing logic may be required for
# implementations of Python, Windows, DOS or Linux that return a different
# string value for the 'sys.platform' string property.
#
# PROGRAM MODIFICATION AND PLATFORM USAGE NOTES:
# Use "import sys" and "print sys.platform" commands within Python Interpreter
# To Determine Your "sys.platform" value of Your Implementation Of Python If
# Your OS Platform Is Something Other Than Red Hat Linux V6.1, SusE Linux V7.0,
# or MS Windows 98 Second Edition under MS-DOS. Platforms Other Than Linux,
# Unix, Windows Or MS-DOS May Have A Value Other Than 'clear' or 'cls' For The
# System Clear Terminal Screen Command.
#
# PROGRAM USE AND LICENSING:
# Program is submitted under terms of the Python license and has been submitted
# for consideration for inclusion in the upcoming O'Reilly Publication,
# "Python Cookbook". Permission is granted for any editing or modification of
# the program for publication purposes.
#
# ==================================
# ==> Python Module Importations <==
# ==================================
import sys # For OS Platform Determination
import os # For Implementation of System Clear Terminal Screen Command,
# Command Line Argument Processing and Exit To OS Prompt Function
# ================================
# ==> Constant Initializations <==
# ================================
dashln = '-' * 78
hexfmtlen = 6
# ==========================
# ==> Main Program Logic <==
# ==========================
#
# >>> Platform Testing <<<
#
# >>> Test Whether Program Is Being Run Under A Flavor Of Linux or Unix? <<<
# >>> Known Linux Implementations Tested Were Red Hat V6.1 and SuSE V7.0 <<<
if sys.platform == 'linux-i386' or sys.platform == 'linux2':
SysCls = 'clear'
# >>> Test Whether Program Is Being Run Under Windows 32-bit Implementation <<<
elif sys.platform == 'win32' or sys.platform == 'dos' or sys.platform[0:5] == 'ms-dos':
SysCls = 'cls'
# >>> Otherwise the Clear Screen Func is 'unknown' to prevent erroneous command execution <<<
else:
SysCls = 'unknown'
def headings(argFile, argDashes, argSysCls): # Arguments are Filename, Line, Clr Scrn Fn Command
if argSysCls != 'unknown': # Is A Valid Clear Screen Command Known?
os.system(argSysCls) # Invoke System Clear Screen Display OS Command
print 'File: ', argFile
print argDashes
return 0
def GetDisplayChar(argCharVal, argInChar): # Arguments are ASCII Char Value & Character Read
if charval < 32 or (charval > 127 and charval < 161):
return '.' # Use '.' to denote Non-displayable character
else:
return argInChar
if SysCls != 'unknown': # Is A Valid Clear Screen Command Known?
os.system(SysCls) # Invoke System Clear Screen Display OS Command
# =================================
# ==> Print Program Info Banner <==
# =================================
print 'READBIN.PY -- Python Hex File Format Dump Command Line Utility For Binary Files'
print 'Version 1.0'
print 'By Tony Dycks'
print ' '
print 'Date Written: June 13, 2001'
print 'Date Last Revised: June 20, 2001'
print
print 'Submitted For Inclusion In The Python Cookbook'
print 'Open Source Contribution Under The GNU Public License'
print ' '
print ' '
print 'Running on System Platform:', sys.platform
print ' '
print 'Press <Enter> Key To Continue ... ',
response = sys.stdin.readline()
charln = ''
hexln = ''
offset = 0
offsetlen = 0
hexoffset = ''
charval = 0
charcnt = 0
# ################################################################################
# The following code will require mods if run from within the Python Interpreter
# ##############################################################################
# =========================================================================
# ==> No Command Line Argument Specified; Display Program Usage Message <==
# =========================================================================
if len(sys.argv) < 2:
print chr(7) # ==> Beep Once <==
print 'Incorrect Command Line Syntax ...'
print ' '
print 'Usage: Python readbin.py </Filepath/Filename.Ext> <Enter>'
print ' '
print sys.exit()
# =========================================================
# ==> Open File Specified On The Command Line For Input <==
# =========================================================
try:
infile = open(sys.argv[1], 'r')
lncnt = headings(sys.argv[1], dashln, SysCls) # Successful Return From Function Inits Line Counter
while 1: # ==> Read Until End Of File One Character At A Time <==
inchar = infile.read(1)
if not inchar:
break
charcnt = charcnt + 1
charval = ord(inchar)
hexval = hex(charval)
hexstrlen = len(hexval)
startpos = hexstrlen - 2
if hexval[startpos] == 'x':
startpos = startpos + 1
hexval = '0'+hexval[startpos]
else:
hexval = hexval[startpos:hexstrlen]
hexln = hexln+' '+hexval
charln = charln + GetDisplayChar(charval, inchar)
# ===================================================================
# ==> 16 Characters Appended To String; Time To Print A Dump Line <==
# ===================================================================
if charcnt == 16:
hexoffset = hex(offset)
offsetlen = len(hexoffset)
hexoffset = hexoffset[2:offsetlen]
while len(hexoffset) < hexfmtlen:
hexoffset = '0'+hexoffset
print hexoffset+': '+hexln, ' | ', charln
charln = ''
hexln = ''
charcnt = 0
offset = offset + 16
lncnt = lncnt + 1
# ========================================================================
# ==> A Screen-full of Info Displayed; Prompt for Continuation or Exit <==
# ========================================================================
if lncnt > 19:
print dashln
print 'Press "X" <Enter> To Exit; <Enter> To Continue ... ',
response = sys.stdin.readline()
# =============================================================
# ==> Take The First Character Slice of String, "response"; <==
# ==> Test for Uppercase or Lowercase E<x>it response <==
# ======================================================--=====
if response[0] == "X" or response[0] == 'x':
break # >>> Back to OS Console Prompt <<<
else:
# ====================================================
# ==> Display Headings; Reset Screen Lines Counter <==
# ====================================================
lncnt = headings(sys.argv[1], dashln, SysCls)
except:
print chr(7) # >>> Beep Once <<<
print 'Error Processing File:', sys.argv[1]
print 'Terminating Program -- readbin.py'
print ' '
sys.exit()
# ============================
# ==> End Of Program Logic <==
# ============================
if len(charln) > 0:
while len(charln) < 16:
hexln = hexln + ' '
charln = charln + ' '
hexoffset = hex(offset)
offsetlen = len(hexoffset)
hexoffset = hexoffset[2:offsetlen]
while len(hexoffset) < hexfmtlen:
hexoffset = '0'+hexoffset
print hexoffset+': '+hexln, ' | ', charln
# ===============================================
# ==> Successfully Processed The File Then <==
# ==> Show Message and Exit Back To OS Prompt <==
# ===============================================
print ' '
print '>>> End Of Program -- readbin.py <<<'
print ' '
sys.exit()