You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "bomber.h"
  2. #include "utils.h"
  3. #include "gfx.h"
  4. #include <arpa/inet.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. int volatile hc=0;
  10. char volatile interrupted=0;
  11. static Uint32 cur_unique;
  12. Uint32 network_unique;
  13. Uint32 gtime(void) {
  14. return SDL_GetTicks();
  15. }
  16. Uint32 longtime(void) {
  17. return gtime()/1000;
  18. }
  19. /* surf random generator: x (Daniel J. Bernstein) */
  20. #define ROT(x, b) (((x) << (b)) | ((x) >> (32-(b))))
  21. #define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROT(x,b))
  22. static void surf(Uint32 out[8], const Uint32 in[12], const Uint32 seed[32]) {
  23. Uint32 t[12], x, sum = 0;
  24. int r, i, loop;
  25. for (i = 0; i < 12; ++i) t[i] = in[i] ^ seed[12 + i];
  26. for (i = 0; i < 8; ++i) out[i] = seed[24 + i];
  27. x = t[11];
  28. for (loop = 0; loop < 2; ++loop) {
  29. for (r = 0; r < 16; ++r) {
  30. sum += 0x9e3779b9;
  31. MUSH(0, 5); MUSH(1, 7); MUSH(2, 9); MUSH(3, 13);
  32. MUSH(4, 5); MUSH(5, 7); MUSH(6, 9); MUSH(7, 13);
  33. MUSH(8, 5); MUSH(9, 7); MUSH(10, 9); MUSH(11, 13);
  34. }
  35. for (i = 0; i < 8; ++i) out[i] ^= t[i+4];
  36. }
  37. }
  38. #undef ROT
  39. #undef MUSH
  40. static Uint32 surf_seed[32];
  41. static Uint32 surf_in[12], surf_out[8];
  42. static int surf_left;
  43. static Uint32 surf_init(void) {
  44. Uint32 unique = 0;
  45. int fd;
  46. memset(surf_in, 0, sizeof(surf_in));
  47. memset(surf_out, 0, sizeof(surf_out));
  48. memset(surf_seed, 0, sizeof(surf_seed));
  49. surf_left = 0;
  50. fd = open("/dev/urandom", O_RDONLY);
  51. if (-1 == fd) {
  52. Uint32 genseed[4][8];
  53. surf_seed[0] = surf_seed[1] = gtime();
  54. surf_in[0]++; surf(genseed[0], surf_in, surf_seed);
  55. surf_in[0]++; surf(genseed[1], surf_in, surf_seed);
  56. surf_in[0]++; surf(genseed[2], surf_in, surf_seed);
  57. surf_in[0]++; surf(genseed[3], surf_in, surf_seed);
  58. memcpy(surf_seed, genseed[0], 32);
  59. memcpy(surf_seed, genseed[1], 32);
  60. memcpy(surf_seed, genseed[2], 32);
  61. memcpy(surf_seed, genseed[3], 32);
  62. surf_in[0] = gtime();
  63. surf(genseed[0], surf_in, surf_seed);
  64. surf_in[0] = 0;
  65. unique = genseed[0][0];
  66. } else {
  67. read(fd, &unique, sizeof(unique));
  68. read(fd, &surf_seed, sizeof(surf_seed));
  69. close(fd);
  70. }
  71. return unique;
  72. }
  73. static Uint32 surf_random(void) {
  74. if (surf_left == 0) {
  75. int i;
  76. for (i = 0; (i < 12) && !(++surf_in[i]); i++) ;
  77. surf_left = 8;
  78. surf(surf_out, surf_in, surf_seed);
  79. }
  80. return surf_out[--surf_left];
  81. }
  82. void read_seed_unique(unsigned char *buf) {
  83. int i;
  84. memset(surf_in, 0, sizeof(surf_in));
  85. memset(surf_out, 0, sizeof(surf_out));
  86. surf_left = 0;
  87. memcpy(&surf_seed, buf, sizeof(surf_seed));
  88. memcpy(&network_unique, buf+sizeof(surf_seed), sizeof(network_unique));
  89. cur_unique = ntohl(network_unique);
  90. for (i = 0; i < 32; i++) surf_seed[i] = ntohl(surf_seed[i]);
  91. }
  92. unsigned char* write_seed_unique(unsigned char *buf) {
  93. int i;
  94. for (i = 0; i < 32; i++) {
  95. Uint32 l = htonl(surf_seed[i]);
  96. memcpy(buf, &l, sizeof(l));
  97. buf += sizeof(l);
  98. }
  99. memcpy(buf, &network_unique, sizeof(network_unique));
  100. buf += sizeof(network_unique);
  101. return buf;
  102. }
  103. void create_seed_unique(void) {
  104. cur_unique = surf_init();
  105. network_unique = htonl(cur_unique);
  106. }
  107. int myrand(void) {
  108. return surf_random() & 0xffffu;
  109. }
  110. #if 0
  111. /* random generator */
  112. #define TAP1 250
  113. #define TAP2 103
  114. /*
  115. #define TAP1 55
  116. #define TAP2 31
  117. */
  118. static unsigned char myrandblock[TAP1];
  119. static int myrandtake;
  120. static int myrand1(void) {
  121. int i;
  122. int val;
  123. i=myrandtake-TAP2;
  124. if(i<0) i+=TAP1;
  125. val=myrandblock[myrandtake++]^=myrandblock[i];
  126. if(myrandtake==TAP1) myrandtake=0;
  127. return val;
  128. }
  129. int myrand(void) {
  130. int v;
  131. v=myrand1();
  132. return (v<<8) | myrand1();
  133. }
  134. static void initmyrand(Uint32 unique) {
  135. int i,j;
  136. unsigned char *p;
  137. int msb,msk;
  138. myrandtake=0;
  139. p=myrandblock;
  140. j=12345 ^ unique;
  141. i=TAP1;
  142. while(i--) {
  143. j=(j*1277)&0xffff;
  144. *p++=j>>8;
  145. }
  146. p=myrandblock+14;
  147. msk=0xff;
  148. msb=0x80;
  149. do {
  150. *p&=msk;
  151. *p|=msb;
  152. p+=11;
  153. msk>>=1;
  154. msb>>=1;
  155. } while(msk);
  156. i=500;
  157. while(i--) myrand();
  158. }
  159. void create_unique(void) {
  160. set_unique(gtime());
  161. }
  162. void set_unique(Uint32 unique) {
  163. cur_unique = unique;
  164. network_unique = htonl(cur_unique);
  165. initmyrand(cur_unique);
  166. }
  167. Uint32 get_unique(void) {
  168. return cur_unique;
  169. }
  170. #endif
  171. void nomem(char *str) {
  172. printf("No memory!!![%s]\n",str);
  173. exit(1);
  174. }
  175. int mypause(void) {
  176. while(!interrupted) {
  177. pollinput();
  178. SDL_Delay(1);
  179. }
  180. interrupted=0;
  181. return 1;
  182. }
  183. static Uint32 sdlhandler(Uint32 time) {
  184. #if defined (SDL_LATENCY)
  185. outmsgs();
  186. #endif
  187. interrupted=1;
  188. hc++;
  189. return time;
  190. }
  191. void pulseon(void) {
  192. SDL_SetTimer(40,sdlhandler);
  193. }
  194. void hexdump(unsigned char *p, int len) {
  195. int i;
  196. for (i = 0; i < len; i++) {
  197. if (15 == len % 16)
  198. fprintf(stderr, "0x%X\n", p[i]);
  199. else
  200. fprintf(stderr, "0x%X ", p[i]);
  201. }
  202. if (i % 16)
  203. fprintf(stderr, "\n");
  204. }