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 : #include "precompiled.h" 23 : 24 : #include "FileIo.h" 25 : 26 : #include "lib/byte_order.h" 27 : #include "lib/code_annotation.h" 28 : #include "lib/file/vfs/vfs.h" 29 : #include "lib/lib.h" 30 : #include "lib/status.h" 31 : #include "ps/CLogger.h" 32 : #include "ps/CStr.h" 33 : #include "ps/Filesystem.h" 34 : 35 : #include <algorithm> 36 : #include <cstring> 37 : #include <string> 38 : 39 : #pragma pack(push, 1) 40 : 41 : struct FileHeader 42 : { 43 : char magic[4]; 44 : std::uint32_t version_le; 45 : std::uint32_t payloadSize_le; // = file size - sizeof(FileHeader) 46 : }; 47 : cassert(sizeof(FileHeader) == 12); 48 : 49 : #pragma pack(pop) 50 : 51 : 52 : //----------------------------------------------------------------------------- 53 : // CFilePacker 54 : 55 0 : CFilePacker::CFilePacker(std::uint32_t version, const char magic[4]) 56 : { 57 : // put header in our data array. 58 : // (its payloadSize_le will be updated on every Pack*() call) 59 0 : FileHeader header; 60 0 : std::copy(magic, magic + 4, header.magic); 61 0 : write_le32(&header.version_le, version); 62 0 : write_le32(&header.payloadSize_le, 0); 63 0 : m_writeBuffer.Append(&header, sizeof(FileHeader)); 64 0 : } 65 : 66 : 67 0 : CFilePacker::~CFilePacker() 68 : { 69 0 : } 70 : 71 : 72 0 : void CFilePacker::Write(const VfsPath& filename) 73 : { 74 0 : const size_t payloadSize = m_writeBuffer.Size() - sizeof(FileHeader); 75 0 : const std::uint32_t payloadSize_le = to_le32(u32_from_larger(payloadSize)); 76 0 : m_writeBuffer.Overwrite(&payloadSize_le, sizeof(payloadSize_le), 0+offsetof(FileHeader, payloadSize_le)); 77 : 78 : // write out all data (including header) 79 0 : const Status st = g_VFS->CreateFile(filename, {m_writeBuffer.Data().get(), m_writeBuffer.Size()}); 80 0 : if (st < 0) 81 : { 82 0 : LOGERROR("Failed to write file '{}' with status '{}'", filename.string8(), st); 83 0 : throw PSERROR_File_WriteFailed(); 84 : } 85 0 : } 86 : 87 : 88 0 : void CFilePacker::PackRaw(const void* rawData, size_t rawSize) 89 : { 90 0 : m_writeBuffer.Append(rawData, rawSize); 91 0 : } 92 : 93 0 : void CFilePacker::PackSize(size_t value) 94 : { 95 0 : const std::uint32_t value_le32 = to_le32(u32_from_larger(value)); 96 0 : PackRaw(&value_le32, sizeof(value_le32)); 97 0 : } 98 : 99 0 : void CFilePacker::PackString(const CStr& str) 100 : { 101 0 : const size_t length = str.length(); 102 0 : PackSize(length); 103 0 : PackRaw(str.c_str(), length); 104 0 : } 105 : 106 : 107 : //----------------------------------------------------------------------------- 108 : // CFileUnpacker 109 : 110 10 : CFileUnpacker::CFileUnpacker() 111 10 : : m_bufSize(0), m_unpackPos(0), m_version(0) 112 : { 113 10 : } 114 : 115 : 116 10 : CFileUnpacker::~CFileUnpacker() 117 : { 118 10 : } 119 : 120 : 121 7 : void CFileUnpacker::Read(const VfsPath& filename, const char magic[4]) 122 : { 123 : // if the file doesn't exist, LoadFile would raise an annoying error dialog. 124 : // since this (unfortunately) happens so often, we perform a separate 125 : // check and "just" raise an exception for the caller to handle. 126 : // (this is nicer than somehow squelching internal VFS error reporting) 127 7 : if(!VfsFileExists(filename)) 128 0 : throw PSERROR_File_OpenFailed(); 129 : 130 : // load the whole thing into memory 131 7 : if(g_VFS->LoadFile(filename, m_buf, m_bufSize) < 0) 132 0 : throw PSERROR_File_OpenFailed(); 133 : 134 : // make sure we read enough for the header 135 7 : if(m_bufSize < sizeof(FileHeader)) 136 : { 137 0 : m_buf.reset(); 138 0 : m_bufSize = 0; 139 0 : throw PSERROR_File_ReadFailed(); 140 : } 141 : 142 : // extract data from header 143 7 : FileHeader* header = (FileHeader*)m_buf.get(); 144 7 : m_version = read_le32(&header->version_le); 145 7 : const size_t payloadSize = (size_t)read_le32(&header->payloadSize_le); 146 : 147 : // check we've got the right kind of file 148 : // .. and that we read exactly headerSize+payloadSize 149 7 : if(strncmp(header->magic, magic, 4) != 0 || m_bufSize != sizeof(FileHeader)+payloadSize) 150 : { 151 0 : m_buf.reset(); 152 0 : m_bufSize = 0; 153 0 : throw PSERROR_File_InvalidType(); 154 : } 155 : 156 7 : m_unpackPos = sizeof(FileHeader); 157 7 : } 158 : 159 : 160 3055 : void CFileUnpacker::UnpackRaw(void* rawData, size_t rawDataSize) 161 : { 162 : // fail if reading past end of stream 163 3055 : if (m_unpackPos+rawDataSize > m_bufSize) 164 0 : throw PSERROR_File_UnexpectedEOF(); 165 : 166 3055 : void* src = m_buf.get() + m_unpackPos; 167 3055 : memcpy(rawData, src, rawDataSize); 168 3055 : m_unpackPos += rawDataSize; 169 3055 : } 170 : 171 : 172 36 : size_t CFileUnpacker::UnpackSize() 173 : { 174 36 : std::uint32_t value_le32; 175 32 : UnpackRaw(&value_le32, sizeof(value_le32)); 176 36 : return (size_t)to_le32(value_le32); 177 : } 178 : 179 : 180 4 : void CFileUnpacker::UnpackString(CStr8& result) 181 : { 182 4 : const size_t length = UnpackSize(); 183 : 184 : // fail if reading past end of stream 185 4 : if (m_unpackPos+length > m_bufSize) 186 0 : throw PSERROR_File_UnexpectedEOF(); 187 : 188 4 : result = CStr((char*)m_buf.get()+m_unpackPos, length); 189 4 : m_unpackPos += length; 190 4 : }