/
Hexdump.php
465 lines (409 loc) · 14.6 KB
/
Hexdump.php
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/**
* This library contains a hexdump function.
*
* PHP Version 5.1.0
*
* @category Debug
* @package Hexdump
* @author Thorsten Heymann <info@metashock.net>
* @copyright 2011 - 2012 Thorsten Heymann
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @version 1.1.0
* @link http://www.metashock.de/pirum
* @since 0.1.0
*/
/**
* Prints a hexdump of data. You can configure the number of
* bytes per line, the linedelimiter and choose whether hexadecimal
* numbers should be uppercased or not.
*
* @example examples/demo.php
*
* @param string $data The data to be hexdumped
* @param integer $ncols The number of bytes per line
* @param string $format The output format. Either plain or html
* @param boolean $uppercase If true hexadecimal number will be uppercased
* @param string $output Where should the output go to?
* Either stdout, stderr or none. If its none then
* hexdump will just being returned
*
* @return string
*
* @throws InvalidArgumentException if one of the params data type mismatch
* @throws UnexpectedValueException if $ncolumns is lower than 1
*
* @version 1.1.0
* @since 0.1.0
*/
function hexdump (
$data,
$ncols = NULL,
$format = NULL,
$uppercase = NULL,
$output = NULL
) {
static $printer;
if(!$printer) {
$printer = new Hexdump();
}
return $printer->draw($data, $ncols, $format, $uppercase, $output);
}
/**
* Usually one will just use the hexdump() function as its less to type.
* So why an extra class? The sense of the Hexdump class is to act as a
* global configuration container for hexdump() thus to keep the syntax
* of the function itself short.
*
* Also it is resonsible for param validation
*
*
* @version 1.1.0
* @since 1.1.0
*/
class Hexdump
{
/**
* @var array $options
*/
protected static $options;
/**
* Gets or sets global options
*
* @version 1.1.0
* @since 1.1.0
*
* @param string $optname Name of the option of interest
* @param mixed $optval If omitted the method will return the current
* value. If set, the option will be set to it
*
* @return mixed
*
* @throws UnexpectedValueException if either the option name is unknown
* or the optval isn't valid for the option.
*/
public static function option($optname, $optval = NULL) {
if(!is_array(self::$options)) {
self::$options = array (
'ncols' => 16,
'output' => 'stdout',
'format' => 'plain',
'uppercase' => false
);
}
// Check that the option name is valid
if(!array_key_exists($optname, self::$options)) {
throw new UnexpectedValueException (
'Unknown option \'' . $optname . '\'. Expected was one of the '
. 'following: ncols|output|format|uppercase'
);
}
// If $optval is null are a getter and just return the values.
// Note that this will work only because none of the options currently
// allows NULL values. If this will be the case sometimes, the check
// will have to use func_get_args() instead.
if(is_null($optval)) {
return self::$options[$optname];
}
// The option is requested to set. We have to validate optval
// before its usage. static::validateOption() will throw an Exception
// if $optval isn't valid
static::validateOption($optname, $optval);
// finally set it
self::$options[$optname] = $optval;
}
/**
* Validates an option value
*
* @version 1.1.0
* @since 1.1.0
*
* @param string $optname
* @param mixed $value
*
* @return boolean
*
* @throws InvalidArgumentException if $optval isn't from the expected type
* @throws UnexpectedValueException if $optval is not in the value range
*/
protected static function validateOption($optname, $optval) {
switch ($optname) {
case 'ncols':
// check the type of $optval
if (!is_int($optval)) {
throw new InvalidArgumentException(
'The value for ncols expected to be an integer. '
. gettype($optval) . ' found.'
);
}
// check whether $ncolumns is greater than zero
if ($optval < 1 ) {
throw new UnexpectedValueException(
'The value for ncols expected to be greater than zero.'
. ' Got: \'' . strval($optval) . '\''
);
}
break;
case 'output':
// check the type of $optval
if (!is_string($optval)) {
throw new InvalidArgumentException(
'The value for output expected to be a string. '
. gettype($optval) . ' found.'
);
}
// check if the value is valid
if (!in_array($optval, array('stdout', 'stderr', 'none'))) {
throw new UnexpectedValueException(
'The value for output expected to be one of the '
. 'following: stdout|stderr|none. zero. '
. 'Got: \'' . $optval . '\''
);
}
break;
case 'format':
// check the type of $optval
if (!is_string($optval)) {
throw new InvalidArgumentException(
'The value for format expected to be a string. '
. gettype($optval) . ' found.'
);
}
// check if the value is valid
if (!in_array($optval, array('plain', 'html'))) {
throw new UnexpectedValueException(
'The value for format expected to be one of the '
. 'following: plain|html. '
. 'Got: \'' . $optval . '\''
);
}
break;
case 'uppercase':
// check the type of $optval
if (!is_bool($optval)) {
throw new InvalidArgumentException(
'The value for uppercase expected to be a boolean. '
. gettype($optval) . ' found.'
);
}
break;
default:
throw new UnexpectedValueException (
'Unknown option \'' . $optname . '\'. Expected was one of the '
. 'following: ncols|output|format|uppercase'
);
}
return true;
}
/**
* Wrapper for output operations. Separates beween stdout
* and stderr output and does HTML post processing.
*
* @since 1.1.0
* @version 1.1.0
*
* @param array|string $data
* @param string $format
* @param string $output
*
* @return void
*/
protected function write($data, $format, $output) {
if(is_array($data)) {
$string = implode('', $data);
} else {
$string = $data;
}
switch ($format) {
case 'plain' :
// nothing to do
break;
case 'html' :
$string = htmlspecialchars($string);
break;
default :
throw new UnexpectedValueException(
'The value for format expected to be one of the '
. 'following: plain|html. '
. 'Got: \'' . $optval . '\''
);
}
// printing although $output === 'none' may sound awesome
// but output buffering is used when output is none so we
// don't care at this point
if($output === 'stdout' || $output === 'none') {
echo $string;
} else if ($output === 'stderr') {
fwrite(STDERR, $string);
}
}
/**
* Renders a hexdump of data. You can configure the number of
* bytes per line, the linedelimiter and choose whether hexadecimal
* numbers should be uppercased or not.
*
* @example examples/demo.php
*
* @param string $data The data to be hexdumped
* @param integer $ncolumns The number of bytes per line
* @param string $linedelim The line delimiter
* @param boolean $uppercase If true hexadecimal number will be uppercased
*
* @return void
*
* @throws InvalidArgumentException if one of the params data type mismatch
* @throws UnexpectedValueException if $ncolumns is lower than 1
*
* @version 1.1.0
* @since 1.1.0
*/
public function draw (
$data,
$ncols = NULL,
$format = NULL,
$uppercase = NULL,
$output = NULL
) {
// check the type of $data
if (!is_string($data)) {
throw new InvalidArgumentException(
'$data expected to be string. ' . gettype($data) . ' found.'
);
}
// validate the remaining arguments
if(!is_null($ncols)) {
static::validateOption('ncols', $ncols);
} else {
$ncols = static::option('ncols');
}
if(!is_null($format)) {
static::validateOption('format', $format);
} else {
$format = static::option('format');
}
if(!is_null($output)) {
static::validateOption('output', $output);
} else {
$output = static::option('output');
}
if(!is_null($uppercase)) {
static::validateOption('uppercase', $uppercase);
} else {
$uppercase = self::option('uppercase');
}
if($output === 'stdout') {
// using output buffering to increase performance
ob_start(null, 4096);
} else {
ob_start();
}
// default line delim for plain output
$linedelim = PHP_EOL;
// when format is html we enclose the output in <pre> tags
if($format === 'html') {
$this->write('<pre>', 'plain', $output);
}
// will contain a binary string with all non printable bytes
static $from = '';
// will contain a string containing length of $from times '.'
static $to = '';
// the column of the current byte
$c = 1;
// total offset in $data
$offset = 0;
// total number of bytes to process
$len = strlen($data);
// just return $data on empty input
if ($len < 1) {
return $data;
}
// prepare a translation table to convert non printable bytes
// to a '.' char. the translation table will be created statically
// the first time hexdump is called. Using a translation table with
// php's strtr() function appeared slight faster then translating them
// in this script directly. @thanks to mindplay.dk for the strtr() idea
// @see http://stackoverflow.com/questions/1057572/\ ...
// ... how-can-i-get-a-hex-dump-of-a-string-in-php
if ($from === '') {
for ($i = 0; $i < 0x21; $i++) {
$from .= chr($i);
$to .= '.';
}
for ($i = 0x7E; $i <= 0xFF; $i++) {
$from .= chr($i);
$to .= '.';
}
}
// iterate through $data
$this->write('00000000: ', $format, $output);
for ($i = 0; $i < $len; $i++, $c++) {
// get byte at current position and convert it to a hex string
if ($uppercase) {
$this->write(strtoupper(bin2hex($data[$i])) . ' ', $format, $output);
} else {
$this->write(bin2hex($data[$i]) . ' ', $format, $output);
}
// after the number of bytes has been reached we print
// the ascii representation of the last line.
if ($c === $ncols) {
$this->write(array(
'|',
// non printable characters have to be converted to '.'
strtr(substr($data, $i - $ncols + 1, $ncols), $from, $to) ,
'|' , $linedelim
), $format, $output); // end of line
// next line
$c = 0;
// increment line offset and prepend it to the new line
// if the end of data isn't reached now
$offset += $ncols;
if ( $offset === $len) {
// when format is html we enclose the output in <pre> tags
if($format === 'html') {
$this->write('</pre>', 'plain', $output);
}
$hexdump = ob_get_contents();
if($output === 'stdout') {
ob_end_flush();
} else {
ob_end_clean();
}
return $hexdump;
}
if ($uppercase) {
$this->write(sprintf('%08X: ', $offset), $format, $output);
} else {
$this->write(sprintf('%08x: ', $offset), $format, $output);
}
}
}
// the last line needs special attention because it may not contain
// exactly $ncolumn bytes. the remaining gap between the last hex char
// and the ascii output has therefore to be filled with spaces
// get the number of remaining bytes
$remains = $ncols - ($i % $ncols);
if ( $remains !== $ncols) {
// display whitespaces for each remaining byte
$this->write(array(
str_repeat(' ', $remains)
// display the asciis for the last bytes
, '|'
, strtr(substr($data, $i - ($i % $ncols)), $from, $to)
, '|'
// and a final newline
, $linedelim
), $format, $output);
}
// when format is html we enclose the output in <pre> tags
if($format === 'html') {
$this->write('</pre>', 'plain', $output);
}
$hexdump = ob_get_contents();
if($output === 'stdout') {
ob_end_flush();
} else {
ob_end_clean();
}
return $hexdump;
}
}