ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.86 by root, Mon Jan 12 03:40:21 2009 UTC vs.
Revision 1.92 by root, Tue Nov 3 23:18:23 2009 UTC

3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24/* 25/*
25 * General convenience functions for crossfire. 26 * General convenience functions for deliantra.
26 */ 27 */
27 28
28#include <cstdlib> 29#include <cstdlib>
29#include <sys/types.h> 30#include <sys/types.h>
30#include <unistd.h> 31#include <unistd.h>
32#include <time.h> 33#include <time.h>
33#include <signal.h> 34#include <signal.h>
34 35
35#include <global.h> 36#include <global.h>
36#include <material.h> 37#include <material.h>
38#include <object.h>
37 39
38#include <sys/time.h> 40#include <sys/time.h>
39#include <sys/resource.h> 41#include <sys/resource.h>
40 42
41#include <glib.h> 43#include <glib.h>
408 } 410 }
409 } 411 }
410 result[resultlen] = '\0'; 412 result[resultlen] = '\0';
411} 413}
412 414
413/** 415/******************************************************************************/
414 * Taking a string as an argument, mutate it into a string that looks like a list.
415 * a 'list' for the purposes here, is a string of items, seperated by commas, except
416 * for the last entry, which has an 'and' before it, and a full stop (period) after it.
417 * This function will also strip all trailing non alphanumeric characters.
418 * It does not insert an oxford comma.
419 */
420void
421make_list_like (char *input)
422{
423 char *p, tmp[MAX_BUF];
424 int i;
425 416
426 if (!input || strlen (input) > MAX_BUF - 5) 417/* Checks a player-provided string which will become the msg property of
427 return; 418 * an object for dangerous input.
428 /* bad stuff would happen if we continued here, the -5 is to make space for ' and ' */ 419 */
420bool
421msg_is_safe (const char *msg)
422{
423 bool safe = true;
429 424
430 strncpy (tmp, input, MAX_BUF - 5); 425 /* Trying to cheat by getting data into the object */
431 /*trim all trailing commas, spaces etc. */ 426 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
432 for (i = strlen (tmp); !isalnum (tmp[i]) && i >= 0; i--) 427 || strstr (msg, "\nendmsg"))
433 tmp[i] = '\0'; 428 safe = false;
434 429
435 strcat (tmp, "."); 430 /* Trying to make the object talk, and potentially access arbitrary code */
431 if (object::msg_has_dialogue (msg))
432 safe = false;
436 433
437 p = strrchr (tmp, ',');
438 if (p)
439 {
440 *p = '\0';
441 strcpy (input, tmp);
442 p++;
443 strcat (input, " and");
444 strcat (input, p);
445 }
446 else
447 strcpy (input, tmp);
448
449 return; 434 return safe;
450} 435}
451 436
452///////////////////////////////////////////////////////////////////////////// 437/////////////////////////////////////////////////////////////////////////////
453 438
454void 439void
588 memcpy (dst, src, ++len); 573 memcpy (dst, src, ++len);
589 574
590 return len; 575 return len;
591} 576}
592 577
593const char * 578char *
579vformat (const char *format, va_list ap)
580{
581 static dynbuf_text buf; buf.clear ();
582 buf.vprintf (format, ap);
583 return buf;
584}
585
586char *
594format (const char *format, ...) 587format (const char *format, ...)
595{ 588{
596 static dynbuf_text buf;
597
598 buf.clear ();
599
600 va_list ap; 589 va_list ap;
601 va_start (ap, format); 590 va_start (ap, format);
602 buf.vprintf (format, ap); 591 char *buf = vformat (format, ap);
603 va_end (ap); 592 va_end (ap);
604 593
605 return buf; 594 return buf;
606} 595}
607 596

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines