Quake II RTX doxygen  1.0 dev
sizebuf.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
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.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "common/protocol.h"
21 #include "common/sizebuf.h"
22 
23 void SZ_TagInit(sizebuf_t *buf, void *data, size_t size, uint32_t tag)
24 {
25  memset(buf, 0, sizeof(*buf));
26  buf->data = data;
27  buf->maxsize = size;
28  buf->tag = tag;
29 }
30 
31 void SZ_Init(sizebuf_t *buf, void *data, size_t size)
32 {
33  memset(buf, 0, sizeof(*buf));
34  buf->data = data;
35  buf->maxsize = size;
36  buf->allowoverflow = qtrue;
37  buf->allowunderflow = qtrue;
38 }
39 
40 void SZ_Clear(sizebuf_t *buf)
41 {
42  buf->cursize = 0;
43  buf->readcount = 0;
44  buf->bitpos = 0;
45  buf->overflowed = qfalse;
46 }
47 
48 void *SZ_GetSpace(sizebuf_t *buf, size_t len)
49 {
50  void *data;
51 
52  if (buf->cursize > buf->maxsize) {
53  Com_Error(ERR_FATAL,
54  "%s: %#x: already overflowed",
55  __func__, buf->tag);
56  }
57 
58  if (len > buf->maxsize - buf->cursize) {
59  if (len > buf->maxsize) {
60  Com_Error(ERR_FATAL,
61  "%s: %#x: %"PRIz" is > full buffer size %"PRIz"",
62  __func__, buf->tag, len, buf->maxsize);
63  }
64 
65  if (!buf->allowoverflow) {
66  Com_Error(ERR_FATAL,
67  "%s: %#x: overflow without allowoverflow set",
68  __func__, buf->tag);
69  }
70 
71  //Com_DPrintf("%s: %#x: overflow\n", __func__, buf->tag);
72  SZ_Clear(buf);
73  buf->overflowed = qtrue;
74  }
75 
76  data = buf->data + buf->cursize;
77  buf->cursize += len;
78  buf->bitpos = buf->cursize << 3;
79  return data;
80 }
81 
82 void SZ_WriteByte(sizebuf_t *sb, int c)
83 {
84  byte *buf;
85 
86  buf = SZ_GetSpace(sb, 1);
87  buf[0] = c;
88 }
89 
90 void SZ_WriteShort(sizebuf_t *sb, int c)
91 {
92  byte *buf;
93 
94  buf = SZ_GetSpace(sb, 2);
95  buf[0] = c & 0xff;
96  buf[1] = c >> 8;
97 }
98 
99 void SZ_WriteLong(sizebuf_t *sb, int c)
100 {
101  byte *buf;
102 
103  buf = SZ_GetSpace(sb, 4);
104  buf[0] = c & 0xff;
105  buf[1] = (c >> 8) & 0xff;
106  buf[2] = (c >> 16) & 0xff;
107  buf[3] = c >> 24;
108 }
109 
110 #if USE_MVD_SERVER
111 void SZ_WriteString(sizebuf_t *sb, const char *s)
112 {
113  size_t len;
114 
115  if (!s) {
116  SZ_WriteByte(sb, 0);
117  return;
118  }
119 
120  len = strlen(s);
121  if (len >= MAX_NET_STRING) {
122  Com_WPrintf("%s: overflow: %"PRIz" chars", __func__, len);
123  SZ_WriteByte(sb, 0);
124  return;
125  }
126 
127  SZ_Write(sb, s, len + 1);
128 }
129 #endif
SZ_WriteByte
void SZ_WriteByte(sizebuf_t *sb, int c)
Definition: sizebuf.c:82
SZ_WriteShort
void SZ_WriteShort(sizebuf_t *sb, int c)
Definition: sizebuf.c:90
SZ_Init
void SZ_Init(sizebuf_t *buf, void *data, size_t size)
Definition: sizebuf.c:31
SZ_GetSpace
void * SZ_GetSpace(sizebuf_t *buf, size_t len)
Definition: sizebuf.c:48
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
SZ_TagInit
void SZ_TagInit(sizebuf_t *buf, void *data, size_t size, uint32_t tag)
Definition: sizebuf.c:23
c
statCounters_t c
Definition: main.c:30
SZ_WriteLong
void SZ_WriteLong(sizebuf_t *sb, int c)
Definition: sizebuf.c:99
SZ_Clear
void SZ_Clear(sizebuf_t *buf)
Definition: sizebuf.c:40