44
55namespace PhpIso \Util ;
66
7+ use PhpIso \Exception ;
8+
79class Buffer
810{
911 /**
@@ -28,6 +30,9 @@ public static function getString(array &$buffer, int $length, int &$offset = 0,
2830 {
2931 $ string = '' ;
3032 for ($ i = $ offset ; $ i < $ offset + $ length ; $ i ++) {
33+ if (!isset ($ buffer [$ i ])) {
34+ throw new Exception ('Failed to read buffer entry ' . $ i );
35+ }
3136 $ string .= chr ($ buffer [$ i ]);
3237 }
3338
@@ -68,6 +73,9 @@ public static function getBytes(array &$buffer, int $length, int &$offset = 0):
6873 {
6974 $ datas = '' ;
7075 for ($ i = $ offset ; $ i < $ offset + $ length ; $ i ++) {
76+ if (!isset ($ buffer [$ i ])) {
77+ throw new Exception ('Failed to read buffer entry ' . $ i );
78+ }
7179 $ datas .= $ buffer [$ i ];
7280 }
7381
@@ -89,6 +97,12 @@ public static function readBBO(array &$buffer, int $length, int &$offset = 0): i
8997 $ len = $ length / 2 ;
9098
9199 for ($ i = 0 ; $ i < $ len ; $ i ++) {
100+ if (!isset ($ buffer [$ offset + ($ len - 1 - $ i )])) {
101+ throw new Exception ('Failed to read buffer entry ' . ($ offset + ($ len - 1 - $ i )));
102+ }
103+ if (!isset ($ buffer [$ offset + $ len + $ i ])) {
104+ throw new Exception ('Failed to read buffer entry ' . ($ offset + $ len + $ i ));
105+ }
92106 $ n1 += $ buffer [$ offset + ($ len - 1 - $ i )];
93107 $ n2 += $ buffer [$ offset + $ len + $ i ];
94108
@@ -115,6 +129,10 @@ public static function readLSB(array &$buffer, int $length, int &$offset = 0): i
115129 {
116130 $ lsb = 0 ;
117131 for ($ i = 0 ; $ i < $ length ; $ i ++) {
132+ if (!isset ($ buffer [$ offset + ($ length - 1 - $ i )])) {
133+ throw new Exception ('Failed to read buffer entry ' . ($ offset + ($ length - 1 - $ i )));
134+ }
135+
118136 $ lsb += $ buffer [$ offset + $ length - 1 - $ i ];
119137
120138 if ($ i + 1 < $ length ) {
@@ -135,6 +153,9 @@ public static function readMSB(array &$buffer, int $length, int &$offset = 0): i
135153 {
136154 $ msb = 0 ;
137155 for ($ i = 0 ; $ i < $ length ; $ i ++) {
156+ if (!isset ($ buffer [$ offset + $ i ])) {
157+ throw new Exception ('Failed to read buffer entry ' . ($ offset + $ i ));
158+ }
138159 $ msb += $ buffer [$ offset + $ i ];
139160
140161 if ($ i + 1 < $ length ) {
@@ -155,6 +176,14 @@ public static function readInt16(array &$buffer, int &$offset = 0): int
155176 {
156177 $ output = 0 ;
157178
179+ if (!isset ($ buffer [$ offset + 0 ])) {
180+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 0 ));
181+ }
182+
183+ if (!isset ($ buffer [$ offset + 1 ])) {
184+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 1 ));
185+ }
186+
158187 $ output += $ buffer [$ offset + 0 ] << 8 ;
159188 $ output += $ buffer [$ offset + 1 ];
160189
@@ -171,6 +200,22 @@ public static function readInt32(array &$buffer, int &$offset = 0): int
171200 {
172201 $ output = 0 ;
173202
203+ if (!isset ($ buffer [$ offset + 0 ])) {
204+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 0 ));
205+ }
206+
207+ if (!isset ($ buffer [$ offset + 1 ])) {
208+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 1 ));
209+ }
210+
211+ if (!isset ($ buffer [$ offset + 2 ])) {
212+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 2 ));
213+ }
214+
215+ if (!isset ($ buffer [$ offset + 3 ])) {
216+ throw new Exception ('Failed to read buffer entry ' . ($ offset + 3 ));
217+ }
218+
174219 $ output += $ buffer [$ offset + 0 ] << 24 ;
175220 $ output += $ buffer [$ offset + 1 ] << 16 ;
176221 $ output += $ buffer [$ offset + 2 ] << 8 ;
0 commit comments