utils.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #pragma once
  2. static const char threadTimestampFmt[] = "%Y/%m/%d %H:%M:%S %Z";
  3. static const char httpTimestampFmt[] = "%a, %d %b %Y %H:%M:%S GMT";
  4. static void *
  5. memmem_priv(const void *l, size_t l_len, const void *s, size_t s_len)
  6. {
  7. register char *cur, *last;
  8. const char *cl = (const char *)l;
  9. const char *cs = (const char *)s;
  10. /* we need something to compare */
  11. if (l_len == 0 || s_len == 0)
  12. return NULL;
  13. /* "s" must be smaller or equal to "l" */
  14. if (l_len < s_len)
  15. return NULL;
  16. /* special case where s_len == 1 */
  17. if (s_len == 1)
  18. return (void *)memchr(l, (int)*cs, l_len);
  19. /* the last position where its possible to find "s" in "l" */
  20. last = (char *)cl + l_len - s_len;
  21. for (cur = (char *)cl; cur <= last; cur++)
  22. if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
  23. return cur;
  24. return NULL;
  25. }
  26. static int decryptMail(unsigned char *decrypted, char *encrypted)
  27. {
  28. char current[5]="0x";
  29. unsigned char *ptr = decrypted;
  30. current[2] = encrypted[0];
  31. current[3] = encrypted[1];
  32. unsigned int r = strtol(current,NULL,16);
  33. int len = strlen(encrypted);
  34. int n = 2;
  35. for(;n<len;n+=2) {
  36. current[2] = encrypted[n];
  37. current[3] = encrypted[n+1];
  38. unsigned int i = strtol(current,NULL,16);
  39. *ptr++ = i^r;
  40. }
  41. *ptr = 0;
  42. //fprintf(stderr,"%s->%s\n",encrypted,decrypted);
  43. return ptr - decrypted;
  44. }
  45. static size_t header_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  46. {
  47. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  48. if(conn->status) return size*nitems;
  49. if(!strncasecmp("Connection",buffer,10)) {
  50. fprintf(conn->fpw,"Connection: Close\r\n");
  51. return size*nitems;
  52. }
  53. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  54. if(allow_chunked && strstr(buffer+19,"chunked")) {
  55. conn->chunked = true;
  56. //fprintf(stderr,"%s",buffer);
  57. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  58. return ret;
  59. }
  60. return size*nitems;
  61. }
  62. else if(conn->force5ch && !strncasecmp("Set-Cookie:",buffer,11)) {
  63. char *ptr = (char *)memmem_priv(buffer,size*nitems,"domain=",7);
  64. if(ptr) {
  65. char *end = ptr;
  66. while(*end != ';' && *end != '\r') end++;
  67. ptr = (char *)memmem_priv(ptr,end-ptr,"5ch.net",7);
  68. if(ptr) {
  69. fwrite(buffer,1,ptr-buffer,conn->fpw);
  70. fputc('2',conn->fpw);
  71. fwrite(ptr+1,1,size*nitems-(ptr+1-buffer),conn->fpw);
  72. return size*nitems;
  73. }
  74. }
  75. return fwrite(buffer,1,size*nitems,conn->fpw);
  76. }
  77. else {
  78. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  79. //fprintf(stderr,"%s",buffer);
  80. if(!memcmp(buffer,"\r\n",2)) {
  81. fflush(conn->fpw);
  82. conn->status = 1;
  83. }
  84. return ret;
  85. }
  86. }
  87. static size_t write_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  88. {
  89. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  90. if(conn->chunked) fprintf(conn->fpw,"%lx\r\n",size*nitems);
  91. size_t ret = fwrite(buffer,size,nitems,conn->fpw);
  92. if(conn->chunked) fprintf(conn->fpw,"\r\n");
  93. fflush(conn->fpw);
  94. return ret;
  95. }
  96. static size_t header_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  97. {
  98. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  99. if(!strncasecmp("Connection",buffer,10)) {
  100. data->appendBytes("Connection: Close\r\n",19);
  101. return size*nitems;
  102. }
  103. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  104. return size*nitems;
  105. }
  106. else {
  107. data->appendBytes(buffer,size*nitems);
  108. return size*nitems;
  109. }
  110. }
  111. static size_t write_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  112. {
  113. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  114. size_t downloaded = size*nitems;
  115. data->appendBytes(buffer, downloaded);
  116. return downloaded;
  117. }
  118. static size_t read_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  119. {
  120. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  121. if(size*nitems < 1) return 0;
  122. if(conn->content_length) {
  123. size_t bytesToRead = conn->content_length;
  124. if(size*nitems < conn->content_length) bytesToRead = size*nitems;
  125. size_t ret = fread(buffer,1,bytesToRead,conn->fpr);
  126. conn->content_length -= ret;
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. static void sendBasicHeaders(int respCode, const char *respMsg, FILE *fpw)
  132. {
  133. char date[256];
  134. time_t now = time(0);
  135. strftime(date, 256, httpTimestampFmt, gmtime(&now));
  136. if(0 > fprintf(fpw, "HTTP/1.1 %d %s\r\n",respCode,respMsg)) return;
  137. if(0 > fprintf(fpw, "Connection: Close\r\n")) return;
  138. if(0 > fprintf(fpw, "Server: 2ch Proxy\r\n")) return;
  139. if(0 > fprintf(fpw, "Date: %s\r\n",date)) return;
  140. fflush(fpw);
  141. }
  142. static void sendResponse(int respCode, const char *respMsg, FILE *fpw)
  143. {
  144. sendBasicHeaders(respCode, respMsg, fpw);
  145. if(0 > fprintf(fpw, "Content-Type: text/plain; charset=UTF-8\r\n")) return;
  146. if(0 > fprintf(fpw, "\r\n")) return;
  147. if(respCode >= 400) {
  148. if(0 > fprintf(fpw, "   ∧_∧   / ̄ ̄ ̄ ̄ ̄\n")) return;
  149. if(0 > fprintf(fpw, "  ( ´∀`)< %s\n",respMsg)) return;
  150. if(0 > fprintf(fpw, "  (    ) \_____\n")) return;
  151. if(0 > fprintf(fpw, "   │ │ │\n")) return;
  152. if(0 > fprintf(fpw, "  (__)_)\n")) return;
  153. }
  154. fflush(fpw);
  155. }
  156. #ifdef __WIN32__
  157. const char * strp_weekdays[] =
  158. { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
  159. const char * strp_monthnames[] =
  160. { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
  161. bool strp_atoi(const char * & s, int & result, int low, int high, int offset)
  162. {
  163. bool worked = false;
  164. char * end;
  165. unsigned long num = strtoul(s, & end, 10);
  166. if (num >= (unsigned long)low && num <= (unsigned long)high)
  167. {
  168. result = (int)(num + offset);
  169. s = end;
  170. worked = true;
  171. }
  172. return worked;
  173. }
  174. char * strptime(const char *s, const char *format, struct tm *tm)
  175. {
  176. bool working = true;
  177. while (working && *format && *s)
  178. {
  179. switch (*format)
  180. {
  181. case '%':
  182. {
  183. ++format;
  184. switch (*format)
  185. {
  186. case 'a':
  187. case 'A': // weekday name
  188. tm->tm_wday = -1;
  189. working = false;
  190. for (size_t i = 0; i < 7; ++ i)
  191. {
  192. size_t len = strlen(strp_weekdays[i]);
  193. if (!strnicmp(strp_weekdays[i], s, len))
  194. {
  195. tm->tm_wday = i;
  196. s += len;
  197. working = true;
  198. break;
  199. }
  200. else if (!strnicmp(strp_weekdays[i], s, 3))
  201. {
  202. tm->tm_wday = i;
  203. s += 3;
  204. working = true;
  205. break;
  206. }
  207. }
  208. break;
  209. case 'b':
  210. case 'B':
  211. case 'h': // month name
  212. tm->tm_mon = -1;
  213. working = false;
  214. for (size_t i = 0; i < 12; ++ i)
  215. {
  216. size_t len = strlen(strp_monthnames[i]);
  217. if (!strnicmp(strp_monthnames[i], s, len))
  218. {
  219. tm->tm_mon = i;
  220. s += len;
  221. working = true;
  222. break;
  223. }
  224. else if (!strnicmp(strp_monthnames[i], s, 3))
  225. {
  226. tm->tm_mon = i;
  227. s += 3;
  228. working = true;
  229. break;
  230. }
  231. }
  232. break;
  233. case 'd':
  234. case 'e': // day of month number
  235. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  236. break;
  237. case 'D': // %m/%d/%y
  238. {
  239. const char * s_save = s;
  240. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  241. if (working && *s == '/')
  242. {
  243. ++ s;
  244. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  245. if (working && *s == '/')
  246. {
  247. ++ s;
  248. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  249. if (working && tm->tm_year < 69)
  250. tm->tm_year += 100;
  251. }
  252. }
  253. if (!working)
  254. s = s_save;
  255. }
  256. break;
  257. case 'H': // hour
  258. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  259. break;
  260. case 'I': // hour 12-hour clock
  261. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  262. break;
  263. case 'j': // day number of year
  264. working = strp_atoi(s, tm->tm_yday, 1, 366, -1);
  265. break;
  266. case 'm': // month number
  267. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  268. break;
  269. case 'M': // minute
  270. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  271. break;
  272. case 'n': // arbitrary whitespace
  273. case 't':
  274. while (isspace((int)*s))
  275. ++s;
  276. break;
  277. case 'p': // am / pm
  278. if (!strnicmp(s, "am", 2))
  279. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  280. if (tm->tm_hour == 12) // 12 am == 00 hours
  281. tm->tm_hour = 0;
  282. }
  283. else if (!strnicmp(s, "pm", 2))
  284. {
  285. if (tm->tm_hour < 12) // 12 pm == 12 hours
  286. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  287. }
  288. else
  289. working = false;
  290. break;
  291. case 'r': // 12 hour clock %I:%M:%S %p
  292. {
  293. const char * s_save = s;
  294. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  295. if (working && *s == ':')
  296. {
  297. ++ s;
  298. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  299. if (working && *s == ':')
  300. {
  301. ++ s;
  302. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  303. if (working && isspace((int)*s))
  304. {
  305. ++ s;
  306. while (isspace((int)*s))
  307. ++s;
  308. if (!strnicmp(s, "am", 2))
  309. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  310. if (tm->tm_hour == 12) // 12 am == 00 hours
  311. tm->tm_hour = 0;
  312. }
  313. else if (!strnicmp(s, "pm", 2))
  314. {
  315. if (tm->tm_hour < 12) // 12 pm == 12 hours
  316. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  317. }
  318. else
  319. working = false;
  320. }
  321. }
  322. }
  323. if (!working)
  324. s = s_save;
  325. }
  326. break;
  327. case 'R': // %H:%M
  328. {
  329. const char * s_save = s;
  330. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  331. if (working && *s == ':')
  332. {
  333. ++ s;
  334. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  335. }
  336. if (!working)
  337. s = s_save;
  338. }
  339. break;
  340. case 'S': // seconds
  341. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  342. break;
  343. case 'T': // %H:%M:%S
  344. {
  345. const char * s_save = s;
  346. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  347. if (working && *s == ':')
  348. {
  349. ++ s;
  350. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  351. if (working && *s == ':')
  352. {
  353. ++ s;
  354. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  355. }
  356. }
  357. if (!working)
  358. s = s_save;
  359. }
  360. break;
  361. case 'w': // weekday number 0->6 sunday->saturday
  362. working = strp_atoi(s, tm->tm_wday, 0, 6, 0);
  363. break;
  364. case 'Y': // year
  365. working = strp_atoi(s, tm->tm_year, 1900, 65535, -1900);
  366. break;
  367. case 'y': // 2-digit year
  368. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  369. if (working && tm->tm_year < 69)
  370. tm->tm_year += 100;
  371. break;
  372. case '%': // escaped
  373. if (*s != '%')
  374. working = false;
  375. ++s;
  376. break;
  377. default:
  378. working = false;
  379. }
  380. }
  381. break;
  382. case ' ':
  383. case '\t':
  384. case '\r':
  385. case '\n':
  386. case '\f':
  387. case '\v':
  388. // zero or more whitespaces:
  389. while (isspace((int)*s))
  390. ++ s;
  391. break;
  392. default:
  393. // match character
  394. if (*s != *format)
  395. working = false;
  396. else
  397. ++s;
  398. break;
  399. }
  400. ++format;
  401. }
  402. return (working?(char *)s:0);
  403. }
  404. #endif