File: FileIo.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (180 lines) | stat: -rw-r--r-- 4,705 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (C) 2013 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * endian-safe binary file IO helpers.
 */

#include "precompiled.h"

#include "FileIo.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "lib/byte_order.h"

#pragma pack(push, 1)

struct FileHeader
{
	char magic[4];
	u32 version_le;
	u32 payloadSize_le;	// = file size - sizeof(FileHeader)
};
cassert(sizeof(FileHeader) == 12);

#pragma pack(pop)


//-----------------------------------------------------------------------------
// CFilePacker

CFilePacker::CFilePacker(u32 version, const char magic[4])
{
	// put header in our data array.
	// (its payloadSize_le will be updated on every Pack*() call)
	FileHeader header;
	strncpy(header.magic, magic, 4);	// not 0-terminated => no _s
	write_le32(&header.version_le, version);
	write_le32(&header.payloadSize_le, 0);
	m_writeBuffer.Append(&header, sizeof(FileHeader));
}


CFilePacker::~CFilePacker()
{
}


void CFilePacker::Write(const VfsPath& filename)
{
	const size_t payloadSize = m_writeBuffer.Size() - sizeof(FileHeader);
	const u32 payloadSize_le = to_le32(u32_from_larger(payloadSize));
	m_writeBuffer.Overwrite(&payloadSize_le, sizeof(payloadSize_le), 0+offsetof(FileHeader, payloadSize_le));

	// write out all data (including header)
	const Status st = g_VFS->CreateFile(filename, m_writeBuffer.Data(), m_writeBuffer.Size());
	if (st < 0)
	{
		LOGERROR("Failed to write file '%s' with status '%lld'", filename.string8(), (long long)st);
		throw PSERROR_File_WriteFailed();
	}
}


void CFilePacker::PackRaw(const void* rawData, size_t rawSize)
{
	m_writeBuffer.Append(rawData, rawSize);
}

void CFilePacker::PackSize(size_t value)
{
	const u32 value_le32 = to_le32(u32_from_larger(value));
	PackRaw(&value_le32, sizeof(value_le32));
}

void CFilePacker::PackString(const CStr& str)
{
	const size_t length = str.length();
	PackSize(length);
	PackRaw(str.c_str(), length);
}


//-----------------------------------------------------------------------------
// CFileUnpacker

CFileUnpacker::CFileUnpacker()
	: m_bufSize(0), m_unpackPos(0), m_version(0)
{
}


CFileUnpacker::~CFileUnpacker()
{
}


void CFileUnpacker::Read(const VfsPath& filename, const char magic[4])
{
	// if the file doesn't exist, LoadFile would raise an annoying error dialog.
	// since this (unfortunately) happens so often, we perform a separate
	// check and "just" raise an exception for the caller to handle.
	// (this is nicer than somehow squelching internal VFS error reporting)
	if(!VfsFileExists(filename))
		throw PSERROR_File_OpenFailed();

	// load the whole thing into memory
	if(g_VFS->LoadFile(filename, m_buf, m_bufSize) < 0)
		throw PSERROR_File_OpenFailed();

	// make sure we read enough for the header
	if(m_bufSize < sizeof(FileHeader))
	{
		m_buf.reset();
		m_bufSize = 0;
		throw PSERROR_File_ReadFailed();
	}

	// extract data from header
	FileHeader* header = (FileHeader*)m_buf.get();
	m_version = read_le32(&header->version_le);
	const size_t payloadSize = (size_t)read_le32(&header->payloadSize_le);

	// check we've got the right kind of file
	// .. and that we read exactly headerSize+payloadSize
	if(strncmp(header->magic, magic, 4) != 0 || m_bufSize != sizeof(FileHeader)+payloadSize)
	{
		m_buf.reset();
		m_bufSize = 0;
		throw PSERROR_File_InvalidType();
	}

	m_unpackPos = sizeof(FileHeader);
}


void CFileUnpacker::UnpackRaw(void* rawData, size_t rawDataSize)
{
	// fail if reading past end of stream
	if (m_unpackPos+rawDataSize > m_bufSize)
		throw PSERROR_File_UnexpectedEOF();

	void* src = m_buf.get() + m_unpackPos;
	memcpy(rawData, src, rawDataSize);
	m_unpackPos += rawDataSize;
}


size_t CFileUnpacker::UnpackSize()
{
	u32 value_le32;
	UnpackRaw(&value_le32, sizeof(value_le32));
	return (size_t)to_le32(value_le32);
}


void CFileUnpacker::UnpackString(CStr8& result)
{
	const size_t length = UnpackSize();

	// fail if reading past end of stream
	if (m_unpackPos+length > m_bufSize)
		throw PSERROR_File_UnexpectedEOF();

	result = CStr((char*)m_buf.get()+m_unpackPos, length);
	m_unpackPos += length;
}