Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * endian-safe binary file IO helpers. 20 : */ 21 : 22 : // the file format has passing similarity to IFF. note however that 23 : // "chunks" aren't identified by FOURCCs; only the file header is 24 : // so marked. 25 : // all > 8-bit integers are stored in little-endian format 26 : // (hence the _le suffix). however, the caller is responsible for 27 : // swapping their raw data before passing it to PackRaw. a convenience 28 : // routine is provided for the common case of storing size_t. 29 : 30 : #ifndef INCLUDED_FILEPACKER 31 : #define INCLUDED_FILEPACKER 32 : 33 : #include "lib/allocators/shared_ptr.h" 34 : #include "lib/file/io/write_buffer.h" 35 : #include "lib/file/vfs/vfs_path.h" 36 : #include "ps/Errors.h" 37 : 38 : #include <cstddef> 39 : #include <memory> 40 : 41 : class CStr8; 42 : 43 : ERROR_GROUP(File); 44 : ERROR_TYPE(File, OpenFailed); 45 : ERROR_TYPE(File, WriteFailed); 46 : ERROR_TYPE(File, InvalidType); 47 : ERROR_TYPE(File, InvalidVersion); 48 : ERROR_TYPE(File, ReadFailed); 49 : ERROR_TYPE(File, UnexpectedEOF); 50 : 51 : 52 : /** 53 : * helper class for writing binary files. this is basically a 54 : * resizable buffer that allows adding raw data and strings; 55 : * upon calling Write(), everything is written out to disk. 56 : **/ 57 : class CFilePacker 58 : { 59 : public: 60 : /** 61 : * adds version and signature (i.e. the header) to the buffer. 62 : * this means Write() can write the entire buffer to file in one go, 63 : * which is simpler and more efficient than writing in pieces. 64 : **/ 65 : CFilePacker(std::uint32_t version, const char magic[4]); 66 : 67 : ~CFilePacker(); 68 : 69 : /** 70 : * write out to file all packed data added so far. 71 : * it's safe to call this multiple times, but typically would 72 : * only be done once. 73 : **/ 74 : void Write(const VfsPath& filename); 75 : 76 : /** 77 : * pack given number of bytes onto the end of the data stream 78 : **/ 79 : void PackRaw(const void* rawData, size_t rawDataSize); 80 : 81 : /** 82 : * convenience: convert a number (almost always a size type) to 83 : * little-endian std::uint32_t and pack that. 84 : **/ 85 : void PackSize(size_t value); 86 : 87 : /** 88 : * pack a string onto the end of the data stream 89 : * (encoded as a 32-bit length followed by the characters) 90 : **/ 91 : void PackString(const CStr8& str); 92 : 93 : private: 94 : /** 95 : * the output data stream built during pack operations. 96 : * contains the header, so we can write this out in one go. 97 : **/ 98 : WriteBuffer m_writeBuffer; 99 : }; 100 : 101 : 102 : /** 103 : * helper class for reading binary files 104 : **/ 105 : class CFileUnpacker 106 : { 107 : public: 108 : CFileUnpacker(); 109 : ~CFileUnpacker(); 110 : 111 : /** 112 : * open and read in given file, check magic bits against those given; 113 : * throw variety of exceptions if open failed / version incorrect, etc. 114 : **/ 115 : void Read(const VfsPath& filename, const char magic[4]); 116 : 117 : /** 118 : * @return version number that was stored in the file's header. 119 : **/ 120 35 : std::uint32_t GetVersion() const 121 : { 122 28 : return m_version; 123 : } 124 : 125 : /** 126 : * unpack given number of bytes from the input into the given array. 127 : * throws PSERROR_File_UnexpectedEOF if the end of the data stream is 128 : * reached before the given number of bytes have been read. 129 : **/ 130 : void UnpackRaw(void* rawData, size_t rawDataSize); 131 : 132 : /** 133 : * use UnpackRaw to retrieve 32-bits; returns their value as size_t 134 : * after converting from little endian to native byte order. 135 : **/ 136 : size_t UnpackSize(); 137 : 138 : /** 139 : * unpack a string from the raw data stream. 140 : * @param result is assigned a newly constructed CStr8 holding the 141 : * string read from the input stream. 142 : **/ 143 : void UnpackString(CStr8& result); 144 : 145 : private: 146 : // the data read from file and used during unpack operations 147 : std::unique_ptr<std::uint8_t[], AlignedDeleter> m_buf; 148 : size_t m_bufSize; 149 : 150 : size_t m_unpackPos; /// current unpack position in stream 151 : std::uint32_t m_version; /// version that was stored in the file header 152 : }; 153 : 154 : #endif