@@ -83,6 +83,19 @@ public static function flipLongEndianness(int $value) : int{
8383 return self ::readLLong (self ::writeLong ($ value ));
8484 }
8585
86+ /**
87+ * @return mixed[]
88+ */
89+ private static function safeUnpack (string $ formatCode , string $ bytes ) : array {
90+ //unpack SUCKS SO BADLY. We really need an extension to replace this garbage :(
91+ $ result = unpack ($ formatCode , $ bytes );
92+ if ($ result === false ){
93+ //assume the formatting code is valid, since we provided it
94+ throw new BinaryDataException ("Invalid input data (not enough?) " );
95+ }
96+ return $ result ;
97+ }
98+
8699 /**
87100 * Reads a byte boolean
88101 */
@@ -122,14 +135,14 @@ public static function writeByte(int $c) : string{
122135 * Reads a 16-bit unsigned big-endian number
123136 */
124137 public static function readShort (string $ str ) : int {
125- return unpack ("n " , $ str )[1 ];
138+ return self :: safeUnpack ("n " , $ str )[1 ];
126139 }
127140
128141 /**
129142 * Reads a 16-bit signed big-endian number
130143 */
131144 public static function readSignedShort (string $ str ) : int {
132- return self ::signShort (unpack ("n " , $ str )[1 ]);
145+ return self ::signShort (self :: safeUnpack ("n " , $ str )[1 ]);
133146 }
134147
135148 /**
@@ -143,14 +156,14 @@ public static function writeShort(int $value) : string{
143156 * Reads a 16-bit unsigned little-endian number
144157 */
145158 public static function readLShort (string $ str ) : int {
146- return unpack ("v " , $ str )[1 ];
159+ return self :: safeUnpack ("v " , $ str )[1 ];
147160 }
148161
149162 /**
150163 * Reads a 16-bit signed little-endian number
151164 */
152165 public static function readSignedLShort (string $ str ) : int {
153- return self ::signShort (unpack ("v " , $ str )[1 ]);
166+ return self ::signShort (self :: safeUnpack ("v " , $ str )[1 ]);
154167 }
155168
156169 /**
@@ -164,7 +177,7 @@ public static function writeLShort(int $value) : string{
164177 * Reads a 3-byte big-endian number
165178 */
166179 public static function readTriad (string $ str ) : int {
167- return unpack ("N " , "\x00" . $ str )[1 ];
180+ return self :: safeUnpack ("N " , "\x00" . $ str )[1 ];
168181 }
169182
170183 /**
@@ -178,7 +191,7 @@ public static function writeTriad(int $value) : string{
178191 * Reads a 3-byte little-endian number
179192 */
180193 public static function readLTriad (string $ str ) : int {
181- return unpack ("V " , $ str . "\x00" )[1 ];
194+ return self :: safeUnpack ("V " , $ str . "\x00" )[1 ];
182195 }
183196
184197 /**
@@ -192,7 +205,7 @@ public static function writeLTriad(int $value) : string{
192205 * Reads a 4-byte signed integer
193206 */
194207 public static function readInt (string $ str ) : int {
195- return self ::signInt (unpack ("N " , $ str )[1 ]);
208+ return self ::signInt (self :: safeUnpack ("N " , $ str )[1 ]);
196209 }
197210
198211 /**
@@ -206,7 +219,7 @@ public static function writeInt(int $value) : string{
206219 * Reads a 4-byte signed little-endian integer
207220 */
208221 public static function readLInt (string $ str ) : int {
209- return self ::signInt (unpack ("V " , $ str )[1 ]);
222+ return self ::signInt (self :: safeUnpack ("V " , $ str )[1 ]);
210223 }
211224
212225 /**
@@ -220,7 +233,7 @@ public static function writeLInt(int $value) : string{
220233 * Reads a 4-byte floating-point number
221234 */
222235 public static function readFloat (string $ str ) : float {
223- return unpack ("G " , $ str )[1 ];
236+ return self :: safeUnpack ("G " , $ str )[1 ];
224237 }
225238
226239 /**
@@ -241,7 +254,7 @@ public static function writeFloat(float $value) : string{
241254 * Reads a 4-byte little-endian floating-point number.
242255 */
243256 public static function readLFloat (string $ str ) : float {
244- return unpack ("g " , $ str )[1 ];
257+ return self :: safeUnpack ("g " , $ str )[1 ];
245258 }
246259
247260 /**
@@ -269,7 +282,7 @@ public static function printFloat(float $value) : string{
269282 * Reads an 8-byte floating-point number.
270283 */
271284 public static function readDouble (string $ str ) : float {
272- return unpack ("E " , $ str )[1 ];
285+ return self :: safeUnpack ("E " , $ str )[1 ];
273286 }
274287
275288 /**
@@ -283,7 +296,7 @@ public static function writeDouble(float $value) : string{
283296 * Reads an 8-byte little-endian floating-point number.
284297 */
285298 public static function readLDouble (string $ str ) : float {
286- return unpack ("e " , $ str )[1 ];
299+ return self :: safeUnpack ("e " , $ str )[1 ];
287300 }
288301
289302 /**
@@ -297,7 +310,7 @@ public static function writeLDouble(float $value) : string{
297310 * Reads an 8-byte integer.
298311 */
299312 public static function readLong (string $ str ) : int {
300- return unpack ("J " , $ str )[1 ];
313+ return self :: safeUnpack ("J " , $ str )[1 ];
301314 }
302315
303316 /**
@@ -311,7 +324,7 @@ public static function writeLong(int $value) : string{
311324 * Reads an 8-byte little-endian integer.
312325 */
313326 public static function readLLong (string $ str ) : int {
314- return unpack ("P " , $ str )[1 ];
327+ return self :: safeUnpack ("P " , $ str )[1 ];
315328 }
316329
317330 /**
0 commit comments