| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2020 - 2024 * |
| 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 * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "agg_file.h" |
| 22 | |
| 23 | #include <cctype> |
| 24 | #include <cstdint> |
| 25 | #include <iterator> |
| 26 | #include <string> |
| 27 | |
| 28 | namespace fheroes2 |
| 29 | { |
| 30 | bool AGGFile::open( const std::string & fileName ) |
| 31 | { |
| 32 | if ( !_stream.open( fileName, "rb" ) ) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | const size_t size = _stream.size(); |
| 37 | const size_t count = _stream.getLE16(); |
| 38 | const size_t fileRecordSize = sizeof( uint32_t ) * 3; |
| 39 | |
| 40 | if ( count * ( fileRecordSize + _maxFilenameSize ) >= size ) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | ROStreamBuf fileEntries = _stream.getStreamBuf( count * fileRecordSize ); |
| 45 | const size_t nameEntriesSize = _maxFilenameSize * count; |
| 46 | _stream.seek( size - nameEntriesSize ); |
| 47 | ROStreamBuf nameEntries = _stream.getStreamBuf( nameEntriesSize ); |
| 48 | |
| 49 | for ( size_t i = 0; i < count; ++i ) { |
| 50 | std::string name = nameEntries.getString( _maxFilenameSize ); |
| 51 | |
| 52 | // Check 32-bit filename hash. |
| 53 | if ( fileEntries.getLE32() != calculateAggFilenameHash( name ) ) { |
| 54 | // Hash check failed. AGG file is corrupted. |
| 55 | _files.clear(); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | const uint32_t fileOffset = fileEntries.getLE32(); |
| 60 | const uint32_t fileSize = fileEntries.getLE32(); |
| 61 | _files.try_emplace( std::move( name ), std::make_pair( fileSize, fileOffset ) ); |
| 62 | } |
| 63 | |
| 64 | if ( _files.size() != count ) { |
| 65 | _files.clear(); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return !_stream.fail(); |
| 70 | } |
| 71 | |
| 72 | std::vector<uint8_t> AGGFile::read( const std::string & fileName ) |
| 73 | { |
| 74 | auto it = _files.find( fileName ); |
| 75 | if ( it == _files.end() ) { |
| 76 | return {}; |
| 77 | } |
| 78 | |
| 79 | const auto [fileSize, fileOffset] = it->second; |
| 80 | if ( fileSize > 0 ) { |
| 81 | _stream.seek( fileOffset ); |
| 82 | return _stream.getRaw( fileSize ); |
| 83 | } |
| 84 | |
| 85 | return {}; |
| 86 | } |
| 87 | |
| 88 | uint32_t calculateAggFilenameHash( const std::string_view str ) |
| 89 | { |
| 90 | uint32_t hash = 0; |
| 91 | uint32_t sum = 0; |
| 92 | |
| 93 | for ( auto iter = str.rbegin(); iter != str.rend(); ++iter ) { |
| 94 | const unsigned char c = static_cast<unsigned char>( std::toupper( static_cast<unsigned char>( *iter ) ) ); |
| 95 | |
| 96 | hash = ( hash << 5 ) + ( hash >> 25 ); |
| 97 | |
| 98 | sum += c; |
| 99 | hash += sum + c; |
| 100 | } |
| 101 | |
| 102 | return hash; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | IStreamBase & operator>>( IStreamBase & stream, fheroes2::ICNHeader & icn ) |
| 107 | { |
| 108 | icn.offsetX = static_cast<int16_t>( stream.getLE16() ); |
| 109 | icn.offsetY = static_cast<int16_t>( stream.getLE16() ); |
| 110 | icn.width = stream.getLE16(); |
| 111 | icn.height = stream.getLE16(); |
| 112 | icn.animationFrames = stream.get(); |
| 113 | icn.offsetData = stream.getLE32(); |
| 114 | |
| 115 | return stream; |
| 116 | } |