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.83 by root, Sun Dec 28 06:59:26 2008 UTC vs.
Revision 1.90 by root, Thu Oct 15 21:09:32 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/*
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>
51 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 53 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
52 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 54 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
53 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; 55 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
54 56
55 for (int i = 11; --i; ) 57 for (int i = 11; --i; )
56 operator ()(); 58 next ();
57} 59}
58 60
59uint32_t 61uint32_t
60tausworthe_random_generator::next () 62tausworthe_random_generator::next ()
61{ 63{
65 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 67 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
66 68
67 return state [0] ^ state [1] ^ state [2] ^ state [3]; 69 return state [0] ^ state [1] ^ state [2] ^ state [3];
68} 70}
69 71
72template<class generator>
70uint32_t 73uint32_t
71tausworthe_random_generator::get_range (uint32_t num) 74random_number_generator<generator>::get_range (uint32_t num)
72{ 75{
73 return (next () * (uint64_t)num) >> 32U; 76 return (this->next () * (uint64_t)num) >> 32U;
74} 77}
75 78
76// return a number within (min .. max) 79// return a number within (min .. max)
80template<class generator>
77int 81int
78tausworthe_random_generator::get_range (int r_min, int r_max) 82random_number_generator<generator>::get_range (int r_min, int r_max)
79{ 83{
80 return r_min + get_range (max (r_max - r_min + 1, 0)); 84 return r_min + get_range (max (r_max - r_min + 1, 0));
81} 85}
86
87template struct random_number_generator<tausworthe_random_generator>;
88template struct random_number_generator<xorshift_random_generator>;
82 89
83/* 90/*
84 * The random functions here take luck into account when rolling random 91 * The random functions here take luck into account when rolling random
85 * dice or numbers. This function has less of an impact the larger the 92 * dice or numbers. This function has less of an impact the larger the
86 * difference becomes in the random numbers. IE, the effect is lessened 93 * difference becomes in the random numbers. IE, the effect is lessened
221 int j; 228 int j;
222 229
223 if (!op->materialname) 230 if (!op->materialname)
224 return; 231 return;
225 232
226 if (change->materialname && strcmp (op->materialname, change->materialname)) 233 if (op->materialname != change->materialname)
227 return; 234 return;
228 235
229 if (!op->is_armor ()) 236 if (!op->is_armor ())
230 return; 237 return;
231 238
442 strcpy (input, tmp); 449 strcpy (input, tmp);
443 450
444 return; 451 return;
445} 452}
446 453
454/******************************************************************************/
455
456/* Checks a player-provided string which will become the msg property of
457 * an object for dangerous input.
458 */
459bool
460msg_is_safe (const char *msg)
461{
462 bool safe = true;
463
464 /* Trying to cheat by getting data into the object */
465 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
466 || strstr (msg, "\nendmsg"))
467 safe = false;
468
469 /* Trying to make the object talk, and potentially access arbitrary code */
470 if (object::msg_has_dialogue (msg))
471 safe = false;
472
473 return safe;
474}
475
447///////////////////////////////////////////////////////////////////////////// 476/////////////////////////////////////////////////////////////////////////////
448 477
449void 478void
450fork_abort (const char *msg) 479fork_abort (const char *msg)
451{ 480{
554 583
555#endif 584#endif
556 585
557/******************************************************************************/ 586/******************************************************************************/
558 587
588int
559void assign (char *dst, const char *src, int maxlen) 589assign (char *dst, const char *src, int maxsize)
560{ 590{
561 if (!src) 591 if (!src)
562 src = ""; 592 src = "";
563 593
564 int len = strlen (src); 594 int len = strlen (src);
565 595
566 if (len >= maxlen - 1) 596 if (len >= maxsize)
567 { 597 {
568 if (maxlen <= 4) 598 if (maxsize <= 4)
569 { 599 {
570 memset (dst, '.', maxlen - 1); 600 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 601 dst [maxsize - 1] = 0;
572 } 602 }
573 else 603 else
574 { 604 {
575 memcpy (dst, src, maxlen - 4); 605 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 606 memcpy (dst + maxsize - 4, "...", 4);
577 } 607 }
608
609 len = maxsize;
578 } 610 }
579 else 611 else
580 memcpy (dst, src, len + 1); 612 memcpy (dst, src, ++len);
581}
582 613
583const char * 614 return len;
615}
616
617char *
618vformat (const char *format, va_list ap)
619{
620 static dynbuf_text buf; buf.clear ();
621 buf.vprintf (format, ap);
622 return buf;
623}
624
625char *
584format (const char *format, ...) 626format (const char *format, ...)
585{ 627{
586 static dynbuf_text buf;
587
588 buf.clear ();
589
590 va_list ap; 628 va_list ap;
591 va_start (ap, format); 629 va_start (ap, format);
592 buf.vprintf (format, ap); 630 char *buf = vformat (format, ap);
593 va_end (ap); 631 va_end (ap);
594 632
595 return buf; 633 return buf;
596} 634}
597 635

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines