This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
IFPSGlobals.php
157 lines (149 loc) · 4.56 KB
/
IFPSGlobals.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
<?php
// Global functions, used by multiple classes.
final class IFPSGlobals {
public static $isVoid = false;
public static function ReadUInt32($data,$length,&$offset) {
if ($length < $offset + 4) throw new Exception("Reached end of file");
$offset += 4;
return current(unpack("V",substr($data,$offset - 4,4)));
}
public static function MakeHash($s) {
$ret = 0;
for ($i = 0; $i < strlen($s); $i++) {
$ret = (($ret << 7) | ($ret << 25)) + ord($s[$i]);
}
return $ret;
}
public static function hex2float($number) {
$binfinal = sprintf("%032b",hexdec($number));
$sign = substr($binfinal, 0, 1);
$exp = substr($binfinal, 1, 8);
$mantissa = "1".substr($binfinal, 9);
$mantissa = str_split($mantissa);
$exp = bindec($exp)-127;
$significand=0;
for ($i = 0; $i < 24; $i++) {
$significand += (1 / pow(2,$i))*$mantissa[$i];
}
return $significand * pow(2,$exp) * ($sign*-2+1);
}
// BUGBUG: This doesn't work, and there will probably be no way to ever get such a function to work.
public static function hex_extended2float($number) {
$binfinal = sprintf("%080b",hexdec($number));
$sign = substr($binfinal, 0, 1);
$exp = substr($binfinal, 1, 15);
$mantissa = "1".substr($binfinal, 16);
$mantissa = str_split($mantissa);
$exp = bindec($exp)-127;
$significand=0;
for ($i = 0; $i < 65; $i++) {
$significand += (1 / pow(2,$i))*$mantissa[$i];
}
return $significand * pow(2,$exp) * ($sign*-2+1);
}
public static function isLittleEndian() {
return current(unpack('S',"\x01\x00")) === 1;
}
public static function ParseVar($a) {
if ($a < 0x40000000) return (object)array("type"=>"Global","index"=>$a);
$a -= 0x60000000;
if (($a == -1) && (!self::$isVoid)) return (object)array("type"=>"RetVal","index"=>$a);
if ($a >= 0) return (object)array("type"=>"Var","index"=>$a);
return (object)array("type"=>"Arg","index"=>((-$a) - (!self::$isVoid)));
}
public static function ParseFDecl($f) {
$ret = (object) array();
$offset = 0;
if (substr($f,0,4) == "dll:") {
$f = explode("\x00",$f,3);
$ret->type = 0;
$ret->dll = substr($f[0],4);
$ret->func = $f[1];
$restlen = strlen($f[2]);
$ret->CallingConv = current(unpack('C',substr($f[2],$offset++,1)));
switch ($ret->CallingConv) {
case 0:
$ret->CallingConv = "register";
break;
case 1:
$ret->CallingConv = "pascal";
break;
case 2:
$ret->CallingConv = "cdecl";
break;
case 3:
$ret->CallingConv = "stdcall";
break;
default:
throw new Exception("Unhandled calling convention ".$ret->CallingConv);
}
$ret->DelayLoad = (bool) current(unpack('C',substr($f[2],$offset++,1)));
$ret->LoadWithAlteredSearchPath = (bool) current(unpack('C',substr($f[2],$offset++,1)));
$ret->isVoid = !( (bool) ( current(unpack('C',substr($f[2],$offset++,1))) ) );
$f = $f[2];
} elseif (substr($f,0,6) == "class:") {
$f = substr($f,6);
if ($f == "+") {
$ret->type = 1;
$ret->classname = "Class";
$ret->func = "CastToType";
$ret->isVoid = 0;
$ret->CallingConv = "pascal";
$ret->params = array((object) array("modeIn"=> false),(object) array("modeIn"=> false));
return $ret;
} elseif ($f == "-") {
$ret->type = 1;
$ret->classname = "Class";
$ret->func = "SetNil";
$ret->isVoid = 0;
$ret->CallingConv = "pascal";
$ret->params = array((object) array("modeIn"=> false));
return $ret;
}
$f = explode("|",$f);
$ret->type = 1;
$ret->classname = $f[0];
$ret->func = $f[1];
if (substr($ret->func,-1) == "@") {
$ret->isProperty = true;
$ret->func = substr($ret->func,0,-1);
} else $ret->isProperty = false;
try {
$restlen = strlen($f[2]);
} catch (Exception $e) {
var_dump($f);
throw $e;
}
$ret->CallingConv = current(unpack('C',substr($f[2],$offset++,1)));
switch ($ret->CallingConv) {
case 0:
$ret->CallingConv = "register";
break;
case 1:
$ret->CallingConv = "pascal";
break;
case 2:
$ret->CallingConv = "cdecl";
break;
case 3:
$ret->CallingConv = "stdcall";
break;
default:
throw new Exception("Unhandled calling convention ".$ret->CallingConv);
}
$ret->isVoid = !( (bool) ( current(unpack('C',substr($f[2],$offset++,1))) ) );
$f = $f[2];
} else {
$restlen = strlen($f);
$ret->type = 2;
$ret->isVoid = !( (bool) ( current(unpack('C',substr($f,$offset++,1))) ) );
}
$ret->params = array();
while ($offset < $restlen) {
$p = current(unpack('C',substr($f,$offset++,1)));
$ret->params[] = (object) array("modeIn"=> (bool)$p);
$offset++;
}
return $ret;
}
}