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
<?php
/*
Quick & dirty library to read/extract BlitzMax compatible data types from raw input data (files/UDP).
Globals:
- $d: holds the data to read (d for Data)
- $dOS: read offset in bytes (dOS for Data OffSet)
- the normal functions manipulate $d (which is slow), they do not use $dOS
- the fast functions use $dOS to keep track of the read offset, they do not manipulate $d (faster)
*/
function readLine(){
	global $d;
	$line='';
	do {
		$char=ord(substr($d,0,1));
		$line.=chr($char);
		$d=substr($d,1);
	} while ($char!=13);
	$d=substr($d,1);
	return substr($line,0,strlen($line)-1);
}
function readString(){
	global $d;
	$length=ord(substr($d,0,1));
	$string=substr($d,1,$length);
	$d=substr($d,$length+1);
	return $string;
}
function readByte(){
	global $d;
	$byte=ord(substr($d,0,1));
	$d=substr($d,1);
	return $byte;
}
function readShort(){
	global $d;
	$short=substr($d,0,2);
	$d=substr($d,2);
	$r=unpack('S',$short);
	return $r[1];
}
function readInt(){
	global $d;
	$int=substr($d,0,4);
	$d=substr($d,4);
	$r=unpack('l',$int);
	return $r[1];
}
function readFloat(){
	global $d;
	$float=ord(substr($d,0,4));
	$d=substr($d,4);
	$r=unpack('f',$float);
	return $r[1];
}
// Fast versions using an offset ($dOS) instead of manipulating $d
function readLineFast(){
	global $d;
	global $dOS;
	$line='';
	do {
		$char=ord(substr($d,$dOS,1));
		$line.=chr($char);
		$dOS++;
	} while ($char!=13);
	$dOS++;
	return substr($line,0,strlen($line)-1);
}
function readStringFast(){
	global $d;
	global $dOS;
	$length=ord(substr($d,$dOS,1));
	$string=substr($d,$dOS+1,$length);
	$dOS+=($length+1);
	return $string;
}
function readByteFast(){
	global $d;
	global $dOS;
	$byte=ord(substr($d,$dOS,1));
	$dOS++;
	return $byte;
}
function readShortFast(){
	global $d;
	global $dOS;
	$short=substr($d,$dOS,2);
	$dOS+=2;
	$r=unpack('S',$short);
	return $r[1];
}
function readIntFast(){
	global $d;
	global $dOS;
	$int=substr($d,$dOS,4);
	$dOS+=4;
	$r=unpack('l',$int);
	return $r[1];
}
function readFloatFast(){
	global $d;
	global $dOS;
	$float=ord(substr($d,$dOS,4));
	$dOS+=4;
	$r=unpack('f',$float);
	return $r[1];
}
?>
Don't use the non-fast functions. I was just too lazy to remove them and to rename the other ones.