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.78 by root, Fri May 16 17:09:38 2008 UTC vs.
Revision 1.88 by root, Sun Oct 11 05:31:54 2009 UTC

32#include <time.h> 32#include <time.h>
33#include <signal.h> 33#include <signal.h>
34 34
35#include <global.h> 35#include <global.h>
36#include <material.h> 36#include <material.h>
37#include <object.h>
38
39#include <sys/time.h>
40#include <sys/resource.h>
37 41
38#include <glib.h> 42#include <glib.h>
39 43
40refcnt_base::refcnt_t refcnt_dummy; 44refcnt_base::refcnt_t refcnt_dummy;
41ssize_t slice_alloc; 45ssize_t slice_alloc;
48 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U; 52 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
49 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U; 53 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
50 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; 54 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
51 55
52 for (int i = 11; --i; ) 56 for (int i = 11; --i; )
53 operator ()(); 57 next ();
54} 58}
55 59
56uint32_t 60uint32_t
57tausworthe_random_generator::next () 61tausworthe_random_generator::next ()
58{ 62{
62 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U); 66 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
63 67
64 return state [0] ^ state [1] ^ state [2] ^ state [3]; 68 return state [0] ^ state [1] ^ state [2] ^ state [3];
65} 69}
66 70
71template<class generator>
67uint32_t 72uint32_t
68tausworthe_random_generator::get_range (uint32_t num) 73random_number_generator<generator>::get_range (uint32_t num)
69{ 74{
70 return (next () * (uint64_t)num) >> 32U; 75 return (this->next () * (uint64_t)num) >> 32U;
71} 76}
72 77
73// return a number within (min .. max) 78// return a number within (min .. max)
79template<class generator>
74int 80int
75tausworthe_random_generator::get_range (int r_min, int r_max) 81random_number_generator<generator>::get_range (int r_min, int r_max)
76{ 82{
77 return r_min + get_range (max (r_max - r_min + 1, 0)); 83 return r_min + get_range (max (r_max - r_min + 1, 0));
78} 84}
85
86template struct random_number_generator<tausworthe_random_generator>;
87template struct random_number_generator<xorshift_random_generator>;
79 88
80/* 89/*
81 * The random functions here take luck into account when rolling random 90 * The random functions here take luck into account when rolling random
82 * dice or numbers. This function has less of an impact the larger the 91 * dice or numbers. This function has less of an impact the larger the
83 * difference becomes in the random numbers. IE, the effect is lessened 92 * difference becomes in the random numbers. IE, the effect is lessened
197} 206}
198 207
199/* convert materialname to materialtype_t */ 208/* convert materialname to materialtype_t */
200 209
201materialtype_t * 210materialtype_t *
202name_to_material (const shstr &name) 211name_to_material (const shstr_cmp name)
203{ 212{
204 for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) 213 for (materialtype_t *mt = materialt; mt; mt = mt->next)
205 if (name == mt->name) 214 if (name == mt->name)
206 return mt; 215 return mt;
207 216
208 return 0; 217 return 0;
209} 218}
215transmute_materialname (object *op, const object *change) 224transmute_materialname (object *op, const object *change)
216{ 225{
217 materialtype_t *mt; 226 materialtype_t *mt;
218 int j; 227 int j;
219 228
220 if (op->materialname == NULL) 229 if (!op->materialname)
221 return; 230 return;
222 231
223 if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) 232 if (op->materialname != change->materialname)
224 return; 233 return;
225 234
226 if (!op->is_armor ()) 235 if (!op->is_armor ())
227 return; 236 return;
228 237
248void 257void
249set_materialname (object *op, int difficulty, materialtype_t *nmt) 258set_materialname (object *op, int difficulty, materialtype_t *nmt)
250{ 259{
251 materialtype_t *mt, *lmt; 260 materialtype_t *mt, *lmt;
252 261
253 if (op->materialname != NULL) 262 if (!op->materialname)
254 return; 263 return;
255 264
256 if (nmt == NULL) 265 if (nmt)
266 lmt = nmt;
267 else
257 { 268 {
258 lmt = NULL; 269 lmt = 0;
259 270
260 for (mt = materialt; mt && mt->next; mt = mt->next) 271 for (mt = materialt; mt; mt = mt->next)
261 if (op->materials & mt->material && rndm (1, 100) <= mt->chance && 272 if (op->materials & mt->material && rndm (1, 100) <= mt->chance &&
262 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) 273 difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0))
263 { 274 {
264 lmt = mt; 275 lmt = mt;
265 if (!(op->is_weapon () || op->is_armor ())) 276 if (!(op->is_weapon () || op->is_armor ()))
266 break; 277 break;
267 } 278 }
268 } 279 }
269 else
270 lmt = nmt;
271 280
272 if (lmt != NULL) 281 if (lmt)
273 { 282 {
274 if (op->stats.dam && op->is_weapon ()) 283 if (op->stats.dam && op->is_weapon ())
275 { 284 {
276 op->stats.dam += lmt->damage; 285 op->stats.dam += lmt->damage;
277 if (op->stats.dam < 1) 286 if (op->stats.dam < 1)
439 strcpy (input, tmp); 448 strcpy (input, tmp);
440 449
441 return; 450 return;
442} 451}
443 452
453/******************************************************************************/
454
455/* Checks a player-provided string which will become the msg property of
456 * an object for dangerous input.
457 */
458bool
459msg_is_safe (const char *msg)
460{
461 bool safe = true;
462
463 /* Trying to cheat by getting data into the object */
464 if (!strncmp (msg, "endmsg", sizeof ("endmsg") - 1)
465 || strstr (msg, "\nendmsg"))
466 safe = false;
467
468 /* Trying to make the object talk, and potentially access arbitrary code */
469 if (object::msg_has_dialogue (msg))
470 safe = false;
471
472 return safe;
473}
474
444///////////////////////////////////////////////////////////////////////////// 475/////////////////////////////////////////////////////////////////////////////
445 476
446void 477void
447fork_abort (const char *msg) 478fork_abort (const char *msg)
448{ 479{
449 if (!fork ()) 480 if (!fork ())
450 { 481 {
482 signal (SIGINT , SIG_IGN);
483 signal (SIGTERM, SIG_IGN);
451 signal (SIGABRT, SIG_DFL); 484 signal (SIGABRT, SIG_IGN);
485
486 signal (SIGSEGV, SIG_DFL);
487 signal (SIGBUS , SIG_DFL);
488 signal (SIGILL , SIG_DFL);
489 signal (SIGTRAP, SIG_DFL);
490
452 // try to put corefiles into a subdirectory, if existing, to allow 491 // try to put corefiles into a subdirectory, if existing, to allow
453 // an administrator to reduce the I/O load. 492 // an administrator to reduce the I/O load.
454 chdir ("cores"); 493 chdir ("cores");
494
495 // try to detach us from as many external dependencies as possible
496 // as coredumping can take time by closing all fd's.
497 {
498 struct rlimit lim;
499
500 if (getrlimit (RLIMIT_NOFILE, &lim))
501 lim.rlim_cur = 1024;
502
503 for (int i = 0; i < lim.rlim_cur; ++i)
504 close (i);
505 }
506
507 {
508 sigset_t empty;
509 sigemptyset (&empty);
510 sigprocmask (SIG_SETMASK, &empty, 0);
511 }
512
513 // try to coredump with SIGTRAP
514 kill (getpid (), SIGTRAP);
455 abort (); 515 abort ();
456 } 516 }
457 517
458 LOG (llevError, "fork abort: %s\n", msg); 518 LOG (llevError, "fork abort: %s\n", msg);
459} 519}
522 582
523#endif 583#endif
524 584
525/******************************************************************************/ 585/******************************************************************************/
526 586
587int
527void assign (char *dst, const char *src, int maxlen) 588assign (char *dst, const char *src, int maxsize)
528{ 589{
529 if (!src) 590 if (!src)
530 src = ""; 591 src = "";
531 592
532 int len = strlen (src); 593 int len = strlen (src);
533 594
534 if (len >= maxlen - 1) 595 if (len >= maxsize)
535 { 596 {
536 if (maxlen <= 4) 597 if (maxsize <= 4)
537 { 598 {
538 memset (dst, '.', maxlen - 1); 599 memset (dst, '.', maxsize - 2);
539 dst [maxlen - 1] = 0; 600 dst [maxsize - 1] = 0;
540 } 601 }
541 else 602 else
542 { 603 {
543 memcpy (dst, src, maxlen - 4); 604 memcpy (dst, src, maxsize - 4);
544 memcpy (dst + maxlen - 4, "...", 4); 605 memcpy (dst + maxsize - 4, "...", 4);
545 } 606 }
607
608 len = maxsize;
546 } 609 }
547 else 610 else
548 memcpy (dst, src, len + 1); 611 memcpy (dst, src, ++len);
612
613 return len;
549} 614}
550 615
551const char * 616const char *
552format (const char *format, ...) 617format (const char *format, ...)
553{ 618{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines