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"cxx . . $ `<file>/src/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_Initcxx . . $ SZ_Init(548a69dbf7140519).(sizebuf_tcxx . . $ sizebuf_t# * sz, Uint8cxx . . $ Uint8# * buf, unsigned int size)
47 {
48 sz->datacxx . . $ sizebuf_s#data. = buf;
49 sz->bufferLencxx . . $ sizebuf_s#bufferLen. = size;
50 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. = 0;
51 sz->errorcxx . . $ sizebuf_s#error. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
52 }
53
54 /* Check error flags */
55 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! SZ_Errorcxx . . $ SZ_Error(afbd6f368aaad226).(sizebuf_tcxx . . $ sizebuf_t# * sz)
56 {
57 return sz->errorcxx . . $ sizebuf_s#error.;
58 }
59
60 /* mimic memset */
61 void SZ_Memsetcxx . . $ SZ_Memset(8ebbc4f4e9fcf4ca).(sizebuf_tcxx . . $ sizebuf_t# * sz, int value, size_t count)
62 {
63 /* Do bounds checking before writing */
64 if (sz->errorcxx . . $ sizebuf_s#error. || sz->bufferPoscxx . . $ sizebuf_s#bufferPos. + count > sz->bufferLencxx . . $ sizebuf_s#bufferLen.)
65 {
66 sz->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
67 return;
68 }
69
70 /* Memset and increment pointer */
71 memsetcxx . . $ memset(d1de1e4fb215f1c6).(sz->datacxx . . $ sizebuf_s#data. + sz->bufferPoscxx . . $ sizebuf_s#bufferPos., value, count);
72 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. += count;
73 }
74
75 /* Mimic memcpy. */
76 void SZ_Memcpy2cxx . . $ SZ_Memcpy2(b3bf8a9a77d50383).(sizebuf_tcxx . . $ sizebuf_t# * sz, sizebuf_tcxx . . $ sizebuf_t# * bf, size_t count)
77 {
78 /* State checking */
79 if (sz->errorcxx . . $ sizebuf_s#error. || sz->bufferPoscxx . . $ sizebuf_s#bufferPos. + count > sz->bufferLencxx . . $ sizebuf_s#bufferLen.)
80 {
81 sz->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
82 return;
83 }
84 if (bf->errorcxx . . $ sizebuf_s#error. || bf->bufferPoscxx . . $ sizebuf_s#bufferPos. + count > bf->bufferLencxx . . $ sizebuf_s#bufferLen.)
85 {
86 bf->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
87 return;
88 }
89
90 /* Memcpy & increment */
91 memcpycxx . . $ memcpy(cc817cb175f282d).(sz->datacxx . . $ sizebuf_s#data. + sz->bufferPoscxx . . $ sizebuf_s#bufferPos., bf->datacxx . . $ sizebuf_s#data. + bf->bufferPoscxx . . $ sizebuf_s#bufferPos., count);
92 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. += count;
93 bf->bufferPoscxx . . $ sizebuf_s#bufferPos. += count;
94 }
95
96 /* Reposition buffer pointer */
97 void SZ_Seekcxx . . $ SZ_Seek(e703b2df531be2a0).(sizebuf_tcxx . . $ 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_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!:
104 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. = count;
105 break;
106 case SEEK_CURcxx . . $ `<external>/232824719582829767/stdio.h:111:9`!:
107 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. += count;
108 break;
109 case SEEK_ENDcxx . . $ `<external>/232824719582829767/stdio.h:112:9`!:
110 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. = sz->bufferLencxx . . $ sizebuf_s#bufferLen. - count;
111 break;
112 default:
113 assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!(false);
114 }
115
116 /* Check errors */
117 if (sz->bufferPoscxx . . $ sizebuf_s#bufferPos. > sz->bufferLencxx . . $ sizebuf_s#bufferLen.)
118 sz->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
119 else
120 sz->errorcxx . . $ sizebuf_s#error. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
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_ReadBytecxx . . $ MSG_ReadByte(67ffb87ed4221dd).(sizebuf_tcxx . . $ sizebuf_t# * sz)
128 {
129 unsigned int ret;
130
131 if (sz->errorcxx . . $ sizebuf_s#error. || sz->bufferPoscxx . . $ sizebuf_s#bufferPos. + 1 > sz->bufferLencxx . . $ sizebuf_s#bufferLen.)
132 {
133 sz->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
134 return 0;
135 }
136
137 ret = sz->datacxx . . $ sizebuf_s#data.[sz->bufferPoscxx . . $ sizebuf_s#bufferPos.];
138 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. += 1;
139
140 return ret;
141 }
142
143 unsigned int MSG_ReadWordcxx . . $ MSG_ReadWord(67ffb87ed4221dd).(sizebuf_tcxx . . $ sizebuf_t# * sz)
144 {
145 unsigned int ret;
146
147 if (sz->errorcxx . . $ sizebuf_s#error. || sz->bufferPoscxx . . $ sizebuf_s#bufferPos. + 2 > sz->bufferLencxx . . $ sizebuf_s#bufferLen.)
148 {
149 sz->errorcxx . . $ sizebuf_s#error. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
150 return 0;
151 }
152
153 ret = SDL_SwapLE16cxx . . $ `<external>/7875807700584907893/SDL_endian.h:368:9`!(*((Uint16 *)(sz->data + sz->bufferPos)));
154 sz->bufferPoscxx . . $ sizebuf_s#bufferPos. += 2;
155
156 return ret;
157 }