1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * This code is based on Broken Sword 2.5 engine
25  *
26  * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
27  *
28  * Licensed under GNU GPL v2
29  *
30  */
31 
32 #include "common/textconsole.h"
33 
34 #include "sword25/kernel/inputpersistenceblock.h"
35 
36 namespace Sword25 {
37 
InputPersistenceBlock(const void * data,uint dataLength,int version)38 InputPersistenceBlock::InputPersistenceBlock(const void *data, uint dataLength, int version) :
39 	_data(static_cast<const byte *>(data), dataLength),
40 	_errorState(NONE),
41 	_version(version) {
42 	_iter = _data.begin();
43 }
44 
~InputPersistenceBlock()45 InputPersistenceBlock::~InputPersistenceBlock() {
46 	if (_iter != _data.end())
47 		warning("Persistence block was not read to the end.");
48 }
49 
read(int16 & value)50 void InputPersistenceBlock::read(int16 &value) {
51 	int32 v;
52 	read(v);
53 	value = static_cast<int16>(v);
54 }
55 
read(int32 & value)56 void InputPersistenceBlock::read(int32 &value) {
57 	if (checkMarker(SINT_MARKER)) {
58 		value = (int32)READ_LE_UINT32(_iter);
59 		_iter += 4;
60 	} else {
61 		value = 0;
62 	}
63 }
64 
read(uint32 & value)65 void InputPersistenceBlock::read(uint32 &value) {
66 	if (checkMarker(UINT_MARKER)) {
67 		value = READ_LE_UINT32(_iter);
68 		_iter += 4;
69 	} else {
70 		value = 0;
71 	}
72 }
73 
read(float & value)74 void InputPersistenceBlock::read(float &value) {
75 	if (checkMarker(FLOAT_MARKER)) {
76 		uint32 tmp[1];
77 		tmp[0] = READ_LE_UINT32(_iter);
78 		value = ((float *)tmp)[0];
79 		_iter += 4;
80 	} else {
81 		value = 0.0f;
82 	}
83 }
84 
read(bool & value)85 void InputPersistenceBlock::read(bool &value) {
86 	if (checkMarker(BOOL_MARKER)) {
87 		uint uintBool = READ_LE_UINT32(_iter);
88 		_iter += 4;
89 		value = uintBool != 0;
90 	} else {
91 		value = false;
92 	}
93 }
94 
readString(Common::String & value)95 void InputPersistenceBlock::readString(Common::String &value) {
96 	value = "";
97 
98 	if (checkMarker(STRING_MARKER)) {
99 		uint32 size;
100 		read(size);
101 
102 		if (checkBlockSize(size)) {
103 			value = Common::String(reinterpret_cast<const char *>(&*_iter), size);
104 			_iter += size;
105 		}
106 	}
107 }
108 
readByteArray(Common::Array<byte> & value)109 void InputPersistenceBlock::readByteArray(Common::Array<byte> &value) {
110 	if (checkMarker(BLOCK_MARKER)) {
111 		uint32 size;
112 		read(size);
113 
114 		if (checkBlockSize(size)) {
115 			value = Common::Array<byte>(_iter, size);
116 			_iter += size;
117 		}
118 	}
119 }
120 
checkBlockSize(int size)121 bool InputPersistenceBlock::checkBlockSize(int size) {
122 	if (_data.end() - _iter >= size) {
123 		return true;
124 	} else {
125 		_errorState = END_OF_DATA;
126 		error("Unexpected end of persistence block.");
127 		return false;
128 	}
129 }
130 
checkMarker(byte marker)131 bool InputPersistenceBlock::checkMarker(byte marker) {
132 	if (!isGood() || !checkBlockSize(1))
133 		return false;
134 
135 	if (*_iter++ == marker) {
136 		return true;
137 	} else {
138 		_errorState = OUT_OF_SYNC;
139 		error("Wrong type marker found in persistence block.");
140 		return false;
141 	}
142 }
143 
144 } // End of namespace Sword25
145