-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomCode.php
104 lines (91 loc) · 1.91 KB
/
randomCode.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
<?php
class PNGFirst8Bytes
{
/*The first eight bytes of a PNG file always contain the following (decimal) values:
137 80 78 71 13 10 26 10*/
private $resource;
private $header = [];
public function __construct($resource)
{
$this->resource = $resource;
}
public function get()
{
if (empty( $this->header)) {
for($i =0; $i < 8; $i++) {
$c = fgetc($this->resource);
if ($c === FALSE) {
throw new \Exception("unable to read png header's first 8 bytes");
} else {
$this->header[] = ord($c);
}
}
}
return $this;
}
public function check()
{
$expected = [137 ,80 ,78 ,71 ,13 ,10 ,26 ,10];
$diff = array_diff_assoc($expected, $this->header);
if (is_array($diff) && empty($diff)){
return true;
}
return false;
}
public function getFirst8Bytes()
{
return $this->header;
}
}
class FileFormatSignatureChecker
{
/** @param resource $handler ***/
public static function png($handler)
{
$check = new PNGFirst8Bytes($handler);
return $check->get()->check();
}
}
function check($handler)
{
$length = 8;
$m = pngMagicNumber();
$n = pngFile($handler);
for($i = 0; $i < 8; $i++) {
if ($n->current() != $m->current() ){
return false;
}
$n->next();
$m->next();
}
return true;
}
function pngFile($handler)
{
for($i = 0; $i < 8; $i++) {
$c = fgetc($handler);
if ($c === FALSE) {
throw new \Exception("unable to read png header's first 8 bytes");
} else {
yield ord($c);
}
}
}
function pngMagicNumber()
{
$expected = [137 ,80 ,78 ,71 ,13 ,10 ,26 ,10];
foreach ($expected as $value) {
yield $value;
}
}
$h = fopen("C:\Users\Anru Chen\Pictures\org_partner.png", "r");
//$b = FileFormatSignatureChecker::png($h);
$b = check($h);
var_dump($b);
fclose($h);
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);
$len = strlen($binarydata);
for($i=0; $i<$len; $i++) {
printf("%d\n",bin2hex($binarydata[$i]));
//echo bin2hex($binarydata[$i])."\n";
}