|
| 1 | +#define WIN32_LEAN_AND_MEAN |
| 2 | +#include <windows.h> |
| 3 | +#include <wininet.h> |
| 4 | +#include <string.h> |
| 5 | +#include "lib.h" |
| 6 | + |
| 7 | +char rot13c(char c) |
| 8 | +{ |
| 9 | + char u[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 10 | + char l[] = "abcdefghijklmnopqrstuvwxyz"; |
| 11 | + char *p; |
| 12 | + |
| 13 | + if ((p = xstrchr(u, c)) != NULL) |
| 14 | + return u[((p-u) + 13) % 26]; |
| 15 | + else if ((p = xstrchr(l, c)) != NULL) |
| 16 | + return l[((p-l) + 13) % 26]; |
| 17 | + else |
| 18 | + return c; |
| 19 | +} |
| 20 | + |
| 21 | +void rot13(char *buf, const char *in) |
| 22 | +{ |
| 23 | + while (*in) |
| 24 | + *buf++ = rot13c(*in++); |
| 25 | + *buf = 0; |
| 26 | +} |
| 27 | + |
| 28 | +void mk_smtpdate(FILETIME *in_ft, char *buf) |
| 29 | +{ |
| 30 | + SYSTEMTIME t; |
| 31 | + TIME_ZONE_INFORMATION tmz_info; |
| 32 | + DWORD daylight_flag; int utc_offs, utc_offs_u; |
| 33 | + LPSTR weekdays[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
| 34 | + LPSTR months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 35 | + |
| 36 | + if (in_ft == NULL) { |
| 37 | + GetLocalTime(&t); |
| 38 | + } else { |
| 39 | + FILETIME lft; |
| 40 | + FileTimeToLocalFileTime(in_ft, &lft); |
| 41 | + FileTimeToSystemTime(&lft, &t); |
| 42 | + } |
| 43 | + |
| 44 | + tmz_info.Bias = 0; |
| 45 | + daylight_flag = GetTimeZoneInformation(&tmz_info); |
| 46 | + |
| 47 | + utc_offs = tmz_info.Bias; |
| 48 | + if (daylight_flag == TIME_ZONE_ID_DAYLIGHT) utc_offs += tmz_info.DaylightBias; |
| 49 | + utc_offs = -utc_offs; |
| 50 | + utc_offs_u = (utc_offs >= 0) ? utc_offs : -utc_offs; |
| 51 | + |
| 52 | + if (t.wDayOfWeek > 6) t.wDayOfWeek = 6; |
| 53 | + if (t.wMonth == 0) t.wMonth = 1; |
| 54 | + if (t.wMonth > 12) t.wMonth = 12; |
| 55 | + |
| 56 | + wsprintf(buf, |
| 57 | + "%s, %u %s %u %.2u:%.2u:%.2u %s%.2u%.2u", |
| 58 | + weekdays[t.wDayOfWeek], t.wDay, |
| 59 | + months[t.wMonth-1], t.wYear, |
| 60 | + t.wHour, t.wMinute, t.wSecond, |
| 61 | + (utc_offs >= 0) ? "+" : "-", |
| 62 | + utc_offs_u / 60, utc_offs_u % 60 |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +static DWORD xrand16_seed; |
| 67 | + |
| 68 | +void xrand_init(void) |
| 69 | +{ |
| 70 | + xrand16_seed = GetTickCount(); |
| 71 | +} |
| 72 | + |
| 73 | +WORD xrand16(void) |
| 74 | +{ |
| 75 | + xrand16_seed = 0x015a4e35L * xrand16_seed + 1L; |
| 76 | + return ((WORD)(xrand16_seed >> 16L) & (WORD)0xffff); |
| 77 | +} |
| 78 | + |
| 79 | +DWORD xrand32(void) |
| 80 | +{ |
| 81 | + return xrand16() | (xrand16() << 16); |
| 82 | +} |
| 83 | + |
| 84 | +char *xstrstr(const char *str, const char *pat) |
| 85 | +{ |
| 86 | + const char *p, *q; |
| 87 | + for (; *str; str++) { |
| 88 | + for (p=str, q=pat; *p && *q; p++, q++) |
| 89 | + if (*p != *q) break; |
| 90 | + if (p == q || *q == 0) return (char *)str; |
| 91 | + } |
| 92 | + return NULL; |
| 93 | +} |
| 94 | + |
| 95 | +char *xstrrchr(const char *str, char ch) |
| 96 | +{ |
| 97 | + register char *start = (char *)str; |
| 98 | + while (*str++); |
| 99 | + while (--str != start && *str != ch); |
| 100 | + if (*str == (char)ch) return((char *)str); |
| 101 | + return NULL; |
| 102 | +} |
| 103 | + |
| 104 | +char *xstrchr(const char *str, char ch) |
| 105 | +{ |
| 106 | + while (*str && *str != ch) str++; |
| 107 | + return (*str == ch) ? (char *)str : NULL; |
| 108 | +} |
| 109 | + |
| 110 | +int xsystem(char *cmd, int wait) |
| 111 | +{ |
| 112 | + PROCESS_INFORMATION pi; |
| 113 | + STARTUPINFO si; |
| 114 | + |
| 115 | + ZeroMemory(&si, sizeof(si)); |
| 116 | + si.cb = sizeof(si); |
| 117 | + si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEOFFFEEDBACK; |
| 118 | + si.wShowWindow = SW_HIDE; |
| 119 | + |
| 120 | + if (CreateProcess(0, cmd, 0, 0, TRUE, 0, 0, 0, &si, &pi) == 0) |
| 121 | + return 1; /* FAILED */ |
| 122 | + |
| 123 | + if (wait) { |
| 124 | + WaitForSingleObject(pi.hProcess, INFINITE); |
| 125 | + CloseHandle(pi.hThread); |
| 126 | + CloseHandle(pi.hProcess); |
| 127 | + } |
| 128 | + |
| 129 | + return 0; /* SUCCESS */ |
| 130 | +} |
| 131 | + |
| 132 | +int xmemcmpi(unsigned char *p, unsigned char *q, int len) |
| 133 | +{ |
| 134 | + while (len--) |
| 135 | + if (tolower(*p++) != tolower(*q++)) return 1; |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | +int xstrncmp(const char *first, const char *last, size_t count) |
| 140 | +{ |
| 141 | + if (!count) |
| 142 | + return(0); |
| 143 | + |
| 144 | + while (--count && *first && *first == *last) |
| 145 | + { |
| 146 | + first++; |
| 147 | + last++; |
| 148 | + } |
| 149 | + |
| 150 | + return (*(unsigned char *)first - *(unsigned char *)last); |
| 151 | +} |
| 152 | + |
| 153 | +/* "&#<number>" -> ascii */ |
| 154 | +int html_replace(char *str) |
| 155 | +{ |
| 156 | + char tmp[20], *mv_from, *mv_to; |
| 157 | + int i, j, amp_start, amp_end, amp_len, charcode, chr_len, mv_len; |
| 158 | + int found; |
| 159 | + |
| 160 | + for (i=0,found=0; str[i]; i++) { |
| 161 | + if (str[i] != '&') continue; |
| 162 | + amp_start = i; |
| 163 | + if (str[++i] != '#') continue; |
| 164 | + for (j=0,i++; j<(sizeof(tmp)-5);) { |
| 165 | + if (!isdigit(str[i])) break; |
| 166 | + tmp[j++] = str[i++]; |
| 167 | + } |
| 168 | + tmp[j] = 0; |
| 169 | + for (charcode=0,j=0; tmp[j]; j++) |
| 170 | + charcode = charcode * 10 + (tmp[j] - '0'); |
| 171 | + |
| 172 | + if (str[i] == ';') i++; |
| 173 | + amp_end = i; |
| 174 | + |
| 175 | + if (charcode <= 0) continue; |
| 176 | + ZeroMemory(tmp, sizeof(tmp)); |
| 177 | + if (charcode < 256) { |
| 178 | + tmp[0] = charcode; |
| 179 | + tmp[1] = 0; |
| 180 | + } else { |
| 181 | + WideCharToMultiByte(CP_ACP, 0, (WCHAR*)&charcode, 1, tmp, sizeof(tmp), NULL, NULL); |
| 182 | + } |
| 183 | + if (tmp[0] == 0) continue; |
| 184 | + |
| 185 | + amp_len = amp_end - amp_start; |
| 186 | + chr_len = lstrlen(tmp); |
| 187 | + |
| 188 | + if (amp_len != chr_len) { |
| 189 | + mv_from = str + amp_start + amp_len; |
| 190 | + mv_to = str + amp_start + chr_len; |
| 191 | + mv_len = (str + lstrlen(str)) - mv_from + 1; |
| 192 | + if (mv_to < mv_from) { |
| 193 | + for (j=0; j<mv_len; j++) *mv_to++ = *mv_from++; |
| 194 | + } else { |
| 195 | + for (j=mv_len-1; j>=0; j--) mv_to[j] = mv_from[j]; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + memcpy(str + amp_start, tmp, lstrlen(tmp)); |
| 200 | + i = amp_start; |
| 201 | + found++; |
| 202 | + } |
| 203 | + return found; |
| 204 | +} |
| 205 | + |
| 206 | +/* urldecode algorithm */ |
| 207 | +/* useful for <a href="mailto:xxx%40xxx"> tags */ |
| 208 | +int html_replace2(char *str) |
| 209 | +{ |
| 210 | + char tmp[20], *mv_from, *mv_to; |
| 211 | + int i, j, amp_start, amp_end, amp_len, charcode, chr_len, mv_len; |
| 212 | + int found; |
| 213 | + |
| 214 | + for (i=0,found=0; str[i]; i++) { |
| 215 | + if (str[i] != '%') continue; |
| 216 | + amp_start = i; |
| 217 | + if (!isxdigit(str[i+1])) continue; |
| 218 | + tmp[0] = toupper(str[++i]); |
| 219 | + if (!isxdigit(str[i+1])) continue; |
| 220 | + tmp[1] = toupper(str[++i]); |
| 221 | + tmp[2] = 0; |
| 222 | + amp_end = ++i; |
| 223 | + |
| 224 | + charcode = ((tmp[1] >= 'A') && (tmp[1] <= 'Z')) ? 10+tmp[1]-'A' : tmp[1]-'0'; |
| 225 | + charcode += (((tmp[0] >= 'A') && (tmp[0] <= 'Z')) ? 10+tmp[0]-'A' : tmp[0]-'0') << 4; |
| 226 | + |
| 227 | + if (charcode <= 0) continue; |
| 228 | + tmp[0] = charcode; |
| 229 | + tmp[1] = 0; |
| 230 | + |
| 231 | + amp_len = amp_end - amp_start; |
| 232 | + chr_len = lstrlen(tmp); |
| 233 | + |
| 234 | + if (amp_len != chr_len) { |
| 235 | + mv_from = str + amp_start + amp_len; |
| 236 | + mv_to = str + amp_start + chr_len; |
| 237 | + mv_len = (str + lstrlen(str)) - mv_from + 1; |
| 238 | + if (mv_to < mv_from) { |
| 239 | + for (j=0; j<mv_len; j++) *mv_to++ = *mv_from++; |
| 240 | + } else { |
| 241 | + for (j=mv_len-1; j>=0; j--) mv_to[j] = mv_from[j]; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + memcpy(str + amp_start, tmp, lstrlen(tmp)); |
| 246 | + i = amp_start; |
| 247 | + found++; |
| 248 | + } |
| 249 | + return found; |
| 250 | +} |
| 251 | + |
| 252 | +typedef BOOL (WINAPI *WININET_GETCONNECTEDSTATE)(LPDWORD lpdwFlags, DWORD dwReserved); |
| 253 | + |
| 254 | +/* Returns: 0=offline, 1=online, 2=don't know */ |
| 255 | +int is_online(void) |
| 256 | +{ |
| 257 | + WININET_GETCONNECTEDSTATE pInternetGetConnectedState; |
| 258 | + HINSTANCE hWinInet; |
| 259 | + DWORD igcs_flags; |
| 260 | + char tmp[64]; |
| 261 | + |
| 262 | + rot13(tmp, "jvavarg.qyy"); /* "wininet.dll" */ |
| 263 | + hWinInet = GetModuleHandle(tmp); |
| 264 | + if (hWinInet == NULL || hWinInet == INVALID_HANDLE_VALUE) { |
| 265 | + hWinInet = LoadLibrary(tmp); |
| 266 | + if (hWinInet == NULL || hWinInet == INVALID_HANDLE_VALUE) |
| 267 | + return 2; |
| 268 | + } |
| 269 | + |
| 270 | + rot13(tmp, "VagreargTrgPbaarpgrqFgngr"); /* "InternetGetConnectedState" */ |
| 271 | + pInternetGetConnectedState = (WININET_GETCONNECTEDSTATE)GetProcAddress(hWinInet, tmp); |
| 272 | + if (pInternetGetConnectedState == NULL) |
| 273 | + return 2; |
| 274 | + |
| 275 | + return (pInternetGetConnectedState(&igcs_flags, 0) == 0) ? 0 : 1; |
| 276 | +} |
| 277 | + |
| 278 | +int cat_wsprintf(LPTSTR lpOutput, LPCTSTR lpFormat, ...) |
| 279 | +{ |
| 280 | + register int ret; |
| 281 | + va_list arglist; |
| 282 | + va_start(arglist, lpFormat); |
| 283 | + ret = wvsprintf(lpOutput + lstrlen(lpOutput), lpFormat, arglist); |
| 284 | + va_end(arglist); |
| 285 | + return ret; |
| 286 | +} |
0 commit comments