2 Copyright (C) 1996-1997 Id Software, Inc.
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.
13 See the GNU General Public License for more details.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 // many buffers use this size
25 #define MAX_INPUTLINE 16384
28 // MSVC has a different name for several standard functions
30 # define strcasecmp _stricmp
31 # define strncasecmp _strnicmp
34 // Create our own define for Mac OS X
35 #if defined(__APPLE__) && defined(__MACH__)
40 #include <sys/file.h> // Needed for FNDELAY
43 //============================================================================
45 typedef struct sizebuf_s
47 qboolean allowoverflow; // if false, do a Sys_Error
48 qboolean overflowed; // set to true if the buffer size failed
54 void SZ_Clear (sizebuf_t *buf);
55 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
56 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
57 void SZ_HexDumpToConsole(const sizebuf_t *buf);
59 void Com_HexDumpToConsole(const unsigned char *data, int size);
61 unsigned short CRC_Block(const unsigned char *data, size_t size);
62 unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); // for hash lookup functions that use strcasecmp for comparison
64 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
66 // these are actually md4sum (mdfour.c)
67 unsigned Com_BlockChecksum (void *buffer, int length);
68 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
71 //============================================================================
73 //============================================================================
75 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
78 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
82 # define LITTLE_ENDIAN 1234
83 # define BIG_ENDIAN 4321
86 // If we still don't know the CPU endianess at this point, we try to guess
87 // normally including sys/types.h includes endian.h for the platform, which defines BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN, however endian.h is a BSD-ism, and may not be present on all platforms (particularly windows)
89 # if defined(WIN32) || defined (__i386) || defined(__amd64)
90 # define BYTE_ORDER LITTLE_ENDIAN
93 # if defined(__i386) || defined(__amd64)
94 # define BYTE_ORDER LITTLE_ENDIAN
96 # define BYTE_ORDER BIG_ENDIAN
99 # warning "Unable to determine the CPU endianess. Defaulting to little endian"
100 # define BYTE_ORDER LITTLE_ENDIAN
106 short ShortSwap (short l);
107 int LongSwap (int l);
108 float FloatSwap (float f);
110 #if BYTE_ORDER == LITTLE_ENDIAN
112 #define BigShort(l) ShortSwap(l)
113 #define LittleShort(l) (l)
114 #define BigLong(l) LongSwap(l)
115 #define LittleLong(l) (l)
116 #define BigFloat(l) FloatSwap(l)
117 #define LittleFloat(l) (l)
120 #define BigShort(l) (l)
121 #define LittleShort(l) ShortSwap(l)
122 #define BigLong(l) (l)
123 #define LittleLong(l) LongSwap(l)
124 #define BigFloat(l) (l)
125 #define LittleFloat(l) FloatSwap(l)
128 unsigned int BuffBigLong (const unsigned char *buffer);
129 unsigned short BuffBigShort (const unsigned char *buffer);
130 unsigned int BuffLittleLong (const unsigned char *buffer);
131 unsigned short BuffLittleShort (const unsigned char *buffer);
134 //============================================================================
136 // these versions are purely for internal use, never sent in network protocol
137 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
138 typedef enum protocolversion_e
141 PROTOCOL_DARKPLACES7, // added QuakeWorld-style movement protocol to allow more consistent prediction
142 PROTOCOL_DARKPLACES6, // various changes
143 PROTOCOL_DARKPLACES5, // uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
144 PROTOCOL_DARKPLACES4, // various changes
145 PROTOCOL_DARKPLACES3, // uses EntityFrame4 entity snapshot encoder/decoder which is broken, this attempted to do partial snapshot updates on a QuakeWorld-like protocol, but it is broken and impossible to fix
146 PROTOCOL_DARKPLACES2, // various changes
147 PROTOCOL_DARKPLACES1, // uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
148 PROTOCOL_QUAKEDP, // darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
149 PROTOCOL_NEHAHRAMOVIE, // Nehahra movie protocol, a big nasty hack dating back to early days of the Quake Standards Group (but only ever used by neh_gl.exe), this is potentially backwards compatible with quake protocol as long as no extended features are used (but in actuality the neh_gl.exe which wrote this protocol ALWAYS wrote the extended information)
150 PROTOCOL_QUAKE, // quake (aka netquake/normalquake/nq) protocol
151 PROTOCOL_QUAKEWORLD, // quakeworld protocol
152 PROTOCOL_NEHAHRABJP, // same as QUAKEDP but with 16bit modelindex
153 PROTOCOL_NEHAHRABJP2, // same as NEHAHRABJP but with 16bit soundindex
154 PROTOCOL_NEHAHRABJP3, // same as NEHAHRABJP2 but with some changes
158 void MSG_WriteChar (sizebuf_t *sb, int c);
159 void MSG_WriteByte (sizebuf_t *sb, int c);
160 void MSG_WriteShort (sizebuf_t *sb, int c);
161 void MSG_WriteLong (sizebuf_t *sb, int c);
162 void MSG_WriteFloat (sizebuf_t *sb, float f);
163 void MSG_WriteString (sizebuf_t *sb, const char *s);
164 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
165 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
166 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
167 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
168 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
169 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
170 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
171 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
172 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
173 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
175 extern int msg_readcount;
176 extern qboolean msg_badread; // set if a read goes beyond end of message
178 void MSG_BeginReading (void);
179 int MSG_ReadLittleShort (void);
180 int MSG_ReadBigShort (void);
181 int MSG_ReadLittleLong (void);
182 int MSG_ReadBigLong (void);
183 float MSG_ReadLittleFloat (void);
184 float MSG_ReadBigFloat (void);
185 char *MSG_ReadString (void);
186 int MSG_ReadBytes (int numbytes, unsigned char *out);
188 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
189 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
190 #define MSG_ReadShort MSG_ReadLittleShort
191 #define MSG_ReadLong MSG_ReadLittleLong
192 #define MSG_ReadFloat MSG_ReadLittleFloat
194 float MSG_ReadAngle8i (void);
195 float MSG_ReadAngle16i (void);
196 float MSG_ReadAngle32f (void);
197 float MSG_ReadCoord13i (void);
198 float MSG_ReadCoord16i (void);
199 float MSG_ReadCoord32f (void);
200 float MSG_ReadCoord (protocolversion_t protocol);
201 void MSG_ReadVector (float *v, protocolversion_t protocol);
202 float MSG_ReadAngle (protocolversion_t protocol);
204 //============================================================================
206 typedef float (*COM_WordWidthFunc_t) (void *passthrough, const char *w, size_t *length, float maxWidth); // length is updated to the longest fitting string into maxWidth; if maxWidth < 0, all characters are used and length is used as is
207 typedef int (*COM_LineProcessorFunc) (void *passthrough, const char *line, size_t length, float width, qboolean isContination);
208 int COM_Wordwrap(const char *string, size_t length, float continuationSize, float maxWidth, COM_WordWidthFunc_t wordWidth, void *passthroughCW, COM_LineProcessorFunc processLine, void *passthroughPL);
210 extern char com_token[MAX_INPUTLINE];
212 int COM_ParseToken_Simple(const char **datapointer, qboolean returnnewline, qboolean parsebackslash);
213 int COM_ParseToken_QuakeC(const char **datapointer, qboolean returnnewline);
214 int COM_ParseToken_VM_Tokenize(const char **datapointer, qboolean returnnewline);
215 int COM_ParseToken_Console(const char **datapointer);
218 extern const char **com_argv;
220 int COM_CheckParm (const char *parm);
221 void COM_Init (void);
222 void COM_Shutdown (void);
223 void COM_InitGameType (void);
225 char *va(const char *format, ...) DP_FUNC_PRINTF(1);
226 // does a varargs printf into a temp buffer
229 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
230 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
231 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
233 // dpsnprintf and dpvsnprintf
234 // return the number of printed characters, excluding the final '\0'
235 // or return -1 if the buffer isn't big enough to contain the entire string.
236 // buffer is ALWAYS null-terminated
237 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...) DP_FUNC_PRINTF(3);
238 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
240 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
241 // LordHavoc: added #undef lines here to avoid warnings in Linux
243 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
245 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
247 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
249 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
251 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
254 //============================================================================
256 extern struct cvar_s registered;
257 extern struct cvar_s cmdline;
259 typedef enum gamemode_e
273 GAME_TENEBRAE, // full of evil hackery
275 GAME_OPENQUARTZ, //this game sucks
279 GAME_DEFEATINDETAIL2,
281 GAME_CONTAGIONTHEORY,
289 extern gamemode_t gamemode;
290 extern const char *gamename;
291 extern const char *gamedirname1;
292 extern const char *gamedirname2;
293 extern const char *gamescreenshotname;
294 extern const char *gameuserdirname;
295 extern char com_modname[MAX_OSPATH];
297 void COM_ToLowerString (const char *in, char *out, size_t size_out);
298 void COM_ToUpperString (const char *in, char *out, size_t size_out);
299 int COM_StringBeginsWith(const char *s, const char *match);
301 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
303 size_t COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
304 qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
305 void COM_ToLowerString (const char *in, char *out, size_t size_out);
306 void COM_ToUpperString (const char *in, char *out, size_t size_out);
308 typedef struct stringlist_s
310 // maxstrings changes as needed, causing reallocation of strings[] array
316 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
317 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one);
318 void stringlistinit(stringlist_t *list);
319 void stringlistfreecontents(stringlist_t *list);
320 void stringlistappend(stringlist_t *list, const char *text);
321 void stringlistsort(stringlist_t *list);
322 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
324 char *SearchInfostring(const char *infostring, const char *key);
326 void InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
327 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
328 void InfoString_Print(char *buffer);
330 // strlcat and strlcpy, from OpenBSD
331 // Most (all?) BSDs already have them
332 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
333 # define HAVE_STRLCAT 1
334 # define HAVE_STRLCPY 1
339 * Appends src to string dst of size siz (unlike strncat, siz is the
340 * full size of dst, not space left). At most siz-1 characters
341 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
342 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
343 * If retval >= siz, truncation occurred.
345 size_t strlcat(char *dst, const char *src, size_t siz);
346 #endif // #ifndef HAVE_STRLCAT
350 * Copy src to string dst of size siz. At most siz-1 characters
351 * will be copied. Always NUL terminates (unless siz == 0).
352 * Returns strlen(src); if retval >= siz, truncation occurred.
354 size_t strlcpy(char *dst, const char *src, size_t siz);
356 #endif // #ifndef HAVE_STRLCPY
358 void FindFraction(double val, int *num, int *denom, int denomMax);