utils.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Igor van den Hoven 2004 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. int hex_digit(char *str)
  27. {
  28. if (isdigit((int) *str))
  29. {
  30. return *str - '0';
  31. }
  32. else
  33. {
  34. return toupper((int) *str) - 'A' + 10;
  35. }
  36. }
  37. unsigned long long hex_number_64bit(char *str)
  38. {
  39. unsigned long long len, mul, val = 0;
  40. for (len = 0 ; len < 16 ; len++)
  41. {
  42. if (!isxdigit((int) str[len]))
  43. {
  44. break;
  45. }
  46. }
  47. for (mul = 1 ; len > 0 ; mul *= 16)
  48. {
  49. val += mul * hex_digit(str + --len);
  50. }
  51. return val;
  52. }
  53. int hex_number_8bit(char *str)
  54. {
  55. int value = 0;
  56. if (str)
  57. {
  58. if (isdigit((int) *str))
  59. {
  60. value += 16 * (*str - '0');
  61. }
  62. else
  63. {
  64. value += 16 * (toupper((int) *str) - 'A' + 10);
  65. }
  66. str++;
  67. }
  68. if (str)
  69. {
  70. if (isdigit((int) *str))
  71. {
  72. value += *str - '0';
  73. }
  74. else
  75. {
  76. value += toupper((int) *str) - 'A' + 10;
  77. }
  78. str++;
  79. }
  80. return value;
  81. }
  82. int oct_number(char *str)
  83. {
  84. int value = 0;
  85. if (str)
  86. {
  87. if (isdigit((int) *str))
  88. {
  89. value += 8 * (*str - '0');
  90. }
  91. str++;
  92. }
  93. if (str)
  94. {
  95. if (isdigit((int) *str))
  96. {
  97. value += *str - '0';
  98. }
  99. str++;
  100. }
  101. return value;
  102. }
  103. int unicode_16_bit(char *str, char *out)
  104. {
  105. int val = 0;
  106. unsigned char *pto = (unsigned char *) out;
  107. if (isdigit((int) *str))
  108. {
  109. val += 4096 * (*str - '0');
  110. }
  111. else
  112. {
  113. val += 4096 * (toupper((int) *str) - 'A' + 10);
  114. }
  115. str++;
  116. if (isdigit((int) *str))
  117. {
  118. val += 256 * (*str - '0');
  119. }
  120. else
  121. {
  122. val += 256 * (toupper((int) *str) - 'A' + 10);
  123. }
  124. str++;
  125. if (isdigit((int) *str))
  126. {
  127. val += 16 * (*str - '0');
  128. }
  129. else
  130. {
  131. val += 16 * (toupper((int) *str) - 'A' + 10);
  132. }
  133. str++;
  134. if (isdigit((int) *str))
  135. {
  136. val += (*str - '0');
  137. }
  138. else
  139. {
  140. val += (toupper((int) *str) - 'A' + 10);
  141. }
  142. str++;
  143. if (val < 128)
  144. {
  145. *pto++ = val;
  146. *pto++ = 0;
  147. return 1;
  148. }
  149. else if (val < 4096)
  150. {
  151. *pto++ = 192 + val / 64;
  152. *pto++ = 128 + val % 64;
  153. *pto++ = 0;
  154. return 2;
  155. }
  156. else
  157. {
  158. *pto++ = 224 + val / 4096;
  159. *pto++ = 128 + val / 64 % 64;
  160. *pto++ = 128 + val % 64;
  161. *pto++ = 0;
  162. return 3;
  163. }
  164. }
  165. int unicode_21_bit(char *str, char *out)
  166. {
  167. int val = 0;
  168. unsigned char *pto = (unsigned char *) out;
  169. if (str)
  170. {
  171. if (isdigit((int) *str))
  172. {
  173. val += 1048576 * (*str - '0');
  174. }
  175. else
  176. {
  177. val += 1048576 * (toupper((int) *str) - 'A' + 10);
  178. }
  179. str++;
  180. }
  181. if (str)
  182. {
  183. if (isdigit((int) *str))
  184. {
  185. val += 65536 * (*str - '0');
  186. }
  187. else
  188. {
  189. val += 65536 * (toupper((int) *str) - 'A' + 10);
  190. }
  191. str++;
  192. }
  193. if (str)
  194. {
  195. if (isdigit((int) *str))
  196. {
  197. val += 4096 * (*str - '0');
  198. }
  199. else
  200. {
  201. val += 4096 * (toupper((int) *str) - 'A' + 10);
  202. }
  203. str++;
  204. }
  205. if (str)
  206. {
  207. if (isdigit((int) *str))
  208. {
  209. val += 256 * (*str - '0');
  210. }
  211. else
  212. {
  213. val += 256 * (toupper((int) *str) - 'A' + 10);
  214. }
  215. str++;
  216. }
  217. if (str)
  218. {
  219. if (isdigit((int) *str))
  220. {
  221. val += 16 * (*str - '0');
  222. }
  223. else
  224. {
  225. val += 16 * (toupper((int) *str) - 'A' + 10);
  226. }
  227. str++;
  228. }
  229. if (str)
  230. {
  231. if (isdigit((int) *str))
  232. {
  233. val += (*str - '0');
  234. }
  235. else
  236. {
  237. val += (toupper((int) *str) - 'A' + 10);
  238. }
  239. str++;
  240. }
  241. if (val < 128)
  242. {
  243. *pto++ = val;
  244. return 1;
  245. }
  246. else if (val < 4096)
  247. {
  248. *pto++ = 192 + val / 64;
  249. *pto++ = 128 + val % 64;
  250. *pto++ = 0;
  251. return 2;
  252. }
  253. else if (val < 65536)
  254. {
  255. *pto++ = 224 + val / 4096;
  256. *pto++ = 128 + val / 64 % 64;
  257. *pto++ = 128 + val % 64;
  258. *pto++ = 0;
  259. return 3;
  260. }
  261. else if (val < 1114112)
  262. {
  263. *pto++ = 240 + val / 262144;
  264. *pto++ = 128 + val / 4096 % 64;
  265. *pto++ = 128 + val / 64 % 64;
  266. *pto++ = 128 + val % 64;
  267. *pto++ = 0;
  268. return 4;
  269. }
  270. else
  271. {
  272. *pto++ = 239;
  273. *pto++ = 191;
  274. *pto++ = 189;
  275. *pto++ = 0;
  276. return 3;
  277. }
  278. }
  279. unsigned long long utime()
  280. {
  281. struct timeval now_time;
  282. gettimeofday(&now_time, NULL);
  283. if (gtd->utime >= now_time.tv_sec * 1000000ULL + now_time.tv_usec)
  284. {
  285. gtd->utime++;
  286. }
  287. else
  288. {
  289. gtd->utime = now_time.tv_sec * 1000000ULL + now_time.tv_usec;
  290. }
  291. return gtd->utime;
  292. }
  293. void seed_rand(struct session *ses, unsigned long long seed)
  294. {
  295. ses->rand = seed % 4294967291ULL;
  296. }
  297. unsigned long long generate_rand(struct session *ses)
  298. {
  299. ses->rand = ses->rand * 279470273ULL % 4294967291ULL;
  300. // return ses->rand % 1000000000ULL;
  301. return ses->rand;
  302. }
  303. /*
  304. uint32_t lcg_rand(uint32_t *state)
  305. {
  306. return *state = (uint64_t)*state * 279470273u % 0xfffffffb;
  307. }
  308. ses->rand = 6364136223846793005ULL * ses->rand + 1ULL;
  309. return ses->rand;
  310. }
  311. */
  312. char *capitalize(char *str)
  313. {
  314. static char outbuf[BUFFER_SIZE];
  315. int cnt;
  316. for (cnt = 0 ; str[cnt] != 0 ; cnt++)
  317. {
  318. outbuf[cnt] = toupper((int) str[cnt]);
  319. }
  320. outbuf[cnt] = 0;
  321. return outbuf;
  322. }
  323. char *ntos(long long number)
  324. {
  325. static char outbuf[100][NUMBER_SIZE];
  326. static int cnt;
  327. cnt = (cnt + 1) % 100;
  328. sprintf(outbuf[cnt], "%lld", number);
  329. return outbuf[cnt];
  330. }
  331. char *indent_one(int len)
  332. {
  333. static char outbuf[10][STACK_SIZE];
  334. static int cnt;
  335. cnt = (cnt + 1) % 10;
  336. memset(outbuf[cnt], ' ', UMAX(1, len));
  337. outbuf[cnt][len] = 0;
  338. return outbuf[cnt];
  339. }
  340. char *indent(int len)
  341. {
  342. static char outbuf[10][STACK_SIZE];
  343. static int cnt;
  344. cnt = (cnt + 1) % 10;
  345. memset(outbuf[cnt], ' ', UMAX(1, len * 5));
  346. outbuf[cnt][len * 5] = 0;
  347. return outbuf[cnt];
  348. }
  349. int cat_sprintf(char *dest, char *fmt, ...)
  350. {
  351. char buf[STRING_SIZE];
  352. int size;
  353. va_list args;
  354. va_start(args, fmt);
  355. size = vsprintf(buf, fmt, args);
  356. va_end(args);
  357. strcat(dest, buf);
  358. return size;
  359. }
  360. void ins_sprintf(char *dest, char *fmt, ...)
  361. {
  362. char buf[STRING_SIZE], tmp[STRING_SIZE];
  363. va_list args;
  364. va_start(args, fmt);
  365. vsprintf(buf, fmt, args);
  366. va_end(args);
  367. strcpy(tmp, dest);
  368. strcpy(dest, buf);
  369. strcat(dest, tmp);
  370. }
  371. int str_suffix(char *str1, char *str2)
  372. {
  373. int len1, len2;
  374. len1 = strlen(str1);
  375. len2 = strlen(str2);
  376. if (len1 >= len2)
  377. {
  378. if (!strcasecmp(str1 + len1 - len2, str2))
  379. {
  380. return FALSE;
  381. }
  382. }
  383. return TRUE;
  384. }
  385. void socket_printf(struct session *ses, size_t length, char *format, ...)
  386. {
  387. size_t size;
  388. char buf[STRING_SIZE];
  389. va_list args;
  390. va_start(args, format);
  391. size = vsprintf(buf, format, args);
  392. va_end(args);
  393. if (size != length && HAS_BIT(ses->telopts, TELOPT_FLAG_DEBUG))
  394. {
  395. tintin_printf(ses, "DEBUG TELNET: socket_printf size difference: %d vs %d", size, length);
  396. }
  397. if (HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  398. {
  399. write_line_mud(ses, buf, length);
  400. }
  401. }
  402. void telnet_printf(struct session *ses, int length, char *format, ...)
  403. {
  404. size_t size;
  405. char buf[STRING_SIZE];
  406. va_list args;
  407. va_start(args, format);
  408. size = vsprintf(buf, format, args);
  409. va_end(args);
  410. if (length != -1 && size != length && HAS_BIT(ses->telopts, TELOPT_FLAG_DEBUG))
  411. {
  412. tintin_printf(ses, "DEBUG TELNET: telnet_printf size difference: %d vs %d", size, length);
  413. }
  414. if (HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  415. {
  416. SET_BIT(ses->telopts, TELOPT_FLAG_TELNET);
  417. write_line_mud(ses, buf, size);
  418. DEL_BIT(ses->telopts, TELOPT_FLAG_TELNET);
  419. }
  420. }