… | |
… | |
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 | |
40 | refcnt_base::refcnt_t refcnt_dummy; |
44 | refcnt_base::refcnt_t refcnt_dummy; |
41 | ssize_t slice_alloc; |
45 | ssize_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 | |
56 | uint32_t |
60 | uint32_t |
57 | tausworthe_random_generator::next () |
61 | tausworthe_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 | |
|
|
71 | template<class generator> |
67 | uint32_t |
72 | uint32_t |
68 | tausworthe_random_generator::get_range (uint32_t num) |
73 | random_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) |
|
|
79 | template<class generator> |
74 | int |
80 | int |
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
81 | random_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 | |
|
|
86 | template struct random_number_generator<tausworthe_random_generator>; |
|
|
87 | template 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 | |
201 | materialtype_t * |
210 | materialtype_t * |
202 | name_to_material (const shstr &name) |
211 | name_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 | } |
… | |
… | |
215 | transmute_materialname (object *op, const object *change) |
224 | transmute_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 | |
… | |
… | |
248 | void |
257 | void |
249 | set_materialname (object *op, int difficulty, materialtype_t *nmt) |
258 | set_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 | */ |
|
|
458 | bool |
|
|
459 | msg_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 | |
446 | void |
477 | void |
447 | fork_abort (const char *msg) |
478 | fork_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 | |
|
|
587 | int |
527 | void assign (char *dst, const char *src, int maxlen) |
588 | assign (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 | |
551 | const char * |
616 | const char * |
552 | format (const char *format, ...) |
617 | format (const char *format, ...) |
553 | { |
618 | { |