@@ -7,69 +7,258 @@ export class BinaryReader {
77 this . offset = offset ;
88 }
99
10- readUInt64 ( ) {
10+ #checkBounds( bytes : number ) {
11+ if ( this . offset + bytes > this . buffer . length ) {
12+ throw new Error ( `BinaryReader: Attempted to read ${ bytes } bytes at offset ${ this . offset } , but only ${ this . buffer . length - this . offset } bytes remaining` ) ;
13+ }
14+ }
15+
16+ get remaining ( ) {
17+ return this . buffer . length - this . offset ;
18+ }
19+
20+ seek ( offset : number ) {
21+ if ( offset < 0 || offset > this . buffer . length ) {
22+ throw new Error ( `BinaryReader: Seek out of bounds (${ offset } )` ) ;
23+ }
24+ this . offset = offset ;
25+ }
26+
27+ skip ( bytes : number ) {
28+ this . #checkBounds( bytes ) ;
29+ this . offset += bytes ;
30+ }
31+
32+ readUInt8 ( ) {
33+ this . #checkBounds( 1 ) ;
34+ const value = this . buffer . readUInt8 ( this . offset ) ;
35+ this . offset += 1 ;
36+ return value ;
37+ }
38+
39+ readUInt16LE ( ) {
40+ this . #checkBounds( 2 ) ;
41+ const value = this . buffer . readUInt16LE ( this . offset ) ;
42+ this . offset += 2 ;
43+ return value ;
44+ }
45+
46+ readUInt16BE ( ) {
47+ this . #checkBounds( 2 ) ;
48+ const value = this . buffer . readUInt16BE ( this . offset ) ;
49+ this . offset += 2 ;
50+ return value ;
51+ }
52+
53+ readUInt32LE ( ) {
54+ this . #checkBounds( 4 ) ;
55+ const value = this . buffer . readUInt32LE ( this . offset ) ;
56+ this . offset += 4 ;
57+ return value ;
58+ }
59+
60+ readUInt32BE ( ) {
61+ this . #checkBounds( 4 ) ;
62+ const value = this . buffer . readUInt32BE ( this . offset ) ;
63+ this . offset += 4 ;
64+ return value ;
65+ }
66+
67+ readUInt64LE ( ) {
68+ this . #checkBounds( 8 ) ;
69+ const value = this . buffer . readBigUInt64LE ( this . offset ) ;
70+ this . offset += 8 ;
71+ return value ;
72+ }
73+
74+ readUInt64BE ( ) {
75+ this . #checkBounds( 8 ) ;
1176 const value = this . buffer . readBigUInt64BE ( this . offset ) ;
1277 this . offset += 8 ;
1378 return value ;
1479 }
1580
16- peekUInt64 ( ) {
17- return this . buffer . readBigUInt64BE ( this . offset ) ;
81+ readInt8 ( ) {
82+ this . #checkBounds( 1 ) ;
83+ const value = this . buffer . readInt8 ( this . offset ) ;
84+ this . offset += 1 ;
85+ return value ;
1886 }
1987
20- readUInt32 ( ) {
21- const value = this . buffer . readUInt32BE ( this . offset ) ;
88+ readInt16LE ( ) {
89+ this . #checkBounds( 2 ) ;
90+ const value = this . buffer . readInt16LE ( this . offset ) ;
91+ this . offset += 2 ;
92+ return value ;
93+ }
94+
95+ readInt16BE ( ) {
96+ this . #checkBounds( 2 ) ;
97+ const value = this . buffer . readInt16BE ( this . offset ) ;
98+ this . offset += 2 ;
99+ return value ;
100+ }
101+
102+ readInt32LE ( ) {
103+ this . #checkBounds( 4 ) ;
104+ const value = this . buffer . readInt32LE ( this . offset ) ;
22105 this . offset += 4 ;
23106 return value ;
24107 }
25108
26- peekUInt32 ( ) {
27- return this . buffer . readUInt32BE ( this . offset ) ;
109+ readInt32BE ( ) {
110+ this . #checkBounds( 4 ) ;
111+ const value = this . buffer . readInt32BE ( this . offset ) ;
112+ this . offset += 4 ;
113+ return value ;
114+ }
115+
116+ readInt64LE ( ) {
117+ this . #checkBounds( 8 ) ;
118+ const value = this . buffer . readBigInt64LE ( this . offset ) ;
119+ this . offset += 8 ;
120+ return value ;
121+ }
122+
123+ readInt64BE ( ) {
124+ this . #checkBounds( 8 ) ;
125+ const value = this . buffer . readBigInt64BE ( this . offset ) ;
126+ this . offset += 8 ;
127+ return value ;
128+ }
129+
130+ readUInt32 ( ) {
131+ return this . readUInt32BE ( ) ;
28132 }
29133
30134 readInt32 ( ) {
31- const value = this . buffer . readInt32BE ( this . offset ) ;
135+ return this . readInt32BE ( ) ;
136+ }
137+
138+ readUInt64 ( ) {
139+ return this . readUInt64BE ( ) ;
140+ }
141+
142+ readUInt16 ( ) {
143+ return this . readUInt16BE ( ) ;
144+ }
145+
146+ readFloatLE ( ) {
147+ this . #checkBounds( 4 ) ;
148+ const value = this . buffer . readFloatLE ( this . offset ) ;
32149 this . offset += 4 ;
33150 return value ;
34151 }
35152
36- readUInt32LE ( ) {
37- const value = this . buffer . readUInt32LE ( this . offset ) ;
153+ readFloatBE ( ) {
154+ this . #checkBounds( 4 ) ;
155+ const value = this . buffer . readFloatBE ( this . offset ) ;
38156 this . offset += 4 ;
39157 return value ;
40158 }
41159
42- peekInt32 ( ) {
43- return this . buffer . readInt32BE ( this . offset ) ;
160+ readDoubleLE ( ) {
161+ this . #checkBounds( 8 ) ;
162+ const value = this . buffer . readDoubleLE ( this . offset ) ;
163+ this . offset += 8 ;
164+ return value ;
44165 }
45166
46- readUInt16 ( ) {
47- const value = this . buffer . readUInt16BE ( this . offset ) ;
48- this . offset += 2 ;
167+ readDoubleBE ( ) {
168+ this . #checkBounds( 8 ) ;
169+ const value = this . buffer . readDoubleBE ( this . offset ) ;
170+ this . offset += 8 ;
49171 return value ;
50172 }
51173
52- peekUInt16 ( ) {
53- return this . buffer . readUInt16BE ( this . offset ) ;
174+ readString ( length : number , encoding : BufferEncoding = 'utf8' ) {
175+ this . #checkBounds( length ) ;
176+ const value = this . buffer . toString ( encoding , this . offset , this . offset + length ) ;
177+ this . offset += length ;
178+ return value ;
179+ }
180+
181+ readCString ( encoding : BufferEncoding = 'utf8' ) {
182+ const start = this . offset ;
183+ let end = start ;
184+
185+ while ( end < this . buffer . length && this . buffer [ end ] !== 0 ) {
186+ end ++ ;
187+ }
188+
189+ const value = this . buffer . toString ( encoding , start , end ) ;
190+ this . offset = end + 1 ;
191+
192+ return value ;
193+ }
194+
195+ readLengthPrefixedString ( encoding : BufferEncoding = 'utf8' ) {
196+ const length = this . readUInt32LE ( ) ;
197+ this . readInt32 ( ) ;
198+ return this . readString ( length , encoding ) ;
54199 }
55200
56201 readBytes ( length : number ) {
202+ this . #checkBounds( length ) ;
57203 const value = this . buffer . subarray ( this . offset , this . offset + length ) ;
58204 this . offset += length ;
59205 return value ;
60206 }
61207
62208 peekBytes ( length : number ) {
209+ this . #checkBounds( length ) ;
63210 return this . buffer . subarray ( this . offset , this . offset + length ) ;
64211 }
65212
66- remaining ( ) {
67- return this . buffer . length - this . offset ;
213+ peekUInt32LE ( ) {
214+ this . #checkBounds( 4 ) ;
215+ return this . buffer . readUInt32LE ( this . offset ) ;
216+ }
217+
218+ peekUInt32BE ( ) {
219+ this . #checkBounds( 4 ) ;
220+ return this . buffer . readUInt32BE ( this . offset ) ;
221+ }
222+
223+ peekUInt32 ( ) {
224+ return this . peekUInt32BE ( ) ;
225+ }
226+
227+ peekInt32 ( ) {
228+ this . #checkBounds( 4 ) ;
229+ return this . buffer . readInt32BE ( this . offset ) ;
230+ }
231+
232+ peekUInt16 ( ) {
233+ this . #checkBounds( 2 ) ;
234+ return this . buffer . readUInt16BE ( this . offset ) ;
235+ }
236+
237+ peekUInt64 ( ) {
238+ this . #checkBounds( 8 ) ;
239+ return this . buffer . readBigUInt64BE ( this . offset ) ;
68240 }
69241
70242 readRemaining ( ) {
71243 const value = this . buffer . subarray ( this . offset ) ;
72244 this . offset = this . buffer . length ;
73245 return value ;
74246 }
247+
248+ slice ( length : number ) {
249+ this . #checkBounds( length ) ;
250+ return new BinaryReader ( this . buffer . subarray ( this . offset , this . offset + length ) ) ;
251+ }
252+
253+ tell ( ) {
254+ return this . offset ;
255+ }
256+
257+ eof ( ) {
258+ return this . offset >= this . buffer . length ;
259+ }
260+
261+ reset ( ) {
262+ this . offset = 0 ;
263+ }
75264}
0 commit comments