Debian Codegraph @

src/engine/safeReader.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "safeReader.h"
21 #include "../ta-log.h"
22
23 //#define READ_DEBUG
24
25 bool SafeReader::seek(ssize_t where, int whence) {
26 switch (whence) {
27 case SEEK_SET:
28 if (where<0) return false;
29 if (where>(ssize_t)len) return false;
30 curSeek=where;
31 break;
32 case SEEK_CUR: {
33 ssize_t finalSeek=curSeek+where;
34 if (finalSeek<0) return false;
35 if (finalSeek>(ssize_t)len) return false;
36 curSeek=finalSeek;
37 break;
38 }
39 case SEEK_END: {
40 ssize_t finalSeek=len-where;
41 if (finalSeek<0) return false;
42 if (finalSeek>(ssize_t)len) return false;
43 curSeek=finalSeek;
44 break;
45 }
46 }
47 return true;
48 }
49
50 size_t SafeReader::tell() {
51 return curSeek;
52 }
53
54 size_t SafeReader::size() {
55 return len;
56 }
57
58 int SafeReader::read(void* where, size_t count) {
59 #ifdef READ_DEBUG
60 logD("SR: reading %d bytes at %x",count,curSeek);
61 #endif
62 if (count==0) return 0;
63 if (curSeek+count>len) throw EndOfFileException(this,len);
64 if (curSeek+count<curSeek) throw EndOfFileException(this,len);
65 memcpy(where,&buf[curSeek],count);
66 curSeek+=count;
67 return count;
68 }
69
70 signed char SafeReader::readC() {
71 #ifdef READ_DEBUG
72 logD("SR: reading char %x:",curSeek);
73 #endif
74 if (curSeek+1>len) throw EndOfFileException(this,len);
75 if (curSeek+1<curSeek) throw EndOfFileException(this,len);
76 #ifdef READ_DEBUG
77 logD("SR: %.2x",buf[curSeek]);
78 #endif
79 return (signed char)buf[curSeek++];
80 }
81
82 #ifdef TA_BIG_ENDIAN
83 short SafeReader::readS_BE() {
84 #ifdef READ_DEBUG
85 logD("SR: reading short %x:",curSeek);
86 #endif
87 if (curSeek+2>len) throw EndOfFileException(this,len);
88 if (curSeek+2<curSeek) throw EndOfFileException(this,len);
89 short ret;
90 memcpy(&ret,&buf[curSeek],2);
91 #ifdef READ_DEBUG
92 logD("SR: %.4x",ret);
93 #endif
94 curSeek+=2;
95 return ret;
96 }
97
98 short SafeReader::readS() {
99 if (curSeek+2>len) throw EndOfFileException(this,len);
100 if (curSeek+2<curSeek) throw EndOfFileException(this,len);
101 short ret;
102 memcpy(&ret,&buf[curSeek],2);
103 curSeek+=2;
104 return ((ret>>8)&0xff)|(ret<<8);
105 }
106
107 int SafeReader::readI_BE() {
108 #ifdef READ_DEBUG
109 logD("SR: reading int %x:",curSeek);
110 #endif
111 if (curSeek+4>len) throw EndOfFileException(this,len);
112 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
113 int ret;
114 memcpy(&ret,&buf[curSeek],4);
115 curSeek+=4;
116 #ifdef READ_DEBUG
117 logD("SR: %.8x",ret);
118 #endif
119 return ret;
120 }
121
122 int SafeReader::readI() {
123 if (curSeek+4>len) throw EndOfFileException(this,len);
124 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
125 unsigned int ret;
126 memcpy(&ret,&buf[curSeek],4);
127 curSeek+=4;
128 return (int)((ret>>24)|((ret&0xff0000)>>8)|((ret&0xff00)<<8)|((ret&0xff)<<24));
129 }
130
131 int64_t SafeReader::readL() {
132 if (curSeek+8>len) throw EndOfFileException(this,len);
133 if (curSeek+8<curSeek) throw EndOfFileException(this,len);
134 unsigned char ret[8];
135 memcpy(ret,&buf[curSeek],8);
136 curSeek+=8;
137 return (int64_t)(ret[0]|(ret[1]<<8)|(ret[2]<<16)|(ret[3]<<24)|((uint64_t)ret[4]<<32)|((uint64_t)ret[5]<<40)|((uint64_t)ret[6]<<48)|((uint64_t)ret[7]<<56));
138 }
139
140 float SafeReader::readF() {
141 if (curSeek+4>len) throw EndOfFileException(this,len);
142 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
143 unsigned int ret;
144 memcpy(&ret,&buf[curSeek],4);
145 curSeek+=4;
146 ret=((ret>>24)|((ret&0xff0000)>>8)|((ret&0xff00)<<8)|((ret&0xff)<<24));
147 float realRet;
148 memcpy(&realRet,&ret,4);
149 return realRet;
150 }
151
152 double SafeReader::readD() {
153 if (curSeek+8>len) throw EndOfFileException(this,len);
154 if (curSeek+8<curSeek) throw EndOfFileException(this,len);
155 unsigned char ret[8];
156 unsigned char retB[8];
157 memcpy(ret,&buf[curSeek],8);
158 curSeek+=8;
159 retB[0]=ret[7];
160 retB[1]=ret[6];
161 retB[2]=ret[5];
162 retB[3]=ret[4];
163 retB[4]=ret[3];
164 retB[5]=ret[2];
165 retB[6]=ret[1];
166 retB[7]=ret[0];
167 double realRet;
168 memcpy(&realRet,retB,8);
169 return realRet;
170 }
171 #else
172 short SafeReader::readS() {
173 #ifdef READ_DEBUG
174 logD("SR: reading short %x:",curSeek);
175 #endif
176 if (curSeek+2>len) throw EndOfFileException(this,len);
177 if (curSeek+2<curSeek) throw EndOfFileException(this,len);
178 short ret;
179 memcpy(&ret,&buf[curSeek],2);
180 #ifdef READ_DEBUG
181 logD("SR: %.4x",ret);
182 #endif
183 curSeek+=2;
184 return ret;
185 }
186
187 short SafeReader::readS_BE() {
188 if (curSeek+2>len) throw EndOfFileException(this,len);
189 if (curSeek+2<curSeek) throw EndOfFileException(this,len);
190 short ret;
191 memcpy(&ret,&buf[curSeek],2);
192 curSeek+=2;
193 return ((ret>>8)&0xff)|(ret<<8);
194 }
195
196 int SafeReader::readI() {
197 #ifdef READ_DEBUG
198 logD("SR: reading int %x:",curSeek);
199 #endif
200 if (curSeek+4>len) throw EndOfFileException(this,len);
201 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
202 int ret;
203 memcpy(&ret,&buf[curSeek],4);
204 curSeek+=4;
205 #ifdef READ_DEBUG
206 logD("SR: %.8x",ret);
207 #endif
208 return ret;
209 }
210
211 int SafeReader::readI_BE() {
212 if (curSeek+4>len) throw EndOfFileException(this,len);
213 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
214 unsigned int ret;
215 memcpy(&ret,&buf[curSeek],4);
216 curSeek+=4;
217 return (int)((ret>>24)|((ret&0xff0000)>>8)|((ret&0xff00)<<8)|((ret&0xff)<<24));
218 }
219
220 int64_t SafeReader::readL() {
221 if (curSeek+8>len) throw EndOfFileException(this,len);
222 if (curSeek+8<curSeek) throw EndOfFileException(this,len);
223 int64_t ret;
224 memcpy(&ret,&buf[curSeek],8);
225 curSeek+=8;
226 return ret;
227 }
228
229 float SafeReader::readF() {
230 if (curSeek+4>len) throw EndOfFileException(this,len);
231 if (curSeek+4<curSeek) throw EndOfFileException(this,len);
232 float ret;
233 memcpy(&ret,&buf[curSeek],4);
234 curSeek+=4;
235 return ret;
236 }
237
238 double SafeReader::readD() {
239 if (curSeek+8>len) throw EndOfFileException(this,len);
240 if (curSeek+8<curSeek) throw EndOfFileException(this,len);
241 double ret;
242 memcpy(&ret,&buf[curSeek],8);
243 curSeek+=8;
244 return ret;
245 }
246 #endif
247
248 String SafeReader::readStringWithEncoding(DivStringEncoding encoding, size_t stlen) {
249 String ret;
250 #ifdef READ_DEBUG
251 logD("SR: reading string len %d at %x",stlen,curSeek);
252 #endif
253 size_t curPos=0;
254 if (isEOF()) throw EndOfFileException(this, len);
255 bool zero=false;
256
257 while (!isEOF() && curPos<stlen) {
258 unsigned char c=readC();
259 if (c==0) {
260 zero=true;
261 }
262 if (!zero) {
263 if (encoding==DIV_ENCODING_LATIN1) {
264 if (c>=0x20) {
265 if (c&0x80) {
266 if (c>=0xa0) {
267 ret.push_back(0xc0|(c>>6));
268 ret.push_back(0x80|(c&63));
269 }
270 } else {
271 ret.push_back(c);
272 }
273 }
274 } else if (encoding==DIV_ENCODING_LATIN1_SPECIAL) {
275 if (c&0x80) {
276 if (c>=0xa0) {
277 ret.push_back(0xc0|(c>>6));
278 ret.push_back(0x80|(c&63));
279 }
280 } else {
281 ret.push_back(c);
282 }
283 } else {
284 ret.push_back(c);
285 }
286 }
287 curPos++;
288 }
289 return ret;
290 }
291
292 String SafeReader::readStringWithEncoding(DivStringEncoding encoding) {
293 String ret;
294 unsigned char c;
295 if (isEOF()) throw EndOfFileException(this, len);
296
297 while (!isEOF() && (c=readC())!=0) {
298 if (encoding==DIV_ENCODING_LATIN1) {
299 if (c>=0x20) {
300 if (c&0x80) {
301 if (c>=0xa0) {
302 ret.push_back(0xc0|(c>>6));
303 ret.push_back(0x80|(c&63));
304 }
305 } else {
306 ret.push_back(c);
307 }
308 }
309 } else if (encoding==DIV_ENCODING_LATIN1_SPECIAL) {
310 if (c&0x80) {
311 if (c>=0xa0) {
312 ret.push_back(0xc0|(c>>6));
313 ret.push_back(0x80|(c&63));
314 }
315 } else {
316 ret.push_back(c);
317 }
318 } else {
319 ret.push_back(c);
320 }
321 }
322 return ret;
323 }
324
325 String SafeReader::readString() {
326 return readStringWithEncoding(DIV_ENCODING_NONE);
327 }
328
329 String SafeReader::readString(size_t stlen) {
330 return readStringWithEncoding(DIV_ENCODING_NONE,stlen);
331 }
332
333 String SafeReader::readStringLatin1() {
334 return readStringWithEncoding(DIV_ENCODING_LATIN1);
335 }
336
337 String SafeReader::readStringLatin1(size_t stlen) {
338 return readStringWithEncoding(DIV_ENCODING_LATIN1,stlen);
339 }
340
341 String SafeReader::readStringLatin1Special() {
342 return readStringWithEncoding(DIV_ENCODING_LATIN1_SPECIAL);
343 }
344
345 String SafeReader::readStringLatin1Special(size_t stlen) {
346 return readStringWithEncoding(DIV_ENCODING_LATIN1_SPECIAL,stlen);
347 }
348
349 String SafeReader::readStringLine() {
350 String ret;
351 unsigned char c;
352 if (isEOF()) throw EndOfFileException(this, len);
353
354 while (!isEOF() && (c=readC())!=0) {
355 if (c=='\r' || c=='\n') {
356 break;
357 }
358 ret.push_back(c);
359 }
360 return ret;
361 }
362
363 String SafeReader::readStringToken(unsigned char delim, bool stripContiguous) {
364 String ret;
365 unsigned char c;
366 if (isEOF()) throw EndOfFileException(this, len);
367
368 while (!isEOF() && (c=readC())!=0) {
369 if (c=='\r' || c=='\n') {
370 break;
371 }
372 if (c==delim) {
373 if (ret.length()==0 && stripContiguous) {
374 continue;
375 }
376 break;
377 }
378 ret.push_back(c);
379 }
380 return ret;
381 }
382
383 String SafeReader::readStringToken() {
384 // This will strip LHS whitespace and only return contents after it.
385 return readStringToken(' ', true);
386 }