~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/sizebuf.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * OpenTyrian: A modern cross-platform port of Tyrian
 
3
 * Copyright (C) 2007-2009  The OpenTyrian Development Team
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
/*
 
21
 * This file is largely based on (and named after) a set of common reading/
 
22
 * writing functions used in Quake engines.  Its purpose is to allow extraction
 
23
 * of bytes, words, and dwords in a safe, endian adjused environment and should
 
24
 * probably be used in any situation where checking for buffer overflows
 
25
 * manually makes the code a godawful mess.
 
26
 *
 
27
 * Currently this is only used by the animation decoding.
 
28
 *
 
29
 * This file is written with the intention of being easily converted into a
 
30
 * class capable of throwing exceptions if data is out of range.
 
31
 *
 
32
 * If an operation fails, subsequent operations will also fail.  The sizebuf
 
33
 * is assumed to be in an invalid state.  This COULD be changed pretty easily
 
34
 * and in normal Quake IIRC it is.  But our MO is to bail on failure, not
 
35
 * figure out what went wrong (making throws perfect).
 
36
 */
 
37
 
 
38
#include "sizebuf.h"
 
39
#include <assert.h>
 
40
 
 
41
#include "SDL_endian.h"
 
42
 
 
43
/* Construct buffer with the passed array and size */
 
44
void SZ_Init(sizebuf_t * sz, Uint8 * buf, unsigned int size)
 
45
{
 
46
        sz->data = buf;
 
47
        sz->bufferLen = size;
 
48
        sz->bufferPos = 0;
 
49
        sz->error = false;
 
50
}
 
51
/* Check error flags */
 
52
bool SZ_Error(sizebuf_t * sz)
 
53
{
 
54
        return(sz->error);
 
55
}
 
56
/* mimic memset */
 
57
void SZ_Memset(sizebuf_t * sz, int value, size_t count)
 
58
{
 
59
        /* Do bounds checking before writing */
 
60
        if (sz->error || sz->bufferPos + count > sz->bufferLen)
 
61
        {
 
62
                sz->error = true;
 
63
                return;
 
64
        }
 
65
 
 
66
        /* Memset and increment pointer */
 
67
        memset(sz->data + sz->bufferPos, value, count);
 
68
        sz->bufferPos += count;
 
69
}
 
70
/* Mimic memcpy.  Two versions, one for buffers, one for sizebuf objects.
 
71
 * Overload in C++. */
 
72
void SZ_Memcpy(sizebuf_t * sz, const Uint8 * buf, size_t count)
 
73
{
 
74
        /* State checking */
 
75
        if (sz->error || sz->bufferPos + count > sz->bufferLen)
 
76
        {
 
77
                sz->error = true;
 
78
                return;
 
79
        }
 
80
 
 
81
        /* Memcpy & increment */
 
82
        memcpy(sz->data + sz->bufferPos, buf, count);
 
83
        sz->bufferPos += count;
 
84
}
 
85
void SZ_Memcpy2(sizebuf_t * sz, sizebuf_t * bf, size_t count)
 
86
{
 
87
        /* State checking */
 
88
        if (sz->error || sz->bufferPos + count > sz->bufferLen)
 
89
        {
 
90
                sz->error = true;
 
91
                return;
 
92
        }
 
93
        if (bf->error || bf->bufferPos + count > bf->bufferLen)
 
94
        {
 
95
                bf->error = true;
 
96
                return;
 
97
        }
 
98
 
 
99
        /* Memcpy & increment */
 
100
        memcpy(sz->data + sz->bufferPos, bf->data + bf->bufferPos, count);
 
101
        sz->bufferPos += count;
 
102
        bf->bufferPos += count;
 
103
}
 
104
/* Reposition buffer pointer */
 
105
void SZ_Seek(sizebuf_t * sz, long count, int mode)
 
106
{
 
107
        /* Okay, it's reasonable to reset the error bool on seeking... */
 
108
 
 
109
        switch(mode)
 
110
        {
 
111
                case SEEK_SET:
 
112
                        sz->bufferPos = count;
 
113
                        break;
 
114
                case SEEK_CUR:
 
115
                        sz->bufferPos += count;
 
116
                        break;
 
117
                case SEEK_END:
 
118
                        sz->bufferPos = sz->bufferLen - count;
 
119
                        break;
 
120
                default:
 
121
                        assert(false);
 
122
        }
 
123
 
 
124
        /* Check errors */
 
125
        if (sz->bufferPos > sz->bufferLen)
 
126
        {
 
127
                sz->error = true;
 
128
        } else {
 
129
                sz->error = false;
 
130
        }
 
131
}
 
132
const Uint8 * SZ_GetCurBufferPtr (sizebuf_t * sz)
 
133
{
 
134
        return(sz->data);
 
135
}
 
136
 
 
137
/* The code below makes use of pointer casts, similar to what is in efread.
 
138
 * It's not the ONLY way to write ints to a stream, but it's probably the
 
139
 * cleanest of the lot.  Better to have it here than littered all over the code.
 
140
 */
 
141
void MSG_WriteByte(sizebuf_t * sz, unsigned int value)
 
142
{
 
143
        if (sz->error || sz->bufferPos + 1 > sz->bufferLen)
 
144
        {
 
145
                sz->error = true;
 
146
                return;
 
147
        }
 
148
 
 
149
        sz->data[sz->bufferPos] = value;
 
150
        sz->bufferPos++;
 
151
}
 
152
void MSG_WriteWord(sizebuf_t * sz, unsigned int value)
 
153
{
 
154
        if (sz->error || sz->bufferPos + 2 > sz->bufferLen)
 
155
        {
 
156
                sz->error = true;
 
157
                return;
 
158
        }
 
159
 
 
160
        *((Uint16 *)(sz->data + sz->bufferPos)) = SDL_SwapLE16( ((Uint16)value) );
 
161
        sz->bufferPos += 2;
 
162
}
 
163
void MSG_WriteDWord(sizebuf_t * sz, unsigned int value)
 
164
{
 
165
        if (sz->error || sz->bufferPos + 4 > sz->bufferLen)
 
166
        {
 
167
                sz->error = true;
 
168
                return;
 
169
        }
 
170
 
 
171
        *((Uint32 *)(sz->data + sz->bufferPos)) = SDL_SwapLE32( ((Uint32)value) );
 
172
        sz->bufferPos += 4;
 
173
}
 
174
 
 
175
unsigned int MSG_ReadByte(sizebuf_t * sz)
 
176
{
 
177
        unsigned int ret;
 
178
 
 
179
 
 
180
        if (sz->error || sz->bufferPos + 1 > sz->bufferLen)
 
181
        {
 
182
                sz->error = true;
 
183
                return(0);
 
184
        }
 
185
 
 
186
        ret = sz->data[sz->bufferPos];
 
187
        sz->bufferPos += 1;
 
188
 
 
189
        return(ret);
 
190
}
 
191
unsigned int MSG_ReadWord(sizebuf_t * sz)
 
192
{
 
193
        unsigned int ret;
 
194
 
 
195
 
 
196
        if (sz->error || sz->bufferPos + 2 > sz->bufferLen)
 
197
        {
 
198
                sz->error = true;
 
199
                return(0);
 
200
        }
 
201
 
 
202
        ret = SDL_SwapLE16(*((Uint16 *)(sz->data + sz->bufferPos)));
 
203
        sz->bufferPos += 2;
 
204
 
 
205
        return(ret);
 
206
}
 
207
unsigned int MSG_ReadDWord(sizebuf_t * sz)
 
208
{
 
209
        unsigned int ret;
 
210
 
 
211
 
 
212
        if (sz->error || sz->bufferPos + 4 > sz->bufferLen)
 
213
        {
 
214
                sz->error = true;
 
215
                return(0);
 
216
        }
 
217
 
 
218
        ret = SDL_SwapLE32(*((Uint32 *)(sz->data + sz->bufferPos)));
 
219
        sz->bufferPos += 4;
 
220
 
 
221
        return(ret);
 
222
}