utils.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 > 0) return size*nitems;
  49. else if(conn->status == -1) {
  50. if(!memcmp(buffer,"\r\n",2)) {
  51. conn->status = 0;
  52. }
  53. return size*nitems;
  54. }
  55. if(!strncasecmp("HTTP/", buffer, 5) && !strchr(buffer, ':')) {
  56. const char *ptr = buffer + 5;
  57. const char *end = buffer + size*nitems;
  58. while(*ptr != ' ' && ptr < end) ptr++;
  59. while(*ptr == ' ' && ptr < end) ptr++;
  60. if(ptr < end) {
  61. int code = atoi(ptr);
  62. if (code == 100) {
  63. conn->status = -1;
  64. return size*nitems;
  65. }
  66. }
  67. }
  68. if(!strncasecmp("Connection",buffer,10)) {
  69. conn->socketToClient->writeString("Connection: Close\r\n");
  70. return size*nitems;
  71. }
  72. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  73. if(allow_chunked && strstr(buffer+19,"chunked")) {
  74. conn->chunked = true;
  75. //fprintf(stderr,"%s",buffer);
  76. size_t ret = conn->socketToClient->write(buffer, size*nitems);
  77. return ret;
  78. }
  79. return size*nitems;
  80. }
  81. else if(conn->force5ch && !strncasecmp("Set-Cookie:",buffer,11)) {
  82. char *ptr = (char *)memmem_priv(buffer,size*nitems,"domain=",7);
  83. if(ptr) {
  84. char *end = ptr;
  85. while(*end != ';' && *end != '\r') end++;
  86. ptr = (char *)memmem_priv(ptr,end-ptr,"5ch.net",7);
  87. if(ptr) {
  88. conn->socketToClient->write(buffer, ptr-buffer);
  89. conn->socketToClient->write("2", 1);
  90. conn->socketToClient->write(ptr+1, size*nitems-(ptr+1-buffer));
  91. return size*nitems;
  92. }
  93. }
  94. return conn->socketToClient->write(buffer, size*nitems);
  95. }
  96. else {
  97. if (conn->bbscgi) {
  98. if (!strncasecmp("X-Chx-Error:", buffer, 12)) {
  99. const char *ptr = buffer + 12;
  100. while (*ptr == ' ') ptr++;
  101. log_printf(0, "Posting to bbs.cgi has been cancelled. Reason: %s", ptr);
  102. if (*ptr == 'E') {
  103. int code = atoi(ptr+1);
  104. if ((code >= 3310 && code <= 3311) || /* inconsistent with User-Agent or something? */
  105. (code >= 3320 && code <= 3324) || /* key expiration? */
  106. (code >= 3390 && code <= 3392)) /* 3390: BAN, 3391-3392: key expiration? */
  107. conn->setMonaKey("", code);
  108. }
  109. }
  110. else if (!strncasecmp("X-MonaKey:", buffer, 10)) {
  111. const char *ptr = buffer + 10;
  112. while (*ptr == ' ') ptr++;
  113. const char *end = ptr;
  114. while (*end != '\n' && *end != '\r' && *end) end++;
  115. conn->setMonaKey(std::string(ptr, end-ptr), 0);
  116. }
  117. }
  118. size_t ret = conn->socketToClient->write(buffer, size*nitems);
  119. //fprintf(stderr,"%s",buffer);
  120. if(!memcmp(buffer,"\r\n",2)) {
  121. conn->status = 1;
  122. }
  123. return ret;
  124. }
  125. }
  126. static size_t header_callback_bbscgi(char *buffer, size_t size, size_t nitems, void *userdata)
  127. {
  128. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  129. if(conn->status > 0) return size*nitems;
  130. else if(conn->status == -1) {
  131. if(!memcmp(buffer,"\r\n",2)) {
  132. conn->status = 0;
  133. }
  134. return size*nitems;
  135. }
  136. if(!strncasecmp("HTTP/", buffer, 5) && !strchr(buffer, ':')) {
  137. const char *ptr = buffer + 5;
  138. const char *end = buffer + size*nitems;
  139. while(*ptr != ' ' && ptr < end) ptr++;
  140. while(*ptr == ' ' && ptr < end) ptr++;
  141. if(ptr < end) {
  142. int code = atoi(ptr);
  143. if (code == 100) {
  144. conn->status = -1;
  145. return size*nitems;
  146. }
  147. }
  148. }
  149. if(!strncasecmp("Connection",buffer,10)) {
  150. conn->responseHeaders.append("Connection: Close\r\n");
  151. return size*nitems;
  152. }
  153. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  154. if(allow_chunked && strstr(buffer+19,"chunked")) {
  155. conn->chunked = true;
  156. //fprintf(stderr,"%s",buffer);
  157. conn->responseHeaders.append(buffer, size*nitems);
  158. }
  159. return size*nitems;
  160. }
  161. else if(conn->force5ch && !strncasecmp("Set-Cookie:",buffer,11)) {
  162. char *ptr = (char *)memmem_priv(buffer,size*nitems,"domain=",7);
  163. if(ptr) {
  164. char *end = ptr;
  165. while(*end != ';' && *end != '\r') end++;
  166. ptr = (char *)memmem_priv(ptr,end-ptr,"5ch.net",7);
  167. if(ptr) {
  168. conn->responseHeaders.append(buffer, ptr-buffer);
  169. conn->responseHeaders.append(1, '2');
  170. conn->responseHeaders.append(ptr+1, size*nitems-(ptr+1-buffer));
  171. return size*nitems;
  172. }
  173. }
  174. conn->responseHeaders.append(buffer, size*nitems);
  175. return size*nitems;
  176. }
  177. else {
  178. if (!strncasecmp("X-Chx-Error:", buffer, 12)) {
  179. const char *ptr = buffer + 12;
  180. while (*ptr == ' ') ptr++;
  181. log_printf(0, "Posting to bbs.cgi has been cancelled. Reason: %s", ptr);
  182. if (*ptr == 'E') {
  183. int code = atoi(ptr+1);
  184. if ((code >= 3310 && code <= 3311) || /* inconsistent with User-Agent or something? */
  185. (code >= 3320 && code <= 3324) || /* key expiration? */
  186. (code >= 3390 && code <= 3392)) /* 3390: BAN, 3391-3392: key expiration? */
  187. {
  188. conn->setMonaKey("", code);
  189. conn->status = 2;
  190. return 0;
  191. }
  192. }
  193. }
  194. else if (!strncasecmp("X-MonaKey:", buffer, 10)) {
  195. const char *ptr = buffer + 10;
  196. while (*ptr == ' ') ptr++;
  197. const char *end = ptr;
  198. while (*end != '\n' && *end != '\r' && *end) end++;
  199. conn->setMonaKey(std::string(ptr, end-ptr), 0);
  200. }
  201. conn->responseHeaders.append(buffer, size*nitems);
  202. //fprintf(stderr,"%s",buffer);
  203. if(!memcmp(buffer,"\r\n",2)) {
  204. conn->socketToClient->write(conn->responseHeaders.data(), conn->responseHeaders.size());
  205. conn->status = 1;
  206. }
  207. return size*nitems;
  208. }
  209. }
  210. static size_t write_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  211. {
  212. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  213. if(conn->chunked) {
  214. char buf[64];
  215. snprintf(buf, 64, "%lx\r\n", size*nitems);
  216. conn->socketToClient->write(buf, strlen(buf));
  217. }
  218. size_t ret = conn->socketToClient->write(buffer, size*nitems);
  219. if(conn->chunked) conn->socketToClient->writeString("\r\n");
  220. return ret;
  221. }
  222. static size_t header_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  223. {
  224. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  225. if(!strncasecmp("Connection",buffer,10)) {
  226. data->appendBytes("Connection: Close\r\n",19);
  227. return size*nitems;
  228. }
  229. else if(!strncasecmp("Transfer-Encoding",buffer,17)) {
  230. return size*nitems;
  231. }
  232. else {
  233. data->appendBytes(buffer,size*nitems);
  234. return size*nitems;
  235. }
  236. }
  237. static size_t write_callback_download(char *buffer, size_t size, size_t nitems, void *userdata)
  238. {
  239. DataStorage *data = reinterpret_cast<DataStorage *>(userdata);
  240. size_t downloaded = size*nitems;
  241. data->appendBytes(buffer, downloaded);
  242. return downloaded;
  243. }
  244. static size_t read_callback_proxy(char *buffer, size_t size, size_t nitems, void *userdata)
  245. {
  246. BBS2chProxyConnection *conn = reinterpret_cast<BBS2chProxyConnection *>(userdata);
  247. if(size*nitems < 1) return 0;
  248. if(conn->content_length) {
  249. size_t bytesToRead = conn->content_length;
  250. if(size*nitems < conn->content_length) bytesToRead = size*nitems;
  251. size_t ret = conn->socketToClient->read(buffer, bytesToRead);
  252. conn->content_length -= ret;
  253. return ret;
  254. }
  255. return 0;
  256. }
  257. static void sendBasicHeaders(int respCode, const char *respMsg, IBBS2chProxySocket *socket)
  258. {
  259. char date[256];
  260. time_t now = time(0);
  261. strftime(date, 256, httpTimestampFmt, gmtime(&now));
  262. std::ostringstream ss;
  263. ss << "HTTP/1.1 " << respCode << " " << respMsg << "\r\n";
  264. if(0 >= socket->writeString(ss.str())) return;
  265. if(0 >= socket->writeString("Connection: Close\r\n")) return;
  266. if(0 >= socket->writeString("Server: 2ch Proxy\r\n")) return;
  267. if(0 >= socket->writeString(std::string("Date: ") + date + "\r\n")) return;
  268. }
  269. static void sendResponse(int respCode, const char *respMsg, IBBS2chProxySocket *socket)
  270. {
  271. sendBasicHeaders(respCode, respMsg, socket);
  272. if(0 >= socket->writeString("Content-Type: text/plain; charset=UTF-8\r\n")) return;
  273. if(0 >= socket->writeString("\r\n")) return;
  274. if(respCode >= 400) {
  275. if(0 >= socket->writeString("   ∧_∧   / ̄ ̄ ̄ ̄ ̄\n")) return;
  276. if(0 >= socket->writeString(std::string("  ( ´∀`)< ") + respMsg + "\n")) return;
  277. if(0 >= socket->writeString("  (    ) \_____\n")) return;
  278. if(0 >= socket->writeString("   │ │ │\n")) return;
  279. if(0 >= socket->writeString("  (__)_)\n")) return;
  280. }
  281. }
  282. static double getCurrentTime(void)
  283. {
  284. #ifdef _WIN32
  285. FILETIME ft;
  286. GetSystemTimeAsFileTime(&ft);
  287. unsigned long long now = ft.dwHighDateTime;
  288. now <<= 32;
  289. now |= ft.dwLowDateTime;
  290. return (double)(now * 1e-7 - 11644473600.0);
  291. #else
  292. struct timeval tv;
  293. gettimeofday(&tv, NULL);
  294. return (double)(tv.tv_sec + tv.tv_usec * 1e-6);
  295. #endif
  296. }
  297. static std::string decodeURIComponent(const char *input, size_t inputLength, bool decodePlus)
  298. {
  299. std::string output;
  300. for (int i=0;i<inputLength;i++) {
  301. if (input[i] == '%') {
  302. if (i < inputLength - 2) {
  303. char from[3];
  304. char *end;
  305. from[0] = input[i+1];
  306. from[1] = input[i+2];
  307. from[2] = 0;
  308. unsigned long n = strtoul(from, &end, 16);
  309. if (n < 256 && end == from+2) {
  310. output.append(1, n);
  311. i += 2;
  312. continue;
  313. }
  314. }
  315. }
  316. else if (decodePlus && input[i] == '+') {
  317. output.append(" ");
  318. continue;
  319. }
  320. output.append(1, input[i]);
  321. }
  322. return output;
  323. }
  324. static std::string encodeURIComponent(const char *input, size_t inputLength, bool spaceAsPlus)
  325. {
  326. std::string output;
  327. for (int i=0;i<inputLength;i++) {
  328. unsigned char c = (unsigned char)input[i];
  329. if ((c >= '0' && c <= '9') ||
  330. (c >= 'A' && c <= 'Z') ||
  331. (c >= 'a' && c <= 'z') ||
  332. (c == '*') || (c == '-') || (c == '.') || (c == '_')) {
  333. output.append(1, c);
  334. }
  335. else if (c == ' ' && spaceAsPlus) {
  336. output.append("+");
  337. }
  338. else {
  339. char percentEncoded[4];
  340. snprintf(percentEncoded, 4, "%%%02X", c);
  341. output.append(percentEncoded);
  342. }
  343. }
  344. return output;
  345. }
  346. static void appendPostSignature(const char *body, const std::string &userAgent, const std::string &monaKey, BBS2chProxyHttpHeaders *headers)
  347. {
  348. std::map<std::string, std::string> fields;
  349. const char *ptr = body;
  350. while (1) {
  351. const char *tmp = ptr;
  352. while (*tmp != '=' && *tmp != 0) tmp++;
  353. if (*tmp == 0) break;
  354. std::string key(ptr, tmp-ptr);
  355. tmp++;
  356. ptr = tmp;
  357. while (*tmp != '&' && *tmp != 0) tmp++;
  358. fields.insert(std::make_pair(key, decodeURIComponent(ptr, tmp-ptr, true)));
  359. if (*tmp == 0) break;
  360. ptr = tmp + 1;
  361. }
  362. char nonce[32];
  363. snprintf(nonce, 32, "%.3f", getCurrentTime());
  364. std::string message;
  365. message.append(fields["bbs"]);
  366. message.append("<>");
  367. message.append(fields["key"]);
  368. message.append("<>");
  369. message.append(fields["time"]);
  370. message.append("<>");
  371. message.append(fields["FROM"]);
  372. message.append("<>");
  373. message.append(fields["mail"]);
  374. message.append("<>");
  375. message.append(fields["MESSAGE"]);
  376. message.append("<>");
  377. message.append(fields["subject"]);
  378. message.append("<>");
  379. message.append(userAgent);
  380. message.append("<>");
  381. message.append(monaKey);
  382. message.append("<>");
  383. message.append("<>");
  384. message.append(nonce);
  385. unsigned char digest[32];
  386. char digestStr[65];
  387. static const char *table = "0123456789abcdef";
  388. proxy2ch_HMAC_SHA256(hmacKey, strlen(hmacKey), message.data(), message.length(), digest);
  389. for (int i=0; i<32; i++) {
  390. unsigned char c = digest[i];
  391. unsigned char upper = (c >> 4) & 0xf;
  392. unsigned char lower = c & 0xf;
  393. digestStr[i*2] = table[upper];
  394. digestStr[i*2+1] = table[lower];
  395. }
  396. digestStr[64] = 0;
  397. headers->set("X-APIKey", appKey);
  398. log_printf(1, "Appended header \"X-APIKey: %s\"\n", appKey);
  399. headers->set("X-PostSig", digestStr);
  400. log_printf(1, "Appended header \"X-PostSig: %s\"\n", digestStr);
  401. headers->set("X-PostNonce", nonce);
  402. log_printf(1, "Appended header \"X-PostNonce: %s\"\n", nonce);
  403. headers->set("X-MonaKey", monaKey);
  404. log_printf(1, "Appended header \"X-MonaKey: %s\"\n", monaKey.c_str());
  405. }
  406. #ifdef _WIN32
  407. const char * strp_weekdays[] =
  408. { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
  409. const char * strp_monthnames[] =
  410. { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
  411. bool strp_atoi(const char * & s, int & result, int low, int high, int offset)
  412. {
  413. bool worked = false;
  414. char * end;
  415. unsigned long num = strtoul(s, & end, 10);
  416. if (num >= (unsigned long)low && num <= (unsigned long)high)
  417. {
  418. result = (int)(num + offset);
  419. s = end;
  420. worked = true;
  421. }
  422. return worked;
  423. }
  424. char * strptime(const char *s, const char *format, struct tm *tm)
  425. {
  426. bool working = true;
  427. while (working && *format && *s)
  428. {
  429. switch (*format)
  430. {
  431. case '%':
  432. {
  433. ++format;
  434. switch (*format)
  435. {
  436. case 'a':
  437. case 'A': // weekday name
  438. tm->tm_wday = -1;
  439. working = false;
  440. for (size_t i = 0; i < 7; ++ i)
  441. {
  442. size_t len = strlen(strp_weekdays[i]);
  443. if (!strnicmp(strp_weekdays[i], s, len))
  444. {
  445. tm->tm_wday = i;
  446. s += len;
  447. working = true;
  448. break;
  449. }
  450. else if (!strnicmp(strp_weekdays[i], s, 3))
  451. {
  452. tm->tm_wday = i;
  453. s += 3;
  454. working = true;
  455. break;
  456. }
  457. }
  458. break;
  459. case 'b':
  460. case 'B':
  461. case 'h': // month name
  462. tm->tm_mon = -1;
  463. working = false;
  464. for (size_t i = 0; i < 12; ++ i)
  465. {
  466. size_t len = strlen(strp_monthnames[i]);
  467. if (!strnicmp(strp_monthnames[i], s, len))
  468. {
  469. tm->tm_mon = i;
  470. s += len;
  471. working = true;
  472. break;
  473. }
  474. else if (!strnicmp(strp_monthnames[i], s, 3))
  475. {
  476. tm->tm_mon = i;
  477. s += 3;
  478. working = true;
  479. break;
  480. }
  481. }
  482. break;
  483. case 'd':
  484. case 'e': // day of month number
  485. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  486. break;
  487. case 'D': // %m/%d/%y
  488. {
  489. const char * s_save = s;
  490. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  491. if (working && *s == '/')
  492. {
  493. ++ s;
  494. working = strp_atoi(s, tm->tm_mday, 1, 31, 0);
  495. if (working && *s == '/')
  496. {
  497. ++ s;
  498. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  499. if (working && tm->tm_year < 69)
  500. tm->tm_year += 100;
  501. }
  502. }
  503. if (!working)
  504. s = s_save;
  505. }
  506. break;
  507. case 'H': // hour
  508. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  509. break;
  510. case 'I': // hour 12-hour clock
  511. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  512. break;
  513. case 'j': // day number of year
  514. working = strp_atoi(s, tm->tm_yday, 1, 366, -1);
  515. break;
  516. case 'm': // month number
  517. working = strp_atoi(s, tm->tm_mon, 1, 12, -1);
  518. break;
  519. case 'M': // minute
  520. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  521. break;
  522. case 'n': // arbitrary whitespace
  523. case 't':
  524. while (isspace((int)*s))
  525. ++s;
  526. break;
  527. case 'p': // am / pm
  528. if (!strnicmp(s, "am", 2))
  529. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  530. if (tm->tm_hour == 12) // 12 am == 00 hours
  531. tm->tm_hour = 0;
  532. }
  533. else if (!strnicmp(s, "pm", 2))
  534. {
  535. if (tm->tm_hour < 12) // 12 pm == 12 hours
  536. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  537. }
  538. else
  539. working = false;
  540. break;
  541. case 'r': // 12 hour clock %I:%M:%S %p
  542. {
  543. const char * s_save = s;
  544. working = strp_atoi(s, tm->tm_hour, 1, 12, 0);
  545. if (working && *s == ':')
  546. {
  547. ++ s;
  548. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  549. if (working && *s == ':')
  550. {
  551. ++ s;
  552. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  553. if (working && isspace((int)*s))
  554. {
  555. ++ s;
  556. while (isspace((int)*s))
  557. ++s;
  558. if (!strnicmp(s, "am", 2))
  559. { // the hour will be 1 -> 12 maps to 12 am, 1 am .. 11 am, 12 noon 12 pm .. 11 pm
  560. if (tm->tm_hour == 12) // 12 am == 00 hours
  561. tm->tm_hour = 0;
  562. }
  563. else if (!strnicmp(s, "pm", 2))
  564. {
  565. if (tm->tm_hour < 12) // 12 pm == 12 hours
  566. tm->tm_hour += 12; // 1 pm -> 13 hours, 11 pm -> 23 hours
  567. }
  568. else
  569. working = false;
  570. }
  571. }
  572. }
  573. if (!working)
  574. s = s_save;
  575. }
  576. break;
  577. case 'R': // %H:%M
  578. {
  579. const char * s_save = s;
  580. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  581. if (working && *s == ':')
  582. {
  583. ++ s;
  584. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  585. }
  586. if (!working)
  587. s = s_save;
  588. }
  589. break;
  590. case 'S': // seconds
  591. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  592. break;
  593. case 'T': // %H:%M:%S
  594. {
  595. const char * s_save = s;
  596. working = strp_atoi(s, tm->tm_hour, 0, 23, 0);
  597. if (working && *s == ':')
  598. {
  599. ++ s;
  600. working = strp_atoi(s, tm->tm_min, 0, 59, 0);
  601. if (working && *s == ':')
  602. {
  603. ++ s;
  604. working = strp_atoi(s, tm->tm_sec, 0, 60, 0);
  605. }
  606. }
  607. if (!working)
  608. s = s_save;
  609. }
  610. break;
  611. case 'w': // weekday number 0->6 sunday->saturday
  612. working = strp_atoi(s, tm->tm_wday, 0, 6, 0);
  613. break;
  614. case 'Y': // year
  615. working = strp_atoi(s, tm->tm_year, 1900, 65535, -1900);
  616. break;
  617. case 'y': // 2-digit year
  618. working = strp_atoi(s, tm->tm_year, 0, 99, 0);
  619. if (working && tm->tm_year < 69)
  620. tm->tm_year += 100;
  621. break;
  622. case '%': // escaped
  623. if (*s != '%')
  624. working = false;
  625. ++s;
  626. break;
  627. default:
  628. working = false;
  629. }
  630. }
  631. break;
  632. case ' ':
  633. case '\t':
  634. case '\r':
  635. case '\n':
  636. case '\f':
  637. case '\v':
  638. // zero or more whitespaces:
  639. while (isspace((int)*s))
  640. ++ s;
  641. break;
  642. default:
  643. // match character
  644. if (*s != *format)
  645. working = false;
  646. else
  647. ++s;
  648. break;
  649. }
  650. ++format;
  651. }
  652. return (working?(char *)s:0);
  653. }
  654. #endif