Move EventMgr to GUI.
[gemrb.git] / gemrb / core / DataStream.cpp
blobe30c8d6f9c0deedb8e226af48dd1ce83852c451e
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "DataStream.h"
23 #include "win32def.h"
25 #include <ctype.h>
27 static bool EndianSwitch = false;
29 DataStream::DataStream(void)
31 Pos = 0;
32 Encrypted = false;
35 DataStream::~DataStream(void)
39 void DataStream::SetEndianSwitch(int tmp)
41 EndianSwitch = !! tmp;
44 bool DataStream::IsEndianSwitch()
46 return EndianSwitch;
49 /** Returns true if the stream is encrypted */
50 bool DataStream::CheckEncrypted()
52 ieWord two;
53 Seek( 0, GEM_STREAM_START );
54 Read( &two, 2 );
55 if (two == 0xFFFF) {
56 Pos = 0;
57 Encrypted = true;
58 size -= 2;
59 return true;
61 Seek( 0, GEM_STREAM_START );
62 Encrypted = false;
63 return false;
65 /** No descriptions */
66 void DataStream::ReadDecrypted(void* buf, unsigned int size)
68 for (unsigned int i = 0; i < size; i++)
69 ( ( unsigned char * ) buf )[i] ^= GEM_ENCRYPTION_KEY[( Pos + i ) & 63];
72 void DataStream::Rewind()
74 Seek( Encrypted ? 2 : 0, GEM_STREAM_START );
75 Pos = 0;
78 unsigned long DataStream::GetPos() const
80 return Pos;
83 unsigned long DataStream::Size() const
85 return size;
88 unsigned long DataStream::Remains() const
90 return size-Pos;
93 int DataStream::ReadWord(ieWord *dest)
95 int len = Read(dest, 2);
96 if (EndianSwitch) {
97 unsigned char tmp;
98 tmp=((unsigned char *) dest)[0];
99 ((unsigned char *) dest)[0]=((unsigned char *) dest)[1];
100 ((unsigned char *) dest)[1]=tmp;
102 return len;
105 int DataStream::ReadWordSigned(ieWordSigned *dest)
107 int len = Read(dest, 2);
108 if (EndianSwitch) {
109 unsigned char tmp;
110 tmp=((unsigned char *) dest)[0];
111 ((unsigned char *) dest)[0]=((unsigned char *) dest)[1];
112 ((unsigned char *) dest)[1]=tmp;
114 return len;
117 int DataStream::WriteWord(const ieWord *src)
119 int len;
120 if (EndianSwitch) {
121 char tmp[2];
122 tmp[0]=((unsigned char *) src)[1];
123 tmp[1]=((unsigned char *) src)[0];
124 len = Write( tmp, 2 );
126 else {
127 len = Write( src, 2 );
129 return len;
132 int DataStream::ReadDword(ieDword *dest)
134 int len = Read(dest, 4);
135 if (EndianSwitch) {
136 unsigned char tmp;
137 tmp=((unsigned char *) dest)[0];
138 ((unsigned char *) dest)[0]=((unsigned char *) dest)[3];
139 ((unsigned char *) dest)[3]=tmp;
140 tmp=((unsigned char *) dest)[1];
141 ((unsigned char *) dest)[1]=((unsigned char *) dest)[2];
142 ((unsigned char *) dest)[2]=tmp;
144 return len;
147 int DataStream::WriteDword(const ieDword *src)
149 int len;
150 if (EndianSwitch) {
151 char tmp[4];
152 tmp[0]=((unsigned char *) src)[3];
153 tmp[1]=((unsigned char *) src)[2];
154 tmp[2]=((unsigned char *) src)[1];
155 tmp[3]=((unsigned char *) src)[0];
156 len = Write( tmp, 4 );
158 else {
159 len = Write( src, 4 );
161 return len;
164 int DataStream::ReadResRef(ieResRef dest)
166 int len = Read(dest, 8);
167 int i;
168 // lowercase the resref
169 for(i = 0; i < 8; i++) {
170 dest[i] = (char) tolower(dest[i]);
172 // remove trailing spaces
173 for (i = 7; i >= 0; i--) {
174 if (dest[i] == ' ') dest[i] = 0;
175 else break;
177 // null-terminate
178 dest[8] = 0;
179 return len;
182 int DataStream::WriteResRef(const ieResRef src)
184 return Write( src, 8);