-
Notifications
You must be signed in to change notification settings - Fork 0
/
HEXDUMP.PAS
313 lines (306 loc) · 8.95 KB
/
HEXDUMP.PAS
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2022
@website(https://www.gladir.com/freebsd-0)
@abstract(Target: Turbo Pascal 7, Free Pascal)
}
Program HEXDUMP;
Var
OutputFormat:(_Text,_C,_CSV,_HTML,_Json,_Pascal,_SQL,_XML);
Option:(_None,_Offset);
I:Integer;
ByteReaded:Integer;
Err:Word;
StartPos,CurrPos:LongInt;
FileView:File;
CurrParam,FileName:String;
Buffer:Array[0..255]of Byte;
Function StrToUpper(S:String):String;
Var
I:Byte;
Begin
For I:=1 to Length(S)do Begin
If S[I] in['a'..'z']Then S[I]:=Chr(Ord(S[I])-32);
End;
StrToUpper:=S;
End;
Function ByteHex2Str(value:Byte):String;
Const
matrix:Array[0..15]of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
Begin
ByteHex2Str:=matrix[(value shr 4) and $0F]+matrix[value and $F];
End;
Function HexWord2Str(value:Word):String;Begin
HexWord2Str:=ByteHex2Str(Hi(value))+ByteHex2Str(Lo(value));
End;
Function LongHex2Str(value:LongInt):String;
Begin
LongHex2Str:=ByteHex2Str((value shr 24)and $FF)+
ByteHex2Str((value shr 16)and $FF)+
ByteHex2Str((value shr 8)and $FF)+
ByteHex2Str(value and $FF);
End;
BEGIN
OutputFormat:=_Text;
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/h')or(ParamStr(1)='/H')Then Begin
WriteLn('HEXDUMP : Cette commande permet d''afficher le contenu d''un fichier en format hexadecimal.');
WriteLn;
WriteLn('Syntaxe : HEXDUMP fichier [option]');
WriteLn;
WriteLn(' fichier Nom du fichier a afficher');
WriteLn(' -s deplacement Deplacement d''octets a partir du debut');
WriteLn(' /FORMAT:format Sortie en C, CSV, HTML, Json, PASCAL, SQL ou XML');
WriteLn;
End
Else
Begin
Option:=_None;
StartPos:=0;
FileName:='';
For I:=1 to ParamCount do Begin
CurrParam:=ParamStr(I);
If Option=_Offset Then Begin
Option:=_None;
Val(CurrParam,StartPos,Err);
End
Else
If CurrParam='-s'Then Option:=_Offset Else
If(StrToUpper(Copy(CurrParam,1,8))='/FORMAT:')Then Begin
If StrToUpper(Copy(CurrParam,9,255))='C'Then OutputFormat:=_C Else
If StrToUpper(Copy(CurrParam,9,255))='CSV'Then OutputFormat:=_CSV Else
If StrToUpper(Copy(CurrParam,9,255))='HTML'Then OutputFormat:=_HTML Else
If StrToUpper(Copy(CurrParam,9,255))='JSON'Then OutputFormat:=_Json Else
If StrToUpper(Copy(CurrParam,9,255))='PASCAL'Then OutputFormat:=_Pascal Else
If StrToUpper(Copy(CurrParam,9,255))='SQL'Then OutputFormat:=_SQL Else
If StrToUpper(Copy(CurrParam,9,255))='XML'Then OutputFormat:=_XML;
End
Else
FileName:=CurrParam;
End;
CurrPos:=0;
If FileName=''Then Begin
WriteLn('Fichier requis !');
End
Else
Begin
{$I-}Assign(FileView,FileName);
Reset(FileView,1);{$I+}
If IoResult<>0 Then Begin
WriteLn('Fichier introuvable !');
Halt;
End;
Seek(FileView,StartPos);
Case OutputFormat of
_C:Begin
WriteLn('const unsigned char samples[',FileSize(FileView)-1-StartPos,'] = {');
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
Write('/*',LongHex2Str(CurrPos),'*/');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ':3)
Else
Begin
Write('0x',ByteHex2Str(Buffer[I]));
If I+1<ByteReaded Then Write(',');
End;
If I=7Then Write(' ');
End;
Write(' /* ');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('*/');
WriteLn;
Inc(CurrPos,ByteReaded);
End;
WriteLn('};');
End;
_CSV:Begin
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
Write(LongHex2Str(CurrPos),',');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ':3)
Else
Begin
Write(ByteHex2Str(Buffer[I]));
If I+1<ByteReaded Then Write(',');
End;
End;
WriteLn;
Inc(CurrPos,ByteReaded);
End;
WriteLn('};');
End;
_HTML:Begin
WriteLn('<!DOCTYPE html>');
WriteLn('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">');
WriteLn('<body>');
WriteLn('<table>');
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
WriteLn('<tr>');
Write('<td>',LongHex2Str(CurrPos),'</td>');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write('<td>',' ':3,'</td>')
Else Write('<td>',ByteHex2Str(Buffer[I]),'</td>');
End;
Write('<td>');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('</td>');
WriteLn;
WriteLn('</tr>');
Inc(CurrPos,ByteReaded);
End;
WriteLn('</table>');
WriteLn('</body>');
WriteLn('</html>');
End;
_Json:Begin
WriteLn('{');
While Not EOF(FileView)do Begin
WriteLn('{');
BlockRead(FileView,Buffer,16,ByteReaded);
Write('"position":"',LongHex2Str(CurrPos),'",');
Write('"data":"');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ':3)
Else Write(ByteHex2Str(Buffer[I]),' ');
End;
Write('",');
Write('"text":"');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('"');
WriteLn;
Inc(CurrPos,ByteReaded);
WriteLn('},');
End;
WriteLn('}');
End;
_Pascal:Begin
WriteLn('Const samples:Array[0..',FileSize(FileView)-1-StartPos,'] of Byte=(');
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
Write('{',LongHex2Str(CurrPos),'}');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ':3)
Else
Begin
Write('$',ByteHex2Str(Buffer[I]));
If I+1<ByteReaded Then Write(',');
End;
If I=7Then Write(' ');
End;
Write(' { ');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('}');
WriteLn;
Inc(CurrPos,ByteReaded);
End;
WriteLn(');');
End;
_SQL:Begin
WriteLn('CREATE TABLE line (');
WriteLn(' ':8,'d1 TEXT,');
WriteLn(' ':8,'d2 TEXT,');
WriteLn(' ':8,'d3 TEXT,');
WriteLn(' ':8,'d4 TEXT,');
WriteLn(' ':8,'d5 TEXT,');
WriteLn(' ':8,'d6 TEXT,');
WriteLn(' ':8,'d7 TEXT,');
WriteLn(' ':8,'d8 TEXT,');
WriteLn(' ':8,'d9 TEXT,');
WriteLn(' ':8,'da TEXT,');
WriteLn(' ':8,'db TEXT,');
WriteLn(' ':8,'dc TEXT,');
WriteLn(' ':8,'dd TEXT,');
WriteLn(' ':8,'de TEXT,');
WriteLn(' ':8,'df TEXT');
WriteLn(');');
While Not EOF(FileView)do Begin
WriteLn('INSERT INTO line (d1,d2,d3,d4,d5,d6,d7,d8,d9,da,db,dc,dd,de,df) VALUES (');
BlockRead(FileView,Buffer,16,ByteReaded);
Write('/*',LongHex2Str(CurrPos),'*/');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write('''','''')
Else
Begin
Write('''',ByteHex2Str(Buffer[I]),'''');
End;
If I<15 Then Write(',');
End;
Write(' /*');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('*/');
WriteLn(');');
WriteLn;
Inc(CurrPos,ByteReaded);
End;
End;
_XML:Begin
WriteLn('<?xml version="1.0" encoding="UTF-8"?>');
WriteLn('<data>');
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
WriteLn('<line>');
Write('<position>',LongHex2Str(CurrPos),'</position>');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write('<byte>',' ':3,'</byte>')
Else Write('<byte>',ByteHex2Str(Buffer[I]),'</byte>');
End;
Write('<!-- ');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('-->');
WriteLn;
WriteLn('</line>');
Inc(CurrPos,ByteReaded);
End;
WriteLn('</data>');
End;
Else Begin
While Not EOF(FileView)do Begin
BlockRead(FileView,Buffer,16,ByteReaded);
Write(LongHex2Str(CurrPos),' ');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ':3)
Else Write(ByteHex2Str(Buffer[I]),' ');
If I=7Then Write(' ');
End;
Write(' |');
For I:=0 to 15 do Begin
If I>=ByteReaded Then Write(' ')Else
If Buffer[I]<32Then Write(' ')
Else Write(Char(Buffer[I]));
End;
Write('|');
WriteLn;
Inc(CurrPos,ByteReaded);
End;
End;
End;
Close(FileView);
End;
End;
END.