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.87 by sf-marcmagus, Sun Oct 11 01:35:53 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>
37 38
38#include <sys/time.h> 39#include <sys/time.h>
39#include <sys/resource.h> 40#include <sys/resource.h>
40 41
41#include <glib.h> 42#include <glib.h>
51 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;
52 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;
53 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;
54 55
55 for (int i = 11; --i; ) 56 for (int i = 11; --i; )
56 operator ()(); 57 next ();
57} 58}
58 59
59uint32_t 60uint32_t
60tausworthe_random_generator::next () 61tausworthe_random_generator::next ()
61{ 62{
65 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);
66 67
67 return state [0] ^ state [1] ^ state [2] ^ state [3]; 68 return state [0] ^ state [1] ^ state [2] ^ state [3];
68} 69}
69 70
71template<class generator>
70uint32_t 72uint32_t
71tausworthe_random_generator::get_range (uint32_t num) 73random_number_generator<generator>::get_range (uint32_t num)
72{ 74{
73 return (next () * (uint64_t)num) >> 32U; 75 return (this->next () * (uint64_t)num) >> 32U;
74} 76}
75 77
76// return a number within (min .. max) 78// return a number within (min .. max)
79template<class generator>
77int 80int
78tausworthe_random_generator::get_range (int r_min, int r_max) 81random_number_generator<generator>::get_range (int r_min, int r_max)
79{ 82{
80 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));
81} 84}
85
86template struct random_number_generator<tausworthe_random_generator>;
87template struct random_number_generator<xorshift_random_generator>;
82 88
83/* 89/*
84 * The random functions here take luck into account when rolling random 90 * 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 91 * 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 92 * difference becomes in the random numbers. IE, the effect is lessened
221 int j; 227 int j;
222 228
223 if (!op->materialname) 229 if (!op->materialname)
224 return; 230 return;
225 231
226 if (change->materialname && strcmp (op->materialname, change->materialname)) 232 if (op->materialname != change->materialname)
227 return; 233 return;
228 234
229 if (!op->is_armor ()) 235 if (!op->is_armor ())
230 return; 236 return;
231 237
442 strcpy (input, tmp); 448 strcpy (input, tmp);
443 449
444 return; 450 return;
445} 451}
446 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", strlen ("endmsg")) || strstr (msg, "\nendmsg"))
465 safe = false;
466
467 /* Trying to make the object talk, and potentially access arbitrary code */
468 if (object::msg_has_dialogue (msg))
469 safe = false;
470
471 return safe;
472}
473
447///////////////////////////////////////////////////////////////////////////// 474/////////////////////////////////////////////////////////////////////////////
448 475
449void 476void
450fork_abort (const char *msg) 477fork_abort (const char *msg)
451{ 478{
554 581
555#endif 582#endif
556 583
557/******************************************************************************/ 584/******************************************************************************/
558 585
586int
559void assign (char *dst, const char *src, int maxlen) 587assign (char *dst, const char *src, int maxsize)
560{ 588{
561 if (!src) 589 if (!src)
562 src = ""; 590 src = "";
563 591
564 int len = strlen (src); 592 int len = strlen (src);
565 593
566 if (len >= maxlen - 1) 594 if (len >= maxsize)
567 { 595 {
568 if (maxlen <= 4) 596 if (maxsize <= 4)
569 { 597 {
570 memset (dst, '.', maxlen - 1); 598 memset (dst, '.', maxsize - 2);
571 dst [maxlen - 1] = 0; 599 dst [maxsize - 1] = 0;
572 } 600 }
573 else 601 else
574 { 602 {
575 memcpy (dst, src, maxlen - 4); 603 memcpy (dst, src, maxsize - 4);
576 memcpy (dst + maxlen - 4, "...", 4); 604 memcpy (dst + maxsize - 4, "...", 4);
577 } 605 }
606
607 len = maxsize;
578 } 608 }
579 else 609 else
580 memcpy (dst, src, len + 1); 610 memcpy (dst, src, ++len);
611
612 return len;
581} 613}
582 614
583const char * 615const char *
584format (const char *format, ...) 616format (const char *format, ...)
585{ 617{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines