-
Notifications
You must be signed in to change notification settings - Fork 3
/
hexdump.cmd
315 lines (305 loc) · 12.1 KB
/
hexdump.cmd
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION&::(Don't pollute the global environment with the following)
::**********************************************************************
SET $NAME=%~n0
SET $DESCRIPTION=Dump a file in hex format
SET $AUTHOR=Dave Benham [dbenham]
SET $SOURCE=%~f0
::@(#)NAME
::@(-) The name of the command or function, followed by a one-line description of what it does.
::@(#) %$NAME% -- %$DESCRIPTION%
::@(#)
::@(#)SYNOPSIS
::@(-) In the case of a command, a formal description of how to run it and what command line options it takes.
::@(-) For program functions, a list of the parameters the function takes and which header file contains its definition.
::@(-)
::@(#) %$NAME% [/option]... file
::@(#)
::@ (#)OPTIONS
::@(-) Flags, parameters, arguments (NOT the Monty Python way)
::@(#) -h Help page
::@(#) The format of the dump can be modified by the following case insensitive
::@(#) options:
::@(#)
::@(#) /BblockSize The blockSize after the /B specifies the number of bytes
::@(#) to print in each block. If the blockSize is {LT}= 0 then /C,
::@(#) /D, /E, /A and /O options are ignored and the bytes are
::@(#) output in a continuous stream without any delimiters or
::@(#) linebreaks.
::@(#) The default blockSize is 1.
::@(#)
::@(#) /CblockCount The blockCount after the /C specifies the number of blocks
::@(#) to include on each line of output.
::@(#) The default blockCount is 16.
::@(#)
::@(#) /DbyteDelim The byteDelim after the /D specifies the delimiter string
::@(#) to use between bytes within a block.
::@(#) The default byteDelim is undefined (no delimiter)
::@(#)
::@(#) /EblockDelim The blockDelim after the /E specifies the delimiter string
::@(#) to use between blocks within a line.
::@(#) The default blockDelim is '/E "' ^(a single space^)
::@(#)
::@(#) /SstartOffset The startOffset after the /S specifies the number of bytes
::@(#) to skip before displaying bytes.
::@(#) The default startOffset is 0.
::@(#)
::@(#) /Nlength The length after the /N specifies the total number of
::@(#) bytes to display after the startOffset. The default is to
::@(#) display up until the end of the file.
::@(#)
::@(#) /A Append the ASCII representation of the bytes to the end
::@(#) of each line. Non-printable and extended ASCII characters
::@(#) are displayed as periods.
::@(#)
::@(#) /O Prefix each line with the starting offset of the line in
::@(#) hexadecimal notation.
::@(#)
::@(#) /H Display hexDump help
::@(#)
::@(#) Each option must be entered as a separate argument. Numeric components to
::@(#) options may be specified using any numeric expression supported by SET /A.
::@(#) The option defaults may be modified by presetting a hexDumpDefaults
::@(#) variable.
::@(#)
::@(#)DESCRIPTION
::@(-) A textual description of the functioning of the command or function.
::@(#) Displays the content of a binary file using a pair of hexadecimal digits
::@(#) for each byte. By default the ouput displays 16 bytes per line, with the
::@(#) bytes (hexadecimal pairs) delimited by a space.
::@(#)
::@(#)
::@(#)EXAMPLES
::@(-) Some examples of common usage.
::@(#)
::@(#) echo Hello>out.txt
::@(#) echo world>>out.txt
::@(#) hexdump out.txt
::@(#) 00000000: 48 65 6C 6C 6F 0D 0A 77 6F 72 6C 64 0D 0A Hello..world..
::@(#)
::@ (#)EXIT STATUS
::@(-) Exit status / errorlevel is 0 if OK, otherwise 1+.
::@ (#)
::@ (#)ENVIRONMENT
::@(-) Variables affected
::@ (#)
::@ (#)
::@ (#)FILES,
::@(-) Files used, required, affected
::@ (#)
::@ (#)
::@ (#)BUGS / KNOWN PROBLEMS
::@(-) If any known
::@ (#)
::@ (#)
::@(#)REQUIRES
::@(-) Dependecies
::@(#) _Debug.cmd Setting up debug environment for batch scripts
::@(#) _GetOpt.cmd Parse command line options and create environment vars
::@(#)
::@ (#)SEE ALSO
::@(-) A list of related commands or functions.
::@ (#)
::@ (#)
::@(#)REFERENCE
::@(-) References to inspiration, clips and other documentation
::@(#) Author: Dave Benham [dbenham]
::@(#) URL: http://www.dostips.com/forum/viewtopic.php?t=1786
::@(#)
::@(#)SOURCE
::@(-) Where to find this source
::@(#) %$Source%
::@(#)
::@ (#)AUTHOR
::@(-) Who did what
::@ (#) %$AUTHOR%
::*** HISTORY **********************************************************
::SET $VERSION=YYYY-MM-DD&SET $REVISION=hh:mm:ss&SET $COMMENT=Description/init
::SET $VERSION=2015-02-19&SET $REVISION=16:00:00&SET $COMMENT=Autoupdate / ErikBachmann
::SET $VERSION=2015-03-30&SET $REVISION=10:40:00&SET $COMMENT=Example / ErikBachmann
SET $VERSION=2015-10-08&SET $REVISION=11:20:00&SET $COMMENT=GetOpt: Calling usage on -h and exit on error / ErikBachmann
::**********************************************************************
::@(#)(c)%$Version:~0,4% %$Author%
::**********************************************************************
CALL "%~dp0\_DEBUG"
CALL "%~dp0\_Getopt" %*&IF ERRORLEVEL 1 EXIT /B 1
::ENDLOCAL
:hexDump [/option]... file -- dump a file in hex format
:: author: Dave Benham [dbenham]
:: URL: http://www.dostips.com/forum/viewtopic.php?t=1786
::
:: Displays the content of a binary file using a pair of hexadecimal digits
:: for each byte. By default the ouput displays 16 bytes per line, with the
:: bytes (hexadecimal pairs) delimited by a space.
::
:: The format of the dump can be modified by the following case insensitive
:: options:
::
:: /BblockSize The blockSize after the /B specifies the number of bytes
:: to print in each block. If the blockSize is <= 0 then /C,
:: /D, /E, /A and /O options are ignored and the bytes are
:: output in a continuous stream without any delimiters or
:: linebreaks.
:: The default blockSize is 1.
::
:: /CblockCount The blockCount after the /C specifies the number of blocks
:: to include on each line of output.
:: The default blockCount is 16.
::
:: /DbyteDelim The byteDelim after the /D specifies the delimiter string
:: to use between bytes within a block.
:: The default byteDelim is undefined (no delimiter)
::
:: /EblockDelim The blockDelim after the /E specifies the delimiter string
:: to use between blocks within a line.
:: The default blockDelim is "/E " (a single space)
::
:: /SstartOffset The startOffset after the /S specifies the number of bytes
:: to skip before displaying bytes.
:: The default startOffset is 0.
::
:: /Nlength The length after the /N specifies the total number of
:: bytes to display after the startOffset. The default is to
:: display up until the end of the file.
::
:: /A Append the ASCII representation of the bytes to the end
:: of each line. Non-printable and extended ASCII characters
:: are displayed as periods.
::
:: /O Prefix each line with the starting offset of the line in
:: hexadecimal notation.
::
:: /H Display hexDump help
::
:: Each option must be entered as a separate argument. Numeric components to
:: options may be specified using any numeric expression supported by SET /A.
:: The option defaults may be modified by presetting a hexDumpDefaults
:: variable.
::
setlocal enableDelayedExpansion
set /a blockSize=1, blockCount=16, startOffset=0
set ascii=
set offset=TRUE
set len=
set opts=
set byteDelim=
set "blockDelim= "
set endDefault=
for %%a in (!hexDumpDefaults! // %*) do (
if not defined opts (
set "arg=%%~a"
if "!arg:~0,1!"=="/" (
if defined endDefault shift /1
set "opt=!arg:~1,1!"
if /i "!opt!"=="B" set /a blockSize=!arg:~2!
if /i "!opt!"=="C" set /a blockCount=!arg:~2!
if /i "!opt!"=="D" set "byteDelim=!arg:~2!"
if /i "!opt!"=="E" set "blockDelim=!arg:~2!"
if /i "!opt!"=="S" set /a startOffset=!arg:~2!
if /i "!opt!"=="N" set /a len=!arg:~2!
if /i "!opt!"=="A" set "ascii= "
if /i "!opt!"=="O" set offset=TRUE
if /i "!opt!"=="H" set "opts=TRUE" & goto :hexDump.help
if /i "!opt!"=="/" set endDefault=true
) else set opts=TRUE
)
)
if "%~1"=="" goto :hexDump.help
if not exist %1 (
echo ERROR: File not found >&2
exit /b 1
)
set fileSize=%~z1
if defined len (
set /a "endOffset = startOffset + len"
if !endOffset! gtr %fileSize% set endOffset=%fileSize%
) else set endOffset=%fileSize%
if defined offset set offset=%startOffset%
if %blockSize% lss 1 (
set /a "blockSize=0, blockCount=2000"
set "ascii="
set "offset="
set "byteDelim="
rem set "blockDelim="
)
set dummy="!temp!\hexDumpDummy%random%.txt"
<nul >%dummy% set /p ".=A"
set dummySize=1
for /l %%n in (1,1,32) do (if !dummySize! lss %endOffset% set /a "dummySize*=2" & type !dummy! >>!dummy!)
set /a "pos=0, cnt=0, skipStart=startOffset+1, lnBytes=blockSize*blockCount"
set "off="
set "hex="
set "txt=%ascii%"
set map= ^^^!^"#$%%^&'^(^)*+,-./0123456789:;^<=^>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^^^^_`abcdefghijklmnopqrstuvwxyz{^|}~
set hexMap=0123456789ABCDEF
::DEFINE INTERNAL MACRO USED SOLELY BY THIS FUNCTION
setlocal disableDelayedExpansion
set LF=^
::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
set callMacro=for /f %%a in
set addChar= do (%\n%
set "byte=%%~a"%\n%
if "!byte!"=="2space" set "byte= "%\n%
if defined ascii if "!byte!" neq " " (%\n%
set /a "d=0x!byte!-32"%\n%
if !d! lss 0 set d=14%\n%
if !d! gtr 94 set d=14%\n%
for %%d in (!d!) do set txt=!txt!!map:~%%d,1!%\n%
)%\n%
if %blockSize% gtr 0 set /a pos+=1%\n%
if !pos!==%blockSize% set /a "pos=0, cnt+=1"%\n%
if not !cnt!==!blockCount! (%\n%
if !pos!==0 (set "hex=!hex!!byte!!blockDelim!") else set "hex=!hex!!byte!!byteDelim!"%\n%
) else (%\n%
set "hex=!hex!!byte!"%\n%
set cnt=0%\n%
if defined offset (%\n%
set off=%\n%
set dec=!offset!%\n%
for /l %%n in (1,1,8) do (%\n%
set /a "d=dec&15,dec>>=4"%\n%
for %%d in (!d!) do set "off=!hexMap:~%%d,1!!off!"%\n%
)%\n%
set "off=!off!: "%\n%
set /a offset+=lnBytes%\n%
)%\n%
set "ln=!off!!hex!!txt!"%\n%
if %blockSize%==0 (^<nul set /p ".=!ln!") else echo !ln!%\n%
set hex=%\n%
set "txt=%ascii%"%\n%
)%\n%
)
::END OF MACRO DEFS
setlocal enableDelayedExpansion
for /f "eol=F usebackq tokens=1,2 skip=1 delims=:[] " %%A in (`fc /b "%~dpf1" %dummy%`) do (
set /a skipEnd=0x%%A && (
if !skipEnd! geq %startOffset% if !skipStart! leq %endOffset% (
for /l %%n in (!skipStart!,1,!skipEnd!) do %callMacro% ("41") %addChar%
%callMacro% ("%%B") %addChar%
set /a skipStart=skipEnd+2
)
)
)
for /l %%n in (%skipStart%,1,%endOffset%) do %callMacro% ("41") %addChar%
if %blockSize%==0 if defined hex (<nul set /p ".=!hex!") & set hex=
for /l %%n in (1,1,%lnBytes%) do if defined hex %callMacro% ("2space") %addChar%
del %dummy%
exit /b
::-------------------------------------------------------
:hexDump.help
setlocal disableDelayedExpansion
echo:
set file="%~f0"
set beg=
for /f "tokens=1,* delims=:" %%a in ('findstr /n /r /i /c:"^:hexDump " %file%') do (
if not defined beg set beg=%%a
)
set end=
for /f "tokens=1 delims=:" %%a in ('findstr /n /r /c:"^[^:]" %file%') do (
if not defined end if %beg% LSS %%a set end=%%a
)
for /f "tokens=1,* delims=[]:" %%a in ('findstr /n /r /c:"^ *:[^:]" /c:"^::[^:]" /c:"^ *::$" %file%') do (
if %beg% LEQ %%a if %%a LEQ %end% echo: %%b
)
exit /b 0