Debian Codegraph @

src/engine/serialize.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2019 - 2025 *
4 * *
5 * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 *
6 * Copyright (C) 2012 by Andrey Afletdinov <[email protected]> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23
24 #include "serialize.h"
25
26 #include <algorithm>
27 #include <cstring>
28 #include <ostream>
29 #include <string>
30 #include <string_view>
31
32 #ifdef __EMSCRIPTEN__
33 #include <cstdio>
34
35 #include <emscripten.h>
36 #include <errno.h>
37 #include <fcntl.h>
38
39 #include "tools.h"
40 #endif
41
42 #include "logging.h"
43
44 namespace
45 {
46 const size_t minBufferCapacity = 1024;
47 }
48
49 void StreamBase::setBigendian( bool f )
50 {
51 if ( f ) {
52 _flags |= BIGENDIAN;
53 }
54 else {
55 _flags &= ~BIGENDIAN;
56 }
57 }
58
59 void StreamBase::setFail( bool f )
60 {
61 if ( f ) {
62 _flags |= FAILURE;
63 }
64 else {
65 _flags &= ~FAILURE;
66 }
67 }
68
69 uint16_t IStreamBase::get16()
70 {
71 return bigendian() ? getBE16() : getLE16();
72 }
73
74 uint32_t IStreamBase::get32()
75 {
76 return bigendian() ? getBE32() : getLE32();
77 }
78
79 IStreamBase & IStreamBase::operator>>( bool & v )
80 {
81 v = ( get8() != 0 );
82
83 return *this;
84 }
85
86 IStreamBase & IStreamBase::operator>>( char & v )
87 {
88 v = get8();
89
90 return *this;
91 }
92
93 IStreamBase & IStreamBase::operator>>( int8_t & v )
94 {
95 v = static_cast<int8_t>( get8() );
96
97 return *this;
98 }
99
100 IStreamBase & IStreamBase::operator>>( uint8_t & v )
101 {
102 v = get8();
103
104 return *this;
105 }
106
107 IStreamBase & IStreamBase::operator>>( int16_t & v )
108 {
109 v = get16();
110
111 return *this;
112 }
113
114 IStreamBase & IStreamBase::operator>>( uint16_t & v )
115 {
116 v = get16();
117
118 return *this;
119 }
120
121 IStreamBase & IStreamBase::operator>>( int32_t & v )
122 {
123 v = get32();
124
125 return *this;
126 }
127
128 IStreamBase & IStreamBase::operator>>( uint32_t & v )
129 {
130 v = get32();
131
132 return *this;
133 }
134
135 IStreamBase & IStreamBase::operator>>( std::string & v )
136 {
137 v.resize( get32() );
138
139 std::for_each( v.begin(), v.end(), [this]( char & item ) { item = get8(); } );
140
141 return *this;
142 }
143
144 IStreamBase & IStreamBase::operator>>( fheroes2::Point & v )
145 {
146 return *this >> v.x >> v.y;
147 }
148
149 void OStreamBase::put16( uint16_t v )
150 {
151 bigendian() ? putBE16( v ) : putLE16( v );
152 }
153
154 void OStreamBase::put32( uint32_t v )
155 {
156 bigendian() ? putBE32( v ) : putLE32( v );
157 }
158
159 OStreamBase & OStreamBase::operator<<( const bool v )
160 {
161 put8( v );
162
163 return *this;
164 }
165
166 OStreamBase & OStreamBase::operator<<( const char v )
167 {
168 put8( v );
169
170 return *this;
171 }
172
173 OStreamBase & OStreamBase::operator<<( const int8_t v )
174 {
175 put8( static_cast<uint8_t>( v ) );
176
177 return *this;
178 }
179
180 OStreamBase & OStreamBase::operator<<( const uint8_t v )
181 {
182 put8( v );
183
184 return *this;
185 }
186
187 OStreamBase & OStreamBase::operator<<( const int16_t v )
188 {
189 put16( v );
190
191 return *this;
192 }
193
194 OStreamBase & OStreamBase::operator<<( const uint16_t v )
195 {
196 put16( v );
197
198 return *this;
199 }
200
201 OStreamBase & OStreamBase::operator<<( const int32_t v )
202 {
203 put32( v );
204
205 return *this;
206 }
207
208 OStreamBase & OStreamBase::operator<<( const uint32_t v )
209 {
210 put32( v );
211
212 return *this;
213 }
214
215 OStreamBase & OStreamBase::operator<<( const std::string_view v )
216 {
217 put32( static_cast<uint32_t>( v.size() ) );
218
219 // A string is a container of bytes so it doesn't matter which endianess is being used.
220 putRaw( v.data(), v.size() );
221
222 return *this;
223 }
224
225 OStreamBase & OStreamBase::operator<<( const fheroes2::Point & v )
226 {
227 return *this << v.x << v.y;
228 }
229
230 RWStreamBuf::RWStreamBuf( const size_t size )
231 {
232 if ( size ) {
233 reallocBuf( size );
234 }
235
236 setBigendian( IS_BIGENDIAN );
237 }
238
239 void RWStreamBuf::putBE16( uint16_t v )
240 {
241 put8( v >> 8 );
242 put8( v & 0xFF );
243 }
244
245 void RWStreamBuf::putLE16( uint16_t v )
246 {
247 put8( v & 0xFF );
248 put8( v >> 8 );
249 }
250
251 void RWStreamBuf::putBE32( uint32_t v )
252 {
253 put8( v >> 24 );
254 put8( ( v >> 16 ) & 0xFF );
255 put8( ( v >> 8 ) & 0xFF );
256 put8( v & 0xFF );
257 }
258
259 void RWStreamBuf::putLE32( uint32_t v )
260 {
261 put8( v & 0xFF );
262 put8( ( v >> 8 ) & 0xFF );
263 put8( ( v >> 16 ) & 0xFF );
264 put8( v >> 24 );
265 }
266
267 void RWStreamBuf::putRaw( const void * ptr, size_t size )
268 {
269 if ( size == 0 ) {
270 return;
271 }
272
273 if ( sizep() < size ) {
274 if ( size < capacity() / 2 ) {
275 reallocBuf( capacity() + capacity() / 2 );
276 }
277 else {
278 reallocBuf( capacity() + size );
279 }
280 }
281
282 if ( sizep() < size ) {
283 assert( 0 );
284 return;
285 }
286
287 memcpy( _itput, ptr, size );
288
289 _itput = _itput + size;
290 }
291
292 void RWStreamBuf::put8( const uint8_t v )
293 {
294 if ( sizep() < 1 ) {
295 reallocBuf( capacity() + capacity() / 2 );
296 }
297
298 if ( sizep() < 1 ) {
299 assert( 0 );
300 return;
301 }
302
303 *_itput = v;
304 ++_itput;
305 }
306
307 size_t RWStreamBuf::tellp() const
308 {
309 assert( _itbeg <= _itput );
310
311 return _itput - _itbeg;
312 }
313
314 size_t RWStreamBuf::sizep() const
315 {
316 assert( _itput <= _itend );
317
318 return _itend - _itput;
319 }
320
321 void RWStreamBuf::reallocBuf( size_t size )
322 {
323 if ( !_buf ) {
324 assert( ( []( const auto... args ) { return ( ( args == nullptr ) && ... ); }( _itbeg, _itget, _itput, _itend ) ) );
325
326 size = std::max( size, minBufferCapacity );
327
328 _buf = std::make_unique<uint8_t[]>( size );
329
330 _itbeg = _buf.get();
331 _itend = _itbeg + size;
332
333 _itput = _itbeg;
334 _itget = _itbeg;
335
336 return;
337 }
338
339 assert( ( []( const auto... args ) { return ( ( args != nullptr ) && ... ); }( _itbeg, _itget, _itput, _itend ) ) && _itbeg <= _itget && _itget <= _itput
340 && _itput <= _itend );
341
342 if ( sizep() < size ) {
343 size = std::max( size, minBufferCapacity );
344
345 std::unique_ptr<uint8_t[]> newBuf = std::make_unique<uint8_t[]>( size );
346
347 std::copy( _itbeg, _itput, newBuf.get() );
348
349 _itput = newBuf.get() + tellp();
350 _itget = newBuf.get() + tellg();
351
352 _buf = std::move( newBuf );
353
354 _itbeg = _buf.get();
355 _itend = _itbeg + size;
356 }
357 }
358
359 ROStreamBuf::ROStreamBuf( const std::vector<uint8_t> & buf )
360 {
361 _itbeg = buf.data();
362 _itend = _itbeg + buf.size();
363 _itget = _itbeg;
364 _itput = _itend;
365
366 setBigendian( IS_BIGENDIAN );
367 }
368
369 ROStreamBuf::ROStreamBuf( std::vector<uint8_t> && buf )
370 : _buf( std::move( buf ) )
371 {
372 _itbeg = _buf.data();
373 _itend = _itbeg + _buf.size();
374 _itget = _itbeg;
375 _itput = _itend;
376
377 setBigendian( IS_BIGENDIAN );
378 }
379
380 std::pair<const uint8_t *, size_t> ROStreamBuf::getRawView( const size_t size /* = 0 */ )
381 {
382 const size_t remainSize = sizeg();
383 const size_t resultSize = size > 0 ? std::min( size, remainSize ) : remainSize;
384
385 auto v = std::make_pair( _itget, resultSize );
386
387 _itget += resultSize;
388
389 return v;
390 }
391
392 std::string_view ROStreamBuf::getStringView( const size_t size /* = 0 */ )
393 {
394 const size_t remainSize = sizeg();
395 const size_t sizeToSkip = size > 0 ? std::min( size, remainSize ) : remainSize;
396
397 const uint8_t * strBeg = _itget;
398 _itget += sizeToSkip;
399
400 const uint8_t * strEnd = std::find( strBeg, _itget, 0 );
401 assert( strBeg <= strEnd );
402
403 static_assert( std::is_same_v<std::remove_const_t<std::remove_reference_t<decltype( *strBeg )>>, unsigned char> );
404
405 return { reinterpret_cast<const char *>( strBeg ), static_cast<size_t>( strEnd - strBeg ) };
406 }
407
408 bool StreamFile::open( const std::string & fn, const std::string & mode )
409 {
410 _file.reset( std::fopen( fn.c_str(), mode.c_str() ) );
411 // codechecker_false_positive [alpha.unix.Stream] Opened stream never closed. Potential resource leak
412 if ( !_file ) {
413 ERROR_LOG( "Error opening file " << fn )
414 }
415
416 setFail( !_file );
417
418 return !fail();
419 }
420
421 void StreamFile::close()
422 {
423 _file.reset();
424 }
425
426 size_t StreamFile::size()
427 {
428 if ( !_file ) {
429 return 0;
430 }
431
432 const long pos = std::ftell( _file.get() );
433 if ( pos < 0 ) {
434 setFail();
435
436 return 0;
437 }
438
439 if ( std::fseek( _file.get(), 0, SEEK_END ) != 0 ) {
440 setFail();
441
442 return 0;
443 }
444
445 const long len = std::ftell( _file.get() );
446 if ( len < 0 ) {
447 setFail();
448
449 return 0;
450 }
451
452 if ( std::fseek( _file.get(), pos, SEEK_SET ) != 0 ) {
453 setFail();
454
455 return 0;
456 }
457
458 return static_cast<size_t>( len );
459 }
460
461 size_t StreamFile::tell()
462 {
463 if ( !_file ) {
464 return 0;
465 }
466
467 const long pos = std::ftell( _file.get() );
468 if ( pos < 0 ) {
469 setFail();
470
471 return 0;
472 }
473
474 return static_cast<size_t>( pos );
475 }
476
477 void StreamFile::seek( const size_t pos )
478 {
479 if ( !_file ) {
480 return;
481 }
482
483 if ( std::fseek( _file.get(), static_cast<long>( pos ), SEEK_SET ) != 0 ) {
484 setFail();
485 }
486 }
487
488 size_t StreamFile::sizeg()
489 {
490 if ( !_file ) {
491 return 0;
492 }
493
494 const long pos = std::ftell( _file.get() );
495 if ( pos < 0 ) {
496 setFail();
497
498 return 0;
499 }
500
501 if ( std::fseek( _file.get(), 0, SEEK_END ) != 0 ) {
502 setFail();
503
504 return 0;
505 }
506
507 const long len = std::ftell( _file.get() );
508 if ( len < 0 ) {
509 setFail();
510
511 return 0;
512 }
513
514 if ( std::fseek( _file.get(), pos, SEEK_SET ) != 0 ) {
515 setFail();
516
517 return 0;
518 }
519
520 // Something weird has happened
521 if ( len < pos ) {
522 setFail();
523
524 return 0;
525 }
526
527 return static_cast<size_t>( len - pos );
528 }
529
530 void StreamFile::skip( size_t size )
531 {
532 if ( !_file ) {
533 return;
534 }
535
536 if ( std::fseek( _file.get(), static_cast<long int>( size ), SEEK_CUR ) != 0 ) {
537 setFail();
538 }
539 }
540
541 uint8_t StreamFile::get8()
542 {
543 return getUint<uint8_t>();
544 }
545
546 void StreamFile::put8( const uint8_t v )
547 {
548 putUint<uint8_t>( v );
549 }
550
551 uint16_t StreamFile::getBE16()
552 {
553 return be16toh( getUint<uint16_t>() );
554 }
555
556 uint16_t StreamFile::getLE16()
557 {
558 return le16toh( getUint<uint16_t>() );
559 }
560
561 uint32_t StreamFile::getBE32()
562 {
563 return be32toh( getUint<uint32_t>() );
564 }
565
566 uint32_t StreamFile::getLE32()
567 {
568 return le32toh( getUint<uint32_t>() );
569 }
570
571 void StreamFile::putBE16( uint16_t v )
572 {
573 putUint<uint16_t>( htobe16( v ) );
574 }
575
576 void StreamFile::putLE16( uint16_t v )
577 {
578 putUint<uint16_t>( htole16( v ) );
579 }
580
581 void StreamFile::putBE32( uint32_t v )
582 {
583 putUint<uint32_t>( htobe32( v ) );
584 }
585
586 void StreamFile::putLE32( uint32_t v )
587 {
588 putUint<uint32_t>( htole32( v ) );
589 }
590
591 std::vector<uint8_t> StreamFile::getRaw( const size_t size )
592 {
593 if ( !_file ) {
594 return {};
595 }
596
597 const size_t chunkSize = size > 0 ? size : sizeg();
598 if ( chunkSize == 0 ) {
599 return {};
600 }
601
602 std::vector<uint8_t> v( chunkSize, 0 );
603
604 if ( std::fread( v.data(), chunkSize, 1, _file.get() ) != 1 ) {
605 setFail();
606
607 return {};
608 }
609
610 return v;
611 }
612
613 void StreamFile::putRaw( const void * ptr, size_t size )
614 {
615 if ( size == 0 ) {
616 // Nothing to write. Ignore it.
617 return;
618 }
619
620 if ( !_file ) {
621 return;
622 }
623
624 if ( std::fwrite( ptr, size, 1, _file.get() ) != 1 ) {
625 setFail();
626 }
627 }
628
629 ROStreamBuf StreamFile::getStreamBuf( const size_t size /* = 0 */ )
630 {
631 return ROStreamBuf{ getRaw( size ) };
632 }
633
634 std::string StreamFile::getString( const size_t size /* = 0 */ )
635 {
636 const std::vector<uint8_t> buf = getRaw( size );
637
638 return { buf.begin(), std::find( buf.begin(), buf.end(), 0 ) };
639 }
640
641 int StreamFile::closeFile( std::FILE * f )
642 {
643 #ifdef __EMSCRIPTEN__
644 const bool needSyncFS = [f]() {
645 static_assert( CountBits( O_WRONLY ) == 1 && CountBits( O_RDWR ) == 1 );
646
647 const int fn = fileno( f );
648 if ( fn < 0 ) {
649 ERROR_LOG( "fileno() error: " << errno )
650 return false;
651 }
652
653 const int flags = fcntl( fn, F_GETFL );
654 if ( flags < 0 ) {
655 ERROR_LOG( "fcntl() error: " << errno )
656 return false;
657 }
658
659 return ( flags & O_WRONLY ) || ( flags & O_RDWR );
660 }();
661 #endif
662
663 const int res = std::fclose( f );
664
665 #ifdef __EMSCRIPTEN__
666 if ( needSyncFS ) {
667 #if defined( __GNUC__ )
668 #pragma GCC diagnostic push
669
670 #pragma GCC diagnostic ignored "-Wvariadic-macro-arguments-omitted"
671 #endif
672
673 EM_ASM(
674 // The following code is not C++ code, but JavaScript code.
675 // clang-format off
676 FS.syncfs( err => err && console.warn( "FS.syncfs() error:", err ) )
677 // clang-format on
678 );
679
680 #if defined( __GNUC__ )
681 #pragma GCC diagnostic pop
682 #endif
683 }
684 #endif
685
686 return res;
687 }