Debian Codegraph @

src/sizebuf.c

raw symbols SCIP
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 adjusted 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 #include "sizebuf.h"
38
39 #include "SDL_endian.h"
40
41 #include <assert.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 /* Construct buffer with the passed array and size */
46 void SZ_Init(sizebuf_t * sz, Uint8 * buf, unsigned int size)
47 {
48 sz->data = buf;
49 sz->bufferLen = size;
50 sz->bufferPos = 0;
51 sz->error = false;
52 }
53
54 /* Check error flags */
55 bool SZ_Error(sizebuf_t * sz)
56 {
57 return sz->error;
58 }
59
60 /* mimic memset */
61 void SZ_Memset(sizebuf_t * sz, int value, size_t count)
62 {
63 /* Do bounds checking before writing */
64 if (sz->error || sz->bufferPos + count > sz->bufferLen)
65 {
66 sz->error = true;
67 return;
68 }
69
70 /* Memset and increment pointer */
71 memset(sz->data + sz->bufferPos, value, count);
72 sz->bufferPos += count;
73 }
74
75 /* Mimic memcpy. */
76 void SZ_Memcpy2(sizebuf_t * sz, sizebuf_t * bf, size_t count)
77 {
78 /* State checking */
79 if (sz->error || sz->bufferPos + count > sz->bufferLen)
80 {
81 sz->error = true;
82 return;
83 }
84 if (bf->error || bf->bufferPos + count > bf->bufferLen)
85 {
86 bf->error = true;
87 return;
88 }
89
90 /* Memcpy & increment */
91 memcpy(sz->data + sz->bufferPos, bf->data + bf->bufferPos, count);
92 sz->bufferPos += count;
93 bf->bufferPos += count;
94 }
95
96 /* Reposition buffer pointer */
97 void SZ_Seek(sizebuf_t * sz, long count, int mode)
98 {
99 /* Okay, it's reasonable to reset the error bool on seeking... */
100
101 switch (mode)
102 {
103 case SEEK_SET:
104 sz->bufferPos = count;
105 break;
106 case SEEK_CUR:
107 sz->bufferPos += count;
108 break;
109 case SEEK_END:
110 sz->bufferPos = sz->bufferLen - count;
111 break;
112 default:
113 assert(false);
114 }
115
116 /* Check errors */
117 if (sz->bufferPos > sz->bufferLen)
118 sz->error = true;
119 else
120 sz->error = false;
121 }
122
123 /* The code below makes use of pointer casts, similar to what is in efread.
124 * It's not the ONLY way to write ints to a stream, but it's probably the
125 * cleanest of the lot. Better to have it here than littered all over the code.
126 */
127 unsigned int MSG_ReadByte(sizebuf_t * sz)
128 {
129 unsigned int ret;
130
131 if (sz->error || sz->bufferPos + 1 > sz->bufferLen)
132 {
133 sz->error = true;
134 return 0;
135 }
136
137 ret = sz->data[sz->bufferPos];
138 sz->bufferPos += 1;
139
140 return ret;
141 }
142
143 unsigned int MSG_ReadWord(sizebuf_t * sz)
144 {
145 unsigned int ret;
146
147 if (sz->error || sz->bufferPos + 2 > sz->bufferLen)
148 {
149 sz->error = true;
150 return 0;
151 }
152
153 ret = SDL_SwapLE16(*((Uint16 *)(sz->data + sz->bufferPos)));
154 sz->bufferPos += 2;
155
156 return ret;
157 }