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"
27 static bool EndianSwitch
= false;
29 DataStream::DataStream(void)
35 DataStream::~DataStream(void)
39 void DataStream::SetEndianSwitch(int tmp
)
41 EndianSwitch
= !! tmp
;
44 bool DataStream::IsEndianSwitch()
49 /** Returns true if the stream is encrypted */
50 bool DataStream::CheckEncrypted()
53 Seek( 0, GEM_STREAM_START
);
61 Seek( 0, GEM_STREAM_START
);
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
);
78 unsigned long DataStream::GetPos() const
83 unsigned long DataStream::Size() const
88 unsigned long DataStream::Remains() const
93 int DataStream::ReadWord(ieWord
*dest
)
95 int len
= Read(dest
, 2);
98 tmp
=((unsigned char *) dest
)[0];
99 ((unsigned char *) dest
)[0]=((unsigned char *) dest
)[1];
100 ((unsigned char *) dest
)[1]=tmp
;
105 int DataStream::ReadWordSigned(ieWordSigned
*dest
)
107 int len
= Read(dest
, 2);
110 tmp
=((unsigned char *) dest
)[0];
111 ((unsigned char *) dest
)[0]=((unsigned char *) dest
)[1];
112 ((unsigned char *) dest
)[1]=tmp
;
117 int DataStream::WriteWord(const ieWord
*src
)
122 tmp
[0]=((unsigned char *) src
)[1];
123 tmp
[1]=((unsigned char *) src
)[0];
124 len
= Write( tmp
, 2 );
127 len
= Write( src
, 2 );
132 int DataStream::ReadDword(ieDword
*dest
)
134 int len
= Read(dest
, 4);
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
;
147 int DataStream::WriteDword(const ieDword
*src
)
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 );
159 len
= Write( src
, 4 );
164 int DataStream::ReadResRef(ieResRef dest
)
166 int len
= Read(dest
, 8);
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;
182 int DataStream::WriteResRef(const ieResRef src
)
184 return Write( src
, 8);