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.
23 #include "DataStream.h"
25 static bool EndianSwitch
= false;
27 DataStream::DataStream(void)
33 DataStream::~DataStream(void)
37 void DataStream::SetEndianSwitch(int tmp
)
39 EndianSwitch
= !! tmp
;
42 bool DataStream::IsEndianSwitch()
47 /** Returns true if the stream is encrypted */
48 bool DataStream::CheckEncrypted()
51 Seek( 0, GEM_STREAM_START
);
59 Seek( 0, GEM_STREAM_START
);
63 /** No descriptions */
64 void DataStream::ReadDecrypted(void* buf
, unsigned int size
)
66 for (unsigned int i
= 0; i
< size
; i
++)
67 ( ( unsigned char * ) buf
)[i
] ^= GEM_ENCRYPTION_KEY
[( Pos
+ i
) & 63];
70 void DataStream::Rewind()
72 Seek( Encrypted
? 2 : 0, GEM_STREAM_START
);
76 unsigned long DataStream::GetPos() const
81 unsigned long DataStream::Size() const
86 unsigned long DataStream::Remains() const
91 int DataStream::ReadWord(ieWord
*dest
)
93 int len
= Read(dest
, 2);
96 tmp
=((unsigned char *) dest
)[0];
97 ((unsigned char *) dest
)[0]=((unsigned char *) dest
)[1];
98 ((unsigned char *) dest
)[1]=tmp
;
103 int DataStream::ReadWordSigned(ieWordSigned
*dest
)
105 int len
= Read(dest
, 2);
108 tmp
=((unsigned char *) dest
)[0];
109 ((unsigned char *) dest
)[0]=((unsigned char *) dest
)[1];
110 ((unsigned char *) dest
)[1]=tmp
;
115 int DataStream::WriteWord(const ieWord
*src
)
120 tmp
[0]=((unsigned char *) src
)[1];
121 tmp
[1]=((unsigned char *) src
)[0];
122 len
= Write( tmp
, 2 );
125 len
= Write( src
, 2 );
130 int DataStream::ReadDword(ieDword
*dest
)
132 int len
= Read(dest
, 4);
135 tmp
=((unsigned char *) dest
)[0];
136 ((unsigned char *) dest
)[0]=((unsigned char *) dest
)[3];
137 ((unsigned char *) dest
)[3]=tmp
;
138 tmp
=((unsigned char *) dest
)[1];
139 ((unsigned char *) dest
)[1]=((unsigned char *) dest
)[2];
140 ((unsigned char *) dest
)[2]=tmp
;
145 int DataStream::WriteDword(const ieDword
*src
)
150 tmp
[0]=((unsigned char *) src
)[3];
151 tmp
[1]=((unsigned char *) src
)[2];
152 tmp
[2]=((unsigned char *) src
)[1];
153 tmp
[3]=((unsigned char *) src
)[0];
154 len
= Write( tmp
, 4 );
157 len
= Write( src
, 4 );
162 int DataStream::ReadResRef(ieResRef dest
)
164 int len
= Read(dest
, 8);
166 // lowercase the resref
167 for(i
= 0; i
< 8; i
++) {
168 dest
[i
] = (char) tolower(dest
[i
]);
170 // remove trailing spaces
171 for (i
= 7; i
>= 0; i
--) {
172 if (dest
[i
] == ' ') dest
[i
] = 0;
180 int DataStream::WriteResRef(const ieResRef src
)
182 return Write( src
, 8);